Drop the "Lua" in Lua Action Buttons:
[ardour.git] / gtk2_ardour / luainstance.cc
index ff2c8d3164754d3139aca22a783abb9ebfd897ab..8f9572b98e7da461365eaa4c17461bfe7fd41eea 100644 (file)
@@ -20,6 +20,9 @@
 #include <cairomm/surface.h>
 #include <pango/pangocairo.h>
 
+#include "pbd/strsplit.h"
+
+#include "gtkmm2ext/bindings.h"
 #include "gtkmm2ext/gui_thread.h"
 
 #include "ardour/audioengine.h"
@@ -424,6 +427,69 @@ lua_exec (std::string cmd)
        return 0;
 }
 #endif
+
+////////////////////////////////////////////////////////////////////////////////
+
+static int
+lua_actionlist (lua_State *L)
+{
+       using namespace std;
+
+       vector<string> paths;
+       vector<string> labels;
+       vector<string> tooltips;
+       vector<string> keys;
+       vector<Glib::RefPtr<Gtk::Action> > actions;
+       Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
+
+       vector<string>::iterator p;
+       vector<string>::iterator l;
+
+       luabridge::LuaRef action_tbl (luabridge::newTable (L));
+
+       for (l = labels.begin(), p = paths.begin(); l != labels.end(); ++p, ++l) {
+               if (l->empty ()) {
+                       continue;
+               }
+
+               vector<string> parts;
+               split (*p, parts, '/');
+
+               if (parts.empty()) {
+                       continue;
+               }
+
+               //kinda kludgy way to avoid displaying menu items as mappable
+               if (parts[1] == _("Main_menu"))
+                       continue;
+               if (parts[1] == _("JACK"))
+                       continue;
+               if (parts[1] == _("redirectmenu"))
+                       continue;
+               if (parts[1] == _("Editor_menus"))
+                       continue;
+               if (parts[1] == _("RegionList"))
+                       continue;
+               if (parts[1] == _("ProcessorMenu"))
+                       continue;
+
+               /* strip <Actions>/ from the start */
+               string path = (*p);
+               path = path.substr (strlen ("<Actions>/"));
+
+               if (!action_tbl[parts[1]].isTable()) {
+                       action_tbl[parts[1]] = luabridge::newTable (L);
+               }
+               assert (action_tbl[parts[1]].isTable());
+               luabridge::LuaRef tbl (action_tbl[parts[1]]);
+               assert (tbl.isTable());
+               tbl[*l] = path;
+       }
+
+       luabridge::push (L, action_tbl);
+       return 1;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 
 // ARDOUR_UI and instance() are not exposed.
@@ -728,14 +794,14 @@ LuaInstance::register_classes (lua_State* L)
 
                .beginClass <RegionSelection> ("RegionSelection")
                .addFunction ("start", &RegionSelection::start)
-               .addFunction ("end_frame", &RegionSelection::end_frame)
+               .addFunction ("end_sample", &RegionSelection::end_sample)
                .addFunction ("n_midi_regions", &RegionSelection::n_midi_regions)
                .addFunction ("regionlist", &RegionSelection::regionlist) // XXX check windows binding (libardour)
                .endClass ()
 
                .deriveClass <TimeSelection, std::list<ARDOUR::AudioRange> > ("TimeSelection")
                .addFunction ("start", &TimeSelection::start)
-               .addFunction ("end_frame", &TimeSelection::end_frame)
+               .addFunction ("end_sample", &TimeSelection::end_sample)
                .addFunction ("length", &TimeSelection::length)
                .endClass ()
 
@@ -949,6 +1015,8 @@ LuaInstance::register_classes (lua_State* L)
                .addConst ("Add", Selection::Operation(Selection::Add))
                .endNamespace ()
 
+               .addCFunction ("actionlist", &lua_actionlist)
+
                .endNamespace () // end ArdourUI
 
                .beginNamespace ("os")
@@ -1290,7 +1358,7 @@ LuaInstance::interactive_add (LuaScriptInfo::ScriptType type, int id)
        switch (type) {
                case LuaScriptInfo::EditorAction:
                        reg = lua_action_names ();
-                       title = _("Add Lua Action");
+                       title = _("Add Shortcut or Lua Script");
                        break;
                case LuaScriptInfo::EditorHook:
                        reg = lua_slot_names ();
@@ -1323,18 +1391,27 @@ LuaInstance::interactive_add (LuaScriptInfo::ScriptType type, int id)
 
        try {
                script = Glib::file_get_contents (spi->path);
-       } catch (Glib::FileError e) {
+       } catch (Glib::FileError const& e) {
                string msg = string_compose (_("Cannot read script '%1': %2"), spi->path, e.what());
                Gtk::MessageDialog am (msg);
                am.run ();
                return false;
        }
 
-       LuaScriptParamList lsp = LuaScriptParams::script_params (spi, param_function);
+       LuaState ls;
+       register_classes (ls.getState ());
+       LuaScriptParamList lsp = LuaScriptParams::script_params (ls, spi->path, param_function);
+
+       /* allow cancel */
+       for (size_t i = 0; i < lsp.size(); ++i) {
+               if (lsp[i]->preseeded && lsp[i]->name == "x-script-abort") {
+                       return false;
+               }
+       }
 
        ScriptParameterDialog spd (_("Set Script Parameters"), spi, reg, lsp);
 
-       if (!spd.need_interation ()) {
+       if (spd.need_interation ()) {
                switch (spd.run ()) {
                        case Gtk::RESPONSE_ACCEPT:
                                break;
@@ -1343,7 +1420,7 @@ LuaInstance::interactive_add (LuaScriptInfo::ScriptType type, int id)
                }
        }
 
-       LuaScriptParamPtr lspp (new LuaScriptParam("x-script-origin", "", spi->path, false));
+       LuaScriptParamPtr lspp (new LuaScriptParam("x-script-origin", "", spi->path, false, true));
        lsp.push_back (lspp);
 
        switch (type) {
@@ -1360,7 +1437,7 @@ LuaInstance::interactive_add (LuaScriptInfo::ScriptType type, int id)
                                string msg = string_compose (_("Session script '%1' instantiation failed: %2"), spd.name(), e.what ());
                                Gtk::MessageDialog am (msg);
                                am.run ();
-                       } catch (SessionException e) {
+                       } catch (SessionException const& e) {
                                string msg = string_compose (_("Loading Session script '%1' failed: %2"), spd.name(), e.what ());
                                Gtk::MessageDialog am (msg);
                                am.run ();