change codename
[awesome.git] / titlebar.c
blob1d6482e109848700eec5c104fbba186c08de8174
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)
72 uint32_t request[] = { - sw->geometry.width, - sw->geometry.height };
73 /* Move the titlebar to the same place as the window. */
74 xcb_configure_window(globalconf.connection, sw->window,
75 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
76 request);
79 /* Remove titlebar geometry from client. */
80 if((c = client_getbytitlebar(titlebar)))
81 c->geometry = titlebar_geometry_remove(titlebar, 0, c->geometry);
83 titlebar->isbanned = true;
87 /** Move a titlebar on top of its client.
88 * \param titlebar The titlebar.
90 void
91 titlebar_unban(wibox_t *titlebar)
93 /* Do this manually because the system doesn't know we moved the toolbar.
94 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
95 * end up offscreen anyway. */
96 if(titlebar && titlebar->isbanned)
98 client_t *c;
99 simple_window_t *sw = &titlebar->sw;
101 if(sw->window)
103 /* All resizing is done, so only move now. */
104 uint32_t request[] = { sw->geometry.x, sw->geometry.y };
106 xcb_configure_window(globalconf.connection, sw->window,
107 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
108 request);
111 titlebar->isbanned = false;
113 /* Add titlebar geometry from client. */
114 if((c = client_getbytitlebar(titlebar)))
115 c->geometry = titlebar_geometry_add(titlebar, 0, c->geometry);
119 /** Get titlebar area.
120 * \param c The client
121 * \param geometry The client geometry including borders, excluding titlebars.
122 * \param res Pointer to area of titlebar, must be allocated already.
124 void
125 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
127 int height, width, x_offset = 0, y_offset = 0;
129 switch(c->titlebar->position)
131 default:
132 return;
133 case Top:
134 width = MAX(1, geometry.width);
135 switch(c->titlebar->align)
137 default:
138 break;
139 case AlignRight:
140 x_offset = geometry.width - width;
141 break;
142 case AlignCenter:
143 x_offset = (geometry.width - width) / 2;
144 break;
146 res->x = geometry.x + x_offset;
147 res->y = geometry.y - c->titlebar->sw.geometry.height;
148 res->width = width;
149 res->height = c->titlebar->sw.geometry.height;
150 break;
151 case Bottom:
152 width = MAX(1, geometry.width);
153 switch(c->titlebar->align)
155 default:
156 break;
157 case AlignRight:
158 x_offset = geometry.width - width;
159 break;
160 case AlignCenter:
161 x_offset = (geometry.width - width) / 2;
162 break;
164 res->x = geometry.x + x_offset;
165 res->y = geometry.y + geometry.height;
166 res->width = width;
167 res->height = c->titlebar->sw.geometry.height;
168 break;
169 case Left:
170 height = MAX(1, geometry.height);
171 switch(c->titlebar->align)
173 default:
174 break;
175 case AlignRight:
176 y_offset = geometry.height - height;
177 break;
178 case AlignCenter:
179 y_offset = (geometry.height - height) / 2;
180 break;
182 res->x = geometry.x - c->titlebar->sw.geometry.width;
183 res->y = geometry.y + y_offset;
184 res->width = c->titlebar->sw.geometry.width;
185 res->height = height;
186 break;
187 case Right:
188 height = MAX(1, geometry.height);
189 switch(c->titlebar->align)
191 default:
192 break;
193 case AlignRight:
194 y_offset = geometry.height - height;
195 break;
196 case AlignCenter:
197 y_offset = (geometry.height - height) / 2;
198 break;
200 res->x = geometry.x + geometry.width;
201 res->y = geometry.y + y_offset;
202 res->width = c->titlebar->sw.geometry.width;
203 res->height = height;
204 break;
208 /** Detach a wibox titlebar from its client.
209 * \param c The client.
211 void
212 titlebar_client_detach(client_t *c)
214 /* If client has a titlebar, kick it out. */
215 if(c && c->titlebar)
217 /* Update client geometry to exclude the titlebar. */
218 c->geometry = titlebar_geometry_remove(c->titlebar, 0, c->geometry);
219 simplewindow_wipe(&c->titlebar->sw);
220 c->titlebar->type = WIBOX_TYPE_NORMAL;
221 c->titlebar->screen = NULL;
223 wibox_unref(globalconf.L, c->titlebar);
224 c->titlebar = NULL;
226 client_need_arrange(c);
227 client_stack();
231 /** Attach a wibox to a client as its titlebar.
232 * \param c The client.
233 * \param ud The index of the wibox on the stack.
235 void
236 titlebar_client_attach(client_t *c)
238 /* check if we can register the object */
239 wibox_t *t = wibox_ref(globalconf.L);
241 titlebar_client_detach(c);
243 /* check if titlebar is already on a client */
244 titlebar_client_detach(client_getbytitlebar(t));
246 /* check if client already has a titlebar. */
247 titlebar_client_detach(c);
249 /* set the object as new client's titlebar */
250 c->titlebar = t;
252 t->type = WIBOX_TYPE_TITLEBAR;
253 t->screen = c->screen;
255 switch(t->position)
257 case Floating:
258 t->position = Top;
259 case Top:
260 case Bottom:
261 if(!t->sw.geometry.height)
262 t->sw.geometry.height = 1.5 * globalconf.font->height;
263 break;
264 case Left:
265 case Right:
266 if(!t->sw.geometry.width)
267 t->sw.geometry.width = 1.5 * globalconf.font->height;
268 break;
271 /* Update client geometry to include the titlebar. */
272 c->geometry = titlebar_geometry_add(c->titlebar, 0, c->geometry);
274 /* Client geometry without titlebar, but including borders, since that is always consistent. */
275 area_t wingeom;
276 titlebar_geometry_compute(c, titlebar_geometry_remove(c->titlebar, 0, c->geometry), &wingeom);
278 simplewindow_init(&t->sw, c->phys_screen,
279 wingeom, 0, t->sw.orientation,
280 &t->sw.ctx.fg, &t->sw.ctx.bg);
281 simplewindow_border_color_set(&t->sw, &t->sw.border.color);
283 t->need_update = true;
285 /* Call update geometry. This will move the wibox to the right place,
286 * which might be the same as `wingeom', but then it will ban the
287 * titlebar if needed. */
288 titlebar_update_geometry(c);
290 xcb_map_window(globalconf.connection, t->sw.window);
292 client_need_arrange(c);
293 client_stack();
296 /** Map or unmap a titlebar wibox.
297 * \param t The wibox/titlebar.
298 * \param visible The new state of the titlebar.
300 void
301 titlebar_set_visible(wibox_t *t, bool visible)
303 if(visible != t->isvisible)
305 if((t->isvisible = visible))
306 titlebar_unban(t);
307 else
308 titlebar_ban(t);
310 t->screen->need_arrange = true;
311 client_stack();
315 /** Titlebar newindex.
316 * \param L The Lua VM state.
317 * \param titlebar The wibox titlebar.
318 * \param tok The attribute token.
319 * \return The number of elements pushed on stack.
322 luaA_titlebar_newindex(lua_State *L, wibox_t *titlebar, awesome_token_t tok)
324 client_t *c = NULL;
326 switch(tok)
328 position_t position;
329 int i;
330 size_t len;
331 const char *buf;
333 case A_TK_ALIGN:
334 if((buf = luaL_checklstring(L, 3, &len)))
335 titlebar->align = draw_align_fromstr(buf, len);
336 else
337 return 0;
338 break;
339 case A_TK_BORDER_WIDTH:
340 if((i = luaL_checknumber(L, 3)) >= 0)
341 simplewindow_border_width_set(&titlebar->sw, i);
342 else
343 return 0;
344 break;
345 case A_TK_BORDER_COLOR:
346 if((buf = luaL_checklstring(L, 3, &len)))
347 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar->sw.border.color, buf, len)))
348 simplewindow_border_color_set(&titlebar->sw, &titlebar->sw.border.color);
349 return 0;
350 case A_TK_POSITION:
351 buf = luaL_checklstring(L, 3, &len);
352 position = position_fromstr(buf, len);
353 if(position != titlebar->position)
355 switch(position)
357 case Left:
358 switch(titlebar->position)
360 int tmp;
361 case Left:
362 case Right:
363 break;
364 case Top:
365 case Bottom:
366 case Floating:
367 tmp = titlebar->sw.geometry.width;
368 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
369 titlebar->sw.geometry.height = tmp;
370 break;
372 simplewindow_orientation_set(&titlebar->sw, North);
373 break;
374 case Right:
375 switch(titlebar->position)
377 int tmp;
378 case Left:
379 case Right:
380 break;
381 case Top:
382 case Bottom:
383 case Floating:
384 tmp = titlebar->sw.geometry.width;
385 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
386 titlebar->sw.geometry.height = tmp;
387 break;
389 simplewindow_orientation_set(&titlebar->sw, South);
390 break;
391 case Top:
392 case Bottom:
393 case Floating:
394 switch(titlebar->position)
396 int tmp;
397 case Left:
398 case Right:
399 tmp = titlebar->sw.geometry.width;
400 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
401 titlebar->sw.geometry.height = tmp;
402 break;
403 case Top:
404 case Bottom:
405 case Floating:
406 break;
408 simplewindow_orientation_set(&titlebar->sw, East);
409 break;
411 titlebar->position = position;
412 if((c = client_getbytitlebar(titlebar)))
414 titlebar_update_geometry(c);
415 /* call geometry hook for client because some like to
416 * set titlebar width in that hook, which make sense */
417 hooks_property(c, "geometry");
420 break;
421 default:
422 return 0;
425 if((c || (c = client_getbytitlebar(titlebar))))
426 client_need_arrange(c);
428 return 0;
431 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80