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.
25 #include "statusbar.h"
31 extern AwesomeConf globalconf
;
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
)
44 XFreePixmap(globalconf
.display
, statusbar
->drawable
);
46 DrawCtx
*ctx
= draw_get_context(phys_screen
,
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
));
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
)
74 if(statusbar
->position
== Right
)
75 d
= draw_rotate(ctx
, phys_screen
, M_PI_2
, statusbar
->height
, 0);
77 d
= draw_rotate(ctx
, phys_screen
, - M_PI_2
, 0, statusbar
->width
);
79 statusbar
->drawable
= d
;
80 draw_free_context(ctx
);
84 statusbar
->drawable
= ctx
->drawable
;
85 /* just delete the struct, don't delete the drawable */
89 statusbar_display(statusbar
);
93 statusbar_draw_all(int screen
)
97 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
98 statusbar_draw(statusbar
);
102 statusbar_display(Statusbar
*statusbar
)
104 int phys_screen
= get_phys_screen(statusbar
->screen
);
106 /* don't waste our time */
107 if(statusbar
->position
== Off
)
110 if(statusbar
->position
== Right
111 || statusbar
->position
== Left
)
112 XCopyArea(globalconf
.display
, statusbar
->drawable
,
114 DefaultGC(globalconf
.display
, phys_screen
), 0, 0,
116 statusbar
->width
, 0, 0);
118 XCopyArea(globalconf
.display
, statusbar
->drawable
,
120 DefaultGC(globalconf
.display
, phys_screen
), 0, 0,
121 statusbar
->width
, statusbar
->height
, 0, 0);
125 statusbar_init(Statusbar
*statusbar
, int screen
)
128 XSetWindowAttributes wa
;
129 int phys_screen
= get_phys_screen(screen
);
130 Area area
= get_screen_area(statusbar
->screen
,
132 &globalconf
.screens
[screen
].padding
);
134 if(statusbar
->height
<= 0)
136 /* 1.5 as default factor, it fits nice but no one know why */
137 statusbar
->height
= globalconf
.screens
[screen
].font
->height
* 1.5;
139 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
141 statusbar
->height
= MAX(statusbar
->height
, widget
->font
->height
* 1.5);
144 if(statusbar
->width
<= 0)
146 if(statusbar
->position
== Right
|| statusbar
->position
== Left
)
147 statusbar
->width
= area
.height
;
149 statusbar
->width
= area
.width
;
152 statusbar
->screen
= screen
;
154 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
155 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
156 wa
.cursor
= globalconf
.cursor
[CurNormal
];
157 wa
.override_redirect
= 1;
158 wa
.background_pixmap
= ParentRelative
;
159 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
160 if(statusbar
->dposition
== Right
|| statusbar
->dposition
== Left
)
161 statusbar
->window
= XCreateWindow(globalconf
.display
,
162 RootWindow(globalconf
.display
,
168 DefaultDepth(globalconf
.display
,
171 DefaultVisual(globalconf
.display
,
178 statusbar
->window
= XCreateWindow(globalconf
.display
,
179 RootWindow(globalconf
.display
,
185 DefaultDepth(globalconf
.display
,
188 DefaultVisual(globalconf
.display
,
195 statusbar
->drawable
= XCreatePixmap(globalconf
.display
,
196 RootWindow(globalconf
.display
, phys_screen
),
197 statusbar
->width
, statusbar
->height
,
198 DefaultDepth(globalconf
.display
, phys_screen
));
201 XDefineCursor(globalconf
.display
,
203 globalconf
.cursor
[CurNormal
]);
205 widget_calculate_alignments(statusbar
->widgets
);
207 statusbar_update_position(statusbar
);
208 XMapRaised(globalconf
.display
, statusbar
->window
);
210 statusbar_draw(statusbar
);
214 statusbar_update_position(Statusbar
*statusbar
)
217 Area area
= get_screen_area(statusbar
->screen
,
219 &globalconf
.screens
[statusbar
->screen
].padding
);
221 XMapRaised(globalconf
.display
, statusbar
->window
);
222 switch(statusbar
->position
)
225 XMoveWindow(globalconf
.display
, statusbar
->window
,
229 XMoveWindow(globalconf
.display
, statusbar
->window
,
230 area
.x
, (area
.y
+ area
.height
) - statusbar
->width
);
233 XMoveWindow(globalconf
.display
, statusbar
->window
,
234 area
.x
+ (area
.width
- statusbar
->height
), area
.y
);
237 XMoveWindow(globalconf
.display
, statusbar
->window
,
238 area
.x
, area
.height
- statusbar
->height
);
241 XUnmapWindow(globalconf
.display
, statusbar
->window
);
244 XSync(globalconf
.display
, False
);
245 while(XCheckMaskEvent(globalconf
.display
, EnterWindowMask
, &ev
));
249 statusbar_get_position_from_str(const char *pos
)
251 if(!a_strncmp(pos
, "off", 3))
253 else if(!a_strncmp(pos
, "bottom", 6))
255 else if(!a_strncmp(pos
, "right", 5))
257 else if(!a_strncmp(pos
, "left", 4))
263 get_statusbar_byname(int screen
, const char *name
)
267 for(sb
= globalconf
.screens
[screen
].statusbar
; sb
; sb
= sb
->next
)
268 if(!a_strcmp(sb
->name
, name
))
276 statusbar_toggle(Statusbar
*statusbar
)
278 if(statusbar
->position
== Off
)
279 statusbar
->position
= (statusbar
->dposition
== Off
) ? Top
: statusbar
->dposition
;
281 statusbar
->position
= Off
;
283 statusbar_update_position(statusbar
);
287 * \param screen Screen ID
289 * \ingroup ui_callback
292 uicb_statusbar_toggle(int screen
, char *arg
)
294 Statusbar
*sb
= get_statusbar_byname(screen
, arg
);
297 statusbar_toggle(sb
);
299 for(sb
= globalconf
.screens
[screen
].statusbar
; sb
; sb
= sb
->next
)
300 statusbar_toggle(sb
);
305 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80