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 button_array_wipe(&widget
->buttons
);
46 return luaA_object_gc(L
);
49 /** Delete a widget node structure.
50 * \param node The node to destroy.
53 widget_node_delete(widget_node_t
*node
)
55 luaA_object_unref(globalconf
.L
, node
->widget
);
58 /** Get a widget node from a wibox by coords.
59 * \param orientation Wibox orientation.
60 * \param widgets The widget list.
61 * \param width The container width.
62 * \param height The container height.
63 * \param x X coordinate of the widget.
64 * \param y Y coordinate of the widget.
68 widget_getbycoords(orientation_t orientation
, widget_node_array_t
*widgets
,
69 int width
, int height
, int16_t *x
, int16_t *y
)
73 /* Need to transform coordinates like it was top/bottom */
90 if(w
->widget
->isvisible
91 && *x
>= w
->geometry
.x
&& *x
< w
->geometry
.x
+ w
->geometry
.width
92 && *y
>= w
->geometry
.y
&& *y
< w
->geometry
.y
+ w
->geometry
.height
)
98 /** Convert a Lua table to a list of widget nodet.
99 * \param L The Lua VM state.
100 * \param widgets The linked list of widget node.
103 luaA_table2widgets(lua_State
*L
, widget_node_array_t
*widgets
)
105 if(lua_istable(L
, -1))
108 while(luaA_next(L
, -2))
109 luaA_table2widgets(L
, widgets
);
110 /* remove the table */
115 widget_t
*widget
= luaA_toudata(L
, -1, "widget");
120 w
.widget
= luaA_object_ref(L
, -1);
121 widget_node_array_append(widgets
, w
);
124 lua_pop(L
, 1); /* remove value */
128 /** Retrieve a list of widget geometries using a Lua layout function.
129 * a table which contains the geometries is then pushed onto the stack
130 * \param wibox The wibox.
131 * \return True is everything is ok, false otherwise.
132 * \todo What do we do if there's no layout defined?
135 widget_geometries(wibox_t
*wibox
)
137 /* get the layout field of the widget table */
138 if(wibox
->widgets_table
)
141 luaA_object_push(globalconf
.L
, wibox
);
142 /* push widgets table */
143 luaA_object_push_item(globalconf
.L
, -1, wibox
->widgets_table
);
145 lua_remove(globalconf
.L
, -2);
146 lua_getfield(globalconf
.L
, -1, "layout");
149 lua_pushnil(globalconf
.L
);
151 /* if the layout field is a function */
152 if(lua_isfunction(globalconf
.L
, -1))
154 /* Push 1st argument: wibox geometry */
155 area_t geometry
= wibox
->geometry
;
158 /* we need to exchange the width and height of the wibox window if it
159 * it is rotated, so the layout function doesn't need to care about that
161 if(wibox
->orientation
!= East
)
163 int i
= geometry
.height
;
164 geometry
.height
= geometry
.width
;
167 luaA_pusharea(globalconf
.L
, geometry
);
168 /* Re-push 2nd argument: widget table */
169 lua_pushvalue(globalconf
.L
, -3);
170 /* Push 3rd argument: wibox screen */
171 lua_pushnumber(globalconf
.L
, screen_array_indexof(&globalconf
.screens
, wibox
->screen
));
172 /* Re-push the layout function */
173 lua_pushvalue(globalconf
.L
, -4);
174 /* call the layout function with 3 arguments (wibox geometry, widget
175 * table, screen) and wait for one result */
176 if(!luaA_dofunction(globalconf
.L
, 3, 1))
179 lua_insert(globalconf
.L
, -3);
180 lua_pop(globalconf
.L
, 2);
184 /* Remove the "nil function" */
185 lua_pop(globalconf
.L
, 1);
187 /* If no layout function has been specified, we just push a table with
188 * geometries onto the stack. These geometries are nothing fancy, they
189 * have x = y = 0 and their height and width set to the widgets demands
190 * or the wibox size, depending on which is less.
193 widget_node_array_t
*widgets
= &wibox
->widgets
;
194 widget_node_array_wipe(widgets
);
195 widget_node_array_init(widgets
);
198 luaA_object_push(globalconf
.L
, wibox
);
199 /* push widgets table */
200 luaA_object_push_item(globalconf
.L
, -1, wibox
->widgets_table
);
202 lua_remove(globalconf
.L
, -2);
203 luaA_table2widgets(globalconf
.L
, widgets
);
205 lua_newtable(globalconf
.L
);
206 for(int i
= 0; i
< widgets
->len
; i
++)
208 lua_pushnumber(globalconf
.L
, i
+ 1);
209 widget_t
*widget
= widgets
->tab
[i
].widget
;
210 lua_pushnumber(globalconf
.L
, screen_array_indexof(&globalconf
.screens
, wibox
->screen
));
211 area_t geometry
= widget
->extents(globalconf
.L
, widget
);
212 lua_pop(globalconf
.L
, 1);
213 geometry
.x
= geometry
.y
= 0;
214 geometry
.width
= MIN(wibox
->geometry
.width
, geometry
.width
);
215 geometry
.height
= MIN(wibox
->geometry
.height
, geometry
.height
);
217 luaA_pusharea(globalconf
.L
, geometry
);
219 lua_settable(globalconf
.L
, -3);
225 /** Render a list of widgets.
226 * \param wibox The wibox.
230 widget_render(wibox_t
*wibox
)
232 lua_State
*L
= globalconf
.L
;
233 draw_context_t
*ctx
= &wibox
->ctx
;
234 area_t rectangle
= { 0, 0, 0, 0 };
237 rectangle
.width
= ctx
->width
;
238 rectangle
.height
= ctx
->height
;
240 if (!widget_geometries(wibox
))
243 if(ctx
->bg
.alpha
!= 0xffff)
245 int x
= wibox
->geometry
.x
, y
= wibox
->geometry
.y
;
246 xcb_get_property_reply_t
*prop_r
;
248 xcb_pixmap_t rootpix
;
249 xcb_get_property_cookie_t prop_c
;
250 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
251 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
253 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
256 && (data
= xcb_get_property_value(prop_r
))
257 && (rootpix
= *(xcb_pixmap_t
*) data
))
258 switch(wibox
->orientation
)
262 rootpix
, ctx
->pixmap
,
263 s
->width_in_pixels
, s
->height_in_pixels
,
264 ctx
->width
, ctx
->height
,
271 rootpix
, ctx
->pixmap
,
272 s
->width_in_pixels
, s
->height_in_pixels
,
273 ctx
->width
, ctx
->height
,
279 xcb_copy_area(globalconf
.connection
, rootpix
,
280 wibox
->pixmap
, wibox
->gc
,
283 ctx
->width
, ctx
->height
);
290 widget_node_array_t
*widgets
= &wibox
->widgets
;
292 widget_node_array_wipe(widgets
);
293 widget_node_array_init(widgets
);
295 luaA_object_push(globalconf
.L
, wibox
);
296 /* push widgets table */
297 luaA_object_push_item(globalconf
.L
, -1, wibox
->widgets_table
);
299 lua_remove(globalconf
.L
, -2);
300 luaA_table2widgets(L
, widgets
);
302 /* get computed geometries */
303 for(unsigned int i
= 0; i
< lua_objlen(L
, -1); i
++)
305 lua_pushnumber(L
, i
+ 1);
308 widgets
->tab
[i
].geometry
.x
= luaA_getopt_number(L
, -1, "x", wibox
->geometry
.x
);
309 widgets
->tab
[i
].geometry
.y
= luaA_getopt_number(L
, -1, "y", wibox
->geometry
.y
);
310 widgets
->tab
[i
].geometry
.width
= luaA_getopt_number(L
, -1, "width", 1);
311 widgets
->tab
[i
].geometry
.height
= luaA_getopt_number(L
, -1, "height", 1);
317 /* draw background image, only if the background color is not opaque */
318 if(wibox
->bg_image
&& ctx
->bg
.alpha
!= 0xffff)
319 draw_image(ctx
, 0, 0, 1.0, wibox
->bg_image
);
321 /* draw background color */
322 xcolor_to_color(&ctx
->bg
, &col
);
323 draw_rectangle(ctx
, rectangle
, 1.0, true, &col
);
325 /* draw everything! */
326 for(int i
= 0; i
< widgets
->len
; i
++)
327 if(widgets
->tab
[i
].widget
->isvisible
)
328 widgets
->tab
[i
].widget
->draw(widgets
->tab
[i
].widget
,
329 ctx
, widgets
->tab
[i
].geometry
, wibox
);
331 switch(wibox
->orientation
)
334 draw_rotate(ctx
, ctx
->pixmap
, wibox
->pixmap
,
335 ctx
->width
, ctx
->height
,
336 ctx
->height
, ctx
->width
,
337 M_PI_2
, ctx
->height
, 0);
340 draw_rotate(ctx
, ctx
->pixmap
, wibox
->pixmap
,
341 ctx
->width
, ctx
->height
,
342 ctx
->height
, ctx
->width
,
343 - M_PI_2
, 0, ctx
->width
);
350 /** Invalidate widgets which should be refresh depending on their types.
351 * \param type Widget type to invalidate.
354 widget_invalidate_bytype(widget_constructor_t
*type
)
356 foreach(wibox
, globalconf
.wiboxes
)
357 foreach(wnode
, (*wibox
)->widgets
)
358 if(wnode
->widget
->type
== type
)
360 (*wibox
)->need_update
= true;
365 /** Set a wibox needs update because it has widget, or redraw a titlebar.
366 * \param widget The widget to look for.
369 widget_invalidate_bywidget(widget_t
*widget
)
371 foreach(wibox
, globalconf
.wiboxes
)
372 if(!(*wibox
)->need_update
)
373 foreach(wnode
, (*wibox
)->widgets
)
374 if(wnode
->widget
== widget
)
376 (*wibox
)->need_update
= true;
380 foreach(_c
, globalconf
.clients
)
383 if(c
->titlebar
&& !c
->titlebar
->need_update
)
384 for(int j
= 0; j
< c
->titlebar
->widgets
.len
; j
++)
385 if(c
->titlebar
->widgets
.tab
[j
].widget
== widget
)
387 c
->titlebar
->need_update
= true;
393 /** Create a new widget.
394 * \param L The Lua VM state.
397 * \lparam A table with at least a type value.
398 * \lreturn A brand new widget.
401 luaA_widget_new(lua_State
*L
)
405 widget_constructor_t
*wc
= NULL
;
408 luaA_checktable(L
, 2);
410 type
= luaA_getopt_lstring(L
, 2, "type", NULL
, &len
);
412 switch(a_tokenize(type
, len
))
417 case A_TK_PROGRESSBAR
:
418 wc
= widget_progressbar
;
427 wc
= widget_imagebox
;
440 luaA_warn(L
, "unkown widget type: %s", type
);
446 /* Set visible by default. */
452 /** Get or set mouse buttons bindings to a widget.
453 * \param L The Lua VM state.
457 * \lparam An array of mouse button bindings objects, or nothing.
458 * \return The array of mouse button bindings objects of this widget.
461 luaA_widget_buttons(lua_State
*L
)
463 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
465 if(lua_gettop(L
) == 2)
467 luaA_button_array_set(L
, 1, 2, &widget
->buttons
);
471 return luaA_button_array_get(L
, 1, &widget
->buttons
);
475 * \param L The Lua VM state.
476 * \return The number of elements pushed on stack.
478 * \lfield visible The widget visibility.
479 * \lfield mouse_enter A function to execute when the mouse enter the widget.
480 * \lfield mouse_leave A function to execute when the mouse leave the widget.
483 luaA_widget_index(lua_State
*L
)
486 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
487 const char *buf
= luaL_checklstring(L
, 2, &len
);
488 awesome_token_t token
;
490 if(luaA_usemetatable(L
, 1, 2))
493 switch((token
= a_tokenize(buf
, len
)))
496 lua_pushboolean(L
, widget
->isvisible
);
498 case A_TK_MOUSE_ENTER
:
499 return luaA_object_push_item(L
, 1, widget
->mouse_enter
);
500 case A_TK_MOUSE_LEAVE
:
501 return luaA_object_push_item(L
, 1, widget
->mouse_leave
);
506 return widget
->index
? widget
->index(L
, token
) : 0;
509 /** Generic widget newindex.
510 * \param L The Lua VM state.
511 * \return The number of elements pushed on stack.
514 luaA_widget_newindex(lua_State
*L
)
517 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
518 const char *buf
= luaL_checklstring(L
, 2, &len
);
519 awesome_token_t token
;
521 switch((token
= a_tokenize(buf
, len
)))
524 widget
->isvisible
= luaA_checkboolean(L
, 3);
526 case A_TK_MOUSE_ENTER
:
527 luaA_checkfunction(L
, 3);
528 luaA_object_unref_item(L
, 1, widget
->mouse_enter
);
529 widget
->mouse_enter
= luaA_object_ref_item(L
, 1, 3);
531 case A_TK_MOUSE_LEAVE
:
532 luaA_checkfunction(L
, 3);
533 luaA_object_unref_item(L
, 1, widget
->mouse_leave
);
534 widget
->mouse_leave
= luaA_object_ref_item(L
, 1, 3);
537 return widget
->newindex
? widget
->newindex(L
, token
) : 0;
540 widget_invalidate_bywidget(widget
);
546 luaA_widget_extents(lua_State
*L
)
548 widget_t
*widget
= luaL_checkudata(L
, 1, "widget");
557 g
= widget
->extents(L
, widget
);
560 lua_pushnumber(L
, g
.width
);
561 lua_setfield(L
, -2, "width");
562 lua_pushnumber(L
, g
.height
);
563 lua_setfield(L
, -2, "height");
568 const struct luaL_reg awesome_widget_methods
[] =
570 LUA_CLASS_METHODS(widget
)
571 { "__call", luaA_widget_new
},
574 const struct luaL_reg awesome_widget_meta
[] =
576 LUA_OBJECT_META(widget
)
577 { "buttons", luaA_widget_buttons
},
578 { "extents", luaA_widget_extents
},
579 { "__index", luaA_widget_index
},
580 { "__newindex", luaA_widget_newindex
},
581 { "__gc", luaA_widget_gc
},
585 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80