change codename
[awesome.git] / titlebar.c
blobd5de3e46240929c7bfe497dfdfb49a1d5812c854
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 extern awesome_t globalconf;
32 /** Get a client by its titlebar.
33 * \param titlebar The titlebar.
34 * \return A client.
36 client_t *
37 client_getbytitlebar(wibox_t *titlebar)
39 client_t *c;
41 for(c = globalconf.clients; c; c = c->next)
42 if(c->titlebar == titlebar)
43 return c;
45 return NULL;
48 /** Get a client by its titlebar window.
49 * \param win The window.
50 * \return A client.
52 client_t *
53 client_getbytitlebarwin(xcb_window_t win)
55 client_t *c;
57 for(c = globalconf.clients; c; c = c->next)
58 if(c->titlebar && c->titlebar->sw.window == win)
59 return c;
61 return NULL;
64 /** Move a titlebar out of the viewport.
65 * \param titlebar The titlebar.
67 void
68 titlebar_ban(wibox_t *titlebar)
70 /* Do it manually because client geometry remains unchanged. */
71 if(titlebar)
73 simple_window_t *sw = &titlebar->sw;
75 if(sw->window)
77 uint32_t request[] = { - sw->geometry.width, - sw->geometry.height };
78 /* Move the titlebar to the same place as the window. */
79 xcb_configure_window(globalconf.connection, sw->window,
80 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
81 request);
86 /** Get titlebar area.
87 * \param c The client
88 * \param geometry The client geometry including borders, excluding titlebars.
89 * \param res Pointer to area of titlebar, must be allocated already.
91 void
92 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
94 int height, width, x_offset = 0, y_offset = 0;
96 switch(c->titlebar->position)
98 default:
99 return;
100 case Top:
101 width = MAX(1, geometry.width);
102 switch(c->titlebar->align)
104 default:
105 break;
106 case AlignRight:
107 x_offset = geometry.width - width;
108 break;
109 case AlignCenter:
110 x_offset = (geometry.width - width) / 2;
111 break;
113 res->x = geometry.x + x_offset;
114 res->y = geometry.y - c->titlebar->sw.geometry.height;
115 res->width = width;
116 res->height = c->titlebar->sw.geometry.height;
117 break;
118 case Bottom:
119 width = MAX(1, geometry.width);
120 switch(c->titlebar->align)
122 default:
123 break;
124 case AlignRight:
125 x_offset = geometry.width - width;
126 break;
127 case AlignCenter:
128 x_offset = (geometry.width - width) / 2;
129 break;
131 res->x = geometry.x + x_offset;
132 res->y = geometry.y + geometry.height;
133 res->width = width;
134 res->height = c->titlebar->sw.geometry.height;
135 break;
136 case Left:
137 height = MAX(1, geometry.height);
138 switch(c->titlebar->align)
140 default:
141 break;
142 case AlignRight:
143 y_offset = geometry.height - height;
144 break;
145 case AlignCenter:
146 y_offset = (geometry.height - height) / 2;
147 break;
149 res->x = geometry.x - c->titlebar->sw.geometry.width;
150 res->y = geometry.y + y_offset;
151 res->width = c->titlebar->sw.geometry.width;
152 res->height = height;
153 break;
154 case Right:
155 height = MAX(1, geometry.height);
156 switch(c->titlebar->align)
158 default:
159 break;
160 case AlignRight:
161 y_offset = geometry.height - height;
162 break;
163 case AlignCenter:
164 y_offset = (geometry.height - height) / 2;
165 break;
167 res->x = geometry.x + geometry.width;
168 res->y = geometry.y + y_offset;
169 res->width = c->titlebar->sw.geometry.width;
170 res->height = height;
171 break;
175 /** Detach a wibox titlebar from its client.
176 * \param c The client.
178 void
179 titlebar_client_detach(client_t *c)
181 /* If client has a titlebar, kick it out. */
182 if(c && c->titlebar)
184 /* Update client geometry to exclude the titlebar. */
185 c->geometry = titlebar_geometry_remove(c->titlebar, 0, c->geometry);
186 simplewindow_wipe(&c->titlebar->sw);
187 c->titlebar->type = WIBOX_TYPE_NORMAL;
188 c->titlebar->screen = SCREEN_UNDEF;
189 wibox_unref(&c->titlebar);
190 c->titlebar = NULL;
191 client_need_arrange(c);
192 client_stack();
196 /** Attach a wibox to a client as its titlebar.
197 * \param c The client.
198 * \param t The wibox/titlebar.
200 void
201 titlebar_client_attach(client_t *c, wibox_t *t)
203 titlebar_client_detach(c);
205 if(c && t)
207 area_t wingeom;
209 /* check if titlebar is already on a client */
210 titlebar_client_detach(client_getbytitlebar(t));
212 /* check if client already has a titlebar. */
213 titlebar_client_detach(c);
215 c->titlebar = wibox_ref(&t);
216 t->type = WIBOX_TYPE_TITLEBAR;
217 t->screen = c->screen;
219 switch(t->position)
221 case Floating:
222 t->position = Top;
223 case Top:
224 case Bottom:
225 if(!t->sw.geometry.height)
226 t->sw.geometry.height = 1.5 * globalconf.font->height;
227 break;
228 case Left:
229 case Right:
230 if(!t->sw.geometry.width)
231 t->sw.geometry.width = 1.5 * globalconf.font->height;
232 break;
235 /* Update client geometry to include the titlebar. */
236 c->geometry = titlebar_geometry_add(c->titlebar, 0, c->geometry);
238 /* Client geometry without titlebar, but including borders, since that is always consistent. */
239 titlebar_geometry_compute(c, titlebar_geometry_remove(c->titlebar, 0, c->geometry), &wingeom);
241 simplewindow_init(&t->sw, c->phys_screen,
242 wingeom, 0, t->sw.orientation,
243 &t->sw.ctx.fg, &t->sw.ctx.bg);
244 simplewindow_border_color_set(&t->sw, &t->sw.border.color);
246 t->need_update = true;
248 /* Call update geometry. This will move the wibox to the right place,
249 * which might be the same as `wingeom', but then it will ban the
250 * titlebar if needed. */
251 titlebar_update_geometry(c);
253 if(t->isvisible)
254 xcb_map_window(globalconf.connection, t->sw.window);
256 client_need_arrange(c);
257 client_stack();
261 /** Map or unmap a titlebar wibox.
262 * \param t The wibox/titlebar.
263 * \param visible The new state of the titlebar.
265 void
266 titlebar_set_visible(wibox_t *t, bool visible)
268 if (visible != t->isvisible)
270 /* The price of (un)mapping something small like a titlebar is pretty cheap.
271 * It would complicate matters if this rare case was treated like clients.
272 * Clients are moved out of the viewport when banned.
274 if ((t->isvisible = visible))
275 xcb_map_window(globalconf.connection, t->sw.window);
276 else
277 xcb_unmap_window(globalconf.connection, t->sw.window);
279 globalconf.screens[t->screen].need_arrange = true;
280 client_stack();
284 /** Titlebar newindex.
285 * \param L The Lua VM state.
286 * \param titlebar The wibox titlebar.
287 * \param tok The attribute token.
288 * \return The number of elements pushed on stack.
291 luaA_titlebar_newindex(lua_State *L, wibox_t *titlebar, awesome_token_t tok)
293 client_t *c = NULL;
295 switch(tok)
297 position_t position;
298 int i;
299 size_t len;
300 const char *buf;
302 case A_TK_ALIGN:
303 if((buf = luaL_checklstring(L, 3, &len)))
304 titlebar->align = draw_align_fromstr(buf, len);
305 else
306 return 0;
307 break;
308 case A_TK_BORDER_WIDTH:
309 if((i = luaL_checknumber(L, 3)) >= 0)
310 simplewindow_border_width_set(&titlebar->sw, i);
311 else
312 return 0;
313 break;
314 case A_TK_BORDER_COLOR:
315 if((buf = luaL_checklstring(L, 3, &len)))
316 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar->sw.border.color, buf, len)))
317 simplewindow_border_color_set(&titlebar->sw, &titlebar->sw.border.color);
318 return 0;
319 case A_TK_POSITION:
320 buf = luaL_checklstring(L, 3, &len);
321 position = position_fromstr(buf, len);
322 if(position != titlebar->position)
324 switch(position)
326 case Left:
327 switch(titlebar->position)
329 int tmp;
330 case Left:
331 case Right:
332 break;
333 case Top:
334 case Bottom:
335 case Floating:
336 tmp = titlebar->sw.geometry.width;
337 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
338 titlebar->sw.geometry.height = tmp;
339 break;
341 simplewindow_orientation_set(&titlebar->sw, North);
342 break;
343 case Right:
344 switch(titlebar->position)
346 int tmp;
347 case Left:
348 case Right:
349 break;
350 case Top:
351 case Bottom:
352 case Floating:
353 tmp = titlebar->sw.geometry.width;
354 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
355 titlebar->sw.geometry.height = tmp;
356 break;
358 simplewindow_orientation_set(&titlebar->sw, South);
359 break;
360 case Top:
361 case Bottom:
362 case Floating:
363 switch(titlebar->position)
365 int tmp;
366 case Left:
367 case Right:
368 tmp = titlebar->sw.geometry.width;
369 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
370 titlebar->sw.geometry.height = tmp;
371 break;
372 case Top:
373 case Bottom:
374 case Floating:
375 break;
377 simplewindow_orientation_set(&titlebar->sw, East);
378 break;
380 titlebar->position = position;
381 if((c = client_getbytitlebar(titlebar)))
383 titlebar_update_geometry(c);
384 /* call geometry hook for client because some like to
385 * set titlebar width in that hook, which make sense */
386 hooks_property(c, "geometry");
389 break;
390 default:
391 return 0;
394 if((c || (c = client_getbytitlebar(titlebar))))
395 client_need_arrange(c);
397 return 0;
400 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80