cosmetic
[awesome.git] / statusbar.c
blob9aaaa7900ccdc8074b99c39cfea842ee9feea425
1 /*
2 * statusbar.c - statusbar functions
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <math.h>
25 #include "statusbar.h"
26 #include "screen.h"
27 #include "util.h"
28 #include "tag.h"
29 #include "widget.h"
31 extern AwesomeConf globalconf;
33 static void
34 statusbar_update_position(Statusbar *statusbar)
36 Area area = get_screen_area(statusbar->screen,
37 NULL,
38 &globalconf.screens[statusbar->screen].padding);
40 XMapRaised(globalconf.display, statusbar->window);
41 switch(statusbar->position)
43 default:
44 XMoveWindow(globalconf.display, statusbar->window,
45 area.x, area.y);
46 break;
47 case Left:
48 XMoveWindow(globalconf.display, statusbar->window,
49 area.x, (area.y + area.height) - statusbar->width);
50 break;
51 case Right:
52 XMoveWindow(globalconf.display, statusbar->window,
53 area.x + (area.width - statusbar->height), area.y);
54 break;
55 case Bottom:
56 XMoveWindow(globalconf.display, statusbar->window,
57 area.x, area.height - statusbar->height);
58 break;
59 case Off:
60 XUnmapWindow(globalconf.display, statusbar->window);
61 break;
63 XSync(globalconf.display, False);
66 void
67 statusbar_draw(Statusbar *statusbar)
69 int phys_screen = get_phys_screen(statusbar->screen);
70 Widget *widget, *last_drawn = NULL;
71 int left = 0, right = 0;
73 /* don't waste our time */
74 if(statusbar->position == Off)
75 return;
77 XFreePixmap(globalconf.display, statusbar->drawable);
79 DrawCtx *ctx = draw_get_context(phys_screen,
80 statusbar->width,
81 statusbar->height);
83 draw_rectangle(ctx, 0, 0, statusbar->width, statusbar->height, True,
84 globalconf.screens[statusbar->screen].colors_normal[ColBG]);
86 for(widget = statusbar->widgets; widget; widget = widget->next)
87 if (widget->alignment == AlignLeft)
89 widget->cache.needs_update = False;
90 left += widget->draw(widget, ctx, left, (left + right));
93 /* renders right widget from last to first */
94 for(widget = statusbar->widgets; widget; widget = widget->next)
95 if (widget->alignment == AlignRight && last_drawn == widget->next)
97 widget->cache.needs_update = False;
98 right += widget->draw(widget, ctx, right, (left + right));
99 last_drawn = widget;
100 widget = statusbar->widgets;
103 for(widget = statusbar->widgets; widget; widget = widget->next)
104 if (widget->alignment == AlignFlex)
106 widget->cache.needs_update = False;
107 left += widget->draw(widget, ctx, left, (left + right));
110 if(statusbar->position == Right
111 || statusbar->position == Left)
113 if(statusbar->position == Right)
114 statusbar->drawable = draw_rotate(ctx, phys_screen, M_PI_2, statusbar->height, 0);
115 else
116 statusbar->drawable = draw_rotate(ctx, phys_screen, - M_PI_2, 0, statusbar->width);
118 draw_free_context(ctx);
120 else
122 statusbar->drawable = ctx->drawable;
123 /* just delete the struct, don't delete the drawable */
124 p_delete(&ctx);
127 statusbar_display(statusbar);
130 void
131 statusbar_display(Statusbar *statusbar)
133 int phys_screen = get_phys_screen(statusbar->screen);
135 /* don't waste our time */
136 if(statusbar->position == Off)
137 return;
139 if(statusbar->position == Right
140 || statusbar->position == Left)
141 XCopyArea(globalconf.display, statusbar->drawable,
142 statusbar->window,
143 DefaultGC(globalconf.display, phys_screen), 0, 0,
144 statusbar->height,
145 statusbar->width, 0, 0);
146 else
147 XCopyArea(globalconf.display, statusbar->drawable,
148 statusbar->window,
149 DefaultGC(globalconf.display, phys_screen), 0, 0,
150 statusbar->width, statusbar->height, 0, 0);
153 void
154 statusbar_init(Statusbar *statusbar, int screen)
156 Widget *widget;
157 XSetWindowAttributes wa;
158 int phys_screen = get_phys_screen(screen);
159 Area area = get_screen_area(statusbar->screen,
160 NULL,
161 &globalconf.screens[screen].padding);
163 if(statusbar->height <= 0)
165 /* 1.5 as default factor, it fits nice but no one know why */
166 statusbar->height = globalconf.screens[screen].font->height * 1.5;
168 for(widget = statusbar->widgets; widget; widget = widget->next)
169 if(widget->font)
170 statusbar->height = MAX(statusbar->height, widget->font->height * 1.5);
173 if(statusbar->width <= 0)
175 if(statusbar->position == Right || statusbar->position == Left)
176 statusbar->width = area.height;
177 else
178 statusbar->width = area.width;
181 statusbar->screen = screen;
183 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
184 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
185 wa.cursor = globalconf.cursor[CurNormal];
186 wa.override_redirect = 1;
187 wa.background_pixmap = ParentRelative;
188 wa.event_mask = ButtonPressMask | ExposureMask;
189 if(statusbar->dposition == Right || statusbar->dposition == Left)
190 statusbar->window = XCreateWindow(globalconf.display,
191 RootWindow(globalconf.display,
192 phys_screen),
193 0, 0,
194 statusbar->height,
195 statusbar->width,
197 DefaultDepth(globalconf.display,
198 phys_screen),
199 CopyFromParent,
200 DefaultVisual(globalconf.display,
201 phys_screen),
202 CWOverrideRedirect |
203 CWBackPixmap |
204 CWEventMask,
205 &wa);
206 else
207 statusbar->window = XCreateWindow(globalconf.display,
208 RootWindow(globalconf.display,
209 phys_screen),
210 0, 0,
211 statusbar->width,
212 statusbar->height,
214 DefaultDepth(globalconf.display,
215 phys_screen),
216 CopyFromParent,
217 DefaultVisual(globalconf.display,
218 phys_screen),
219 CWOverrideRedirect |
220 CWBackPixmap |
221 CWEventMask,
222 &wa);
224 statusbar->drawable = XCreatePixmap(globalconf.display,
225 RootWindow(globalconf.display, phys_screen),
226 statusbar->width, statusbar->height,
227 DefaultDepth(globalconf.display, phys_screen));
230 XDefineCursor(globalconf.display,
231 statusbar->window,
232 globalconf.cursor[CurNormal]);
234 widget_calculate_alignments(statusbar->widgets);
236 statusbar_update_position(statusbar);
237 XMapRaised(globalconf.display, statusbar->window);
239 statusbar_draw(statusbar);
242 void
243 statusbar_refresh()
245 int screen;
246 Statusbar *statusbar;
247 Widget *widget;
249 for(screen = 0; screen < get_screen_count(); screen++)
250 for(statusbar = globalconf.screens[screen].statusbar;
251 statusbar;
252 statusbar = statusbar->next)
253 for(widget = statusbar->widgets; widget; widget = widget->next)
254 if(widget->cache.needs_update)
256 statusbar_draw(statusbar);
257 break;
261 Position
262 statusbar_get_position_from_str(const char *pos)
264 if(!a_strncmp(pos, "off", 3))
265 return Off;
266 else if(!a_strncmp(pos, "bottom", 6))
267 return Bottom;
268 else if(!a_strncmp(pos, "right", 5))
269 return Right;
270 else if(!a_strncmp(pos, "left", 4))
271 return Left;
272 return Top;
275 static Statusbar *
276 get_statusbar_byname(int screen, const char *name)
278 Statusbar *sb;
280 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
281 if(!a_strcmp(sb->name, name))
282 return sb;
284 return NULL;
287 static void
288 statusbar_toggle(Statusbar *statusbar)
290 if(statusbar->position == Off)
291 statusbar->position = (statusbar->dposition == Off) ? Top : statusbar->dposition;
292 else
293 statusbar->position = Off;
295 statusbar_update_position(statusbar);
298 /** Toggle statusbar
299 * \param screen Screen ID
300 * \param arg statusbar name
301 * \ingroup ui_callback
303 void
304 uicb_statusbar_toggle(int screen, char *arg)
306 Statusbar *sb = get_statusbar_byname(screen, arg);
308 if(sb)
309 statusbar_toggle(sb);
310 else
311 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
312 statusbar_toggle(sb);
314 arrange(screen);
317 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80