Add support for Bottom titlebar
[awesome.git] / statusbar.c
blob10adb8599af48477fb46e8306d5c16729d08ec93
1 /*
2 * statusbar.c - statusbar functions
4 * Copyright © 2007-2008 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_t 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 default:
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;
76 int left = 0, right = 0;
77 area_t rectangle = { 0, 0, 0, 0, NULL, 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].styles.normal.bg);
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 = *widget_list_last(&statusbar->widgets);
120 widget;
121 widget = widget_list_prev(&statusbar->widgets, widget))
122 if (widget->alignment == AlignRight)
124 widget->cache.needs_update = False;
125 right += widget->draw(widget, ctx, right, (left + right));
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 draw_context_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 if(statusbar->height <= 0)
170 /* 1.5 as default factor, it fits nice but no one knows why */
171 statusbar->height = 1.5 * MAX(globalconf.screens[statusbar->screen].styles.normal.font->height,
172 MAX(globalconf.screens[statusbar->screen].styles.focus.font->height,
173 globalconf.screens[statusbar->screen].styles.urgent.font->height));
176 void
177 statusbar_init(Statusbar *statusbar)
179 Statusbar *sb;
180 int phys_screen = get_phys_screen(statusbar->screen);
181 area_t area = screen_get_area(statusbar->screen,
182 globalconf.screens[statusbar->screen].statusbar,
183 &globalconf.screens[statusbar->screen].padding);
186 /* Top and Bottom Statusbar have prio */
187 for(sb = globalconf.screens[statusbar->screen].statusbar; sb; sb = sb->next)
188 switch(sb->position)
190 case Left:
191 case Right:
192 area.width += sb->height;
193 break;
194 default:
195 break;
198 if(statusbar->width <= 0)
200 if(statusbar->position == Right || statusbar->position == Left)
201 statusbar->width = area.height;
202 else
203 statusbar->width = area.width;
206 switch(statusbar->dposition)
208 case Right:
209 case Left:
210 statusbar->sw =
211 simplewindow_new(globalconf.display, phys_screen, 0, 0,
212 statusbar->height, statusbar->width, 0);
213 break;
214 default:
215 statusbar->sw =
216 simplewindow_new(globalconf.display, phys_screen, 0, 0,
217 statusbar->width, statusbar->height, 0);
218 break;
221 widget_calculate_alignments(statusbar->widgets);
223 statusbar_update_position(statusbar);
225 statusbar_draw(statusbar);
228 void
229 statusbar_refresh()
231 int screen;
232 Statusbar *statusbar;
233 Widget *widget;
235 for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
236 for(statusbar = globalconf.screens[screen].statusbar;
237 statusbar;
238 statusbar = statusbar->next)
239 for(widget = statusbar->widgets; widget; widget = widget->next)
240 if(widget->cache.needs_update)
242 statusbar_draw(statusbar);
243 break;
247 static Statusbar *
248 get_statusbar_byname(int screen, const char *name)
250 Statusbar *sb;
252 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
253 if(!a_strcmp(sb->name, name))
254 return sb;
256 return NULL;
259 static void
260 statusbar_toggle(Statusbar *statusbar)
262 if(statusbar->position == Off)
263 statusbar->position = (statusbar->dposition == Off) ? Top : statusbar->dposition;
264 else
265 statusbar->position = Off;
267 statusbar_update_position(statusbar);
268 globalconf.screens[statusbar->screen].need_arrange = True;
271 /** Toggle statusbar
272 * \param screen Screen ID
273 * \param arg statusbar name
274 * \ingroup ui_callback
276 void
277 uicb_statusbar_toggle(int screen, char *arg)
279 Statusbar *sb = get_statusbar_byname(screen, arg);
281 if(sb)
282 statusbar_toggle(sb);
283 else
284 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
285 statusbar_toggle(sb);
288 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80