Remove dead assignment in Editor::metric_get_bbt
[ardour.git] / gtk2_ardour / luainstance.cc
index c8a4fe9e3b09f9033d1f7e72fee18555d6d85acd..d8a1ce6382d1bf72c310f42aeb7d88bcd706e703 100644 (file)
@@ -34,6 +34,7 @@
 #include "ardour_ui.h"
 #include "public_editor.h"
 #include "region_selection.h"
+#include "luadialog.h"
 #include "luainstance.h"
 #include "luasignal.h"
 #include "marker.h"
@@ -548,6 +549,51 @@ LuaInstance::bind_cairo (lua_State* L)
 
 }
 
+void
+LuaInstance::bind_dialog (lua_State* L)
+{
+       luabridge::getGlobalNamespace (L)
+               .beginNamespace ("LuaDialog")
+
+               .beginClass <LuaDialog::Message> ("Message")
+               .addConstructor <void (*) (std::string const&, std::string const&, LuaDialog::Message::MessageType, LuaDialog::Message::ButtonType)> ()
+               .addFunction ("run", &LuaDialog::Message::run)
+               .endClass ()
+
+               .beginClass <LuaDialog::Dialog> ("Dialog")
+               .addConstructor <void (*) (std::string const&, luabridge::LuaRef)> ()
+               .addCFunction ("run", &LuaDialog::Dialog::run)
+               .endClass ()
+
+               /* enums */
+               .beginNamespace ("MessageType")
+               .addConst ("Info", LuaDialog::Message::Info)
+               .addConst ("Warning", LuaDialog::Message::Warning)
+               .addConst ("Question", LuaDialog::Message::Question)
+               .addConst ("Error", LuaDialog::Message::Error)
+               .endNamespace ()
+
+               .beginNamespace ("ButtonType")
+               .addConst ("OK", LuaDialog::Message::OK)
+               .addConst ("Close", LuaDialog::Message::Close)
+               .addConst ("Cancel", LuaDialog::Message::Cancel)
+               .addConst ("Yes_No", LuaDialog::Message::Yes_No)
+               .addConst ("OK_Cancel", LuaDialog::Message::OK_Cancel)
+               .endNamespace ()
+
+               .beginNamespace ("Response")
+               .addConst ("OK", 0)
+               .addConst ("Cancel", 1)
+               .addConst ("Close", 2)
+               .addConst ("Yes", 3)
+               .addConst ("No", 4)
+               .addConst ("None", -1)
+               .endNamespace ()
+
+               .endNamespace ();
+
+}
+
 void
 LuaInstance::register_classes (lua_State* L)
 {
@@ -557,6 +603,7 @@ LuaInstance::register_classes (lua_State* L)
        LuaBindings::osc (L);
 
        bind_cairo (L);
+       bind_dialog (L);
        register_hooks (L);
 
        luabridge::getGlobalNamespace (L)
@@ -575,25 +622,37 @@ LuaInstance::register_classes (lua_State* L)
                .addFunction ("_type", &ArdourMarker::type)
                .endClass ()
 
-#if 0
                .beginClass <AxisView> ("AxisView")
                .endClass ()
+
                .deriveClass <TimeAxisView, AxisView> ("TimeAxisView")
                .endClass ()
-               .deriveClass <RouteTimeAxisView, TimeAxisView> ("RouteTimeAxisView")
-               .endClass ()
-#endif
 
                .beginClass <Selectable> ("Selectable")
                .endClass ()
+
                .deriveClass <TimeAxisViewItem, Selectable> ("TimeAxisViewItem")
                .endClass ()
+
                .deriveClass <RegionView, TimeAxisViewItem> ("RegionView")
                .endClass ()
 
+               .deriveClass <RouteUI, Selectable> ("RouteUI")
+               .endClass ()
+
+               .deriveClass <RouteTimeAxisView, RouteUI> ("RouteTimeAxisView")
+               .addCast<TimeAxisView> ("to_timeaxisview")
+               .endClass ()
+
+               // std::list<Selectable*>
                .beginStdCPtrList <Selectable> ("SelectionList")
                .endClass ()
 
+               // std::list<TimeAxisView*>
+               .beginStdCPtrList <TimeAxisView> ("TrackViewStdList")
+               .endClass ()
+
+
                .beginClass <RegionSelection> ("RegionSelection")
                .addFunction ("start", &RegionSelection::start)
                .addFunction ("end_frame", &RegionSelection::end_frame)
@@ -610,8 +669,9 @@ LuaInstance::register_classes (lua_State* L)
                .deriveClass <MarkerSelection, std::list<ArdourMarker*> > ("MarkerSelection")
                .endClass ()
 
-               .beginClass <TrackViewList> ("TrackViewList")
-               .addFunction ("routelist", &TrackViewList::routelist) // XXX check windows binding (libardour)
+               .deriveClass <TrackViewList, std::list<TimeAxisView*> > ("TrackViewList")
+               .addFunction ("contains", &TrackViewList::contains)
+               .addFunction ("routelist", &TrackViewList::routelist)
                .endClass ()
 
                .deriveClass <TrackSelection, TrackViewList> ("TrackSelection")
@@ -689,23 +749,23 @@ LuaInstance::register_classes (lua_State* L)
                .addFunction ("get_current_zoom", &PublicEditor::get_current_zoom)
                .addFunction ("reset_zoom", &PublicEditor::reset_zoom)
 
-#if 0 // These need TimeAxisView* which isn't exposed, yet
-               .addFunction ("playlist_selector", &PublicEditor::playlist_selector)
                .addFunction ("clear_playlist", &PublicEditor::clear_playlist)
                .addFunction ("new_playlists", &PublicEditor::new_playlists)
                .addFunction ("copy_playlists", &PublicEditor::copy_playlists)
                .addFunction ("clear_playlists", &PublicEditor::clear_playlists)
-#endif
 
                .addFunction ("select_all_tracks", &PublicEditor::select_all_tracks)
                .addFunction ("deselect_all", &PublicEditor::deselect_all)
-#if 0
+
+#if 0 // TimeAxisView&  can't be bound (pure virtual fn)
                .addFunction ("set_selected_track", &PublicEditor::set_selected_track)
                .addFunction ("set_selected_mixer_strip", &PublicEditor::set_selected_mixer_strip)
-               .addFunction ("hide_track_in_display", &PublicEditor::hide_track_in_display)
+               .addFunction ("ensure_time_axis_view_is_visible", &PublicEditor::ensure_time_axis_view_is_visible)
 #endif
+               .addFunction ("hide_track_in_display", &PublicEditor::hide_track_in_display)
+               .addFunction ("show_track_in_display", &PublicEditor::show_track_in_display)
 
-               .addFunction ("get_regionview_from_region", &PublicEditor::get_regionview_from_region)
+               .addFunction ("regionview_from_region", &PublicEditor::regionview_from_region)
                .addFunction ("set_stationary_playhead", &PublicEditor::set_stationary_playhead)
                .addFunction ("stationary_playhead", &PublicEditor::stationary_playhead)
                .addFunction ("set_follow_playhead", &PublicEditor::set_follow_playhead)
@@ -716,7 +776,6 @@ LuaInstance::register_classes (lua_State* L)
                .addFunction ("current_page_samples", &PublicEditor::current_page_samples)
                .addFunction ("visible_canvas_height", &PublicEditor::visible_canvas_height)
                .addFunction ("temporal_zoom_step", &PublicEditor::temporal_zoom_step)
-               //.addFunction ("ensure_time_axis_view_is_visible", &PublicEditor::ensure_time_axis_view_is_visible)
                .addFunction ("override_visible_track_count", &PublicEditor::override_visible_track_count)
 
                .addFunction ("scroll_tracks_down_line", &PublicEditor::scroll_tracks_down_line)
@@ -748,15 +807,15 @@ LuaInstance::register_classes (lua_State* L)
                .addFunction ("set_video_timeline_height", &PublicEditor::set_video_timeline_height)
 
 #if 0
-               .addFunction ("get_route_view_by_route_id", &PublicEditor::get_route_view_by_route_id)
                .addFunction ("get_equivalent_regions", &PublicEditor::get_equivalent_regions)
-
-               .addFunction ("axis_view_from_route", &PublicEditor::axis_view_from_route)
-               .addFunction ("axis_views_from_routes", &PublicEditor::axis_views_from_routes)
-               .addFunction ("get_track_views", &PublicEditor::get_track_views)
                .addFunction ("drags", &PublicEditor::drags)
 #endif
 
+               .addFunction ("get_route_view_by_route_id", &PublicEditor::get_route_view_by_route_id)
+               .addFunction ("get_track_views", &PublicEditor::get_track_views)
+               .addFunction ("rtav_from_route", &PublicEditor::rtav_from_route)
+               .addFunction ("axis_views_from_routes", &PublicEditor::axis_views_from_routes)
+
                .addFunction ("center_screen", &PublicEditor::center_screen)
 
                .addFunction ("get_smart_mode", &PublicEditor::get_smart_mode)
@@ -965,7 +1024,6 @@ LuaInstance::init ()
                        ""
                        "  local function serialize (name, value)"
                        "   local rv = name .. ' = '"
-                       "   collectgarbage()"
                        "   if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
                        "    return rv .. basic_serialize(value) .. ' '"
                        "   elseif type(value) == \"table\" then"
@@ -973,7 +1031,6 @@ LuaInstance::init ()
                        "    for k,v in pairs(value) do"
                        "     local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
                        "     rv = rv .. serialize(fieldname, v) .. ' '"
-                       "     collectgarbage()" // string concatenation allocates a new string
                        "    end"
                        "    return rv;"
                        "   elseif type(value) == \"function\" then"
@@ -1178,13 +1235,19 @@ LuaInstance::interactive_add (LuaScriptInfo::ScriptType type, int id)
        LuaScriptParamList lsp = LuaScriptParams::script_params (spi, param_function);
 
        ScriptParameterDialog spd (_("Set Script Parameters"), spi, reg, lsp);
-       switch (spd.run ()) {
-               case Gtk::RESPONSE_ACCEPT:
-                       break;
-               default:
-                       return false;
+
+       if (!spd.need_interation ()) {
+               switch (spd.run ()) {
+                       case Gtk::RESPONSE_ACCEPT:
+                               break;
+                       default:
+                               return false;
+               }
        }
 
+       LuaScriptParamPtr lspp (new LuaScriptParam("x-script-origin", "", spi->path, false));
+       lsp.push_back (lspp);
+
        switch (type) {
                case LuaScriptInfo::EditorAction:
                        return set_lua_action (id, spd.name(), script, lsp);
@@ -1226,7 +1289,7 @@ LuaInstance::get_action_state ()
        g_free (b64);
 
        XMLNode* script_node = new XMLNode (X_("ActionScript"));
-       script_node->add_property (X_("lua"), LUA_VERSION);
+       script_node->set_property (X_("lua"), LUA_VERSION);
        script_node->add_content (b64s);
 
        return *script_node;
@@ -1585,17 +1648,25 @@ LuaCallback::get_state (void)
                luabridge::LuaRef savedstate ((*_lua_save)());
                saved = savedstate.cast<std::string>();
        }
-       lua.collect_garbage ();
+
+       lua.collect_garbage (); // this may be expensive:
+       /* Editor::instant_save() calls Editor::get_state() which
+        * calls LuaInstance::get_hook_state() which in turn calls
+        * this LuaCallback::get_state() for every registered hook.
+        *
+        * serialize in _lua_save() allocates many small strings
+        * on the lua-stack, collecting them all may take a ms.
+        */
 
        gchar* b64 = g_base64_encode ((const guchar*)saved.c_str (), saved.size ());
        std::string b64s (b64);
        g_free (b64);
 
        XMLNode* script_node = new XMLNode (X_("LuaCallback"));
-       script_node->add_property (X_("lua"), LUA_VERSION);
-       script_node->add_property (X_("id"), _id.to_s ());
-       script_node->add_property (X_("name"), _name);
-       script_node->add_property (X_("signals"), _signals.to_string ());
+       script_node->set_property (X_("lua"), LUA_VERSION);
+       script_node->set_property (X_("id"), _id.to_s ());
+       script_node->set_property (X_("name"), _name);
+       script_node->set_property (X_("signals"), _signals.to_string ());
        script_node->add_content (b64s);
        return *script_node;
 }
@@ -1661,7 +1732,6 @@ LuaCallback::init (void)
                        ""
                        "  local function serialize (name, value)"
                        "   local rv = name .. ' = '"
-                       "   collectgarbage()"
                        "   if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
                        "    return rv .. basic_serialize(value) .. ' '"
                        "   elseif type(value) == \"table\" then"
@@ -1669,7 +1739,6 @@ LuaCallback::init (void)
                        "    for k,v in pairs(value) do"
                        "     local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
                        "     rv = rv .. serialize(fieldname, v) .. ' '"
-                       "     collectgarbage()" // string concatenation allocates a new string
                        "    end"
                        "    return rv;"
                        "   elseif type(value) == \"function\" then"