tabulous: remove
[awesome.git] / widget.c
blob24a8068a31b7b52e98210acb1fdd26328cac09b3
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 "common/atoms.h"
33 #include "common/xutil.h"
35 /** Collect a widget structure.
36 * \param L The Lua VM state.
37 * \return 0
39 static int
40 luaA_widget_gc(lua_State *L)
42 widget_t *widget = luaL_checkudata(L, 1, "widget");
43 if(widget->destructor)
44 widget->destructor(widget);
45 return luaA_object_gc(L);
48 /** Delete a widget node structure.
49 * \param node The node to destroy.
51 void
52 widget_node_delete(widget_node_t *node)
54 widget_unref(globalconf.L, node->widget);
57 /** Get a widget node from a wibox by coords.
58 * \param orientation Wibox orientation.
59 * \param widgets The widget list.
60 * \param width The container width.
61 * \param height The container height.
62 * \param x X coordinate of the widget.
63 * \param y Y coordinate of the widget.
64 * \return A widget.
66 widget_t *
67 widget_getbycoords(orientation_t orientation, widget_node_array_t *widgets,
68 int width, int height, int16_t *x, int16_t *y)
70 int tmp;
72 /* Need to transform coordinates like it was top/bottom */
73 switch(orientation)
75 case South:
76 tmp = *y;
77 *y = width - *x;
78 *x = tmp;
79 break;
80 case North:
81 tmp = *y;
82 *y = *x;
83 *x = height - tmp;
84 break;
85 default:
86 break;
88 foreach(w, *widgets)
89 if(w->widget->isvisible
90 && *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
91 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
92 return w->widget;
94 return NULL;
97 /** Convert a Lua table to a list of widget nodet.
98 * \param L The Lua VM state.
99 * \param widgets The linked list of widget node.
101 static void
102 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
104 if(lua_istable(L, -1))
106 lua_pushnil(L);
107 while(luaA_next(L, -2))
108 luaA_table2widgets(L, widgets);
109 /* remove the table */
110 lua_pop(L, 1);
112 else
114 widget_t *widget = luaA_toudata(L, -1, "widget");
115 if(widget)
117 widget_node_t w;
118 p_clear(&w, 1);
119 w.widget = widget_ref(L, -1);
120 widget_node_array_append(widgets, w);
122 else
123 lua_pop(L, 1); /* remove value */
127 /** Retrieve a list of widget geometries using a Lua layout function.
128 * a table which contains the geometries is then pushed onto the stack
129 * \param wibox The wibox.
130 * \return True is everything is ok, false otherwise.
131 * \todo What do we do if there's no layout defined?
133 bool
134 widget_geometries(wibox_t *wibox)
136 /* get the layout field of the widget table */
137 if(wibox->widgets_table)
139 /* push wibox */
140 wibox_push(globalconf.L, wibox);
141 /* push widgets table */
142 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
143 /* remove wibox */
144 lua_remove(globalconf.L, -2);
145 lua_getfield(globalconf.L, -1, "layout");
147 else
148 lua_pushnil(globalconf.L);
150 /* if the layout field is a function */
151 if(lua_isfunction(globalconf.L, -1))
153 /* Push 1st argument: wibox geometry */
154 area_t geometry = wibox->geometry;
155 geometry.x = 0;
156 geometry.y = 0;
157 /* we need to exchange the width and height of the wibox window if it
158 * it is rotated, so the layout function doesn't need to care about that
160 if(wibox->orientation != East)
162 int i = geometry.height;
163 geometry.height = geometry.width;
164 geometry.width = i;
166 luaA_pusharea(globalconf.L, geometry);
167 /* Re-push 2nd argument: widget table */
168 lua_pushvalue(globalconf.L, -3);
169 /* Push 3rd argument: wibox screen */
170 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
171 /* Re-push the layout function */
172 lua_pushvalue(globalconf.L, -4);
173 /* call the layout function with 3 arguments (wibox geometry, widget
174 * table, screen) and wait for one result */
175 if(!luaA_dofunction(globalconf.L, 3, 1))
176 return false;
178 lua_insert(globalconf.L, -3);
179 lua_pop(globalconf.L, 2);
181 else
183 /* Remove the "nil function" */
184 lua_pop(globalconf.L, 1);
186 /* If no layout function has been specified, we just push a table with
187 * geometries onto the stack. These geometries are nothing fancy, they
188 * have x = y = 0 and their height and width set to the widgets demands
189 * or the wibox size, depending on which is less.
192 widget_node_array_t *widgets = &wibox->widgets;
193 widget_node_array_wipe(widgets);
194 widget_node_array_init(widgets);
196 /* push wibox */
197 wibox_push(globalconf.L, wibox);
198 /* push widgets table */
199 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
200 /* remove wibox */
201 lua_remove(globalconf.L, -2);
202 luaA_table2widgets(globalconf.L, widgets);
204 lua_newtable(globalconf.L);
205 for(int i = 0; i < widgets->len; i++)
207 lua_pushnumber(globalconf.L, i + 1);
208 widget_t *widget = widgets->tab[i].widget;
209 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
210 area_t geometry = widget->extents(globalconf.L, widget);
211 lua_pop(globalconf.L, 1);
212 geometry.x = geometry.y = 0;
213 geometry.width = MIN(wibox->geometry.width, geometry.width);
214 geometry.height = MIN(wibox->geometry.height, geometry.height);
216 luaA_pusharea(globalconf.L, geometry);
218 lua_settable(globalconf.L, -3);
221 return true;
224 /** Render a list of widgets.
225 * \param wibox The wibox.
226 * \todo Remove GC.
228 void
229 widget_render(wibox_t *wibox)
231 lua_State *L = globalconf.L;
232 draw_context_t *ctx = &wibox->ctx;
233 area_t rectangle = { 0, 0, 0, 0 };
234 color_t col;
236 rectangle.width = ctx->width;
237 rectangle.height = ctx->height;
239 if (!widget_geometries(wibox))
240 return;
242 if(ctx->bg.alpha != 0xffff)
244 int x = wibox->geometry.x, y = wibox->geometry.y;
245 xcb_get_property_reply_t *prop_r;
246 char *data;
247 xcb_pixmap_t rootpix;
248 xcb_get_property_cookie_t prop_c;
249 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
250 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
251 PIXMAP, 0, 1);
252 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
254 if(prop_r->value_len
255 && (data = xcb_get_property_value(prop_r))
256 && (rootpix = *(xcb_pixmap_t *) data))
257 switch(wibox->orientation)
259 case North:
260 draw_rotate(ctx,
261 rootpix, ctx->pixmap,
262 s->width_in_pixels, s->height_in_pixels,
263 ctx->width, ctx->height,
264 M_PI_2,
265 y + ctx->width,
266 - x);
267 break;
268 case South:
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,
275 x + ctx->height);
276 break;
277 case East:
278 xcb_copy_area(globalconf.connection, rootpix,
279 wibox->pixmap, wibox->gc,
280 x, y,
281 0, 0,
282 ctx->width, ctx->height);
283 break;
285 p_delete(&prop_r);
289 widget_node_array_t *widgets = &wibox->widgets;
291 widget_node_array_wipe(widgets);
292 widget_node_array_init(widgets);
293 /* push wibox */
294 wibox_push(globalconf.L, wibox);
295 /* push widgets table */
296 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
297 /* remove wibox */
298 lua_remove(globalconf.L, -2);
299 luaA_table2widgets(L, widgets);
301 /* get computed geometries */
302 for(unsigned int i = 0; i < lua_objlen(L, -1); i++)
304 lua_pushnumber(L, i + 1);
305 lua_gettable(L, -2);
307 widgets->tab[i].geometry.x = luaA_getopt_number(L, -1, "x", wibox->geometry.x);
308 widgets->tab[i].geometry.y = luaA_getopt_number(L, -1, "y", wibox->geometry.y);
309 widgets->tab[i].geometry.width = luaA_getopt_number(L, -1, "width", 1);
310 widgets->tab[i].geometry.height = luaA_getopt_number(L, -1, "height", 1);
312 lua_pop(L, 1);
314 lua_pop(L, 1);
316 /* draw background image, only if the background color is not opaque */
317 if(wibox->bg_image && ctx->bg.alpha != 0xffff)
318 draw_image(ctx, 0, 0, 1.0, wibox->bg_image);
320 /* draw background color */
321 xcolor_to_color(&ctx->bg, &col);
322 draw_rectangle(ctx, rectangle, 1.0, true, &col);
324 /* draw everything! */
325 for(int i = 0; i < widgets->len; i++)
326 if(widgets->tab[i].widget->isvisible)
327 widgets->tab[i].widget->draw(widgets->tab[i].widget,
328 ctx, widgets->tab[i].geometry, wibox);
330 switch(wibox->orientation)
332 case South:
333 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
334 ctx->width, ctx->height,
335 ctx->height, ctx->width,
336 M_PI_2, ctx->height, 0);
337 break;
338 case North:
339 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
340 ctx->width, ctx->height,
341 ctx->height, ctx->width,
342 - M_PI_2, 0, ctx->width);
343 break;
344 case East:
345 break;
349 /** Invalidate widgets which should be refresh depending on their types.
350 * \param type Widget type to invalidate.
352 void
353 widget_invalidate_bytype(widget_constructor_t *type)
355 foreach(wibox, globalconf.wiboxes)
356 foreach(wnode, (*wibox)->widgets)
357 if(wnode->widget->type == type)
359 (*wibox)->need_update = true;
360 break;
364 /** Set a wibox needs update because it has widget, or redraw a titlebar.
365 * \param widget The widget to look for.
367 void
368 widget_invalidate_bywidget(widget_t *widget)
370 foreach(wibox, globalconf.wiboxes)
371 if(!(*wibox)->need_update)
372 foreach(wnode, (*wibox)->widgets)
373 if(wnode->widget == widget)
375 (*wibox)->need_update = true;
376 break;
379 foreach(_c, globalconf.clients)
381 client_t *c = *_c;
382 if(c->titlebar && !c->titlebar->need_update)
383 for(int j = 0; j < c->titlebar->widgets.len; j++)
384 if(c->titlebar->widgets.tab[j].widget == widget)
386 c->titlebar->need_update = true;
387 break;
392 /** Create a new widget.
393 * \param L The Lua VM state.
395 * \luastack
396 * \lparam A table with at least a type value.
397 * \lreturn A brand new widget.
399 static int
400 luaA_widget_new(lua_State *L)
402 const char *type;
403 widget_t *w;
404 widget_constructor_t *wc = NULL;
405 size_t len;
407 luaA_checktable(L, 2);
409 type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
411 switch(a_tokenize(type, len))
413 case A_TK_TEXTBOX:
414 wc = widget_textbox;
415 break;
416 case A_TK_PROGRESSBAR:
417 wc = widget_progressbar;
418 break;
419 case A_TK_GRAPH:
420 wc = widget_graph;
421 break;
422 case A_TK_SYSTRAY:
423 wc = widget_systray;
424 break;
425 case A_TK_IMAGEBOX:
426 wc = widget_imagebox;
427 break;
428 default:
429 break;
432 if(wc)
434 w = widget_new(L);
435 wc(w);
437 else
439 luaA_warn(L, "unkown widget type: %s", type);
440 return 0;
443 w->type = wc;
445 /* Set visible by default. */
446 w->isvisible = true;
448 return 1;
451 /** Generic widget.
452 * \param L The Lua VM state.
453 * \return The number of elements pushed on stack.
454 * \luastack
455 * \lfield visible The widget visibility.
456 * \lfield mouse_enter A function to execute when the mouse enter the widget.
457 * \lfield mouse_leave A function to execute when the mouse leave the widget.
459 static int
460 luaA_widget_index(lua_State *L)
462 size_t len;
463 widget_t *widget = luaL_checkudata(L, 1, "widget");
464 const char *buf = luaL_checklstring(L, 2, &len);
465 awesome_token_t token;
467 if(luaA_usemetatable(L, 1, 2))
468 return 1;
470 switch((token = a_tokenize(buf, len)))
472 case A_TK_VISIBLE:
473 lua_pushboolean(L, widget->isvisible);
474 return 1;
475 case A_TK_MOUSE_ENTER:
476 return luaA_object_push_item(L, 1, widget->mouse_enter);
477 case A_TK_MOUSE_LEAVE:
478 return luaA_object_push_item(L, 1, widget->mouse_leave);
479 case A_TK_BUTTONS:
480 return luaA_object_push_item(L, 1, widget->buttons);
481 default:
482 break;
485 return widget->index ? widget->index(L, token) : 0;
488 /** Generic widget newindex.
489 * \param L The Lua VM state.
490 * \return The number of elements pushed on stack.
492 static int
493 luaA_widget_newindex(lua_State *L)
495 size_t len;
496 widget_t *widget = luaL_checkudata(L, 1, "widget");
497 const char *buf = luaL_checklstring(L, 2, &len);
498 awesome_token_t token;
500 switch((token = a_tokenize(buf, len)))
502 case A_TK_VISIBLE:
503 widget->isvisible = luaA_checkboolean(L, 3);
504 break;
505 case A_TK_MOUSE_ENTER:
506 luaA_checkfunction(L, 3);
507 luaA_object_unref_item(L, 1, widget->mouse_enter);
508 widget->mouse_enter = luaA_object_ref_item(L, 1, 3);
509 return 0;
510 case A_TK_MOUSE_LEAVE:
511 luaA_checkfunction(L, 3);
512 luaA_object_unref_item(L, 1, widget->mouse_leave);
513 widget->mouse_leave = luaA_object_ref_item(L, 1, 3);
514 return 0;
515 case A_TK_BUTTONS:
516 luaA_object_unref_item(L, 1, widget->buttons);
517 widget->buttons = luaA_object_ref_item(L, 1, 3);
518 break;
519 default:
520 return widget->newindex ? widget->newindex(L, token) : 0;
523 widget_invalidate_bywidget(widget);
525 return 0;
528 static int
529 luaA_widget_extents(lua_State *L)
531 widget_t *widget = luaL_checkudata(L, 1, "widget");
532 area_t g = {
533 .x = 0,
534 .y = 0,
535 .width = 0,
536 .height = 0
539 if(widget->extents)
540 g = widget->extents(L, widget);
542 lua_newtable(L);
543 lua_pushnumber(L, g.width);
544 lua_setfield(L, -2, "width");
545 lua_pushnumber(L, g.height);
546 lua_setfield(L, -2, "height");
548 return 1;
551 const struct luaL_reg awesome_widget_methods[] =
553 LUA_CLASS_METHODS(widget)
554 { "__call", luaA_widget_new },
555 { NULL, NULL }
557 const struct luaL_reg awesome_widget_meta[] =
559 LUA_OBJECT_META(widget)
560 { "extents", luaA_widget_extents },
561 { "__index", luaA_widget_index },
562 { "__newindex", luaA_widget_newindex },
563 { "__gc", luaA_widget_gc },
564 { NULL, NULL }
567 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80