drop EnterWindow events to keep focus
[awesome.git] / statusbar.c
blob28d800de8bb5f477701e902d14b9fed5bdf72713
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;
65 void
66 statusbar_draw(Statusbar *statusbar)
68 int phys_screen = get_phys_screen(statusbar->screen);
69 Widget *widget, *last_drawn = NULL;
70 int left = 0, right = 0;
72 /* don't waste our time */
73 if(statusbar->position == Off)
74 return;
76 XFreePixmap(globalconf.display, statusbar->drawable);
78 DrawCtx *ctx = draw_get_context(phys_screen,
79 statusbar->width,
80 statusbar->height);
82 draw_rectangle(ctx, 0, 0, statusbar->width, statusbar->height, True,
83 globalconf.screens[statusbar->screen].colors_normal[ColBG]);
85 for(widget = statusbar->widgets; widget; widget = widget->next)
86 if (widget->alignment == AlignLeft)
88 widget->cache.needs_update = False;
89 left += widget->draw(widget, ctx, left, (left + right));
92 /* renders right widget from last to first */
93 for(widget = statusbar->widgets; widget; widget = widget->next)
94 if (widget->alignment == AlignRight && last_drawn == widget->next)
96 widget->cache.needs_update = False;
97 right += widget->draw(widget, ctx, right, (left + right));
98 last_drawn = widget;
99 widget = statusbar->widgets;
102 for(widget = statusbar->widgets; widget; widget = widget->next)
103 if (widget->alignment == AlignFlex)
105 widget->cache.needs_update = False;
106 left += widget->draw(widget, ctx, left, (left + right));
109 if(statusbar->position == Right
110 || statusbar->position == Left)
112 if(statusbar->position == Right)
113 statusbar->drawable = draw_rotate(ctx, phys_screen, M_PI_2, statusbar->height, 0);
114 else
115 statusbar->drawable = draw_rotate(ctx, phys_screen, - M_PI_2, 0, statusbar->width);
117 draw_free_context(ctx);
119 else
121 statusbar->drawable = ctx->drawable;
122 /* just delete the struct, don't delete the drawable */
123 p_delete(&ctx);
126 statusbar_display(statusbar);
129 void
130 statusbar_display(Statusbar *statusbar)
132 int phys_screen = get_phys_screen(statusbar->screen);
134 /* don't waste our time */
135 if(statusbar->position == Off)
136 return;
138 if(statusbar->position == Right
139 || statusbar->position == Left)
140 XCopyArea(globalconf.display, statusbar->drawable,
141 statusbar->window,
142 DefaultGC(globalconf.display, phys_screen), 0, 0,
143 statusbar->height,
144 statusbar->width, 0, 0);
145 else
146 XCopyArea(globalconf.display, statusbar->drawable,
147 statusbar->window,
148 DefaultGC(globalconf.display, phys_screen), 0, 0,
149 statusbar->width, statusbar->height, 0, 0);
152 void
153 statusbar_init(Statusbar *statusbar, int screen)
155 Widget *widget;
156 XSetWindowAttributes wa;
157 int phys_screen = get_phys_screen(screen);
158 Area area = get_screen_area(statusbar->screen,
159 NULL,
160 &globalconf.screens[screen].padding);
162 if(statusbar->height <= 0)
164 /* 1.5 as default factor, it fits nice but no one know why */
165 statusbar->height = globalconf.screens[screen].font->height * 1.5;
167 for(widget = statusbar->widgets; widget; widget = widget->next)
168 if(widget->font)
169 statusbar->height = MAX(statusbar->height, widget->font->height * 1.5);
172 if(statusbar->width <= 0)
174 if(statusbar->position == Right || statusbar->position == Left)
175 statusbar->width = area.height;
176 else
177 statusbar->width = area.width;
180 statusbar->screen = screen;
182 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
183 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
184 wa.cursor = globalconf.cursor[CurNormal];
185 wa.override_redirect = 1;
186 wa.background_pixmap = ParentRelative;
187 wa.event_mask = ButtonPressMask | ExposureMask;
188 if(statusbar->dposition == Right || statusbar->dposition == Left)
189 statusbar->window = XCreateWindow(globalconf.display,
190 RootWindow(globalconf.display,
191 phys_screen),
192 0, 0,
193 statusbar->height,
194 statusbar->width,
196 DefaultDepth(globalconf.display,
197 phys_screen),
198 CopyFromParent,
199 DefaultVisual(globalconf.display,
200 phys_screen),
201 CWOverrideRedirect |
202 CWBackPixmap |
203 CWEventMask,
204 &wa);
205 else
206 statusbar->window = XCreateWindow(globalconf.display,
207 RootWindow(globalconf.display,
208 phys_screen),
209 0, 0,
210 statusbar->width,
211 statusbar->height,
213 DefaultDepth(globalconf.display,
214 phys_screen),
215 CopyFromParent,
216 DefaultVisual(globalconf.display,
217 phys_screen),
218 CWOverrideRedirect |
219 CWBackPixmap |
220 CWEventMask,
221 &wa);
223 statusbar->drawable = XCreatePixmap(globalconf.display,
224 RootWindow(globalconf.display, phys_screen),
225 statusbar->width, statusbar->height,
226 DefaultDepth(globalconf.display, phys_screen));
229 XDefineCursor(globalconf.display,
230 statusbar->window,
231 globalconf.cursor[CurNormal]);
233 widget_calculate_alignments(statusbar->widgets);
235 statusbar_update_position(statusbar);
236 XMapRaised(globalconf.display, statusbar->window);
238 statusbar_draw(statusbar);
241 void
242 statusbar_refresh()
244 int screen;
245 Statusbar *statusbar;
246 Widget *widget;
248 for(screen = 0; screen < globalconf.nscreens; screen++)
249 for(statusbar = globalconf.screens[screen].statusbar;
250 statusbar;
251 statusbar = statusbar->next)
252 for(widget = statusbar->widgets; widget; widget = widget->next)
253 if(widget->cache.needs_update)
255 statusbar_draw(statusbar);
256 break;
260 Position
261 statusbar_get_position_from_str(const char *pos)
263 if(!a_strncmp(pos, "off", 3))
264 return Off;
265 else if(!a_strncmp(pos, "bottom", 6))
266 return Bottom;
267 else if(!a_strncmp(pos, "right", 5))
268 return Right;
269 else if(!a_strncmp(pos, "left", 4))
270 return Left;
271 return Top;
274 static Statusbar *
275 get_statusbar_byname(int screen, const char *name)
277 Statusbar *sb;
279 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
280 if(!a_strcmp(sb->name, name))
281 return sb;
283 return NULL;
286 static void
287 statusbar_toggle(Statusbar *statusbar)
289 if(statusbar->position == Off)
290 statusbar->position = (statusbar->dposition == Off) ? Top : statusbar->dposition;
291 else
292 statusbar->position = Off;
294 statusbar_update_position(statusbar);
297 /** Toggle statusbar
298 * \param screen Screen ID
299 * \param arg statusbar name
300 * \ingroup ui_callback
302 void
303 uicb_statusbar_toggle(int screen, char *arg)
305 Statusbar *sb = get_statusbar_byname(screen, arg);
307 if(sb)
308 statusbar_toggle(sb);
309 else
310 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
311 statusbar_toggle(sb);
313 arrange(screen);
316 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80