button, key: emit events
[awesome.git] / titlebar.c
bloba5847d74db597b91dad03e7274e8b84d9e8c74c8
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->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;
69 if(titlebar->window)
70 xcb_unmap_window(globalconf.connection, titlebar->window);
72 /* Remove titlebar geometry from client. */
73 if((c = client_getbytitlebar(titlebar)))
74 c->geometry = titlebar_geometry_remove(titlebar, 0, c->geometry);
76 titlebar->isbanned = true;
80 /** Move a titlebar on top of its client.
81 * \param titlebar The titlebar.
83 void
84 titlebar_unban(wibox_t *titlebar)
86 /* Do this manually because the system doesn't know we moved the toolbar.
87 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
88 * end up offscreen anyway. */
89 if(titlebar && titlebar->isbanned)
91 client_t *c;
93 if(titlebar->window)
94 xcb_map_window(globalconf.connection, titlebar->window);
96 titlebar->isbanned = false;
98 /* Add titlebar geometry from client. */
99 if((c = client_getbytitlebar(titlebar)))
100 c->geometry = titlebar_geometry_add(titlebar, 0, c->geometry);
104 /** Get titlebar area.
105 * \param c The client
106 * \param geometry The client geometry including borders, excluding titlebars.
107 * \param res Pointer to area of titlebar, must be allocated already.
109 void
110 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
112 int height, width, x_offset = 0, y_offset = 0;
114 switch(c->titlebar->position)
116 default:
117 return;
118 case Top:
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 - c->titlebar->geometry.height;
133 res->width = width;
134 res->height = c->titlebar->geometry.height;
135 break;
136 case Bottom:
137 width = MAX(1, geometry.width);
138 switch(c->titlebar->align)
140 default:
141 break;
142 case AlignRight:
143 x_offset = geometry.width - width;
144 break;
145 case AlignCenter:
146 x_offset = (geometry.width - width) / 2;
147 break;
149 res->x = geometry.x + x_offset;
150 res->y = geometry.y + geometry.height;
151 res->width = width;
152 res->height = c->titlebar->geometry.height;
153 break;
154 case Left:
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 - c->titlebar->geometry.width;
168 res->y = geometry.y + y_offset;
169 res->width = c->titlebar->geometry.width;
170 res->height = height;
171 break;
172 case Right:
173 height = MAX(1, geometry.height);
174 switch(c->titlebar->align)
176 default:
177 break;
178 case AlignRight:
179 y_offset = geometry.height - height;
180 break;
181 case AlignCenter:
182 y_offset = (geometry.height - height) / 2;
183 break;
185 res->x = geometry.x + geometry.width;
186 res->y = geometry.y + y_offset;
187 res->width = c->titlebar->geometry.width;
188 res->height = height;
189 break;
193 /** Detach a wibox titlebar from its client.
194 * \param c The client.
196 void
197 titlebar_client_detach(client_t *c)
199 /* If client has a titlebar, kick it out. */
200 if(c && c->titlebar)
202 /* Update client geometry to exclude the titlebar. */
203 c->geometry = titlebar_geometry_remove(c->titlebar, 0, c->geometry);
204 wibox_wipe(c->titlebar);
205 c->titlebar->type = WIBOX_TYPE_NORMAL;
206 c->titlebar->screen = NULL;
208 wibox_unref(globalconf.L, c->titlebar);
209 c->titlebar = NULL;
211 hook_property(client, c, "titlebar");
212 client_stack();
216 /** Attach a wibox to a client as its titlebar.
217 * \param c The client.
219 void
220 titlebar_client_attach(client_t *c)
222 /* check if we can register the object */
223 wibox_t *t = wibox_ref(globalconf.L, -1);
225 titlebar_client_detach(c);
227 /* check if titlebar is already on a client */
228 titlebar_client_detach(client_getbytitlebar(t));
230 /* check if client already has a titlebar. */
231 titlebar_client_detach(c);
233 /* set the object as new client's titlebar */
234 c->titlebar = t;
236 t->type = WIBOX_TYPE_TITLEBAR;
237 t->screen = c->screen;
239 switch(t->position)
241 case Top:
242 case Bottom:
243 if(!t->geometry.height)
244 t->geometry.height = 1.5 * globalconf.font->height;
245 break;
246 case Left:
247 case Right:
248 if(!t->geometry.width)
249 t->geometry.width = 1.5 * globalconf.font->height;
250 break;
253 /* Update client geometry to include the titlebar. */
254 c->geometry = titlebar_geometry_add(c->titlebar, 0, c->geometry);
256 /* Client geometry without titlebar, but including borders, since that is always consistent. */
257 titlebar_geometry_compute(c, titlebar_geometry_remove(c->titlebar, 0, c->geometry), &t->geometry);
259 wibox_init(t, c->phys_screen);
261 t->need_update = true;
263 /* Call update geometry. This will move the wibox to the right place,
264 * which might be the same as `wingeom', but then it will ban the
265 * titlebar if needed. */
266 titlebar_update_geometry(c);
268 xcb_map_window(globalconf.connection, t->window);
270 hook_property(client, c, "titlebar");
271 client_stack();
274 /** Map or unmap a titlebar wibox.
275 * \param t The wibox/titlebar.
276 * \param visible The new state of the titlebar.
278 void
279 titlebar_set_visible(wibox_t *t, bool visible)
281 if(visible != t->isvisible)
283 if((t->isvisible = visible))
284 titlebar_unban(t);
285 else
286 titlebar_ban(t);
288 hook_property(wibox, t, "visible");
289 client_stack();
293 /** Titlebar newindex.
294 * \param L The Lua VM state.
295 * \param titlebar The wibox titlebar.
296 * \param tok The attribute token.
297 * \return The number of elements pushed on stack.
300 luaA_titlebar_newindex(lua_State *L, wibox_t *titlebar, awesome_token_t tok)
302 client_t *c = NULL;
304 switch(tok)
306 position_t position;
307 int i;
308 size_t len;
309 const char *buf;
311 case A_TK_ALIGN:
312 if((buf = luaL_checklstring(L, 3, &len)))
313 titlebar->align = draw_align_fromstr(buf, len);
314 else
315 return 0;
316 break;
317 case A_TK_BORDER_WIDTH:
318 if((i = luaL_checknumber(L, 3)) >= 0)
319 wibox_border_width_set(titlebar, i);
320 else
321 return 0;
322 break;
323 case A_TK_BORDER_COLOR:
324 if((buf = luaL_checklstring(L, 3, &len)))
325 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar->border.color, buf, len)))
326 wibox_border_color_set(titlebar, &titlebar->border.color);
327 return 0;
328 case A_TK_POSITION:
329 buf = luaL_checklstring(L, 3, &len);
330 position = position_fromstr(buf, len);
331 if(position != titlebar->position)
333 switch(position)
335 case Left:
336 switch(titlebar->position)
338 int tmp;
339 case Left:
340 case Right:
341 break;
342 case Top:
343 case Bottom:
344 tmp = titlebar->geometry.width;
345 titlebar->geometry.width = titlebar->geometry.height;
346 titlebar->geometry.height = tmp;
347 break;
349 wibox_orientation_set(titlebar, North);
350 break;
351 case Right:
352 switch(titlebar->position)
354 int tmp;
355 case Left:
356 case Right:
357 break;
358 case Top:
359 case Bottom:
360 tmp = titlebar->geometry.width;
361 titlebar->geometry.width = titlebar->geometry.height;
362 titlebar->geometry.height = tmp;
363 break;
365 wibox_orientation_set(titlebar, South);
366 break;
367 case Top:
368 case Bottom:
369 switch(titlebar->position)
371 int tmp;
372 case Left:
373 case Right:
374 tmp = titlebar->geometry.width;
375 titlebar->geometry.width = titlebar->geometry.height;
376 titlebar->geometry.height = tmp;
377 break;
378 case Top:
379 case Bottom:
380 break;
382 wibox_orientation_set(titlebar, East);
383 break;
385 titlebar->position = position;
386 if((c = client_getbytitlebar(titlebar)))
388 titlebar_update_geometry(c);
389 /* call geometry hook for client because some like to
390 * set titlebar width in that hook, which make sense */
391 hook_property(client, c, "geometry");
394 break;
395 default:
396 return 0;
399 return 0;
402 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80