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
)
70 return barwidth
- offset
- widgetwidth
;
73 /** Get a widget node from a wibox by coords.
74 * \param Container position.
75 * \param widgets The widget list.
76 * \param width The container width.
77 * \param height The container height.
78 * \param x X coordinate of the widget.
79 * \param y Y coordinate of the widget.
83 widget_getbycoords(position_t position
, widget_node_array_t
*widgets
,
84 int width
, int height
, int16_t *x
, int16_t *y
)
88 /* Need to transform coordinates like it was top/bottom */
104 for(int i
= 0; i
< widgets
->len
; i
++)
106 widget_node_t
*w
= &widgets
->tab
[i
];
107 if(w
->widget
->isvisible
&&
108 *x
>= w
->geometry
.x
&& *x
< w
->geometry
.x
+ w
->geometry
.width
109 && *y
>= w
->geometry
.y
&& *y
< w
->geometry
.y
+ w
->geometry
.height
)
116 /** Convert a Lua table to a list of widget nodet.
117 * \param L The Lua VM state.
118 * \param widgets The linked list of widget node.
121 luaA_table2widgets(lua_State
*L
, widget_node_array_t
*widgets
)
123 if(lua_istable(L
, -1))
126 while(luaA_next(L
, -2))
128 luaA_table2widgets(L
, widgets
);
129 lua_pop(L
, 1); /* remove value */
134 widget_t
**widget
= luaA_toudata(L
, -1, "widget");
139 w
.widget
= widget_ref(widget
);
140 widget_node_array_append(widgets
, w
);
145 /** Render a list of widgets.
146 * \param wibox The wibox.
150 widget_render(wibox_t
*wibox
)
152 draw_context_t
*ctx
= &wibox
->sw
.ctx
;
153 int left
= 0, right
= 0;
154 area_t rectangle
= { 0, 0, 0, 0 };
156 rectangle
.width
= ctx
->width
;
157 rectangle
.height
= ctx
->height
;
159 if(ctx
->bg
.alpha
!= 0xffff)
161 int x
= wibox
->sw
.geometry
.x
, y
= wibox
->sw
.geometry
.y
;
162 xcb_get_property_reply_t
*prop_r
;
164 xcb_pixmap_t rootpix
;
165 xcb_get_property_cookie_t prop_c
;
166 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
167 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
169 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
172 && (data
= xcb_get_property_value(prop_r
))
173 && (rootpix
= *(xcb_pixmap_t
*) data
))
174 switch(wibox
->sw
.orientation
)
178 rootpix
, ctx
->pixmap
,
179 s
->width_in_pixels
, s
->height_in_pixels
,
180 ctx
->width
, ctx
->height
,
187 rootpix
, ctx
->pixmap
,
188 s
->width_in_pixels
, s
->height_in_pixels
,
189 ctx
->width
, ctx
->height
,
195 xcb_copy_area(globalconf
.connection
, rootpix
,
196 wibox
->sw
.pixmap
, wibox
->sw
.gc
,
199 ctx
->width
, ctx
->height
);
206 widget_node_array_t
*widgets
= &wibox
->widgets
;
208 /* compute geometry */
209 for(int i
= 0; i
< widgets
->len
; i
++)
210 if(widgets
->tab
[i
].widget
->align
== AlignLeft
&& widgets
->tab
[i
].widget
->isvisible
)
212 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
213 wibox
->screen
, ctx
->height
,
214 ctx
->width
- (left
+ right
));
215 widgets
->tab
[i
].geometry
.x
= left
;
216 left
+= widgets
->tab
[i
].geometry
.width
;
219 for(int i
= widgets
->len
- 1; i
>= 0; i
--)
220 if(widgets
->tab
[i
].widget
->align
== AlignRight
&& widgets
->tab
[i
].widget
->isvisible
)
222 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
223 wibox
->screen
, ctx
->height
,
224 ctx
->width
- (left
+ right
));
225 right
+= widgets
->tab
[i
].geometry
.width
;
226 widgets
->tab
[i
].geometry
.x
= ctx
->width
- right
;
229 /* save left value */
230 int fake_left
= left
;
232 /* compute width of flex or fixed aligned widgets */
234 for(int i
= 0; i
< widgets
->len
; i
++)
235 if(widgets
->tab
[i
].widget
->align
& (AlignFlex
| AlignFixed
)
236 && widgets
->tab
[i
].widget
->isvisible
)
238 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
239 && widgets
->tab
[i
].widget
->align
== AlignFlex
)
242 fake_left
+= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
243 wibox
->screen
, ctx
->height
,
244 ctx
->width
- (fake_left
+ right
)).width
;
247 /* now compute everybody together! */
248 int flex_rendered
= 0;
249 for(int i
= 0; i
< widgets
->len
; i
++)
250 if(widgets
->tab
[i
].widget
->align
& (AlignFlex
| AlignFixed
)
251 && widgets
->tab
[i
].widget
->isvisible
)
253 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
254 && widgets
->tab
[i
].widget
->align
== AlignFlex
)
256 int width
= (ctx
->width
- (right
+ fake_left
)) / flex
;
257 /* give last pixels to last flex to be rendered */
258 if(flex_rendered
== flex
- 1)
259 width
+= (ctx
->width
- (right
+ fake_left
)) % flex
;
260 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
261 wibox
->screen
, ctx
->height
,
266 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
267 wibox
->screen
, ctx
->height
,
268 ctx
->width
- (left
+ right
));
269 widgets
->tab
[i
].geometry
.x
= left
;
270 left
+= widgets
->tab
[i
].geometry
.width
;
273 /* draw everything! */
274 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
276 for(int i
= 0; i
< widgets
->len
; i
++)
277 if(widgets
->tab
[i
].widget
->isvisible
)
279 widgets
->tab
[i
].geometry
.y
= 0;
280 widgets
->tab
[i
].widget
->draw(widgets
->tab
[i
].widget
,
281 ctx
, widgets
->tab
[i
].geometry
,
282 wibox
->screen
, wibox
);
285 switch(wibox
->sw
.orientation
)
288 draw_rotate(ctx
, ctx
->pixmap
, wibox
->sw
.pixmap
,
289 ctx
->width
, ctx
->height
,
290 ctx
->height
, ctx
->width
,
291 M_PI_2
, ctx
->height
, 0);
294 draw_rotate(ctx
, ctx
->pixmap
, wibox
->sw
.pixmap
,
295 ctx
->width
, ctx
->height
,
296 ctx
->height
, ctx
->width
,
297 - M_PI_2
, 0, ctx
->width
);
304 /** Invalidate widgets which should be refresh depending on their types.
305 * \param screen Virtual screen number.
306 * \param type Widget type to invalidate.
309 widget_invalidate_bytype(int screen
, widget_constructor_t
*type
)
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
->type
== type
)
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 type value. Optional attributes
360 * \lreturn A brand new widget.
363 luaA_widget_new(lua_State
*L
)
365 const char *align
, *type
;
367 widget_constructor_t
*wc
;
370 luaA_checktable(L
, 2);
372 type
= luaA_getopt_lstring(L
, 2, "type", NULL
, &len
);
374 if((wc
= name_func_lookup(type
, len
, WidgetList
)))
376 w
= p_new(widget_t
, 1);
381 luaA_warn(L
, "unkown widget type: %s", type
);
387 align
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
388 w
->align_supported
|= AlignLeft
| AlignRight
| AlignFixed
;
389 w
->align
= draw_align_fromstr(align
, len
);
391 /* Set visible by default. */
394 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
396 return luaA_widget_userdata_new(L
, w
);
399 /** Get or set mouse buttons bindings to a widget.
400 * \param L The Lua VM state.
404 * \lparam An array of mouse button bindings objects, or nothing.
405 * \return The array of mouse button bindings objects of this widget.
408 luaA_widget_buttons(lua_State
*L
)
410 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
411 button_array_t
*buttons
= &(*widget
)->buttons
;
413 if(lua_gettop(L
) == 2)
415 luaA_button_array_set(L
, 2, buttons
);
419 return luaA_button_array_get(L
, buttons
);
423 * \param L The Lua VM state.
424 * \return The number of elements pushed on stack.
426 * \lfield align The widget alignment.
427 * \lfield visible The widget visibility.
428 * \lfield mouse_enter A function to execute when the mouse enter the widget.
429 * \lfield mouse_leave A function to execute when the mouse leave the widget.
432 luaA_widget_index(lua_State
*L
)
435 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
436 const char *buf
= luaL_checklstring(L
, 2, &len
);
437 awesome_token_t token
;
439 if(luaA_usemetatable(L
, 1, 2))
442 switch((token
= a_tokenize(buf
, len
)))
445 lua_pushstring(L
, draw_align_tostr((*widget
)->align
));
448 lua_pushboolean(L
, (*widget
)->isvisible
);
450 case A_TK_MOUSE_ENTER
:
451 if((*widget
)->mouse_enter
!= LUA_REFNIL
)
452 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
456 case A_TK_MOUSE_LEAVE
:
457 if((*widget
)->mouse_leave
!= LUA_REFNIL
)
458 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
466 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
469 /** Generic widget newindex.
470 * \param L The Lua VM state.
471 * \return The number of elements pushed on stack.
474 luaA_widget_newindex(lua_State
*L
)
477 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
478 const char *buf
= luaL_checklstring(L
, 2, &len
);
479 awesome_token_t token
;
481 switch((token
= a_tokenize(buf
, len
)))
484 buf
= luaL_checklstring(L
, 3, &len
);
485 (*widget
)->align
= draw_align_fromstr(buf
, len
);
488 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
490 case A_TK_MOUSE_ENTER
:
491 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
493 case A_TK_MOUSE_LEAVE
:
494 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
497 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
500 widget_invalidate_bywidget(*widget
);
505 const struct luaL_reg awesome_widget_methods
[] =
507 { "__call", luaA_widget_new
},
510 const struct luaL_reg awesome_widget_meta
[] =
512 { "buttons", luaA_widget_buttons
},
513 { "__index", luaA_widget_index
},
514 { "__newindex", luaA_widget_newindex
},
515 { "__gc", luaA_widget_gc
},
516 { "__eq", luaA_widget_eq
},
517 { "__tostring", luaA_widget_tostring
},
521 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80