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
);
51 p_delete(&(*widget
)->name
);
55 /** Compute offset for drawing the first pixel of a widget.
56 * \param barwidth The wibox width.
57 * \param widgetwidth The widget width.
58 * \param alignment The widget alignment on wibox.
59 * \return The x coordinate to draw at.
62 widget_calculate_offset(int barwidth
, int widgetwidth
, int offset
, int alignment
)
70 return barwidth
- offset
- widgetwidth
;
73 /** Common function for button press event on widget.
74 * It will look into configuration to find the callback function to call.
75 * \param w The widget node.
76 * \param ev The button press event the widget received.
77 * \param screen The screen number.
78 * \param p The object where user clicked.
81 widget_common_button(widget_node_t
*w
,
82 xcb_button_press_event_t
*ev
,
83 int screen
__attribute__ ((unused
)),
86 button_array_t
*b
= &w
->widget
->buttons
;
88 for(int i
= 0; i
< b
->len
; i
++)
89 if(ev
->detail
== b
->tab
[i
]->button
90 && XUTIL_MASK_CLEAN(ev
->state
) == b
->tab
[i
]->mod
)
92 luaA_wibox_userdata_new(globalconf
.L
, p
);
93 luaA_dofunction(globalconf
.L
,
94 ev
->response_type
== XCB_BUTTON_PRESS
?
95 b
->tab
[i
]->press
: b
->tab
[i
]->release
,
100 /** Convert a Lua table to a list of widget nodet.
101 * \param L The Lua VM state.
102 * \param widgets The linked list of widget node.
105 luaA_table2widgets(lua_State
*L
, widget_node_array_t
*widgets
)
107 if(lua_istable(L
, -1))
110 while(luaA_next(L
, -2))
112 luaA_table2widgets(L
, widgets
);
113 lua_pop(L
, 1); /* remove value */
118 widget_t
**widget
= luaA_toudata(L
, -1, "widget");
123 w
.widget
= widget_ref(widget
);
124 widget_node_array_append(widgets
, w
);
129 /** Render a list of widgets.
130 * \param wnode The list of widgets.
131 * \param ctx The draw context where to render.
132 * \param rotate_px The rotate pixmap: where to rotate and render the final
133 * pixmap when the object oritation is not east.
134 * \param screen The logical screen used to render.
135 * \param orientation The object orientation.
136 * \param x The x coordinates of the object.
137 * \param y The y coordinates of the object.
138 * \param wibox The wibox.
142 widget_render(widget_node_array_t
*widgets
, draw_context_t
*ctx
, xcb_gcontext_t gc
, xcb_pixmap_t rotate_px
,
143 int screen
, orientation_t orientation
,
144 int x
, int y
, wibox_t
*wibox
)
146 int left
= 0, right
= 0;
147 area_t rectangle
= { 0, 0, 0, 0 };
149 rectangle
.width
= ctx
->width
;
150 rectangle
.height
= ctx
->height
;
152 if(ctx
->bg
.alpha
!= 0xffff)
154 xcb_get_property_reply_t
*prop_r
;
156 xcb_pixmap_t rootpix
;
157 xcb_get_property_cookie_t prop_c
;
158 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
159 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
161 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
164 && (data
= xcb_get_property_value(prop_r
))
165 && (rootpix
= *(xcb_pixmap_t
*) data
))
170 rootpix
, ctx
->pixmap
,
171 s
->width_in_pixels
, s
->height_in_pixels
,
172 ctx
->width
, ctx
->height
,
179 rootpix
, ctx
->pixmap
,
180 s
->width_in_pixels
, s
->height_in_pixels
,
181 ctx
->width
, ctx
->height
,
187 xcb_copy_area(globalconf
.connection
, rootpix
,
191 ctx
->width
, ctx
->height
);
198 /* compute geometry */
199 for(int i
= 0; i
< widgets
->len
; i
++)
200 if(widgets
->tab
[i
].widget
->align
== AlignLeft
&& widgets
->tab
[i
].widget
->isvisible
)
202 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
204 ctx
->width
- (left
+ right
));
205 widgets
->tab
[i
].geometry
.x
= left
;
206 left
+= widgets
->tab
[i
].geometry
.width
;
209 for(int i
= widgets
->len
- 1; i
>= 0; i
--)
210 if(widgets
->tab
[i
].widget
->align
== AlignRight
&& widgets
->tab
[i
].widget
->isvisible
)
212 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
214 ctx
->width
- (left
+ right
));
215 right
+= widgets
->tab
[i
].geometry
.width
;
216 widgets
->tab
[i
].geometry
.x
= ctx
->width
- right
;
219 /* save left value */
220 int fake_left
= left
;
222 /* compute width of flex aligned widgets that does not support it */
224 for(int i
= 0; i
< widgets
->len
; i
++)
225 if(widgets
->tab
[i
].widget
->align
== AlignFlex
226 && widgets
->tab
[i
].widget
->isvisible
)
228 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
)
231 fake_left
+= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
233 ctx
->width
- (fake_left
+ right
)).width
;
236 /* now compute everybody together! */
237 int flex_rendered
= 0;
238 for(int i
= 0; i
< widgets
->len
; i
++)
239 if(widgets
->tab
[i
].widget
->align
== AlignFlex
240 && widgets
->tab
[i
].widget
->isvisible
)
242 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
)
244 int width
= (ctx
->width
- (right
+ fake_left
)) / flex
;
245 /* give last pixels to last flex to be rendered */
246 if(flex_rendered
== flex
- 1)
247 width
+= (ctx
->width
- (right
+ fake_left
)) % flex
;
248 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
254 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
256 ctx
->width
- (left
+ right
));
257 widgets
->tab
[i
].geometry
.x
= left
;
258 left
+= widgets
->tab
[i
].geometry
.width
;
261 /* draw everything! */
262 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
264 for(int i
= 0; i
< widgets
->len
; i
++)
265 if(widgets
->tab
[i
].widget
->isvisible
)
267 widgets
->tab
[i
].geometry
.y
= 0;
268 widgets
->tab
[i
].widget
->draw(widgets
->tab
[i
].widget
,
269 ctx
, widgets
->tab
[i
].geometry
,
276 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
277 ctx
->width
, ctx
->height
,
278 ctx
->height
, ctx
->width
,
279 M_PI_2
, ctx
->height
, 0);
282 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
283 ctx
->width
, ctx
->height
,
284 ctx
->height
, ctx
->width
,
285 - M_PI_2
, 0, ctx
->width
);
292 /** Common function for creating a widget.
293 * \param widget The allocated widget.
296 widget_common_new(widget_t
*widget
)
298 widget
->button
= widget_common_button
;
299 widget
->align_supported
= AlignLeft
| AlignRight
;
302 /** Invalidate widgets which should be refresh upon
303 * external modifications. widget_t who watch flags will
304 * be set to be refreshed.
305 * \param screen Virtual screen number.
306 * \param flags Cache flags to invalidate.
309 widget_invalidate_cache(int screen
, int flags
)
311 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
313 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
315 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
316 if(wibox
->widgets
.tab
[j
].widget
->cache_flags
& flags
)
318 wibox
->need_update
= true;
324 /** Set a wibox needs update because it has widget, or redraw a titlebar.
325 * \todo Probably needs more optimization.
326 * \param widget The widget to look for.
329 widget_invalidate_bywidget(widget_t
*widget
)
331 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
332 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
334 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
335 if(!wibox
->need_update
)
336 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
337 if(wibox
->widgets
.tab
[j
].widget
== widget
)
339 wibox
->need_update
= true;
344 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
345 if(c
->titlebar
&& !c
->titlebar
->need_update
)
346 for(int j
= 0; j
< c
->titlebar
->widgets
.len
; j
++)
347 if(c
->titlebar
->widgets
.tab
[j
].widget
== widget
)
349 c
->titlebar
->need_update
= true;
354 /** Create a new widget.
355 * \param L The Lua VM state.
358 * \lparam A table with at least a name and a type value. Optional attributes
360 * \lreturn A brand new widget.
363 luaA_widget_new(lua_State
*L
)
365 const char *buf
, *type
;
367 widget_constructor_t
*wc
;
371 luaA_checktable(L
, 2);
373 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
374 align
= draw_align_fromstr(buf
, len
);
376 if(!(buf
= luaA_getopt_string(L
, 2, "name", NULL
)))
377 luaL_error(L
, "object widget must have a name");
379 type
= luaA_getopt_string(L
, 2, "type", NULL
);
381 if((wc
= name_func_lookup(type
, WidgetList
)))
384 luaL_error(L
, "unkown widget type: %s", type
);
388 /* Set visible by default. */
391 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
393 w
->name
= a_strdup(buf
);
395 return luaA_widget_userdata_new(L
, w
);
398 /** Get or set mouse buttons bindings to a widget.
399 * \param L The Lua VM state.
403 * \lparam An array of mouse button bindings objects, or nothing.
404 * \return The array of mouse button bindings objects of this widget.
407 luaA_widget_buttons(lua_State
*L
)
409 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
410 button_array_t
*buttons
= &(*widget
)->buttons
;
412 if(lua_gettop(L
) == 2)
414 luaA_button_array_set(L
, 2, buttons
);
418 return luaA_button_array_get(L
, buttons
);
422 * \param L The Lua VM state.
423 * \return The number of elements pushed on stack.
425 * \lfield visible The widget visibility.
426 * \lfield name The widget name.
427 * \lfield mouse_enter A function to execute when the mouse enter the widget.
428 * \lfield mouse_leave A function to execute when the mouse leave the widget.
431 luaA_widget_index(lua_State
*L
)
434 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
435 const char *buf
= luaL_checklstring(L
, 2, &len
);
436 awesome_token_t token
;
438 if(luaA_usemetatable(L
, 1, 2))
441 switch((token
= a_tokenize(buf
, len
)))
444 lua_pushboolean(L
, (*widget
)->isvisible
);
447 lua_pushstring(L
, (*widget
)->name
);
449 case A_TK_MOUSE_ENTER
:
450 if((*widget
)->mouse_enter
!= LUA_REFNIL
)
451 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
455 case A_TK_MOUSE_LEAVE
:
456 if((*widget
)->mouse_leave
!= LUA_REFNIL
)
457 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
465 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
468 /** Generic widget newindex.
469 * \param L The Lua VM state.
470 * \return The number of elements pushed on stack.
473 luaA_widget_newindex(lua_State
*L
)
476 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
477 const char *buf
= luaL_checklstring(L
, 2, &len
);
478 awesome_token_t token
;
480 switch((token
= a_tokenize(buf
, len
)))
483 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
485 case A_TK_MOUSE_ENTER
:
486 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
488 case A_TK_MOUSE_LEAVE
:
489 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
492 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
495 widget_invalidate_bywidget(*widget
);
500 const struct luaL_reg awesome_widget_methods
[] =
502 { "__call", luaA_widget_new
},
505 const struct luaL_reg awesome_widget_meta
[] =
507 { "buttons", luaA_widget_buttons
},
508 { "__index", luaA_widget_index
},
509 { "__newindex", luaA_widget_newindex
},
510 { "__gc", luaA_widget_gc
},
511 { "__eq", luaA_widget_eq
},
512 { "__tostring", luaA_widget_tostring
},
516 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80