diff options
author | Luis Ontanon <luis.ontanon@gmail.com> | 2006-02-06 01:29:05 +0000 |
---|---|---|
committer | Luis Ontanon <luis.ontanon@gmail.com> | 2006-02-06 01:29:05 +0000 |
commit | 41a8855e1b67bb3909f8e86a466af397204334ad (patch) | |
tree | 6fec537e644deaf49f096fa8b36cbf13585a5ba5 /plugins | |
parent | fc89f6d4391530b2a36380b6d4136f9caa62fc80 (diff) |
Lua uses the simple dialog
svn path=/trunk/; revision=17173
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/lua/lua_gui.c | 108 | ||||
-rw-r--r-- | plugins/lua/packet-lua.c | 4 | ||||
-rw-r--r-- | plugins/lua/packet-lua.h | 2 | ||||
-rw-r--r-- | plugins/lua/test/simple_dialog.lua | 13 |
4 files changed, 126 insertions, 1 deletions
diff --git a/plugins/lua/lua_gui.c b/plugins/lua/lua_gui.c index 94f01d1019..e80c61fc17 100644 --- a/plugins/lua/lua_gui.c +++ b/plugins/lua/lua_gui.c @@ -95,6 +95,114 @@ extern int lua_register_menu(lua_State* L) { } + + +struct _dlg_cb_data { + lua_State* L; + int func_ref; + int data_ref; +}; + +static int dlg_cb_error_handler(lua_State* L) { + const gchar* error = lua_tostring(L,1); + report_failure("Lua: Error During execution of dialog callback:\n %s",error); + return 0; +} + +static void lua_dialog_cb(gchar** user_input, void* data) { + struct _dlg_cb_data* dcbd = data; + int i = 0; + gchar* input; + lua_State* L = dcbd->L; + + lua_settop(L,0); + lua_pushcfunction(L,dlg_cb_error_handler); + lua_rawgeti(L, LUA_REGISTRYINDEX, dcbd->func_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, dcbd->data_ref); + + for (i = 0; (input = user_input[i]) ; i++) { + lua_pushstring(L,input); + g_free(input); + } + + g_free(user_input); + + switch ( lua_pcall(L,i+1,0,1) ) { + case 0: + break; + case LUA_ERRRUN: + g_warning("Runtime error while calling dialog callback"); + break; + case LUA_ERRMEM: + g_warning("Memory alloc error while calling dialog callback"); + break; + default: + g_assert_not_reached(); + break; + } + +} + +extern int lua_new_dialog(lua_State* L) { + const gchar* title; + int top = lua_gettop(L); + int i; + GPtrArray* labels; + struct _dlg_cb_data* dcbd; + + if (! ops) { + luaL_argerror(L,1,"too early for dialog"); + return 0; + } + + if (! (title = luaL_checkstring(L,1)) ) { + luaL_argerror(L,1,"the title must be a string"); + return 0; + } + + if (! lua_isfunction(L,2)) { + luaL_argerror(L,2,"must be a function"); + return 0; + } + + if (top < 3) { + luaL_error(L,"too few arguments"); + return 0; + } + + + dcbd = g_malloc(sizeof(struct _dlg_cb_data)); + dcbd->L = L; + + lua_remove(L,1); + + lua_pushvalue(L, 1); + dcbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_remove(L,1); + + lua_pushvalue(L, 1); + dcbd->data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_remove(L,1); + + labels = g_ptr_array_new(); + + top -= 3; + + for (i = 1; i <= top; i++) { + gchar* label = (void*)luaL_checkstring(L,i); + g_ptr_array_add(labels,label); + } + + g_ptr_array_add(labels,NULL); + + ops->new_dialog(title, (const gchar**)labels->pdata, lua_dialog_cb, dcbd); + + g_ptr_array_free(labels,TRUE); + + return 0; +} + + /* * TextWindow */ diff --git a/plugins/lua/packet-lua.c b/plugins/lua/packet-lua.c index aafa9f53ef..45cfe93e35 100644 --- a/plugins/lua/packet-lua.c +++ b/plugins/lua/packet-lua.c @@ -295,6 +295,10 @@ void proto_register_lua(void) lua_pushcfunction(L, lua_register_menu); lua_settable(L, LUA_GLOBALSINDEX); + lua_pushstring(L, "dialog"); + lua_pushcfunction(L, lua_new_dialog); + lua_settable(L, LUA_GLOBALSINDEX); + lua_pushstring(L, LUA_HANDOFF_ROUTINES); lua_newtable (L); lua_settable(L, LUA_REGISTRYINDEX); diff --git a/plugins/lua/packet-lua.h b/plugins/lua/packet-lua.h index f4cf6f5925..a83c6d1f32 100644 --- a/plugins/lua/packet-lua.h +++ b/plugins/lua/packet-lua.h @@ -248,7 +248,7 @@ extern void lua_tap_draw(void *tapdata); extern const gchar* lua_shiftstring(lua_State* L,int idx); extern int lua_register_menu(lua_State* L); - +extern int lua_new_dialog(lua_State* L); extern GString* lua_register_all_taps(void); extern void lua_prime_all_fields(proto_tree* tree); diff --git a/plugins/lua/test/simple_dialog.lua b/plugins/lua/test/simple_dialog.lua new file mode 100644 index 0000000000..b10db7694a --- /dev/null +++ b/plugins/lua/test/simple_dialog.lua @@ -0,0 +1,13 @@ +function dialog_menu() + + function dialog_func(datum,a,b,c) + local win = TextWindow.new("Result"); + win:set("one: " .. a .."\ntwo: " .. b .. "\nthree: " .. c .. "\n"); + end + + dialog("Dialog Test",dialog_func,nil,"one","two","three") + +end + +register_menu("Lua Dialog Test",dialog_menu) + |