luaa: do not replace string.len(), export wlen()
[awesome.git] / widget.c
blobffd3d85036e0ca9bc2a9b38e91c314354761fa51
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 /** Delete a widget structure.
41 * \param widget The widget to destroy.
43 void
44 widget_delete(widget_t **widget)
46 if((*widget)->destructor)
47 (*widget)->destructor(*widget);
48 button_array_wipe(&(*widget)->buttons);
49 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
50 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
51 p_delete(widget);
54 /** Compute offset for drawing the first pixel of a widget.
55 * \param barwidth The wibox width.
56 * \param widgetwidth The widget width.
57 * \param alignment The widget alignment on wibox.
58 * \return The x coordinate to draw at.
60 int
61 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
63 switch(alignment)
65 case AlignLeft:
66 case AlignFlex:
67 case AlignFixed:
68 return offset;
70 return barwidth - offset - widgetwidth;
74 /** Get a widget node from a wibox by coords.
75 * \param Container position.
76 * \param widgets The widget list.
77 * \param width The container width.
78 * \param height The container height.
79 * \param x X coordinate of the widget.
80 * \param y Y coordinate of the widget.
81 * \return A widget.
83 widget_t *
84 widget_getbycoords(position_t position, widget_node_array_t *widgets,
85 int width, int height, int16_t *x, int16_t *y)
87 int tmp;
89 /* Need to transform coordinates like it was top/bottom */
90 switch(position)
92 case Right:
93 tmp = *y;
94 *y = width - *x;
95 *x = tmp;
96 break;
97 case Left:
98 tmp = *y;
99 *y = *x;
100 *x = height - tmp;
101 break;
102 default:
103 break;
105 for(int i = 0; i < widgets->len; i++)
107 widget_node_t *w = &widgets->tab[i];
108 if(w->widget->isvisible &&
109 *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
110 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
111 return w->widget;
114 return NULL;
117 /** Convert a Lua table to a list of widget nodet.
118 * \param L The Lua VM state.
119 * \param widgets The linked list of widget node.
121 void
122 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
124 if(lua_istable(L, -1))
126 lua_pushnil(L);
127 while(luaA_next(L, -2))
129 luaA_table2widgets(L, widgets);
130 lua_pop(L, 1); /* remove value */
133 else
135 widget_t **widget = luaA_toudata(L, -1, "widget");
136 if(widget)
138 widget_node_t w;
139 p_clear(&w, 1);
140 w.widget = widget_ref(widget);
141 widget_node_array_append(widgets, w);
146 /** Render a list of widgets.
147 * \param wnode The list of widgets.
148 * \param ctx The draw context where to render.
149 * \param rotate_px The rotate pixmap: where to rotate and render the final
150 * pixmap when the object oritation is not east.
151 * \param screen The logical screen used to render.
152 * \param orientation The object orientation.
153 * \param x The x coordinates of the object.
154 * \param y The y coordinates of the object.
155 * \param wibox The wibox.
156 * \todo Remove GC.
158 void
159 widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
160 int screen, orientation_t orientation,
161 int x, int y, wibox_t *wibox)
163 int left = 0, right = 0;
164 area_t rectangle = { 0, 0, 0, 0 };
166 rectangle.width = ctx->width;
167 rectangle.height = ctx->height;
169 if(ctx->bg.alpha != 0xffff)
171 xcb_get_property_reply_t *prop_r;
172 char *data;
173 xcb_pixmap_t rootpix;
174 xcb_get_property_cookie_t prop_c;
175 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
176 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
177 PIXMAP, 0, 1);
178 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
180 if(prop_r->value_len
181 && (data = xcb_get_property_value(prop_r))
182 && (rootpix = *(xcb_pixmap_t *) data))
183 switch(orientation)
185 case North:
186 draw_rotate(ctx,
187 rootpix, ctx->pixmap,
188 s->width_in_pixels, s->height_in_pixels,
189 ctx->width, ctx->height,
190 M_PI_2,
191 y + ctx->width,
192 - x);
193 break;
194 case South:
195 draw_rotate(ctx,
196 rootpix, ctx->pixmap,
197 s->width_in_pixels, s->height_in_pixels,
198 ctx->width, ctx->height,
199 - M_PI_2,
200 - y,
201 x + ctx->height);
202 break;
203 case East:
204 xcb_copy_area(globalconf.connection, rootpix,
205 rotate_px, gc,
206 x, y,
207 0, 0,
208 ctx->width, ctx->height);
209 break;
211 p_delete(&prop_r);
215 /* compute geometry */
216 for(int i = 0; i < widgets->len; i++)
217 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
219 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
220 screen, ctx->height,
221 ctx->width - (left + right));
222 widgets->tab[i].geometry.x = left;
223 left += widgets->tab[i].geometry.width;
226 for(int i = widgets->len - 1; i >= 0; i--)
227 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
229 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
230 screen, ctx->height,
231 ctx->width - (left + right));
232 right += widgets->tab[i].geometry.width;
233 widgets->tab[i].geometry.x = ctx->width - right;
236 /* save left value */
237 int fake_left = left;
239 /* compute width of flex or fixed aligned widgets */
240 int flex = 0;
241 for(int i = 0; i < widgets->len; i++)
242 if(widgets->tab[i].widget->align & (AlignFlex | AlignFixed)
243 && widgets->tab[i].widget->isvisible)
245 if(widgets->tab[i].widget->align_supported & AlignFlex
246 && widgets->tab[i].widget->align == AlignFlex)
247 flex++;
248 else
249 fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
250 screen, ctx->height,
251 ctx->width - (fake_left + right)).width;
254 /* now compute everybody together! */
255 int flex_rendered = 0;
256 for(int i = 0; i < widgets->len; i++)
257 if(widgets->tab[i].widget->align & (AlignFlex | AlignFixed)
258 && widgets->tab[i].widget->isvisible)
260 if(widgets->tab[i].widget->align_supported & AlignFlex
261 && widgets->tab[i].widget->align == AlignFlex)
263 int width = (ctx->width - (right + fake_left)) / flex;
264 /* give last pixels to last flex to be rendered */
265 if(flex_rendered == flex - 1)
266 width += (ctx->width - (right + fake_left)) % flex;
267 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
268 screen, ctx->height,
269 width);
270 flex_rendered++;
272 else
273 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
274 screen, ctx->height,
275 ctx->width - (left + right));
276 widgets->tab[i].geometry.x = left;
277 left += widgets->tab[i].geometry.width;
280 /* draw everything! */
281 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
283 for(int i = 0; i < widgets->len; i++)
284 if(widgets->tab[i].widget->isvisible)
286 widgets->tab[i].geometry.y = 0;
287 widgets->tab[i].widget->draw(widgets->tab[i].widget,
288 ctx, widgets->tab[i].geometry,
289 screen, wibox);
292 switch(orientation)
294 case South:
295 draw_rotate(ctx, ctx->pixmap, rotate_px,
296 ctx->width, ctx->height,
297 ctx->height, ctx->width,
298 M_PI_2, ctx->height, 0);
299 break;
300 case North:
301 draw_rotate(ctx, ctx->pixmap, rotate_px,
302 ctx->width, ctx->height,
303 ctx->height, ctx->width,
304 - M_PI_2, 0, ctx->width);
305 break;
306 case East:
307 break;
311 /** Invalidate widgets which should be refresh depending on their types.
312 * \param screen Virtual screen number.
313 * \param type Widget type to invalidate.
315 void
316 widget_invalidate_bytype(int screen, widget_constructor_t *type)
318 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
320 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
322 for(int j = 0; j < wibox->widgets.len; j++)
323 if(wibox->widgets.tab[j].widget->type == type)
325 wibox->need_update = true;
326 break;
331 /** Set a wibox needs update because it has widget, or redraw a titlebar.
332 * \todo Probably needs more optimization.
333 * \param widget The widget to look for.
335 void
336 widget_invalidate_bywidget(widget_t *widget)
338 for(int screen = 0; screen < globalconf.nscreen; screen++)
339 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
341 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
342 if(!wibox->need_update)
343 for(int j = 0; j < wibox->widgets.len; j++)
344 if(wibox->widgets.tab[j].widget == widget)
346 wibox->need_update = true;
347 break;
351 for(client_t *c = globalconf.clients; c; c = c->next)
352 if(c->titlebar && !c->titlebar->need_update)
353 for(int j = 0; j < c->titlebar->widgets.len; j++)
354 if(c->titlebar->widgets.tab[j].widget == widget)
356 c->titlebar->need_update = true;
357 break;
361 /** Create a new widget.
362 * \param L The Lua VM state.
364 * \luastack
365 * \lparam A table with at least a type value. Optional attributes
366 * are: align.
367 * \lreturn A brand new widget.
369 static int
370 luaA_widget_new(lua_State *L)
372 const char *align, *type;
373 widget_t *w;
374 widget_constructor_t *wc;
375 size_t len;
377 luaA_checktable(L, 2);
379 type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
381 if((wc = name_func_lookup(type, len, WidgetList)))
383 w = p_new(widget_t, 1);
384 wc(w);
386 else
388 luaA_warn(L, "unkown widget type: %s", type);
389 return 0;
392 w->type = wc;
394 align = luaA_getopt_lstring(L, 2, "align", "left", &len);
395 w->align_supported |= AlignLeft | AlignRight | AlignFixed;
396 w->align = draw_align_fromstr(align, len);
398 /* Set visible by default. */
399 w->isvisible = true;
401 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
403 return luaA_widget_userdata_new(L, w);
406 /** Get or set mouse buttons bindings to a widget.
407 * \param L The Lua VM state.
409 * \luastack
410 * \lvalue A widget.
411 * \lparam An array of mouse button bindings objects, or nothing.
412 * \return The array of mouse button bindings objects of this widget.
414 static int
415 luaA_widget_buttons(lua_State *L)
417 widget_t **widget = luaA_checkudata(L, 1, "widget");
418 button_array_t *buttons = &(*widget)->buttons;
420 if(lua_gettop(L) == 2)
422 luaA_button_array_set(L, 2, buttons);
423 return 1;
426 return luaA_button_array_get(L, buttons);
429 /** Generic widget.
430 * \param L The Lua VM state.
431 * \return The number of elements pushed on stack.
432 * \luastack
433 * \lfield visible The widget visibility.
434 * \lfield mouse_enter A function to execute when the mouse enter the widget.
435 * \lfield mouse_leave A function to execute when the mouse leave the widget.
437 static int
438 luaA_widget_index(lua_State *L)
440 size_t len;
441 widget_t **widget = luaA_checkudata(L, 1, "widget");
442 const char *buf = luaL_checklstring(L, 2, &len);
443 awesome_token_t token;
445 if(luaA_usemetatable(L, 1, 2))
446 return 1;
448 switch((token = a_tokenize(buf, len)))
450 case A_TK_VISIBLE:
451 lua_pushboolean(L, (*widget)->isvisible);
452 return 1;
453 case A_TK_MOUSE_ENTER:
454 if((*widget)->mouse_enter != LUA_REFNIL)
455 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
456 else
457 return 0;
458 return 1;
459 case A_TK_MOUSE_LEAVE:
460 if((*widget)->mouse_leave != LUA_REFNIL)
461 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
462 else
463 return 0;
464 return 1;
465 default:
466 break;
469 return (*widget)->index ? (*widget)->index(L, token) : 0;
472 /** Generic widget newindex.
473 * \param L The Lua VM state.
474 * \return The number of elements pushed on stack.
476 static int
477 luaA_widget_newindex(lua_State *L)
479 size_t len;
480 widget_t **widget = luaA_checkudata(L, 1, "widget");
481 const char *buf = luaL_checklstring(L, 2, &len);
482 awesome_token_t token;
484 switch((token = a_tokenize(buf, len)))
486 case A_TK_VISIBLE:
487 (*widget)->isvisible = luaA_checkboolean(L, 3);
488 break;
489 case A_TK_MOUSE_ENTER:
490 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
491 break;
492 case A_TK_MOUSE_LEAVE:
493 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
494 break;
495 default:
496 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
499 widget_invalidate_bywidget(*widget);
501 return 0;
504 const struct luaL_reg awesome_widget_methods[] =
506 { "__call", luaA_widget_new },
507 { NULL, NULL }
509 const struct luaL_reg awesome_widget_meta[] =
511 { "buttons", luaA_widget_buttons },
512 { "__index", luaA_widget_index },
513 { "__newindex", luaA_widget_newindex },
514 { "__gc", luaA_widget_gc },
515 { "__eq", luaA_widget_eq },
516 { "__tostring", luaA_widget_tostring },
517 { NULL, NULL }
520 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80