awesomerc: remove useless wibox name
[awesome.git] / widget.c
blob3d04da26ae9136a480995e57197476cecee48b38
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)->name);
52 p_delete(widget);
55 /** Compute offset for drawing the first pixel of a widget.
56 * \param barwidth The wibox width.
57 * \param widgetwidth The widget width.
58 * \param alignment The widget alignment on wibox.
59 * \return The x coordinate to draw at.
61 int
62 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
64 switch(alignment)
66 case AlignLeft:
67 case AlignFlex:
68 return offset;
70 return barwidth - offset - widgetwidth;
73 /** Common function for button press event on widget.
74 * It will look into configuration to find the callback function to call.
75 * \param w The widget node.
76 * \param ev The button press event the widget received.
77 * \param screen The screen number.
78 * \param p The object where user clicked.
80 static void
81 widget_common_button(widget_node_t *w,
82 xcb_button_press_event_t *ev,
83 int screen __attribute__ ((unused)),
84 wibox_t *p)
86 button_array_t *b = &w->widget->buttons;
88 for(int i = 0; i < b->len; i++)
89 if(ev->detail == b->tab[i]->button
90 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
92 luaA_wibox_userdata_new(globalconf.L, p);
93 luaA_dofunction(globalconf.L,
94 ev->response_type == XCB_BUTTON_PRESS ?
95 b->tab[i]->press : b->tab[i]->release,
96 1, 0);
100 /** Convert a Lua table to a list of widget nodet.
101 * \param L The Lua VM state.
102 * \param widgets The linked list of widget node.
104 void
105 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
107 if(lua_istable(L, -1))
109 lua_pushnil(L);
110 while(luaA_next(L, -2))
112 luaA_table2widgets(L, widgets);
113 lua_pop(L, 1); /* remove value */
116 else
118 widget_t **widget = luaA_toudata(L, -1, "widget");
119 if(widget)
121 widget_node_t w;
122 p_clear(&w, 1);
123 w.widget = widget_ref(widget);
124 widget_node_array_append(widgets, w);
129 /** Render a list of widgets.
130 * \param wnode The list of widgets.
131 * \param ctx The draw context where to render.
132 * \param rotate_px The rotate pixmap: where to rotate and render the final
133 * pixmap when the object oritation is not east.
134 * \param screen The logical screen used to render.
135 * \param orientation The object orientation.
136 * \param x The x coordinates of the object.
137 * \param y The y coordinates of the object.
138 * \param wibox The wibox.
139 * \todo Remove GC.
141 void
142 widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
143 int screen, orientation_t orientation,
144 int x, int y, wibox_t *wibox)
146 int left = 0, right = 0;
147 area_t rectangle = { 0, 0, 0, 0 };
149 rectangle.width = ctx->width;
150 rectangle.height = ctx->height;
152 if(ctx->bg.alpha != 0xffff)
154 xcb_get_property_reply_t *prop_r;
155 char *data;
156 xcb_pixmap_t rootpix;
157 xcb_get_property_cookie_t prop_c;
158 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
159 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
160 PIXMAP, 0, 1);
161 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
163 if(prop_r->value_len
164 && (data = xcb_get_property_value(prop_r))
165 && (rootpix = *(xcb_pixmap_t *) data))
166 switch(orientation)
168 case North:
169 draw_rotate(ctx,
170 rootpix, ctx->pixmap,
171 s->width_in_pixels, s->height_in_pixels,
172 ctx->width, ctx->height,
173 M_PI_2,
174 y + ctx->width,
175 - x);
176 break;
177 case South:
178 draw_rotate(ctx,
179 rootpix, ctx->pixmap,
180 s->width_in_pixels, s->height_in_pixels,
181 ctx->width, ctx->height,
182 - M_PI_2,
183 - y,
184 x + ctx->height);
185 break;
186 case East:
187 xcb_copy_area(globalconf.connection, rootpix,
188 rotate_px, gc,
189 x, y,
190 0, 0,
191 ctx->width, ctx->height);
192 break;
194 p_delete(&prop_r);
198 /* compute geometry */
199 for(int i = 0; i < widgets->len; i++)
200 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
202 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
203 screen, ctx->height,
204 ctx->width - (left + right));
205 widgets->tab[i].geometry.x = left;
206 left += widgets->tab[i].geometry.width;
209 for(int i = widgets->len - 1; i >= 0; i--)
210 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
212 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
213 screen, ctx->height,
214 ctx->width - (left + right));
215 right += widgets->tab[i].geometry.width;
216 widgets->tab[i].geometry.x = ctx->width - right;
219 /* save left value */
220 int fake_left = left;
222 /* compute width of flex aligned widgets that does not support it */
223 int flex = 0;
224 for(int i = 0; i < widgets->len; i++)
225 if(widgets->tab[i].widget->align == AlignFlex
226 && widgets->tab[i].widget->isvisible)
228 if(widgets->tab[i].widget->align_supported & AlignFlex)
229 flex++;
230 else
231 fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
232 screen, ctx->height,
233 ctx->width - (fake_left + right)).width;
236 /* now compute everybody together! */
237 int flex_rendered = 0;
238 for(int i = 0; i < widgets->len; i++)
239 if(widgets->tab[i].widget->align == AlignFlex
240 && widgets->tab[i].widget->isvisible)
242 if(widgets->tab[i].widget->align_supported & AlignFlex)
244 int width = (ctx->width - (right + fake_left)) / flex;
245 /* give last pixels to last flex to be rendered */
246 if(flex_rendered == flex - 1)
247 width += (ctx->width - (right + fake_left)) % flex;
248 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
249 screen, ctx->height,
250 width);
251 flex_rendered++;
253 else
254 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
255 screen, ctx->height,
256 ctx->width - (left + right));
257 widgets->tab[i].geometry.x = left;
258 left += widgets->tab[i].geometry.width;
261 /* draw everything! */
262 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
264 for(int i = 0; i < widgets->len; i++)
265 if(widgets->tab[i].widget->isvisible)
267 widgets->tab[i].geometry.y = 0;
268 widgets->tab[i].widget->draw(widgets->tab[i].widget,
269 ctx, widgets->tab[i].geometry,
270 screen, wibox);
273 switch(orientation)
275 case South:
276 draw_rotate(ctx, ctx->pixmap, rotate_px,
277 ctx->width, ctx->height,
278 ctx->height, ctx->width,
279 M_PI_2, ctx->height, 0);
280 break;
281 case North:
282 draw_rotate(ctx, ctx->pixmap, rotate_px,
283 ctx->width, ctx->height,
284 ctx->height, ctx->width,
285 - M_PI_2, 0, ctx->width);
286 break;
287 case East:
288 break;
292 /** Common function for creating a widget.
293 * \param widget The allocated widget.
295 void
296 widget_common_new(widget_t *widget)
298 widget->button = widget_common_button;
299 widget->align_supported = AlignLeft | AlignRight;
302 /** Invalidate widgets which should be refresh upon
303 * external modifications. widget_t who watch flags will
304 * be set to be refreshed.
305 * \param screen Virtual screen number.
306 * \param flags Cache flags to invalidate.
308 void
309 widget_invalidate_cache(int screen, int flags)
311 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
313 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
315 for(int j = 0; j < wibox->widgets.len; j++)
316 if(wibox->widgets.tab[j].widget->cache_flags & flags)
318 wibox->need_update = true;
319 break;
324 /** Set a wibox needs update because it has widget, or redraw a titlebar.
325 * \todo Probably needs more optimization.
326 * \param widget The widget to look for.
328 void
329 widget_invalidate_bywidget(widget_t *widget)
331 for(int screen = 0; screen < globalconf.nscreen; screen++)
332 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
334 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
335 if(!wibox->need_update)
336 for(int j = 0; j < wibox->widgets.len; j++)
337 if(wibox->widgets.tab[j].widget == widget)
339 wibox->need_update = true;
340 break;
344 for(client_t *c = globalconf.clients; c; c = c->next)
345 if(c->titlebar && !c->titlebar->need_update)
346 for(int j = 0; j < c->titlebar->widgets.len; j++)
347 if(c->titlebar->widgets.tab[j].widget == widget)
349 c->titlebar->need_update = true;
350 break;
354 /** Create a new widget.
355 * \param L The Lua VM state.
357 * \luastack
358 * \lparam A table with at least a name and a type value. Optional attributes
359 * are: align.
360 * \lreturn A brand new widget.
362 static int
363 luaA_widget_new(lua_State *L)
365 const char *buf, *type;
366 widget_t *w = NULL;
367 widget_constructor_t *wc;
368 alignment_t align;
369 size_t len;
371 luaA_checktable(L, 2);
373 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
374 align = draw_align_fromstr(buf, len);
376 if(!(buf = luaA_getopt_string(L, 2, "name", NULL)))
377 luaL_error(L, "object widget must have a name");
379 type = luaA_getopt_string(L, 2, "type", NULL);
381 if((wc = name_func_lookup(type, WidgetList)))
382 w = wc(align);
383 else
384 luaL_error(L, "unkown widget type: %s", type);
386 w->type = wc;
388 /* Set visible by default. */
389 w->isvisible = true;
391 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
393 w->name = a_strdup(buf);
395 return luaA_widget_userdata_new(L, w);
398 /** Get or set mouse buttons bindings to a widget.
399 * \param L The Lua VM state.
401 * \luastack
402 * \lvalue A widget.
403 * \lparam An array of mouse button bindings objects, or nothing.
404 * \return The array of mouse button bindings objects of this widget.
406 static int
407 luaA_widget_buttons(lua_State *L)
409 widget_t **widget = luaA_checkudata(L, 1, "widget");
410 button_array_t *buttons = &(*widget)->buttons;
412 if(lua_gettop(L) == 2)
414 luaA_button_array_set(L, 2, buttons);
415 return 1;
418 return luaA_button_array_get(L, buttons);
421 /** Generic widget.
422 * \param L The Lua VM state.
423 * \return The number of elements pushed on stack.
424 * \luastack
425 * \lfield visible The widget visibility.
426 * \lfield name The widget name.
427 * \lfield mouse_enter A function to execute when the mouse enter the widget.
428 * \lfield mouse_leave A function to execute when the mouse leave the widget.
430 static int
431 luaA_widget_index(lua_State *L)
433 size_t len;
434 widget_t **widget = luaA_checkudata(L, 1, "widget");
435 const char *buf = luaL_checklstring(L, 2, &len);
436 awesome_token_t token;
438 if(luaA_usemetatable(L, 1, 2))
439 return 1;
441 switch((token = a_tokenize(buf, len)))
443 case A_TK_VISIBLE:
444 lua_pushboolean(L, (*widget)->isvisible);
445 return 1;
446 case A_TK_NAME:
447 lua_pushstring(L, (*widget)->name);
448 return 1;
449 case A_TK_MOUSE_ENTER:
450 if((*widget)->mouse_enter != LUA_REFNIL)
451 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
452 else
453 return 0;
454 return 1;
455 case A_TK_MOUSE_LEAVE:
456 if((*widget)->mouse_leave != LUA_REFNIL)
457 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
458 else
459 return 0;
460 return 1;
461 default:
462 break;
465 return (*widget)->index ? (*widget)->index(L, token) : 0;
468 /** Generic widget newindex.
469 * \param L The Lua VM state.
470 * \return The number of elements pushed on stack.
472 static int
473 luaA_widget_newindex(lua_State *L)
475 size_t len;
476 widget_t **widget = luaA_checkudata(L, 1, "widget");
477 const char *buf = luaL_checklstring(L, 2, &len);
478 awesome_token_t token;
480 switch((token = a_tokenize(buf, len)))
482 case A_TK_VISIBLE:
483 (*widget)->isvisible = luaA_checkboolean(L, 3);
484 break;
485 case A_TK_MOUSE_ENTER:
486 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
487 break;
488 case A_TK_MOUSE_LEAVE:
489 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
490 break;
491 default:
492 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
495 widget_invalidate_bywidget(*widget);
497 return 0;
500 const struct luaL_reg awesome_widget_methods[] =
502 { "__call", luaA_widget_new },
503 { NULL, NULL }
505 const struct luaL_reg awesome_widget_meta[] =
507 { "buttons", luaA_widget_buttons },
508 { "__index", luaA_widget_index },
509 { "__newindex", luaA_widget_newindex },
510 { "__gc", luaA_widget_gc },
511 { "__eq", luaA_widget_eq },
512 { "__tostring", luaA_widget_tostring },
513 { NULL, NULL }
516 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80