avoid use of Port::port_offset() everywhere except Port::flush_buffers() and Port...
[ardour.git] / gtk2_ardour / luadialog.cc
index 04040c820162bc9499e17edcf06fdd6de9d01c2d..79ba81f700d3bc719cf9295f050ade1d8d0ae6bd 100644 (file)
@@ -1,21 +1,24 @@
 /*
- * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
+ * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
+ * Copyright (C) 2018 Nikolaus Gullotta <nikolaus.gullotta@gmail.com>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include <algorithm>
+
 #include <gtkmm.h>
 
 #include "ardour/dB.h"
 #include "widgets/ardour_dropdown.h"
 #include "widgets/slider_controller.h"
 
+#include "stripable_colorpicker.h"
 #include "ardour_dialog.h"
 #include "luadialog.h"
+#include "splash.h"
 #include "utils.h"
 
 using namespace LuaDialog;
 
-/*******************************************************************************
+/* *****************************************************************************
  * Simple Message Dialog
  */
 Message::Message (std::string const& title, std::string const& msg, Message::MessageType mt, Message::ButtonType bt)
-       : _message_dialog (msg, false, to_gtk_mt (mt), to_gtk_bt (bt), true)
+       : _message_dialog (msg, true, to_gtk_mt (mt), to_gtk_bt (bt), true)
 {
        _message_dialog.set_title (title);
 }
