widgets: generic set/get for list

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-08-19 19:13:46 +02:00
parent 145e19b2e6
commit d261f9a3cc
6 changed files with 80 additions and 78 deletions

View File

@ -70,7 +70,6 @@ true
urgent
visible
vertical
widgets
width
workarea
yes

View File

@ -1419,7 +1419,7 @@ end
-- @param c The client to update.
function titlebar.update(c)
if c.titlebar and titlebar.data[c] then
local widgets = c.titlebar.widgets
local widgets = c.titlebar:widgets()
local title, close
for k, v in ipairs(widgets) do
if v.name == "title" then title = v end

View File

@ -541,49 +541,14 @@ static int
luaA_statusbar_widgets(lua_State *L)
{
statusbar_t **statusbar = luaA_checkudata(L, 1, "statusbar");
widget_node_t *witer;
if(lua_gettop(L) == 2)
{
luaA_checktable(L, 2);
/* remove all widgets */
for(witer = (*statusbar)->widgets; witer; witer = (*statusbar)->widgets)
{
if(witer->widget->detach)
witer->widget->detach(witer->widget, *statusbar);
widget_unref(&witer->widget);
widget_node_list_detach(&(*statusbar)->widgets, witer);
p_delete(&witer);
}
luaA_widget_set(L, 2, *statusbar, &(*statusbar)->widgets);
(*statusbar)->need_update = true;
/* now read all widgets and add them */
lua_pushnil(L);
while(lua_next(L, 2))
{
widget_t **widget = luaA_checkudata(L, -1, "widget");
widget_node_t *w = p_new(widget_node_t, 1);
w->widget = *widget;
widget_node_list_append(&(*statusbar)->widgets, w);
widget_ref(widget);
lua_pop(L, 1);
}
lua_pop(L, 1);
return 1;
}
else
{
int i = 0;
lua_newtable(L);
for(witer = (*statusbar)->widgets; witer; witer = witer->next)
{
luaA_widget_userdata_new(L, witer->widget);
lua_rawseti(L, -2, ++i);
}
}
return 1;
return luaA_widget_get(L, (*statusbar)->widgets);
}
/** Remove a statubar from a screen.

View File

@ -372,7 +372,6 @@ luaA_titlebar_newindex(lua_State *L)
const char *buf, *attr = luaL_checklstring(L, 2, &len);
client_t *c = NULL, **newc;
int i;
widget_node_t *witer;
position_t position;
switch(a_tokenize(attr, len))
@ -449,33 +448,6 @@ luaA_titlebar_newindex(lua_State *L)
globalconf.default_screen,
buf, len)))
(*titlebar)->need_update = true;
return 0;
case A_TK_WIDGETS:
luaA_checktable(L, 3);
/* remove all widgets */
for(witer = (*titlebar)->widgets; witer; witer = (*titlebar)->widgets)
{
if(witer->widget->detach)
witer->widget->detach(witer->widget, *titlebar);
widget_unref(&witer->widget);
widget_node_list_detach(&(*titlebar)->widgets, witer);
p_delete(&witer);
}
(*titlebar)->need_update = true;
/* now read all widgets and add them */
lua_pushnil(L);
while(lua_next(L, 3))
{
widget_t **widget = luaA_checkudata(L, -1, "widget");
widget_node_t *w = p_new(widget_node_t, 1);
w->widget = *widget;
widget_node_list_append(&(*titlebar)->widgets, w);
widget_ref(widget);
lua_pop(L, 1);
}
break;
case A_TK_POSITION:
buf = luaL_checklstring(L, 3, &len);
@ -518,8 +490,6 @@ luaA_titlebar_index(lua_State *L)
titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
const char *attr = luaL_checklstring(L, 2, &len);
client_t *c;
widget_node_t *witer;
int i = 0;
if(luaA_usemetatable(L, 1, 2))
return 1;
@ -546,14 +516,6 @@ luaA_titlebar_index(lua_State *L)
case A_TK_BG:
luaA_pushcolor(L, &(*titlebar)->colors.bg);
break;
case A_TK_WIDGETS:
lua_newtable(L);
for(witer = (*titlebar)->widgets; witer; witer = witer->next)
{
luaA_widget_userdata_new(L, witer->widget);
lua_rawseti(L, -2, ++i);
}
break;
case A_TK_POSITION:
lua_pushstring(L, position_tostr((*titlebar)->position));
break;
@ -564,6 +526,20 @@ luaA_titlebar_index(lua_State *L)
return 1;
}
static int
luaA_titlebar_widgets(lua_State *L)
{
titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
if(lua_gettop(L) == 2)
{
luaA_widget_set(L, 2, *titlebar, &(*titlebar)->widgets);
(*titlebar)->need_update = true;
return 1;
}
return luaA_widget_get(L, (*titlebar)->widgets);
}
/** Convert a titlebar to a printable string.
* \param L The Lua VM state.
* \return The number of value pushed.
@ -587,6 +563,7 @@ const struct luaL_reg awesome_titlebar_methods[] =
};
const struct luaL_reg awesome_titlebar_meta[] =
{
{ "widgets", luaA_titlebar_widgets },
{ "__index", luaA_titlebar_index },
{ "__newindex", luaA_titlebar_newindex },
{ "__eq", luaA_titlebar_eq },

View File

@ -401,6 +401,65 @@ luaA_widget_newindex(lua_State *L)
return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
}
/** Generic widget set.
* \param L The Lua VM state.
* \param idx The table of widgets index.
* \param object The object the widget will be attached to.
* \param widgets The widgets to fill.
* \return The number of elements pushed on stack.
*/
void
luaA_widget_set(lua_State *L, int idx, void *object, widget_node_t **widgets)
{
widget_node_t *witer;
luaA_checktable(L, idx);
/* remove all widgets */
for(witer = *widgets; witer; witer = *widgets)
{
if(witer->widget->detach)
witer->widget->detach(witer->widget, object);
widget_unref(&witer->widget);
widget_node_list_detach(widgets, witer);
p_delete(&witer);
}
/* now read all widgets and add them */
lua_pushnil(L);
while(lua_next(L, idx))
{
widget_t **widget = luaA_checkudata(L, -1, "widget");
widget_node_t *w = p_new(widget_node_t, 1);
w->widget = *widget;
widget_node_list_append(widgets, w);
widget_ref(widget);
lua_pop(L, 1);
}
}
/** Generic widget get.
* \param L The Lua VM state.
* \param widget The widget list.
* \return The number of elements pushed on stack.
*/
int
luaA_widget_get(lua_State *L, widget_node_t *widgets)
{
widget_node_t *witer;
int i = 0;
lua_newtable(L);
for(witer = widgets; witer; witer = witer->next)
{
luaA_widget_userdata_new(L, witer->widget);
lua_rawseti(L, -2, ++i);
}
return 1;
}
const struct luaL_reg awesome_widget_methods[] =
{
{ "__call", luaA_widget_new },

View File

@ -32,6 +32,8 @@ void widget_invalidate_cache(int, int);
int widget_calculate_offset(int, int, int, int);
void widget_common_new(widget_t *);
void widget_render(widget_node_t *, draw_context_t *, xcb_gcontext_t, xcb_drawable_t, int, position_t, int, int, void *, awesome_type_t);
void luaA_widget_set(lua_State *, int, void *, widget_node_t **);
int luaA_widget_get(lua_State *, widget_node_t *);
int luaA_widget_userdata_new(lua_State *, widget_t *);