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
)
73 if(statusbar
->position
== Right
)
74 statusbar
->drawable
= draw_rotate(ctx
, phys_screen
, M_PI_2
, statusbar
->height
, 0);
76 statusbar
->drawable
= draw_rotate(ctx
, phys_screen
, - M_PI_2
, 0, statusbar
->width
);
78 draw_free_context(ctx
);
82 statusbar
->drawable
= ctx
->drawable
;
83 /* just delete the struct, don't delete the drawable */
87 statusbar_display(statusbar
);
91 statusbar_draw_all(int screen
)
95 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
96 statusbar_draw(statusbar
);
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
)
108 if(statusbar
->position
== Right
109 || statusbar
->position
== Left
)
110 XCopyArea(globalconf
.display
, statusbar
->drawable
,
112 DefaultGC(globalconf
.display
, phys_screen
), 0, 0,
114 statusbar
->width
, 0, 0);
116 XCopyArea(globalconf
.display
, statusbar
->drawable
,
118 DefaultGC(globalconf
.display
, phys_screen
), 0, 0,
119 statusbar
->width
, statusbar
->height
, 0, 0);
123 statusbar_init(Statusbar
*statusbar
, int screen
)
126 XSetWindowAttributes wa
;
127 int phys_screen
= get_phys_screen(screen
);
128 Area area
= get_screen_area(statusbar
->screen
,
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
)
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
;
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
,
166 DefaultDepth(globalconf
.display
,
169 DefaultVisual(globalconf
.display
,
176 statusbar
->window
= XCreateWindow(globalconf
.display
,
177 RootWindow(globalconf
.display
,
183 DefaultDepth(globalconf
.display
,
186 DefaultVisual(globalconf
.display
,
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
,
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
);
212 statusbar_update_position(Statusbar
*statusbar
)
215 Area area
= get_screen_area(statusbar
->screen
,
217 &globalconf
.screens
[statusbar
->screen
].padding
);
219 XMapRaised(globalconf
.display
, statusbar
->window
);
220 switch(statusbar
->position
)
223 XMoveWindow(globalconf
.display
, statusbar
->window
,
227 XMoveWindow(globalconf
.display
, statusbar
->window
,
228 area
.x
, (area
.y
+ area
.height
) - statusbar
->width
);
231 XMoveWindow(globalconf
.display
, statusbar
->window
,
232 area
.x
+ (area
.width
- statusbar
->height
), area
.y
);
235 XMoveWindow(globalconf
.display
, statusbar
->window
,
236 area
.x
, area
.height
- statusbar
->height
);
239 XUnmapWindow(globalconf
.display
, statusbar
->window
);
242 XSync(globalconf
.display
, False
);
243 while(XCheckMaskEvent(globalconf
.display
, EnterWindowMask
, &ev
));
247 statusbar_get_position_from_str(const char *pos
)
249 if(!a_strncmp(pos
, "off", 3))
251 else if(!a_strncmp(pos
, "bottom", 6))
253 else if(!a_strncmp(pos
, "right", 5))
255 else if(!a_strncmp(pos
, "left", 4))
261 get_statusbar_byname(int screen
, const char *name
)
265 for(sb
= globalconf
.screens
[screen
].statusbar
; sb
; sb
= sb
->next
)
266 if(!a_strcmp(sb
->name
, name
))
274 statusbar_toggle(Statusbar
*statusbar
)
276 if(statusbar
->position
== Off
)
277 statusbar
->position
= (statusbar
->dposition
== Off
) ? Top
: statusbar
->dposition
;
279 statusbar
->position
= Off
;
281 statusbar_update_position(statusbar
);
285 * \param screen Screen ID
287 * \ingroup ui_callback
290 uicb_statusbar_toggle(int screen
, char *arg
)
292 Statusbar
*sb
= get_statusbar_byname(screen
, arg
);
295 statusbar_toggle(sb
);
297 for(sb
= globalconf
.screens
[screen
].statusbar
; sb
; sb
= sb
->next
)
298 statusbar_toggle(sb
);
303 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80