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 /** Render a list of widgets.
86 * \param wnode The list of widgets.
87 * \param ctx The draw context where to render.
88 * \param rotate_px The rotate pixmap: where to rotate and render the final
89 * pixmap when the object oritation is not east.
90 * \param screen The logical screen used to render.
91 * \param orientation The object orientation.
92 * \param x The x coordinates of the object.
93 * \param y The y coordinates of the object.
94 * \param object The wibox.
98 widget_render(widget_node_t
*wnode
, draw_context_t
*ctx
, xcb_gcontext_t gc
, xcb_pixmap_t rotate_px
,
99 int screen
, orientation_t orientation
,
100 int x
, int y
, wibox_t
*object
)
102 xcb_pixmap_t rootpix
;
105 int left
= 0, right
= 0;
107 xcb_get_property_reply_t
*prop_r
;
108 xcb_get_property_cookie_t prop_c
;
109 area_t rectangle
= { 0, 0, 0, 0 };
111 rectangle
.width
= ctx
->width
;
112 rectangle
.height
= ctx
->height
;
114 if(ctx
->bg
.alpha
!= 0xffff)
116 s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
117 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
119 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
122 && (data
= xcb_get_property_value(prop_r
))
123 && (rootpix
= *(xcb_pixmap_t
*) data
))
128 rootpix
, ctx
->pixmap
,
129 s
->width_in_pixels
, s
->height_in_pixels
,
130 ctx
->width
, ctx
->height
,
137 rootpix
, ctx
->pixmap
,
138 s
->width_in_pixels
, s
->height_in_pixels
,
139 ctx
->width
, ctx
->height
,
145 xcb_copy_area(globalconf
.connection
, rootpix
,
149 ctx
->width
, ctx
->height
);
156 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
158 for(w
= wnode
; w
; w
= w
->next
)
159 if(w
->widget
->align
== AlignLeft
&& w
->widget
->isvisible
)
160 left
+= w
->widget
->draw(ctx
, screen
, w
, left
, (left
+ right
), object
);
162 /* renders right widget from last to first */
163 for(w
= *widget_node_list_last(&wnode
); w
; w
= w
->prev
)
164 if(w
->widget
->align
== AlignRight
&& w
->widget
->isvisible
)
165 right
+= w
->widget
->draw(ctx
, screen
, w
, right
, (left
+ right
), object
);
167 for(w
= wnode
; w
; w
= w
->next
)
168 if(w
->widget
->align
== AlignFlex
&& w
->widget
->isvisible
)
169 left
+= w
->widget
->draw(ctx
, screen
, w
, left
, (left
+ right
), object
);
174 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
175 ctx
->width
, ctx
->height
,
176 ctx
->height
, ctx
->width
,
177 M_PI_2
, ctx
->height
, 0);
180 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
181 ctx
->width
, ctx
->height
,
182 ctx
->height
, ctx
->width
,
183 - M_PI_2
, 0, ctx
->width
);
191 /** Common function for creating a widget.
192 * \param widget The allocated widget.
195 widget_common_new(widget_t
*widget
)
197 widget
->align
= AlignLeft
;
198 widget
->button
= widget_common_button
;
201 /** Invalidate widgets which should be refresh upon
202 * external modifications. widget_t who watch flags will
203 * be set to be refreshed.
204 * \param screen Virtual screen number.
205 * \param flags Cache flags to invalidate.
208 widget_invalidate_cache(int screen
, int flags
)
210 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
212 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
213 widget_node_t
*widget
;
215 for(widget
= wibox
->widgets
; widget
; widget
= widget
->next
)
216 if(widget
->widget
->cache_flags
& flags
)
218 wibox
->need_update
= true;
224 /** Set a wibox needs update because it has widget, or redraw a titlebar.
225 * \todo Probably needs more optimization.
226 * \param widget The widget to look for.
229 widget_invalidate_bywidget(widget_t
*widget
)
232 widget_node_t
*witer
;
235 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
236 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
238 wibox_t
*wibox
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
240 if(!wibox
->need_update
)
241 for(witer
= wibox
->widgets
; witer
; witer
= witer
->next
)
242 if(witer
->widget
== widget
)
244 wibox
->need_update
= true;
249 for(c
= globalconf
.clients
; c
; c
= c
->next
)
250 if(c
->titlebar
&& !c
->titlebar
->need_update
)
251 for(witer
= c
->titlebar
->widgets
; witer
; witer
= witer
->next
)
252 if(witer
->widget
== widget
)
253 c
->titlebar
->need_update
= true;
256 /** Create a new widget.
257 * \param L The Lua VM state.
260 * \lparam A table with at least a name and a type value. Optional attributes
262 * \lreturn A brand new widget.
265 luaA_widget_new(lua_State
*L
)
267 const char *buf
, *type
;
269 widget_constructor_t
*wc
;
273 luaA_checktable(L
, 2);
275 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
276 align
= draw_align_fromstr(buf
, len
);
278 if(!(buf
= luaA_getopt_string(L
, 2, "name", NULL
)))
279 luaL_error(L
, "object widget must have a name");
281 type
= luaA_getopt_string(L
, 2, "type", NULL
);
283 if((wc
= name_func_lookup(type
, WidgetList
)))
286 luaL_error(L
, "unkown widget type: %s", type
);
290 /* Set visible by default. */
293 w
->mouse_enter
= w
->mouse_leave
= LUA_REFNIL
;
295 w
->name
= a_strdup(buf
);
297 return luaA_widget_userdata_new(L
, w
);
300 /** Get or set mouse buttons bindings to a widget.
301 * \param L The Lua VM state.
305 * \lparam An array of mouse button bindings objects, or nothing.
306 * \return The array of mouse button bindings objects of this widget.
309 luaA_widget_buttons(lua_State
*L
)
311 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
312 button_array_t
*buttons
= &(*widget
)->buttons
;
314 if(lua_gettop(L
) == 2)
315 luaA_button_array_set(L
, 2, buttons
);
317 return luaA_button_array_get(L
, buttons
);
320 /** Convert a widget into a printable string.
321 * \param L The Lua VM state.
327 luaA_widget_tostring(lua_State
*L
)
329 widget_t
**p
= luaA_checkudata(L
, 1, "widget");
330 lua_pushfstring(L
, "[widget udata(%p) name(%s)]", *p
, (*p
)->name
);
335 * \param L The Lua VM state.
336 * \return The number of elements pushed on stack.
338 * \lfield visible The widget visibility.
339 * \lfield name The widget name.
340 * \lfield mouse_enter A function to execute when the mouse enter the widget.
341 * \lfield mouse_leave A function to execute when the mouse leave the widget.
344 luaA_widget_index(lua_State
*L
)
347 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
348 const char *buf
= luaL_checklstring(L
, 2, &len
);
349 awesome_token_t token
;
351 if(luaA_usemetatable(L
, 1, 2))
354 switch((token
= a_tokenize(buf
, len
)))
357 lua_pushboolean(L
, (*widget
)->isvisible
);
360 lua_pushstring(L
, (*widget
)->name
);
362 case A_TK_MOUSE_ENTER
:
363 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_enter
);
365 case A_TK_MOUSE_LEAVE
:
366 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*widget
)->mouse_leave
);
372 return (*widget
)->index
? (*widget
)->index(L
, token
) : 0;
375 /** Generic widget newindex.
376 * \param L The Lua VM state.
377 * \return The number of elements pushed on stack.
380 luaA_widget_newindex(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 switch((token
= a_tokenize(buf
, len
)))
390 (*widget
)->isvisible
= luaA_checkboolean(L
, 3);
392 case A_TK_MOUSE_ENTER
:
393 luaA_registerfct(L
, 3, &(*widget
)->mouse_enter
);
395 case A_TK_MOUSE_LEAVE
:
396 luaA_registerfct(L
, 3, &(*widget
)->mouse_leave
);
399 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
402 widget_invalidate_bywidget(*widget
);
407 const struct luaL_reg awesome_widget_methods
[] =
409 { "__call", luaA_widget_new
},
412 const struct luaL_reg awesome_widget_meta
[] =
414 { "buttons", luaA_widget_buttons
},
415 { "__index", luaA_widget_index
},
416 { "__newindex", luaA_widget_newindex
},
417 { "__gc", luaA_widget_gc
},
418 { "__eq", luaA_widget_eq
},
419 { "__tostring", luaA_widget_tostring
},
423 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80