add coding style stuff
[awesome.git] / statusbar.c
blob989bd2165a4400de9cccb19c0707444604558c85
1 /*
2 * draw.c - draw 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>
24 #include "layout.h"
25 #include "statusbar.h"
26 #include "draw.h"
27 #include "screen.h"
28 #include "util.h"
29 #include "layouts/tile.h"
31 /** Check if at least a client is tagged with tag number t and is on screen
32 * screen
33 * \param t tag number
34 * \param screen screen number
35 * \return True or False
37 static Bool
38 isoccupied(Client **head, unsigned int t, int screen)
40 Client *c;
42 for(c = *head; c; c = c->next)
43 if(c->tags[t] && c->screen == screen)
44 return True;
45 return False;
48 void
49 drawstatusbar(Display *disp, awesome_config * awesomeconf)
51 int z, i, x = 0, y = 0, w;
53 for(i = 0; i < awesomeconf->ntags; i++)
55 w = textwidth(disp, awesomeconf->font,
56 awesomeconf->tags[i].name, a_strlen(awesomeconf->tags[i].name))
57 + awesomeconf->font->height;
58 if(awesomeconf->tags[i].selected)
60 drawtext(disp, awesomeconf->phys_screen,
61 x, y, w,
62 awesomeconf->statusbar.height,
63 awesomeconf->statusbar.drawable,
64 awesomeconf->statusbar.width,
65 awesomeconf->statusbar.height,
66 awesomeconf->font,
67 awesomeconf->tags[i].name, awesomeconf->colors_selected);
68 if(isoccupied(awesomeconf->clients, i, awesomeconf->screen))
69 drawrectangle(disp, awesomeconf->phys_screen,
70 x, y,
71 (awesomeconf->font->height + 2) / 4,
72 (awesomeconf->font->height + 2) / 4,
73 awesomeconf->statusbar.drawable,
74 awesomeconf->statusbar.width,
75 awesomeconf->statusbar.height,
76 *awesomeconf->client_sel && (*awesomeconf->client_sel)->tags[i],
77 awesomeconf->colors_selected[ColFG]);
79 else
81 drawtext(disp, awesomeconf->phys_screen,
82 x, y, w,
83 awesomeconf->statusbar.height,
84 awesomeconf->statusbar.drawable,
85 awesomeconf->statusbar.width,
86 awesomeconf->statusbar.height,
87 awesomeconf->font,
88 awesomeconf->tags[i].name, awesomeconf->colors_normal);
89 if(isoccupied(awesomeconf->clients, i, awesomeconf->screen))
90 drawrectangle(disp, awesomeconf->phys_screen,
91 x, y,
92 (awesomeconf->font->height + 2) / 4,
93 (awesomeconf->font->height + 2) / 4,
94 awesomeconf->statusbar.drawable,
95 awesomeconf->statusbar.width,
96 awesomeconf->statusbar.height,
97 *awesomeconf->client_sel && (*awesomeconf->client_sel)->tags[i],
98 awesomeconf->colors_normal[ColFG]);
100 x += w;
102 drawtext(disp, awesomeconf->phys_screen,
103 x, y, awesomeconf->statusbar.txtlayoutwidth,
104 awesomeconf->statusbar.height,
105 awesomeconf->statusbar.drawable,
106 awesomeconf->statusbar.width,
107 awesomeconf->statusbar.height,
108 awesomeconf->font,
109 awesomeconf->current_layout->symbol, awesomeconf->colors_normal);
110 z = x + awesomeconf->statusbar.txtlayoutwidth;
111 w = textwidth(disp, awesomeconf->font, awesomeconf->statustext, a_strlen(awesomeconf->statustext))
112 + awesomeconf->font->height;
113 x = awesomeconf->statusbar.width - w;
114 if(x < z)
116 x = z;
117 w = awesomeconf->statusbar.width - z;
119 drawtext(disp, awesomeconf->phys_screen,
120 x, y, w,
121 awesomeconf->statusbar.height,
122 awesomeconf->statusbar.drawable,
123 awesomeconf->statusbar.width,
124 awesomeconf->statusbar.height,
125 awesomeconf->font,
126 awesomeconf->statustext, awesomeconf->colors_normal);
127 if((w = x - z) > awesomeconf->statusbar.height)
129 x = z;
130 if(*awesomeconf->client_sel)
132 drawtext(disp, awesomeconf->phys_screen,
133 x, y, w,
134 awesomeconf->statusbar.height,
135 awesomeconf->statusbar.drawable,
136 awesomeconf->statusbar.width,
137 awesomeconf->statusbar.height,
138 awesomeconf->font,
139 (*awesomeconf->client_sel)->name, awesomeconf->colors_selected);
140 if((*awesomeconf->client_sel)->isfloating)
141 drawcircle(disp, awesomeconf->phys_screen,
142 x, y,
143 (awesomeconf->font->height + 2) / 4,
144 awesomeconf->statusbar.drawable,
145 awesomeconf->statusbar.width,
146 awesomeconf->statusbar.height,
147 (*awesomeconf->client_sel)->ismax,
148 awesomeconf->colors_selected[ColFG]);
150 else if(IS_ARRANGE(0, layout_tile) || IS_ARRANGE(0, layout_tileleft))
152 char buf[256];
153 snprintf(buf, sizeof(buf), "nmaster: %d ncol: %d mwfact: %.2lf", awesomeconf->nmaster, awesomeconf->ncol, awesomeconf->mwfact);
154 drawtext(disp, awesomeconf->phys_screen,
155 x, y, w,
156 awesomeconf->statusbar.height,
157 awesomeconf->statusbar.drawable,
158 awesomeconf->statusbar.width,
159 awesomeconf->statusbar.height,
160 awesomeconf->font,
161 buf, awesomeconf->colors_normal);
163 else
164 drawtext(disp, awesomeconf->phys_screen,
165 x, y, w,
166 awesomeconf->statusbar.height,
167 awesomeconf->statusbar.drawable,
168 awesomeconf->statusbar.width,
169 awesomeconf->statusbar.height,
170 awesomeconf->font,
171 NULL, awesomeconf->colors_normal);
173 XCopyArea(disp, awesomeconf->statusbar.drawable,
174 awesomeconf->statusbar.window, DefaultGC(disp, awesomeconf->phys_screen), 0, 0,
175 awesomeconf->statusbar.width, awesomeconf->statusbar.height, 0, 0);
176 XSync(disp, False);
179 void
180 initstatusbar(Display *disp, int screen, Statusbar *statusbar, Cursor cursor, XftFont *font, Layout *layouts, int nlayouts)
182 XSetWindowAttributes wa;
183 int i, phys_screen = get_phys_screen(disp, screen);
184 ScreenInfo *si = get_screen_info(disp, screen, NULL);
186 statusbar->width = si[screen].width;
187 statusbar->height = font->height + 2;
188 p_delete(&si);
190 statusbar->screen = screen;
192 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
193 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
194 wa.cursor = cursor;
195 wa.override_redirect = 1;
196 wa.background_pixmap = ParentRelative;
197 wa.event_mask = ButtonPressMask | ExposureMask;
198 statusbar->window = XCreateWindow(disp, RootWindow(disp, phys_screen), 0, 0,
199 statusbar->width,
200 statusbar->height,
201 0, DefaultDepth(disp, phys_screen), CopyFromParent,
202 DefaultVisual(disp, phys_screen),
203 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
204 XDefineCursor(disp, statusbar->window, cursor);
205 updatebarpos(disp, *statusbar);
206 XMapRaised(disp, statusbar->window);
207 statusbar->drawable = XCreatePixmap(disp,
208 RootWindow(disp, phys_screen),
209 statusbar->width,
210 statusbar->height,
211 DefaultDepth(disp, phys_screen));
213 for(i = 0; i < nlayouts; i++)
214 statusbar->txtlayoutwidth = MAX(statusbar->txtlayoutwidth,
215 (font->height + textwidth(disp, font, layouts[i].symbol, a_strlen(layouts[i].symbol))));
218 void
219 updatebarpos(Display *disp, Statusbar statusbar)
221 XEvent ev;
222 ScreenInfo *si = get_screen_info(disp, statusbar.screen, NULL);
224 switch (statusbar.position)
226 default:
227 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org, si[statusbar.screen].y_org);
228 break;
229 case BarBot:
230 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org, si[statusbar.screen].height - statusbar.height);
231 break;
232 case BarOff:
233 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org, si[statusbar.screen].y_org - statusbar.height);
234 break;
236 p_delete(&si);
237 XSync(disp, False);
238 while(XCheckMaskEvent(disp, EnterWindowMask, &ev));
241 void
242 uicb_togglebar(awesome_config *awesomeconf,
243 const char *arg __attribute__ ((unused)))
245 if(awesomeconf->statusbar.position == BarOff)
246 awesomeconf->statusbar.position = (awesomeconf->statusbar_default_position == BarOff) ? BarTop : awesomeconf->statusbar_default_position;
247 else
248 awesomeconf->statusbar.position = BarOff;
249 updatebarpos(awesomeconf->display, awesomeconf->statusbar);
250 arrange(awesomeconf->display, awesomeconf);
254 void
255 uicb_setstatustext(awesome_config *awesomeconf, const char *arg)
257 if(!arg)
258 return;
259 a_strncpy(awesomeconf->statustext, sizeof(awesomeconf->statustext), arg, a_strlen(arg));
261 drawstatusbar(awesomeconf->display, awesomeconf);
263 // vim: filetype=c:expandtab:shiftwidth=6:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99