awful.menu: add support for functions
[awesome.git] / widget.c
blob8b8d2aa1e86bb19c2ad0448a6803fa1892534164
1 /*
2 * widget.c - widget managing
4 * Copyright © 2007-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 <math.h>
24 #include <xcb/xcb.h>
25 #include <xcb/xcb_atom.h>
27 #include "mouse.h"
28 #include "widget.h"
29 #include "wibox.h"
30 #include "common/atoms.h"
32 extern awesome_t globalconf;
34 DO_LUA_NEW(extern, widget_t, widget, "widget", widget_ref)
35 DO_LUA_GC(widget_t, widget, "widget", widget_unref)
36 DO_LUA_EQ(widget_t, widget, "widget")
38 #include "widgetgen.h"
40 /** Compute offset for drawing the first pixel of a widget.
41 * \param barwidth The wibox width.
42 * \param widgetwidth The widget width.
43 * \param alignment The widget alignment on wibox.
44 * \return The x coordinate to draw at.
46 int
47 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
49 switch(alignment)
51 case AlignLeft:
52 case AlignFlex:
53 return offset;
55 return barwidth - offset - widgetwidth;
58 /** Common function for button press event on widget.
59 * It will look into configuration to find the callback function to call.
60 * \param w The widget node.
61 * \param ev The button press event the widget received.
62 * \param screen The screen number.
63 * \param p The object where user clicked.
65 static void
66 widget_common_button(widget_node_t *w,
67 xcb_button_press_event_t *ev,
68 int screen __attribute__ ((unused)),
69 wibox_t *p)
71 button_array_t *b = &w->widget->buttons;
73 for(int i = 0; i < b->len; i++)
74 if(ev->detail == b->tab[i]->button
75 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
77 luaA_wibox_userdata_new(globalconf.L, p);
78 luaA_dofunction(globalconf.L,
79 ev->response_type == XCB_BUTTON_PRESS ?
80 b->tab[i]->press : b->tab[i]->release,
81 1, 0);
85 /** Convert a Lua table to a list of widget nodet.
86 * \param L The Lua VM state.
87 * \param widgets The linked list of widget node.
89 void
90 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
92 if(lua_istable(L, -1))
94 lua_pushnil(L);
95 while(luaA_next(L, -2))
97 luaA_table2widgets(L, widgets);
98 lua_pop(L, 1); /* remove value */
101 else
103 widget_t **widget = luaA_toudata(L, -1, "widget");
104 if(widget)
106 widget_node_t w;
107 p_clear(&w, 1);
108 w.widget = widget_ref(widget);
109 widget_node_array_append(widgets, w);
114 /** Render a list of widgets.
115 * \param wnode The list of widgets.
116 * \param ctx The draw context where to render.
117 * \param rotate_px The rotate pixmap: where to rotate and render the final
118 * pixmap when the object oritation is not east.
119 * \param screen The logical screen used to render.
120 * \param orientation The object orientation.
121 * \param x The x coordinates of the object.
122 * \param y The y coordinates of the object.
123 * \param wibox The wibox.
124 * \todo Remove GC.
126 void
127 widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
128 int screen, orientation_t orientation,
129 int x, int y, wibox_t *wibox)
131 int left = 0, right = 0;
132 area_t rectangle = { 0, 0, 0, 0 };
134 rectangle.width = ctx->width;
135 rectangle.height = ctx->height;
137 if(ctx->bg.alpha != 0xffff)
139 xcb_get_property_reply_t *prop_r;
140 char *data;
141 xcb_pixmap_t rootpix;
142 xcb_get_property_cookie_t prop_c;
143 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
144 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
145 PIXMAP, 0, 1);
146 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
148 if(prop_r->value_len
149 && (data = xcb_get_property_value(prop_r))
150 && (rootpix = *(xcb_pixmap_t *) data))
151 switch(orientation)
153 case North:
154 draw_rotate(ctx,
155 rootpix, ctx->pixmap,
156 s->width_in_pixels, s->height_in_pixels,
157 ctx->width, ctx->height,
158 M_PI_2,
159 y + ctx->width,
160 - x);
161 break;
162 case South:
163 draw_rotate(ctx,
164 rootpix, ctx->pixmap,
165 s->width_in_pixels, s->height_in_pixels,
166 ctx->width, ctx->height,
167 - M_PI_2,
168 - y,
169 x + ctx->height);
170 break;
171 case East:
172 xcb_copy_area(globalconf.connection, rootpix,
173 rotate_px, gc,
174 x, y,
175 0, 0,
176 ctx->width, ctx->height);
177 break;
179 p_delete(&prop_r);
183 /* compute geometry */
184 for(int i = 0; i < widgets->len; i++)
185 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
187 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
188 screen, ctx->height,
189 ctx->width - (left + right));
190 widgets->tab[i].geometry.x = left;
191 left += widgets->tab[i].geometry.width;
194 for(int i = widgets->len - 1; i >= 0; i--)
195 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
197 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
198 screen, ctx->height,
199 ctx->width - (left + right));
200 right += widgets->tab[i].geometry.width;
201 widgets->tab[i].geometry.x = ctx->width - right;
204 int flex = 0;
205 for(int i = 0; i < widgets->len; i++)
206 if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible)
207 flex++;
209 if(flex)
211 int length = (ctx->width - (left + right)) / flex;
213 for(int i = 0; i < widgets->len; i++)
214 if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible)
216 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
217 screen, ctx->height,
218 length);
219 widgets->tab[i].geometry.x = left;
220 left += widgets->tab[i].geometry.width;
224 /* draw everything! */
225 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
227 for(int i = 0; i < widgets->len; i++)
228 if(widgets->tab[i].widget->isvisible)
230 widgets->tab[i].geometry.y = 0;
231 widgets->tab[i].widget->draw(widgets->tab[i].widget,
232 ctx, widgets->tab[i].geometry,
233 screen, wibox);
236 switch(orientation)
238 case South:
239 draw_rotate(ctx, ctx->pixmap, rotate_px,
240 ctx->width, ctx->height,
241 ctx->height, ctx->width,
242 M_PI_2, ctx->height, 0);
243 break;
244 case North:
245 draw_rotate(ctx, ctx->pixmap, rotate_px,
246 ctx->width, ctx->height,
247 ctx->height, ctx->width,
248 - M_PI_2, 0, ctx->width);
249 break;
250 case East:
251 break;
256 /** Common function for creating a widget.
257 * \param widget The allocated widget.
259 void
260 widget_common_new(widget_t *widget)
262 widget->align = AlignLeft;
263 widget->button = widget_common_button;
266 /** Invalidate widgets which should be refresh upon
267 * external modifications. widget_t who watch flags will
268 * be set to be refreshed.
269 * \param screen Virtual screen number.
270 * \param flags Cache flags to invalidate.
272 void
273 widget_invalidate_cache(int screen, int flags)
275 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
277 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
279 for(int j = 0; j < wibox->widgets.len; j++)
280 if(wibox->widgets.tab[j].widget->cache_flags & flags)
282 wibox->need_update = true;
283 break;
288 /** Set a wibox needs update because it has widget, or redraw a titlebar.
289 * \todo Probably needs more optimization.
290 * \param widget The widget to look for.
292 void
293 widget_invalidate_bywidget(widget_t *widget)
295 for(int screen = 0; screen < globalconf.nscreen; screen++)
296 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
298 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
299 if(!wibox->need_update)
300 for(int j = 0; j < wibox->widgets.len; j++)
301 if(wibox->widgets.tab[j].widget == widget)
303 wibox->need_update = true;
304 break;
308 for(client_t *c = globalconf.clients; c; c = c->next)
309 if(c->titlebar && !c->titlebar->need_update)
310 for(int j = 0; j < c->titlebar->widgets.len; j++)
311 if(c->titlebar->widgets.tab[j].widget == widget)
313 c->titlebar->need_update = true;
314 break;
318 /** Create a new widget.
319 * \param L The Lua VM state.
321 * \luastack
322 * \lparam A table with at least a name and a type value. Optional attributes
323 * are: align.
324 * \lreturn A brand new widget.
326 static int
327 luaA_widget_new(lua_State *L)
329 const char *buf, *type;
330 widget_t *w = NULL;
331 widget_constructor_t *wc;
332 alignment_t align;
333 size_t len;
335 luaA_checktable(L, 2);
337 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
338 align = draw_align_fromstr(buf, len);
340 if(!(buf = luaA_getopt_string(L, 2, "name", NULL)))
341 luaL_error(L, "object widget must have a name");
343 type = luaA_getopt_string(L, 2, "type", NULL);
345 if((wc = name_func_lookup(type, WidgetList)))
346 w = wc(align);
347 else
348 luaL_error(L, "unkown widget type: %s", type);
350 w->type = wc;
352 /* Set visible by default. */
353 w->isvisible = true;
355 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
357 w->name = a_strdup(buf);
359 return luaA_widget_userdata_new(L, w);
362 /** Get or set mouse buttons bindings to a widget.
363 * \param L The Lua VM state.
365 * \luastack
366 * \lvalue A widget.
367 * \lparam An array of mouse button bindings objects, or nothing.
368 * \return The array of mouse button bindings objects of this widget.
370 static int
371 luaA_widget_buttons(lua_State *L)
373 widget_t **widget = luaA_checkudata(L, 1, "widget");
374 button_array_t *buttons = &(*widget)->buttons;
376 if(lua_gettop(L) == 2)
377 luaA_button_array_set(L, 2, buttons);
379 return luaA_button_array_get(L, buttons);
382 /** Convert a widget into a printable string.
383 * \param L The Lua VM state.
385 * \luastack
386 * \lvalue A widget.
388 static int
389 luaA_widget_tostring(lua_State *L)
391 widget_t **p = luaA_checkudata(L, 1, "widget");
392 lua_pushfstring(L, "[widget udata(%p) name(%s)]", *p, (*p)->name);
393 return 1;
396 /** Generic widget.
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
399 * \luastack
400 * \lfield visible The widget visibility.
401 * \lfield name The widget name.
402 * \lfield mouse_enter A function to execute when the mouse enter the widget.
403 * \lfield mouse_leave A function to execute when the mouse leave the widget.
405 static int
406 luaA_widget_index(lua_State *L)
408 size_t len;
409 widget_t **widget = luaA_checkudata(L, 1, "widget");
410 const char *buf = luaL_checklstring(L, 2, &len);
411 awesome_token_t token;
413 if(luaA_usemetatable(L, 1, 2))
414 return 1;
416 switch((token = a_tokenize(buf, len)))
418 case A_TK_VISIBLE:
419 lua_pushboolean(L, (*widget)->isvisible);
420 return 1;
421 case A_TK_NAME:
422 lua_pushstring(L, (*widget)->name);
423 return 1;
424 case A_TK_MOUSE_ENTER:
425 if((*widget)->mouse_enter != LUA_REFNIL)
426 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
427 else
428 return 0;
429 return 1;
430 case A_TK_MOUSE_LEAVE:
431 if((*widget)->mouse_leave != LUA_REFNIL)
432 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
433 else
434 return 0;
435 return 1;
436 default:
437 break;
440 return (*widget)->index ? (*widget)->index(L, token) : 0;
443 /** Generic widget newindex.
444 * \param L The Lua VM state.
445 * \return The number of elements pushed on stack.
447 static int
448 luaA_widget_newindex(lua_State *L)
450 size_t len;
451 widget_t **widget = luaA_checkudata(L, 1, "widget");
452 const char *buf = luaL_checklstring(L, 2, &len);
453 awesome_token_t token;
455 switch((token = a_tokenize(buf, len)))
457 case A_TK_VISIBLE:
458 (*widget)->isvisible = luaA_checkboolean(L, 3);
459 break;
460 case A_TK_MOUSE_ENTER:
461 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
462 break;
463 case A_TK_MOUSE_LEAVE:
464 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
465 break;
466 default:
467 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
470 widget_invalidate_bywidget(*widget);
472 return 0;
475 const struct luaL_reg awesome_widget_methods[] =
477 { "__call", luaA_widget_new },
478 { NULL, NULL }
480 const struct luaL_reg awesome_widget_meta[] =
482 { "buttons", luaA_widget_buttons },
483 { "__index", luaA_widget_index },
484 { "__newindex", luaA_widget_newindex },
485 { "__gc", luaA_widget_gc },
486 { "__eq", luaA_widget_eq },
487 { "__tostring", luaA_widget_tostring },
488 { NULL, NULL }
491 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80