luaobject: remove prefix##_push_item()
[awesome.git] / widget.c
blob4511f2eb0626bf6c8b99a4b0dbdaad740d8d9988
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 button_array_wipe(&widget->buttons);
46 return luaA_object_gc(L);
49 /** Delete a widget node structure.
50 * \param node The node to destroy.
52 void
53 widget_node_delete(widget_node_t *node)
55 luaA_object_unref(globalconf.L, node->widget);
58 /** Get a widget node from a wibox by coords.
59 * \param orientation Wibox orientation.
60 * \param widgets The widget list.
61 * \param width The container width.
62 * \param height The container height.
63 * \param x X coordinate of the widget.
64 * \param y Y coordinate of the widget.
65 * \return A widget.
67 widget_t *
68 widget_getbycoords(orientation_t orientation, widget_node_array_t *widgets,
69 int width, int height, int16_t *x, int16_t *y)
71 int tmp;
73 /* Need to transform coordinates like it was top/bottom */
74 switch(orientation)
76 case South:
77 tmp = *y;
78 *y = width - *x;
79 *x = tmp;
80 break;
81 case North:
82 tmp = *y;
83 *y = *x;
84 *x = height - tmp;
85 break;
86 default:
87 break;
89 foreach(w, *widgets)
90 if(w->widget->isvisible
91 && *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
92 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
93 return w->widget;
95 return NULL;
98 /** Convert a Lua table to a list of widget nodet.
99 * \param L The Lua VM state.
100 * \param widgets The linked list of widget node.
102 static void
103 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
105 if(lua_istable(L, -1))
107 lua_pushnil(L);
108 while(luaA_next(L, -2))
109 luaA_table2widgets(L, widgets);
110 /* remove the table */
111 lua_pop(L, 1);
113 else
115 widget_t *widget = luaA_toudata(L, -1, "widget");
116 if(widget)
118 widget_node_t w;
119 p_clear(&w, 1);
120 w.widget = luaA_object_ref(L, -1);
121 widget_node_array_append(widgets, w);
123 else
124 lua_pop(L, 1); /* remove value */
128 /** Retrieve a list of widget geometries using a Lua layout function.
129 * a table which contains the geometries is then pushed onto the stack
130 * \param wibox The wibox.
131 * \return True is everything is ok, false otherwise.
132 * \todo What do we do if there's no layout defined?
134 bool
135 widget_geometries(wibox_t *wibox)
137 /* get the layout field of the widget table */
138 if(wibox->widgets_table)
140 /* push wibox */
141 luaA_object_push(globalconf.L, wibox);
142 /* push widgets table */
143 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
144 /* remove wibox */
145 lua_remove(globalconf.L, -2);
146 lua_getfield(globalconf.L, -1, "layout");
148 else
149 lua_pushnil(globalconf.L);
151 /* if the layout field is a function */
152 if(lua_isfunction(globalconf.L, -1))
154 /* Push 1st argument: wibox geometry */
155 area_t geometry = wibox->geometry;
156 geometry.x = 0;
157 geometry.y = 0;
158 /* we need to exchange the width and height of the wibox window if it
159 * it is rotated, so the layout function doesn't need to care about that
161 if(wibox->orientation != East)
163 int i = geometry.height;
164 geometry.height = geometry.width;
165 geometry.width = i;
167 luaA_pusharea(globalconf.L, geometry);
168 /* Re-push 2nd argument: widget table */
169 lua_pushvalue(globalconf.L, -3);
170 /* Push 3rd argument: wibox screen */
171 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
172 /* Re-push the layout function */
173 lua_pushvalue(globalconf.L, -4);
174 /* call the layout function with 3 arguments (wibox geometry, widget
175 * table, screen) and wait for one result */
176 if(!luaA_dofunction(globalconf.L, 3, 1))
177 return false;
179 lua_insert(globalconf.L, -3);
180 lua_pop(globalconf.L, 2);
182 else
184 /* Remove the "nil function" */
185 lua_pop(globalconf.L, 1);
187 /* If no layout function has been specified, we just push a table with
188 * geometries onto the stack. These geometries are nothing fancy, they
189 * have x = y = 0 and their height and width set to the widgets demands
190 * or the wibox size, depending on which is less.
193 widget_node_array_t *widgets = &wibox->widgets;
194 widget_node_array_wipe(widgets);
195 widget_node_array_init(widgets);
197 /* push wibox */
198 luaA_object_push(globalconf.L, wibox);
199 /* push widgets table */
200 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
201 /* remove wibox */
202 lua_remove(globalconf.L, -2);
203 luaA_table2widgets(globalconf.L, widgets);
205 lua_newtable(globalconf.L);
206 for(int i = 0; i < widgets->len; i++)
208 lua_pushnumber(globalconf.L, i + 1);
209 widget_t *widget = widgets->tab[i].widget;
210 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen));
211 area_t geometry = widget->extents(globalconf.L, widget);
212 lua_pop(globalconf.L, 1);
213 geometry.x = geometry.y = 0;
214 geometry.width = MIN(wibox->geometry.width, geometry.width);
215 geometry.height = MIN(wibox->geometry.height, geometry.height);
217 luaA_pusharea(globalconf.L, geometry);
219 lua_settable(globalconf.L, -3);
222 return true;
225 /** Render a list of widgets.
226 * \param wibox The wibox.
227 * \todo Remove GC.
229 void
230 widget_render(wibox_t *wibox)
232 lua_State *L = globalconf.L;
233 draw_context_t *ctx = &wibox->ctx;
234 area_t rectangle = { 0, 0, 0, 0 };
235 color_t col;
237 rectangle.width = ctx->width;
238 rectangle.height = ctx->height;
240 if (!widget_geometries(wibox))
241 return;
243 if(ctx->bg.alpha != 0xffff)
245 int x = wibox->geometry.x, y = wibox->geometry.y;
246 xcb_get_property_reply_t *prop_r;
247 char *data;
248 xcb_pixmap_t rootpix;
249 xcb_get_property_cookie_t prop_c;
250 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
251 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
252 PIXMAP, 0, 1);
253 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
255 if(prop_r->value_len
256 && (data = xcb_get_property_value(prop_r))
257 && (rootpix = *(xcb_pixmap_t *) data))
258 switch(wibox->orientation)
260 case North:
261 draw_rotate(ctx,
262 rootpix, ctx->pixmap,
263 s->width_in_pixels, s->height_in_pixels,
264 ctx->width, ctx->height,
265 M_PI_2,
266 y + ctx->width,
267 - x);
268 break;
269 case South:
270 draw_rotate(ctx,
271 rootpix, ctx->pixmap,
272 s->width_in_pixels, s->height_in_pixels,
273 ctx->width, ctx->height,
274 - M_PI_2,
275 - y,
276 x + ctx->height);
277 break;
278 case East:
279 xcb_copy_area(globalconf.connection, rootpix,
280 wibox->pixmap, wibox->gc,
281 x, y,
282 0, 0,
283 ctx->width, ctx->height);
284 break;
286 p_delete(&prop_r);
290 widget_node_array_t *widgets = &wibox->widgets;
292 widget_node_array_wipe(widgets);
293 widget_node_array_init(widgets);
294 /* push wibox */
295 luaA_object_push(globalconf.L, wibox);
296 /* push widgets table */
297 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
298 /* remove wibox */
299 lua_remove(globalconf.L, -2);
300 luaA_table2widgets(L, widgets);
302 /* get computed geometries */
303 for(unsigned int i = 0; i < lua_objlen(L, -1); i++)
305 lua_pushnumber(L, i + 1);
306 lua_gettable(L, -2);
308 widgets->tab[i].geometry.x = luaA_getopt_number(L, -1, "x", wibox->geometry.x);
309 widgets->tab[i].geometry.y = luaA_getopt_number(L, -1, "y", wibox->geometry.y);
310 widgets->tab[i].geometry.width = luaA_getopt_number(L, -1, "width", 1);
311 widgets->tab[i].geometry.height = luaA_getopt_number(L, -1, "height", 1);
313 lua_pop(L, 1);
315 lua_pop(L, 1);
317 /* draw background image, only if the background color is not opaque */
318 if(wibox->bg_image && ctx->bg.alpha != 0xffff)
319 draw_image(ctx, 0, 0, 1.0, wibox->bg_image);
321 /* draw background color */
322 xcolor_to_color(&ctx->bg, &col);
323 draw_rectangle(ctx, rectangle, 1.0, true, &col);
325 /* draw everything! */
326 for(int i = 0; i < widgets->len; i++)
327 if(widgets->tab[i].widget->isvisible)
328 widgets->tab[i].widget->draw(widgets->tab[i].widget,
329 ctx, widgets->tab[i].geometry, wibox);
331 switch(wibox->orientation)
333 case South:
334 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
335 ctx->width, ctx->height,
336 ctx->height, ctx->width,
337 M_PI_2, ctx->height, 0);
338 break;
339 case North:
340 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
341 ctx->width, ctx->height,
342 ctx->height, ctx->width,
343 - M_PI_2, 0, ctx->width);
344 break;
345 case East:
346 break;
350 /** Invalidate widgets which should be refresh depending on their types.
351 * \param type Widget type to invalidate.
353 void
354 widget_invalidate_bytype(widget_constructor_t *type)
356 foreach(wibox, globalconf.wiboxes)
357 foreach(wnode, (*wibox)->widgets)
358 if(wnode->widget->type == type)
360 (*wibox)->need_update = true;
361 break;
365 /** Set a wibox needs update because it has widget, or redraw a titlebar.
366 * \param widget The widget to look for.
368 void
369 widget_invalidate_bywidget(widget_t *widget)
371 foreach(wibox, globalconf.wiboxes)
372 if(!(*wibox)->need_update)
373 foreach(wnode, (*wibox)->widgets)
374 if(wnode->widget == widget)
376 (*wibox)->need_update = true;
377 break;
380 foreach(_c, globalconf.clients)
382 client_t *c = *_c;
383 if(c->titlebar && !c->titlebar->need_update)
384 for(int j = 0; j < c->titlebar->widgets.len; j++)
385 if(c->titlebar->widgets.tab[j].widget == widget)
387 c->titlebar->need_update = true;
388 break;
393 /** Create a new widget.
394 * \param L The Lua VM state.
396 * \luastack
397 * \lparam A table with at least a type value.
398 * \lreturn A brand new widget.
400 static int
401 luaA_widget_new(lua_State *L)
403 const char *type;
404 widget_t *w;
405 widget_constructor_t *wc = NULL;
406 size_t len;
408 luaA_checktable(L, 2);
410 type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
412 switch(a_tokenize(type, len))
414 case A_TK_TEXTBOX:
415 wc = widget_textbox;
416 break;
417 case A_TK_PROGRESSBAR:
418 wc = widget_progressbar;
419 break;
420 case A_TK_GRAPH:
421 wc = widget_graph;
422 break;
423 case A_TK_SYSTRAY:
424 wc = widget_systray;
425 break;
426 case A_TK_IMAGEBOX:
427 wc = widget_imagebox;
428 break;
429 default:
430 break;
433 if(wc)
435 w = widget_new(L);
436 wc(w);
438 else
440 luaA_warn(L, "unkown widget type: %s", type);
441 return 0;
444 w->type = wc;
446 /* Set visible by default. */
447 w->isvisible = true;
449 return 1;
452 /** Get or set mouse buttons bindings to a widget.
453 * \param L The Lua VM state.
455 * \luastack
456 * \lvalue A widget.
457 * \lparam An array of mouse button bindings objects, or nothing.
458 * \return The array of mouse button bindings objects of this widget.
460 static int
461 luaA_widget_buttons(lua_State *L)
463 widget_t *widget = luaL_checkudata(L, 1, "widget");
465 if(lua_gettop(L) == 2)
467 luaA_button_array_set(L, 1, 2, &widget->buttons);
468 return 1;
471 return luaA_button_array_get(L, 1, &widget->buttons);
474 /** Generic widget.
475 * \param L The Lua VM state.
476 * \return The number of elements pushed on stack.
477 * \luastack
478 * \lfield visible The widget visibility.
479 * \lfield mouse_enter A function to execute when the mouse enter the widget.
480 * \lfield mouse_leave A function to execute when the mouse leave the widget.
482 static int
483 luaA_widget_index(lua_State *L)
485 size_t len;
486 widget_t *widget = luaL_checkudata(L, 1, "widget");
487 const char *buf = luaL_checklstring(L, 2, &len);
488 awesome_token_t token;
490 if(luaA_usemetatable(L, 1, 2))
491 return 1;
493 switch((token = a_tokenize(buf, len)))
495 case A_TK_VISIBLE:
496 lua_pushboolean(L, widget->isvisible);
497 return 1;
498 case A_TK_MOUSE_ENTER:
499 return luaA_object_push_item(L, 1, widget->mouse_enter);
500 case A_TK_MOUSE_LEAVE:
501 return luaA_object_push_item(L, 1, widget->mouse_leave);
502 default:
503 break;
506 return widget->index ? widget->index(L, token) : 0;
509 /** Generic widget newindex.
510 * \param L The Lua VM state.
511 * \return The number of elements pushed on stack.
513 static int
514 luaA_widget_newindex(lua_State *L)
516 size_t len;
517 widget_t *widget = luaL_checkudata(L, 1, "widget");
518 const char *buf = luaL_checklstring(L, 2, &len);
519 awesome_token_t token;
521 switch((token = a_tokenize(buf, len)))
523 case A_TK_VISIBLE:
524 widget->isvisible = luaA_checkboolean(L, 3);
525 break;
526 case A_TK_MOUSE_ENTER:
527 luaA_checkfunction(L, 3);
528 luaA_object_unref_item(L, 1, widget->mouse_enter);
529 widget->mouse_enter = luaA_object_ref_item(L, 1, 3);
530 return 0;
531 case A_TK_MOUSE_LEAVE:
532 luaA_checkfunction(L, 3);
533 luaA_object_unref_item(L, 1, widget->mouse_leave);
534 widget->mouse_leave = luaA_object_ref_item(L, 1, 3);
535 return 0;
536 default:
537 return widget->newindex ? widget->newindex(L, token) : 0;
540 widget_invalidate_bywidget(widget);
542 return 0;
545 static int
546 luaA_widget_extents(lua_State *L)
548 widget_t *widget = luaL_checkudata(L, 1, "widget");
549 area_t g = {
550 .x = 0,
551 .y = 0,
552 .width = 0,
553 .height = 0
556 if(widget->extents)
557 g = widget->extents(L, widget);
559 lua_newtable(L);
560 lua_pushnumber(L, g.width);
561 lua_setfield(L, -2, "width");
562 lua_pushnumber(L, g.height);
563 lua_setfield(L, -2, "height");
565 return 1;
568 const struct luaL_reg awesome_widget_methods[] =
570 LUA_CLASS_METHODS(widget)
571 { "__call", luaA_widget_new },
572 { NULL, NULL }
574 const struct luaL_reg awesome_widget_meta[] =
576 LUA_OBJECT_META(widget)
577 { "buttons", luaA_widget_buttons },
578 { "extents", luaA_widget_extents },
579 { "__index", luaA_widget_index },
580 { "__newindex", luaA_widget_newindex },
581 { "__gc", luaA_widget_gc },
582 { NULL, NULL }
585 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80