presentation
Andreas Butti 7 years ago
parent d918ab4ea4
commit 04878851af
  1. 3
      plugins/Example/main.lua
  2. 9
      src/plugin/Plugin.cpp
  3. 71
      src/plugin/luapi_application.h
  4. 18
      src/util/XojMsgBox.cpp
  5. 11
      src/util/XojMsgBox.h

@ -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

@ -1,6 +1,7 @@
#include "Plugin.h"
#include <config.h>
#include <i18n.h>
#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<int, string> button;
button.insert(std::pair<int, string>(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<int, string> button;
button.insert(std::pair<int, string>(0, _("OK")));
XojMsgBox::showPluginMessage(name, errMsg, button, true);
g_warning("Error in Plugin: «%s», error: «%s»", name.c_str(), errMsg);
return false;

@ -12,43 +12,38 @@
#include <XojMsgBox.h>
#include <StringUtils.h>
#include <map>
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<int, string> 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<int, string>(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;
}

@ -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<int, string> 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));

@ -15,13 +15,8 @@
#include <string>
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 <map>
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<int, string> button, bool error = false);
static int replaceFileQuestion(GtkWindow* win, string msg);
static void showHelp(GtkWindow* win);
};

Loading…
Cancel
Save