widget: bug fix when widget->len = 0
[awesome.git] / widget.c
blob166a638106c2317ca9076a9a6b075958c9b02ccb
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 xcb_pixmap_t rootpix;
132 xcb_screen_t *s;
133 int left = 0, right = 0;
134 char *data;
135 xcb_get_property_reply_t *prop_r;
136 xcb_get_property_cookie_t prop_c;
137 area_t rectangle = { 0, 0, 0, 0 };
139 rectangle.width = ctx->width;
140 rectangle.height = ctx->height;
142 if(ctx->bg.alpha != 0xffff)
144 s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
145 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
146 PIXMAP, 0, 1);
147 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
149 if(prop_r->value_len
150 && (data = xcb_get_property_value(prop_r))
151 && (rootpix = *(xcb_pixmap_t *) data))
152 switch(orientation)
154 case North:
155 draw_rotate(ctx,
156 rootpix, ctx->pixmap,
157 s->width_in_pixels, s->height_in_pixels,
158 ctx->width, ctx->height,
159 M_PI_2,
160 y + ctx->width,
161 - x);
162 break;
163 case South:
164 draw_rotate(ctx,
165 rootpix, ctx->pixmap,
166 s->width_in_pixels, s->height_in_pixels,
167 ctx->width, ctx->height,
168 - M_PI_2,
169 - y,
170 x + ctx->height);
171 break;
172 case East:
173 xcb_copy_area(globalconf.connection, rootpix,
174 rotate_px, gc,
175 x, y,
176 0, 0,
177 ctx->width, ctx->height);
178 break;
180 p_delete(&prop_r);
184 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
186 for(int i = 0; i < widgets->len; i++)
187 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
188 left += widgets->tab[i].widget->draw(ctx, screen, &widgets->tab[i], left, (left + right), wibox);
190 /* renders right widget from last to first */
191 for(int i = widgets->len - 1; i >= 0; i--)
192 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
193 right += widgets->tab[i].widget->draw(ctx, screen, &widgets->tab[i], right, (left + right), wibox);
195 /* \todo rewrite this */
196 int flex = 0;
197 for(int i = 0; i < widgets->len; i++)
198 if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible)
199 flex++;
201 if(flex)
203 int length = (ctx->width - (left + right)) / flex;
205 for(int i = 0; i < widgets->len; i++)
206 if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible)
207 left += widgets->tab[i].widget->draw(ctx, screen, &widgets->tab[i], left, (left + right) + length * --flex , wibox);
210 switch(orientation)
212 case South:
213 draw_rotate(ctx, ctx->pixmap, rotate_px,
214 ctx->width, ctx->height,
215 ctx->height, ctx->width,
216 M_PI_2, ctx->height, 0);
217 break;
218 case North:
219 draw_rotate(ctx, ctx->pixmap, rotate_px,
220 ctx->width, ctx->height,
221 ctx->height, ctx->width,
222 - M_PI_2, 0, ctx->width);
223 break;
224 case East:
225 break;
230 /** Common function for creating a widget.
231 * \param widget The allocated widget.
233 void
234 widget_common_new(widget_t *widget)
236 widget->align = AlignLeft;
237 widget->button = widget_common_button;
240 /** Invalidate widgets which should be refresh upon
241 * external modifications. widget_t who watch flags will
242 * be set to be refreshed.
243 * \param screen Virtual screen number.
244 * \param flags Cache flags to invalidate.
246 void
247 widget_invalidate_cache(int screen, int flags)
249 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
251 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
253 for(int j = 0; j < wibox->widgets.len; j++)
254 if(wibox->widgets.tab[j].widget->cache_flags & flags)
256 wibox->need_update = true;
257 break;
262 /** Set a wibox needs update because it has widget, or redraw a titlebar.
263 * \todo Probably needs more optimization.
264 * \param widget The widget to look for.
266 void
267 widget_invalidate_bywidget(widget_t *widget)
269 for(int screen = 0; screen < globalconf.nscreen; screen++)
270 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
272 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
273 if(!wibox->need_update)
274 for(int j = 0; j < wibox->widgets.len; j++)
275 if(wibox->widgets.tab[j].widget == widget)
277 wibox->need_update = true;
278 break;
282 for(client_t *c = globalconf.clients; c; c = c->next)
283 if(c->titlebar && !c->titlebar->need_update)
284 for(int j = 0; j < c->titlebar->widgets.len; j++)
285 if(c->titlebar->widgets.tab[j].widget == widget)
287 c->titlebar->need_update = true;
288 break;
292 /** Create a new widget.
293 * \param L The Lua VM state.
295 * \luastack
296 * \lparam A table with at least a name and a type value. Optional attributes
297 * are: align.
298 * \lreturn A brand new widget.
300 static int
301 luaA_widget_new(lua_State *L)
303 const char *buf, *type;
304 widget_t *w = NULL;
305 widget_constructor_t *wc;
306 alignment_t align;
307 size_t len;
309 luaA_checktable(L, 2);
311 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
312 align = draw_align_fromstr(buf, len);
314 if(!(buf = luaA_getopt_string(L, 2, "name", NULL)))
315 luaL_error(L, "object widget must have a name");
317 type = luaA_getopt_string(L, 2, "type", NULL);
319 if((wc = name_func_lookup(type, WidgetList)))
320 w = wc(align);
321 else
322 luaL_error(L, "unkown widget type: %s", type);
324 w->type = wc;
326 /* Set visible by default. */
327 w->isvisible = true;
329 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
331 w->name = a_strdup(buf);
333 return luaA_widget_userdata_new(L, w);
336 /** Get or set mouse buttons bindings to a widget.
337 * \param L The Lua VM state.
339 * \luastack
340 * \lvalue A widget.
341 * \lparam An array of mouse button bindings objects, or nothing.
342 * \return The array of mouse button bindings objects of this widget.
344 static int
345 luaA_widget_buttons(lua_State *L)
347 widget_t **widget = luaA_checkudata(L, 1, "widget");
348 button_array_t *buttons = &(*widget)->buttons;
350 if(lua_gettop(L) == 2)
351 luaA_button_array_set(L, 2, buttons);
353 return luaA_button_array_get(L, buttons);
356 /** Convert a widget into a printable string.
357 * \param L The Lua VM state.
359 * \luastack
360 * \lvalue A widget.
362 static int
363 luaA_widget_tostring(lua_State *L)
365 widget_t **p = luaA_checkudata(L, 1, "widget");
366 lua_pushfstring(L, "[widget udata(%p) name(%s)]", *p, (*p)->name);
367 return 1;
370 /** Generic widget.
371 * \param L The Lua VM state.
372 * \return The number of elements pushed on stack.
373 * \luastack
374 * \lfield visible The widget visibility.
375 * \lfield name The widget name.
376 * \lfield mouse_enter A function to execute when the mouse enter the widget.
377 * \lfield mouse_leave A function to execute when the mouse leave the widget.
379 static int
380 luaA_widget_index(lua_State *L)
382 size_t len;
383 widget_t **widget = luaA_checkudata(L, 1, "widget");
384 const char *buf = luaL_checklstring(L, 2, &len);
385 awesome_token_t token;
387 if(luaA_usemetatable(L, 1, 2))
388 return 1;
390 switch((token = a_tokenize(buf, len)))
392 case A_TK_VISIBLE:
393 lua_pushboolean(L, (*widget)->isvisible);
394 return 1;
395 case A_TK_NAME:
396 lua_pushstring(L, (*widget)->name);
397 return 1;
398 case A_TK_MOUSE_ENTER:
399 if((*widget)->mouse_enter != LUA_REFNIL)
400 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
401 else
402 return 0;
403 return 1;
404 case A_TK_MOUSE_LEAVE:
405 if((*widget)->mouse_leave != LUA_REFNIL)
406 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
407 else
408 return 0;
409 return 1;
410 default:
411 break;
414 return (*widget)->index ? (*widget)->index(L, token) : 0;
417 /** Generic widget newindex.
418 * \param L The Lua VM state.
419 * \return The number of elements pushed on stack.
421 static int
422 luaA_widget_newindex(lua_State *L)
424 size_t len;
425 widget_t **widget = luaA_checkudata(L, 1, "widget");
426 const char *buf = luaL_checklstring(L, 2, &len);
427 awesome_token_t token;
429 switch((token = a_tokenize(buf, len)))
431 case A_TK_VISIBLE:
432 (*widget)->isvisible = luaA_checkboolean(L, 3);
433 break;
434 case A_TK_MOUSE_ENTER:
435 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
436 break;
437 case A_TK_MOUSE_LEAVE:
438 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
439 break;
440 default:
441 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
444 widget_invalidate_bywidget(*widget);
446 return 0;
449 const struct luaL_reg awesome_widget_methods[] =
451 { "__call", luaA_widget_new },
452 { NULL, NULL }
454 const struct luaL_reg awesome_widget_meta[] =
456 { "buttons", luaA_widget_buttons },
457 { "__index", luaA_widget_index },
458 { "__newindex", luaA_widget_newindex },
459 { "__gc", luaA_widget_gc },
460 { "__eq", luaA_widget_eq },
461 { "__tostring", luaA_widget_tostring },
462 { NULL, NULL }
465 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80