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 /** Compute offset for drawing the first pixel of a widget.
41 * \param barwidth The wibox width.
42 * \param widgetwidth The widget width.
43 * \param alignment The widget alignment on wibox.
44 * \return The x coordinate to draw at.
47 widget_calculate_offset(int barwidth
, int widgetwidth
, int offset
, int alignment
)
55 return barwidth
- offset
- widgetwidth
;
58 /** Common function for button press event on widget.
59 * It will look into configuration to find the callback function to call.
60 * \param w The widget node.
61 * \param ev The button press event the widget received.
62 * \param screen The screen number.
63 * \param p The object where user clicked.
66 widget_common_button(widget_node_t
*w
,
67 xcb_button_press_event_t
*ev
,
68 int screen
__attribute__ ((unused
)),
71 button_array_t
*b
= &w
->widget
->buttons
;
73 for(int i
= 0; i
< b
->len
; i
++)
74 if(ev
->detail
== b
->tab
[i
]->button
75 && XUTIL_MASK_CLEAN(ev
->state
) == b
->tab
[i
]->mod
)
77 luaA_wibox_userdata_new(globalconf
.L
, p
);
78 luaA_dofunction(globalconf
.L
,
79 ev
->response_type
== XCB_BUTTON_PRESS
?
80 b
->tab
[i
]->press
: b
->tab
[i
]->release
,
85 /** Convert a Lua table to a list of widget nodet.
86 * \param L The Lua VM state.
87 * \param widgets The linked list of widget node.
90 luaA_table2widgets(lua_State
*L
, widget_node_array_t
*widgets
)
92 if(lua_istable(L
, -1))
95 while(luaA_next(L
, -2))
97 luaA_table2widgets(L
, widgets
);
98 lua_pop(L
, 1); /* remove value */
103 widget_t
**widget
= luaA_toudata(L
, -1, "widget");
108 w
.widget
= widget_ref(widget
);
109 widget_node_array_append(widgets
, w
);
114 /** Render a list of widgets.
115 * \param wnode The list of widgets.
116 * \param ctx The draw context where to render.
117 * \param rotate_px The rotate pixmap: where to rotate and render the final
118 * pixmap when the object oritation is not east.
119 * \param screen The logical screen used to render.
120 * \param orientation The object orientation.
121 * \param x The x coordinates of the object.
122 * \param y The y coordinates of the object.
123 * \param wibox The wibox.
127 widget_render(widget_node_array_t
*widgets
, draw_context_t
*ctx
, xcb_gcontext_t gc
, xcb_pixmap_t rotate_px
,
128 int screen
, orientation_t orientation
,
129 int x
, int y
, wibox_t
*wibox
)
131 int left
= 0, right
= 0;
132 area_t rectangle
= { 0, 0, 0, 0 };
134 rectangle
.width
= ctx
->width
;
135 rectangle
.height
= ctx
->height
;
137 if(ctx
->bg
.alpha
!= 0xffff)
139 xcb_get_property_reply_t
*prop_r
;
141 xcb_pixmap_t rootpix
;
142 xcb_get_property_cookie_t prop_c
;
143 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
144 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
146 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
149 && (data
= xcb_get_property_value(prop_r
))
150 && (rootpix
= *(xcb_pixmap_t
*) data
))
155 rootpix
, ctx
->pixmap
,
156 s
->width_in_pixels
, s
->height_in_pixels
,
157 ctx
->width
, ctx
->height
,
164 rootpix
, ctx
->pixmap
,
165 s
->width_in_pixels
, s
->height_in_pixels
,
166 ctx
->width
, ctx
->height
,
172 xcb_copy_area(globalconf
.connection
, rootpix
,
176 ctx
->width
, ctx
->height
);
183 /* compute geometry */
184 for(int i
= 0; i
< widgets
->len
; i
++)
185 if(widgets
->tab
[i
].widget
->align
== AlignLeft
&& widgets
->tab
[i
].widget
->isvisible
)
187 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
189 ctx
->width
- (left
+ right
));
190 widgets
->tab
[i
].geometry
.x
= left
;
191 left
+= widgets
->tab
[i
].geometry
.width
;
194 for(int i
= widgets
->len
- 1; i
>= 0; i
--)
195 if(widgets
->tab
[i
].widget
->align
== AlignRight
&& widgets
->tab
[i
].widget
->isvisible
)
197 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
199 ctx
->width
- (left
+ right
));
200 right
+= widgets
->tab
[i
].geometry
.width
;
201 widgets
->tab
[i
].geometry
.x
= ctx
->width
- right
;
205 for(int i
= 0; i
< widgets
->len
; i
++)
206 if(widgets
->tab
[i
].widget
->align
== AlignFlex
&& widgets
->tab
[i
].widget
->isvisible
)
211 int length
= (ctx
->width
- (left
+ right
)) / flex
;
213 for(int i
= 0; i
< widgets
->len
; i
++)
214 if(widgets
->tab
[i
].widget
->align
== AlignFlex
&& widgets
->tab
[i
].widget
->isvisible
)
216 widgets
->tab
[i
].geometry
= widgets
->tab
[i
].widget
->geometry(widgets
->tab
[i
].widget
,
219 widgets
->tab
[i
].geometry
.x
= left
;
220 left
+= widgets
->tab
[i
].geometry
.width
;
224 /* draw everything! */
225 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
227 for(int i
= 0; i
< widgets
->len
; i
++)
228 if(widgets
->tab
[i
].widget
->isvisible
)
230 widgets
->tab
[i
].geometry
.y
= 0;
231 widgets
->tab
[i
].widget
->draw(widgets
->tab
[i
].widget
,
232 ctx
, widgets
->tab
[i
].geometry
,
239 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
240 ctx
->width
, ctx
->height
,
241 ctx
->height
, ctx
->width
,
242 M_PI_2
, ctx
->height
, 0);
245 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
246 ctx
->width
, ctx
->height
,
247 ctx
->height
, ctx
->width
,
248 - M_PI_2
, 0, ctx
->width
);
256 /** Common function for creating a widget.
257 * \param widget The allocated widget.
260 widget_common_new(widget_t
*widget
)
262 widget
->align
= AlignLeft
;
263 widget
->button
= widget_common_button
;
266 /** Invalidate widgets which should be refresh upon
267 * external modifications. widget_t who watch flags will
268 * be set to be refreshed.
269 * \param screen Virtual screen number.
270 * \param flags Cache flags to invalidate.
273 widget_invalidate_cache(int screen
, int flags
)
275 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
277 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
279 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
280 if(wibox
->widgets
.tab
[j
].widget
->cache_flags
& flags
)
282 wibox
->need_update
= true;
288 /** Set a wibox needs update because it has widget, or redraw a titlebar.
289 * \todo Probably needs more optimization.
290 * \param widget The widget to look for.
293 widget_invalidate_bywidget(widget_t
*widget
)
295 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
296 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
298 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
299 if(!wibox
->need_update
)
300 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
301 if(wibox
->widgets
.tab
[j
].widget
== widget
)
303 wibox
->need_update
= true;
308 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
309 if(c
->titlebar
&& !c
->titlebar
->need_update
)
310 for(int j
= 0; j
< c
->titlebar
->widgets
.len
; j
++)
311 if(c
->titlebar
->widgets
.tab
[j
].widget
== widget
)
313 c
->titlebar
->need_update
= true;
318 /** Create a new widget.
319 * \param L The Lua VM state.
322 * \lparam A table with at least a name and a type value. Optional attributes
324 * \lreturn A brand new widget.
327 luaA_widget_new(lua_State
*L
)
329 const char *buf
, *type
;
331 widget_constructor_t
*wc
;
335 luaA_checktable(L
, 2);
337 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
338 align
= draw_align_fromstr(buf
, len
);
340 if(!(buf
= luaA_getopt_string(L
, 2, "name", NULL
)))
341 luaL_error(L
, "object widget must have a name");
343 type
= luaA_getopt_string(L
, 2, "type", NULL
);
345 if((wc
= name_func_lookup(type
, WidgetList
)))
348 luaL_error(L
, "unkown widget type: %s", type
);
352 /* Set visible by default. */
355 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
357 w
->name
= a_strdup(buf
);
359 return luaA_widget_userdata_new(L
, w
);
362 /** Get or set mouse buttons bindings to a widget.
363 * \param L The Lua VM state.
367 * \lparam An array of mouse button bindings objects, or nothing.
368 * \return The array of mouse button bindings objects of this widget.
371 luaA_widget_buttons(lua_State
*L
)
373 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
374 button_array_t
*buttons
= &(*widget
)->buttons
;
376 if(lua_gettop(L
) == 2)
377 luaA_button_array_set(L
, 2, buttons
);
379 return luaA_button_array_get(L
, buttons
);
382 /** Convert a widget into a printable string.
383 * \param L The Lua VM state.
389 luaA_widget_tostring(lua_State
*L
)
391 widget_t
**p
= luaA_checkudata(L
, 1, "widget");
392 lua_pushfstring(L
, "[widget udata(%p) name(%s)]", *p
, (*p
)->name
);
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
400 * \lfield visible The widget visibility.
401 * \lfield name The widget name.
402 * \lfield mouse_enter A function to execute when the mouse enter the widget.
403 * \lfield mouse_leave A function to execute when the mouse leave the widget.
406 luaA_widget_index(lua_State
*L
)
409 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
410 const char *buf
= luaL_checklstring(L
, 2, &len
);
411 awesome_token_t token
;
413 if(luaA_usemetatable(L
, 1, 2))
416 switch((token
= a_tokenize(buf
, len
)))
419 lua_pushboolean(L
, (*widget
)->isvisible
);
422 lua_pushstring(L
, (*widget
)->name
);
424 case A_TK_MOUSE_ENTER
:
425 if((*widget
)->mouse_enter
!= LUA_REFNIL
)
426 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
430 case A_TK_MOUSE_LEAVE
:
431 if((*widget
)->mouse_leave
!= LUA_REFNIL
)
432 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
440 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
443 /** Generic widget newindex.
444 * \param L The Lua VM state.
445 * \return The number of elements pushed on stack.
448 luaA_widget_newindex(lua_State
*L
)
451 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
452 const char *buf
= luaL_checklstring(L
, 2, &len
);
453 awesome_token_t token
;
455 switch((token
= a_tokenize(buf
, len
)))
458 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
460 case A_TK_MOUSE_ENTER
:
461 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
463 case A_TK_MOUSE_LEAVE
:
464 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
467 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
470 widget_invalidate_bywidget(*widget
);
475 const struct luaL_reg awesome_widget_methods
[] =
477 { "__call", luaA_widget_new
},
480 const struct luaL_reg awesome_widget_meta
[] =
482 { "buttons", luaA_widget_buttons
},
483 { "__index", luaA_widget_index
},
484 { "__newindex", luaA_widget_newindex
},
485 { "__gc", luaA_widget_gc
},
486 { "__eq", luaA_widget_eq
},
487 { "__tostring", luaA_widget_tostring
},
491 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80