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 xcb_pixmap_t rootpix
;
133 int left
= 0, right
= 0;
135 xcb_get_property_reply_t
*prop_r
;
136 xcb_get_property_cookie_t prop_c
;
137 area_t rectangle
= { 0, 0, 0, 0 };
139 rectangle
.width
= ctx
->width
;
140 rectangle
.height
= ctx
->height
;
142 if(ctx
->bg
.alpha
!= 0xffff)
144 s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
145 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
147 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
150 && (data
= xcb_get_property_value(prop_r
))
151 && (rootpix
= *(xcb_pixmap_t
*) data
))
156 rootpix
, ctx
->pixmap
,
157 s
->width_in_pixels
, s
->height_in_pixels
,
158 ctx
->width
, ctx
->height
,
165 rootpix
, ctx
->pixmap
,
166 s
->width_in_pixels
, s
->height_in_pixels
,
167 ctx
->width
, ctx
->height
,
173 xcb_copy_area(globalconf
.connection
, rootpix
,
177 ctx
->width
, ctx
->height
);
184 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
186 for(int i
= 0; i
< widgets
->len
; i
++)
187 if(widgets
->tab
[i
].widget
->align
== AlignLeft
&& widgets
->tab
[i
].widget
->isvisible
)
188 left
+= widgets
->tab
[i
].widget
->draw(ctx
, screen
, &widgets
->tab
[i
], left
, (left
+ right
), wibox
);
190 /* renders right widget from last to first */
191 for(int i
= widgets
->len
- 1; i
>= 0; i
--)
192 if(widgets
->tab
[i
].widget
->align
== AlignRight
&& widgets
->tab
[i
].widget
->isvisible
)
193 right
+= widgets
->tab
[i
].widget
->draw(ctx
, screen
, &widgets
->tab
[i
], right
, (left
+ right
), wibox
);
195 /* \todo rewrite this */
197 for(int i
= 0; i
< widgets
->len
; i
++)
198 if(widgets
->tab
[i
].widget
->align
== AlignFlex
&& widgets
->tab
[i
].widget
->isvisible
)
203 int length
= (ctx
->width
- (left
+ right
)) / flex
;
205 for(int i
= 0; i
< widgets
->len
; i
++)
206 if(widgets
->tab
[i
].widget
->align
== AlignFlex
&& widgets
->tab
[i
].widget
->isvisible
)
207 left
+= widgets
->tab
[i
].widget
->draw(ctx
, screen
, &widgets
->tab
[i
], left
, (left
+ right
) + length
* --flex
, wibox
);
213 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
214 ctx
->width
, ctx
->height
,
215 ctx
->height
, ctx
->width
,
216 M_PI_2
, ctx
->height
, 0);
219 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
220 ctx
->width
, ctx
->height
,
221 ctx
->height
, ctx
->width
,
222 - M_PI_2
, 0, ctx
->width
);
230 /** Common function for creating a widget.
231 * \param widget The allocated widget.
234 widget_common_new(widget_t
*widget
)
236 widget
->align
= AlignLeft
;
237 widget
->button
= widget_common_button
;
240 /** Invalidate widgets which should be refresh upon
241 * external modifications. widget_t who watch flags will
242 * be set to be refreshed.
243 * \param screen Virtual screen number.
244 * \param flags Cache flags to invalidate.
247 widget_invalidate_cache(int screen
, int flags
)
249 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
251 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
253 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
254 if(wibox
->widgets
.tab
[j
].widget
->cache_flags
& flags
)
256 wibox
->need_update
= true;
262 /** Set a wibox needs update because it has widget, or redraw a titlebar.
263 * \todo Probably needs more optimization.
264 * \param widget The widget to look for.
267 widget_invalidate_bywidget(widget_t
*widget
)
269 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
270 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
272 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
273 if(!wibox
->need_update
)
274 for(int j
= 0; j
< wibox
->widgets
.len
; j
++)
275 if(wibox
->widgets
.tab
[j
].widget
== widget
)
277 wibox
->need_update
= true;
282 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
283 if(c
->titlebar
&& !c
->titlebar
->need_update
)
284 for(int j
= 0; j
< c
->titlebar
->widgets
.len
; j
++)
285 if(c
->titlebar
->widgets
.tab
[j
].widget
== widget
)
287 c
->titlebar
->need_update
= true;
292 /** Create a new widget.
293 * \param L The Lua VM state.
296 * \lparam A table with at least a name and a type value. Optional attributes
298 * \lreturn A brand new widget.
301 luaA_widget_new(lua_State
*L
)
303 const char *buf
, *type
;
305 widget_constructor_t
*wc
;
309 luaA_checktable(L
, 2);
311 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
312 align
= draw_align_fromstr(buf
, len
);
314 if(!(buf
= luaA_getopt_string(L
, 2, "name", NULL
)))
315 luaL_error(L
, "object widget must have a name");
317 type
= luaA_getopt_string(L
, 2, "type", NULL
);
319 if((wc
= name_func_lookup(type
, WidgetList
)))
322 luaL_error(L
, "unkown widget type: %s", type
);
326 /* Set visible by default. */
329 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
331 w
->name
= a_strdup(buf
);
333 return luaA_widget_userdata_new(L
, w
);
336 /** Get or set mouse buttons bindings to a widget.
337 * \param L The Lua VM state.
341 * \lparam An array of mouse button bindings objects, or nothing.
342 * \return The array of mouse button bindings objects of this widget.
345 luaA_widget_buttons(lua_State
*L
)
347 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
348 button_array_t
*buttons
= &(*widget
)->buttons
;
350 if(lua_gettop(L
) == 2)
351 luaA_button_array_set(L
, 2, buttons
);
353 return luaA_button_array_get(L
, buttons
);
356 /** Convert a widget into a printable string.
357 * \param L The Lua VM state.
363 luaA_widget_tostring(lua_State
*L
)
365 widget_t
**p
= luaA_checkudata(L
, 1, "widget");
366 lua_pushfstring(L
, "[widget udata(%p) name(%s)]", *p
, (*p
)->name
);
371 * \param L The Lua VM state.
372 * \return The number of elements pushed on stack.
374 * \lfield visible The widget visibility.
375 * \lfield name The widget name.
376 * \lfield mouse_enter A function to execute when the mouse enter the widget.
377 * \lfield mouse_leave A function to execute when the mouse leave the widget.
380 luaA_widget_index(lua_State
*L
)
383 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
384 const char *buf
= luaL_checklstring(L
, 2, &len
);
385 awesome_token_t token
;
387 if(luaA_usemetatable(L
, 1, 2))
390 switch((token
= a_tokenize(buf
, len
)))
393 lua_pushboolean(L
, (*widget
)->isvisible
);
396 lua_pushstring(L
, (*widget
)->name
);
398 case A_TK_MOUSE_ENTER
:
399 if((*widget
)->mouse_enter
!= LUA_REFNIL
)
400 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
404 case A_TK_MOUSE_LEAVE
:
405 if((*widget
)->mouse_leave
!= LUA_REFNIL
)
406 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
414 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
417 /** Generic widget newindex.
418 * \param L The Lua VM state.
419 * \return The number of elements pushed on stack.
422 luaA_widget_newindex(lua_State
*L
)
425 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
426 const char *buf
= luaL_checklstring(L
, 2, &len
);
427 awesome_token_t token
;
429 switch((token
= a_tokenize(buf
, len
)))
432 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
434 case A_TK_MOUSE_ENTER
:
435 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
437 case A_TK_MOUSE_LEAVE
:
438 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
441 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
444 widget_invalidate_bywidget(*widget
);
449 const struct luaL_reg awesome_widget_methods
[] =
451 { "__call", luaA_widget_new
},
454 const struct luaL_reg awesome_widget_meta
[] =
456 { "buttons", luaA_widget_buttons
},
457 { "__index", luaA_widget_index
},
458 { "__newindex", luaA_widget_newindex
},
459 { "__gc", luaA_widget_gc
},
460 { "__eq", luaA_widget_eq
},
461 { "__tostring", luaA_widget_tostring
},
465 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80