Merge pull request #921 from andreasb242/scripting

Plugin interface. First usable plugin
presentation
andreasb242 7 years ago committed by GitHub
commit 9fe7446335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      plugins/Example/plugin.ini
  2. 13
      plugins/ToggleGrid/plugin.ini
  3. 16
      plugins/ToggleGrid/togglegrid.lua
  4. 9
      src/control/PageBackgroundChangeController.cpp
  5. 1
      src/control/PageBackgroundChangeController.h
  6. 2
      src/enums/ActionGroup.enum.h
  7. 6
      src/gui/toolbarMenubar/ToolButton.cpp
  8. 15
      src/plugin/Plugin.cpp
  9. 13
      src/plugin/Plugin.h
  10. 2
      src/plugin/PluginController.cpp
  11. 101
      src/plugin/luapi_application.h

@ -8,5 +8,8 @@ author=Andreas Butti
## <xournalpp> then it gets the same version number
version=<xournalpp>
[default]
enabled=false
[plugin]
mainfile=main.lua

@ -0,0 +1,13 @@
[about]
## Author / Copyright notice
author=Andreas Butti
## If the plugin is packed with Xournal++, use
## <xournalpp> then it gets the same version number
version=<xournalpp>
[default]
enabled=false
[plugin]
mainfile=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

@ -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();

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

@ -65,8 +65,6 @@ enum ActionGroup
GROUP_LAYOUT_LR,
GROUP_LAYOUT_TB,
};
ActionGroup ActionGroup_fromString(string value);

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

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

@ -21,6 +21,7 @@
#include <lua.h>
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<MenuEntry*> menuEntries;
/**
* The main controller
*/
Control* control;
/**
* Flag to check if init ui is currently running
*/

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

@ -12,6 +12,9 @@
#include <XojMsgBox.h>
#include <StringUtils.h>
#include "control/Control.h"
#include "control/PageBackgroundChangeController.h"
#include <map>
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;
}

Loading…
Cancel
Save