2 * widget.c - widget managing
4 * Copyright © 2007-2009 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.
25 #include <xcb/xcb_atom.h>
32 #include "common/atoms.h"
33 #include "common/xutil.h"
35 /** Collect a widget structure.
36 * \param L The Lua VM state.
40 luaA_widget_gc(lua_State
*L
)
42 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
43 if(widget
->destructor
)
44 widget
->destructor(widget
);
45 return luaA_object_gc(L
);
48 /** Delete a widget node structure.
49 * \param node The node to destroy.
52 widget_node_delete(widget_node_t
*node
)
54 widget_unref(globalconf
.L
, node
->widget
);
57 /** Get a widget node from a wibox by coords.
58 * \param orientation Wibox orientation.
59 * \param widgets The widget list.
60 * \param width The container width.
61 * \param height The container height.
62 * \param x X coordinate of the widget.
63 * \param y Y coordinate of the widget.
67 widget_getbycoords(orientation_t orientation
, widget_node_array_t
*widgets
,
68 int width
, int height
, int16_t *x
, int16_t *y
)
72 /* Need to transform coordinates like it was top/bottom */
89 if(w
->widget
->isvisible
90 && *x
>= w
->geometry
.x
&& *x
< w
->geometry
.x
+ w
->geometry
.width
91 && *y
>= w
->geometry
.y
&& *y
< w
->geometry
.y
+ w
->geometry
.height
)
97 /** Convert a Lua table to a list of widget nodet.
98 * \param L The Lua VM state.
99 * \param widgets The linked list of widget node.
102 luaA_table2widgets(lua_State
*L
, widget_node_array_t
*widgets
)
104 if(lua_istable(L
, -1))
107 while(luaA_next(L
, -2))
108 luaA_table2widgets(L
, widgets
);
109 /* remove the table */
114 widget_t
*widget
= luaA_toudata(L
, -1, "widget");
119 w
.widget
= widget_ref(L
, -1);
120 widget_node_array_append(widgets
, w
);
123 lua_pop(L
, 1); /* remove value */
127 /** Retrieve a list of widget geometries using a Lua layout function.
128 * a table which contains the geometries is then pushed onto the stack
129 * \param wibox The wibox.
130 * \return True is everything is ok, false otherwise.
131 * \todo What do we do if there's no layout defined?
134 widget_geometries(wibox_t
*wibox
)
136 /* get the layout field of the widget table */
137 if(wibox
->widgets_table
)
140 wibox_push(globalconf
.L
, wibox
);
141 /* push widgets table */
142 luaA_object_push_item(globalconf
.L
, -1, wibox
->widgets_table
);
144 lua_remove(globalconf
.L
, -2);
145 lua_getfield(globalconf
.L
, -1, "layout");
148 lua_pushnil(globalconf
.L
);
150 /* if the layout field is a function */
151 if(lua_isfunction(globalconf
.L
, -1))
153 /* Push 1st argument: wibox geometry */
154 area_t geometry
= wibox
->geometry
;
157 /* we need to exchange the width and height of the wibox window if it
158 * it is rotated, so the layout function doesn't need to care about that
160 if(wibox
->orientation
!= East
)
162 int i
= geometry
.height
;
163 geometry
.height
= geometry
.width
;
166 luaA_pusharea(globalconf
.L
, geometry
);
167 /* Re-push 2nd argument: widget table */
168 lua_pushvalue(globalconf
.L
, -3);
169 /* Push 3rd argument: wibox screen */
170 lua_pushnumber(globalconf
.L
, screen_array_indexof(&globalconf
.screens
, wibox
->screen
));
171 /* Re-push the layout function */
172 lua_pushvalue(globalconf
.L
, -4);
173 /* call the layout function with 3 arguments (wibox geometry, widget
174 * table, screen) and wait for one result */
175 if(!luaA_dofunction(globalconf
.L
, 3, 1))
178 lua_insert(globalconf
.L
, -3);
179 lua_pop(globalconf
.L
, 2);
183 /* Remove the "nil function" */
184 lua_pop(globalconf
.L
, 1);
186 /* If no layout function has been specified, we just push a table with
187 * geometries onto the stack. These geometries are nothing fancy, they
188 * have x = y = 0 and their height and width set to the widgets demands
189 * or the wibox size, depending on which is less.
192 widget_node_array_t
*widgets
= &wibox
->widgets
;
193 widget_node_array_wipe(widgets
);
194 widget_node_array_init(widgets
);
197 wibox_push(globalconf
.L
, wibox
);
198 /* push widgets table */
199 luaA_object_push_item(globalconf
.L
, -1, wibox
->widgets_table
);
201 lua_remove(globalconf
.L
, -2);
202 luaA_table2widgets(globalconf
.L
, widgets
);
204 lua_newtable(globalconf
.L
);
205 for(int i
= 0; i
< widgets
->len
; i
++)
207 lua_pushnumber(globalconf
.L
, i
+ 1);
208 widget_t
*widget
= widgets
->tab
[i
].widget
;
209 lua_pushnumber(globalconf
.L
, screen_array_indexof(&globalconf
.screens
, wibox
->screen
));
210 area_t geometry
= widget
->extents(globalconf
.L
, widget
);
211 lua_pop(globalconf
.L
, 1);
212 geometry
.x
= geometry
.y
= 0;
213 geometry
.width
= MIN(wibox
->geometry
.width
, geometry
.width
);
214 geometry
.height
= MIN(wibox
->geometry
.height
, geometry
.height
);
216 luaA_pusharea(globalconf
.L
, geometry
);
218 lua_settable(globalconf
.L
, -3);
224 /** Render a list of widgets.
225 * \param wibox The wibox.
229 widget_render(wibox_t
*wibox
)
231 lua_State
*L
= globalconf
.L
;
232 draw_context_t
*ctx
= &wibox
->ctx
;
233 area_t rectangle
= { 0, 0, 0, 0 };
236 rectangle
.width
= ctx
->width
;
237 rectangle
.height
= ctx
->height
;
239 if (!widget_geometries(wibox
))
242 if(ctx
->bg
.alpha
!= 0xffff)
244 int x
= wibox
->geometry
.x
, y
= wibox
->geometry
.y
;
245 xcb_get_property_reply_t
*prop_r
;
247 xcb_pixmap_t rootpix
;
248 xcb_get_property_cookie_t prop_c
;
249 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
250 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
252 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
255 && (data
= xcb_get_property_value(prop_r
))
256 && (rootpix
= *(xcb_pixmap_t
*) data
))
257 switch(wibox
->orientation
)
261 rootpix
, ctx
->pixmap
,
262 s
->width_in_pixels
, s
->height_in_pixels
,
263 ctx
->width
, ctx
->height
,
270 rootpix
, ctx
->pixmap
,
271 s
->width_in_pixels
, s
->height_in_pixels
,
272 ctx
->width
, ctx
->height
,
278 xcb_copy_area(globalconf
.connection
, rootpix
,
279 wibox
->pixmap
, wibox
->gc
,
282 ctx
->width
, ctx
->height
);
289 widget_node_array_t
*widgets
= &wibox
->widgets
;
291 widget_node_array_wipe(widgets
);
292 widget_node_array_init(widgets
);
294 wibox_push(globalconf
.L
, wibox
);
295 /* push widgets table */
296 luaA_object_push_item(globalconf
.L
, -1, wibox
->widgets_table
);
298 lua_remove(globalconf
.L
, -2);
299 luaA_table2widgets(L
, widgets
);
301 /* get computed geometries */
302 for(unsigned int i
= 0; i
< lua_objlen(L
, -1); i
++)
304 lua_pushnumber(L
, i
+ 1);
307 widgets
->tab
[i
].geometry
.x
= luaA_getopt_number(L
, -1, "x", wibox
->geometry
.x
);
308 widgets
->tab
[i
].geometry
.y
= luaA_getopt_number(L
, -1, "y", wibox
->geometry
.y
);
309 widgets
->tab
[i
].geometry
.width
= luaA_getopt_number(L
, -1, "width", 1);
310 widgets
->tab
[i
].geometry
.height
= luaA_getopt_number(L
, -1, "height", 1);
316 /* draw background image, only if the background color is not opaque */
317 if(wibox
->bg_image
&& ctx
->bg
.alpha
!= 0xffff)
318 draw_image(ctx
, 0, 0, 1.0, wibox
->bg_image
);
320 /* draw background color */
321 xcolor_to_color(&ctx
->bg
, &col
);
322 draw_rectangle(ctx
, rectangle
, 1.0, true, &col
);
324 /* draw everything! */
325 for(int i
= 0; i
< widgets
->len
; i
++)
326 if(widgets
->tab
[i
].widget
->isvisible
)
327 widgets
->tab
[i
].widget
->draw(widgets
->tab
[i
].widget
,
328 ctx
, widgets
->tab
[i
].geometry
, wibox
);
330 switch(wibox
->orientation
)
333 draw_rotate(ctx
, ctx
->pixmap
, wibox
->pixmap
,
334 ctx
->width
, ctx
->height
,
335 ctx
->height
, ctx
->width
,
336 M_PI_2
, ctx
->height
, 0);
339 draw_rotate(ctx
, ctx
->pixmap
, wibox
->pixmap
,
340 ctx
->width
, ctx
->height
,
341 ctx
->height
, ctx
->width
,
342 - M_PI_2
, 0, ctx
->width
);
349 /** Invalidate widgets which should be refresh depending on their types.
350 * \param type Widget type to invalidate.
353 widget_invalidate_bytype(widget_constructor_t
*type
)
355 foreach(wibox
, globalconf
.wiboxes
)
356 foreach(wnode
, (*wibox
)->widgets
)
357 if(wnode
->widget
->type
== type
)
359 (*wibox
)->need_update
= true;
364 /** Set a wibox needs update because it has widget, or redraw a titlebar.
365 * \param widget The widget to look for.
368 widget_invalidate_bywidget(widget_t
*widget
)
370 foreach(wibox
, globalconf
.wiboxes
)
371 if(!(*wibox
)->need_update
)
372 foreach(wnode
, (*wibox
)->widgets
)
373 if(wnode
->widget
== widget
)
375 (*wibox
)->need_update
= true;
379 foreach(_c
, globalconf
.clients
)
382 if(c
->titlebar
&& !c
->titlebar
->need_update
)
383 for(int j
= 0; j
< c
->titlebar
->widgets
.len
; j
++)
384 if(c
->titlebar
->widgets
.tab
[j
].widget
== widget
)
386 c
->titlebar
->need_update
= true;
392 /** Create a new widget.
393 * \param L The Lua VM state.
396 * \lparam A table with at least a type value.
397 * \lreturn A brand new widget.
400 luaA_widget_new(lua_State
*L
)
404 widget_constructor_t
*wc
= NULL
;
407 luaA_checktable(L
, 2);
409 type
= luaA_getopt_lstring(L
, 2, "type", NULL
, &len
);
411 switch(a_tokenize(type
, len
))
416 case A_TK_PROGRESSBAR
:
417 wc
= widget_progressbar
;
426 wc
= widget_imagebox
;
439 luaA_warn(L
, "unkown widget type: %s", type
);
445 /* Set visible by default. */
452 * \param L The Lua VM state.
453 * \return The number of elements pushed on stack.
455 * \lfield visible The widget visibility.
456 * \lfield mouse_enter A function to execute when the mouse enter the widget.
457 * \lfield mouse_leave A function to execute when the mouse leave the widget.
460 luaA_widget_index(lua_State
*L
)
463 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
464 const char *buf
= luaL_checklstring(L
, 2, &len
);
465 awesome_token_t token
;
467 if(luaA_usemetatable(L
, 1, 2))
470 switch((token
= a_tokenize(buf
, len
)))
473 lua_pushboolean(L
, widget
->isvisible
);
475 case A_TK_MOUSE_ENTER
:
476 return luaA_object_push_item(L
, 1, widget
->mouse_enter
);
477 case A_TK_MOUSE_LEAVE
:
478 return luaA_object_push_item(L
, 1, widget
->mouse_leave
);
480 return luaA_object_push_item(L
, 1, widget
->buttons
);
485 return widget
->index
? widget
->index(L
, token
) : 0;
488 /** Generic widget newindex.
489 * \param L The Lua VM state.
490 * \return The number of elements pushed on stack.
493 luaA_widget_newindex(lua_State
*L
)
496 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
497 const char *buf
= luaL_checklstring(L
, 2, &len
);
498 awesome_token_t token
;
500 switch((token
= a_tokenize(buf
, len
)))
503 widget
->isvisible
= luaA_checkboolean(L
, 3);
505 case A_TK_MOUSE_ENTER
:
506 luaA_checkfunction(L
, 3);
507 luaA_object_unref_item(L
, 1, widget
->mouse_enter
);
508 widget
->mouse_enter
= luaA_object_ref_item(L
, 1, 3);
510 case A_TK_MOUSE_LEAVE
:
511 luaA_checkfunction(L
, 3);
512 luaA_object_unref_item(L
, 1, widget
->mouse_leave
);
513 widget
->mouse_leave
= luaA_object_ref_item(L
, 1, 3);
516 luaA_object_unref_item(L
, 1, widget
->buttons
);
517 widget
->buttons
= luaA_object_ref_item(L
, 1, 3);
520 return widget
->newindex
? widget
->newindex(L
, token
) : 0;
523 widget_invalidate_bywidget(widget
);
529 luaA_widget_extents(lua_State
*L
)
531 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
540 g
= widget
->extents(L
, widget
);
543 lua_pushnumber(L
, g
.width
);
544 lua_setfield(L
, -2, "width");
545 lua_pushnumber(L
, g
.height
);
546 lua_setfield(L
, -2, "height");
551 const struct luaL_reg awesome_widget_methods
[] =
553 LUA_CLASS_METHODS(widget
)
554 { "__call", luaA_widget_new
},
557 const struct luaL_reg awesome_widget_meta
[] =
559 LUA_OBJECT_META(widget
)
560 { "extents", luaA_widget_extents
},
561 { "__index", luaA_widget_index
},
562 { "__newindex", luaA_widget_newindex
},
563 { "__gc", luaA_widget_gc
},
567 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80