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 statusbar width.
42 * \param widgetwidth The widget width.
43 * \param alignment The widget alignment on statusbar.
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.
64 * \param type The object type.
67 widget_common_button(widget_node_t
*w
,
68 xcb_button_press_event_t
*ev
,
69 int screen
__attribute__ ((unused
)),
73 button_array_t
*b
= &w
->widget
->buttons
;
75 for(int i
= 0; i
< b
->len
; i
++)
76 if(ev
->detail
== b
->tab
[i
]->button
77 && XUTIL_MASK_CLEAN(ev
->state
) == b
->tab
[i
]->mod
)
79 luaA_pushpointer(globalconf
.L
, p
, type
);
80 luaA_dofunction(globalconf
.L
,
81 ev
->response_type
== XCB_BUTTON_PRESS
?
82 b
->tab
[i
]->press
: b
->tab
[i
]->release
,
87 /** Render a list of widgets.
88 * \param wnode The list of widgets.
89 * \param ctx The draw context where to render.
90 * \param rotate_px The rotate pixmap: where to rotate and render the final
91 * \param screen The logical screen used to render.
92 * \param position The object position.
93 * \param x The x coordinates of the object.
94 * \param y The y coordinates of the object.
95 * pixmap when the object position is right or left.
96 * \param object The object pointer.
97 * \param type The object type.
101 widget_render(widget_node_t
*wnode
, draw_context_t
*ctx
, xcb_gcontext_t gc
, xcb_pixmap_t rotate_px
,
102 int screen
, position_t position
,
103 int x
, int y
, void *object
, awesome_type_t type
)
105 xcb_pixmap_t rootpix
;
108 int left
= 0, right
= 0;
110 xcb_get_property_reply_t
*prop_r
;
111 xcb_get_property_cookie_t prop_c
;
112 area_t rectangle
= { 0, 0, 0, 0 };
114 rectangle
.width
= ctx
->width
;
115 rectangle
.height
= ctx
->height
;
117 if(ctx
->bg
.alpha
!= 0xffff)
119 s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
120 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
122 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
125 && (data
= xcb_get_property_value(prop_r
))
126 && (rootpix
= *(xcb_pixmap_t
*) data
))
131 rootpix
, ctx
->pixmap
,
132 s
->width_in_pixels
, s
->height_in_pixels
,
133 ctx
->width
, ctx
->height
,
140 rootpix
, ctx
->pixmap
,
141 s
->width_in_pixels
, s
->height_in_pixels
,
142 ctx
->width
, ctx
->height
,
148 xcb_copy_area(globalconf
.connection
, rootpix
,
152 ctx
->width
, ctx
->height
);
159 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
161 for(w
= wnode
; w
; w
= w
->next
)
162 if(w
->widget
->align
== AlignLeft
&& w
->widget
->isvisible
)
163 left
+= w
->widget
->draw(ctx
, screen
, w
, left
, (left
+ right
), object
, type
);
165 /* renders right widget from last to first */
166 for(w
= *widget_node_list_last(&wnode
); w
; w
= w
->prev
)
167 if(w
->widget
->align
== AlignRight
&& w
->widget
->isvisible
)
168 right
+= w
->widget
->draw(ctx
, screen
, w
, right
, (left
+ right
), object
, type
);
170 for(w
= wnode
; w
; w
= w
->next
)
171 if(w
->widget
->align
== AlignFlex
&& w
->widget
->isvisible
)
172 left
+= w
->widget
->draw(ctx
, screen
, w
, left
, (left
+ right
), object
, type
);
177 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
178 ctx
->width
, ctx
->height
,
179 ctx
->height
, ctx
->width
,
180 M_PI_2
, ctx
->height
, 0);
183 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
184 ctx
->width
, ctx
->height
,
185 ctx
->height
, ctx
->width
,
186 - M_PI_2
, 0, ctx
->width
);
194 /** Common function for creating a widget.
195 * \param widget The allocated widget.
198 widget_common_new(widget_t
*widget
)
200 widget
->align
= AlignLeft
;
201 widget
->button
= widget_common_button
;
204 /** Invalidate widgets which should be refresh upon
205 * external modifications. widget_t who watch flags will
206 * be set to be refreshed.
207 * \param screen Virtual screen number.
208 * \param flags Cache flags to invalidate.
211 widget_invalidate_cache(int screen
, int flags
)
213 for(int i
= 0; i
< globalconf
.screens
[screen
].statusbars
.len
; i
++)
215 statusbar_t
*statusbar
= globalconf
.screens
[screen
].statusbars
.tab
[i
];
216 widget_node_t
*widget
;
218 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
219 if(widget
->widget
->cache_flags
& flags
)
221 statusbar
->need_update
= true;
227 /** Set a statusbar needs update because it has widget, or redraw a titlebar.
228 * \todo Probably needs more optimization.
229 * \param widget The widget to look for.
232 widget_invalidate_bywidget(widget_t
*widget
)
235 widget_node_t
*witer
;
238 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
239 for(int i
= 0; i
< globalconf
.screens
[screen
].statusbars
.len
; i
++)
241 statusbar_t
*statusbar
= globalconf
.screens
[screen
].statusbars
.tab
[i
];
243 if(!statusbar
->need_update
)
244 for(witer
= statusbar
->widgets
; witer
; witer
= witer
->next
)
245 if(witer
->widget
== widget
)
247 statusbar
->need_update
= true;
252 for(c
= globalconf
.clients
; c
; c
= c
->next
)
253 if(c
->titlebar
&& !c
->titlebar
->need_update
)
254 for(witer
= c
->titlebar
->widgets
; witer
; witer
= witer
->next
)
255 if(witer
->widget
== widget
)
256 c
->titlebar
->need_update
= true;
259 /** Create a new widget.
260 * \param L The Lua VM state.
263 * \lparam A table with at least a name and a type value. Optional attributes
265 * \lreturn A brand new widget.
268 luaA_widget_new(lua_State
*L
)
270 const char *buf
, *type
;
272 widget_constructor_t
*wc
;
276 luaA_checktable(L
, 2);
278 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
279 align
= draw_align_fromstr(buf
, len
);
281 if(!(buf
= luaA_getopt_string(L
, 2, "name", NULL
)))
282 luaL_error(L
, "object widget must have a name");
284 type
= luaA_getopt_string(L
, 2, "type", NULL
);
286 if((wc
= name_func_lookup(type
, WidgetList
)))
289 luaL_error(L
, "unkown widget type: %s", type
);
293 /* Set visible by default. */
296 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
298 w
->name
= a_strdup(buf
);
300 return luaA_widget_userdata_new(L
, w
);
303 /** Get or set mouse buttons bindings to a widget.
304 * \param L The Lua VM state.
308 * \lparam An array of mouse button bindings objects, or nothing.
309 * \return The array of mouse button bindings objects of this widget.
312 luaA_widget_buttons(lua_State
*L
)
314 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
315 button_array_t
*buttons
= &(*widget
)->buttons
;
317 if(lua_gettop(L
) == 2)
318 luaA_button_array_set(L
, 2, buttons
);
320 return luaA_button_array_get(L
, buttons
);
323 /** Convert a widget into a printable string.
324 * \param L The Lua VM state.
330 luaA_widget_tostring(lua_State
*L
)
332 widget_t
**p
= luaA_checkudata(L
, 1, "widget");
333 lua_pushfstring(L
, "[widget udata(%p) name(%s)]", *p
, (*p
)->name
);
338 * \param L The Lua VM state.
339 * \return The number of elements pushed on stack.
341 * \lfield visible The widget visibility.
342 * \lfield name The widget name.
343 * \lfield mouse_enter A function to execute when the mouse enter the widget.
344 * \lfield mouse_leave A function to execute when the mouse leave the widget.
347 luaA_widget_index(lua_State
*L
)
350 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
351 const char *buf
= luaL_checklstring(L
, 2, &len
);
352 awesome_token_t token
;
354 if(luaA_usemetatable(L
, 1, 2))
357 switch((token
= a_tokenize(buf
, len
)))
360 lua_pushboolean(L
, (*widget
)->isvisible
);
363 lua_pushstring(L
, (*widget
)->name
);
365 case A_TK_MOUSE_ENTER
:
366 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
368 case A_TK_MOUSE_LEAVE
:
369 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
375 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
378 /** Generic widget newindex.
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
383 luaA_widget_newindex(lua_State
*L
)
386 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
387 const char *buf
= luaL_checklstring(L
, 2, &len
);
388 awesome_token_t token
;
390 switch((token
= a_tokenize(buf
, len
)))
393 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
395 case A_TK_MOUSE_ENTER
:
396 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
398 case A_TK_MOUSE_LEAVE
:
399 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
402 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
405 widget_invalidate_bywidget(*widget
);
410 /** Generic widget set.
411 * \param L The Lua VM state.
412 * \param idx The table of widgets index.
413 * \param object The object the widget will be attached to.
414 * \param widgets The widgets to fill.
415 * \return The number of elements pushed on stack.
418 luaA_widget_set(lua_State
*L
, int idx
, void *object
, widget_node_t
**widgets
)
420 widget_node_t
*witer
;
422 luaA_checktable(L
, idx
);
424 /* remove all widgets */
425 for(witer
= *widgets
; witer
; witer
= *widgets
)
427 if(witer
->widget
->detach
)
428 witer
->widget
->detach(witer
->widget
, object
);
429 widget_unref(&witer
->widget
);
430 widget_node_list_detach(widgets
, witer
);
434 /* now read all widgets and add them */
436 while(lua_next(L
, idx
))
438 widget_t
**widget
= luaA_checkudata(L
, -1, "widget");
439 widget_node_t
*w
= p_new(widget_node_t
, 1);
441 widget_node_list_append(widgets
, w
);
447 /** Generic widget get.
448 * \param L The Lua VM state.
449 * \param widget The widget list.
450 * \return The number of elements pushed on stack.
453 luaA_widget_get(lua_State
*L
, widget_node_t
*widgets
)
455 widget_node_t
*witer
;
460 for(witer
= widgets
; witer
; witer
= witer
->next
)
462 luaA_widget_userdata_new(L
, witer
->widget
);
463 lua_rawseti(L
, -2, ++i
);
469 const struct luaL_reg awesome_widget_methods
[] =
471 { "__call", luaA_widget_new
},
474 const struct luaL_reg awesome_widget_meta
[] =
476 { "buttons", luaA_widget_buttons
},
477 { "__index", luaA_widget_index
},
478 { "__newindex", luaA_widget_newindex
},
479 { "__gc", luaA_widget_gc
},
480 { "__eq", luaA_widget_eq
},
481 { "__tostring", luaA_widget_tostring
},
485 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80