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
;
74 /** Get a widget node from a wibox by coords.
75 * \param Container position.
76 * \param widgets The widget list.
77 * \param width The container width.
78 * \param height The container height.
79 * \param x X coordinate of the widget.
80 * \param y Y coordinate of the widget.
84 widget_getbycoords(position_t position
, widget_node_array_t
*widgets
,
85 int width
, int height
, int16_t *x
, int16_t *y
)
89 /* Need to transform coordinates like it was top/bottom */
105 for(int i
= 0; i
< widgets
->len
; i
++)
107 widget_node_t
*w
= &widgets
->tab
[i
];
108 if(w
->widget
->isvisible
&&
109 *x
>= w
->geometry
.x
&& *x
< w
->geometry
.x
+ w
->geometry
.width
110 && *y
>= w
->geometry
.y
&& *y
< w
->geometry
.y
+ w
->geometry
.height
)
117 /** Convert a Lua table to a list of widget nodet.
118 * \param L The Lua VM state.
119 * \param widgets The linked list of widget node.
122 luaA_table2widgets(lua_State
*L
, widget_node_array_t
*widgets
)
124 if(lua_istable(L
, -1))
127 while(luaA_next(L
, -2))
129 luaA_table2widgets(L
, widgets
);
130 lua_pop(L
, 1); /* remove value */
135 widget_t
**widget
= luaA_toudata(L
, -1, "widget");
140 w
.widget
= widget_ref(widget
);
141 widget_node_array_append(widgets
, w
);
146 /** Render a list of widgets.
147 * \param wnode The list of widgets.
148 * \param ctx The draw context where to render.
149 * \param rotate_px The rotate pixmap: where to rotate and render the final
150 * pixmap when the object oritation is not east.
151 * \param screen The logical screen used to render.
152 * \param orientation The object orientation.
153 * \param x The x coordinates of the object.
154 * \param y The y coordinates of the object.
155 * \param wibox The wibox.
159 widget_render(widget_node_array_t
*widgets
, draw_context_t
*ctx
, xcb_gcontext_t gc
, xcb_pixmap_t rotate_px
,
160 int screen
, orientation_t orientation
,
161 int x
, int y
, wibox_t
*wibox
)
163 int left
= 0, right
= 0;
164 area_t rectangle
= { 0, 0, 0, 0 };
166 rectangle
.width
= ctx
->width
;
167 rectangle
.height
= ctx
->height
;
169 if(ctx
->bg
.alpha
!= 0xffff)
171 xcb_get_property_reply_t
*prop_r
;
173 xcb_pixmap_t rootpix
;
174 xcb_get_property_cookie_t prop_c
;
175 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
176 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
178 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
181 && (data
= xcb_get_property_value(prop_r
))
182 && (rootpix
= *(xcb_pixmap_t
*) data
))
187 rootpix
, ctx
->pixmap
,
188 s
->width_in_pixels
, s
->height_in_pixels
,
189 ctx
->width
, ctx
->height
,
196 rootpix
, ctx
->pixmap
,
197 s
->width_in_pixels
, s
->height_in_pixels
,
198 ctx
->width
, ctx
->height
,
204 xcb_copy_area(globalconf
.connection
, rootpix
,
208 ctx
->width
, ctx
->height
);
215 /* compute geometry */
216 for(int i
= 0; i
< widgets
->len
; i
++)
217 if(widgets
->tab
[i
].widget
->align
== AlignLeft
&& widgets
->tab
[i
].widget
->isvisible
)
219 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
221 ctx
->width
- (left
+ right
));
222 widgets
->tab
[i
].geometry
.x
= left
;
223 left
+= widgets
->tab
[i
].geometry
.width
;
226 for(int i
= widgets
->len
- 1; i
>= 0; i
--)
227 if(widgets
->tab
[i
].widget
->align
== AlignRight
&& widgets
->tab
[i
].widget
->isvisible
)
229 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
231 ctx
->width
- (left
+ right
));
232 right
+= widgets
->tab
[i
].geometry
.width
;
233 widgets
->tab
[i
].geometry
.x
= ctx
->width
- right
;
236 /* save left value */
237 int fake_left
= left
;
239 /* compute width of flex or fixed aligned widgets */
241 for(int i
= 0; i
< widgets
->len
; i
++)
242 if(widgets
->tab
[i
].widget
->align
& (AlignFlex
| AlignFixed
)
243 && widgets
->tab
[i
].widget
->isvisible
)
245 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
246 && widgets
->tab
[i
].widget
->align
== AlignFlex
)
249 fake_left
+= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
251 ctx
->width
- (fake_left
+ right
)).width
;
254 /* now compute everybody together! */
255 int flex_rendered
= 0;
256 for(int i
= 0; i
< widgets
->len
; i
++)
257 if(widgets
->tab
[i
].widget
->align
& (AlignFlex
| AlignFixed
)
258 && widgets
->tab
[i
].widget
->isvisible
)
260 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
261 && widgets
->tab
[i
].widget
->align
== AlignFlex
)
263 int width
= (ctx
->width
- (right
+ fake_left
)) / flex
;
264 /* give last pixels to last flex to be rendered */
265 if(flex_rendered
== flex
- 1)
266 width
+= (ctx
->width
- (right
+ fake_left
)) % flex
;
267 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
273 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
275 ctx
->width
- (left
+ right
));
276 widgets
->tab
[i
].geometry
.x
= left
;
277 left
+= widgets
->tab
[i
].geometry
.width
;
280 /* draw everything! */
281 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
283 for(int i
= 0; i
< widgets
->len
; i
++)
284 if(widgets
->tab
[i
].widget
->isvisible
)
286 widgets
->tab
[i
].geometry
.y
= 0;
287 widgets
->tab
[i
].widget
->draw(widgets
->tab
[i
].widget
,
288 ctx
, widgets
->tab
[i
].geometry
,
295 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
296 ctx
->width
, ctx
->height
,
297 ctx
->height
, ctx
->width
,
298 M_PI_2
, ctx
->height
, 0);
301 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
302 ctx
->width
, ctx
->height
,
303 ctx
->height
, ctx
->width
,
304 - M_PI_2
, 0, ctx
->width
);
311 /** Invalidate widgets which should be refresh depending on their types.
312 * \param screen Virtual screen number.
313 * \param type Widget type to invalidate.
316 widget_invalidate_bytype(int screen
, widget_constructor_t
*type
)
318 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
320 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
322 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
323 if(wibox
->widgets
.tab
[j
].widget
->type
== type
)
325 wibox
->need_update
= true;
331 /** Set a wibox needs update because it has widget, or redraw a titlebar.
332 * \todo Probably needs more optimization.
333 * \param widget The widget to look for.
336 widget_invalidate_bywidget(widget_t
*widget
)
338 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
339 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
341 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
342 if(!wibox
->need_update
)
343 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
344 if(wibox
->widgets
.tab
[j
].widget
== widget
)
346 wibox
->need_update
= true;
351 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
352 if(c
->titlebar
&& !c
->titlebar
->need_update
)
353 for(int j
= 0; j
< c
->titlebar
->widgets
.len
; j
++)
354 if(c
->titlebar
->widgets
.tab
[j
].widget
== widget
)
356 c
->titlebar
->need_update
= true;
361 /** Create a new widget.
362 * \param L The Lua VM state.
365 * \lparam A table with at least a type value. Optional attributes
367 * \lreturn A brand new widget.
370 luaA_widget_new(lua_State
*L
)
372 const char *align
, *type
;
374 widget_constructor_t
*wc
;
377 luaA_checktable(L
, 2);
379 type
= luaA_getopt_lstring(L
, 2, "type", NULL
, &len
);
381 if((wc
= name_func_lookup(type
, len
, WidgetList
)))
383 w
= p_new(widget_t
, 1);
388 luaA_warn(L
, "unkown widget type: %s", type
);
394 align
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
395 w
->align_supported
|= AlignLeft
| AlignRight
| AlignFixed
;
396 w
->align
= draw_align_fromstr(align
, len
);
398 /* Set visible by default. */
401 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
403 return luaA_widget_userdata_new(L
, w
);
406 /** Get or set mouse buttons bindings to a widget.
407 * \param L The Lua VM state.
411 * \lparam An array of mouse button bindings objects, or nothing.
412 * \return The array of mouse button bindings objects of this widget.
415 luaA_widget_buttons(lua_State
*L
)
417 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
418 button_array_t
*buttons
= &(*widget
)->buttons
;
420 if(lua_gettop(L
) == 2)
422 luaA_button_array_set(L
, 2, buttons
);
426 return luaA_button_array_get(L
, buttons
);
430 * \param L The Lua VM state.
431 * \return The number of elements pushed on stack.
433 * \lfield visible The widget visibility.
434 * \lfield mouse_enter A function to execute when the mouse enter the widget.
435 * \lfield mouse_leave A function to execute when the mouse leave the widget.
438 luaA_widget_index(lua_State
*L
)
441 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
442 const char *buf
= luaL_checklstring(L
, 2, &len
);
443 awesome_token_t token
;
445 if(luaA_usemetatable(L
, 1, 2))
448 switch((token
= a_tokenize(buf
, len
)))
451 lua_pushboolean(L
, (*widget
)->isvisible
);
453 case A_TK_MOUSE_ENTER
:
454 if((*widget
)->mouse_enter
!= LUA_REFNIL
)
455 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
459 case A_TK_MOUSE_LEAVE
:
460 if((*widget
)->mouse_leave
!= LUA_REFNIL
)
461 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
469 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
472 /** Generic widget newindex.
473 * \param L The Lua VM state.
474 * \return The number of elements pushed on stack.
477 luaA_widget_newindex(lua_State
*L
)
480 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
481 const char *buf
= luaL_checklstring(L
, 2, &len
);
482 awesome_token_t token
;
484 switch((token
= a_tokenize(buf
, len
)))
487 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
489 case A_TK_MOUSE_ENTER
:
490 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
492 case A_TK_MOUSE_LEAVE
:
493 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
496 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
499 widget_invalidate_bywidget(*widget
);
504 const struct luaL_reg awesome_widget_methods
[] =
506 { "__call", luaA_widget_new
},
509 const struct luaL_reg awesome_widget_meta
[] =
511 { "buttons", luaA_widget_buttons
},
512 { "__index", luaA_widget_index
},
513 { "__newindex", luaA_widget_newindex
},
514 { "__gc", luaA_widget_gc
},
515 { "__eq", luaA_widget_eq
},
516 { "__tostring", luaA_widget_tostring
},
520 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80