naughty: fix margin when using icon
[awesome.git] / widget.c
blob7c6ebb73ab8bb10768726db08b106b8f4466a179
1 /*
2 * widget.c - widget managing
4 * Copyright © 2007-2009 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 <math.h>
24 #include <xcb/xcb.h>
25 #include <xcb/xcb_atom.h>
27 #include "screen.h"
28 #include "mouse.h"
29 #include "widget.h"
30 #include "wibox.h"
31 #include "client.h"
32 #include "luaa.h"
33 #include "common/atoms.h"
34 #include "common/xutil.h"
36 LUA_OBJECT_FUNCS(widget_class, widget_t, widget);
38 /** Collect a widget structure.
39 * \param L The Lua VM state.
40 * \return 0
42 static int
43 luaA_widget_gc(lua_State *L)
45 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
46 if(widget->destructor)
47 widget->destructor(widget);
48 button_array_wipe(&widget->buttons);
49 return luaA_object_gc(L);
52 /** Delete a widget node structure.
53 * \param node The node to destroy.
55 void
56 widget_node_delete(widget_node_t *node)
58 luaA_object_unref(globalconf.L, node->widget);
61 /** Get a widget node from a wibox by coords.
62 * \param orientation Wibox orientation.
63 * \param widgets The widget list.
64 * \param width The container width.
65 * \param height The container height.
66 * \param x X coordinate of the widget.
67 * \param y Y coordinate of the widget.
68 * \return A widget.
70 widget_t *
71 widget_getbycoords(orientation_t orientation, widget_node_array_t *widgets,
72 int width, int height, int16_t *x, int16_t *y)
74 int tmp;
76 /* Need to transform coordinates like it was top/bottom */
77 switch(orientation)
79 case South:
80 tmp = *y;
81 *y = width - *x;
82 *x = tmp;
83 break;
84 case North:
85 tmp = *y;
86 *y = *x;
87 *x = height - tmp;
88 break;
89 default:
90 break;
92 foreach(w, *widgets)
93 if(w->widget->isvisible
94 && *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
95 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
96 return w->widget;
98 return NULL;
101 /** Convert a Lua table to a list of widget nodet.
102 * \param L The Lua VM state.
103 * \param widgets The linked list of widget node.
105 static void
106 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
108 if(lua_istable(L, -1))
110 lua_pushnil(L);
111 while(luaA_next(L, -2))
112 luaA_table2widgets(L, widgets);
113 /* remove the table */
114 lua_pop(L, 1);
116 else
118 widget_t *widget = luaA_toudata(L, -1, &widget_class);
119 if(widget)
121 widget_node_t w;
122 p_clear(&w, 1);
123 w.widget = luaA_object_ref(L, -1);
124 widget_node_array_append(widgets, w);
126 else
127 lua_pop(L, 1); /* remove value */
131 /** Retrieve a list of widget geometries using a Lua layout function.
132 * a table which contains the geometries is then pushed onto the stack
133 * \param wibox The wibox.
134 * \return True is everything is ok, false otherwise.
135 * \todo What do we do if there's no layout defined?
137 bool
138 widget_geometries(wibox_t *wibox)
140 /* get the layout field of the widget table */
141 if(wibox->widgets_table)
143 /* push wibox */
144 luaA_object_push(globalconf.L, wibox);
145 /* push widgets table */
146 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
147 /* remove wibox */
148 lua_remove(globalconf.L, -2);
149 lua_getfield(globalconf.L, -1, "layout");
151 else
152 lua_pushnil(globalconf.L);
154 /* if the layout field is a function */
155 if(lua_isfunction(globalconf.L, -1))
157 /* Push 1st argument: wibox geometry */
158 area_t geometry = wibox->geometry;
159 geometry.x = 0;
160 geometry.y = 0;
161 /* we need to exchange the width and height of the wibox window if it
162 * it is rotated, so the layout function doesn't need to care about that
164 if(wibox->orientation != East)
166 int i = geometry.height;
167 geometry.height = geometry.width;
168 geometry.width = i;
170 luaA_pusharea(globalconf.L, geometry);
171 /* Re-push 2nd argument: widget table */
172 lua_pushvalue(globalconf.L, -3);
173 /* Push 3rd argument: wibox screen */
174 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
175 /* Re-push the layout function */
176 lua_pushvalue(globalconf.L, -4);
177 /* call the layout function with 3 arguments (wibox geometry, widget
178 * table, screen) and wait for one result */
179 if(!luaA_dofunction(globalconf.L, 3, 1))
180 return false;
182 lua_insert(globalconf.L, -3);
183 lua_pop(globalconf.L, 2);
185 else
187 /* Remove the "nil function" */
188 lua_pop(globalconf.L, 1);
190 /* If no layout function has been specified, we just push a table with
191 * geometries onto the stack. These geometries are nothing fancy, they
192 * have x = y = 0 and their height and width set to the widgets demands
193 * or the wibox size, depending on which is less.
196 widget_node_array_t *widgets = &wibox->widgets;
197 widget_node_array_wipe(widgets);
198 widget_node_array_init(widgets);
200 /* push wibox */
201 luaA_object_push(globalconf.L, wibox);
202 /* push widgets table */
203 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
204 /* remove wibox */
205 lua_remove(globalconf.L, -2);
206 luaA_table2widgets(globalconf.L, widgets);
208 lua_newtable(globalconf.L);
209 for(int i = 0; i < widgets->len; i++)
211 lua_pushnumber(globalconf.L, i + 1);
212 widget_t *widget = widgets->tab[i].widget;
213 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
214 area_t geometry = widget->extents(globalconf.L, widget);
215 lua_pop(globalconf.L, 1);
216 geometry.x = geometry.y = 0;
217 geometry.width = MIN(wibox->geometry.width, geometry.width);
218 geometry.height = MIN(wibox->geometry.height, geometry.height);
220 luaA_pusharea(globalconf.L, geometry);
222 lua_settable(globalconf.L, -3);
225 return true;
228 /** Render a list of widgets.
229 * \param wibox The wibox.
230 * \todo Remove GC.
232 void
233 widget_render(wibox_t *wibox)
235 lua_State *L = globalconf.L;
236 draw_context_t *ctx = &wibox->ctx;
237 area_t rectangle = { 0, 0, 0, 0 };
238 color_t col;
240 rectangle.width = ctx->width;
241 rectangle.height = ctx->height;
243 if (!widget_geometries(wibox))
244 return;
246 if(ctx->bg.alpha != 0xffff)
248 int x = wibox->geometry.x, y = wibox->geometry.y;
249 xcb_get_property_reply_t *prop_r;
250 char *data;
251 xcb_pixmap_t rootpix;
252 xcb_get_property_cookie_t prop_c;
253 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
254 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
255 PIXMAP, 0, 1);
256 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
258 if(prop_r->value_len
259 && (data = xcb_get_property_value(prop_r))
260 && (rootpix = *(xcb_pixmap_t *) data))
261 switch(wibox->orientation)
263 case North:
264 draw_rotate(ctx,
265 rootpix, ctx->pixmap,
266 s->width_in_pixels, s->height_in_pixels,
267 ctx->width, ctx->height,
268 M_PI_2,
269 y + ctx->width,
270 - x);
271 break;
272 case South:
273 draw_rotate(ctx,
274 rootpix, ctx->pixmap,
275 s->width_in_pixels, s->height_in_pixels,
276 ctx->width, ctx->height,
277 - M_PI_2,
278 - y,
279 x + ctx->height);
280 break;
281 case East:
282 xcb_copy_area(globalconf.connection, rootpix,
283 wibox->pixmap, wibox->gc,
284 x, y,
285 0, 0,
286 ctx->width, ctx->height);
287 break;
289 p_delete(&prop_r);
293 widget_node_array_t *widgets = &wibox->widgets;
295 widget_node_array_wipe(widgets);
296 widget_node_array_init(widgets);
297 /* push wibox */
298 luaA_object_push(globalconf.L, wibox);
299 /* push widgets table */
300 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
301 /* remove wibox */
302 lua_remove(globalconf.L, -2);
303 luaA_table2widgets(L, widgets);
305 /* get computed geometries */
306 for(unsigned int i = 0; i < lua_objlen(L, -1); i++)
308 lua_pushnumber(L, i + 1);
309 lua_gettable(L, -2);
311 widgets->tab[i].geometry.x = luaA_getopt_number(L, -1, "x", wibox->geometry.x);
312 widgets->tab[i].geometry.y = luaA_getopt_number(L, -1, "y", wibox->geometry.y);
313 widgets->tab[i].geometry.width = luaA_getopt_number(L, -1, "width", 1);
314 widgets->tab[i].geometry.height = luaA_getopt_number(L, -1, "height", 1);
316 lua_pop(L, 1);
318 lua_pop(L, 1);
320 /* draw background image, only if the background color is not opaque */
321 if(wibox->bg_image && ctx->bg.alpha != 0xffff)
322 draw_image(ctx, 0, 0, 1.0, wibox->bg_image);
324 /* draw background color */
325 xcolor_to_color(&ctx->bg, &col);
326 draw_rectangle(ctx, rectangle, 1.0, true, &col);
328 /* draw everything! */
329 for(int i = 0; i < widgets->len; i++)
330 if(widgets->tab[i].widget->isvisible)
331 widgets->tab[i].widget->draw(widgets->tab[i].widget,
332 ctx, widgets->tab[i].geometry, wibox);
334 switch(wibox->orientation)
336 case South:
337 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
338 ctx->width, ctx->height,
339 ctx->height, ctx->width,
340 M_PI_2, ctx->height, 0);
341 break;
342 case North:
343 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
344 ctx->width, ctx->height,
345 ctx->height, ctx->width,
346 - M_PI_2, 0, ctx->width);
347 break;
348 case East:
349 break;
353 /** Invalidate widgets which should be refresh depending on their types.
354 * \param type Widget type to invalidate.
356 void
357 widget_invalidate_bytype(widget_constructor_t *type)
359 foreach(wibox, globalconf.wiboxes)
360 foreach(wnode, (*wibox)->widgets)
361 if(wnode->widget->type == type)
363 (*wibox)->need_update = true;
364 break;
368 /** Set a wibox needs update because it has widget, or redraw a titlebar.
369 * \param widget The widget to look for.
371 void
372 widget_invalidate_bywidget(widget_t *widget)
374 foreach(wibox, globalconf.wiboxes)
375 if(!(*wibox)->need_update)
376 foreach(wnode, (*wibox)->widgets)
377 if(wnode->widget == widget)
379 (*wibox)->need_update = true;
380 break;
383 foreach(_c, globalconf.clients)
385 client_t *c = *_c;
386 if(c->titlebar && !c->titlebar->need_update)
387 for(int j = 0; j < c->titlebar->widgets.len; j++)
388 if(c->titlebar->widgets.tab[j].widget == widget)
390 c->titlebar->need_update = true;
391 break;
396 /** Create a new widget.
397 * \param L The Lua VM state.
399 * \luastack
400 * \lparam A table with at least a type value.
401 * \lreturn A brand new widget.
403 static int
404 luaA_widget_new(lua_State *L)
406 luaA_class_new(L, &widget_class);
408 widget_t *w = luaA_checkudata(L, -1, &widget_class);
409 /* Set visible by default. */
410 w->isvisible = true;
412 return 1;
415 /** Get or set mouse buttons bindings to a widget.
416 * \param L The Lua VM state.
418 * \luastack
419 * \lvalue A widget.
420 * \lparam An array of mouse button bindings objects, or nothing.
421 * \return The array of mouse button bindings objects of this widget.
423 static int
424 luaA_widget_buttons(lua_State *L)
426 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
428 if(lua_gettop(L) == 2)
430 luaA_button_array_set(L, 1, 2, &widget->buttons);
431 luaA_object_emit_signal(L, 1, "property::buttons", 0);
432 return 1;
435 return luaA_button_array_get(L, 1, &widget->buttons);
438 /** Generic widget.
439 * \param L The Lua VM state.
440 * \return The number of elements pushed on stack.
441 * \luastack
442 * \lfield visible The widget visibility.
443 * \lfield mouse_enter A function to execute when the mouse enter the widget.
444 * \lfield mouse_leave A function to execute when the mouse leave the widget.
446 static int
447 luaA_widget_index(lua_State *L)
449 size_t len;
450 const char *prop = luaL_checklstring(L, 2, &len);
451 awesome_token_t token = a_tokenize(prop, len);
453 /* Try standard method */
454 if(luaA_class_index(L))
455 return 1;
457 /* Then call special widget index */
458 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
459 return widget->index ? widget->index(L, token) : 0;
462 /** Generic widget newindex.
463 * \param L The Lua VM state.
464 * \return The number of elements pushed on stack.
466 static int
467 luaA_widget_newindex(lua_State *L)
469 size_t len;
470 const char *prop = luaL_checklstring(L, 2, &len);
471 awesome_token_t token = a_tokenize(prop, len);
473 /* Try standard method */
474 luaA_class_newindex(L);
476 /* Then call special widget newindex */
477 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
478 return widget->newindex ? widget->newindex(L, token) : 0;
481 static int
482 luaA_widget_extents(lua_State *L)
484 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
485 area_t g = {
486 .x = 0,
487 .y = 0,
488 .width = 0,
489 .height = 0
492 if(widget->extents)
493 g = widget->extents(L, widget);
495 lua_newtable(L);
496 lua_pushnumber(L, g.width);
497 lua_setfield(L, -2, "width");
498 lua_pushnumber(L, g.height);
499 lua_setfield(L, -2, "height");
501 return 1;
504 static int
505 luaA_widget_set_type(lua_State *L, widget_t *w)
507 size_t len;
508 const char *type = luaL_checklstring(L, -1, &len);
509 widget_constructor_t *wc = NULL;
511 switch(a_tokenize(type, len))
513 case A_TK_TEXTBOX:
514 wc = widget_textbox;
515 break;
516 case A_TK_PROGRESSBAR:
517 wc = widget_progressbar;
518 break;
519 case A_TK_GRAPH:
520 wc = widget_graph;
521 break;
522 case A_TK_SYSTRAY:
523 wc = widget_systray;
524 break;
525 case A_TK_IMAGEBOX:
526 wc = widget_imagebox;
527 break;
528 default:
529 break;
532 if(!wc)
533 luaL_error(L, "unkown widget type: %s", type);
535 wc(w);
536 w->type = wc;
537 luaA_object_emit_signal(L, -3, "property::type", 0);
539 return 0;
542 static int
543 luaA_widget_get_visible(lua_State *L, widget_t *w)
545 lua_pushboolean(L, w->isvisible);
546 return 1;
549 static int
550 luaA_widget_set_visible(lua_State *L, widget_t *w)
552 w->isvisible = luaA_checkboolean(L, -1);
553 widget_invalidate_bywidget(w);
554 luaA_object_emit_signal(L, -3, "property::visible", 0);
555 return 0;
558 void
559 widget_class_setup(lua_State *L)
561 static const struct luaL_reg widget_methods[] =
563 LUA_CLASS_METHODS(widget)
564 { "__call", luaA_widget_new },
565 { NULL, NULL }
568 static const struct luaL_reg widget_meta[] =
570 LUA_OBJECT_META(widget)
571 { "buttons", luaA_widget_buttons },
572 { "extents", luaA_widget_extents },
573 { "__index", luaA_widget_index },
574 { "__newindex", luaA_widget_newindex },
575 { "__gc", luaA_widget_gc },
576 { NULL, NULL }
579 luaA_class_setup(L, &widget_class, "widget", (lua_class_allocator_t) widget_new,
580 NULL, NULL,
581 widget_methods, widget_meta);
582 luaA_class_add_property(&widget_class, A_TK_VISIBLE,
583 (lua_class_propfunc_t) luaA_widget_set_visible,
584 (lua_class_propfunc_t) luaA_widget_get_visible,
585 (lua_class_propfunc_t) luaA_widget_set_visible);
586 luaA_class_add_property(&widget_class, A_TK_TYPE,
587 (lua_class_propfunc_t) luaA_widget_set_type,
588 NULL,
589 NULL);
592 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80