2007-12-22 15:46:57 +01:00
|
|
|
/*
|
|
|
|
* widget.c - widget managing
|
|
|
|
*
|
2009-04-17 16:19:55 +02:00
|
|
|
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
|
2007-12-22 15:46:57 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-06-03 16:32:23 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <xcb/xcb.h>
|
2008-06-17 21:23:38 +02:00
|
|
|
#include <xcb/xcb_atom.h>
|
2008-06-03 16:32:23 +02:00
|
|
|
|
2009-04-17 16:14:09 +02:00
|
|
|
#include "screen.h"
|
2008-08-12 13:23:10 +02:00
|
|
|
#include "mouse.h"
|
2007-12-15 07:53:53 +01:00
|
|
|
#include "widget.h"
|
2008-09-21 20:39:23 +02:00
|
|
|
#include "wibox.h"
|
2009-04-17 16:19:55 +02:00
|
|
|
#include "client.h"
|
2008-06-30 18:55:14 +02:00
|
|
|
#include "common/atoms.h"
|
2009-04-17 16:52:25 +02:00
|
|
|
#include "common/xutil.h"
|
2007-12-15 07:53:53 +01:00
|
|
|
|
2008-01-16 08:06:59 +01:00
|
|
|
#include "widgetgen.h"
|
2007-12-15 18:21:02 +01:00
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
DO_LUA_TOSTRING(widget_t, widget, "widget")
|
|
|
|
|
|
|
|
/** Collect a widget structure.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return 0
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_widget_gc(lua_State *L)
|
|
|
|
{
|
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
2009-05-08 09:53:20 +02:00
|
|
|
luaA_ref_array_wipe(&widget->refs);
|
2009-04-09 15:12:17 +02:00
|
|
|
if(widget->destructor)
|
|
|
|
widget->destructor(widget);
|
|
|
|
button_array_wipe(&widget->buttons);
|
|
|
|
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, widget->mouse_enter);
|
|
|
|
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, widget->mouse_leave);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Delete a widget node structure.
|
|
|
|
* \param node The node to destroy.
|
2008-10-26 14:26:44 +01:00
|
|
|
*/
|
|
|
|
void
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_node_delete(widget_node_t *node)
|
2008-10-26 14:26:44 +01:00
|
|
|
{
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_unref(globalconf.L, node->widget);
|
2008-10-26 14:26:44 +01:00
|
|
|
}
|
|
|
|
|
2008-12-03 23:03:44 +01:00
|
|
|
/** Get a widget node from a wibox by coords.
|
2009-05-10 16:17:39 +02:00
|
|
|
* \param orientation Wibox orientation.
|
2008-12-03 23:03:44 +01:00
|
|
|
* \param widgets The widget list.
|
|
|
|
* \param width The container width.
|
|
|
|
* \param height The container height.
|
|
|
|
* \param x X coordinate of the widget.
|
|
|
|
* \param y Y coordinate of the widget.
|
|
|
|
* \return A widget.
|
|
|
|
*/
|
|
|
|
widget_t *
|
2009-05-10 16:17:39 +02:00
|
|
|
widget_getbycoords(orientation_t orientation, widget_node_array_t *widgets,
|
2008-12-03 23:03:44 +01:00
|
|
|
int width, int height, int16_t *x, int16_t *y)
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
/* Need to transform coordinates like it was top/bottom */
|
2009-05-10 16:17:39 +02:00
|
|
|
switch(orientation)
|
2008-12-03 23:03:44 +01:00
|
|
|
{
|
2009-05-10 16:17:39 +02:00
|
|
|
case South:
|
2008-12-03 23:03:44 +01:00
|
|
|
tmp = *y;
|
|
|
|
*y = width - *x;
|
|
|
|
*x = tmp;
|
|
|
|
break;
|
2009-05-10 16:17:39 +02:00
|
|
|
case North:
|
2008-12-03 23:03:44 +01:00
|
|
|
tmp = *y;
|
|
|
|
*y = *x;
|
|
|
|
*x = height - tmp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-04-09 15:12:17 +02:00
|
|
|
foreach(w, *widgets)
|
|
|
|
if(w->widget->isvisible
|
|
|
|
&& *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
|
2008-12-03 23:03:44 +01:00
|
|
|
&& *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
|
|
|
|
return w->widget;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-19 19:14:55 +02:00
|
|
|
/** Convert a Lua table to a list of widget nodet.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param widgets The linked list of widget node.
|
|
|
|
*/
|
|
|
|
void
|
2008-10-21 17:21:21 +02:00
|
|
|
luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
|
2008-10-19 19:14:55 +02:00
|
|
|
{
|
|
|
|
if(lua_istable(L, -1))
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(luaA_next(L, -2))
|
|
|
|
luaA_table2widgets(L, widgets);
|
2009-04-09 15:12:17 +02:00
|
|
|
/* remove the table */
|
|
|
|
lua_pop(L, 1);
|
2008-10-19 19:14:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-11 13:39:15 +02:00
|
|
|
widget_t *widget = luaA_toudata(L, -1, "widget");
|
2008-10-19 19:14:55 +02:00
|
|
|
if(widget)
|
|
|
|
{
|
2008-10-21 17:21:21 +02:00
|
|
|
widget_node_t w;
|
|
|
|
p_clear(&w, 1);
|
2009-04-09 15:12:17 +02:00
|
|
|
w.widget = widget_ref(L);
|
2008-10-21 17:21:21 +02:00
|
|
|
widget_node_array_append(widgets, w);
|
2008-10-19 19:14:55 +02:00
|
|
|
}
|
2009-04-09 15:12:17 +02:00
|
|
|
else
|
|
|
|
lua_pop(L, 1); /* remove value */
|
2008-10-19 19:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-03 16:32:23 +02:00
|
|
|
/** Render a list of widgets.
|
2008-10-19 19:14:55 +02:00
|
|
|
* \param wibox The wibox.
|
2008-06-03 18:41:54 +02:00
|
|
|
* \todo Remove GC.
|
2008-06-03 16:32:23 +02:00
|
|
|
*/
|
|
|
|
void
|
2009-01-16 10:31:13 +01:00
|
|
|
widget_render(wibox_t *wibox)
|
2008-06-03 16:32:23 +02:00
|
|
|
{
|
2009-01-16 10:31:13 +01:00
|
|
|
draw_context_t *ctx = &wibox->sw.ctx;
|
2008-06-03 16:32:23 +02:00
|
|
|
int left = 0, right = 0;
|
2008-06-22 22:09:48 +02:00
|
|
|
area_t rectangle = { 0, 0, 0, 0 };
|
2009-04-17 18:40:43 +02:00
|
|
|
color_t col;
|
2008-06-03 16:32:23 +02:00
|
|
|
|
|
|
|
rectangle.width = ctx->width;
|
|
|
|
rectangle.height = ctx->height;
|
|
|
|
|
|
|
|
if(ctx->bg.alpha != 0xffff)
|
|
|
|
{
|
2009-01-16 10:31:13 +01:00
|
|
|
int x = wibox->sw.geometry.x, y = wibox->sw.geometry.y;
|
2008-10-22 17:32:17 +02:00
|
|
|
xcb_get_property_reply_t *prop_r;
|
|
|
|
char *data;
|
|
|
|
xcb_pixmap_t rootpix;
|
|
|
|
xcb_get_property_cookie_t prop_c;
|
|
|
|
xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
|
2008-06-30 18:55:14 +02:00
|
|
|
prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
|
2008-06-17 21:23:38 +02:00
|
|
|
PIXMAP, 0, 1);
|
2008-06-03 16:32:23 +02:00
|
|
|
if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
|
|
|
|
{
|
2008-08-05 11:26:06 +02:00
|
|
|
if(prop_r->value_len
|
|
|
|
&& (data = xcb_get_property_value(prop_r))
|
2008-06-15 16:14:39 +02:00
|
|
|
&& (rootpix = *(xcb_pixmap_t *) data))
|
2009-01-16 10:31:13 +01:00
|
|
|
switch(wibox->sw.orientation)
|
2008-06-03 16:32:23 +02:00
|
|
|
{
|
2008-09-24 12:13:21 +02:00
|
|
|
case North:
|
2008-06-03 16:32:23 +02:00
|
|
|
draw_rotate(ctx,
|
2008-06-04 16:13:41 +02:00
|
|
|
rootpix, ctx->pixmap,
|
2008-06-08 10:59:13 +02:00
|
|
|
s->width_in_pixels, s->height_in_pixels,
|
2008-06-03 16:32:23 +02:00
|
|
|
ctx->width, ctx->height,
|
|
|
|
M_PI_2,
|
|
|
|
y + ctx->width,
|
|
|
|
- x);
|
|
|
|
break;
|
2008-09-24 12:13:21 +02:00
|
|
|
case South:
|
2008-06-03 16:32:23 +02:00
|
|
|
draw_rotate(ctx,
|
2008-06-04 16:13:41 +02:00
|
|
|
rootpix, ctx->pixmap,
|
2008-06-08 10:59:13 +02:00
|
|
|
s->width_in_pixels, s->height_in_pixels,
|
2008-06-03 16:32:23 +02:00
|
|
|
ctx->width, ctx->height,
|
|
|
|
- M_PI_2,
|
|
|
|
- y,
|
|
|
|
x + ctx->height);
|
|
|
|
break;
|
2008-09-24 12:13:21 +02:00
|
|
|
case East:
|
2008-06-03 18:41:54 +02:00
|
|
|
xcb_copy_area(globalconf.connection, rootpix,
|
2009-01-16 10:31:13 +01:00
|
|
|
wibox->sw.pixmap, wibox->sw.gc,
|
2008-06-03 18:41:54 +02:00
|
|
|
x, y,
|
2008-08-21 15:08:56 +02:00
|
|
|
0, 0,
|
2008-06-03 18:41:54 +02:00
|
|
|
ctx->width, ctx->height);
|
2008-06-03 16:32:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p_delete(&prop_r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 10:31:13 +01:00
|
|
|
widget_node_array_t *widgets = &wibox->widgets;
|
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
/* compute geometry */
|
2008-10-21 17:21:21 +02:00
|
|
|
for(int i = 0; i < widgets->len; i++)
|
2009-04-09 15:12:17 +02:00
|
|
|
if(widgets->tab[i].widget->align == AlignLeft
|
|
|
|
&& widgets->tab[i].widget->isvisible)
|
2008-10-22 17:32:17 +02:00
|
|
|
{
|
|
|
|
widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
2009-01-16 10:31:13 +01:00
|
|
|
wibox->screen, ctx->height,
|
2008-10-22 17:32:17 +02:00
|
|
|
ctx->width - (left + right));
|
|
|
|
widgets->tab[i].geometry.x = left;
|
|
|
|
left += widgets->tab[i].geometry.width;
|
|
|
|
}
|
2008-06-03 16:32:23 +02:00
|
|
|
|
2008-10-21 17:45:05 +02:00
|
|
|
for(int i = widgets->len - 1; i >= 0; i--)
|
2008-10-21 17:21:21 +02:00
|
|
|
if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
|
2008-10-22 17:32:17 +02:00
|
|
|
{
|
|
|
|
widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
2009-01-16 10:31:13 +01:00
|
|
|
wibox->screen, ctx->height,
|
2008-10-22 17:32:17 +02:00
|
|
|
ctx->width - (left + right));
|
|
|
|
right += widgets->tab[i].geometry.width;
|
|
|
|
widgets->tab[i].geometry.x = ctx->width - right;
|
|
|
|
}
|
2008-06-03 16:32:23 +02:00
|
|
|
|
2008-10-23 17:28:07 +02:00
|
|
|
/* save left value */
|
|
|
|
int fake_left = left;
|
|
|
|
|
2008-11-27 12:17:02 +01:00
|
|
|
/* compute width of flex or fixed aligned widgets */
|
2008-10-19 19:14:55 +02:00
|
|
|
int flex = 0;
|
2008-10-21 17:21:21 +02:00
|
|
|
for(int i = 0; i < widgets->len; i++)
|
2008-11-27 12:17:02 +01:00
|
|
|
if(widgets->tab[i].widget->align & (AlignFlex | AlignFixed)
|
2008-10-23 17:28:07 +02:00
|
|
|
&& widgets->tab[i].widget->isvisible)
|
|
|
|
{
|
2008-11-27 12:17:02 +01:00
|
|
|
if(widgets->tab[i].widget->align_supported & AlignFlex
|
|
|
|
&& widgets->tab[i].widget->align == AlignFlex)
|
2008-10-23 17:28:07 +02:00
|
|
|
flex++;
|
|
|
|
else
|
|
|
|
fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
2009-01-16 10:31:13 +01:00
|
|
|
wibox->screen, ctx->height,
|
2008-10-23 17:28:07 +02:00
|
|
|
ctx->width - (fake_left + right)).width;
|
|
|
|
}
|
2008-10-19 19:14:55 +02:00
|
|
|
|
2008-10-23 17:28:07 +02:00
|
|
|
/* now compute everybody together! */
|
|
|
|
int flex_rendered = 0;
|
|
|
|
for(int i = 0; i < widgets->len; i++)
|
2008-11-27 12:17:02 +01:00
|
|
|
if(widgets->tab[i].widget->align & (AlignFlex | AlignFixed)
|
2008-10-23 17:28:07 +02:00
|
|
|
&& widgets->tab[i].widget->isvisible)
|
|
|
|
{
|
2008-11-27 12:17:02 +01:00
|
|
|
if(widgets->tab[i].widget->align_supported & AlignFlex
|
|
|
|
&& widgets->tab[i].widget->align == AlignFlex)
|
2008-10-22 17:32:17 +02:00
|
|
|
{
|
2008-10-23 17:28:07 +02:00
|
|
|
int width = (ctx->width - (right + fake_left)) / flex;
|
|
|
|
/* give last pixels to last flex to be rendered */
|
|
|
|
if(flex_rendered == flex - 1)
|
|
|
|
width += (ctx->width - (right + fake_left)) % flex;
|
2008-10-22 17:32:17 +02:00
|
|
|
widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
2009-01-16 10:31:13 +01:00
|
|
|
wibox->screen, ctx->height,
|
2008-10-23 17:28:07 +02:00
|
|
|
width);
|
|
|
|
flex_rendered++;
|
2008-10-22 17:32:17 +02:00
|
|
|
}
|
2008-10-23 17:28:07 +02:00
|
|
|
else
|
|
|
|
widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
2009-01-16 10:31:13 +01:00
|
|
|
wibox->screen, ctx->height,
|
2008-11-27 12:17:02 +01:00
|
|
|
ctx->width - (left + right));
|
2008-10-23 17:28:07 +02:00
|
|
|
widgets->tab[i].geometry.x = left;
|
|
|
|
left += widgets->tab[i].geometry.width;
|
|
|
|
}
|
2008-06-03 16:32:23 +02:00
|
|
|
|
2009-03-03 10:22:39 +01:00
|
|
|
/* draw background image, only if the background color is not opaque */
|
|
|
|
if(wibox->bg_image && ctx->bg.alpha != 0xffff)
|
|
|
|
draw_image(ctx, 0, 0, 1.0, wibox->bg_image);
|
|
|
|
|
|
|
|
/* draw background color */
|
2009-04-17 18:40:43 +02:00
|
|
|
xcolor_to_color(&ctx->bg, &col);
|
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, &col);
|
2008-10-22 17:32:17 +02:00
|
|
|
|
2009-03-03 10:22:39 +01:00
|
|
|
/* draw everything! */
|
2008-10-22 17:32:17 +02:00
|
|
|
for(int i = 0; i < widgets->len; i++)
|
|
|
|
if(widgets->tab[i].widget->isvisible)
|
|
|
|
{
|
|
|
|
widgets->tab[i].geometry.y = 0;
|
|
|
|
widgets->tab[i].widget->draw(widgets->tab[i].widget,
|
2009-04-17 16:14:09 +02:00
|
|
|
ctx, widgets->tab[i].geometry, wibox);
|
2008-10-22 17:32:17 +02:00
|
|
|
}
|
|
|
|
|
2009-01-16 10:31:13 +01:00
|
|
|
switch(wibox->sw.orientation)
|
2008-06-03 16:32:23 +02:00
|
|
|
{
|
2008-09-24 12:13:21 +02:00
|
|
|
case South:
|
2009-01-16 10:31:13 +01:00
|
|
|
draw_rotate(ctx, ctx->pixmap, wibox->sw.pixmap,
|
2008-06-03 16:32:23 +02:00
|
|
|
ctx->width, ctx->height,
|
|
|
|
ctx->height, ctx->width,
|
|
|
|
M_PI_2, ctx->height, 0);
|
|
|
|
break;
|
2008-09-24 12:13:21 +02:00
|
|
|
case North:
|
2009-01-16 10:31:13 +01:00
|
|
|
draw_rotate(ctx, ctx->pixmap, wibox->sw.pixmap,
|
2008-06-03 16:32:23 +02:00
|
|
|
ctx->width, ctx->height,
|
|
|
|
ctx->height, ctx->width,
|
|
|
|
- M_PI_2, 0, ctx->width);
|
|
|
|
break;
|
2008-09-24 12:13:21 +02:00
|
|
|
case East:
|
2008-06-03 16:32:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 13:45:08 +01:00
|
|
|
/** Invalidate widgets which should be refresh depending on their types.
|
2009-04-17 16:14:09 +02:00
|
|
|
* \param screen Virtual screen.
|
2008-11-12 13:45:08 +01:00
|
|
|
* \param type Widget type to invalidate.
|
2008-03-06 11:41:26 +01:00
|
|
|
*/
|
2008-01-07 18:12:38 +01:00
|
|
|
void
|
2009-04-17 16:14:09 +02:00
|
|
|
widget_invalidate_bytype(screen_t *screen, widget_constructor_t *type)
|
2008-01-07 18:12:38 +01:00
|
|
|
{
|
2009-04-17 16:14:09 +02:00
|
|
|
foreach(wibox, screen->wiboxes)
|
2009-04-11 00:27:06 +02:00
|
|
|
foreach(wnode, (*wibox)->widgets)
|
|
|
|
if(wnode->widget->type == type)
|
2008-06-09 21:43:09 +02:00
|
|
|
{
|
2009-04-11 00:27:06 +02:00
|
|
|
(*wibox)->need_update = true;
|
2008-06-09 21:43:09 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-01-07 18:12:38 +01:00
|
|
|
}
|
|
|
|
|
2008-09-24 12:13:21 +02:00
|
|
|
/** Set a wibox needs update because it has widget, or redraw a titlebar.
|
2008-06-03 18:41:54 +02:00
|
|
|
* \todo Probably needs more optimization.
|
2008-05-28 09:12:48 +02:00
|
|
|
* \param widget The widget to look for.
|
|
|
|
*/
|
2008-06-25 17:47:51 +02:00
|
|
|
void
|
2008-06-03 18:41:54 +02:00
|
|
|
widget_invalidate_bywidget(widget_t *widget)
|
2007-12-16 10:25:13 +01:00
|
|
|
{
|
2009-04-17 16:14:09 +02:00
|
|
|
foreach(screen, globalconf.screens)
|
|
|
|
foreach(wibox, screen->wiboxes)
|
|
|
|
if(!(*wibox)->need_update)
|
|
|
|
foreach(wnode, (*wibox)->widgets)
|
|
|
|
if(wnode->widget == widget)
|
2008-08-06 14:24:54 +02:00
|
|
|
{
|
2009-04-17 16:14:09 +02:00
|
|
|
(*wibox)->need_update = true;
|
2008-08-06 14:24:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-06-03 18:41:54 +02:00
|
|
|
|
2009-04-09 17:17:10 +02:00
|
|
|
foreach(_c, globalconf.clients)
|
|
|
|
{
|
|
|
|
client_t *c = *_c;
|
2008-08-06 14:24:54 +02:00
|
|
|
if(c->titlebar && !c->titlebar->need_update)
|
2008-10-21 17:21:21 +02:00
|
|
|
for(int j = 0; j < c->titlebar->widgets.len; j++)
|
|
|
|
if(c->titlebar->widgets.tab[j].widget == widget)
|
2008-10-19 19:14:55 +02:00
|
|
|
{
|
2008-07-31 14:10:54 +02:00
|
|
|
c->titlebar->need_update = true;
|
2008-10-19 19:14:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-04-09 17:17:10 +02:00
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
|
2008-05-28 09:12:48 +02:00
|
|
|
/** Create a new widget.
|
2008-06-10 20:12:51 +02:00
|
|
|
* \param L The Lua VM state.
|
|
|
|
*
|
|
|
|
* \luastack
|
2008-11-12 11:48:51 +01:00
|
|
|
* \lparam A table with at least a type value. Optional attributes
|
2008-05-28 09:12:48 +02:00
|
|
|
* are: align.
|
2008-06-10 20:12:51 +02:00
|
|
|
* \lreturn A brand new widget.
|
2008-05-28 09:12:48 +02:00
|
|
|
*/
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
|
|
|
luaA_widget_new(lua_State *L)
|
|
|
|
{
|
2008-11-12 14:55:30 +01:00
|
|
|
const char *align, *type;
|
|
|
|
widget_t *w;
|
2008-05-25 14:07:01 +02:00
|
|
|
widget_constructor_t *wc;
|
2008-06-23 14:01:33 +02:00
|
|
|
size_t len;
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-06-30 14:01:55 +02:00
|
|
|
luaA_checktable(L, 2);
|
2008-06-30 13:50:49 +02:00
|
|
|
|
2008-11-12 15:07:24 +01:00
|
|
|
type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2008-11-12 15:07:24 +01:00
|
|
|
if((wc = name_func_lookup(type, len, WidgetList)))
|
2008-11-12 14:55:30 +01:00
|
|
|
{
|
2009-04-09 15:12:17 +02:00
|
|
|
w = widget_new(L);
|
2008-11-12 14:55:30 +01:00
|
|
|
wc(w);
|
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
else
|
2008-11-05 11:16:09 +01:00
|
|
|
{
|
|
|
|
luaA_warn(L, "unkown widget type: %s", type);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-06-14 18:12:16 +02:00
|
|
|
w->type = wc;
|
2008-11-12 15:07:24 +01:00
|
|
|
|
|
|
|
align = luaA_getopt_lstring(L, 2, "align", "left", &len);
|
2008-11-27 12:17:02 +01:00
|
|
|
w->align_supported |= AlignLeft | AlignRight | AlignFixed;
|
2008-11-12 14:55:30 +01:00
|
|
|
w->align = draw_align_fromstr(align, len);
|
2008-11-12 14:41:26 +01:00
|
|
|
|
2008-05-23 19:29:55 +02:00
|
|
|
/* Set visible by default. */
|
|
|
|
w->isvisible = true;
|
2007-12-17 10:57:17 +01:00
|
|
|
|
2008-08-25 14:34:56 +02:00
|
|
|
w->mouse_enter = w->mouse_leave = LUA_REFNIL;
|
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
return 1;
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
2008-04-03 14:00:46 +02:00
|
|
|
|
2008-08-12 13:23:10 +02:00
|
|
|
/** Get or set mouse buttons bindings to a widget.
|
2008-06-10 20:12:51 +02:00
|
|
|
* \param L The Lua VM state.
|
|
|
|
*
|
|
|
|
* \luastack
|
2008-06-11 02:46:30 +02:00
|
|
|
* \lvalue A widget.
|
2008-08-12 13:23:10 +02:00
|
|
|
* \lparam An array of mouse button bindings objects, or nothing.
|
|
|
|
* \return The array of mouse button bindings objects of this widget.
|
2008-05-28 09:12:48 +02:00
|
|
|
*/
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
2008-08-12 13:23:10 +02:00
|
|
|
luaA_widget_buttons(lua_State *L)
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
|
|
|
button_array_t *buttons = &widget->buttons;
|
2008-06-13 15:35:47 +02:00
|
|
|
|
2008-08-12 13:23:10 +02:00
|
|
|
if(lua_gettop(L) == 2)
|
2008-10-24 17:48:50 +02:00
|
|
|
{
|
2008-08-12 13:23:10 +02:00
|
|
|
luaA_button_array_set(L, 2, buttons);
|
2008-10-24 17:48:50 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2008-06-13 15:35:47 +02:00
|
|
|
|
2008-08-12 13:23:10 +02:00
|
|
|
return luaA_button_array_get(L, buttons);
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
|
2008-07-28 16:03:38 +02:00
|
|
|
/** Generic widget.
|
2008-06-27 12:43:39 +02:00
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
2008-07-02 10:34:14 +02:00
|
|
|
* \luastack
|
2009-01-19 17:17:15 +01:00
|
|
|
* \lfield align The widget alignment.
|
2008-07-02 10:34:14 +02:00
|
|
|
* \lfield visible The widget visibility.
|
2008-08-25 14:34:56 +02:00
|
|
|
* \lfield mouse_enter A function to execute when the mouse enter the widget.
|
|
|
|
* \lfield mouse_leave A function to execute when the mouse leave the widget.
|
2008-06-27 12:43:39 +02:00
|
|
|
*/
|
2008-06-25 16:37:51 +02:00
|
|
|
static int
|
|
|
|
luaA_widget_index(lua_State *L)
|
|
|
|
{
|
2008-07-01 15:20:42 +02:00
|
|
|
size_t len;
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
2008-07-01 15:20:42 +02:00
|
|
|
const char *buf = luaL_checklstring(L, 2, &len);
|
2008-07-01 15:27:41 +02:00
|
|
|
awesome_token_t token;
|
2008-06-25 16:37:51 +02:00
|
|
|
|
2008-07-01 19:25:58 +02:00
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
2008-06-25 16:37:51 +02:00
|
|
|
return 1;
|
2008-06-30 14:01:55 +02:00
|
|
|
|
2008-07-01 15:27:41 +02:00
|
|
|
switch((token = a_tokenize(buf, len)))
|
2008-07-01 15:20:42 +02:00
|
|
|
{
|
2009-01-19 17:17:15 +01:00
|
|
|
case A_TK_ALIGN:
|
2009-04-09 15:12:17 +02:00
|
|
|
lua_pushstring(L, draw_align_tostr(widget->align));
|
2009-01-19 17:17:15 +01:00
|
|
|
return 1;
|
2008-07-01 15:20:42 +02:00
|
|
|
case A_TK_VISIBLE:
|
2009-04-09 15:12:17 +02:00
|
|
|
lua_pushboolean(L, widget->isvisible);
|
2008-07-01 15:20:42 +02:00
|
|
|
return 1;
|
2008-08-25 14:34:56 +02:00
|
|
|
case A_TK_MOUSE_ENTER:
|
2009-04-09 15:12:17 +02:00
|
|
|
if(widget->mouse_enter != LUA_REFNIL)
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, widget->mouse_enter);
|
2008-10-21 10:35:23 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2008-08-25 14:34:56 +02:00
|
|
|
return 1;
|
|
|
|
case A_TK_MOUSE_LEAVE:
|
2009-04-09 15:12:17 +02:00
|
|
|
if(widget->mouse_leave != LUA_REFNIL)
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, widget->mouse_leave);
|
2008-10-21 10:35:23 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2008-08-25 14:34:56 +02:00
|
|
|
return 1;
|
2008-07-01 15:20:42 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
return widget->index ? widget->index(L, token) : 0;
|
2008-06-25 16:37:51 +02:00
|
|
|
}
|
|
|
|
|
2008-06-27 12:43:39 +02:00
|
|
|
/** Generic widget newindex.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_widget_newindex(lua_State *L)
|
|
|
|
{
|
2008-07-01 15:20:42 +02:00
|
|
|
size_t len;
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
2008-07-01 15:20:42 +02:00
|
|
|
const char *buf = luaL_checklstring(L, 2, &len);
|
2008-07-01 15:27:41 +02:00
|
|
|
awesome_token_t token;
|
2008-06-27 12:43:39 +02:00
|
|
|
|
2008-07-01 15:27:41 +02:00
|
|
|
switch((token = a_tokenize(buf, len)))
|
2008-07-01 15:20:42 +02:00
|
|
|
{
|
2009-01-19 17:17:15 +01:00
|
|
|
case A_TK_ALIGN:
|
|
|
|
buf = luaL_checklstring(L, 3, &len);
|
2009-04-09 15:12:17 +02:00
|
|
|
widget->align = draw_align_fromstr(buf, len);
|
2009-01-19 17:17:15 +01:00
|
|
|
break;
|
2008-07-01 15:20:42 +02:00
|
|
|
case A_TK_VISIBLE:
|
2009-04-09 15:12:17 +02:00
|
|
|
widget->isvisible = luaA_checkboolean(L, 3);
|
2008-08-25 14:19:36 +02:00
|
|
|
break;
|
2008-08-25 14:34:56 +02:00
|
|
|
case A_TK_MOUSE_ENTER:
|
2009-04-09 15:12:17 +02:00
|
|
|
luaA_registerfct(L, 3, &widget->mouse_enter);
|
2009-01-19 17:15:58 +01:00
|
|
|
return 0;
|
2008-08-25 14:34:56 +02:00
|
|
|
case A_TK_MOUSE_LEAVE:
|
2009-04-09 15:12:17 +02:00
|
|
|
luaA_registerfct(L, 3, &widget->mouse_leave);
|
2009-01-19 17:15:58 +01:00
|
|
|
return 0;
|
2008-07-01 15:20:42 +02:00
|
|
|
default:
|
2009-04-09 15:12:17 +02:00
|
|
|
return widget->newindex ? widget->newindex(L, token) : 0;
|
2008-07-01 15:20:42 +02:00
|
|
|
}
|
2008-06-27 12:43:39 +02:00
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_invalidate_bywidget(widget);
|
2008-08-21 15:08:56 +02:00
|
|
|
|
|
|
|
return 0;
|
2008-06-27 12:43:39 +02:00
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
const struct luaL_reg awesome_widget_methods[] =
|
|
|
|
{
|
2008-06-30 14:01:55 +02:00
|
|
|
{ "__call", luaA_widget_new },
|
2008-05-20 15:39:47 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
const struct luaL_reg awesome_widget_meta[] =
|
|
|
|
{
|
2008-08-12 13:23:10 +02:00
|
|
|
{ "buttons", luaA_widget_buttons },
|
2008-06-25 16:37:51 +02:00
|
|
|
{ "__index", luaA_widget_index },
|
2008-06-27 12:43:39 +02:00
|
|
|
{ "__newindex", luaA_widget_newindex },
|
2008-05-20 15:39:47 +02:00
|
|
|
{ "__gc", luaA_widget_gc },
|
|
|
|
{ "__tostring", luaA_widget_tostring },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|