From 407dee6650c6fbb8506146eda91428155890c623 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sun, 20 Apr 2014 04:51:19 +1000 Subject: Fix up widget action stuff. --- ClientHamr/GuiLua/GuiLua.c | 76 ++++++++++++++++++++++++++++++++------------ ClientHamr/GuiLua/skang.lua | 15 +++++---- ClientHamr/GuiLua/test.lua | 5 +-- ClientHamr/GuiLua/test.skang | 4 +-- 4 files changed, 69 insertions(+), 31 deletions(-) (limited to 'ClientHamr/GuiLua') diff --git a/ClientHamr/GuiLua/GuiLua.c b/ClientHamr/GuiLua/GuiLua.c index 69fef14..9339552 100644 --- a/ClientHamr/GuiLua/GuiLua.c +++ b/ClientHamr/GuiLua/GuiLua.c @@ -557,10 +557,35 @@ win.quitter.colour.r = 5 -> direct access to the table, well "direct" via Th */ +struct _Widget +{ + char magic[8]; + char *action; + Evas_Object *obj; +}; + +static void _on_click(void *data, Evas_Object *obj, void *event_info EINA_UNUSED) +{ + globals *ourGlobals; + lua_State *L = data; + struct _Widget *wid; + + lua_getfield(L, LUA_REGISTRYINDEX, globName); + ourGlobals = lua_touserdata(L, -1); + lua_pop(L, 1); + + wid = evas_object_data_get(obj, "Widget"); + if (wid) + { + PD("Doing action %s", wid->action); + if (0 != luaL_dostring(L, wid->action)) + PE("Error running - %s", wid->action); + } +} + static int widget(lua_State *L) { globals *ourGlobals; - Evas_Object *bt; char *type = "label"; char *title = ":"; int x = 1, y = 1, w = WIDTH/3, h = HEIGHT/3; @@ -574,34 +599,44 @@ static int widget(lua_State *L) // Poor mans introspection, until I write real introspection into EFL. if (strcmp(type, "button") == 0) { - bt = elm_button_add(ourGlobals->win); - elm_object_text_set(bt, title); - evas_object_smart_callback_add(bt, "clicked", _on_done, ourGlobals); - evas_object_resize(bt, w, h); - evas_object_move(bt, x, y); - evas_object_show(bt); - lua_pushlightuserdata(L, &bt); + struct _Widget *wid; + + wid = calloc(1, sizeof(struct _Widget)); + strcpy(wid->magic, "Widget"); + wid->obj = elm_button_add(ourGlobals->win); + elm_object_text_set(wid->obj, title); + evas_object_smart_callback_add(wid->obj, "clicked", _on_click, L); + evas_object_resize(wid->obj, w, h); + evas_object_move(wid->obj, x, y); + evas_object_show(wid->obj); + evas_object_data_set(wid->obj, "Widget", wid); + /* Evas_Object *bt isn't a real pointer it seems. At least Lua bitches about it - + PANIC: unprotected error in call to Lua API (bad light userdata pointer) + So we wrap it. + */ + lua_pushlightuserdata(L, (void *) wid); return 1; } return 0; } -static void _on_click(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) -{ - // TODO, pull the action out of the widget, then somehow magically send it to Lua. -} - static int action(lua_State *L) { - Evas_Object *obj= lua_touserdata(L, 1); + globals *ourGlobals; + struct _Widget *wid = lua_touserdata(L, 1); char *action = "nada"; - pull_lua(L, 2, "$", &action); -printf("Setting action %s\n", action); - // TODO - stuff the action into the widget somewhere. - evas_object_smart_callback_add(obj, "clicked", _on_click, L); + lua_getfield(L, LUA_REGISTRYINDEX, globName); + ourGlobals = lua_touserdata(L, -1); + lua_pop(L, 1); + pull_lua(L, 2, "$", &action); + if (wid && strcmp(wid->magic, "Widget") == 0) + { + PD("Setting action %s", action); + wid->action = strdup(action); + } return 0; } @@ -795,12 +830,13 @@ void GuiLuaDo(int argc, char **argv) lua_getglobal(L, "require"); lua_pushstring(L, SKANG); lua_call(L, 1, 1); - lua_pop(L, 1); // Ignore the returned value. + lua_setfield(L, LUA_GLOBALSINDEX, SKANG); // Run the main loop via a Lua call. // This does nothing if no module opened a window. - lua_pop(L, loopWindow(L)); + if (0 != luaL_dostring(L, "skang.loopWindow()")) + PEm("Error running - skang.loopWindow()"); lua_pop(L, closeWindow(L)); lua_close(L); } diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index e6b6725..fdd61de 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua @@ -881,7 +881,9 @@ thingasm = function (names, ...) local args, err = loadstring('return ' .. thingy.widget) if args then setfenv(args, parent) - parent.W[name] = {widget = widget(args())} + local result = widget(args()) +print('NO IDEA WHY this does isValid() three times on the action, and the first one being a string.') + parent.W[name] = {Cwidget = result} else print("ERROR - " .. err) end @@ -1025,21 +1027,21 @@ thingasm('nada', 'Do nothing.', function () --[[ This function intentionally lef local widgets = {} --thingasm{widgets, 'window', 'The window.', types='userdata'} thingasm{widgets, 'W', 'Holds all the widgets', types='Keyed'} -widgets.W{'widget', 'The widget.', types='userdata'} +widgets.W{'Cwidget', 'The widget.', types='userdata'} widgets.W{'action,a', 'The action for the widget.', 'nada', types='string'} local aIsValid = function (self, parent) local result = Thing.isValid(self, parent) if result then local value = parent[self.names[1] ] -print('NEW ACTION - ' .. self.names[1] .. ' = ' .. value) ---printTableStart(parent, '', 'parent') - action(parent.widget, value) +print('NEW ACTION - ' .. self.names[1] .. ' = ' .. value .. ' ' .. type(parent.Cwidget)) + action(parent.Cwidget, value) end return result end widgets.W{'look,l', 'The look of the widget.', types='string'} +--[[ widgets.W{'colour,color,c', 'The colours of the widget.', types='table'} widgets.W.c{'red,r', 'The red colour of the widget.', 255, types='number'} widgets.W.c{'green,g', 'The green colour of the widget.', 255, types='number'} @@ -1051,11 +1053,12 @@ local wMum = getmetatable(widgets.W.c) wMum.__call = function (func, ...) return Colour(func, ...) end +]] window = function(w, h, title, name) name = name or 'myWindow' local win = {} - win.W = copy(widgets.W, name) + win = copy(widgets, name) local wMum, wThingy = getStuffed(win.W, 'a') wThingy.isValid = aIsValid win.window = Cwindow(w, h, title, name) diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index a7e3b7f..65e686f 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua @@ -193,14 +193,15 @@ print(stuff.s.sa) print(stuff.s.sb) print('') -skang.printTableStart(stuff.s, '', 'stuff.s') -skang.printTableStart(stuff.S, '', 'stuff.S') +--skang.printTableStart(stuff.s, '', 'stuff.s') +--skang.printTableStart(stuff.S, '', 'stuff.S') --skang.printTableStart(getmetatable(stuff.S), '', 'stuff.S metatable') print(stuff.S['record0'].field1) print(stuff.S['record1'].field0) print(stuff.S['record2'].field1) +--skang.printTableStart(stuff.S['record0'], '', 'stuff.S[record0]') --skang.printTableStart(getmetatable(stuff.S['record0']), '', 'metatable stuff.S[record0]') --skang.printTableStart(getmetatable(stuff.S['record1']), '', 'metatable stuff.S[record1]') --skang.printTableStart(getmetatable(stuff.S['record2']), '', 'metatable stuff.S[record2]') diff --git a/ClientHamr/GuiLua/test.skang b/ClientHamr/GuiLua/test.skang index 340bb08..0c37f92 100644 --- a/ClientHamr/GuiLua/test.skang +++ b/ClientHamr/GuiLua/test.skang @@ -8,9 +8,7 @@ -- This is a bit more verbose than I wanted. lol local win = skang.window(500, 500, "G'day planet.", 'testWindow') skang.thingasm{win, 'quitter', 'Quits the skang window', types = 'widget', widget='"button", "Quit", 10, 10, 100, 50'} -skang.printTableStart(win.W.quitter, '', 'quitter') -win.W.quitter.action = 1 -win.W.quitter.action = 'quit' -- 'quit' is looked up in ThingSpace.commands, and translated into the Lua 'skang.quit()'. +win.W.quitter.action = 'skang.quit()' -- TODO Should look it up in ThingSpace.commands, and translat 'quit' into the Lua 'skang.quit()'? --other.foo = 'stuff' this.bar = 'things' -- cgit v1.1