awful.layout: also arrange on client tag change
[awesome.git] / titlebar.c
blob1379d58cd83974e5714817032bb898393b7b2225
1 /*
2 * titlebar.c - titlebar management
4 * Copyright © 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 <xcb/xcb.h>
24 #include "titlebar.h"
25 #include "client.h"
26 #include "widget.h"
27 #include "wibox.h"
28 #include "screen.h"
30 /** Get a client by its titlebar.
31 * \param titlebar The titlebar.
32 * \return A client.
34 client_t *
35 client_getbytitlebar(wibox_t *titlebar)
37 foreach(c, globalconf.clients)
38 if((*c)->titlebar == titlebar)
39 return *c;
41 return NULL;
44 /** Get a client by its titlebar window.
45 * \param win The window.
46 * \return A client.
48 client_t *
49 client_getbytitlebarwin(xcb_window_t win)
51 foreach(c, globalconf.clients)
52 if((*c)->titlebar && (*c)->titlebar->sw.window == win)
53 return *c;
55 return NULL;
58 /** Move a titlebar out of the viewport.
59 * \param titlebar The titlebar.
61 void
62 titlebar_ban(wibox_t *titlebar)
64 /* Do it manually because client geometry remains unchanged. */
65 if(titlebar && !titlebar->isbanned)
67 client_t *c;
68 simple_window_t *sw = &titlebar->sw;
70 if(sw->window)
71 xcb_unmap_window(globalconf.connection, sw->window);
73 /* Remove titlebar geometry from client. */
74 if((c = client_getbytitlebar(titlebar)))
75 c->geometry = titlebar_geometry_remove(titlebar, 0, c->geometry);
77 titlebar->isbanned = true;
81 /** Move a titlebar on top of its client.
82 * \param titlebar The titlebar.
84 void
85 titlebar_unban(wibox_t *titlebar)
87 /* Do this manually because the system doesn't know we moved the toolbar.
88 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
89 * end up offscreen anyway. */
90 if(titlebar && titlebar->isbanned)
92 client_t *c;
93 simple_window_t *sw = &titlebar->sw;
95 if(sw->window)
96 xcb_map_window(globalconf.connection, sw->window);
98 titlebar->isbanned = false;
100 /* Add titlebar geometry from client. */
101 if((c = client_getbytitlebar(titlebar)))
102 c->geometry = titlebar_geometry_add(titlebar, 0, c->geometry);
106 /** Get titlebar area.
107 * \param c The client
108 * \param geometry The client geometry including borders, excluding titlebars.
109 * \param res Pointer to area of titlebar, must be allocated already.
111 void
112 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
114 int height, width, x_offset = 0, y_offset = 0;
116 switch(c->titlebar->position)
118 default:
119 return;
120 case Top:
121 width = MAX(1, geometry.width);
122 switch(c->titlebar->align)
124 default:
125 break;
126 case AlignRight:
127 x_offset = geometry.width - width;
128 break;
129 case AlignCenter:
130 x_offset = (geometry.width - width) / 2;
131 break;
133 res->x = geometry.x + x_offset;
134 res->y = geometry.y - c->titlebar->sw.geometry.height;
135 res->width = width;
136 res->height = c->titlebar->sw.geometry.height;
137 break;
138 case Bottom:
139 width = MAX(1, geometry.width);
140 switch(c->titlebar->align)
142 default:
143 break;
144 case AlignRight:
145 x_offset = geometry.width - width;
146 break;
147 case AlignCenter:
148 x_offset = (geometry.width - width) / 2;
149 break;
151 res->x = geometry.x + x_offset;
152 res->y = geometry.y + geometry.height;
153 res->width = width;
154 res->height = c->titlebar->sw.geometry.height;
155 break;
156 case Left:
157 height = MAX(1, geometry.height);
158 switch(c->titlebar->align)
160 default:
161 break;
162 case AlignRight:
163 y_offset = geometry.height - height;
164 break;
165 case AlignCenter:
166 y_offset = (geometry.height - height) / 2;
167 break;
169 res->x = geometry.x - c->titlebar->sw.geometry.width;
170 res->y = geometry.y + y_offset;
171 res->width = c->titlebar->sw.geometry.width;
172 res->height = height;
173 break;
174 case Right:
175 height = MAX(1, geometry.height);
176 switch(c->titlebar->align)
178 default:
179 break;
180 case AlignRight:
181 y_offset = geometry.height - height;
182 break;
183 case AlignCenter:
184 y_offset = (geometry.height - height) / 2;
185 break;
187 res->x = geometry.x + geometry.width;
188 res->y = geometry.y + y_offset;
189 res->width = c->titlebar->sw.geometry.width;
190 res->height = height;
191 break;
195 /** Detach a wibox titlebar from its client.
196 * \param c The client.
198 void
199 titlebar_client_detach(client_t *c)
201 /* If client has a titlebar, kick it out. */
202 if(c && c->titlebar)
204 /* Update client geometry to exclude the titlebar. */
205 c->geometry = titlebar_geometry_remove(c->titlebar, 0, c->geometry);
206 simplewindow_wipe(&c->titlebar->sw);
207 c->titlebar->type = WIBOX_TYPE_NORMAL;
208 c->titlebar->screen = NULL;
210 wibox_unref(globalconf.L, c->titlebar);
211 c->titlebar = NULL;
213 hook_property(client, c, "titlebar");
214 client_stack();
218 /** Attach a wibox to a client as its titlebar.
219 * \param c The client.
220 * \param ud The index of the wibox on the stack.
222 void
223 titlebar_client_attach(client_t *c)
225 /* check if we can register the object */
226 wibox_t *t = wibox_ref(globalconf.L, -1);
228 titlebar_client_detach(c);
230 /* check if titlebar is already on a client */
231 titlebar_client_detach(client_getbytitlebar(t));
233 /* check if client already has a titlebar. */
234 titlebar_client_detach(c);
236 /* set the object as new client's titlebar */
237 c->titlebar = t;
239 t->type = WIBOX_TYPE_TITLEBAR;
240 t->screen = c->screen;
242 switch(t->position)
244 case Top:
245 case Bottom:
246 if(!t->sw.geometry.height)
247 t->sw.geometry.height = 1.5 * globalconf.font->height;
248 break;
249 case Left:
250 case Right:
251 if(!t->sw.geometry.width)
252 t->sw.geometry.width = 1.5 * globalconf.font->height;
253 break;
256 /* Update client geometry to include the titlebar. */
257 c->geometry = titlebar_geometry_add(c->titlebar, 0, c->geometry);
259 /* Client geometry without titlebar, but including borders, since that is always consistent. */
260 area_t wingeom;
261 titlebar_geometry_compute(c, titlebar_geometry_remove(c->titlebar, 0, c->geometry), &wingeom);
263 simplewindow_init(&t->sw, c->phys_screen,
264 wingeom, 0, &t->sw.border.color,
265 t->sw.orientation, &t->sw.ctx.fg, &t->sw.ctx.bg);
267 t->need_update = true;
269 /* Call update geometry. This will move the wibox to the right place,
270 * which might be the same as `wingeom', but then it will ban the
271 * titlebar if needed. */
272 titlebar_update_geometry(c);
274 xcb_map_window(globalconf.connection, t->sw.window);
276 hook_property(client, c, "titlebar");
277 client_stack();
280 /** Map or unmap a titlebar wibox.
281 * \param t The wibox/titlebar.
282 * \param visible The new state of the titlebar.
284 void
285 titlebar_set_visible(wibox_t *t, bool visible)
287 if(visible != t->isvisible)
289 if((t->isvisible = visible))
290 titlebar_unban(t);
291 else
292 titlebar_ban(t);
294 hook_property(wibox, t, "visible");
295 client_stack();
299 /** Titlebar newindex.
300 * \param L The Lua VM state.
301 * \param titlebar The wibox titlebar.
302 * \param tok The attribute token.
303 * \return The number of elements pushed on stack.
306 luaA_titlebar_newindex(lua_State *L, wibox_t *titlebar, awesome_token_t tok)
308 client_t *c = NULL;
310 switch(tok)
312 position_t position;
313 int i;
314 size_t len;
315 const char *buf;
317 case A_TK_ALIGN:
318 if((buf = luaL_checklstring(L, 3, &len)))
319 titlebar->align = draw_align_fromstr(buf, len);
320 else
321 return 0;
322 break;
323 case A_TK_BORDER_WIDTH:
324 if((i = luaL_checknumber(L, 3)) >= 0)
325 simplewindow_border_width_set(&titlebar->sw, i);
326 else
327 return 0;
328 break;
329 case A_TK_BORDER_COLOR:
330 if((buf = luaL_checklstring(L, 3, &len)))
331 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar->sw.border.color, buf, len)))
332 simplewindow_border_color_set(&titlebar->sw, &titlebar->sw.border.color);
333 return 0;
334 case A_TK_POSITION:
335 buf = luaL_checklstring(L, 3, &len);
336 position = position_fromstr(buf, len);
337 if(position != titlebar->position)
339 switch(position)
341 case Left:
342 switch(titlebar->position)
344 int tmp;
345 case Left:
346 case Right:
347 break;
348 case Top:
349 case Bottom:
350 tmp = titlebar->sw.geometry.width;
351 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
352 titlebar->sw.geometry.height = tmp;
353 break;
355 simplewindow_orientation_set(&titlebar->sw, North);
356 break;
357 case Right:
358 switch(titlebar->position)
360 int tmp;
361 case Left:
362 case Right:
363 break;
364 case Top:
365 case Bottom:
366 tmp = titlebar->sw.geometry.width;
367 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
368 titlebar->sw.geometry.height = tmp;
369 break;
371 simplewindow_orientation_set(&titlebar->sw, South);
372 break;
373 case Top:
374 case Bottom:
375 switch(titlebar->position)
377 int tmp;
378 case Left:
379 case Right:
380 tmp = titlebar->sw.geometry.width;
381 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
382 titlebar->sw.geometry.height = tmp;
383 break;
384 case Top:
385 case Bottom:
386 break;
388 simplewindow_orientation_set(&titlebar->sw, East);
389 break;
391 titlebar->position = position;
392 if((c = client_getbytitlebar(titlebar)))
394 titlebar_update_geometry(c);
395 /* call geometry hook for client because some like to
396 * set titlebar width in that hook, which make sense */
397 hook_property(client, c, "geometry");
400 break;
401 default:
402 return 0;
405 return 0;
408 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80