OSC: Changed gainVCA to gainfader as VCA is already used.
[ardour.git] / gtk2_ardour / luawindow.cc
index aed565f7ce4e430f63eb086b8d9440cef09b4747..a95376c3a6586a4aa9ed5ef10c67b2846d76241b 100644 (file)
@@ -86,6 +86,7 @@ LuaWindow::instance ()
 LuaWindow::LuaWindow ()
        : Window (Gtk::WINDOW_TOPLEVEL)
        , VisibilityTracker (*((Gtk::Window*) this))
+       , lua (0)
        , _visible (false)
        , _menu_scratch (0)
        , _menu_snippet (0)
@@ -95,10 +96,12 @@ LuaWindow::LuaWindow ()
        , _btn_open (_("Import"))
        , _btn_save (_("Save"))
        , _btn_delete (_("Delete"))
+       , _btn_revert (_("Revert"))
        , _current_buffer ()
 {
        set_name ("Lua");
 
+       reinit_lua ();
        update_title ();
        set_wmclass (X_("ardour_mixer"), PROGRAM_NAME);
 
@@ -118,10 +121,12 @@ LuaWindow::LuaWindow ()
        _btn_open.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::import_script));
        _btn_save.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::save_script));
        _btn_delete.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::delete_script));
+       _btn_revert.signal_clicked.connect (sigc::mem_fun(*this, &LuaWindow::revert_script));
 
        _btn_open.set_sensitive (false); // TODO
        _btn_save.set_sensitive (false);
        _btn_delete.set_sensitive (false);
+       _btn_revert.set_sensitive (false);
 
        // layout
 
@@ -131,6 +136,9 @@ LuaWindow::LuaWindow ()
        scrollout.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
        scrollout.add (outtext);
 
+       entry.set_name ("ArdourLuaEntry");
+       outtext.set_name ("ArdourLuaEntry");
+
        Gtk::HBox *hbox = manage (new HBox());
 
        hbox->pack_start (_btn_run, false, false, 2);
@@ -138,6 +146,7 @@ LuaWindow::LuaWindow ()
        hbox->pack_start (_btn_open, false, false, 2);
        hbox->pack_start (_btn_save, false, false, 2);
        hbox->pack_start (_btn_delete, false, false, 2);
+       hbox->pack_start (_btn_revert, false, false, 2);
        hbox->pack_start (script_select, false, false, 2);
 
        Gtk::VBox *vbox = manage (new VBox());
@@ -151,14 +160,6 @@ LuaWindow::LuaWindow ()
        vpane->show_all ();
        add (*vpane);
        set_size_request (640, 480); // XXX
-
-       lua.Print.connect (sigc::mem_fun (*this, &LuaWindow::append_text));
-
-       lua_State* L = lua.getState();
-       LuaInstance::register_classes (L);
-       luabridge::push <PublicEditor *> (L, &PublicEditor::instance());
-       lua_setglobal (L, "Editor");
-
        ARDOUR_UI_UTILS::set_tooltip (script_select, _("Select Editor Buffer"));
 
        setup_buffers ();
@@ -170,6 +171,7 @@ LuaWindow::LuaWindow ()
 
 LuaWindow::~LuaWindow ()
 {
+       delete lua;
 }
 
 void
@@ -187,6 +189,19 @@ LuaWindow::hide_window (GdkEventAny *ev)
        return just_hide_it (ev, static_cast<Gtk::Window *>(this));
 }
 
+void LuaWindow::reinit_lua ()
+{
+       ENSURE_GUI_THREAD (*this, &LuaWindow::session_going_away);
+       delete lua;
+       lua = new LuaState();
+       lua->Print.connect (sigc::mem_fun (*this, &LuaWindow::append_text));
+
+       lua_State* L = lua->getState();
+       LuaInstance::register_classes (L);
+       luabridge::push <PublicEditor *> (L, &PublicEditor::instance());
+       lua_setglobal (L, "Editor");
+}
+
 void LuaWindow::set_session (Session* s)
 {
        SessionHandlePtr::set_session (s);
@@ -197,7 +212,7 @@ void LuaWindow::set_session (Session* s)
        update_title ();
        _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&LuaWindow::update_title, this), gui_context());
 
-       lua_State* L = lua.getState();
+       lua_State* L = lua->getState();
        LuaBindings::set_session (L, _session);
 }
 
@@ -205,14 +220,13 @@ void
 LuaWindow::session_going_away ()
 {
        ENSURE_GUI_THREAD (*this, &LuaWindow::session_going_away);
-       lua.do_command ("collectgarbage();");
-       //TODO: re-init lua-engine (drop all references)
+       reinit_lua (); // drop state (all variables, session references)
 
        SessionHandlePtr::session_going_away ();
        _session = 0;
        update_title ();
 
-       lua_State* L = lua.getState();
+       lua_State* L = lua->getState();
        LuaBindings::set_session (L, _session);
 }
 
