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>
29 #include "common/atoms.h"
31 extern awesome_t globalconf
;
33 DO_LUA_NEW(extern, widget_t
, widget
, "widget", widget_ref
)
34 DO_LUA_GC(widget_t
, widget
, "widget", widget_unref
)
35 DO_LUA_EQ(widget_t
, widget
, "widget")
37 #include "widgetgen.h"
39 /** Compute offset for drawing the first pixel of a widget.
40 * \param barwidth The statusbar width.
41 * \param widgetwidth The widget width.
42 * \param alignment The widget alignment on statusbar.
43 * \return The x coordinate to draw at.
46 widget_calculate_offset(int barwidth
, int widgetwidth
, int offset
, int alignment
)
54 return barwidth
- offset
- widgetwidth
;
57 /** Common function for button press event on widget.
58 * It will look into configuration to find the callback function to call.
59 * \param w The widget node.
60 * \param ev The button press event the widget received.
61 * \param screen The screen number.
62 * \param p The object where user clicked.
63 * \param type The object type.
66 widget_common_button_press(widget_node_t
*w
,
67 xcb_button_press_event_t
*ev
,
68 int screen
__attribute__ ((unused
)),
74 for(b
= w
->widget
->buttons
; b
; b
= b
->next
)
75 if(ev
->detail
== b
->button
&& CLEANMASK(ev
->state
) == b
->mod
&& b
->fct
)
77 luaA_pushpointer(globalconf
.L
, p
, type
);
78 luaA_dofunction(globalconf
.L
, b
->fct
, 1, 0);
82 /** Render a list of widgets.
83 * \param wnode The list of widgets.
84 * \param ctx The draw context where to render.
85 * \param rotate_px The rotate pixmap: where to rotate and render the final
86 * \param screen The logical screen used to render.
87 * \param position The object position.
88 * \param x The x coordinates of the object.
89 * \param y The y coordinates of the object.
90 * pixmap when the object position is right or left.
91 * \param object The object pointer.
92 * \param type The object type.
96 widget_render(widget_node_t
*wnode
, draw_context_t
*ctx
, xcb_gcontext_t gc
, xcb_pixmap_t rotate_px
,
97 int screen
, position_t position
,
98 int x
, int y
, void *object
, awesome_type_t type
)
100 xcb_pixmap_t rootpix
;
103 int left
= 0, right
= 0;
105 xcb_get_property_reply_t
*prop_r
;
106 xcb_get_property_cookie_t prop_c
;
107 area_t rectangle
= { 0, 0, 0, 0 };
109 rectangle
.width
= ctx
->width
;
110 rectangle
.height
= ctx
->height
;
112 if(ctx
->bg
.alpha
!= 0xffff)
114 s
= xutil_screen_get(globalconf
.connection
, ctx
->phys_screen
);
115 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, s
->root
, _XROOTPMAP_ID
,
117 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
)))
119 if((data
= xcb_get_property_value(prop_r
))
120 && (rootpix
= *(xcb_pixmap_t
*) data
))
125 rootpix
, ctx
->pixmap
,
126 s
->width_in_pixels
, s
->height_in_pixels
,
127 ctx
->width
, ctx
->height
,
134 rootpix
, ctx
->pixmap
,
135 s
->width_in_pixels
, s
->height_in_pixels
,
136 ctx
->width
, ctx
->height
,
142 xcb_copy_area(globalconf
.connection
, rootpix
,
146 ctx
->width
, ctx
->height
);
153 draw_rectangle(ctx
, rectangle
, 1.0, true, &ctx
->bg
);
155 for(w
= wnode
; w
; w
= w
->next
)
156 if(w
->widget
->isvisible
&& w
->widget
->align
== AlignLeft
)
157 left
+= w
->widget
->draw(ctx
, screen
, w
, left
, (left
+ right
), object
, type
);
159 /* renders right widget from last to first */
160 for(w
= *widget_node_list_last(&wnode
); w
; w
= w
->prev
)
161 if(w
->widget
->isvisible
&& w
->widget
->align
== AlignRight
)
162 right
+= w
->widget
->draw(ctx
, screen
, w
, right
, (left
+ right
), object
, type
);
164 for(w
= wnode
; w
; w
= w
->next
)
165 if(w
->widget
->isvisible
&& w
->widget
->align
== AlignFlex
)
166 left
+= w
->widget
->draw(ctx
, screen
, w
, left
, (left
+ right
), object
, type
);
171 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
172 ctx
->width
, ctx
->height
,
173 ctx
->height
, ctx
->width
,
174 M_PI_2
, ctx
->height
, 0);
177 draw_rotate(ctx
, ctx
->pixmap
, rotate_px
,
178 ctx
->width
, ctx
->height
,
179 ctx
->height
, ctx
->width
,
180 - M_PI_2
, 0, ctx
->width
);
188 /** Common function for creating a widget.
189 * \param widget The allocated widget.
192 widget_common_new(widget_t
*widget
)
194 widget
->align
= AlignLeft
;
195 widget
->button_press
= widget_common_button_press
;
198 /** Invalidate widgets which should be refresh upon
199 * external modifications. widget_t who watch flags will
200 * be set to be refreshed.
201 * \param screen Virtual screen number.
202 * \param flags Cache flags to invalidate.
205 widget_invalidate_cache(int screen
, int flags
)
207 statusbar_t
*statusbar
;
208 widget_node_t
*widget
;
210 for(statusbar
= globalconf
.screens
[screen
].statusbar
;
212 statusbar
= statusbar
->next
)
213 for(widget
= statusbar
->widgets
; widget
; widget
= widget
->next
)
214 if(widget
->widget
->cache_flags
& flags
)
216 statusbar
->need_update
= true;
221 /** Set a statusbar needs update because it has widget, or redraw a titlebar.
222 * \todo Probably needs more optimization.
223 * \param widget The widget to look for.
226 widget_invalidate_bywidget(widget_t
*widget
)
229 statusbar_t
*statusbar
;
230 widget_node_t
*witer
;
233 for(screen
= 0; screen
< globalconf
.screens_info
->nscreen
; screen
++)
234 for(statusbar
= globalconf
.screens
[screen
].statusbar
;
236 statusbar
= statusbar
->next
)
237 for(witer
= statusbar
->widgets
; witer
; witer
= witer
->next
)
238 if(witer
->widget
== widget
)
240 statusbar
->need_update
= true;
244 for(c
= globalconf
.clients
; c
; c
= c
->next
)
246 for(witer
= c
->titlebar
->widgets
; witer
; witer
= witer
->next
)
247 if(witer
->widget
== widget
)
248 c
->titlebar
->need_update
= true;
251 /** Create a new widget.
252 * \param L The Lua VM state.
255 * \lparam A table with at least a name and a type value. Optional attributes
257 * \lreturn A brand new widget.
260 luaA_widget_new(lua_State
*L
)
262 const char *buf
, *type
;
264 widget_constructor_t
*wc
;
268 luaA_checktable(L
, 2);
270 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
271 align
= draw_align_fromstr(buf
, len
);
273 if(!(buf
= luaA_getopt_string(L
, 2, "name", NULL
)))
274 luaL_error(L
, "object widget must have a name");
276 type
= luaA_getopt_string(L
, 2, "type", NULL
);
278 if((wc
= name_func_lookup(type
, WidgetList
)))
281 luaL_error(L
, "unkown widget type: %s", type
);
285 /* Set visible by default. */
288 w
->name
= a_strdup(buf
);
290 return luaA_widget_userdata_new(L
, w
);
293 /** Add a mouse button bindings to a widget.
294 * \param L The Lua VM state.
298 * \lparam A mouse button bindings object.
301 luaA_widget_mouse_add(lua_State
*L
)
303 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
304 button_t
**b
= luaA_checkudata(L
, 2, "mouse");
306 button_list_push(&(*widget
)->buttons
, *b
);
312 /** Remove a mouse button bindings from a widget.
313 * \param L The Lua VM state.
317 * \lparam A mouse button bindings object.
320 luaA_widget_mouse_remove(lua_State
*L
)
322 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
323 button_t
**b
= luaA_checkudata(L
, 2, "mouse");
325 button_list_detach(&(*widget
)->buttons
, *b
);
331 /** Convert a widget into a printable string.
332 * \param L The Lua VM state.
338 luaA_widget_tostring(lua_State
*L
)
340 widget_t
**p
= luaA_checkudata(L
, 1, "widget");
341 lua_pushfstring(L
, "[widget udata(%p) name(%s)]", *p
, (*p
)->name
);
346 * \param L The Lua VM state.
347 * \return The number of elements pushed on stack.
349 * \lfield visible The widget visibility.
350 * \lfield name The widget name.
353 luaA_widget_index(lua_State
*L
)
356 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
357 const char *buf
= luaL_checklstring(L
, 2, &len
);
358 awesome_token_t token
;
360 if(luaA_usemetatable(L
, 1, 2))
363 switch((token
= a_tokenize(buf
, len
)))
366 lua_pushboolean(L
, (*widget
)->isvisible
);
369 lua_pushstring(L
, (*widget
)->name
);
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);
399 return (*widget
)->newindex
? (*widget
)->newindex(L
, token
) : 0;
402 const struct luaL_reg awesome_widget_methods
[] =
404 { "__call", luaA_widget_new
},
407 const struct luaL_reg awesome_widget_meta
[] =
409 { "mouse_add", luaA_widget_mouse_add
},
410 { "mouse_remove", luaA_widget_mouse_remove
},
411 { "__index", luaA_widget_index
},
412 { "__newindex", luaA_widget_newindex
},
413 { "__gc", luaA_widget_gc
},
414 { "__eq", luaA_widget_eq
},
415 { "__tostring", luaA_widget_tostring
},
419 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80