naughty: import
[awesome.git] / widget.c
bloba7b6428fae70cd772c56adbaa4cebbd0d5470589
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 /** Render a list of widgets.
86 * \param wnode The list of widgets.
87 * \param ctx The draw context where to render.
88 * \param rotate_px The rotate pixmap: where to rotate and render the final
89 * pixmap when the object oritation is not east.
90 * \param screen The logical screen used to render.
91 * \param orientation The object orientation.
92 * \param x The x coordinates of the object.
93 * \param y The y coordinates of the object.
94 * \param object The wibox.
95 * \todo Remove GC.
97 void
98 widget_render(widget_node_t *wnode, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
99 int screen, orientation_t orientation,
100 int x, int y, wibox_t *object)
102 xcb_pixmap_t rootpix;
103 xcb_screen_t *s;
104 widget_node_t *w;
105 int left = 0, right = 0;
106 char *data;
107 xcb_get_property_reply_t *prop_r;
108 xcb_get_property_cookie_t prop_c;
109 area_t rectangle = { 0, 0, 0, 0 };
111 rectangle.width = ctx->width;
112 rectangle.height = ctx->height;
114 if(ctx->bg.alpha != 0xffff)
116 s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
117 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
118 PIXMAP, 0, 1);
119 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
121 if(prop_r->value_len
122 && (data = xcb_get_property_value(prop_r))
123 && (rootpix = *(xcb_pixmap_t *) data))
124 switch(orientation)
126 case North:
127 draw_rotate(ctx,
128 rootpix, ctx->pixmap,
129 s->width_in_pixels, s->height_in_pixels,
130 ctx->width, ctx->height,
131 M_PI_2,
132 y + ctx->width,
133 - x);
134 break;
135 case South:
136 draw_rotate(ctx,
137 rootpix, ctx->pixmap,
138 s->width_in_pixels, s->height_in_pixels,
139 ctx->width, ctx->height,
140 - M_PI_2,
141 - y,
142 x + ctx->height);
143 break;
144 case East:
145 xcb_copy_area(globalconf.connection, rootpix,
146 rotate_px, gc,
147 x, y,
148 0, 0,
149 ctx->width, ctx->height);
150 break;
152 p_delete(&prop_r);
156 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
158 for(w = wnode; w; w = w->next)
159 if(w->widget->align == AlignLeft && w->widget->isvisible)
160 left += w->widget->draw(ctx, screen, w, left, (left + right), object);
162 /* renders right widget from last to first */
163 for(w = *widget_node_list_last(&wnode); w; w = w->prev)
164 if(w->widget->align == AlignRight && w->widget->isvisible)
165 right += w->widget->draw(ctx, screen, w, right, (left + right), object);
167 for(w = wnode; w; w = w->next)
168 if(w->widget->align == AlignFlex && w->widget->isvisible)
169 left += w->widget->draw(ctx, screen, w, left, (left + right), object);
171 switch(orientation)
173 case South:
174 draw_rotate(ctx, ctx->pixmap, rotate_px,
175 ctx->width, ctx->height,
176 ctx->height, ctx->width,
177 M_PI_2, ctx->height, 0);
178 break;
179 case North:
180 draw_rotate(ctx, ctx->pixmap, rotate_px,
181 ctx->width, ctx->height,
182 ctx->height, ctx->width,
183 - M_PI_2, 0, ctx->width);
184 break;
185 case East:
186 break;
191 /** Common function for creating a widget.
192 * \param widget The allocated widget.
194 void
195 widget_common_new(widget_t *widget)
197 widget->align = AlignLeft;
198 widget->button = widget_common_button;
201 /** Invalidate widgets which should be refresh upon
202 * external modifications. widget_t who watch flags will
203 * be set to be refreshed.
204 * \param screen Virtual screen number.
205 * \param flags Cache flags to invalidate.
207 void
208 widget_invalidate_cache(int screen, int flags)
210 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
212 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
213 widget_node_t *widget;
215 for(widget = wibox->widgets; widget; widget = widget->next)
216 if(widget->widget->cache_flags & flags)
218 wibox->need_update = true;
219 break;
224 /** Set a wibox needs update because it has widget, or redraw a titlebar.
225 * \todo Probably needs more optimization.
226 * \param widget The widget to look for.
228 void
229 widget_invalidate_bywidget(widget_t *widget)
231 int screen;
232 widget_node_t *witer;
233 client_t *c;
235 for(screen = 0; screen < globalconf.nscreen; screen++)
236 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
238 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
240 if(!wibox->need_update)
241 for(witer = wibox->widgets; witer; witer = witer->next)
242 if(witer->widget == widget)
244 wibox->need_update = true;
245 break;
249 for(c = globalconf.clients; c; c = c->next)
250 if(c->titlebar && !c->titlebar->need_update)
251 for(witer = c->titlebar->widgets; witer; witer = witer->next)
252 if(witer->widget == widget)
253 c->titlebar->need_update = true;
256 /** Create a new widget.
257 * \param L The Lua VM state.
259 * \luastack
260 * \lparam A table with at least a name and a type value. Optional attributes
261 * are: align.
262 * \lreturn A brand new widget.
264 static int
265 luaA_widget_new(lua_State *L)
267 const char *buf, *type;
268 widget_t *w = NULL;
269 widget_constructor_t *wc;
270 alignment_t align;
271 size_t len;
273 luaA_checktable(L, 2);
275 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
276 align = draw_align_fromstr(buf, len);
278 if(!(buf = luaA_getopt_string(L, 2, "name", NULL)))
279 luaL_error(L, "object widget must have a name");
281 type = luaA_getopt_string(L, 2, "type", NULL);
283 if((wc = name_func_lookup(type, WidgetList)))
284 w = wc(align);
285 else
286 luaL_error(L, "unkown widget type: %s", type);
288 w->type = wc;
290 /* Set visible by default. */
291 w->isvisible = true;
293 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
295 w->name = a_strdup(buf);
297 return luaA_widget_userdata_new(L, w);
300 /** Get or set mouse buttons bindings to a widget.
301 * \param L The Lua VM state.
303 * \luastack
304 * \lvalue A widget.
305 * \lparam An array of mouse button bindings objects, or nothing.
306 * \return The array of mouse button bindings objects of this widget.
308 static int
309 luaA_widget_buttons(lua_State *L)
311 widget_t **widget = luaA_checkudata(L, 1, "widget");
312 button_array_t *buttons = &(*widget)->buttons;
314 if(lua_gettop(L) == 2)
315 luaA_button_array_set(L, 2, buttons);
317 return luaA_button_array_get(L, buttons);
320 /** Convert a widget into a printable string.
321 * \param L The Lua VM state.
323 * \luastack
324 * \lvalue A widget.
326 static int
327 luaA_widget_tostring(lua_State *L)
329 widget_t **p = luaA_checkudata(L, 1, "widget");
330 lua_pushfstring(L, "[widget udata(%p) name(%s)]", *p, (*p)->name);
331 return 1;
334 /** Generic widget.
335 * \param L The Lua VM state.
336 * \return The number of elements pushed on stack.
337 * \luastack
338 * \lfield visible The widget visibility.
339 * \lfield name The widget name.
340 * \lfield mouse_enter A function to execute when the mouse enter the widget.
341 * \lfield mouse_leave A function to execute when the mouse leave the widget.
343 static int
344 luaA_widget_index(lua_State *L)
346 size_t len;
347 widget_t **widget = luaA_checkudata(L, 1, "widget");
348 const char *buf = luaL_checklstring(L, 2, &len);
349 awesome_token_t token;
351 if(luaA_usemetatable(L, 1, 2))
352 return 1;
354 switch((token = a_tokenize(buf, len)))
356 case A_TK_VISIBLE:
357 lua_pushboolean(L, (*widget)->isvisible);
358 return 1;
359 case A_TK_NAME:
360 lua_pushstring(L, (*widget)->name);
361 return 1;
362 case A_TK_MOUSE_ENTER:
363 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
364 return 1;
365 case A_TK_MOUSE_LEAVE:
366 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
367 return 1;
368 default:
369 break;
372 return (*widget)->index ? (*widget)->index(L, token) : 0;
375 /** Generic widget newindex.
376 * \param L The Lua VM state.
377 * \return The number of elements pushed on stack.
379 static int
380 luaA_widget_newindex(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 switch((token = a_tokenize(buf, len)))
389 case A_TK_VISIBLE:
390 (*widget)->isvisible = luaA_checkboolean(L, 3);
391 break;
392 case A_TK_MOUSE_ENTER:
393 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
394 break;
395 case A_TK_MOUSE_LEAVE:
396 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
397 break;
398 default:
399 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
402 widget_invalidate_bywidget(*widget);
404 return 0;
407 const struct luaL_reg awesome_widget_methods[] =
409 { "__call", luaA_widget_new },
410 { NULL, NULL }
412 const struct luaL_reg awesome_widget_meta[] =
414 { "buttons", luaA_widget_buttons },
415 { "__index", luaA_widget_index },
416 { "__newindex", luaA_widget_newindex },
417 { "__gc", luaA_widget_gc },
418 { "__eq", luaA_widget_eq },
419 { "__tostring", luaA_widget_tostring },
420 { NULL, NULL }
423 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80