From e5d828b369dbaea206fb090a9392bff996e8e1fa Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Fri, 8 Aug 2008 15:47:09 +0200 Subject: [PATCH] statusbar: widgets is now an array Signed-off-by: Julien Danjou --- awesomerc.lua.in | 16 ++--- common/tokenize.gperf | 1 + statusbar.c | 133 +++++++++++++----------------------------- 3 files changed, 52 insertions(+), 98 deletions(-) diff --git a/awesomerc.lua.in b/awesomerc.lua.in index 4489047bf..97b1ecd2f 100644 --- a/awesomerc.lua.in +++ b/awesomerc.lua.in @@ -138,15 +138,17 @@ for s = 1, screen.count() do mystatusbar[s] = statusbar({ position = "top", name = "mystatusbar" .. s, fg = beautiful.fg_normal, bg = beautiful.bg_normal }) -- Add widgets to the statusbar - order matters - mystatusbar[s]:widget_add(mytaglist) - mystatusbar[s]:widget_add(myiconbox) - mystatusbar[s]:widget_add(mytasklist) - mystatusbar[s]:widget_add(mypromptbox) - mystatusbar[s]:widget_add(mytextbox) - mystatusbar[s]:widget_add(mylayoutbox[s]) + mystatusbar[s].widgets = + { + mytaglist, + mytasklist, + mypromptbox, + mytextbox, + mylayoutbox[s] + } mystatusbar[s].screen = s end -mystatusbar[screen.count()]:widget_add(mysystray) +table.insert(mystatusbar[screen.count()].widgets, mysystray) -- }}} -- {{{ Mouse bindings diff --git a/common/tokenize.gperf b/common/tokenize.gperf index 41c9f0024..018f3c6ca 100644 --- a/common/tokenize.gperf +++ b/common/tokenize.gperf @@ -66,5 +66,6 @@ true urgent visible vertical +widgets width yes diff --git a/statusbar.c b/statusbar.c index 65efc069d..9aca6b25c 100644 --- a/statusbar.c +++ b/statusbar.c @@ -187,7 +187,7 @@ statusbar_draw(statusbar_t *statusbar) statusbar, AWESOME_TYPE_STATUSBAR); simplewindow_refresh_pixmap(statusbar->sw); } - + statusbar_systray_refresh(statusbar); } @@ -425,66 +425,6 @@ luaA_statusbar_tostring(lua_State *L) return 1; } -/** Add a widget to a statusbar. - * \param L The Lua VM state. - * - * \luastack - * \lvalue A statusbar. - * \lparam A widget. - */ -static int -luaA_statusbar_widget_add(lua_State *L) -{ - statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); - widget_t **widget = luaA_checkudata(L, 2, "widget"); - widget_node_t *witer, *w = p_new(widget_node_t, 1); - - /* check that there is not already a widget with that name in the titlebar */ - for(witer = (*sb)->widgets; witer; witer = witer->next) - if(witer->widget != *widget - && !a_strcmp(witer->widget->name, (*widget)->name)) - luaL_error(L, "a widget with name `%s' already on statusbar `%s'", - witer->widget->name, (*sb)->name); - - (*sb)->need_update = true; - w->widget = *widget; - widget_node_list_append(&(*sb)->widgets, w); - widget_ref(widget); - - return 0; -} - -/** Remove a widget from a statusbar. - * \param L The Lua VM State. - * - * \luastack - * \lvalue A statusbar. - * \lparam A widget. - */ -static int -luaA_statusbar_widget_remove(lua_State *L) -{ - statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); - widget_t **widget = luaA_checkudata(L, 2, "widget"); - widget_node_t *w, *wnext; - - for(w = (*sb)->widgets; w; w = wnext) - { - wnext = w->next; - if(w->widget == *widget) - { - if((*widget)->detach) - (*widget)->detach(*widget, *sb); - widget_unref(widget); - widget_node_list_detach(&(*sb)->widgets, w); - p_delete(&w); - (*sb)->need_update = true; - } - } - - return 0; -} - /** Create a new statusbar. * \param L The Lua VM state. * @@ -538,32 +478,6 @@ luaA_statusbar_new(lua_State *L) return luaA_statusbar_userdata_new(L, sb); } -/** Get all widget from a statusbar. - * \param L The Lua VM state. - * - * \luastack - * \lvalue A statusbar. - * \lreturn A table with all widgets from the statusbar. - */ -static int -luaA_statusbar_widget_get(lua_State *L) -{ - statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); - widget_node_t *witer; - - lua_newtable(L); - - for(witer = (*sb)->widgets; witer; witer = witer->next) - { - luaA_widget_userdata_new(L, witer->widget); - /* ref again for the list */ - widget_ref(&witer->widget); - lua_setfield(L, -2, witer->widget->name); - } - - return 1; -} - /** Statusbar object. * \param L The Lua VM state. * \return The number of elements pushed on stack. @@ -573,11 +487,14 @@ luaA_statusbar_widget_get(lua_State *L) * \lfield fg Foreground color. * \lfield bg Background color. * \lfield position The position. + * \lfield widget The statusbar widgets. */ static int luaA_statusbar_index(lua_State *L) { size_t len; + int i = 0; + widget_node_t *witer; statusbar_t **statusbar = luaA_checkudata(L, 1, "statusbar"); const char *attr = luaL_checklstring(L, 2, &len); @@ -603,6 +520,14 @@ luaA_statusbar_index(lua_State *L) case A_TK_POSITION: lua_pushstring(L, position_tostr((*statusbar)->position)); break; + case A_TK_WIDGETS: + lua_newtable(L); + for(witer = (*statusbar)->widgets; witer; witer = witer->next) + { + luaA_widget_userdata_new(L, witer->widget); + lua_rawseti(L, -2, ++i); + } + break; default: return 0; } @@ -648,6 +573,7 @@ luaA_statusbar_newindex(lua_State *L) const char *buf, *attr = luaL_checklstring(L, 2, &len); position_t p; int screen; + widget_node_t *witer; switch(a_tokenize(attr, len)) { @@ -657,7 +583,7 @@ luaA_statusbar_newindex(lua_State *L) else { screen = luaL_checknumber(L, 3) - 1; - + luaA_checkscreen(screen); if((*statusbar)->screen == screen) @@ -721,6 +647,34 @@ luaA_statusbar_newindex(lua_State *L) statusbar_position_update(s); ewmh_update_workarea((*statusbar)->phys_screen); } + break; + case A_TK_WIDGETS: + luaA_checktable(L, 3); + + /* 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); + } + + (*statusbar)->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(&(*statusbar)->widgets, w); + widget_ref(widget); + lua_pop(L, 1); + } + break; default: return 0; @@ -736,9 +690,6 @@ const struct luaL_reg awesome_statusbar_methods[] = }; const struct luaL_reg awesome_statusbar_meta[] = { - { "widget_add", luaA_statusbar_widget_add }, - { "widget_remove", luaA_statusbar_widget_remove }, - { "widget_get", luaA_statusbar_widget_get }, { "__index", luaA_statusbar_index }, { "__newindex", luaA_statusbar_newindex }, { "__gc", luaA_statusbar_gc },