textbox: use newindex API

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-06-27 20:24:30 +02:00
parent 39e1fc7011
commit 5aa2768861
3 changed files with 24 additions and 71 deletions

View File

@ -96,12 +96,12 @@ mytasklist:text_set({ ["focus"] = "<bg color='"..bg_focus.."'/> <span color='"..
-- Create a textbox widget -- Create a textbox widget
mytextbox = widget.new({ type = "textbox", name = "mytextbox", align = "right" }) mytextbox = widget.new({ type = "textbox", name = "mytextbox", align = "right" })
-- Set the default text in textbox -- Set the default text in textbox
mytextbox:text_set("<b><small> awesome " .. AWESOME_VERSION .. " </small></b>") mytextbox.text = "<b><small> awesome " .. AWESOME_VERSION .. " </small></b>"
mypromptbox = widget.new({ type = "textbox", name = "mypromptbox", align = "left" }) mypromptbox = widget.new({ type = "textbox", name = "mypromptbox", align = "left" })
-- Create an iconbox widget -- Create an iconbox widget
myiconbox = widget.new({ type = "textbox", name = "myiconbox", align = "left" }) myiconbox = widget.new({ type = "textbox", name = "myiconbox", align = "left" })
myiconbox:text_set("<bg image=\"@AWESOME_ICON_PATH@/awesome16.png\" resize=\"true\"/>") myiconbox.text = "<bg image=\"@AWESOME_ICON_PATH@/awesome16.png\" resize=\"true\"/>"
-- Create a systray -- Create a systray
mysystray = widget.new({ type = "systray", name = "mysystray", align = "right" }) mysystray = widget.new({ type = "systray", name = "mysystray", align = "right" })
@ -115,7 +115,7 @@ for s = 1, screen.count() do
mylayoutbox[s]:mouse_add(mouse.new({ }, 3, function () awful.layout.inc(layouts, -1) end)) mylayoutbox[s]:mouse_add(mouse.new({ }, 3, function () awful.layout.inc(layouts, -1) end))
mylayoutbox[s]:mouse_add(mouse.new({ }, 4, function () awful.layout.inc(layouts, 1) end)) mylayoutbox[s]:mouse_add(mouse.new({ }, 4, function () awful.layout.inc(layouts, 1) end))
mylayoutbox[s]:mouse_add(mouse.new({ }, 5, function () awful.layout.inc(layouts, -1) end)) mylayoutbox[s]:mouse_add(mouse.new({ }, 5, function () awful.layout.inc(layouts, -1) end))
mylayoutbox[s]:text_set("<bg image=\"@AWESOME_ICON_PATH@/layouts/tilew.png\" resize=\"true\"/>") mylayoutbox[s].text = "<bg image=\"@AWESOME_ICON_PATH@/layouts/tilew.png\" resize=\"true\"/>"
end end
-- Create a statusbar for each screen and add it -- Create a statusbar for each screen and add it
@ -334,7 +334,7 @@ end
-- (tag switch, new client, etc) -- (tag switch, new client, etc)
function hook_arrange(screen) function hook_arrange(screen)
local layout = awful.layout.get(screen) local layout = awful.layout.get(screen)
mylayoutbox[screen]:text_set("<bg image=\"@AWESOME_ICON_PATH@/layouts/" .. layout .. "w.png\" resize=\"true\"/>") mylayoutbox[screen].text = "<bg image=\"@AWESOME_ICON_PATH@/layouts/" .. layout .. "w.png\" resize=\"true\"/>"
-- Uncomment if you want mouse warping -- Uncomment if you want mouse warping
--[[ --[[
@ -356,9 +356,9 @@ end
-- Hook called every second -- Hook called every second
function hook_timer () function hook_timer ()
-- For unix time_t lovers -- For unix time_t lovers
mytextbox:text_set(" " .. os.time() .. " time_t ") mytextbox.text = " " .. os.time() .. " time_t "
-- Otherwise use: -- Otherwise use:
-- mytextbox:text_set(" " .. os.date() .. " ") -- mytextbox.text = " " .. os.date() .. " "
end end
-- Set up some hooks -- Set up some hooks

View File

@ -618,7 +618,7 @@ function P.prompt(args, textbox, exe_callback, completion_callback)
if not textbox or not exe_callback then if not textbox or not exe_callback then
return return
end end
textbox:text_set(prompt .. prompt_text_with_cursor(text, inv_col, cur_col, cur_pos)) textbox.text = prompt .. prompt_text_with_cursor(text, inv_col, cur_col, cur_pos)
keygrabber.run( keygrabber.run(
function (mod, key) function (mod, key)
local has_ctrl = false local has_ctrl = false
@ -631,16 +631,16 @@ function P.prompt(args, textbox, exe_callback, completion_callback)
-- Get out cases -- Get out cases
if has_ctrl then if has_ctrl then
if key == "g" then if key == "g" then
textbox:text_set("") textbox.text = ""
return false return false
end end
else else
if key == "Return" then if key == "Return" then
textbox:text_set( "") textbox.text = ""
exe_callback(command) exe_callback(command)
return false return false
elseif key == "Escape" then elseif key == "Escape" then
textbox:text_set("") textbox.text = ""
return false return false
end end
end end
@ -716,7 +716,7 @@ function P.prompt(args, textbox, exe_callback, completion_callback)
end end
-- Update textbox -- Update textbox
textbox:text_set(prompt .. prompt_text_with_cursor(command, inv_col, cur_col, cur_pos)) textbox.text = prompt .. prompt_text_with_cursor(command, inv_col, cur_col, cur_pos)
return true return true
end) end)

View File

@ -94,49 +94,6 @@ textbox_destructor(widget_t *w)
p_delete(&d); p_delete(&d);
} }
/** Set the text of a textbox.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lvalue A widget.
* \lparam The text to set.
*/
static int
luaA_textbox_text_set(lua_State *L)
{
widget_t **widget = luaA_checkudata(L, 1, "widget");
const char *text = luaL_checkstring(L, 2);
textbox_data_t *d = (*widget)->data;
p_delete(&d->text);
a_iso2utf8(text, &d->text);
widget_invalidate_bywidget(*widget);
return 0;
}
/** Set the width of a textbox.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lvalue A widget.
* \lparam The width to set.
*/
static int
luaA_textbox_width_set(lua_State *L)
{
widget_t **widget = luaA_checkudata(L, 1, "widget");
int width = luaL_checknumber(L, 2);
textbox_data_t *d = (*widget)->data;
d->width = width;
widget_invalidate_bywidget(*widget);
return 0;
}
/** The __index method for a textbox object. /** The __index method for a textbox object.
* \param L The Lua VM state. * \param L The Lua VM state.
* \return The number of elements pushed on stack. * \return The number of elements pushed on stack.
@ -151,18 +108,17 @@ luaA_textbox_index(lua_State *L)
switch(a_tokenize(attr, len)) switch(a_tokenize(attr, len))
{ {
case A_TK_TEXT_SET:
lua_pushcfunction(L, luaA_textbox_text_set);
return 1;
case A_TK_WIDTH_SET:
lua_pushcfunction(L, luaA_textbox_width_set);
return 1;
case A_TK_TEXT: case A_TK_TEXT:
lua_pushstring(L, d->text); lua_pushstring(L, d->text);
return 1; return 1;
case A_TK_WIDTH:
lua_pushnumber(L, d->width);
return 1;
default: default:
return 0; return 0;
} }
widget_invalidate_bywidget(*widget);
} }
/** The __newindex method for a textbox object. /** The __newindex method for a textbox object.
@ -174,23 +130,27 @@ luaA_textbox_newindex(lua_State *L)
{ {
size_t len; size_t len;
widget_t **widget = luaA_checkudata(L, 1, "widget"); widget_t **widget = luaA_checkudata(L, 1, "widget");
const char *value, *attr = luaL_checklstring(L, 2, &len); const char *buf, *attr = luaL_checklstring(L, 2, &len);
textbox_data_t *d = (*widget)->data; textbox_data_t *d = (*widget)->data;
switch(a_tokenize(attr, len)) switch(a_tokenize(attr, len))
{ {
case A_TK_TEXT: case A_TK_TEXT:
if((value = luaL_checkstring(L, 3))) if((buf = luaL_checkstring(L, 3)))
{ {
p_delete(&d->text); p_delete(&d->text);
d->text = a_strdup(value); a_iso2utf8(buf, &d->text);
widget_invalidate_bywidget(*widget);
} }
break; break;
case A_TK_WIDTH:
d->width = luaL_checknumber(L, 3);
break;
default: default:
return 0; return 0;
} }
widget_invalidate_bywidget(*widget);
return 0; return 0;
} }
@ -216,11 +176,4 @@ textbox_new(alignment_t align)
return w; return w;
} }
/* This is used for building documentation. */
static const struct luaL_reg awesome_textbox_meta[] __attribute__ ((unused)) =
{
{ "text_set", luaA_textbox_text_set },
{ "width_set", luaA_textbox_width_set }
};
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80