@@ -261,8 +275,8 @@ LuaWindow::run_script ()
        if (bytecode.empty()) {
                // plain script or faulty script -- run directly
                try {
-                       lua.do_command ("function ardour () end");
-                       if (0 == lua.do_command (script)) {
+                       lua->do_command ("function ardour () end");
+                       if (0 == lua->do_command (script)) {
                                append_text ("> OK");
                        }
                } catch (luabridge::LuaException const& e) {
@@ -271,18 +285,18 @@ LuaWindow::run_script ()
        } else {
                // script with factory method
                try {
-                       lua_State* L = lua.getState();
-                       lua.do_command ("function ardour () end");
+                       lua_State* L = lua->getState();
+                       lua->do_command ("function ardour () end");
 
                        LuaScriptParamList args = LuaScriptParams::script_params (script, "action_param", false);
                        luabridge::LuaRef tbl_arg (luabridge::newTable(L));
                        LuaScriptParams::params_to_ref (&tbl_arg, args);
-                       lua.do_command (script); // register "factory"
+                       lua->do_command (script); // register "factory"
                        luabridge::LuaRef lua_factory = luabridge::getGlobal (L, "factory");
                        if (lua_factory.isFunction()) {
                                lua_factory(tbl_arg)();
                        }
-                       lua.do_command ("factory = nil;");
+                       lua->do_command ("factory = nil;");
                } catch (luabridge::LuaException const& e) {
                        append_text (string_compose (_("LuaException: %1"), e.what()));
                }
@@ -334,6 +348,13 @@ LuaWindow::delete_script ()
        new_script ();
 }
 
+void
+LuaWindow::revert_script ()
+{
+       _current_buffer->flags &= BufferFlags(~Buffer_Valid);
+       script_selection_changed (_current_buffer, true);
+}
+
 void
 LuaWindow::import_script ()
 {
@@ -466,6 +487,9 @@ LuaWindow::setup_buffers ()
        script_buffers.push_back (ScriptBufferPtr (new LuaWindow::ScriptBuffer("#1")));
        _current_buffer = script_buffers.front();
 
+       Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
+       tb->set_text (_current_buffer->script);
+
        refresh_scriptlist ();
        update_gui_state ();
 }
@@ -525,7 +549,7 @@ LuaWindow::rebuild_menu ()
 
        for (ScriptBufferList::const_iterator i = script_buffers.begin (); i != script_buffers.end (); ++i) {
                Menu_Helpers::MenuElem elem = Gtk::Menu_Helpers::MenuElem((*i)->name,
-                               sigc::bind(sigc::mem_fun(*this, &LuaWindow::script_selection_changed), (*i)));
+                               sigc::bind(sigc::mem_fun(*this, &LuaWindow::script_selection_changed), (*i), false));
 
                if ((*i)->flags & Buffer_Scratch) {
                        items_scratch.push_back(elem);
@@ -545,14 +569,17 @@ LuaWindow::rebuild_menu ()
 }
 
 void
-LuaWindow::script_selection_changed (ScriptBufferPtr n)
+LuaWindow::script_selection_changed (ScriptBufferPtr n, bool force)
 {
-       if (n == _current_buffer) {
+       if (n == _current_buffer && !force) {
                return;
        }
 
        Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
-       _current_buffer->script = tb->get_text();
+
+       if ((n->flags & Buffer_Valid)) {
+               _current_buffer->script = tb->get_text();
+       }
 
        if (!(n->flags & Buffer_Valid)) {
                if (!n->load()) {
@@ -594,6 +621,7 @@ LuaWindow::update_gui_state ()
 
        _btn_save.set_sensitive (sb.flags & Buffer_Dirty);
        _btn_delete.set_sensitive (sb.flags & Buffer_Scratch); // TODO allow to remove user-scripts
+       _btn_revert.set_sensitive ((sb.flags & Buffer_Dirty) && (sb.flags & Buffer_HasFile));
 }
 
 void
@@ -609,6 +637,9 @@ LuaWindow::ScriptBuffer::ScriptBuffer (const std::string& n)
        : name (n)
        , flags (Buffer_Scratch | Buffer_Valid)
 {
+       script =
+               "-- ardour { [\"type\"] = \"Snippet\", name = \"\" }\n"
+               "-- function factory () return function () -- -- end end\n";
 }
 
 LuaWindow::ScriptBuffer::ScriptBuffer (LuaScriptInfoPtr p)