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.
25 #include <xcb/xcb_atom.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.
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
);
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.
61 widget_calculate_offset(int barwidth
, int widgetwidth
, int offset
, int alignment
)
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.
77 luaA_table2widgets(lua_State
*L
, widget_node_array_t
*widgets
)
79 if(lua_istable(L
, -1))
82 while(luaA_next(L
, -2))
84 luaA_table2widgets(L
, widgets
);
85 lua_pop(L
, 1); /* remove value */
90 widget_t
**widget
= luaA_toudata(L
, -1, "widget");
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.
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
;
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
,
133 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
136 && (data
= xcb_get_property_value(prop_r
))
137 && (rootpix
= *(xcb_pixmap_t
*) data
))
142 rootpix
, ctx
->pixmap
,
143 s
->width_in_pixels
, s
->height_in_pixels
,
144 ctx
->width
, ctx
->height
,
151 rootpix
, ctx
->pixmap
,
152 s
->width_in_pixels
, s
->height_in_pixels
,
153 ctx
->width
, ctx
->height
,
159 xcb_copy_area(globalconf
.connection
, rootpix
,
163 ctx
->width
, ctx
->height
);
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
,
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
,
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 */
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
)
203 fake_left
+= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
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
,
226 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
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
,
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);
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
);
264 /** Invalidate widgets which should be refresh depending on their types.
265 * \param screen Virtual screen number.
266 * \param type Widget type to invalidate.
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;
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.
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;
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;
314 /** Create a new widget.
315 * \param L The Lua VM state.
318 * \lparam A table with at least a type value. Optional attributes
320 * \lreturn A brand new widget.
323 luaA_widget_new(lua_State
*L
)
325 const char *buf
, *type
;
327 widget_constructor_t
*wc
;
331 luaA_checktable(L
, 2);
333 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
334 align
= draw_align_fromstr(buf
, len
);
336 type
= luaA_getopt_string(L
, 2, "type", NULL
);
338 if((wc
= name_func_lookup(type
, WidgetList
)))
342 luaA_warn(L
, "unkown widget type: %s", type
);
348 w
->align_supported
|= AlignLeft
| AlignRight
;
350 /* Set visible by default. */
353 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
355 return luaA_widget_userdata_new(L
, w
);
358 /** Get or set mouse buttons bindings to a widget.
359 * \param L The Lua VM state.
363 * \lparam An array of mouse button bindings objects, or nothing.
364 * \return The array of mouse button bindings objects of this widget.
367 luaA_widget_buttons(lua_State
*L
)
369 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
370 button_array_t
*buttons
= &(*widget
)->buttons
;
372 if(lua_gettop(L
) == 2)
374 luaA_button_array_set(L
, 2, buttons
);
378 return luaA_button_array_get(L
, buttons
);
382 * \param L The Lua VM state.
383 * \return The number of elements pushed on stack.
385 * \lfield visible The widget visibility.
386 * \lfield mouse_enter A function to execute when the mouse enter the widget.
387 * \lfield mouse_leave A function to execute when the mouse leave the widget.
390 luaA_widget_index(lua_State
*L
)
393 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
394 const char *buf
= luaL_checklstring(L
, 2, &len
);
395 awesome_token_t token
;
397 if(luaA_usemetatable(L
, 1, 2))
400 switch((token
= a_tokenize(buf
, len
)))
403 lua_pushboolean(L
, (*widget
)->isvisible
);
405 case A_TK_MOUSE_ENTER
:
406 if((*widget
)->mouse_enter
!= LUA_REFNIL
)
407 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
411 case A_TK_MOUSE_LEAVE
:
412 if((*widget
)->mouse_leave
!= LUA_REFNIL
)
413 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
421 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
424 /** Generic widget newindex.
425 * \param L The Lua VM state.
426 * \return The number of elements pushed on stack.
429 luaA_widget_newindex(lua_State
*L
)
432 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
433 const char *buf
= luaL_checklstring(L
, 2, &len
);
434 awesome_token_t token
;
436 switch((token
= a_tokenize(buf
, len
)))
439 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
441 case A_TK_MOUSE_ENTER
:
442 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
444 case A_TK_MOUSE_LEAVE
:
445 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
448 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
451 widget_invalidate_bywidget(*widget
);
456 const struct luaL_reg awesome_widget_methods
[] =
458 { "__call", luaA_widget_new
},
461 const struct luaL_reg awesome_widget_meta
[] =
463 { "buttons", luaA_widget_buttons
},
464 { "__index", luaA_widget_index
},
465 { "__newindex", luaA_widget_newindex
},
466 { "__gc", luaA_widget_gc
},
467 { "__eq", luaA_widget_eq
},
468 { "__tostring", luaA_widget_tostring
},
472 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80