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
;
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 wnode The list of widgets.
147 * \param ctx The draw context where to render.
148 * \param rotate_px The rotate pixmap: where to rotate and render the final
149 * pixmap when the object oritation is not east.
150 * \param screen The logical screen used to render.
151 * \param orientation The object orientation.
152 * \param x The x coordinates of the object.
153 * \param y The y coordinates of the object.
154 * \param wibox The wibox.
158 widget_render(widget_node_array_t
*widgets
, draw_context_t
*ctx
, xcb_gcontext_t gc
, xcb_pixmap_t rotate_px
,
159 int screen
, orientation_t orientation
,
160 int x
, int y
, wibox_t
*wibox
)
162 int left
= 0, right
= 0;
163 area_t rectangle
= { 0, 0, 0, 0 };
165 rectangle
.width
= ctx
->width
;
166 rectangle
.height
= ctx
->height
;
168 if(ctx
->bg
.alpha
!= 0xffff)
170 xcb_get_property_reply_t
*prop_r
;
172 xcb_pixmap_t rootpix
;
173 xcb_get_property_cookie_t prop_c
;
174 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
175 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
177 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
180 && (data
= xcb_get_property_value(prop_r
))
181 && (rootpix
= *(xcb_pixmap_t
*) data
))
186 rootpix
, ctx
->pixmap
,
187 s
->width_in_pixels
, s
->height_in_pixels
,
188 ctx
->width
, ctx
->height
,
195 rootpix
, ctx
->pixmap
,
196 s
->width_in_pixels
, s
->height_in_pixels
,
197 ctx
->width
, ctx
->height
,
203 xcb_copy_area(globalconf
.connection
, rootpix
,
207 ctx
->width
, ctx
->height
);
214 /* compute geometry */
215 for(int i
= 0; i
< widgets
->len
; i
++)
216 if(widgets
->tab
[i
].widget
->align
== AlignLeft
&& widgets
->tab
[i
].widget
->isvisible
)
218 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
220 ctx
->width
- (left
+ right
));
221 widgets
->tab
[i
].geometry
.x
= left
;
222 left
+= widgets
->tab
[i
].geometry
.width
;
225 for(int i
= widgets
->len
- 1; i
>= 0; i
--)
226 if(widgets
->tab
[i
].widget
->align
== AlignRight
&& widgets
->tab
[i
].widget
->isvisible
)
228 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
230 ctx
->width
- (left
+ right
));
231 right
+= widgets
->tab
[i
].geometry
.width
;
232 widgets
->tab
[i
].geometry
.x
= ctx
->width
- right
;
235 /* save left value */
236 int fake_left
= left
;
238 /* compute width of flex aligned widgets that does not support it */
240 for(int i
= 0; i
< widgets
->len
; i
++)
241 if(widgets
->tab
[i
].widget
->align
== AlignFlex
242 && widgets
->tab
[i
].widget
->isvisible
)
244 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
)
247 fake_left
+= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
249 ctx
->width
- (fake_left
+ right
)).width
;
252 /* now compute everybody together! */
253 int flex_rendered
= 0;
254 for(int i
= 0; i
< widgets
->len
; i
++)
255 if(widgets
->tab
[i
].widget
->align
== AlignFlex
256 && widgets
->tab
[i
].widget
->isvisible
)
258 if(widgets
->tab
[i
].widget
->align_supported
& AlignFlex
)
260 int width
= (ctx
->width
- (right
+ fake_left
)) / flex
;
261 /* give last pixels to last flex to be rendered */
262 if(flex_rendered
== flex
- 1)
263 width
+= (ctx
->width
- (right
+ fake_left
)) % flex
;
264 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
270 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
272 ctx
->width
- (left
+ right
));
273 widgets
->tab
[i
].geometry
.x
= left
;
274 left
+= widgets
->tab
[i
].geometry
.width
;
277 /* draw everything! */
278 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
280 for(int i
= 0; i
< widgets
->len
; i
++)
281 if(widgets
->tab
[i
].widget
->isvisible
)
283 widgets
->tab
[i
].geometry
.y
= 0;
284 widgets
->tab
[i
].widget
->draw(widgets
->tab
[i
].widget
,
285 ctx
, widgets
->tab
[i
].geometry
,
292 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
293 ctx
->width
, ctx
->height
,
294 ctx
->height
, ctx
->width
,
295 M_PI_2
, ctx
->height
, 0);
298 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
299 ctx
->width
, ctx
->height
,
300 ctx
->height
, ctx
->width
,
301 - M_PI_2
, 0, ctx
->width
);
308 /** Invalidate widgets which should be refresh depending on their types.
309 * \param screen Virtual screen number.
310 * \param type Widget type to invalidate.
313 widget_invalidate_bytype(int screen
, widget_constructor_t
*type
)
315 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
317 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
319 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
320 if(wibox
->widgets
.tab
[j
].widget
->type
== type
)
322 wibox
->need_update
= true;
328 /** Set a wibox needs update because it has widget, or redraw a titlebar.
329 * \todo Probably needs more optimization.
330 * \param widget The widget to look for.
333 widget_invalidate_bywidget(widget_t
*widget
)
335 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
336 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
338 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
339 if(!wibox
->need_update
)
340 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
341 if(wibox
->widgets
.tab
[j
].widget
== widget
)
343 wibox
->need_update
= true;
348 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
349 if(c
->titlebar
&& !c
->titlebar
->need_update
)
350 for(int j
= 0; j
< c
->titlebar
->widgets
.len
; j
++)
351 if(c
->titlebar
->widgets
.tab
[j
].widget
== widget
)
353 c
->titlebar
->need_update
= true;
358 /** Create a new widget.
359 * \param L The Lua VM state.
362 * \lparam A table with at least a type value. Optional attributes
364 * \lreturn A brand new widget.
367 luaA_widget_new(lua_State
*L
)
369 const char *align
, *type
;
371 widget_constructor_t
*wc
;
374 luaA_checktable(L
, 2);
376 type
= luaA_getopt_lstring(L
, 2, "type", NULL
, &len
);
378 if((wc
= name_func_lookup(type
, len
, WidgetList
)))
380 w
= p_new(widget_t
, 1);
385 luaA_warn(L
, "unkown widget type: %s", type
);
391 align
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
392 w
->align_supported
|= AlignLeft
| AlignRight
;
393 w
->align
= draw_align_fromstr(align
, len
);
395 /* Set visible by default. */
398 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
400 return luaA_widget_userdata_new(L
, w
);
403 /** Get or set mouse buttons bindings to a widget.
404 * \param L The Lua VM state.
408 * \lparam An array of mouse button bindings objects, or nothing.
409 * \return The array of mouse button bindings objects of this widget.
412 luaA_widget_buttons(lua_State
*L
)
414 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
415 button_array_t
*buttons
= &(*widget
)->buttons
;
417 if(lua_gettop(L
) == 2)
419 luaA_button_array_set(L
, 2, buttons
);
423 return luaA_button_array_get(L
, buttons
);
427 * \param L The Lua VM state.
428 * \return The number of elements pushed on stack.
430 * \lfield visible The widget visibility.
431 * \lfield mouse_enter A function to execute when the mouse enter the widget.
432 * \lfield mouse_leave A function to execute when the mouse leave the widget.
435 luaA_widget_index(lua_State
*L
)
438 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
439 const char *buf
= luaL_checklstring(L
, 2, &len
);
440 awesome_token_t token
;
442 if(luaA_usemetatable(L
, 1, 2))
445 switch((token
= a_tokenize(buf
, len
)))
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 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
486 case A_TK_MOUSE_ENTER
:
487 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
489 case A_TK_MOUSE_LEAVE
:
490 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
493 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
496 widget_invalidate_bywidget(*widget
);
501 const struct luaL_reg awesome_widget_methods
[] =
503 { "__call", luaA_widget_new
},
506 const struct luaL_reg awesome_widget_meta
[] =
508 { "buttons", luaA_widget_buttons
},
509 { "__index", luaA_widget_index
},
510 { "__newindex", luaA_widget_newindex
},
511 { "__gc", luaA_widget_gc
},
512 { "__eq", luaA_widget_eq
},
513 { "__tostring", luaA_widget_tostring
},
517 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80