awful: fix empty command adding
[awesome.git] / titlebar.c
blob3cf2ca4cab23271c9ce4466798b20a075571845d
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>
23 #include <xcb/xcb_aux.h>
25 #include <math.h>
27 #include "titlebar.h"
28 #include "client.h"
29 #include "widget.h"
31 extern awesome_t globalconf;
33 DO_LUA_NEW(extern, titlebar_t, titlebar, "titlebar", titlebar_ref)
34 DO_LUA_GC(titlebar_t, titlebar, "titlebar", titlebar_unref)
35 DO_LUA_EQ(titlebar_t, titlebar, "titlebar")
37 /** Get a client by its titlebar.
38 * \param titlebar The titlebar.
39 * \return A client.
41 client_t *
42 client_getbytitlebar(titlebar_t *titlebar)
44 client_t *c;
46 for(c = globalconf.clients; c; c = c->next)
47 if(c->titlebar == titlebar)
48 return c;
50 return NULL;
53 /** Get a client by its titlebar window.
54 * \param win The window.
55 * \return A client.
57 client_t *
58 client_getbytitlebarwin(xcb_window_t win)
60 client_t *c;
62 for(c = globalconf.clients; c; c = c->next)
63 if(c->titlebar && c->titlebar->sw && c->titlebar->sw->window == win)
64 return c;
66 return NULL;
69 /** Draw the titlebar content.
70 * \param c The client.
72 void
73 titlebar_draw(client_t *c)
75 xcb_drawable_t dw = 0;
76 draw_context_t *ctx;
77 xcb_screen_t *s;
79 if(!c || !c->titlebar || !c->titlebar->sw || !c->titlebar->position)
80 return;
82 s = xutil_screen_get(globalconf.connection,
83 c->titlebar->sw->phys_screen);
85 switch(c->titlebar->position)
87 case Off:
88 return;
89 case Right:
90 case Left:
91 dw = xcb_generate_id(globalconf.connection);
92 xcb_create_pixmap(globalconf.connection, s->root_depth,
93 dw,
94 s->root,
95 c->titlebar->sw->geometry.height,
96 c->titlebar->sw->geometry.width);
97 ctx = draw_context_new(globalconf.connection, c->titlebar->sw->phys_screen,
98 c->titlebar->sw->geometry.height,
99 c->titlebar->sw->geometry.width,
101 &c->titlebar->colors.fg,
102 &c->titlebar->colors.bg);
103 break;
104 default:
105 ctx = draw_context_new(globalconf.connection, c->titlebar->sw->phys_screen,
106 c->titlebar->sw->geometry.width,
107 c->titlebar->sw->geometry.height,
108 c->titlebar->sw->pixmap,
109 &c->titlebar->colors.fg,
110 &c->titlebar->colors.bg);
111 break;
114 widget_render(c->titlebar->widgets, ctx, c->titlebar->sw->gc, c->titlebar->sw->pixmap,
115 c->screen, c->titlebar->position,
116 c->titlebar->sw->geometry.x, c->titlebar->sw->geometry.y,
117 c->titlebar, AWESOME_TYPE_TITLEBAR);
119 switch(c->titlebar->position)
121 case Left:
122 draw_rotate(ctx, ctx->pixmap, c->titlebar->sw->pixmap,
123 ctx->width, ctx->height,
124 ctx->height, ctx->width,
125 - M_PI_2, 0, c->titlebar->sw->geometry.height);
126 xcb_free_pixmap(globalconf.connection, dw);
127 break;
128 case Right:
129 draw_rotate(ctx, ctx->pixmap, c->titlebar->sw->pixmap,
130 ctx->width, ctx->height,
131 ctx->height, ctx->width,
132 M_PI_2, c->titlebar->sw->geometry.width, 0);
133 xcb_free_pixmap(globalconf.connection, dw);
134 default:
135 break;
138 simplewindow_refresh_pixmap(c->titlebar->sw);
140 draw_context_delete(&ctx);
142 c->titlebar->need_update = false;
145 /** Titlebar refresh function.
147 void
148 titlebar_refresh(void)
150 client_t *c;
152 for(c = globalconf.clients; c; c = c->next)
153 if(c->titlebar && c->titlebar->need_update)
154 titlebar_draw(c);
157 void
158 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
160 int width, x_offset = 0, y_offset = 0;
162 switch(c->titlebar->position)
164 default:
165 return;
166 case Top:
167 if(c->titlebar->width)
168 width = MAX(1, MIN(c->titlebar->width, geometry.width - 2 * c->titlebar->border.width));
169 else
170 width = MAX(1, geometry.width + 2 * c->border - 2 * c->titlebar->border.width);
171 switch(c->titlebar->align)
173 default:
174 break;
175 case AlignRight:
176 x_offset = 2 * c->border + geometry.width - width - 2 * c->titlebar->border.width;
177 break;
178 case AlignCenter:
179 x_offset = (geometry.width - width) / 2;
180 break;
182 res->x = geometry.x + x_offset;
183 res->y = geometry.y - c->titlebar->height - 2 * c->titlebar->border.width + c->border;
184 res->width = width;
185 res->height = c->titlebar->height;
186 break;
187 case Bottom:
188 if(c->titlebar->width)
189 width = MAX(1, MIN(c->titlebar->width, geometry.width - 2 * c->titlebar->border.width));
190 else
191 width = MAX(1, geometry.width + 2 * c->border - 2 * c->titlebar->border.width);
192 switch(c->titlebar->align)
194 default:
195 break;
196 case AlignRight:
197 x_offset = 2 * c->border + geometry.width - width - 2 * c->titlebar->border.width;
198 break;
199 case AlignCenter:
200 x_offset = (geometry.width - width) / 2;
201 break;
203 res->x = geometry.x + x_offset;
204 res->y = geometry.y + geometry.height + c->border;
205 res->width = width;
206 res->height = c->titlebar->height;
207 break;
208 case Left:
209 if(c->titlebar->width)
210 width = MAX(1, MIN(c->titlebar->width, geometry.height - 2 * c->titlebar->border.width));
211 else
212 width = MAX(1, geometry.height + 2 * c->border - 2 * c->titlebar->border.width);
213 switch(c->titlebar->align)
215 default:
216 break;
217 case AlignRight:
218 y_offset = 2 * c->border + geometry.height - width - 2 * c->titlebar->border.width;
219 break;
220 case AlignCenter:
221 y_offset = (geometry.height - width) / 2;
222 break;
224 res->x = geometry.x - c->titlebar->height + c->border;
225 res->y = geometry.y + y_offset;
226 res->width = c->titlebar->height;
227 res->height = width;
228 break;
229 case Right:
230 if(c->titlebar->width)
231 width = MAX(1, MIN(c->titlebar->width, geometry.height - 2 * c->titlebar->border.width));
232 else
233 width = MAX(1, geometry.height + 2 * c->border - 2 * c->titlebar->border.width);
234 switch(c->titlebar->align)
236 default:
237 break;
238 case AlignRight:
239 y_offset = 2 * c->border + geometry.height - width - 2 * c->titlebar->border.width;
240 break;
241 case AlignCenter:
242 y_offset = (geometry.height - width) / 2;
243 break;
245 res->x = geometry.x + geometry.width + c->border;
246 res->y = geometry.y + y_offset;
247 res->width = c->titlebar->height;
248 res->height = width;
249 break;
253 /** Set client titlebar position.
254 * \param c The client.
256 void
257 titlebar_init(client_t *c)
259 int width = 0, height = 0;
260 area_t geom;
262 switch(c->titlebar->position)
264 default:
265 c->titlebar->position = Off;
266 return;
267 case Top:
268 case Bottom:
269 if(c->titlebar->width)
270 width = MIN(c->titlebar->width, c->geometry.width - 2 * c->titlebar->border.width);
271 else
272 width = c->geometry.width + 2 * c->border - 2 * c->titlebar->border.width;
273 height = c->titlebar->height;
274 break;
275 case Left:
276 case Right:
277 if(c->titlebar->width)
278 height = MIN(c->titlebar->width, c->geometry.height - 2 * c->titlebar->border.width);
279 else
280 height = c->geometry.height + 2 * c->border - 2 * c->titlebar->border.width;
281 width = c->titlebar->height;
282 break;
285 titlebar_geometry_compute(c, c->geometry, &geom);
287 c->titlebar->sw = simplewindow_new(globalconf.connection, c->phys_screen, geom.x, geom.y,
288 geom.width, geom.height, c->titlebar->border.width);
290 if(c->titlebar->border.width)
291 xcb_change_window_attributes(globalconf.connection, c->titlebar->sw->window,
292 XCB_CW_BORDER_PIXEL, &c->titlebar->border.color.pixel);
294 if(client_isvisible(c, c->screen))
295 globalconf.screens[c->screen].need_arrange = true;
297 c->titlebar->need_update = true;
300 /** Create a new titlebar.
301 * \param L The Lua VM state.
302 * \return The number of value pushed.
304 * \luastack
305 * \lparam A table with values: align, position, fg, bg, border_width,
306 * border_color, width and height.
307 * \lreturn A brand new titlebar.
309 static int
310 luaA_titlebar_new(lua_State *L)
312 titlebar_t *tb;
313 const char *buf;
314 size_t len;
316 luaA_checktable(L, 2);
318 tb = p_new(titlebar_t, 1);
320 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
321 tb->align = draw_align_fromstr(buf, len);
323 tb->width = luaA_getopt_number(L, 2, "width", 0);
324 tb->height = luaA_getopt_number(L, 2, "height", 0);
325 if(tb->height <= 0)
326 /* 1.5 as default factor, it fits nice but no one knows why */
327 tb->height = 1.5 * globalconf.font->height;
329 buf = luaA_getopt_lstring(L, 2, "position", "top", &len);
330 tb->position = position_fromstr(buf, len);
332 tb->colors.fg = globalconf.colors.fg;
333 if((buf = luaA_getopt_lstring(L, 2, "fg", NULL, &len)))
334 xcolor_init(&tb->colors.fg, globalconf.connection,
335 globalconf.default_screen, buf, len);
337 tb->colors.bg = globalconf.colors.bg;
338 if((buf = luaA_getopt_lstring(L, 2, "bg", NULL, &len)))
339 xcolor_init(&tb->colors.bg, globalconf.connection,
340 globalconf.default_screen, buf, len);
342 tb->border.color = globalconf.colors.fg;
343 if((buf = luaA_getopt_lstring(L, 2, "border_color", NULL, &len)))
344 xcolor_init(&tb->border.color, globalconf.connection,
345 globalconf.default_screen, buf, len);
347 tb->border.width = luaA_getopt_number(L, 2, "border_width", 0);
349 return luaA_titlebar_userdata_new(globalconf.L, tb);
352 /** Titlebar newindex.
353 * \param L The Lua VM state.
354 * \return The number of elements pushed on stack.
356 static int
357 luaA_titlebar_newindex(lua_State *L)
359 size_t len;
360 titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
361 const char *buf, *attr = luaL_checklstring(L, 2, &len);
362 client_t *c = NULL, **newc;
363 int i;
364 widget_node_t *witer;
365 position_t position;
367 switch(a_tokenize(attr, len))
369 case A_TK_CLIENT:
370 if(!lua_isnil(L, 3))
371 newc = luaA_checkudata(L, 3, "client");
372 else
373 newc = NULL;
375 if(newc)
377 if((*newc)->titlebar)
379 simplewindow_delete(&(*newc)->titlebar->sw);
380 titlebar_unref(&(*newc)->titlebar);
381 globalconf.screens[(*newc)->screen].need_arrange = true;
383 /* Attach titlebar to client */
384 (*newc)->titlebar = *titlebar;
385 titlebar_ref(titlebar);
386 titlebar_init(*newc);
387 c = *newc;
389 else
391 if((c = client_getbytitlebar(*titlebar)))
393 simplewindow_delete(&(*titlebar)->sw);
394 /* unref and NULL the ref */
395 titlebar_unref(&c->titlebar);
396 globalconf.screens[c->screen].need_arrange = true;
399 break;
400 case A_TK_ALIGN:
401 if((buf = luaL_checklstring(L, 3, &len)))
402 (*titlebar)->align = draw_align_fromstr(buf, len);
403 else
404 return 0;
405 break;
406 case A_TK_BORDER_WIDTH:
407 if((i = luaL_checknumber(L, 3)) >= 0)
408 (*titlebar)->border.width = i;
409 else
410 return 0;
411 break;
412 case A_TK_BORDER_COLOR:
413 if((buf = luaL_checklstring(L, 3, &len)))
414 if(xcolor_init(&(*titlebar)->border.color, globalconf.connection,
415 globalconf.default_screen, buf, len))
416 if((*titlebar)->sw)
417 xcb_change_window_attributes(globalconf.connection, (*titlebar)->sw->window,
418 XCB_CW_BORDER_PIXEL, &(*titlebar)->border.color.pixel);
419 return 0;
420 case A_TK_FG:
421 if((buf = luaL_checklstring(L, 3, &len)))
422 if(xcolor_init(&(*titlebar)->colors.fg, globalconf.connection,
423 globalconf.default_screen, buf, len))
424 (*titlebar)->need_update = true;
425 return 0;
426 case A_TK_BG:
427 if((buf = luaL_checklstring(L, 3, &len)))
428 if(xcolor_init(&(*titlebar)->colors.bg, globalconf.connection,
429 globalconf.default_screen, buf, len))
430 (*titlebar)->need_update = true;
431 return 0;
432 case A_TK_WIDGETS:
433 luaA_checktable(L, 3);
435 /* remove all widgets */
436 for(witer = (*titlebar)->widgets; witer; witer = (*titlebar)->widgets)
438 if(witer->widget->detach)
439 witer->widget->detach(witer->widget, *titlebar);
440 widget_unref(&witer->widget);
441 widget_node_list_detach(&(*titlebar)->widgets, witer);
442 p_delete(&witer);
445 (*titlebar)->need_update = true;
447 /* now read all widgets and add them */
448 lua_pushnil(L);
449 while(lua_next(L, 3))
451 widget_t **widget = luaA_checkudata(L, -1, "widget");
452 widget_node_t *w = p_new(widget_node_t, 1);
453 w->widget = *widget;
454 widget_node_list_append(&(*titlebar)->widgets, w);
455 widget_ref(widget);
456 lua_pop(L, 1);
458 break;
459 case A_TK_POSITION:
460 buf = luaL_checklstring(L, 3, &len);
461 position = position_fromstr(buf, len);
462 if(position != (*titlebar)->position)
464 (*titlebar)->position = position;
465 c = client_getbytitlebar(*titlebar);
466 simplewindow_delete(&c->titlebar->sw);
467 titlebar_init(c);
469 break;
470 default:
471 return 0;
474 if((c || (c = client_getbytitlebar(*titlebar)))
475 && client_isvisible(c, c->screen))
476 globalconf.screens[c->screen].need_arrange = true;
478 return 0;
481 /** Titlebar object.
482 * \param L The Lua VM state.
483 * \return The number of elements pushed on stack.
484 * \luastack
485 * \lfield client The client attached to this titlebar.
486 * \lfield align Alignment relative to the client.
487 * \lfield border_width Border width.
488 * \lfield border_color Border color.
489 * \lfield fg Foreground color.
490 * \lfield bg Background color.
491 * \lfield position Position.
493 static int
494 luaA_titlebar_index(lua_State *L)
496 size_t len;
497 titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
498 const char *attr = luaL_checklstring(L, 2, &len);
499 client_t *c;
500 widget_node_t *witer;
501 int i = 0;
503 if(luaA_usemetatable(L, 1, 2))
504 return 1;
506 switch(a_tokenize(attr, len))
508 case A_TK_CLIENT:
509 if((c = client_getbytitlebar(*titlebar)))
510 return luaA_client_userdata_new(L, c);
511 else
512 return 0;
513 case A_TK_ALIGN:
514 lua_pushstring(L, draw_align_tostr((*titlebar)->align));
515 break;
516 case A_TK_BORDER_WIDTH:
517 lua_pushnumber(L, (*titlebar)->border.width);
518 break;
519 case A_TK_BORDER_COLOR:
520 luaA_pushcolor(L, &(*titlebar)->border.color);
521 break;
522 case A_TK_FG:
523 luaA_pushcolor(L, &(*titlebar)->colors.fg);
524 break;
525 case A_TK_BG:
526 luaA_pushcolor(L, &(*titlebar)->colors.bg);
527 break;
528 case A_TK_WIDGETS:
529 lua_newtable(L);
530 for(witer = (*titlebar)->widgets; witer; witer = witer->next)
532 luaA_widget_userdata_new(L, witer->widget);
533 lua_rawseti(L, -2, ++i);
535 break;
536 case A_TK_POSITION:
537 lua_pushstring(L, position_tostr((*titlebar)->position));
538 break;
539 default:
540 return 0;
543 return 1;
546 /** Convert a titlebar to a printable string.
547 * \param L The Lua VM state.
548 * \return The number of value pushed.
550 * \luastack
551 * \lvalue A titlebar.
552 * \lreturn A string.
554 static int
555 luaA_titlebar_tostring(lua_State *L)
557 titlebar_t **p = luaA_checkudata(L, 1, "titlebar");
558 lua_pushfstring(L, "[titlebar udata(%p)]", *p);
559 return 1;
562 const struct luaL_reg awesome_titlebar_methods[] =
564 { "__call", luaA_titlebar_new },
565 { NULL, NULL }
567 const struct luaL_reg awesome_titlebar_meta[] =
569 { "__index", luaA_titlebar_index },
570 { "__newindex", luaA_titlebar_newindex },
571 { "__eq", luaA_titlebar_eq },
572 { "__gc", luaA_titlebar_gc },
573 { "__tostring", luaA_titlebar_tostring },
574 { NULL, NULL }
577 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80