various documentation update for _index()
[awesome.git] / titlebar.c
blob7adfc7b86aac053808aa7259503212aae05263a7
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, c->titlebar);
118 switch(c->titlebar->position)
120 case Left:
121 draw_rotate(ctx, ctx->pixmap, c->titlebar->sw->pixmap,
122 ctx->width, ctx->height,
123 ctx->height, ctx->width,
124 - M_PI_2, 0, c->titlebar->sw->geometry.height);
125 xcb_free_pixmap(globalconf.connection, dw);
126 break;
127 case Right:
128 draw_rotate(ctx, ctx->pixmap, c->titlebar->sw->pixmap,
129 ctx->width, ctx->height,
130 ctx->height, ctx->width,
131 M_PI_2, c->titlebar->sw->geometry.width, 0);
132 xcb_free_pixmap(globalconf.connection, dw);
133 default:
134 break;
137 simplewindow_refresh_pixmap(c->titlebar->sw);
139 draw_context_delete(&ctx);
142 void
143 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
145 int width, x_offset = 0, y_offset = 0;
147 switch(c->titlebar->position)
149 default:
150 return;
151 case Top:
152 if(c->titlebar->width)
153 width = MAX(1, MIN(c->titlebar->width, geometry.width - 2 * c->titlebar->border.width));
154 else
155 width = MAX(1, geometry.width + 2 * c->border - 2 * c->titlebar->border.width);
156 switch(c->titlebar->align)
158 default:
159 break;
160 case AlignRight:
161 x_offset = 2 * c->border + geometry.width - width - 2 * c->titlebar->border.width;
162 break;
163 case AlignCenter:
164 x_offset = (geometry.width - width) / 2;
165 break;
167 res->x = geometry.x + x_offset;
168 res->y = geometry.y - c->titlebar->height - 2 * c->titlebar->border.width + c->border;
169 res->width = width;
170 res->height = c->titlebar->height;
171 break;
172 case Bottom:
173 if(c->titlebar->width)
174 width = MAX(1, MIN(c->titlebar->width, geometry.width - 2 * c->titlebar->border.width));
175 else
176 width = MAX(1, geometry.width + 2 * c->border - 2 * c->titlebar->border.width);
177 switch(c->titlebar->align)
179 default:
180 break;
181 case AlignRight:
182 x_offset = 2 * c->border + geometry.width - width - 2 * c->titlebar->border.width;
183 break;
184 case AlignCenter:
185 x_offset = (geometry.width - width) / 2;
186 break;
188 res->x = geometry.x + x_offset;
189 res->y = geometry.y + geometry.height + c->border;
190 res->width = width;
191 res->height = c->titlebar->height;
192 break;
193 case Left:
194 if(c->titlebar->width)
195 width = MAX(1, MIN(c->titlebar->width, geometry.height - 2 * c->titlebar->border.width));
196 else
197 width = MAX(1, geometry.height + 2 * c->border - 2 * c->titlebar->border.width);
198 switch(c->titlebar->align)
200 default:
201 break;
202 case AlignRight:
203 y_offset = 2 * c->border + geometry.height - width - 2 * c->titlebar->border.width;
204 break;
205 case AlignCenter:
206 y_offset = (geometry.height - width) / 2;
207 break;
209 res->x = geometry.x - c->titlebar->height + c->border;
210 res->y = geometry.y + y_offset;
211 res->width = c->titlebar->height;
212 res->height = width;
213 break;
214 case Right:
215 if(c->titlebar->width)
216 width = MAX(1, MIN(c->titlebar->width, geometry.height - 2 * c->titlebar->border.width));
217 else
218 width = MAX(1, geometry.height + 2 * c->border - 2 * c->titlebar->border.width);
219 switch(c->titlebar->align)
221 default:
222 break;
223 case AlignRight:
224 y_offset = 2 * c->border + geometry.height - width - 2 * c->titlebar->border.width;
225 break;
226 case AlignCenter:
227 y_offset = (geometry.height - width) / 2;
228 break;
230 res->x = geometry.x + geometry.width + c->border;
231 res->y = geometry.y + y_offset;
232 res->width = c->titlebar->height;
233 res->height = width;
234 break;
238 /** Set client titlebar position.
239 * \param c The client.
241 void
242 titlebar_init(client_t *c)
244 int width = 0, height = 0;
245 area_t geom;
247 switch(c->titlebar->position)
249 default:
250 c->titlebar->position = Off;
251 return;
252 case Top:
253 case Bottom:
254 if(c->titlebar->width)
255 width = MIN(c->titlebar->width, c->geometry.width - 2 * c->titlebar->border.width);
256 else
257 width = c->geometry.width + 2 * c->border - 2 * c->titlebar->border.width;
258 height = c->titlebar->height;
259 break;
260 case Left:
261 case Right:
262 if(c->titlebar->width)
263 height = MIN(c->titlebar->width, c->geometry.height - 2 * c->titlebar->border.width);
264 else
265 height = c->geometry.height + 2 * c->border - 2 * c->titlebar->border.width;
266 width = c->titlebar->height;
267 break;
270 titlebar_geometry_compute(c, c->geometry, &geom);
272 c->titlebar->sw = simplewindow_new(globalconf.connection, c->phys_screen, geom.x, geom.y,
273 geom.width, geom.height, c->titlebar->border.width);
275 if(c->titlebar->border.width)
276 xcb_change_window_attributes(globalconf.connection, c->titlebar->sw->window,
277 XCB_CW_BORDER_PIXEL, &c->titlebar->border.color.pixel);
279 if(client_isvisible(c, c->screen))
280 globalconf.screens[c->screen].need_arrange = true;
282 titlebar_draw(c);
285 /** Create a new titlebar.
286 * \param L The Lua VM state.
287 * \return The number of value pushed.
289 * \luastack
290 * \lparam A table with values: align, position, fg, bg, border_width,
291 * border_color, width and height.
292 * \lreturn A brand new titlebar.
294 static int
295 luaA_titlebar_new(lua_State *L)
297 titlebar_t *tb;
298 const char *buf;
299 size_t len;
301 luaA_checktable(L, 2);
303 tb = p_new(titlebar_t, 1);
305 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
306 tb->align = draw_align_fromstr(buf, len);
308 tb->width = luaA_getopt_number(L, 2, "width", 0);
309 tb->height = luaA_getopt_number(L, 2, "height", 0);
310 if(tb->height <= 0)
311 /* 1.5 as default factor, it fits nice but no one knows why */
312 tb->height = 1.5 * globalconf.font->height;
314 buf = luaA_getopt_lstring(L, 2, "position", "top", &len);
315 tb->position = position_fromstr(buf, len);
317 tb->colors.fg = globalconf.colors.fg;
318 if((buf = luaA_getopt_lstring(L, 2, "fg", NULL, &len)))
319 xcolor_init(&tb->colors.fg, globalconf.connection,
320 globalconf.default_screen, buf, len);
322 tb->colors.bg = globalconf.colors.bg;
323 if((buf = luaA_getopt_lstring(L, 2, "bg", NULL, &len)))
324 xcolor_init(&tb->colors.bg, globalconf.connection,
325 globalconf.default_screen, buf, len);
327 tb->border.color = globalconf.colors.fg;
328 if((buf = luaA_getopt_lstring(L, 2, "border_color", NULL, &len)))
329 xcolor_init(&tb->border.color, globalconf.connection,
330 globalconf.default_screen, buf, len);
332 tb->border.width = luaA_getopt_number(L, 2, "border_width", 0);
334 return luaA_titlebar_userdata_new(globalconf.L, tb);
337 /** Add a widget to a titlebar.
338 * \param L The Lua VM state.
339 * \return The number of value pushed.
341 * \luastack
342 * \lvalue A titlebar.
343 * \lparam A widget.
345 static int
346 luaA_titlebar_widget_add(lua_State *L)
348 titlebar_t **tb = luaA_checkudata(L, 1, "titlebar");
349 widget_t **widget = luaA_checkudata(L, 2, "widget");
350 widget_node_t *witer, *w;
352 if((*widget)->type == systray_new)
353 luaL_error(L, "cannot add systray widget to titlebar");
355 /* check that there is not already a widget with that name in the titlebar */
356 for(witer = (*tb)->widgets; witer; witer = witer->next)
357 if(witer->widget != *widget
358 && !a_strcmp(witer->widget->name, (*widget)->name))
359 luaL_error(L, "a widget with name `%s' already on titlebar", witer->widget->name);
361 w = p_new(widget_node_t, 1);
363 w->widget = *widget;
364 widget_node_list_append(&(*tb)->widgets, w);
365 widget_ref(widget);
367 titlebar_draw(client_getbytitlebar(*tb));
369 return 0;
372 /** Remove a widget from a titlebar.
373 * \param L The Lua VM State.
375 * \luastack
376 * \lvalue A statusbar.
377 * \lparam A widget.
379 static int
380 luaA_titlebar_widget_remove(lua_State *L)
382 titlebar_t **tb = luaA_checkudata(L, 1, "titlebar");
383 widget_t **widget = luaA_checkudata(L, 2, "widget");
384 widget_node_t *w, *wnext;
386 for(w = (*tb)->widgets; w; w = wnext)
388 wnext = w->next;
389 if(w->widget == *widget)
391 widget_unref(widget);
392 widget_node_list_detach(&(*tb)->widgets, w);
393 p_delete(&w);
394 titlebar_draw(client_getbytitlebar(*tb));
398 return 0;
402 /** Get all widgets from a titlebar.
403 * \param L The Lua VM state.
404 * \return The number of value pushed.
406 * \luastack
407 * \lvalue A titlebar
408 * \lreturn A table with all widgets from the titlebar.
410 static int
411 luaA_titlebar_widget_get(lua_State *L)
413 titlebar_t **tb = luaA_checkudata(L, 1, "titlebar");
414 widget_node_t *witer;
416 lua_newtable(L);
418 for(witer = (*tb)->widgets; witer; witer = witer->next)
420 luaA_widget_userdata_new(L, witer->widget);
421 /* ref again for the list */
422 widget_ref(&witer->widget);
423 lua_setfield(L, -2, witer->widget->name);
426 return 1;
429 /** Get the client which the titlebar is attached to. That is a the same as
430 * checking if every clients's titlebar is equal to titlebar.
431 * \param L The Lua VM state.
433 * \luastack
434 * \lvalue A titlebar.
435 * \lreturn A client if the titlebar is attached, nil otherwise.
437 static int
438 luaA_titlebar_client_get(lua_State *L)
440 titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
441 client_t *c;
443 if((c = client_getbytitlebar(*titlebar)))
444 return luaA_client_userdata_new(L, c);
446 return 0;
449 /** Titlebar newindex.
450 * \param L The Lua VM state.
451 * \return The number of elements pushed on stack.
453 static int
454 luaA_titlebar_newindex(lua_State *L)
456 size_t len;
457 titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
458 const char *buf, *attr = luaL_checklstring(L, 2, &len);
459 client_t *c;
460 int i;
462 switch(a_tokenize(attr, len))
464 case A_TK_ALIGN:
465 if((buf = luaL_checklstring(L, 3, &len)))
466 (*titlebar)->align = draw_align_fromstr(buf, len);
467 else
468 return 0;
469 break;
470 case A_TK_BORDER_WIDTH:
471 if((i = luaL_checknumber(L, 3)) >= 0)
472 (*titlebar)->border.width = i;
473 else
474 return 0;
475 break;
476 case A_TK_BORDER_COLOR:
477 if((buf = luaL_checklstring(L, 3, &len)))
478 if(xcolor_init(&(*titlebar)->border.color, globalconf.connection,
479 globalconf.default_screen, buf, len))
480 if((*titlebar)->sw)
481 xcb_change_window_attributes(globalconf.connection, (*titlebar)->sw->window,
482 XCB_CW_BORDER_PIXEL, &(*titlebar)->border.color.pixel);
483 return 0;
484 case A_TK_FG:
485 if((buf = luaL_checklstring(L, 3, &len)))
486 if(xcolor_init(&(*titlebar)->colors.fg, globalconf.connection,
487 globalconf.default_screen, buf, len))
488 titlebar_draw(client_getbytitlebar(*titlebar));
489 return 0;
490 case A_TK_BG:
491 if((buf = luaL_checklstring(L, 3, &len)))
492 if(xcolor_init(&(*titlebar)->colors.bg, globalconf.connection,
493 globalconf.default_screen, buf, len))
494 titlebar_draw(client_getbytitlebar(*titlebar));
495 return 0;
496 default:
497 return 0;
500 if((c = client_getbytitlebar(*titlebar))
501 && client_isvisible(c, c->screen))
502 globalconf.screens[c->screen].need_arrange = true;
504 return 0;
507 /** Titlebar object.
508 * \param L The Lua VM state.
509 * \return The number of elements pushed on stack.
510 * \luastack
511 * \lfield align Alignment relative to the client.
512 * \lfield border_width Border width.
513 * \lfield border_color Border color.
514 * \lfield fg Foreground color.
515 * \lfield bg Background color.
517 static int
518 luaA_titlebar_index(lua_State *L)
520 size_t len;
521 titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
522 const char *attr = luaL_checklstring(L, 2, &len);
524 if(luaA_usemetatable(L, 1, 2))
525 return 1;
527 switch(a_tokenize(attr, len))
529 case A_TK_ALIGN:
530 lua_pushstring(L, draw_align_tostr((*titlebar)->align));
531 break;
532 case A_TK_BORDER_WIDTH:
533 lua_pushnumber(L, (*titlebar)->border.width);
534 break;
535 case A_TK_BORDER_COLOR:
536 luaA_pushcolor(L, &(*titlebar)->border.color);
537 break;
538 case A_TK_FG:
539 luaA_pushcolor(L, &(*titlebar)->colors.fg);
540 break;
541 case A_TK_BG:
542 luaA_pushcolor(L, &(*titlebar)->colors.bg);
543 break;
544 default:
545 return 0;
548 return 1;
551 /** Convert a titlebar to a printable string.
552 * \param L The Lua VM state.
553 * \return The number of value pushed.
555 * \luastack
556 * \lvalue A titlebar.
557 * \lreturn A string.
559 static int
560 luaA_titlebar_tostring(lua_State *L)
562 titlebar_t **p = luaA_checkudata(L, 1, "titlebar");
563 lua_pushfstring(L, "[titlebar udata(%p)]", *p);
564 return 1;
567 const struct luaL_reg awesome_titlebar_methods[] =
569 { "__call", luaA_titlebar_new },
570 { NULL, NULL }
572 const struct luaL_reg awesome_titlebar_meta[] =
574 { "widget_add", luaA_titlebar_widget_add },
575 { "widget_remove", luaA_titlebar_widget_remove },
576 { "widget_get", luaA_titlebar_widget_get },
577 { "client_get", luaA_titlebar_client_get },
578 { "__index", luaA_titlebar_index },
579 { "__newindex", luaA_titlebar_newindex },
580 { "__eq", luaA_titlebar_eq },
581 { "__gc", luaA_titlebar_gc },
582 { "__tostring", luaA_titlebar_tostring },
583 { NULL, NULL }
586 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80