titlebar: correctly ban/unban (FS#443)
[awesome.git] / widget.c
blobebda7fd6dbc2193edb7fb708c1d5e0b4c234dc1b
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;
73 /** Get a widget node from a wibox by coords.
74 * \param Container position.
75 * \param widgets The widget list.
76 * \param width The container width.
77 * \param height The container height.
78 * \param x X coordinate of the widget.
79 * \param y Y coordinate of the widget.
80 * \return A widget.
82 widget_t *
83 widget_getbycoords(position_t position, widget_node_array_t *widgets,
84 int width, int height, int16_t *x, int16_t *y)
86 int tmp;
88 /* Need to transform coordinates like it was top/bottom */
89 switch(position)
91 case Right:
92 tmp = *y;
93 *y = width - *x;
94 *x = tmp;
95 break;
96 case Left:
97 tmp = *y;
98 *y = *x;
99 *x = height - tmp;
100 break;
101 default:
102 break;
104 for(int i = 0; i < widgets->len; i++)
106 widget_node_t *w = &widgets->tab[i];
107 if(w->widget->isvisible &&
108 *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
109 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
110 return w->widget;
113 return NULL;
116 /** Convert a Lua table to a list of widget nodet.
117 * \param L The Lua VM state.
118 * \param widgets The linked list of widget node.
120 void
121 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
123 if(lua_istable(L, -1))
125 lua_pushnil(L);
126 while(luaA_next(L, -2))
128 luaA_table2widgets(L, widgets);
129 lua_pop(L, 1); /* remove value */
132 else
134 widget_t **widget = luaA_toudata(L, -1, "widget");
135 if(widget)
137 widget_node_t w;
138 p_clear(&w, 1);
139 w.widget = widget_ref(widget);
140 widget_node_array_append(widgets, w);
145 /** Render a list of widgets.
146 * \param wibox The wibox.
147 * \todo Remove GC.
149 void
150 widget_render(wibox_t *wibox)
152 draw_context_t *ctx = &wibox->sw.ctx;
153 int left = 0, right = 0;
154 area_t rectangle = { 0, 0, 0, 0 };
156 rectangle.width = ctx->width;
157 rectangle.height = ctx->height;
159 if(ctx->bg.alpha != 0xffff)
161 int x = wibox->sw.geometry.x, y = wibox->sw.geometry.y;
162 xcb_get_property_reply_t *prop_r;
163 char *data;
164 xcb_pixmap_t rootpix;
165 xcb_get_property_cookie_t prop_c;
166 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
167 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
168 PIXMAP, 0, 1);
169 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
171 if(prop_r->value_len
172 && (data = xcb_get_property_value(prop_r))
173 && (rootpix = *(xcb_pixmap_t *) data))
174 switch(wibox->sw.orientation)
176 case North:
177 draw_rotate(ctx,
178 rootpix, ctx->pixmap,
179 s->width_in_pixels, s->height_in_pixels,
180 ctx->width, ctx->height,
181 M_PI_2,
182 y + ctx->width,
183 - x);
184 break;
185 case South:
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,
192 x + ctx->height);
193 break;
194 case East:
195 xcb_copy_area(globalconf.connection, rootpix,
196 wibox->sw.pixmap, wibox->sw.gc,
197 x, y,
198 0, 0,
199 ctx->width, ctx->height);
200 break;
202 p_delete(&prop_r);
206 widget_node_array_t *widgets = &wibox->widgets;
208 /* compute geometry */
209 for(int i = 0; i < widgets->len; i++)
210 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
212 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
213 wibox->screen, ctx->height,
214 ctx->width - (left + right));
215 widgets->tab[i].geometry.x = left;
216 left += widgets->tab[i].geometry.width;
219 for(int i = widgets->len - 1; i >= 0; i--)
220 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
222 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
223 wibox->screen, ctx->height,
224 ctx->width - (left + right));
225 right += widgets->tab[i].geometry.width;
226 widgets->tab[i].geometry.x = ctx->width - right;
229 /* save left value */
230 int fake_left = left;
232 /* compute width of flex or fixed aligned widgets */
233 int flex = 0;
234 for(int i = 0; i < widgets->len; i++)
235 if(widgets->tab[i].widget->align & (AlignFlex | AlignFixed)
236 && widgets->tab[i].widget->isvisible)
238 if(widgets->tab[i].widget->align_supported & AlignFlex
239 && widgets->tab[i].widget->align == AlignFlex)
240 flex++;
241 else
242 fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
243 wibox->screen, ctx->height,
244 ctx->width - (fake_left + right)).width;
247 /* now compute everybody together! */
248 int flex_rendered = 0;
249 for(int i = 0; i < widgets->len; i++)
250 if(widgets->tab[i].widget->align & (AlignFlex | AlignFixed)
251 && widgets->tab[i].widget->isvisible)
253 if(widgets->tab[i].widget->align_supported & AlignFlex
254 && widgets->tab[i].widget->align == AlignFlex)
256 int width = (ctx->width - (right + fake_left)) / flex;
257 /* give last pixels to last flex to be rendered */
258 if(flex_rendered == flex - 1)
259 width += (ctx->width - (right + fake_left)) % flex;
260 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
261 wibox->screen, ctx->height,
262 width);
263 flex_rendered++;
265 else
266 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
267 wibox->screen, ctx->height,
268 ctx->width - (left + right));
269 widgets->tab[i].geometry.x = left;
270 left += widgets->tab[i].geometry.width;
273 /* draw everything! */
274 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
276 for(int i = 0; i < widgets->len; i++)
277 if(widgets->tab[i].widget->isvisible)
279 widgets->tab[i].geometry.y = 0;
280 widgets->tab[i].widget->draw(widgets->tab[i].widget,
281 ctx, widgets->tab[i].geometry,
282 wibox->screen, wibox);
285 switch(wibox->sw.orientation)
287 case South:
288 draw_rotate(ctx, ctx->pixmap, wibox->sw.pixmap,
289 ctx->width, ctx->height,
290 ctx->height, ctx->width,
291 M_PI_2, ctx->height, 0);
292 break;
293 case North:
294 draw_rotate(ctx, ctx->pixmap, wibox->sw.pixmap,
295 ctx->width, ctx->height,
296 ctx->height, ctx->width,
297 - M_PI_2, 0, ctx->width);
298 break;
299 case East:
300 break;
304 /** Invalidate widgets which should be refresh depending on their types.
305 * \param screen Virtual screen number.
306 * \param type Widget type to invalidate.
308 void
309 widget_invalidate_bytype(int screen, widget_constructor_t *type)
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->type == type)
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 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 *align, *type;
366 widget_t *w;
367 widget_constructor_t *wc;
368 size_t len;
370 luaA_checktable(L, 2);
372 type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
374 if((wc = name_func_lookup(type, len, WidgetList)))
376 w = p_new(widget_t, 1);
377 wc(w);
379 else
381 luaA_warn(L, "unkown widget type: %s", type);
382 return 0;
385 w->type = wc;
387 align = luaA_getopt_lstring(L, 2, "align", "left", &len);
388 w->align_supported |= AlignLeft | AlignRight | AlignFixed;
389 w->align = draw_align_fromstr(align, len);
391 /* Set visible by default. */
392 w->isvisible = true;
394 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
396 return luaA_widget_userdata_new(L, w);
399 /** Get or set mouse buttons bindings to a widget.
400 * \param L The Lua VM state.
402 * \luastack
403 * \lvalue A widget.
404 * \lparam An array of mouse button bindings objects, or nothing.
405 * \return The array of mouse button bindings objects of this widget.
407 static int
408 luaA_widget_buttons(lua_State *L)
410 widget_t **widget = luaA_checkudata(L, 1, "widget");
411 button_array_t *buttons = &(*widget)->buttons;
413 if(lua_gettop(L) == 2)
415 luaA_button_array_set(L, 2, buttons);
416 return 1;
419 return luaA_button_array_get(L, buttons);
422 /** Generic widget.
423 * \param L The Lua VM state.
424 * \return The number of elements pushed on stack.
425 * \luastack
426 * \lfield align The widget alignment.
427 * \lfield visible The widget visibility.
428 * \lfield mouse_enter A function to execute when the mouse enter the widget.
429 * \lfield mouse_leave A function to execute when the mouse leave the widget.
431 static int
432 luaA_widget_index(lua_State *L)
434 size_t len;
435 widget_t **widget = luaA_checkudata(L, 1, "widget");
436 const char *buf = luaL_checklstring(L, 2, &len);
437 awesome_token_t token;
439 if(luaA_usemetatable(L, 1, 2))
440 return 1;
442 switch((token = a_tokenize(buf, len)))
444 case A_TK_ALIGN:
445 lua_pushstring(L, draw_align_tostr((*widget)->align));
446 return 1;
447 case A_TK_VISIBLE:
448 lua_pushboolean(L, (*widget)->isvisible);
449 return 1;
450 case A_TK_MOUSE_ENTER:
451 if((*widget)->mouse_enter != LUA_REFNIL)
452 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
453 else
454 return 0;
455 return 1;
456 case A_TK_MOUSE_LEAVE:
457 if((*widget)->mouse_leave != LUA_REFNIL)
458 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
459 else
460 return 0;
461 return 1;
462 default:
463 break;
466 return (*widget)->index ? (*widget)->index(L, token) : 0;
469 /** Generic widget newindex.
470 * \param L The Lua VM state.
471 * \return The number of elements pushed on stack.
473 static int
474 luaA_widget_newindex(lua_State *L)
476 size_t len;
477 widget_t **widget = luaA_checkudata(L, 1, "widget");
478 const char *buf = luaL_checklstring(L, 2, &len);
479 awesome_token_t token;
481 switch((token = a_tokenize(buf, len)))
483 case A_TK_ALIGN:
484 buf = luaL_checklstring(L, 3, &len);
485 (*widget)->align = draw_align_fromstr(buf, len);
486 break;
487 case A_TK_VISIBLE:
488 (*widget)->isvisible = luaA_checkboolean(L, 3);
489 break;
490 case A_TK_MOUSE_ENTER:
491 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
492 return 0;
493 case A_TK_MOUSE_LEAVE:
494 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
495 return 0;
496 default:
497 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
500 widget_invalidate_bywidget(*widget);
502 return 0;
505 const struct luaL_reg awesome_widget_methods[] =
507 { "__call", luaA_widget_new },
508 { NULL, NULL }
510 const struct luaL_reg awesome_widget_meta[] =
512 { "buttons", luaA_widget_buttons },
513 { "__index", luaA_widget_index },
514 { "__newindex", luaA_widget_newindex },
515 { "__gc", luaA_widget_gc },
516 { "__eq", luaA_widget_eq },
517 { "__tostring", luaA_widget_tostring },
518 { NULL, NULL }
521 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80