2 * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2018 Nikolaus Gullotta <nikolaus.gullotta@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "ardour/dB.h"
25 #include "ardour/rc_configuration.h"
27 #include "gtkmm2ext/utils.h"
29 #include "widgets/ardour_dropdown.h"
30 #include "widgets/slider_controller.h"
32 #include "stripable_colorpicker.h"
33 #include "ardour_dialog.h"
34 #include "luadialog.h"
38 using namespace LuaDialog;
40 /* *****************************************************************************
41 * Simple Message Dialog
43 Message::Message (std::string const& title, std::string const& msg, Message::MessageType mt, Message::ButtonType bt)
44 : _message_dialog (msg, true, to_gtk_mt (mt), to_gtk_bt (bt), true)
46 _message_dialog.set_title (title);
53 bool splash_pushed = false;
54 Splash* spl = Splash::instance();
55 if (spl && spl->is_visible()) {
56 spl->pop_back_for (_message_dialog);
60 int rv = _message_dialog.run ();
61 _message_dialog.hide ();
64 spl = Splash::instance();
71 case Gtk::RESPONSE_OK:
73 case Gtk::RESPONSE_CANCEL:
75 case Gtk::RESPONSE_CLOSE:
77 case Gtk::RESPONSE_YES:
79 case Gtk::RESPONSE_NO:
88 Message::to_gtk_bt (ButtonType bt)
92 return Gtk::BUTTONS_OK;
94 return Gtk::BUTTONS_CLOSE;
96 return Gtk::BUTTONS_CANCEL;
98 return Gtk::BUTTONS_YES_NO;
100 return Gtk::BUTTONS_OK_CANCEL;
103 return Gtk::BUTTONS_OK;
107 Message::to_gtk_mt (MessageType mt)
111 return Gtk::MESSAGE_INFO;
113 return Gtk::MESSAGE_WARNING;
115 return Gtk::MESSAGE_QUESTION;
117 return Gtk::MESSAGE_ERROR;
120 return Gtk::MESSAGE_INFO;
124 /* *****************************************************************************
128 class LuaDialogLabel : public LuaDialogWidget
131 LuaDialogLabel (std::string const& title, Gtk::AlignmentEnum xalign)
132 : LuaDialogWidget ("", "", 0, 2)
133 , _lbl (title, xalign, Gtk::ALIGN_CENTER, false)
136 Gtk::Widget* widget ()
141 void assign (luabridge::LuaRef* rv) const { }
147 class LuaDialogHeading : public LuaDialogLabel
150 LuaDialogHeading (std::string const& title, Gtk::AlignmentEnum xalign)
151 : LuaDialogLabel ("<b>" + title + "</b>", xalign)
153 _lbl.set_use_markup ();
157 class LuaHSeparator : public LuaDialogWidget
161 : LuaDialogWidget ("", "", 0, 2)
164 Gtk::Widget* widget ()
169 void assign (luabridge::LuaRef* rv) const { }
171 Gtk::HSeparator _sep;
174 class LuaColorPicker : public LuaDialogWidget
177 LuaColorPicker (std::string const& key)
178 : LuaDialogWidget (key, "", 0, 1)
181 Gtk::Widget* widget ()
185 void assign (luabridge::LuaRef* rv) const {
186 uint32_t rgba = ARDOUR_UI_UTILS::gdk_color_to_rgba(_cs.get_color());
190 Gtk::ColorButton _cs;
193 class LuaDialogCheckbox : public LuaDialogWidget
196 LuaDialogCheckbox (std::string const& key, std::string const& title, bool on)
197 : LuaDialogWidget (key, "", 1, 1)
199 if (!title.empty ()) {
200 _cb.add_label (title, false, 0);
205 Gtk::Widget* widget ()
210 void assign (luabridge::LuaRef* rv) const
212 (*rv)[_key] = _cb.get_active ();
216 Gtk::CheckButton _cb;
219 class LuaDialogEntry : public LuaDialogWidget
222 LuaDialogEntry (std::string const& key, std::string const& title, std::string const& dflt)
223 : LuaDialogWidget (key, title)
225 _entry.set_text (dflt);
228 Gtk::Widget* widget ()
233 void assign (luabridge::LuaRef* rv) const
235 (*rv)[_key] = std::string (_entry.get_text ());
242 class LuaDialogFader : public LuaDialogWidget
245 LuaDialogFader (std::string const& key, std::string const& title, double dflt)
246 : LuaDialogWidget (key, title)
247 , _db_adjustment (ARDOUR::gain_to_slider_position_with_max (1.0, ARDOUR::Config->get_max_gain ()), 0, 1, 0.01, 0.1)
249 _db_slider = Gtk::manage (new ArdourWidgets::HSliderController (&_db_adjustment, boost::shared_ptr<PBD::Controllable> (), 220, 18));
251 _fader_centering_box.pack_start (*_db_slider, true, false);
253 _box.set_spacing (4);
254 _box.set_homogeneous (false);
255 _box.pack_start (_fader_centering_box, false, false);
256 _box.pack_start (_db_display, false, false);
257 _box.pack_start (*Gtk::manage (new Gtk::Label ("dB")), false, false);
259 Gtkmm2ext::set_size_request_to_display_given_text (_db_display, "-99.00", 12, 0);
261 _db_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &LuaDialogFader::db_changed));
262 _db_display.signal_activate ().connect (sigc::mem_fun (*this, &LuaDialogFader::on_activate));
263 _db_display.signal_key_press_event ().connect (sigc::mem_fun (*this, &LuaDialogFader::on_key_press), false);
265 double coeff_val = dB_to_coefficient (dflt);
266 _db_adjustment.set_value (ARDOUR::gain_to_slider_position_with_max (coeff_val, ARDOUR::Config->get_max_gain ()));
270 Gtk::Widget* widget ()
275 void assign (luabridge::LuaRef* rv) const
277 double const val = ARDOUR::slider_position_to_gain_with_max (_db_adjustment.get_value (), ARDOUR::Config->get_max_gain ());
278 (*rv)[_key] = accurate_coefficient_to_dB (val);
284 double const val = ARDOUR::slider_position_to_gain_with_max (_db_adjustment.get_value (), ARDOUR::Config->get_max_gain ());
287 snprintf (buf, sizeof (buf), "-inf");
289 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
291 _db_display.set_text (buf);
296 float db_val = atof (_db_display.get_text ().c_str ());
297 double coeff_val = dB_to_coefficient (db_val);
298 _db_adjustment.set_value (ARDOUR::gain_to_slider_position_with_max (coeff_val, ARDOUR::Config->get_max_gain ()));
301 bool on_key_press (GdkEventKey* ev)
303 if (ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (ev->keyval)) {
309 Gtk::Adjustment _db_adjustment;
310 ArdourWidgets::HSliderController* _db_slider;
311 Gtk::Entry _db_display;
313 Gtk::VBox _fader_centering_box;
316 class LuaDialogSlider : public LuaDialogWidget
319 LuaDialogSlider (std::string const& key, std::string const& title, double lower, double upper, double dflt, int digits, luabridge::LuaRef scalepoints)
320 : LuaDialogWidget (key, title)
321 , _adj (dflt, lower, upper, 1, (upper - lower) / 20, 0)
324 _hscale.set_digits (digits);
325 _hscale.set_draw_value (true);
326 _hscale.set_value_pos (Gtk::POS_TOP);
328 if (!scalepoints.isTable ()) {
332 for (luabridge::Iterator i (scalepoints); !i.isNil (); ++i) {
333 if (!i.key ().isNumber ()) { continue; }
334 if (!i.value ().isString ()) { continue; }
335 _hscale.add_mark (i.key ().cast<double> (), Gtk::POS_BOTTOM, i.value ().cast<std::string> ());
339 Gtk::Widget* widget ()
344 void assign (luabridge::LuaRef* rv) const
346 (*rv)[_key] = _adj.get_value ();
350 Gtk::Adjustment _adj;
354 class LuaDialogSpinBox : public LuaDialogWidget
357 LuaDialogSpinBox (std::string const& key, std::string const& title, double lower, double upper, double dflt, double step, int digits)
358 : LuaDialogWidget (key, title)
359 , _adj (dflt, lower, upper, step, step, 0)
362 _spin.set_digits (digits);
365 Gtk::Widget* widget ()
370 void assign (luabridge::LuaRef* rv) const
372 (*rv)[_key] = _adj.get_value ();
376 Gtk::Adjustment _adj;
377 Gtk::SpinButton _spin;
380 class LuaDialogRadio : public LuaDialogWidget
383 LuaDialogRadio (std::string const& key, std::string const& title, luabridge::LuaRef values, std::string const& dflt)
384 : LuaDialogWidget (key, title)
387 for (luabridge::Iterator i (values); !i.isNil (); ++i) {
388 if (!i.key ().isString ()) { continue; }
389 std::string key = i.key ().cast<std::string> ();
390 Gtk::RadioButton* rb = Gtk::manage (new Gtk::RadioButton (_group, key));
391 _hbox.pack_start (*rb);
392 luabridge::LuaRef* ref = new luabridge::LuaRef (i.value ());
393 _refs.push_back (ref);
394 if (!_rv) { _rv = ref; }
395 rb->signal_toggled ().connect (sigc::bind (
396 sigc::mem_fun (*this, &LuaDialogRadio::rb_toggled), rb, ref
407 for (std::vector<luabridge::LuaRef*>::const_iterator i = _refs.begin (); i != _refs.end (); ++i) {
413 Gtk::Widget* widget ()
418 void assign (luabridge::LuaRef* rv) const
423 (*rv)[_key] = luabridge::Nil ();
428 LuaDialogRadio (LuaDialogRadio const&); // prevent cc
429 void rb_toggled (Gtk::RadioButton* b, luabridge::LuaRef* rv) {
430 if (b->get_active ()) {
436 Gtk::RadioButtonGroup _group;
437 std::vector<luabridge::LuaRef*> _refs;
438 luabridge::LuaRef* _rv;
441 class LuaDialogDropDown : public LuaDialogWidget
444 LuaDialogDropDown (std::string const& key, std::string const& title, luabridge::LuaRef values, std::string const& dflt)
445 : LuaDialogWidget (key, title)
448 populate (_dd.items (), values, dflt);
451 ~LuaDialogDropDown ()
453 for (std::vector<luabridge::LuaRef*>::const_iterator i = _refs.begin (); i != _refs.end (); ++i) {
459 Gtk::Widget* widget ()
464 void assign (luabridge::LuaRef* rv) const
469 (*rv)[_key] = luabridge::Nil ();
474 void populate (Gtk::Menu_Helpers::MenuList& items, luabridge::LuaRef values, std::string const& dflt)
476 using namespace Gtk::Menu_Helpers;
477 std::vector<std::string> keys;
479 for (luabridge::Iterator i (values); !i.isNil (); ++i) {
480 if (!i.key ().isString ()) { continue; }
481 keys.push_back (i.key ().cast<std::string> ());
484 std::sort (keys.begin(), keys.end());
486 for (std::vector<std::string>::const_iterator i = keys.begin (); i != keys.end(); ++i) {
487 std::string key = *i;
489 if (values[key].isTable ()) {
490 Gtk::Menu* menu = Gtk::manage (new Gtk::Menu);
491 items.push_back (MenuElem (key, *menu));
492 populate (menu->items (), values[key], dflt);
495 luabridge::LuaRef* ref = new luabridge::LuaRef (values[key]);
496 _refs.push_back (ref);
497 items.push_back (MenuElem (key,
498 sigc::bind (sigc::mem_fun (*this, &LuaDialogDropDown::dd_select), key, ref)));
500 if (!_rv || key == dflt) {
507 void dd_select (std::string const& key, luabridge::LuaRef* rv) {
512 ArdourWidgets::ArdourDropdown _dd;
513 std::vector<luabridge::LuaRef*> _refs;
514 luabridge::LuaRef* _rv;
517 class LuaFileChooser : public LuaDialogWidget
520 LuaFileChooser (std::string const& key, std::string const& title, Gtk::FileChooserAction a, const std::string& path)
521 : LuaDialogWidget (key, title)
524 Gtkmm2ext::add_volume_shortcuts (_fc);
525 if (!path.empty ()) {
527 case Gtk::FILE_CHOOSER_ACTION_OPEN:
528 case Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER:
529 _fc.set_filename (path);
531 case Gtk::FILE_CHOOSER_ACTION_SAVE:
532 case Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER:
533 /* not supported by Gtk::FileChooserButton */
539 Gtk::Widget* widget ()
544 void assign (luabridge::LuaRef* rv) const
546 (*rv)[_key] = std::string (_fc.get_filename ());
550 Gtk::FileChooserButton _fc;
554 class LuaFileChooserWidget : public LuaDialogWidget
557 LuaFileChooserWidget (std::string const& key, std::string const& title, Gtk::FileChooserAction a, const std::string& path)
558 : LuaDialogWidget (key, title)
561 Gtkmm2ext::add_volume_shortcuts (_fc);
562 if (!path.empty ()) {
564 case Gtk::FILE_CHOOSER_ACTION_OPEN:
565 case Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER:
566 _fc.set_filename (path);
568 case Gtk::FILE_CHOOSER_ACTION_SAVE:
569 case Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER:
570 _fc.set_filename (path);
571 _fc.set_current_name (Glib::path_get_basename (path));
578 Gtk::Widget* widget ()
583 void assign (luabridge::LuaRef* rv) const
585 (*rv)[_key] = std::string (_fc.get_filename ());
589 Gtk::FileChooserWidget _fc;
592 /* *****************************************************************************
593 * Lua Parameter Dialog
596 Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
597 :_ad (title, true, false)
600 if (!lr.isTable ()) {
603 for (luabridge::Iterator i (lr); !i.isNil (); ++i) {
604 if (!i.key ().isNumber ()) { continue; }
605 if (!i.value ().isTable ()) { continue; }
606 if (!i.value ()["title"].isString ()) { continue; }
607 if (!i.value ()["type"].isString ()) { continue; }
609 std::string title = i.value ()["title"].cast<std::string> ();
610 std::string type = i.value ()["type"].cast<std::string> ();
613 if (i.value ()["key"].isString ()) {
614 key = i.value ()["key"].cast<std::string> ();
617 LuaDialogWidget* w = NULL;
619 if (type == "heading") {
620 Gtk::AlignmentEnum xalign = Gtk::ALIGN_CENTER;
621 if (i.value ()["align"].isString ()) {
622 std::string align = i.value ()["align"].cast <std::string> ();
623 if (align == "left") {
624 xalign = Gtk::ALIGN_LEFT;
625 } else if (align == "right") {
626 xalign = Gtk::ALIGN_RIGHT;
629 w = new LuaDialogHeading (title, xalign);
630 } else if (type == "label") {
631 Gtk::AlignmentEnum xalign = Gtk::ALIGN_CENTER;
632 if (i.value ()["align"].isString ()) {
633 std::string align = i.value ()["align"].cast <std::string> ();
634 if (align == "left") {
635 xalign = Gtk::ALIGN_LEFT;
636 } else if (align == "right") {
637 xalign = Gtk::ALIGN_RIGHT;
640 w = new LuaDialogLabel (title, xalign);
641 } else if (type == "hseparator") {
642 w = new LuaHSeparator ();
644 /* the following widgets do require a key */
645 else if (key.empty ()) {
648 else if (type == "checkbox") {
650 if (i.value ()["default"].isBoolean ()) {
651 dflt = i.value ()["default"].cast<bool> ();
653 w = new LuaDialogCheckbox (key, title, dflt);
654 } else if (type == "entry") {
656 if (i.value ()["default"].isString ()) {
657 dflt = i.value ()["default"].cast<std::string> ();
659 w = new LuaDialogEntry (key, title, dflt);
660 } else if (type == "radio") {
662 if (!i.value ()["values"].isTable ()) {
665 if (i.value ()["default"].isString ()) {
666 dflt = i.value ()["default"].cast<std::string> ();
668 w = new LuaDialogRadio (key, title, i.value ()["values"], dflt);
669 } else if (type == "fader") {
671 if (i.value ()["default"].isNumber ()) {
672 dflt = i.value ()["default"].cast<double> ();
674 w = new LuaDialogFader (key, title, dflt);
675 } else if (type == "slider") {
676 double lower, upper, dflt;
678 if (!i.value ()["min"].isNumber ()) { continue; }
679 if (!i.value ()["max"].isNumber ()) { continue; }
680 lower = i.value ()["min"].cast<double> ();
681 upper = i.value ()["max"].cast<double> ();
682 if (i.value ()["default"].isNumber ()) {
683 dflt = i.value ()["default"].cast<double> ();
687 if (i.value ()["digits"].isNumber ()) {
688 digits = i.value ()["digits"].cast<int> ();
690 w = new LuaDialogSlider (key, title, lower, upper, dflt, digits, i.value ()["scalepoints"]);
691 } else if (type == "number") {
692 double lower, upper, dflt, step;
694 if (!i.value ()["min"].isNumber ()) { continue; }
695 if (!i.value ()["max"].isNumber ()) { continue; }
696 lower = i.value ()["min"].cast<double> ();
697 upper = i.value ()["max"].cast<double> ();
698 if (i.value ()["default"].isNumber ()) {
699 dflt = i.value ()["default"].cast<double> ();
703 if (i.value ()["step"].isNumber ()) {
704 step = i.value ()["step"].cast<double> ();
708 if (i.value ()["digits"].isNumber ()) {
709 digits = i.value ()["digits"].cast<int> ();
711 w = new LuaDialogSpinBox (key, title, lower, upper, dflt, step, digits);
712 } else if (type == "dropdown") {
714 if (!i.value ()["values"].isTable ()) {
717 if (i.value ()["default"].isString ()) {
718 dflt = i.value ()["default"].cast<std::string> ();
720 w = new LuaDialogDropDown (key, title, i.value ()["values"], dflt);
721 } else if (type == "file") {
723 if (i.value ()["path"].isString ()) {
724 path = i.value ()["path"].cast<std::string> ();
726 w = new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_OPEN, path);
727 } else if (type == "folder") {
729 if (i.value ()["path"].isString ()) {
730 path = i.value ()["path"].cast<std::string> ();
732 w = new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, path);
733 } else if (type == "createfile") {
735 if (i.value ()["path"].isString ()) {
736 path = i.value ()["path"].cast<std::string> ();
738 w = new LuaFileChooserWidget (key, title, Gtk::FILE_CHOOSER_ACTION_SAVE, path);
739 } else if (type == "createdir") {
741 if (i.value ()["path"].isString ()) {
742 path = i.value ()["path"].cast<std::string> ();
744 w = new LuaFileChooserWidget (key, title, Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER, path);
745 } else if (type == "color") {
746 w = new LuaColorPicker (key);
750 if (i.value ()["col"].isNumber ()) {
751 w->set_col (i.value ()["col"].cast<int> ());
753 if (i.value ()["colspan"].isNumber ()) {
754 w->set_span (i.value ()["colspan"].cast<int> ());
756 _widgets.push_back(w);
760 _ad.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
761 _ad.add_button (Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
763 Gtk::Table* table = Gtk::manage (new Gtk::Table ());
764 table->set_col_spacings (20);
765 table->set_row_spacings (8);
766 table->signal_size_allocate ().connect (sigc::mem_fun (this, &Dialog::table_size_alloc));
768 _scroller.set_shadow_type(Gtk::SHADOW_NONE);
769 _scroller.set_border_width(0);
770 _scroller.add (*table);
771 _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
773 _ad.get_vbox ()->pack_start (_scroller);
778 for (DialogWidgets::const_iterator i = _widgets.begin (); i != _widgets.end (); ++i) {
779 int col = (*i)->col();
780 int cend = col + (*i)->span();
782 if (col < last_end) {
787 std::string const& label = (*i)->label ();
788 if (!label.empty ()) {
789 /* items with implicit label (title) */
790 Gtk::Label* lbl = Gtk::manage (new Gtk::Label (label + ":", Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
791 if (cend - col > 1) {
792 table->attach (*lbl, col, col + 1, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
793 table->attach (*((*i)->widget ()), col + 1, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
795 Gtk::HBox* hb = Gtk::manage (new Gtk::HBox());
797 hb->pack_start (*lbl, true, false);
798 hb->pack_start (*(*i)->widget (), true, false);
799 table->attach (*hb, col, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
802 table->attach (*((*i)->widget ()), col, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
809 for (DialogWidgets::const_iterator i = _widgets.begin (); i != _widgets.end () ; ++i) {
816 Dialog::run (lua_State *L)
818 _ad.get_vbox ()->show_all ();
819 switch (_ad.run ()) {
820 case Gtk::RESPONSE_ACCEPT:
827 luabridge::LuaRef rv (luabridge::newTable (L));
828 for (DialogWidgets::const_iterator i = _widgets.begin (); i != _widgets.end () ; ++i) {
831 luabridge::push (L, rv);
836 Dialog::table_size_alloc (Gtk::Allocation& allocation)
838 /* XXX: consider using 0.75 * screen-height instead of 512 */
839 if (allocation.get_height () > 512) {
840 _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
841 _ad.set_size_request (-1, 512);
845 /* *****************************************************************************
846 * Lua Progress Dialog
849 ProgressWindow::ProgressWindow (std::string const& title, bool allow_cancel)
850 : ArdourDialog (title, true)
853 _bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
855 set_border_width (12);
856 get_vbox()->set_spacing (6);
857 get_vbox()->pack_start (_bar, false, false);
861 Button* b = add_button (Stock::CANCEL, RESPONSE_CANCEL);
862 b->signal_clicked().connect (sigc::mem_fun (*this, &ProgressWindow::cancel_clicked));
865 set_default_size (200, -1);
870 ProgressWindow::progress (float prog, std::string const& text)
872 if (!text.empty ()) {
873 _bar.set_text (text);
875 if (prog < 0 || prog > 1) {
876 std::cerr << "pulse\n";
877 _bar.set_pulse_step(.1);
880 _bar.set_fraction (prog);
887 ProgressWindow::done () {
888 Gtk::Dialog::response(_canceled ? Gtk::RESPONSE_CANCEL : Gtk::RESPONSE_OK);