awesomerc: make titlebar optional and disabled by default
[awesome.git] / widget.c
blob41c7ccb7ddf388bd68a8f8821308a9413d28e6ef
1 /*
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.
22 #include <math.h>
24 #include <xcb/xcb.h>
25 #include <xcb/xcb_atom.h>
27 #include "widget.h"
28 #include "titlebar.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.
45 int
46 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
48 switch(alignment)
50 case AlignLeft:
51 case AlignFlex:
52 return offset;
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.
65 static void
66 widget_common_button_press(widget_node_t *w,
67 xcb_button_press_event_t *ev,
68 int screen __attribute__ ((unused)),
69 void *p,
70 awesome_type_t type)
72 button_t *b;
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.
93 * \todo Remove GC.
95 void
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;
101 xcb_screen_t *s;
102 widget_node_t *w;
103 int left = 0, right = 0;
104 char *data;
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,
116 PIXMAP, 0, 1);
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))
121 switch(position)
123 case Left:
124 draw_rotate(ctx,
125 rootpix, ctx->pixmap,
126 s->width_in_pixels, s->height_in_pixels,
127 ctx->width, ctx->height,
128 M_PI_2,
129 y + ctx->width,
130 - x);
131 break;
132 case Right:
133 draw_rotate(ctx,
134 rootpix, ctx->pixmap,
135 s->width_in_pixels, s->height_in_pixels,
136 ctx->width, ctx->height,
137 - M_PI_2,
138 - y,
139 x + ctx->height);
140 break;
141 default:
142 xcb_copy_area(globalconf.connection, rootpix,
143 rotate_px, gc,
144 x, y,
145 0, 0,
146 ctx->width, ctx->height);
147 break;
149 p_delete(&prop_r);
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);
168 switch(position)
170 case Right:
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);
175 break;
176 case Left:
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);
181 break;
182 default:
183 break;
188 /** Common function for creating a widget.
189 * \param widget The allocated widget.
191 void
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.
204 void
205 widget_invalidate_cache(int screen, int flags)
207 statusbar_t *statusbar;
208 widget_node_t *widget;
210 for(statusbar = globalconf.screens[screen].statusbar;
211 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;
217 break;
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.
225 void
226 widget_invalidate_bywidget(widget_t *widget)
228 int screen;
229 statusbar_t *statusbar;
230 widget_node_t *witer;
231 client_t *c;
233 for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
234 for(statusbar = globalconf.screens[screen].statusbar;
235 statusbar;
236 statusbar = statusbar->next)
237 for(witer = statusbar->widgets; witer; witer = witer->next)
238 if(witer->widget == widget)
240 statusbar->need_update = true;
241 break;
244 for(c = globalconf.clients; c; c = c->next)
245 if(c->titlebar)
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.
254 * \luastack
255 * \lparam A table with at least a name and a type value. Optional attributes
256 * are: align.
257 * \lreturn A brand new widget.
259 static int
260 luaA_widget_new(lua_State *L)
262 const char *buf, *type;
263 widget_t *w = NULL;
264 widget_constructor_t *wc;
265 alignment_t align;
266 size_t len;
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)))
279 w = wc(align);
280 else
281 luaL_error(L, "unkown widget type: %s", type);
283 w->type = wc;
285 /* Set visible by default. */
286 w->isvisible = true;
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.
296 * \luastack
297 * \lvalue A widget.
298 * \lparam A mouse button bindings object.
300 static int
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);
307 button_ref(b);
309 return 0;
312 /** Remove a mouse button bindings from a widget.
313 * \param L The Lua VM state.
315 * \luastack
316 * \lvalue A widget.
317 * \lparam A mouse button bindings object.
319 static int
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);
326 button_unref(b);
328 return 0;
331 /** Convert a widget into a printable string.
332 * \param L The Lua VM state.
334 * \luastack
335 * \lvalue A widget.
337 static int
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);
342 return 1;
345 /** Generic widget.
346 * \param L The Lua VM state.
347 * \return The number of elements pushed on stack.
348 * \luastack
349 * \lfield visible The widget visibility.
350 * \lfield name The widget name.
352 static int
353 luaA_widget_index(lua_State *L)
355 size_t len;
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))
361 return 1;
363 switch((token = a_tokenize(buf, len)))
365 case A_TK_VISIBLE:
366 lua_pushboolean(L, (*widget)->isvisible);
367 return 1;
368 case A_TK_NAME:
369 lua_pushstring(L, (*widget)->name);
370 return 1;
371 default:
372 break;
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.
382 static int
383 luaA_widget_newindex(lua_State *L)
385 size_t len;
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)))
392 case A_TK_VISIBLE:
393 (*widget)->isvisible = luaA_checkboolean(L, 3);
394 return 0;
395 default:
396 break;
399 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
402 const struct luaL_reg awesome_widget_methods[] =
404 { "__call", luaA_widget_new },
405 { NULL, NULL }
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 },
416 { NULL, NULL }
419 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80