diff --git a/plugins/Example/plugin.ini b/plugins/Example/plugin.ini index 6adf9550..c09f1dd6 100644 --- a/plugins/Example/plugin.ini +++ b/plugins/Example/plugin.ini @@ -8,5 +8,8 @@ author=Andreas Butti ## then it gets the same version number version= +[default] +enabled=false + [plugin] mainfile=main.lua diff --git a/plugins/ToggleGrid/plugin.ini b/plugins/ToggleGrid/plugin.ini new file mode 100644 index 00000000..a24df67b --- /dev/null +++ b/plugins/ToggleGrid/plugin.ini @@ -0,0 +1,13 @@ +[about] +## Author / Copyright notice +author=Andreas Butti + +## If the plugin is packed with Xournal++, use +## then it gets the same version number +version= + +[default] +enabled=false + +[plugin] +mainfile=togglegrid.lua diff --git a/plugins/ToggleGrid/togglegrid.lua b/plugins/ToggleGrid/togglegrid.lua new file mode 100644 index 00000000..ec3bfe2b --- /dev/null +++ b/plugins/ToggleGrid/togglegrid.lua @@ -0,0 +1,16 @@ + +-- Register all Toolbar actions and intialize all UI stuff +function initUi() + app.registerUi({["menu"] = "Grid with snapping On", ["callback"] = "toggleGridOn"}); + app.registerUi({["menu"] = "Grid with snapping Off", ["callback"] = "toggleGridOff"}); +end + +function toggleGridOn() + app.uiActionSelected("GROUP_GRID_SNAPPING", "ACTION_GRID_SNAPPING"); + app.changeCurrentPageBackground("graph"); +end + +function toggleGridOff() + app.uiActionSelected("GROUP_GRID_SNAPPING", "ACTION_NONE"); + app.changeCurrentPageBackground("plain"); +end diff --git a/src/control/PageBackgroundChangeController.cpp b/src/control/PageBackgroundChangeController.cpp index 4fcfada6..28aff20a 100644 --- a/src/control/PageBackgroundChangeController.cpp +++ b/src/control/PageBackgroundChangeController.cpp @@ -88,6 +88,13 @@ void PageBackgroundChangeController::changeCurrentPageBackground(PageTypeInfo* i { XOJ_CHECK_TYPE(PageBackgroundChangeController); + changeCurrentPageBackground(info->page); +} + +void PageBackgroundChangeController::changeCurrentPageBackground(PageType& pageType) +{ + XOJ_CHECK_TYPE(PageBackgroundChangeController); + if (ignoreEvent) { return; @@ -116,7 +123,7 @@ void PageBackgroundChangeController::changeCurrentPageBackground(PageTypeInfo* i PageType origType = page->getBackgroundType(); // Apply the new background - applyPageBackground(page, info->page); + applyPageBackground(page, pageType); control->firePageChanged(pageNr); control->updateBackgroundSizeButton(); diff --git a/src/control/PageBackgroundChangeController.h b/src/control/PageBackgroundChangeController.h index 19e4025e..6117feaf 100644 --- a/src/control/PageBackgroundChangeController.h +++ b/src/control/PageBackgroundChangeController.h @@ -29,6 +29,7 @@ public: virtual ~PageBackgroundChangeController(); public: + virtual void changeCurrentPageBackground(PageType& pageType); virtual void changeCurrentPageBackground(PageTypeInfo* info); void changeAllPagesBackground(PageType pt); void insertNewPage(size_t position); diff --git a/src/enums/ActionGroup.enum.h b/src/enums/ActionGroup.enum.h index f312c3d2..4fcc0e02 100644 --- a/src/enums/ActionGroup.enum.h +++ b/src/enums/ActionGroup.enum.h @@ -65,8 +65,6 @@ enum ActionGroup GROUP_LAYOUT_LR, GROUP_LAYOUT_TB, - - }; ActionGroup ActionGroup_fromString(string value); diff --git a/src/gui/toolbarMenubar/ToolButton.cpp b/src/gui/toolbarMenubar/ToolButton.cpp index 38158509..136551ea 100644 --- a/src/gui/toolbarMenubar/ToolButton.cpp +++ b/src/gui/toolbarMenubar/ToolButton.cpp @@ -3,7 +3,8 @@ #include "gui/widgets/gtkmenutooltogglebutton.h" ToolButton::ToolButton(ActionHandler* handler, string id, ActionType type, string iconName, string description, - GtkWidget* menuitem) : AbstractToolItem(id, handler, type, menuitem) + GtkWidget* menuitem) + : AbstractToolItem(id, handler, type, menuitem) { XOJ_INIT_TYPE(ToolButton); @@ -13,7 +14,8 @@ ToolButton::ToolButton(ActionHandler* handler, string id, ActionType type, strin ToolButton::ToolButton(ActionHandler* handler, string id, ActionType type, ActionGroup group, bool toolToggleOnlyEnable, string iconName, string description, - GtkWidget* menuitem) : AbstractToolItem(id, handler, type, menuitem) + GtkWidget* menuitem) + : AbstractToolItem(id, handler, type, menuitem) { XOJ_INIT_TYPE(ToolButton); diff --git a/src/plugin/Plugin.cpp b/src/plugin/Plugin.cpp index 26205481..9d2c3c22 100644 --- a/src/plugin/Plugin.cpp +++ b/src/plugin/Plugin.cpp @@ -31,8 +31,9 @@ static const luaL_Reg loadedlibs[] = { { NULL, NULL } }; -Plugin::Plugin(string name, string path) - : name(name), +Plugin::Plugin(Control* control, string name, string path) + : control(control), + name(name), path(path) { XOJ_INIT_TYPE(Plugin); @@ -205,6 +206,16 @@ int Plugin::registerMenu(string menu, string callback, string accelerator) return menuEntries.size() - 1; } +/** + * @return The main controller + */ +Control* Plugin::getControl() +{ + XOJ_CHECK_TYPE(Plugin); + + return control; +} + /** * Load ini file */ diff --git a/src/plugin/Plugin.h b/src/plugin/Plugin.h index 18851800..9e73e895 100644 --- a/src/plugin/Plugin.h +++ b/src/plugin/Plugin.h @@ -21,6 +21,7 @@ #include class Plugin; +class Control; class MenuEntry { public: @@ -70,7 +71,7 @@ public: class Plugin { public: - Plugin(string name, string path); + Plugin(Control* control, string name, string path); virtual ~Plugin(); public: @@ -111,6 +112,11 @@ public: */ int registerMenu(string menu, string callback, string accelerator); + /** + * @return The main controller + */ + Control* getControl(); + private: /** * Load ini file @@ -181,6 +187,11 @@ private: */ vector menuEntries; + /** + * The main controller + */ + Control* control; + /** * Flag to check if init ui is currently running */ diff --git a/src/plugin/PluginController.cpp b/src/plugin/PluginController.cpp index 50b652fb..cf438a23 100644 --- a/src/plugin/PluginController.cpp +++ b/src/plugin/PluginController.cpp @@ -69,7 +69,7 @@ void PluginController::loadPluginsFrom(string path) pluginFolder += "/"; pluginFolder += file; - Plugin* p = new Plugin(file, pluginFolder); + Plugin* p = new Plugin(control, file, pluginFolder); if (!p->isValid()) { g_warning("Error loading plugin «%s»", file); diff --git a/src/plugin/luapi_application.h b/src/plugin/luapi_application.h index 1c2e34bc..b76aa8e8 100644 --- a/src/plugin/luapi_application.h +++ b/src/plugin/luapi_application.h @@ -12,6 +12,9 @@ #include #include +#include "control/Control.h" +#include "control/PageBackgroundChangeController.h" + #include using std::map; @@ -118,17 +121,103 @@ static int applib_registerUi(lua_State* L) return 1; } +/** + * Execute an UI action (usually internal called from Toolbar / Menu) + */ +static int applib_uiAction(lua_State* L) +{ + Plugin* plugin = Plugin::getPluginFromLua(L); + + // discard any extra arguments passed in + lua_settop(L, 1); + luaL_checktype(L, 1, LUA_TTABLE); + + lua_getfield(L, 1, "group"); + lua_getfield(L, 1, "enabled"); + lua_getfield(L, 1, "action"); + // stack now has following: + // 1 = {["action"] = "ACTION_GRID_SNAPPING", ["group"] = "GROUP_GRID_SNAPPING", ["enabled"] = true} + // -3 = GROUP_GRID_SNAPPING + // -2 = true + // -1 = "ACTION_GRID_SNAPPING" + + bool enabled = true; + + ActionGroup group = GROUP_NOGROUP; + const char* groupStr = luaL_optstring(L, -3, NULL); + if (groupStr != NULL) + { + group = ActionGroup_fromString(groupStr); + } + + if (lua_isboolean(L, -2)) + { + enabled = lua_toboolean(L, -2); + } + + const char* actionStr = luaL_optstring(L, -1, NULL); + if (actionStr == NULL) + { + luaL_error(L, "Missing action!"); + } + + ActionType action = ActionType_fromString(actionStr); + GdkEvent* event = NULL; + GtkMenuItem* menuitem = NULL; + GtkToolButton* toolbutton = NULL; + + Control* ctrl = plugin->getControl(); + ctrl->actionPerformed(action, group, event, menuitem, toolbutton, enabled); + + // Make sure to remove all vars which are put to the stack before! + lua_pop(L, 3); + + return 1; +} + +/** + * Select UI action + */ +static int applib_uiActionSelected(lua_State* L) +{ + Plugin* plugin = Plugin::getPluginFromLua(L); + + ActionGroup group = group = ActionGroup_fromString(luaL_checkstring(L, 1)); + ActionType action = ActionType_fromString(luaL_checkstring(L, 2)); + + Control* ctrl = plugin->getControl(); + ctrl->fireActionSelected(group, action); + + return 1; +} + +/** + * Select UI action + */ +static int applib_changeCurrentPageBackground(lua_State* L) +{ + PageType pt; + pt.format = luaL_checkstring(L, 1); + pt.config = luaL_optstring(L, 2, ""); + + Plugin* plugin = Plugin::getPluginFromLua(L); + Control* ctrl = plugin->getControl(); + PageBackgroundChangeController* pageBgCtrl = ctrl->getPageBackgroundChangeController(); + pageBgCtrl->changeCurrentPageBackground(pt); + + return 1; +} static const luaL_Reg applib[] = { { "msgbox", applib_msgbox }, { "registerUi", applib_registerUi }, + { "uiAction", applib_uiAction }, + { "uiActionSelected", applib_uiActionSelected }, + { "changeCurrentPageBackground", applib_changeCurrentPageBackground }, // Placeholder // {"MSG_BT_OK", NULL}, -// {"MSG_BT_YES", NULL}, -// {"MSG_BT_NO", NULL}, -// {"MSG_BT_CANCEL", NULL}, {NULL, NULL} }; @@ -141,12 +230,6 @@ 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"); return 1; }