add back-pointer to TempoMap from points, and push dirty=true into map
[ardour.git] / gtk2_ardour / luawindow.cc
index 155c6908acb8d8507493795a99948629b28c62db..89a1d387aa490cfdb1ea058c80a803842d8d1cfc 100644 (file)
@@ -37,6 +37,9 @@
 #include "gtkmm2ext/utils.h"
 #include "gtkmm2ext/window_title.h"
 
+#include "widgets/pane.h"
+#include "widgets/tooltips.h"
+
 #include "ardour/filesystem_paths.h"
 #include "ardour/luabindings.h"
 #include "LuaBridge/LuaBridge.h"
 #include "luainstance.h"
 #include "luawindow.h"
 #include "public_editor.h"
-#include "tooltips.h"
 #include "utils.h"
 #include "utils_videotl.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace ARDOUR;
-using namespace ARDOUR_UI_UTILS;
 using namespace PBD;
 using namespace Gtk;
 using namespace Glib;
@@ -94,7 +95,7 @@ LuaWindow::LuaWindow ()
        , _menu_snippet (0)
        , _menu_actions (0)
        , _btn_run (_("Run"))
-       , _btn_clear (_("Clear Outtput"))
+       , _btn_clear (_("Clear Output"))
        , _btn_open (_("Import"))
        , _btn_save (_("Save"))
        , _btn_delete (_("Delete"))
@@ -155,14 +156,15 @@ LuaWindow::LuaWindow ()
        vbox->pack_start (*scrollin, true, true, 0);
        vbox->pack_start (*hbox, false, false, 2);
 
-       Gtk::VPaned *vpane = manage (new Gtk::VPaned ());
-       vpane->pack1 (*vbox, true, false);
-       vpane->pack2 (scrollout, false, true);
+       ArdourWidgets::VPane *vpane = manage (new ArdourWidgets::VPane ());
+       vpane->add (*vbox);
+       vpane->add (scrollout);
+       vpane->set_divider (0, 0.75);
 
        vpane->show_all ();
        add (*vpane);
        set_size_request (640, 480); // XXX
-       ARDOUR_UI_UTILS::set_tooltip (script_select, _("Select Editor Buffer"));
+       ArdourWidgets::set_tooltip (script_select, _("Select Editor Buffer"));
 
        setup_buffers ();
        LuaScripting::instance().scripts_changed.connect (*this, invalidator (*this), boost::bind (&LuaWindow::refresh_scriptlist, this), gui_context());
@@ -188,7 +190,7 @@ LuaWindow::hide_window (GdkEventAny *ev)
 {
        if (!_visible) return 0;
        _visible = false;
-       return just_hide_it (ev, static_cast<Gtk::Window *>(this));
+       return ARDOUR_UI_UTILS::just_hide_it (ev, static_cast<Gtk::Window *>(this));
 }
 
 void LuaWindow::reinit_lua ()
@@ -197,6 +199,7 @@ void LuaWindow::reinit_lua ()
        delete lua;
        lua = new LuaState();
        lua->Print.connect (sigc::mem_fun (*this, &LuaWindow::append_text));
+       lua->sandbox (false);
 
        lua_State* L = lua->getState();
        LuaInstance::register_classes (L);
@@ -283,6 +286,12 @@ LuaWindow::run_script ()
                        }
                } catch (luabridge::LuaException const& e) {
                        append_text (string_compose (_("LuaException: %1"), e.what()));
+               } catch (Glib::Exception const& e) {
+                       append_text (string_compose (_("Glib Exception: %1"), e.what()));
+               } catch (std::exception const& e) {
+                       append_text (string_compose (_("C++ Exception: %1"), e.what()));
+               } catch (...) {
+                       append_text (string_compose (_("C++ Exception: %1"), "..."));
                }
        } else {
                // script with factory method
@@ -301,6 +310,12 @@ LuaWindow::run_script ()
                        lua->do_command ("factory = nil;");
                } catch (luabridge::LuaException const& e) {
                        append_text (string_compose (_("LuaException: %1"), e.what()));
+               } catch (Glib::Exception const& e) {
+                       append_text (string_compose (_("Glib Exception: %1"), e.what()));
+               } catch (std::exception const& e) {
+                       append_text (string_compose (_("C++ Exception: %1"), e.what()));
+               } catch (...) {
+                       append_text (string_compose (_("C++ Exception: %1"), "..."));
                }
        }
 }
@@ -311,6 +326,7 @@ LuaWindow::append_text (std::string s)
        Glib::RefPtr<Gtk::TextBuffer> tb (outtext.get_buffer());
        tb->insert (tb->end(), s + "\n");
        scroll_to_bottom ();
+       Gtkmm2ext::UI::instance()->flush_pending (0.05);
 }
 
 void
@@ -320,6 +336,17 @@ LuaWindow::clear_output ()
        tb->set_text ("");
 }
 
+void
+LuaWindow::edit_script (const std::string& name, const std::string& script)
+{
+       ScriptBuffer* sb = new LuaWindow::ScriptBuffer (name);
+       sb->script = script;
+       script_buffers.push_back (ScriptBufferPtr (sb));
+       script_selection_changed (script_buffers.back ());
+       refresh_scriptlist ();
+       show_window ();
+}
+
 void
 LuaWindow::new_script ()
 {
@@ -379,7 +406,7 @@ LuaWindow::import_script ()
        // TODO convert a few URL (eg. pastebin) to raw.
 #if 0
        char *url = "http://pastebin.com/raw/3UMkZ6nV";
-       char *rv = a3_curl_http_get (url, 0);
+       char *rv = ArdourCurl::http_get (url, 0);
        if (rv) {
                new_script ();
                Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
@@ -605,7 +632,7 @@ LuaWindow::script_selection_changed (ScriptBufferPtr n, bool force)
 
        Glib::RefPtr<Gtk::TextBuffer> tb (entry.get_buffer());
 
-       if ((n->flags & Buffer_Valid)) {
+       if (_current_buffer->flags & Buffer_Valid) {
                _current_buffer->script = tb->get_text();
        }
 
@@ -671,6 +698,7 @@ LuaWindow::ScriptBuffer::ScriptBuffer (const std::string& n)
        , flags (Buffer_Scratch | Buffer_Valid)
 {
        script =
+               "---- this header is (only) required to save the script\n"
                "-- ardour { [\"type\"] = \"Snippet\", name = \"\" }\n"
                "-- function factory () return function () -- -- end end\n";
 }
@@ -708,11 +736,12 @@ LuaWindow::ScriptBuffer::~ScriptBuffer ()
 bool
 LuaWindow::ScriptBuffer::load ()
 {
+       assert (!(flags & Buffer_Valid));
        if (!(flags & Buffer_HasFile)) return false;
-       if (flags & Buffer_Valid) return true;
        try {
                script = Glib::file_get_contents (path);
                flags |= Buffer_Valid;
+               flags &= BufferFlags(~Buffer_Dirty);
        } catch (Glib::FileError e) {
                return false;
        }