awful.tag: fix new when layout is nil
[awesome.git] / widget.c
blob74f8ae8c1a67af3e42d447d41489847235f10ae6
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 nodes.
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.
136 static bool
137 widget_geometries(wibox_t *wibox)
139 /* get the layout field of the widget table */
140 if(wibox->widgets_table)
142 /* push wibox */
143 luaA_object_push(globalconf.L, wibox);
144 /* push widgets table */
145 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
146 /* remove wibox */
147 lua_remove(globalconf.L, -2);
148 /* get layout field from the table */
149 lua_getfield(globalconf.L, -1, "layout");
150 /* remove the widget table */
151 lua_remove(globalconf.L, -2);
153 else
154 lua_pushnil(globalconf.L);
156 /* if the layout field is a function */
157 if(lua_isfunction(globalconf.L, -1))
159 /* Push 1st argument: wibox geometry */
160 area_t geometry = wibox->geometry;
161 geometry.x = 0;
162 geometry.y = 0;
163 /* we need to exchange the width and height of the wibox window if it
164 * it is rotated, so the layout function doesn't need to care about that
166 if(wibox->orientation != East)
168 int i = geometry.height;
169 geometry.height = geometry.width;
170 geometry.width = i;
172 luaA_pusharea(globalconf.L, geometry);
173 /* Push 2nd argument: widget table */
174 luaA_object_push(globalconf.L, wibox);
175 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
176 lua_remove(globalconf.L, -2);
177 /* Push 3rd argument: wibox screen */
178 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
179 /* Re-push the layout function */
180 lua_pushvalue(globalconf.L, -4);
181 /* call the layout function with 3 arguments (wibox geometry, widget
182 * table, screen) and wait for one result */
183 if(!luaA_dofunction(globalconf.L, 3, 1))
184 return false;
186 /* Remove the left over layout function */
187 lua_remove(globalconf.L, -2);
189 else
191 /* Remove the "nil function" */
192 lua_pop(globalconf.L, 1);
194 /* If no layout function has been specified, we just push a table with
195 * geometries onto the stack. These geometries are nothing fancy, they
196 * have x = y = 0 and their height and width set to the widgets demands
197 * or the wibox size, depending on which is less.
200 widget_node_array_t *widgets = &wibox->widgets;
201 widget_node_array_wipe(widgets);
202 widget_node_array_init(widgets);
204 /* push wibox */
205 luaA_object_push(globalconf.L, wibox);
206 /* push widgets table */
207 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
208 /* remove wibox */
209 lua_remove(globalconf.L, -2);
210 luaA_table2widgets(globalconf.L, widgets);
212 lua_newtable(globalconf.L);
213 for(int i = 0; i < widgets->len; i++)
215 lua_pushnumber(globalconf.L, i + 1);
216 widget_t *widget = widgets->tab[i].widget;
217 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
218 area_t geometry = widget->extents(globalconf.L, widget);
219 lua_pop(globalconf.L, 1);
220 geometry.x = geometry.y = 0;
221 geometry.width = MIN(wibox->geometry.width, geometry.width);
222 geometry.height = MIN(wibox->geometry.height, geometry.height);
224 luaA_pusharea(globalconf.L, geometry);
226 lua_settable(globalconf.L, -3);
229 return true;
232 /** Render a list of widgets.
233 * \param wibox The wibox.
234 * \todo Remove GC.
236 void
237 widget_render(wibox_t *wibox)
239 lua_State *L = globalconf.L;
240 draw_context_t *ctx = &wibox->ctx;
241 area_t rectangle = { 0, 0, 0, 0 };
242 color_t col;
244 rectangle.width = ctx->width;
245 rectangle.height = ctx->height;
247 if (!widget_geometries(wibox))
248 return;
250 if(ctx->bg.alpha != 0xffff)
252 int x = wibox->geometry.x + wibox->border_width,
253 y = wibox->geometry.y + wibox->border_width;
254 xcb_get_property_reply_t *prop_r;
255 char *data;
256 xcb_pixmap_t rootpix;
257 xcb_get_property_cookie_t prop_c;
258 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
259 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
260 PIXMAP, 0, 1);
261 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
263 if(prop_r->value_len
264 && (data = xcb_get_property_value(prop_r))
265 && (rootpix = *(xcb_pixmap_t *) data))
266 switch(wibox->orientation)
268 case North:
269 draw_rotate(ctx,
270 rootpix, ctx->pixmap,
271 s->width_in_pixels, s->height_in_pixels,
272 ctx->width, ctx->height,
273 M_PI_2,
274 y + ctx->width,
275 - x);
276 break;
277 case South:
278 draw_rotate(ctx,
279 rootpix, ctx->pixmap,
280 s->width_in_pixels, s->height_in_pixels,
281 ctx->width, ctx->height,
282 - M_PI_2,
283 - y,
284 x + ctx->height);
285 break;
286 case East:
287 xcb_copy_area(globalconf.connection, rootpix,
288 wibox->pixmap, wibox->gc,
289 x, y,
290 0, 0,
291 ctx->width, ctx->height);
292 break;
294 p_delete(&prop_r);
298 widget_node_array_t *widgets = &wibox->widgets;
300 widget_node_array_wipe(widgets);
301 widget_node_array_init(widgets);
302 /* push wibox */
303 luaA_object_push(globalconf.L, wibox);
304 /* push widgets table */
305 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
306 /* remove wibox */
307 lua_remove(globalconf.L, -2);
308 luaA_table2widgets(L, widgets);
310 /* get computed geometries */
311 for(unsigned int i = 0; i < lua_objlen(L, -1); i++)
313 lua_pushnumber(L, i + 1);
314 lua_gettable(L, -2);
316 widgets->tab[i].geometry.x = luaA_getopt_number(L, -1, "x", wibox->geometry.x);
317 widgets->tab[i].geometry.y = luaA_getopt_number(L, -1, "y", wibox->geometry.y);
318 widgets->tab[i].geometry.width = luaA_getopt_number(L, -1, "width", 1);
319 widgets->tab[i].geometry.height = luaA_getopt_number(L, -1, "height", 1);
321 lua_pop(L, 1);
323 lua_pop(L, 1);
325 /* draw background image, only if the background color is not opaque */
326 if(wibox->bg_image && ctx->bg.alpha != 0xffff)
327 draw_image(ctx, 0, 0, 1.0, wibox->bg_image);
329 /* draw background color */
330 xcolor_to_color(&ctx->bg, &col);
331 draw_rectangle(ctx, rectangle, 1.0, true, &col);
333 /* draw everything! */
334 for(int i = 0; i < widgets->len; i++)
335 if(widgets->tab[i].widget->isvisible)
336 widgets->tab[i].widget->draw(widgets->tab[i].widget,
337 ctx, widgets->tab[i].geometry, wibox);
339 switch(wibox->orientation)
341 case South:
342 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
343 ctx->width, ctx->height,
344 ctx->height, ctx->width,
345 M_PI_2, ctx->height, 0);
346 break;
347 case North:
348 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
349 ctx->width, ctx->height,
350 ctx->height, ctx->width,
351 - M_PI_2, 0, ctx->width);
352 break;
353 case East:
354 break;
358 /** Invalidate widgets which should be refresh depending on their types.
359 * \param type Widget type to invalidate.
361 void
362 widget_invalidate_bytype(widget_constructor_t *type)
364 foreach(wibox, globalconf.wiboxes)
365 foreach(wnode, (*wibox)->widgets)
366 if(wnode->widget->type == type)
368 (*wibox)->need_update = true;
369 break;
373 /** Set a wibox needs update because it has widget, or redraw a titlebar.
374 * \param widget The widget to look for.
376 void
377 widget_invalidate_bywidget(widget_t *widget)
379 foreach(wibox, globalconf.wiboxes)
380 if(!(*wibox)->need_update)
381 foreach(wnode, (*wibox)->widgets)
382 if(wnode->widget == widget)
384 (*wibox)->need_update = true;
385 break;
388 foreach(_c, globalconf.clients)
390 client_t *c = *_c;
391 if(c->titlebar && !c->titlebar->need_update)
392 for(int j = 0; j < c->titlebar->widgets.len; j++)
393 if(c->titlebar->widgets.tab[j].widget == widget)
395 c->titlebar->need_update = true;
396 break;
401 /** Create a new widget.
402 * \param L The Lua VM state.
404 * \luastack
405 * \lparam A table with at least a type value.
406 * \lreturn A brand new widget.
408 static int
409 luaA_widget_new(lua_State *L)
411 luaA_class_new(L, &widget_class);
413 widget_t *w = luaA_checkudata(L, -1, &widget_class);
414 /* Set visible by default. */
415 w->isvisible = true;
417 return 1;
420 /** Get or set mouse buttons bindings to a widget.
421 * \param L The Lua VM state.
423 * \luastack
424 * \lvalue A widget.
425 * \lparam An array of mouse button bindings objects, or nothing.
426 * \return The array of mouse button bindings objects of this widget.
428 static int
429 luaA_widget_buttons(lua_State *L)
431 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
433 if(lua_gettop(L) == 2)
435 luaA_button_array_set(L, 1, 2, &widget->buttons);
436 luaA_object_emit_signal(L, 1, "property::buttons", 0);
437 return 1;
440 return luaA_button_array_get(L, 1, &widget->buttons);
443 /** Generic widget.
444 * \param L The Lua VM state.
445 * \return The number of elements pushed on stack.
446 * \luastack
447 * \lfield visible The widget visibility.
448 * \lfield mouse_enter A function to execute when the mouse enter the widget.
449 * \lfield mouse_leave A function to execute when the mouse leave the widget.
451 static int
452 luaA_widget_index(lua_State *L)
454 size_t len;
455 const char *prop = luaL_checklstring(L, 2, &len);
456 awesome_token_t token = a_tokenize(prop, len);
458 /* Try standard method */
459 if(luaA_class_index(L))
460 return 1;
462 /* Then call special widget index */
463 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
464 return widget->index ? widget->index(L, token) : 0;
467 /** Generic widget newindex.
468 * \param L The Lua VM state.
469 * \return The number of elements pushed on stack.
471 static int
472 luaA_widget_newindex(lua_State *L)
474 size_t len;
475 const char *prop = luaL_checklstring(L, 2, &len);
476 awesome_token_t token = a_tokenize(prop, len);
478 /* Try standard method */
479 luaA_class_newindex(L);
481 /* Then call special widget newindex */
482 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
483 return widget->newindex ? widget->newindex(L, token) : 0;
486 static int
487 luaA_widget_extents(lua_State *L)
489 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
490 area_t g = {
491 .x = 0,
492 .y = 0,
493 .width = 0,
494 .height = 0
497 if(widget->extents)
498 g = widget->extents(L, widget);
500 lua_newtable(L);
501 lua_pushnumber(L, g.width);
502 lua_setfield(L, -2, "width");
503 lua_pushnumber(L, g.height);
504 lua_setfield(L, -2, "height");
506 return 1;
509 static int
510 luaA_widget_set_type(lua_State *L, widget_t *w)
512 size_t len;
513 const char *type = luaL_checklstring(L, -1, &len);
514 widget_constructor_t *wc = NULL;
516 switch(a_tokenize(type, len))
518 case A_TK_TEXTBOX:
519 wc = widget_textbox;
520 break;
521 case A_TK_PROGRESSBAR:
522 wc = widget_progressbar;
523 break;
524 case A_TK_GRAPH:
525 wc = widget_graph;
526 break;
527 case A_TK_SYSTRAY:
528 wc = widget_systray;
529 break;
530 case A_TK_IMAGEBOX:
531 wc = widget_imagebox;
532 break;
533 default:
534 break;
537 if(!wc)
538 luaL_error(L, "unknown widget type: %s", type);
540 wc(w);
541 w->type = wc;
542 luaA_object_emit_signal(L, -3, "property::type", 0);
544 return 0;
547 static int
548 luaA_widget_get_visible(lua_State *L, widget_t *w)
550 lua_pushboolean(L, w->isvisible);
551 return 1;
554 static int
555 luaA_widget_set_visible(lua_State *L, widget_t *w)
557 w->isvisible = luaA_checkboolean(L, -1);
558 widget_invalidate_bywidget(w);
559 luaA_object_emit_signal(L, -3, "property::visible", 0);
560 return 0;
563 void
564 widget_class_setup(lua_State *L)
566 static const struct luaL_reg widget_methods[] =
568 LUA_CLASS_METHODS(widget)
569 { "__call", luaA_widget_new },
570 { NULL, NULL }
573 static const struct luaL_reg widget_meta[] =
575 LUA_OBJECT_META(widget)
576 { "buttons", luaA_widget_buttons },
577 { "extents", luaA_widget_extents },
578 { "__index", luaA_widget_index },
579 { "__newindex", luaA_widget_newindex },
580 { "__gc", luaA_widget_gc },
581 { NULL, NULL }
584 luaA_class_setup(L, &widget_class, "widget", (lua_class_allocator_t) widget_new,
585 NULL, NULL,
586 widget_methods, widget_meta);
587 luaA_class_add_property(&widget_class, A_TK_VISIBLE,
588 (lua_class_propfunc_t) luaA_widget_set_visible,
589 (lua_class_propfunc_t) luaA_widget_get_visible,
590 (lua_class_propfunc_t) luaA_widget_set_visible);
591 luaA_class_add_property(&widget_class, A_TK_TYPE,
592 (lua_class_propfunc_t) luaA_widget_set_type,
593 NULL,
594 NULL);
597 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80