xcursor: add new cursor infra
[awesome.git] / widget.c
blob4fa677fc1d9c1cc92e78c3e006f0ac29047ca7c6
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 return offset;
69 return barwidth - offset - widgetwidth;
72 /** Convert a Lua table to a list of widget nodet.
73 * \param L The Lua VM state.
74 * \param widgets The linked list of widget node.
76 void
77 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
79 if(lua_istable(L, -1))
81 lua_pushnil(L);
82 while(luaA_next(L, -2))
84 luaA_table2widgets(L, widgets);
85 lua_pop(L, 1); /* remove value */
88 else
90 widget_t **widget = luaA_toudata(L, -1, "widget");
91 if(widget)
93 widget_node_t w;
94 p_clear(&w, 1);
95 w.widget = widget_ref(widget);
96 widget_node_array_append(widgets, w);
101 /** Render a list of widgets.
102 * \param wnode The list of widgets.
103 * \param ctx The draw context where to render.
104 * \param rotate_px The rotate pixmap: where to rotate and render the final
105 * pixmap when the object oritation is not east.
106 * \param screen The logical screen used to render.
107 * \param orientation The object orientation.
108 * \param x The x coordinates of the object.
109 * \param y The y coordinates of the object.
110 * \param wibox The wibox.
111 * \todo Remove GC.
113 void
114 widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
115 int screen, orientation_t orientation,
116 int x, int y, wibox_t *wibox)
118 int left = 0, right = 0;
119 area_t rectangle = { 0, 0, 0, 0 };
121 rectangle.width = ctx->width;
122 rectangle.height = ctx->height;
124 if(ctx->bg.alpha != 0xffff)
126 xcb_get_property_reply_t *prop_r;
127 char *data;
128 xcb_pixmap_t rootpix;
129 xcb_get_property_cookie_t prop_c;
130 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
131 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
132 PIXMAP, 0, 1);
133 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
135 if(prop_r->value_len
136 && (data = xcb_get_property_value(prop_r))
137 && (rootpix = *(xcb_pixmap_t *) data))
138 switch(orientation)
140 case North:
141 draw_rotate(ctx,
142 rootpix, ctx->pixmap,
143 s->width_in_pixels, s->height_in_pixels,
144 ctx->width, ctx->height,
145 M_PI_2,
146 y + ctx->width,
147 - x);
148 break;
149 case South:
150 draw_rotate(ctx,
151 rootpix, ctx->pixmap,
152 s->width_in_pixels, s->height_in_pixels,
153 ctx->width, ctx->height,
154 - M_PI_2,
155 - y,
156 x + ctx->height);
157 break;
158 case East:
159 xcb_copy_area(globalconf.connection, rootpix,
160 rotate_px, gc,
161 x, y,
162 0, 0,
163 ctx->width, ctx->height);
164 break;
166 p_delete(&prop_r);
170 /* compute geometry */
171 for(int i = 0; i < widgets->len; i++)
172 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
174 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
175 screen, ctx->height,
176 ctx->width - (left + right));
177 widgets->tab[i].geometry.x = left;
178 left += widgets->tab[i].geometry.width;
181 for(int i = widgets->len - 1; i >= 0; i--)
182 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
184 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
185 screen, ctx->height,
186 ctx->width - (left + right));
187 right += widgets->tab[i].geometry.width;
188 widgets->tab[i].geometry.x = ctx->width - right;
191 /* save left value */
192 int fake_left = left;
194 /* compute width of flex aligned widgets that does not support it */
195 int flex = 0;
196 for(int i = 0; i < widgets->len; i++)
197 if(widgets->tab[i].widget->align == AlignFlex
198 && widgets->tab[i].widget->isvisible)
200 if(widgets->tab[i].widget->align_supported & AlignFlex)
201 flex++;
202 else
203 fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
204 screen, ctx->height,
205 ctx->width - (fake_left + right)).width;
208 /* now compute everybody together! */
209 int flex_rendered = 0;
210 for(int i = 0; i < widgets->len; i++)
211 if(widgets->tab[i].widget->align == AlignFlex
212 && widgets->tab[i].widget->isvisible)
214 if(widgets->tab[i].widget->align_supported & AlignFlex)
216 int width = (ctx->width - (right + fake_left)) / flex;
217 /* give last pixels to last flex to be rendered */
218 if(flex_rendered == flex - 1)
219 width += (ctx->width - (right + fake_left)) % flex;
220 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
221 screen, ctx->height,
222 width);
223 flex_rendered++;
225 else
226 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
227 screen, ctx->height,
228 ctx->width - (left + right));
229 widgets->tab[i].geometry.x = left;
230 left += widgets->tab[i].geometry.width;
233 /* draw everything! */
234 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
236 for(int i = 0; i < widgets->len; i++)
237 if(widgets->tab[i].widget->isvisible)
239 widgets->tab[i].geometry.y = 0;
240 widgets->tab[i].widget->draw(widgets->tab[i].widget,
241 ctx, widgets->tab[i].geometry,
242 screen, wibox);
245 switch(orientation)
247 case South:
248 draw_rotate(ctx, ctx->pixmap, rotate_px,
249 ctx->width, ctx->height,
250 ctx->height, ctx->width,
251 M_PI_2, ctx->height, 0);
252 break;
253 case North:
254 draw_rotate(ctx, ctx->pixmap, rotate_px,
255 ctx->width, ctx->height,
256 ctx->height, ctx->width,
257 - M_PI_2, 0, ctx->width);
258 break;
259 case East:
260 break;
264 /** Invalidate widgets which should be refresh depending on their types.
265 * \param screen Virtual screen number.
266 * \param type Widget type to invalidate.
268 void
269 widget_invalidate_bytype(int screen, widget_constructor_t *type)
271 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
273 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
275 for(int j = 0; j < wibox->widgets.len; j++)
276 if(wibox->widgets.tab[j].widget->type == type)
278 wibox->need_update = true;
279 break;
284 /** Set a wibox needs update because it has widget, or redraw a titlebar.
285 * \todo Probably needs more optimization.
286 * \param widget The widget to look for.
288 void
289 widget_invalidate_bywidget(widget_t *widget)
291 for(int screen = 0; screen < globalconf.nscreen; screen++)
292 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
294 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
295 if(!wibox->need_update)
296 for(int j = 0; j < wibox->widgets.len; j++)
297 if(wibox->widgets.tab[j].widget == widget)
299 wibox->need_update = true;
300 break;
304 for(client_t *c = globalconf.clients; c; c = c->next)
305 if(c->titlebar && !c->titlebar->need_update)
306 for(int j = 0; j < c->titlebar->widgets.len; j++)
307 if(c->titlebar->widgets.tab[j].widget == widget)
309 c->titlebar->need_update = true;
310 break;
314 /** Create a new widget.
315 * \param L The Lua VM state.
317 * \luastack
318 * \lparam A table with at least a type value. Optional attributes
319 * are: align.
320 * \lreturn A brand new widget.
322 static int
323 luaA_widget_new(lua_State *L)
325 const char *align, *type;
326 widget_t *w;
327 widget_constructor_t *wc;
328 size_t len;
330 luaA_checktable(L, 2);
332 type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
334 if((wc = name_func_lookup(type, len, WidgetList)))
336 w = p_new(widget_t, 1);
337 wc(w);
339 else
341 luaA_warn(L, "unkown widget type: %s", type);
342 return 0;
345 w->type = wc;
347 align = luaA_getopt_lstring(L, 2, "align", "left", &len);
348 w->align_supported |= AlignLeft | AlignRight;
349 w->align = draw_align_fromstr(align, len);
351 /* Set visible by default. */
352 w->isvisible = true;
354 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
356 return luaA_widget_userdata_new(L, w);
359 /** Get or set mouse buttons bindings to a widget.
360 * \param L The Lua VM state.
362 * \luastack
363 * \lvalue A widget.
364 * \lparam An array of mouse button bindings objects, or nothing.
365 * \return The array of mouse button bindings objects of this widget.
367 static int
368 luaA_widget_buttons(lua_State *L)
370 widget_t **widget = luaA_checkudata(L, 1, "widget");
371 button_array_t *buttons = &(*widget)->buttons;
373 if(lua_gettop(L) == 2)
375 luaA_button_array_set(L, 2, buttons);
376 return 1;
379 return luaA_button_array_get(L, buttons);
382 /** Generic widget.
383 * \param L The Lua VM state.
384 * \return The number of elements pushed on stack.
385 * \luastack
386 * \lfield visible The widget visibility.
387 * \lfield mouse_enter A function to execute when the mouse enter the widget.
388 * \lfield mouse_leave A function to execute when the mouse leave the widget.
390 static int
391 luaA_widget_index(lua_State *L)
393 size_t len;
394 widget_t **widget = luaA_checkudata(L, 1, "widget");
395 const char *buf = luaL_checklstring(L, 2, &len);
396 awesome_token_t token;
398 if(luaA_usemetatable(L, 1, 2))
399 return 1;
401 switch((token = a_tokenize(buf, len)))
403 case A_TK_VISIBLE:
404 lua_pushboolean(L, (*widget)->isvisible);
405 return 1;
406 case A_TK_MOUSE_ENTER:
407 if((*widget)->mouse_enter != LUA_REFNIL)
408 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
409 else
410 return 0;
411 return 1;
412 case A_TK_MOUSE_LEAVE:
413 if((*widget)->mouse_leave != LUA_REFNIL)
414 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
415 else
416 return 0;
417 return 1;
418 default:
419 break;
422 return (*widget)->index ? (*widget)->index(L, token) : 0;
425 /** Generic widget newindex.
426 * \param L The Lua VM state.
427 * \return The number of elements pushed on stack.
429 static int
430 luaA_widget_newindex(lua_State *L)
432 size_t len;
433 widget_t **widget = luaA_checkudata(L, 1, "widget");
434 const char *buf = luaL_checklstring(L, 2, &len);
435 awesome_token_t token;
437 switch((token = a_tokenize(buf, len)))
439 case A_TK_VISIBLE:
440 (*widget)->isvisible = luaA_checkboolean(L, 3);
441 break;
442 case A_TK_MOUSE_ENTER:
443 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
444 break;
445 case A_TK_MOUSE_LEAVE:
446 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
447 break;
448 default:
449 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
452 widget_invalidate_bywidget(*widget);
454 return 0;
457 const struct luaL_reg awesome_widget_methods[] =
459 { "__call", luaA_widget_new },
460 { NULL, NULL }
462 const struct luaL_reg awesome_widget_meta[] =
464 { "buttons", luaA_widget_buttons },
465 { "__index", luaA_widget_index },
466 { "__newindex", luaA_widget_newindex },
467 { "__gc", luaA_widget_gc },
468 { "__eq", luaA_widget_eq },
469 { "__tostring", luaA_widget_tostring },
470 { NULL, NULL }
473 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80