From a440dab39d8e0c38483a035e2c9a1d566cedd94f Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 1 Sep 2010 16:11:48 +0200 Subject: [PATCH] widgets: Use strings instead of tokens Signed-off-by: Uli Schlachter --- objects/widget.c | 12 ++-- objects/widget.h | 4 +- objects/widgets/imagebox.c | 43 ++++++-------- objects/widgets/textbox.c | 114 +++++++++++++++++-------------------- 4 files changed, 76 insertions(+), 97 deletions(-) diff --git a/objects/widget.c b/objects/widget.c index 54e2eec2c..866f8633b 100644 --- a/objects/widget.c +++ b/objects/widget.c @@ -437,9 +437,7 @@ luaA_widget_buttons(lua_State *L) static int luaA_widget_index(lua_State *L) { - size_t len; - const char *prop = luaL_checklstring(L, 2, &len); - awesome_token_t token = a_tokenize(prop, len); + const char *prop = luaL_checkstring(L, 2); /* Try standard method */ if(luaA_class_index(L)) @@ -447,7 +445,7 @@ luaA_widget_index(lua_State *L) /* Then call special widget index */ widget_t *widget = luaA_checkudata(L, 1, &widget_class); - return widget->index ? widget->index(L, token) : 0; + return widget->index ? widget->index(L, prop) : 0; } /** Generic widget newindex. @@ -457,16 +455,14 @@ luaA_widget_index(lua_State *L) static int luaA_widget_newindex(lua_State *L) { - size_t len; - const char *prop = luaL_checklstring(L, 2, &len); - awesome_token_t token = a_tokenize(prop, len); + const char *prop = luaL_checkstring(L, 2); /* Try standard method */ luaA_class_newindex(L); /* Then call special widget newindex */ widget_t *widget = luaA_checkudata(L, 1, &widget_class); - return widget->newindex ? widget->newindex(L, token) : 0; + return widget->newindex ? widget->newindex(L, prop) : 0; } static int diff --git a/objects/widget.h b/objects/widget.h index 7f715d66f..eb4b43248 100644 --- a/objects/widget.h +++ b/objects/widget.h @@ -43,9 +43,9 @@ struct widget_t /** Draw function */ void (*draw)(widget_t *, draw_context_t *, area_t, wibox_t *); /** Index function */ - int (*index)(lua_State *, awesome_token_t); + int (*index)(lua_State *, const char *); /** Newindex function */ - int (*newindex)(lua_State *, awesome_token_t); + int (*newindex)(lua_State *, const char *); /** Misc private data */ void *data; /** Button bindings */ diff --git a/objects/widgets/imagebox.c b/objects/widgets/imagebox.c index b01ef8cfa..2cc34ecaa 100644 --- a/objects/widgets/imagebox.c +++ b/objects/widgets/imagebox.c @@ -84,7 +84,7 @@ imagebox_destructor(widget_t *w) /** Imagebox widget. * \param L The Lua VM state. - * \param token The key token. + * \param prop The key that is being indexed. * \param resize Resize image. * \return The number of elements pushed on stack. * \luastack @@ -92,25 +92,19 @@ imagebox_destructor(widget_t *w) * \lfield bg The background color to use. */ static int -luaA_imagebox_index(lua_State *L, awesome_token_t token) +luaA_imagebox_index(lua_State *L, const char *prop) { widget_t *widget = luaA_checkudata(L, 1, &widget_class); imagebox_data_t *d = widget->data; - switch(token) - { - case A_TK_IMAGE: + if(a_strcmp(prop, "image") == 0) luaA_object_push_item(L, 1, d->image); - break; - case A_TK_BG: + else if(a_strcmp(prop, "bg") == 0) luaA_pushcolor(L, &d->bg); - break; - case A_TK_RESIZE: + else if(a_strcmp(prop, "resize") == 0) lua_pushboolean(L, d->resize); - break; - default: + else return 0; - } return 1; } @@ -121,33 +115,30 @@ luaA_imagebox_index(lua_State *L, awesome_token_t token) * \return The number of elements pushed on stack. */ static int -luaA_imagebox_newindex(lua_State *L, awesome_token_t token) +luaA_imagebox_newindex(lua_State *L, const char *prop) { widget_t *widget = luaA_checkudata(L, 1, &widget_class); imagebox_data_t *d = widget->data; - switch(token) + if(a_strcmp(prop, "image") == 0) { - const char *buf; - size_t len; - - case A_TK_IMAGE: luaA_checkudataornil(L, -1, &image_class); luaA_object_unref_item(L, 1, d->image); d->image = luaA_object_ref_item(L, 1, 3); - break; - case A_TK_BG: + } + else if(a_strcmp(prop, "bg") == 0) + { + const char *buf; + size_t len; if(lua_isnil(L, 3)) p_clear(&d->bg, 1); else if((buf = luaL_checklstring(L, 3, &len))) color_init_reply(color_init_unchecked(&d->bg, buf, len)); - break; - case A_TK_RESIZE: - d->resize = luaA_checkboolean(L, 3); - break; - default: - return 0; } + else if(a_strcmp(prop, "resize") == 0) + d->resize = luaA_checkboolean(L, 3); + else + return 0; widget_invalidate_bywidget(widget); diff --git a/objects/widgets/textbox.c b/objects/widgets/textbox.c index c4b1cd221..486d08992 100644 --- a/objects/widgets/textbox.c +++ b/objects/widgets/textbox.c @@ -232,7 +232,7 @@ luaA_textbox_margin(lua_State *L) /** Textbox widget. * \param L The Lua VM state. - * \param token The key token. + * \param prop The property's name. * \return The number of elements pushed on stack. * \luastack * \lfield text The text to display. @@ -249,52 +249,43 @@ luaA_textbox_margin(lua_State *L) * \lfield bg_resize Background resize. */ static int -luaA_textbox_index(lua_State *L, awesome_token_t token) +luaA_textbox_index(lua_State *L, const char *prop) { widget_t *widget = luaA_checkudata(L, 1, &widget_class); textbox_data_t *d = widget->data; - switch(token) - { - case A_TK_BG_RESIZE: + if(a_strcmp(prop, "bg_resize") == 0) lua_pushboolean(L, d->bg_resize); - return 1; - case A_TK_BG_ALIGN: + else if(a_strcmp(prop, "bg_align") == 0) lua_pushstring(L, draw_align_tostr(d->bg_align)); - return 1; - case A_TK_BG_IMAGE: + else if(a_strcmp(prop, "bg_image") == 0) return luaA_object_push(L, d->bg_image); - case A_TK_BG: + else if(a_strcmp(prop, "bg") == 0) return luaA_pushcolor(L, &d->bg); - case A_TK_MARGIN: + else if(a_strcmp(prop, "margin") == 0) lua_pushcfunction(L, luaA_textbox_margin); - return 1; - case A_TK_ALIGN: + else if(a_strcmp(prop, "align") == 0) lua_pushstring(L, draw_align_tostr(d->align)); - return 1; - case A_TK_VALIGN: + else if(a_strcmp(prop, "valign") == 0) lua_pushstring(L, draw_align_tostr(d->valign)); - return 1; - case A_TK_BORDER_WIDTH: + else if(a_strcmp(prop, "border_width") == 0) lua_pushnumber(L, d->border.width); - return 1; - case A_TK_BORDER_COLOR: + else if(a_strcmp(prop, "border_color") == 0) luaA_pushcolor(L, &d->border.color); - return 1; - case A_TK_TEXT: + else if(a_strcmp(prop, "text") == 0) + { if(d->text_len > 0) { lua_pushlstring(L, d->text, d->text_len); return 1; } return 0; - case A_TK_WIDTH: + } + else if(a_strcmp(prop, "width") == 0) lua_pushnumber(L, d->width); - return 1; - case A_TK_HEIGHT: + else if(a_strcmp(prop, "height") == 0) lua_pushnumber(L, d->height); - return 1; - case A_TK_WRAP: + else if(a_strcmp(prop, "wrap") == 0) switch(d->wrap) { default: @@ -307,8 +298,7 @@ luaA_textbox_index(lua_State *L, awesome_token_t token) lua_pushliteral(L, "word_char"); break; } - return 1; - case A_TK_ELLIPSIZE: + else if(a_strcmp(prop, "ellipsize") == 0) switch(d->ellip) { case PANGO_ELLIPSIZE_START: @@ -321,10 +311,10 @@ luaA_textbox_index(lua_State *L, awesome_token_t token) lua_pushliteral(L, "end"); break; } - return 1; - default: + else return 0; - } + + return 1; } /** The __newindex method for a textbox object. @@ -333,49 +323,52 @@ luaA_textbox_index(lua_State *L, awesome_token_t token) * \return The number of elements pushed on stack. */ static int -luaA_textbox_newindex(lua_State *L, awesome_token_t token) +luaA_textbox_newindex(lua_State *L, const char *prop) { size_t len = 0; widget_t *widget = luaA_checkudata(L, 1, &widget_class); const char *buf = NULL; textbox_data_t *d = widget->data; - switch(token) + if(a_strcmp(prop, "bg_align") == 0) { - case A_TK_BG_ALIGN: buf = luaL_checklstring(L, 3, &len); d->bg_align = draw_align_fromstr(buf, len); - break; - case A_TK_BG_RESIZE: + } + else if(a_strcmp(prop, "bg_resize") == 0) d->bg_resize = luaA_checkboolean(L, 3); - break; - case A_TK_BG_IMAGE: + else if(a_strcmp(prop, "bg_image") == 0) + { luaA_checkudataornil(L, -1, &image_class); luaA_object_unref_item(L, 1, d->bg_image); d->bg_image = luaA_object_ref_item(L, 1, 3); - break; - case A_TK_BG: + } + else if(a_strcmp(prop, "bg") == 0) + { if(lua_isnil(L, 3)) p_clear(&d->bg, 1); else if((buf = luaL_checklstring(L, 3, &len))) color_init_reply(color_init_unchecked(&d->bg, buf, len)); - break; - case A_TK_ALIGN: + } + else if(a_strcmp(prop, "align") == 0) + { if((buf = luaL_checklstring(L, 3, &len))) d->align = draw_align_fromstr(buf, len); - break; - case A_TK_VALIGN: + } + else if(a_strcmp(prop, "valign") == 0) + { if((buf = luaL_checklstring(L, 3, &len))) d->valign = draw_align_fromstr(buf, len); - break; - case A_TK_BORDER_COLOR: + } + else if(a_strcmp(prop, "border_color") == 0) + { if((buf = luaL_checklstring(L, 3, &len))) color_init_reply(color_init_unchecked(&d->border.color, buf, len)); - break; - case A_TK_BORDER_WIDTH: + } + else if(a_strcmp(prop, "border_width") == 0) d->border.width = luaL_checknumber(L, 3); - break; - case A_TK_TEXT: + else if(a_strcmp(prop, "text") == 0) + { if(lua_isnil(L, 3) || (buf = luaL_checklstring(L, 3, &len))) { @@ -420,14 +413,13 @@ luaA_textbox_newindex(lua_State *L, awesome_token_t token) else p_clear(&d->extents, 1); } - break; - case A_TK_WIDTH: + } + else if(a_strcmp(prop, "width") == 0) d->width = luaL_checknumber(L, 3); - break; - case A_TK_HEIGHT: + else if(a_strcmp(prop, "height") == 0) d->height = luaL_checknumber(L, 3); - break; - case A_TK_WRAP: + else if(a_strcmp(prop, "wrap") == 0) + { if((buf = luaL_checkstring(L, 3))) { if(a_strcmp(buf, "word") == 0) @@ -437,8 +429,9 @@ luaA_textbox_newindex(lua_State *L, awesome_token_t token) else if(a_strcmp(buf, "word_char") == 0) d->wrap = PANGO_WRAP_WORD_CHAR; } - break; - case A_TK_ELLIPSIZE: + } + else if(a_strcmp(prop, "ellipsize") == 0) + { if((buf = luaL_checkstring(L, 3))) { if(a_strcmp(buf, "start") == 0) @@ -448,10 +441,9 @@ luaA_textbox_newindex(lua_State *L, awesome_token_t token) else if(a_strcmp(buf, "end") == 0) d->ellip = PANGO_ELLIPSIZE_END; } - break; - default: - return 0; } + else + return 0; widget_invalidate_bywidget(widget);