awful.placement.no_overlap: Honor original position
[awesome.git] / widget.c
blob5869709e9ecce6c40f4f9494e4216a23d0d85cda
1 /*
2 * widget.c - widget managing
4 * Copyright © 2007-2009 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 "screen.h"
28 #include "mouse.h"
29 #include "widget.h"
30 #include "wibox.h"
31 #include "client.h"
32 #include "luaa.h"
33 #include "common/atoms.h"
34 #include "common/xutil.h"
36 LUA_OBJECT_FUNCS(widget_class, widget_t, widget);
38 /** Collect a widget structure.
39 * \param L The Lua VM state.
40 * \return 0
42 static int
43 luaA_widget_gc(lua_State *L)
45 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
46 if(widget->destructor)
47 widget->destructor(widget);
48 button_array_wipe(&widget->buttons);
49 return luaA_object_gc(L);
52 /** Get a widget node from a wibox by coords.
53 * \param orientation Wibox orientation.
54 * \param widgets The widget list.
55 * \param width The container width.
56 * \param height The container height.
57 * \param x X coordinate of the widget.
58 * \param y Y coordinate of the widget.
59 * \return A widget.
61 widget_t *
62 widget_getbycoords(orientation_t orientation, widget_node_array_t *widgets,
63 int width, int height, int16_t *x, int16_t *y)
65 int tmp;
67 /* Need to transform coordinates like it was top/bottom */
68 switch(orientation)
70 case South:
71 tmp = *y;
72 *y = width - *x;
73 *x = tmp;
74 break;
75 case North:
76 tmp = *y;
77 *y = *x;
78 *x = height - tmp;
79 break;
80 default:
81 break;
83 foreach(w, *widgets)
84 if(w->widget->isvisible
85 && *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
86 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
87 return w->widget;
89 return NULL;
92 /** Convert a Lua table to a list of widget nodes.
93 * \param L The Lua VM state.
94 * \param idx Index of the table where to store the widgets references.
95 * \param widgets The linked list of widget node.
97 static void
98 luaA_table2widgets(lua_State *L, int idx, widget_node_array_t *widgets)
100 if(lua_istable(L, -1))
102 int table_idx = luaA_absindex(L, idx);
103 lua_pushnil(L);
104 while(luaA_next(L, -2))
105 luaA_table2widgets(L, table_idx, widgets);
106 /* remove the table */
107 lua_pop(L, 1);
109 else
111 widget_t *widget = luaA_toudata(L, -1, &widget_class);
112 if(widget)
114 widget_node_t w;
115 p_clear(&w, 1);
116 w.widget = luaA_object_ref_item(L, idx, -1);
117 widget_node_array_append(widgets, w);
119 else
120 lua_pop(L, 1); /* remove value */
124 /** Retrieve a list of widget geometries using a Lua layout function.
125 * a table which contains the geometries is then pushed onto the stack
126 * \param wibox The wibox.
127 * \return True is everything is ok, false otherwise.
129 static bool
130 widget_geometries(wibox_t *wibox)
132 /* get the layout field of the widget table */
133 if(wibox->widgets_table)
135 /* push wibox */
136 luaA_object_push(globalconf.L, wibox);
137 /* push widgets table */
138 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
139 /* remove wibox */
140 lua_remove(globalconf.L, -2);
141 /* get layout field from the table */
142 lua_getfield(globalconf.L, -1, "layout");
143 /* remove the widget table */
144 lua_remove(globalconf.L, -2);
146 else
147 lua_pushnil(globalconf.L);
149 /* if the layout field is a function */
150 if(lua_isfunction(globalconf.L, -1))
152 /* Push 1st argument: wibox geometry */
153 area_t geometry = wibox->geometry;
154 geometry.x = 0;
155 geometry.y = 0;
156 /* we need to exchange the width and height of the wibox window if it
157 * it is rotated, so the layout function doesn't need to care about that
159 if(wibox->orientation != East)
161 int i = geometry.height;
162 geometry.height = geometry.width;
163 geometry.width = i;
165 luaA_pusharea(globalconf.L, geometry);
166 /* Push 2nd argument: widget table */
167 luaA_object_push(globalconf.L, wibox);
168 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
169 lua_remove(globalconf.L, -2);
170 /* Push 3rd argument: wibox screen */
171 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen) + 1);
172 /* Re-push the layout function */
173 lua_pushvalue(globalconf.L, -4);
174 /* call the layout function with 3 arguments (wibox geometry, widget
175 * table, screen) and wait for one result */
176 if(!luaA_dofunction(globalconf.L, 3, 1))
177 return false;
179 /* Remove the left over layout function */
180 lua_remove(globalconf.L, -2);
182 else
184 /* Remove the "nil function" */
185 lua_pop(globalconf.L, 1);
187 /* If no layout function has been specified, we just push a table with
188 * geometries onto the stack. These geometries are nothing fancy, they
189 * have x = y = 0 and their height and width set to the widgets demands
190 * or the wibox size, depending on which is less.
193 /* push wibox */
194 luaA_object_push(globalconf.L, wibox);
196 widget_node_array_t *widgets = &wibox->widgets;
197 wibox_widget_node_array_wipe(globalconf.L, -1);
198 widget_node_array_init(widgets);
200 /* push widgets table */
201 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
202 /* Convert widgets table */
203 luaA_table2widgets(globalconf.L, -2, widgets);
204 /* remove wibox */
205 lua_remove(globalconf.L, -1);
207 lua_newtable(globalconf.L);
208 for(int i = 0; i < widgets->len; i++)
210 lua_pushnumber(globalconf.L, i + 1);
211 widget_t *widget = widgets->tab[i].widget;
212 lua_pushnumber(globalconf.L, screen_array_indexof(&globalconf.screens, wibox->screen) + 1);
213 area_t geometry = widget->extents(globalconf.L, widget);
214 lua_pop(globalconf.L, 1);
215 geometry.x = geometry.y = 0;
216 geometry.width = MIN(wibox->geometry.width, geometry.width);
217 geometry.height = MIN(wibox->geometry.height, geometry.height);
219 luaA_pusharea(globalconf.L, geometry);
221 lua_settable(globalconf.L, -3);
224 return true;
227 /** Render a list of widgets.
228 * \param wibox The wibox.
229 * \todo Remove GC.
231 void
232 widget_render(wibox_t *wibox)
234 lua_State *L = globalconf.L;
235 draw_context_t *ctx = &wibox->ctx;
236 area_t rectangle = { 0, 0, 0, 0 };
237 color_t col;
239 rectangle.width = ctx->width;
240 rectangle.height = ctx->height;
242 if (!widget_geometries(wibox))
243 return;
245 if(ctx->bg.alpha != 0xffff)
247 int x = wibox->geometry.x + wibox->border_width,
248 y = wibox->geometry.y + wibox->border_width;
249 xcb_get_property_reply_t *prop_r;
250 char *data;
251 xcb_pixmap_t rootpix;
252 xcb_get_property_cookie_t prop_c;
253 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
254 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
255 XCB_ATOM_PIXMAP, 0, 1);
256 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
258 if(prop_r->value_len
259 && (data = xcb_get_property_value(prop_r))
260 && (rootpix = *(xcb_pixmap_t *) data))
261 switch(wibox->orientation)
263 case North:
264 draw_rotate(ctx,
265 rootpix, ctx->pixmap,
266 s->width_in_pixels, s->height_in_pixels,
267 ctx->width, ctx->height,
268 M_PI_2,
269 y + ctx->width,
270 - x);
271 break;
272 case South:
273 draw_rotate(ctx,
274 rootpix, ctx->pixmap,
275 s->width_in_pixels, s->height_in_pixels,
276 ctx->width, ctx->height,
277 - M_PI_2,
278 - y,
279 x + ctx->height);
280 break;
281 case East:
282 xcb_copy_area(globalconf.connection, rootpix,
283 wibox->pixmap, wibox->gc,
284 x, y,
285 0, 0,
286 ctx->width, ctx->height);
287 break;
289 p_delete(&prop_r);
293 widget_node_array_t *widgets = &wibox->widgets;
295 /* push wibox */
296 luaA_object_push(globalconf.L, wibox);
298 wibox_widget_node_array_wipe(globalconf.L, -1);
299 widget_node_array_init(widgets);
301 /* push widgets table */
302 luaA_object_push_item(globalconf.L, -1, wibox->widgets_table);
303 luaA_table2widgets(L, -2, widgets);
304 /* remove wibox */
305 lua_remove(globalconf.L, -1);
307 size_t num_widgets = lua_objlen(L, -1);
308 if(lua_objlen(L, -1) != (size_t) widgets->len)
310 warn("Widget layout returned wrong number of geometries. Got %d widgets and %lu geometries.",
311 widgets->len, (unsigned long) lua_objlen(L, -1));
312 /* Set num_widgets to min(widgets->len, lua_objlen(L, -1)); */
313 if(num_widgets > (size_t) widgets->len)
314 num_widgets = widgets->len;
317 /* get computed geometries */
318 for(size_t i = 0; i < num_widgets; i++)
320 lua_pushnumber(L, i + 1);
321 lua_gettable(L, -2);
323 widgets->tab[i].geometry.x = luaA_getopt_number(L, -1, "x", wibox->geometry.x);
324 widgets->tab[i].geometry.y = luaA_getopt_number(L, -1, "y", wibox->geometry.y);
325 widgets->tab[i].geometry.width = luaA_getopt_number(L, -1, "width", 1);
326 widgets->tab[i].geometry.height = luaA_getopt_number(L, -1, "height", 1);
328 lua_pop(L, 1);
330 lua_pop(L, 1);
332 /* draw background image, only if the background color is not opaque */
333 if(wibox->bg_image && ctx->bg.alpha != 0xffff)
334 draw_image(ctx, 0, 0, 1.0, wibox->bg_image);
336 /* draw background color */
337 xcolor_to_color(&ctx->bg, &col);
338 draw_rectangle(ctx, rectangle, 1.0, true, &col);
340 /* draw everything! */
341 for(int i = 0; i < widgets->len; i++)
342 if(widgets->tab[i].widget->isvisible)
343 widgets->tab[i].widget->draw(widgets->tab[i].widget,
344 ctx, widgets->tab[i].geometry, wibox);
346 cairo_surface_flush(ctx->surface);
348 switch(wibox->orientation)
350 case South:
351 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
352 ctx->width, ctx->height,
353 ctx->height, ctx->width,
354 M_PI_2, ctx->height, 0);
355 break;
356 case North:
357 draw_rotate(ctx, ctx->pixmap, wibox->pixmap,
358 ctx->width, ctx->height,
359 ctx->height, ctx->width,
360 - M_PI_2, 0, ctx->width);
361 break;
362 case East:
363 break;
367 /** Invalidate widgets which should be refresh depending on their types.
368 * \param type Widget type to invalidate.
370 void
371 widget_invalidate_bytype(widget_constructor_t *type)
373 foreach(wibox, globalconf.wiboxes)
374 foreach(wnode, (*wibox)->widgets)
375 if(wnode->widget->type == type)
377 (*wibox)->need_update = true;
378 break;
382 /** Set a wibox needs update because it has widget, or redraw a titlebar.
383 * \param widget The widget to look for.
385 void
386 widget_invalidate_bywidget(widget_t *widget)
388 foreach(wibox, globalconf.wiboxes)
389 if(!(*wibox)->need_update)
390 foreach(wnode, (*wibox)->widgets)
391 if(wnode->widget == widget)
393 (*wibox)->need_update = true;
394 break;
397 foreach(_c, globalconf.clients)
399 client_t *c = *_c;
400 if(c->titlebar && !c->titlebar->need_update)
401 for(int j = 0; j < c->titlebar->widgets.len; j++)
402 if(c->titlebar->widgets.tab[j].widget == widget)
404 c->titlebar->need_update = true;
405 break;
410 /** Create a new widget.
411 * \param L The Lua VM state.
413 * \luastack
414 * \lparam A table with at least a type value.
415 * \lreturn A brand new widget.
417 static int
418 luaA_widget_new(lua_State *L)
420 luaA_class_new(L, &widget_class);
422 widget_t *w = luaA_checkudata(L, -1, &widget_class);
423 /* Set visible by default. */
424 w->isvisible = true;
426 return 1;
429 /** Get or set mouse buttons bindings to a widget.
430 * \param L The Lua VM state.
432 * \luastack
433 * \lvalue A widget.
434 * \lparam An array of mouse button bindings objects, or nothing.
435 * \return The array of mouse button bindings objects of this widget.
437 static int
438 luaA_widget_buttons(lua_State *L)
440 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
442 if(lua_gettop(L) == 2)
444 luaA_button_array_set(L, 1, 2, &widget->buttons);
445 luaA_object_emit_signal(L, 1, "property::buttons", 0);
446 return 1;
449 return luaA_button_array_get(L, 1, &widget->buttons);
452 /** Generic widget.
453 * \param L The Lua VM state.
454 * \return The number of elements pushed on stack.
455 * \luastack
456 * \lfield visible The widget visibility.
457 * \lfield mouse_enter A function to execute when the mouse enter the widget.
458 * \lfield mouse_leave A function to execute when the mouse leave the widget.
460 static int
461 luaA_widget_index(lua_State *L)
463 size_t len;
464 const char *prop = luaL_checklstring(L, 2, &len);
465 awesome_token_t token = a_tokenize(prop, len);
467 /* Try standard method */
468 if(luaA_class_index(L))
469 return 1;
471 /* Then call special widget index */
472 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
473 return widget->index ? widget->index(L, token) : 0;
476 /** Generic widget newindex.
477 * \param L The Lua VM state.
478 * \return The number of elements pushed on stack.
480 static int
481 luaA_widget_newindex(lua_State *L)
483 size_t len;
484 const char *prop = luaL_checklstring(L, 2, &len);
485 awesome_token_t token = a_tokenize(prop, len);
487 /* Try standard method */
488 luaA_class_newindex(L);
490 /* Then call special widget newindex */
491 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
492 return widget->newindex ? widget->newindex(L, token) : 0;
495 static int
496 luaA_widget_extents(lua_State *L)
498 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
499 area_t g = {
500 .x = 0,
501 .y = 0,
502 .width = 0,
503 .height = 0
506 if(widget->extents)
507 g = widget->extents(L, widget);
509 lua_newtable(L);
510 lua_pushnumber(L, g.width);
511 lua_setfield(L, -2, "width");
512 lua_pushnumber(L, g.height);
513 lua_setfield(L, -2, "height");
515 return 1;
518 static int
519 luaA_widget_set_type(lua_State *L, widget_t *w)
521 size_t len;
522 const char *type = luaL_checklstring(L, -1, &len);
523 widget_constructor_t *wc = NULL;
525 switch(a_tokenize(type, len))
527 case A_TK_TEXTBOX:
528 wc = widget_textbox;
529 break;
530 case A_TK_PROGRESSBAR:
531 wc = widget_progressbar;
532 break;
533 case A_TK_GRAPH:
534 wc = widget_graph;
535 break;
536 case A_TK_SYSTRAY:
537 wc = widget_systray;
538 break;
539 case A_TK_IMAGEBOX:
540 wc = widget_imagebox;
541 break;
542 default:
543 break;
546 if(!wc)
547 luaL_error(L, "unknown widget type: %s", type);
549 wc(w);
550 w->type = wc;
551 luaA_object_emit_signal(L, -3, "property::type", 0);
553 return 0;
556 static int
557 luaA_widget_get_visible(lua_State *L, widget_t *w)
559 lua_pushboolean(L, w->isvisible);
560 return 1;
563 static int
564 luaA_widget_set_visible(lua_State *L, widget_t *w)
566 w->isvisible = luaA_checkboolean(L, -1);
567 widget_invalidate_bywidget(w);
568 luaA_object_emit_signal(L, -3, "property::visible", 0);
569 return 0;
572 void
573 widget_class_setup(lua_State *L)
575 static const struct luaL_reg widget_methods[] =
577 LUA_CLASS_METHODS(widget)
578 { "__call", luaA_widget_new },
579 { NULL, NULL }
582 static const struct luaL_reg widget_meta[] =
584 LUA_OBJECT_META(widget)
585 { "buttons", luaA_widget_buttons },
586 { "extents", luaA_widget_extents },
587 { "__index", luaA_widget_index },
588 { "__newindex", luaA_widget_newindex },
589 { "__gc", luaA_widget_gc },
590 { NULL, NULL }
593 luaA_class_setup(L, &widget_class, "widget", (lua_class_allocator_t) widget_new,
594 NULL, NULL,
595 widget_methods, widget_meta);
596 luaA_class_add_property(&widget_class, A_TK_VISIBLE,
597 (lua_class_propfunc_t) luaA_widget_set_visible,
598 (lua_class_propfunc_t) luaA_widget_get_visible,
599 (lua_class_propfunc_t) luaA_widget_set_visible);
600 luaA_class_add_property(&widget_class, A_TK_TYPE,
601 (lua_class_propfunc_t) luaA_widget_set_type,
602 NULL,
603 NULL);
606 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80