add show_all option to tasklist
[awesome.git] / statusbar.c
blob2019fc38abfec31a4b4ad9d9c83af0dd26d0c955
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_draw(Statusbar *statusbar)
36 int phys_screen = get_phys_screen(statusbar->screen);
37 Widget *widget, *last_drawn = NULL;
38 int left = 0, right = 0;
40 /* don't waste our time */
41 if(statusbar->position == Off)
42 return;
44 XFreePixmap(globalconf.display, statusbar->drawable);
46 DrawCtx *ctx = draw_get_context(phys_screen,
47 statusbar->width,
48 statusbar->height);
50 draw_rectangle(ctx, 0, 0, statusbar->width, statusbar->height, True,
51 globalconf.screens[statusbar->screen].colors_normal[ColBG]);
53 for(widget = statusbar->widgets; widget; widget = widget->next)
54 if (widget->alignment == AlignLeft)
55 left += widget->draw(widget, ctx, left, (left + right));
57 /* renders right widget from last to first */
58 for(widget = statusbar->widgets; widget; widget = widget->next)
59 if (widget->alignment == AlignRight && last_drawn == widget->next)
61 right += widget->draw(widget, ctx, right, (left + right));
62 last_drawn = widget;
63 widget = statusbar->widgets;
66 for(widget = statusbar->widgets; widget; widget = widget->next)
67 if (widget->alignment == AlignFlex)
68 left += widget->draw(widget, ctx, left, (left + right));
70 if(statusbar->position == Right
71 || statusbar->position == Left)
73 if(statusbar->position == Right)
74 statusbar->drawable = draw_rotate(ctx, phys_screen, M_PI_2, statusbar->height, 0);
75 else
76 statusbar->drawable = draw_rotate(ctx, phys_screen, - M_PI_2, 0, statusbar->width);
78 draw_free_context(ctx);
80 else
82 statusbar->drawable = ctx->drawable;
83 /* just delete the struct, don't delete the drawable */
84 p_delete(&ctx);
87 statusbar_display(statusbar);
90 void
91 statusbar_draw_all(int screen)
93 Statusbar *statusbar;
95 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
96 statusbar_draw(statusbar);
99 void
100 statusbar_display(Statusbar *statusbar)
102 int phys_screen = get_phys_screen(statusbar->screen);
104 /* don't waste our time */
105 if(statusbar->position == Off)
106 return;
108 if(statusbar->position == Right
109 || statusbar->position == Left)
110 XCopyArea(globalconf.display, statusbar->drawable,
111 statusbar->window,
112 DefaultGC(globalconf.display, phys_screen), 0, 0,
113 statusbar->height,
114 statusbar->width, 0, 0);
115 else
116 XCopyArea(globalconf.display, statusbar->drawable,
117 statusbar->window,
118 DefaultGC(globalconf.display, phys_screen), 0, 0,
119 statusbar->width, statusbar->height, 0, 0);
122 void
123 statusbar_init(Statusbar *statusbar, int screen)
125 Widget *widget;
126 XSetWindowAttributes wa;
127 int phys_screen = get_phys_screen(screen);
128 Area area = get_screen_area(statusbar->screen,
129 NULL,
130 &globalconf.screens[screen].padding);
132 if(statusbar->height <= 0)
134 /* 1.5 as default factor, it fits nice but no one know why */
135 statusbar->height = globalconf.screens[screen].font->height * 1.5;
137 for(widget = statusbar->widgets; widget; widget = widget->next)
138 if(widget->font)
139 statusbar->height = MAX(statusbar->height, widget->font->height * 1.5);
142 if(statusbar->width <= 0)
144 if(statusbar->position == Right || statusbar->position == Left)
145 statusbar->width = area.height;
146 else
147 statusbar->width = area.width;
150 statusbar->screen = screen;
152 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
153 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
154 wa.cursor = globalconf.cursor[CurNormal];
155 wa.override_redirect = 1;
156 wa.background_pixmap = ParentRelative;
157 wa.event_mask = ButtonPressMask | ExposureMask;
158 if(statusbar->dposition == Right || statusbar->dposition == Left)
159 statusbar->window = XCreateWindow(globalconf.display,
160 RootWindow(globalconf.display,
161 phys_screen),
162 0, 0,
163 statusbar->height,
164 statusbar->width,
166 DefaultDepth(globalconf.display,
167 phys_screen),
168 CopyFromParent,
169 DefaultVisual(globalconf.display,
170 phys_screen),
171 CWOverrideRedirect |
172 CWBackPixmap |
173 CWEventMask,
174 &wa);
175 else
176 statusbar->window = XCreateWindow(globalconf.display,
177 RootWindow(globalconf.display,
178 phys_screen),
179 0, 0,
180 statusbar->width,
181 statusbar->height,
183 DefaultDepth(globalconf.display,
184 phys_screen),
185 CopyFromParent,
186 DefaultVisual(globalconf.display,
187 phys_screen),
188 CWOverrideRedirect |
189 CWBackPixmap |
190 CWEventMask,
191 &wa);
193 statusbar->drawable = XCreatePixmap(globalconf.display,
194 RootWindow(globalconf.display, phys_screen),
195 statusbar->width, statusbar->height,
196 DefaultDepth(globalconf.display, phys_screen));
199 XDefineCursor(globalconf.display,
200 statusbar->window,
201 globalconf.cursor[CurNormal]);
203 widget_calculate_alignments(statusbar->widgets);
205 statusbar_update_position(statusbar);
206 XMapRaised(globalconf.display, statusbar->window);
208 statusbar_draw(statusbar);
211 void
212 statusbar_update_position(Statusbar *statusbar)
214 XEvent ev;
215 Area area = get_screen_area(statusbar->screen,
216 NULL,
217 &globalconf.screens[statusbar->screen].padding);
219 XMapRaised(globalconf.display, statusbar->window);
220 switch(statusbar->position)
222 default:
223 XMoveWindow(globalconf.display, statusbar->window,
224 area.x, area.y);
225 break;
226 case Left:
227 XMoveWindow(globalconf.display, statusbar->window,
228 area.x, (area.y + area.height) - statusbar->width);
229 break;
230 case Right:
231 XMoveWindow(globalconf.display, statusbar->window,
232 area.x + (area.width - statusbar->height), area.y);
233 break;
234 case Bottom:
235 XMoveWindow(globalconf.display, statusbar->window,
236 area.x, area.height - statusbar->height);
237 break;
238 case Off:
239 XUnmapWindow(globalconf.display, statusbar->window);
240 break;
242 XSync(globalconf.display, False);
243 while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &ev));
246 Position
247 statusbar_get_position_from_str(const char *pos)
249 if(!a_strncmp(pos, "off", 3))
250 return Off;
251 else if(!a_strncmp(pos, "bottom", 6))
252 return Bottom;
253 else if(!a_strncmp(pos, "right", 5))
254 return Right;
255 else if(!a_strncmp(pos, "left", 4))
256 return Left;
257 return Top;
260 static Statusbar *
261 get_statusbar_byname(int screen, const char *name)
263 Statusbar *sb;
265 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
266 if(!a_strcmp(sb->name, name))
267 return sb;
269 return NULL;
273 static void
274 statusbar_toggle(Statusbar *statusbar)
276 if(statusbar->position == Off)
277 statusbar->position = (statusbar->dposition == Off) ? Top : statusbar->dposition;
278 else
279 statusbar->position = Off;
281 statusbar_update_position(statusbar);
284 /** Toggle statusbar
285 * \param screen Screen ID
286 * \param arg Unused
287 * \ingroup ui_callback
289 void
290 uicb_statusbar_toggle(int screen, char *arg)
292 Statusbar *sb = get_statusbar_byname(screen, arg);
294 if(sb)
295 statusbar_toggle(sb);
296 else
297 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
298 statusbar_toggle(sb);
300 arrange(screen);
303 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80