@@ -44,8 +49,24 @@ Message::Message (std::string const& title, std::string const& msg, Message::Mes
 int
 Message::run ()
 {
+
+       bool splash_pushed = false;
+       Splash* spl = Splash::instance();
+       if (spl && spl->is_visible()) {
+               spl->pop_back_for (_message_dialog);
+               splash_pushed = true;
+       }
+
        int rv = _message_dialog.run ();
        _message_dialog.hide ();
+
+       if (splash_pushed) {
+               spl = Splash::instance();
+               if (spl) {
+                       spl->pop_front();
+               }
+       }
+
        switch (rv) {
                case Gtk::RESPONSE_OK:
                        return 0;
@@ -108,29 +129,76 @@ class LuaDialogLabel : public LuaDialogWidget
 {
 public:
        LuaDialogLabel (std::string const& title, Gtk::AlignmentEnum xalign)
-               : LuaDialogWidget ("", "")
-               , _lbl ("<b>" + title + "</b>", xalign, Gtk::ALIGN_CENTER, false)
+               : LuaDialogWidget ("", "", 0, 2)
+               , _lbl (title, xalign, Gtk::ALIGN_CENTER, false)
+       { }
+
+       Gtk::Widget* widget ()
+       {
+               return &_lbl;
+       }
+
+       void assign (luabridge::LuaRef* rv) const { }
+protected:
+       Gtk::Label _lbl;
+};
+
+
+class LuaDialogHeading : public LuaDialogLabel
+{
+public:
+       LuaDialogHeading (std::string const& title, Gtk::AlignmentEnum xalign)
+       : LuaDialogLabel ("<b>" + title + "</b>", xalign)
        {
                _lbl.set_use_markup ();
        }
+};
+
+class LuaHSeparator : public LuaDialogWidget
+{
+public:
+       LuaHSeparator ()
+               : LuaDialogWidget ("", "", 0, 2)
+       {}
 
        Gtk::Widget* widget ()
        {
-               return &_lbl;
+               return &_sep;
        }
 
        void assign (luabridge::LuaRef* rv) const { }
 protected:
-       Gtk::Label _lbl;
+       Gtk::HSeparator _sep;
+};
+
+class LuaColorPicker : public LuaDialogWidget
+{
+public:
+       LuaColorPicker (std::string const& key)
+               : LuaDialogWidget (key, "", 0, 1)
+       {}
+
+       Gtk::Widget* widget ()
+       {
+               return &_cs;
+       }
+       void assign (luabridge::LuaRef* rv) const {
+               uint32_t rgba = ARDOUR_UI_UTILS::gdk_color_to_rgba(_cs.get_color());
+               (*rv)[_key] = rgba;
+       }
+protected:
+       Gtk::ColorButton _cs;
 };
 
 class LuaDialogCheckbox : public LuaDialogWidget
 {
 public:
        LuaDialogCheckbox (std::string const& key, std::string const& title, bool on)
-               : LuaDialogWidget (key, "")
-               , _cb (title)
+               : LuaDialogWidget (key, "", 1, 1)
        {
+               if (!title.empty ()) {
+                       _cb.add_label (title, false, 0);
+               }
                _cb.set_active (on);
        }
 
@@ -406,18 +474,27 @@ protected:
        void populate (Gtk::Menu_Helpers::MenuList& items, luabridge::LuaRef values, std::string const& dflt)
        {
                using namespace Gtk::Menu_Helpers;
+               std::vector<std::string> keys;
+
                for (luabridge::Iterator i (values); !i.isNil (); ++i) {
                        if (!i.key ().isString ())  { continue; }
-                       std::string key = i.key ().cast<std::string> ();
-                       if (i.value ().isTable ())  {
+                       keys.push_back (i.key ().cast<std::string> ());
+               }
+
+               std::sort (keys.begin(), keys.end());
+
+               for (std::vector<std::string>::const_iterator i = keys.begin (); i != keys.end(); ++i) {
+                       std::string key = *i;
+
+                       if (values[key].isTable ())  {
                                Gtk::Menu* menu  = Gtk::manage (new Gtk::Menu);
                                items.push_back (MenuElem (key, *menu));
-                               populate (menu->items (), i.value (), dflt);
+                               populate (menu->items (), values[key], dflt);
                                continue;
                        }
-                       luabridge::LuaRef* ref = new luabridge::LuaRef (i.value ());
+                       luabridge::LuaRef* ref = new luabridge::LuaRef (values[key]);
                        _refs.push_back (ref);
-                       items.push_back (MenuElem (i.key ().cast<std::string> (),
+                       items.push_back (MenuElem (key,
                                                sigc::bind (sigc::mem_fun (*this, &LuaDialogDropDown::dd_select), key, ref)));
 
                        if (!_rv || key == dflt) {
@@ -444,20 +521,16 @@ public:
                : LuaDialogWidget (key, title)
                , _fc (a)
        {
+               Gtkmm2ext::add_volume_shortcuts (_fc);
                if (!path.empty ()) {
                        switch (a) {
                                case Gtk::FILE_CHOOSER_ACTION_OPEN:
-                               case Gtk::FILE_CHOOSER_ACTION_SAVE:
-                                       if (Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR|Glib::FILE_TEST_EXISTS)) {
-                                               _fc.set_filename (path);
-                                       }
-                                       break;
                                case Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER:
-                                       if (Glib::file_test (path, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
-                                               _fc.set_filename (path);
-                                       }
+                                       _fc.set_filename (path);
                                        break;
+                               case Gtk::FILE_CHOOSER_ACTION_SAVE:
                                case Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER:
+                                       /* not supported by Gtk::FileChooserButton */
                                        break;
                        }
                }
@@ -478,10 +551,48 @@ protected:
 };
 
 
+class LuaFileChooserWidget : public LuaDialogWidget
+{
+public:
+       LuaFileChooserWidget (std::string const& key, std::string const& title, Gtk::FileChooserAction a, const std::string& path)
+               : LuaDialogWidget (key, title)
+               , _fc (a)
+       {
+               Gtkmm2ext::add_volume_shortcuts (_fc);
+               if (!path.empty ()) {
+                       switch (a) {
+                               case Gtk::FILE_CHOOSER_ACTION_OPEN:
+                               case Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER:
+                                       _fc.set_filename (path);
+                                       break;
+                               case Gtk::FILE_CHOOSER_ACTION_SAVE:
+                               case Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER:
+                                       _fc.set_filename (path);
+                                       _fc.set_current_name (Glib::path_get_basename (path));
+                                       break;
+                                       break;
+                       }
+               }
+       }
+
+       Gtk::Widget* widget ()
+       {
+               return &_fc;
+       }
+
+       void assign (luabridge::LuaRef* rv) const
+       {
+               (*rv)[_key] = std::string (_fc.get_filename ());
+       }
 
-/*******************************************************************************
+protected:
+       Gtk::FileChooserWidget _fc;
+};
+
+/* *****************************************************************************
  * Lua Parameter Dialog
  */
+
 Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
        :_ad (title, true, false)
        , _title (title)
@@ -497,6 +608,13 @@ Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
 
                std::string title = i.value ()["title"].cast<std::string> ();
                std::string type = i.value ()["type"].cast<std::string> ();
+               std::string key;
+
+               if (i.value ()["key"].isString ()) {
+                       key = i.value ()["key"].cast<std::string> ();
+               }
+
+               LuaDialogWidget* w = NULL;
 
                if (type == "heading") {
                        Gtk::AlignmentEnum xalign = Gtk::ALIGN_CENTER;
@@ -508,25 +626,37 @@ Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
                                        xalign = Gtk::ALIGN_RIGHT;
                                }
                        }
-                       _widgets.push_back (new LuaDialogLabel (title, xalign));
+                       w = new LuaDialogHeading (title, xalign);
+               } else if (type == "label") {
+                       Gtk::AlignmentEnum xalign = Gtk::ALIGN_CENTER;
+                       if (i.value ()["align"].isString ()) {
+                               std::string align = i.value ()["align"].cast <std::string> ();
+                               if (align == "left") {
+                                       xalign = Gtk::ALIGN_LEFT;
+                               } else if (align == "right") {
+                                       xalign = Gtk::ALIGN_RIGHT;
+                               }
+                       }
+                       w = new LuaDialogLabel (title, xalign);
+               } else if (type == "hseparator") {
+                       w = new LuaHSeparator ();
+               }
+               /* the following widgets do require a key */
+               else if (key.empty ()) {
                        continue;
                }
-
-               if (!i.value ()["key"].isString ()) { continue; }
-               std::string key = i.value ()["key"].cast<std::string> ();
-
-               if (type == "checkbox") {
+               else if (type == "checkbox") {
                        bool dflt = false;
                        if (i.value ()["default"].isBoolean ()) {
                                dflt = i.value ()["default"].cast<bool> ();
                        }
-                       _widgets.push_back (new LuaDialogCheckbox (key, title, dflt));
+                       w = new LuaDialogCheckbox (key, title, dflt);
                } else if (type == "entry") {
                        std::string dflt;
                        if (i.value ()["default"].isString ()) {
                                dflt = i.value ()["default"].cast<std::string> ();
                        }
-                       _widgets.push_back (new LuaDialogEntry (key, title, dflt));
+                       w = new LuaDialogEntry (key, title, dflt);
                } else if (type == "radio") {
                        std::string dflt;
                        if (!i.value ()["values"].isTable ()) {
@@ -535,13 +665,13 @@ Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
                        if (i.value ()["default"].isString ()) {
                                dflt = i.value ()["default"].cast<std::string> ();
                        }
-                       _widgets.push_back (new LuaDialogRadio (key, title, i.value ()["values"], dflt));
+                       w = new LuaDialogRadio (key, title, i.value ()["values"], dflt);
                } else if (type == "fader") {
                        double dflt = 0;
                        if (i.value ()["default"].isNumber ()) {
                                dflt = i.value ()["default"].cast<double> ();
                        }
-                       _widgets.push_back (new LuaDialogFader (key, title, dflt));
+                       w = new LuaDialogFader (key, title, dflt);
                } else if (type == "slider") {
                        double lower, upper, dflt;
                        int digits = 0;
@@ -557,7 +687,7 @@ Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
                        if (i.value ()["digits"].isNumber ()) {
                                digits = i.value ()["digits"].cast<int> ();
                        }
-                       _widgets.push_back (new LuaDialogSlider (key, title, lower, upper, dflt, digits, i.value ()["scalepoints"]));
+                       w = new LuaDialogSlider (key, title, lower, upper, dflt, digits, i.value ()["scalepoints"]);
                } else if (type == "number") {
                        double lower, upper, dflt, step;
                        int digits = 0;
@@ -578,7 +708,7 @@ Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
                        if (i.value ()["digits"].isNumber ()) {
                                digits = i.value ()["digits"].cast<int> ();
                        }
-                       _widgets.push_back (new LuaDialogSpinBox (key, title, lower, upper, dflt, step, digits));
+                       w = new LuaDialogSpinBox (key, title, lower, upper, dflt, step, digits);
                } else if (type == "dropdown") {
                        std::string dflt;
                        if (!i.value ()["values"].isTable ()) {
@@ -587,19 +717,43 @@ Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
                        if (i.value ()["default"].isString ()) {
                                dflt = i.value ()["default"].cast<std::string> ();
                        }
-                       _widgets.push_back (new LuaDialogDropDown (key, title, i.value ()["values"], dflt));
+                       w = new LuaDialogDropDown (key, title, i.value ()["values"], dflt);
                } else if (type == "file") {
                        std::string path;
                        if (i.value ()["path"].isString ()) {
                                path = i.value ()["path"].cast<std::string> ();
                        }
-                       _widgets.push_back (new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_OPEN, path));
+                       w = new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_OPEN, path);
                } else if (type == "folder") {
                        std::string path;
                        if (i.value ()["path"].isString ()) {
                                path = i.value ()["path"].cast<std::string> ();
                        }
-                       _widgets.push_back (new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, path));
+                       w = new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, path);
+               } else if (type == "createfile") {
+                       std::string path;
+                       if (i.value ()["path"].isString ()) {
+                               path = i.value ()["path"].cast<std::string> ();
+                       }
+                       w = new LuaFileChooserWidget (key, title, Gtk::FILE_CHOOSER_ACTION_SAVE, path);
+               } else if (type == "createdir") {
+                       std::string path;
+                       if (i.value ()["path"].isString ()) {
+                               path = i.value ()["path"].cast<std::string> ();
+                       }
+                       w = new LuaFileChooserWidget (key, title, Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER, path);
+               } else if (type == "color") {
+                       w = new LuaColorPicker (key);
+               }
+
+               if (w) {
+                       if (i.value ()["col"].isNumber ()) {
+                               w->set_col (i.value ()["col"].cast<int> ());
+                       }
+                       if (i.value ()["colspan"].isNumber ()) {
+                               w->set_span (i.value ()["colspan"].cast<int> ());
+                       }
+                       _widgets.push_back(w);
                }
        }
 
@@ -607,23 +761,46 @@ Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
        _ad.add_button (Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
 
        Gtk::Table* table = Gtk::manage (new Gtk::Table ());
-       table->set_col_spacings (4);
+       table->set_col_spacings (20);
        table->set_row_spacings (8);
-       _ad.get_vbox ()->pack_start (*table);
+       table->signal_size_allocate ().connect (sigc::mem_fun (this, &Dialog::table_size_alloc));
+
+       _scroller.set_shadow_type(Gtk::SHADOW_NONE);
+       _scroller.set_border_width(0);
+       _scroller.add (*table);
+       _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
+
+       _ad.get_vbox ()->pack_start (_scroller);
+
        int row = 0;
+       int last_end = -1;
 
        for (DialogWidgets::const_iterator i = _widgets.begin (); i != _widgets.end (); ++i) {
+               int col = (*i)->col();
+               int cend = col + (*i)->span();
+
+               if (col < last_end) {
+                       ++row;
+               }
+               last_end = cend;
+
                std::string const& label = (*i)->label ();
                if (!label.empty ()) {
+                       /* items with implicit label (title) */
                        Gtk::Label* lbl = Gtk::manage (new Gtk::Label (label + ":", Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
-                       table->attach (*lbl, 0, 1, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
-                       table->attach (*((*i)->widget ()), 1, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
-               } else if ((*i)->key ().empty ()) {
-                       table->attach (*((*i)->widget ()), 0, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
+                       if (cend - col > 1) {
+                               table->attach (*lbl, col, col + 1, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
+                               table->attach (*((*i)->widget ()), col + 1, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
+                       } else {
+                               Gtk::HBox* hb = Gtk::manage (new Gtk::HBox());
+                               hb->set_spacing(4);
+                               hb->pack_start (*lbl, true, false);
+                               hb->pack_start (*(*i)->widget (), true, false);
+                               table->attach (*hb, col, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
+                       }
                } else {
-                       table->attach (*((*i)->widget ()), 1, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
+                       table->attach (*((*i)->widget ()), col, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
                }
-               ++row;
        }
 }
 
@@ -654,3 +831,59 @@ Dialog::run (lua_State *L)
        luabridge::push (L, rv);
        return 1;
 }
+
+void
+Dialog::table_size_alloc (Gtk::Allocation& allocation)
+{
+       /* XXX: consider using 0.75 * screen-height instead of 512 */
+       if (allocation.get_height () > 512) {
+               _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
+               _ad.set_size_request (-1, 512);
+       }
+}
+
+/* *****************************************************************************
+ * Lua Progress Dialog
+ */
+
+ProgressWindow::ProgressWindow (std::string const& title, bool allow_cancel)
+       : ArdourDialog (title, true)
+       , _canceled (false)
+{
+       _bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
+
+       set_border_width (12);
+       get_vbox()->set_spacing (6);
+       get_vbox()->pack_start (_bar, false, false);
+
+       if (allow_cancel) {
+               using namespace Gtk;
+               Button* b = add_button (Stock::CANCEL, RESPONSE_CANCEL);
+               b->signal_clicked().connect (sigc::mem_fun (*this, &ProgressWindow::cancel_clicked));
+       }
+
+       set_default_size (200, -1);
+       show_all ();
+}
+
+bool
+ProgressWindow::progress (float prog, std::string const& text)
+{
+       if (!text.empty ()) {
+               _bar.set_text (text);
+       }
+       if (prog < 0 || prog > 1) {
+               std::cerr << "pulse\n";
+               _bar.set_pulse_step(.1);
+               _bar.pulse();
+       } else {
+               _bar.set_fraction (prog);
+       }
+       ARDOUR::GUIIdle ();
+       return _canceled;
+}
+
+void
+ProgressWindow::done () {
+       Gtk::Dialog::response(_canceled ? Gtk::RESPONSE_CANCEL : Gtk::RESPONSE_OK);
+}