From 04878851af3717aa8b0755e0bc6275a6f5b490c8 Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Fri, 22 Feb 2019 15:59:09 +0100 Subject: [PATCH] Lua API --- plugins/Example/main.lua | 3 +- src/plugin/Plugin.cpp | 9 ++++- src/plugin/luapi_application.h | 71 ++++++++++++++++------------------ src/util/XojMsgBox.cpp | 18 ++------- src/util/XojMsgBox.h | 11 ++---- 5 files changed, 48 insertions(+), 64 deletions(-) diff --git a/plugins/Example/main.lua b/plugins/Example/main.lua index 4a599d82..9090e7d9 100644 --- a/plugins/Example/main.lua +++ b/plugins/Example/main.lua @@ -15,5 +15,6 @@ end -- Callback if the menu item is executed function exampleCallback() - app.msgbox("Test123", "yes,no"); + result = app.msgbox("Test123", {[1] = "Yes", [2] = "No"}); + print("result = " .. result) end diff --git a/src/plugin/Plugin.cpp b/src/plugin/Plugin.cpp index 074942ee..93f2769c 100644 --- a/src/plugin/Plugin.cpp +++ b/src/plugin/Plugin.cpp @@ -1,6 +1,7 @@ #include "Plugin.h" #include +#include #ifdef ENABLE_PLUGINS @@ -315,7 +316,9 @@ void Plugin::loadScript() if (lua_pcall(lua, 0, 0, 0) != LUA_OK) { const char* errMsg = lua_tostring(lua, -1); - XojMsgBox::showPluginMessage(name, errMsg, MSG_BT_OK, true); + map button; + button.insert(std::pair(0, _("OK"))); + XojMsgBox::showPluginMessage(name, errMsg, button, true); g_warning("Could not run plugin Lua file: «%s», error: «%s»", luafile.c_str(), errMsg); this->valid = false; @@ -334,7 +337,9 @@ bool Plugin::callFunction(string fnc) if (lua_pcall(lua, 0, 0, 0)) { const char* errMsg = lua_tostring(lua, -1); - XojMsgBox::showPluginMessage(name, errMsg, MSG_BT_OK, true); + map button; + button.insert(std::pair(0, _("OK"))); + XojMsgBox::showPluginMessage(name, errMsg, button, true); g_warning("Error in Plugin: «%s», error: «%s»", name.c_str(), errMsg); return false; diff --git a/src/plugin/luapi_application.h b/src/plugin/luapi_application.h index 0e798e9c..61895114 100644 --- a/src/plugin/luapi_application.h +++ b/src/plugin/luapi_application.h @@ -12,43 +12,38 @@ #include #include +#include +using std::map; + /** * Example: - * app.msgbox("Test123", "yes,no") + * app.msgbox("Test123", {[1] = "Yes", [2] = "No"}) + * Return 1 for yes, 2 for no in this example */ static int applib_msgbox(lua_State* L) { const char* msg = luaL_checkstring(L, 1); - const char* flags = luaL_checkstring(L, 2); - int type = 0; - for (string element : StringUtils::split(flags, ',')) + // discard any extra arguments passed in + lua_settop(L, 2); + luaL_checktype(L, 2, LUA_TTABLE); + + lua_pushnil(L); + + map button; + + while (lua_next(L, 2) != 0) { - if (element == "ok") - { - type |= MSG_BT_OK; - } - else if (element == "yes") - { - type |= MSG_BT_YES; - } - else if (element == "no") - { - type |= MSG_BT_NO; - } - else if (element == "cancel") - { - type |= MSG_BT_CANCEL; - } - else - { - g_warning("Plugin: Unsupported button type for app.msgbox «%s»", element.c_str()); - } + int index = lua_tointeger(L, -2); + const char* buttonText = luaL_checkstring(L, -1); + lua_pop(L, 1); + + button.insert(button.begin(), std::pair(index, buttonText)); } Plugin* plugin = Plugin::getPluginFromLua(L); - int result = XojMsgBox::showPluginMessage(plugin->getName(), msg, type); + int result = XojMsgBox::showPluginMessage(plugin->getName(), msg, button); lua_pushinteger(L, result); return 1; } @@ -76,7 +71,7 @@ static int applib_registerUi(lua_State* L) lua_getfield(L, 1, "menu"); lua_getfield(L, 1, "callback"); // stack now has following: - // 1 = {"menu"="MenuName", callback="functionName"} + // 1 = {"menu"="MenuName", callback="functionName"} // -2 = "MenuName" // -1 = "functionName" @@ -119,10 +114,10 @@ static const luaL_Reg applib[] = { { "registerUi", applib_registerUi }, // Placeholder - {"MSG_BT_OK", NULL}, - {"MSG_BT_YES", NULL}, - {"MSG_BT_NO", NULL}, - {"MSG_BT_CANCEL", NULL}, +// {"MSG_BT_OK", NULL}, +// {"MSG_BT_YES", NULL}, +// {"MSG_BT_NO", NULL}, +// {"MSG_BT_CANCEL", NULL}, {NULL, NULL} }; @@ -133,14 +128,14 @@ static const luaL_Reg applib[] = { LUAMOD_API int luaopen_app(lua_State* L) { luaL_newlib(L, applib); - lua_pushnumber(L, MSG_BT_OK); - lua_setfield(L, -2, "MSG_BT_OK"); - lua_pushnumber(L, MSG_BT_YES); - lua_setfield(L, -2, "MSG_BT_YES"); - lua_pushnumber(L, MSG_BT_NO); - lua_setfield(L, -2, "MSG_BT_NO"); - lua_pushnumber(L, MSG_BT_CANCEL); - lua_setfield(L, -2, "MSG_BT_CANCEL"); +// lua_pushnumber(L, MSG_BT_OK); +// lua_setfield(L, -2, "MSG_BT_OK"); +// lua_pushnumber(L, MSG_BT_YES); +// lua_setfield(L, -2, "MSG_BT_YES"); +// lua_pushnumber(L, MSG_BT_NO); +// lua_setfield(L, -2, "MSG_BT_NO"); +// lua_pushnumber(L, MSG_BT_CANCEL); +// lua_setfield(L, -2, "MSG_BT_CANCEL"); return 1; } diff --git a/src/util/XojMsgBox.cpp b/src/util/XojMsgBox.cpp index 614f0efb..33fd9700 100644 --- a/src/util/XojMsgBox.cpp +++ b/src/util/XojMsgBox.cpp @@ -30,7 +30,7 @@ void XojMsgBox::showErrorToUser(GtkWindow* win, string msg) gtk_widget_destroy(dialog); } -int XojMsgBox::showPluginMessage(string pluginName, string msg, int type, bool error) +int XojMsgBox::showPluginMessage(string pluginName, string msg, map button, bool error) { string header = string("Xournal++ Plugin «") + pluginName + "»"; @@ -52,21 +52,9 @@ int XojMsgBox::showPluginMessage(string pluginName, string msg, int type, bool e g_object_set_property(G_OBJECT(dialog), "secondary-text", &val); g_value_unset(&val); - if (MSG_BT_OK & type) + for (auto& kv : button) { - gtk_dialog_add_button(GTK_DIALOG(dialog), _("OK"), MSG_BT_OK); - } - if (MSG_BT_YES & type) - { - gtk_dialog_add_button(GTK_DIALOG(dialog), _("Yes"), MSG_BT_YES); - } - if (MSG_BT_NO & type) - { - gtk_dialog_add_button(GTK_DIALOG(dialog), _("No"), MSG_BT_NO); - } - if (MSG_BT_CANCEL & type) - { - gtk_dialog_add_button(GTK_DIALOG(dialog), _("Cancel"), MSG_BT_CANCEL); + gtk_dialog_add_button(GTK_DIALOG(dialog), kv.second.c_str(), kv.first); } int res = gtk_dialog_run(GTK_DIALOG(dialog)); diff --git a/src/util/XojMsgBox.h b/src/util/XojMsgBox.h index d5c04dcb..7b9bde3c 100644 --- a/src/util/XojMsgBox.h +++ b/src/util/XojMsgBox.h @@ -15,13 +15,8 @@ #include using std::string; - -enum MsgBoxButtonType { - MSG_BT_OK = 1 << 0, - MSG_BT_YES = 1 << 1, - MSG_BT_NO = 1 << 2, - MSG_BT_CANCEL = 1 << 3 -}; +#include +using std::map; class XojMsgBox { @@ -36,7 +31,7 @@ public: static void setDefaultWindow(GtkWindow* win); static void showErrorToUser(GtkWindow* win, string msg); - static int showPluginMessage(string pluginName, string msg, int type, bool error = false); + static int showPluginMessage(string pluginName, string msg, map button, bool error = false); static int replaceFileQuestion(GtkWindow* win, string msg); static void showHelp(GtkWindow* win); };