rename opts to awesome_opts
[awesome.git] / statusbar.c
blobf8ee1fce36be4d39f1c6eca5e39a13ca692afac7
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 "tag.h"
28 #include "widget.h"
29 #include "window.h"
31 extern AwesomeConf globalconf;
33 static void
34 statusbar_update_position(Statusbar *statusbar)
36 Area area;
38 XMapRaised(globalconf.display, statusbar->sw->window);
40 /* Top and Bottom Statusbar have prio */
41 if(statusbar->position == Top || statusbar->position == Bottom)
42 area = screen_get_area(statusbar->screen,
43 NULL,
44 &globalconf.screens[statusbar->screen].padding);
45 else
46 area = screen_get_area(statusbar->screen,
47 globalconf.screens[statusbar->screen].statusbar,
48 &globalconf.screens[statusbar->screen].padding);
50 switch(statusbar->position)
52 case Top:
53 simplewindow_move(statusbar->sw, area.x, area.y);
54 break;
55 case Bottom:
56 simplewindow_move(statusbar->sw, area.x, (area.y + area.height) - statusbar->sw->geometry.height);
57 break;
58 case Left:
59 simplewindow_move(statusbar->sw, area.x - statusbar->sw->geometry.width,
60 (area.y + area.height) - statusbar->sw->geometry.height);
61 break;
62 case Right:
63 simplewindow_move(statusbar->sw, area.x + area.width, area.y);
64 break;
65 case Off:
66 XUnmapWindow(globalconf.display, statusbar->sw->window);
67 break;
71 static void
72 statusbar_draw(Statusbar *statusbar)
74 int phys_screen = get_phys_screen(statusbar->screen);
75 Widget *widget, *last_drawn = NULL;
76 int left = 0, right = 0;
77 Area rectangle = { 0, 0, 0, 0, NULL };
78 Drawable d;
80 /* don't waste our time */
81 switch(statusbar->position)
83 case Off:
84 return;
85 break;
86 case Right:
87 case Left:
88 /* we need a new pixmap this way [ ] to render */
89 XFreePixmap(globalconf.display, statusbar->sw->drawable);
90 d = XCreatePixmap(globalconf.display,
91 RootWindow(globalconf.display, phys_screen),
92 statusbar->width, statusbar->height,
93 DefaultDepth(globalconf.display, phys_screen));
94 statusbar->sw->drawable = d;
95 break;
96 default:
97 break;
100 DrawCtx *ctx = draw_context_new(globalconf.display,
101 phys_screen,
102 statusbar->width,
103 statusbar->height,
104 statusbar->sw->drawable);
106 rectangle.width = statusbar->width;
107 rectangle.height = statusbar->height;
108 draw_rectangle(ctx, rectangle, True,
109 globalconf.screens[statusbar->screen].colors_normal[ColBG]);
111 for(widget = statusbar->widgets; widget; widget = widget->next)
112 if (widget->alignment == AlignLeft)
114 widget->cache.needs_update = False;
115 left += widget->draw(widget, ctx, left, (left + right));
118 /* renders right widget from last to first */
119 for(widget = statusbar->widgets; widget; widget = widget->next)
120 if (widget->alignment == AlignRight && last_drawn == widget->next)
122 widget->cache.needs_update = False;
123 right += widget->draw(widget, ctx, right, (left + right));
124 last_drawn = widget;
125 widget = statusbar->widgets;
128 for(widget = statusbar->widgets; widget; widget = widget->next)
129 if (widget->alignment == AlignFlex)
131 widget->cache.needs_update = False;
132 left += widget->draw(widget, ctx, left, (left + right));
135 switch(statusbar->position)
137 case Right:
138 d = draw_rotate(ctx, phys_screen, M_PI_2,
139 statusbar->height, 0);
140 XFreePixmap(globalconf.display, statusbar->sw->drawable);
141 statusbar->sw->drawable = d;
142 break;
143 case Left:
144 d = draw_rotate(ctx, phys_screen, - M_PI_2,
145 0, statusbar->width);
146 XFreePixmap(globalconf.display, statusbar->sw->drawable);
147 statusbar->sw->drawable = d;
148 break;
149 default:
150 break;
153 p_delete(&ctx);
155 statusbar_display(statusbar);
158 void
159 statusbar_display(Statusbar *statusbar)
161 /* don't waste our time */
162 if(statusbar->position != Off)
163 simplewindow_refresh_drawable(statusbar->sw, get_phys_screen(statusbar->screen));
166 void
167 statusbar_preinit(Statusbar *statusbar)
169 Widget *widget;
171 if(statusbar->height <= 0)
173 /* 1.5 as default factor, it fits nice but no one know why */
174 statusbar->height = globalconf.screens[statusbar->screen].font->height * 1.5;
176 for(widget = statusbar->widgets; widget; widget = widget->next)
177 if(widget->font)
178 statusbar->height = MAX(statusbar->height, widget->font->height * 1.5);
182 void
183 statusbar_init(Statusbar *statusbar)
185 Statusbar *sb;
186 int phys_screen = get_phys_screen(statusbar->screen);
187 Area area = screen_get_area(statusbar->screen,
188 globalconf.screens[statusbar->screen].statusbar,
189 &globalconf.screens[statusbar->screen].padding);
192 /* Top and Bottom Statusbar have prio */
193 for(sb = globalconf.screens[statusbar->screen].statusbar; sb; sb = sb->next)
194 switch(sb->position)
196 case Left:
197 case Right:
198 area.width += sb->height;
199 break;
200 default:
201 break;
204 if(statusbar->width <= 0)
206 if(statusbar->position == Right || statusbar->position == Left)
207 statusbar->width = area.height;
208 else
209 statusbar->width = area.width;
212 switch(statusbar->dposition)
214 case Right:
215 case Left:
216 statusbar->sw =
217 simplewindow_new(globalconf.display, phys_screen, 0, 0,
218 statusbar->height, statusbar->width, 0);
219 break;
220 default:
221 statusbar->sw =
222 simplewindow_new(globalconf.display, phys_screen, 0, 0,
223 statusbar->width, statusbar->height, 0);
224 break;
227 widget_calculate_alignments(statusbar->widgets);
229 statusbar_update_position(statusbar);
231 statusbar_draw(statusbar);
234 void
235 statusbar_refresh()
237 int screen;
238 Statusbar *statusbar;
239 Widget *widget;
241 for(screen = 0; screen < globalconf.nscreen; screen++)
242 for(statusbar = globalconf.screens[screen].statusbar;
243 statusbar;
244 statusbar = statusbar->next)
245 for(widget = statusbar->widgets; widget; widget = widget->next)
246 if(widget->cache.needs_update)
248 statusbar_draw(statusbar);
249 break;
253 Position
254 statusbar_get_position_from_str(const char *pos)
256 if(!a_strncmp(pos, "off", 3))
257 return Off;
258 else if(!a_strncmp(pos, "bottom", 6))
259 return Bottom;
260 else if(!a_strncmp(pos, "right", 5))
261 return Right;
262 else if(!a_strncmp(pos, "left", 4))
263 return Left;
264 return Top;
267 static Statusbar *
268 get_statusbar_byname(int screen, const char *name)
270 Statusbar *sb;
272 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
273 if(!a_strcmp(sb->name, name))
274 return sb;
276 return NULL;
279 static void
280 statusbar_toggle(Statusbar *statusbar)
282 if(statusbar->position == Off)
283 statusbar->position = (statusbar->dposition == Off) ? Top : statusbar->dposition;
284 else
285 statusbar->position = Off;
287 statusbar_update_position(statusbar);
288 globalconf.screens[statusbar->screen].need_arrange = True;
291 /** Toggle statusbar
292 * \param screen Screen ID
293 * \param arg statusbar name
294 * \ingroup ui_callback
296 void
297 uicb_statusbar_toggle(int screen, char *arg)
299 Statusbar *sb = get_statusbar_byname(screen, arg);
301 if(sb)
302 statusbar_toggle(sb);
303 else
304 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
305 statusbar_toggle(sb);
308 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80