2 Copyright (C) 2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __gtk_ardour_option_editor_h__
21 #define __gtk_ardour_option_editor_h__
23 #include <gtkmm/notebook.h>
24 #include <gtkmm/checkbutton.h>
25 #include <gtkmm/comboboxtext.h>
26 #include <gtkmm/spinbutton.h>
27 #include <gtkmm/table.h>
28 #include <gtkmm/window.h>
30 #include "gtkmm2ext/slider_controller.h"
32 #include "ardour_window.h"
33 #include "audio_clock.h"
34 #include "ardour/types.h"
36 /** @file option_editor.h
37 * @brief Base class for option editing dialog boxes.
39 * Code to provided the basis for dialogs which allow the user to edit options
40 * from an ARDOUR::Configuration class.
42 * The idea is that we have an OptionEditor class which is the dialog box.
43 * This is essentially a GTK Notebook. OptionEditorComponent objects can
44 * then be added to the OptionEditor, and these components are arranged on
45 * the pages of the Notebook. There is also an OptionEditorComponent hierarchy
46 * here, providing things like boolean and combobox option components.
48 * It is intended that OptionEditor be subclassed to implement a particular
56 class OptionEditorPage;
58 /** Base class for components of an OptionEditor dialog */
59 class OptionEditorComponent
62 virtual ~OptionEditorComponent() {}
64 /** Called when a configuration parameter's value has changed.
65 * @param p parameter name
67 virtual void parameter_changed (std::string const & p) = 0;
69 /** Called to instruct the object to set its UI state from the configuration */
70 virtual void set_state_from_config () = 0;
72 /** Called to instruct the object to add itself to an OptionEditorPage */
73 virtual void add_to_page (OptionEditorPage *) = 0;
75 void add_widget_to_page (OptionEditorPage*, Gtk::Widget*);
76 void add_widgets_to_page (OptionEditorPage*, Gtk::Widget*, Gtk::Widget*);
78 void set_note (std::string const &);
80 virtual Gtk::Widget& tip_widget() = 0;
83 void maybe_add_note (OptionEditorPage *, int);
88 /** A component which provides a subheading within the dialog */
89 class OptionEditorHeading : public OptionEditorComponent
92 OptionEditorHeading (std::string const &);
94 void parameter_changed (std::string const &) {}
95 void set_state_from_config () {}
96 void add_to_page (OptionEditorPage *);
98 Gtk::Widget& tip_widget() { return *_label; }
101 Gtk::Label* _label; ///< the label used for the heading
104 /** A component which provides a box into which a subclass can put arbitrary widgets */
105 class OptionEditorBox : public OptionEditorComponent
109 /** Construct an OpenEditorBox */
112 _box = Gtk::manage (new Gtk::VBox);
113 _box->set_spacing (4);
116 void parameter_changed (std::string const &) = 0;
117 void set_state_from_config () = 0;
118 virtual void add_to_page (OptionEditorPage *);
120 Gtk::Widget& tip_widget() { return *_box->children().front().get_widget(); }
124 Gtk::VBox* _box; ///< constituent box for subclasses to add widgets to
127 class OptionEditorPageBox : public OptionEditorBox
130 virtual void add_to_page (OptionEditorPage *);
133 class RcConfigDisplay : public OptionEditorComponent
136 RcConfigDisplay (std::string const &, std::string const &, sigc::slot<std::string>, char s = '\0');
137 void add_to_page (OptionEditorPage *);
138 void parameter_changed (std::string const & p);
139 void set_state_from_config ();
140 Gtk::Widget& tip_widget() { return *_info; }
142 sigc::slot<std::string> _get;
149 class RcActionButton : public OptionEditorComponent
152 RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l = "");
153 void add_to_page (OptionEditorPage *);
155 void parameter_changed (std::string const & p) {}
156 void set_state_from_config () {}
157 Gtk::Widget& tip_widget() { return *_button; }
160 Gtk::Button* _button;
165 /** Base class for components which provide UI to change an option */
166 class Option : public OptionEditorComponent
169 /** Construct an Option.
170 * @param i Option id (e.g. "plugins-stop-with-transport")
171 * @param n User-visible name (e.g. "Stop plugins when the transport is stopped")
173 Option (std::string const & i,
174 std::string const & n
180 void parameter_changed (std::string const & p)
183 set_state_from_config ();
187 virtual void set_state_from_config () = 0;
188 virtual void add_to_page (OptionEditorPage*) = 0;
190 std::string id () const {
200 /** Component which provides the UI to handle a boolean option using a GTK CheckButton */
201 class BoolOption : public Option
205 BoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
206 void set_state_from_config ();
207 void add_to_page (OptionEditorPage*);
209 void set_sensitive (bool yn) {
210 _button->set_sensitive (yn);
213 Gtk::Widget& tip_widget() { return *_button; }
217 virtual void toggled ();
219 sigc::slot<bool> _get; ///< slot to get the configuration variable's value
220 sigc::slot<bool, bool> _set; ///< slot to set the configuration variable's value
221 Gtk::CheckButton* _button; ///< UI button
222 Gtk::Label* _label; ///< label for button, so we can use markup
225 class RouteDisplayBoolOption : public BoolOption
228 RouteDisplayBoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
230 virtual void toggled ();
233 /** Component which allows to add any GTK Widget - intended for single buttons and custom stateless objects */
234 class FooOption : public OptionEditorComponent
237 FooOption (Gtk::Widget *w) : _w (w) {}
239 void add_to_page (OptionEditorPage* p) {
240 add_widget_to_page (p, _w);
243 Gtk::Widget& tip_widget() { return *_w; }
244 void set_state_from_config () {}
245 void parameter_changed (std::string const &) {}
250 /** Component which provides the UI to handle a string option using a GTK Entry */
251 class EntryOption : public Option
255 EntryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
256 void set_state_from_config ();
257 void add_to_page (OptionEditorPage*);
258 void set_sensitive (bool);
259 void set_invalid_chars (std::string i) { _invalid = i; }
261 Gtk::Widget& tip_widget() { return *_entry; }
266 bool focus_out (GdkEventFocus*);
267 void filter_text (const Glib::ustring&, int*);
269 sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
270 sigc::slot<bool, std::string> _set; ///< slot to set the configuration variable's value
271 Gtk::Label* _label; ///< UI label
272 Gtk::Entry* _entry; ///< UI entry
273 std::string _invalid;
277 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
278 * The template parameter is the enumeration.
281 class ComboOption : public Option
285 /** Construct an ComboOption.
287 * @param n User-visible name.
288 * @param g Slot to get the variable's value.
289 * @param s Slot to set the variable's value.
292 std::string const & i,
293 std::string const & n,
295 sigc::slot<bool, T> s
301 _label = Gtk::manage (new Gtk::Label (n + ":"));
302 _label->set_alignment (0, 0.5);
303 _combo = Gtk::manage (new Gtk::ComboBoxText);
304 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboOption::changed));
307 void set_state_from_config () {
309 while (r < _options.size() && _get () != _options[r]) {
313 if (r < _options.size()) {
314 _combo->set_active (r);
318 void add_to_page (OptionEditorPage* p)
320 add_widgets_to_page (p, _label, _combo);
323 /** Add an allowed value for this option.
324 * @param e Enumeration.
325 * @param o User-visible name for this value.
327 void add (T e, std::string const & o) {
328 _options.push_back (e);
329 _combo->append_text (o);
333 _combo->clear_items();
338 uint32_t const r = _combo->get_active_row_number ();
339 if (r < _options.size()) {
344 void set_sensitive (bool yn) {
345 _combo->set_sensitive (yn);
348 Gtk::Widget& tip_widget() { return *_combo; }
353 sigc::slot<bool, T> _set;
355 Gtk::ComboBoxText* _combo;
356 std::vector<T> _options;
360 /** Component which provides the UI for a GTK HScale.
362 class HSliderOption : public Option
366 /** Construct an ComboOption.
368 * @param n User-visible name.
369 * @param g Slot to get the variable's value.
370 * @param s Slot to set the variable's value.
373 std::string const & i,
374 std::string const & n,
379 _label = Gtk::manage (new Gtk::Label (n + ":"));
380 _label->set_alignment (0, 0.5);
381 _hscale = Gtk::manage (new Gtk::HScale(adj));
386 std::string const & i,
387 std::string const & n,
388 Gtk::Adjustment *adj,
390 sigc::slot<bool, float> s
397 _label = Gtk::manage (new Gtk::Label (n + ":"));
398 _label->set_alignment (0, 0.5);
399 _hscale = Gtk::manage (new Gtk::HScale(*_adj));
400 _adj->signal_value_changed().connect (sigc::mem_fun (*this, &HSliderOption::changed));
403 void set_state_from_config () {
404 if (_adj) _adj->set_value (_get());
408 if (_adj) _set (_adj->get_value ());
411 void add_to_page (OptionEditorPage* p)
413 add_widgets_to_page (p, _label, _hscale);
416 void set_sensitive (bool yn) {
417 _hscale->set_sensitive (yn);
420 Gtk::Widget& tip_widget() { return *_hscale; }
421 Gtk::HScale& scale() { return *_hscale; }
424 sigc::slot<float> _get;
425 sigc::slot<bool, float> _set;
427 Gtk::HScale* _hscale;
428 Gtk::Adjustment* _adj;
431 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
432 * The template parameter is the enumeration.
434 class ComboStringOption : public Option
438 /** Construct an ComboOption.
440 * @param n User-visible name.
441 * @param g Slot to get the variable's value.
442 * @param s Slot to set the variable's value.
445 std::string const & i,
446 std::string const & n,
447 sigc::slot<std::string> g,
448 sigc::slot<bool, std::string> s
454 _label = Gtk::manage (new Gtk::Label (n + ":"));
455 _label->set_alignment (0, 0.5);
456 _combo = Gtk::manage (new Gtk::ComboBoxText);
457 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboStringOption::changed));
460 void set_state_from_config () {
461 _combo->set_active_text (_get());
464 void add_to_page (OptionEditorPage* p)
466 add_widgets_to_page (p, _label, _combo);
469 /** Set the allowed strings for this option
470 * @param strings a vector of allowed strings
472 void set_popdown_strings (const std::vector<std::string>& strings) {
473 _combo->clear_items ();
474 for (std::vector<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i) {
475 _combo->append_text (*i);
480 _combo->clear_items();
484 _set (_combo->get_active_text ());
487 void set_sensitive (bool yn) {
488 _combo->set_sensitive (yn);
491 Gtk::Widget& tip_widget() { return *_combo; }
494 sigc::slot<std::string> _get;
495 sigc::slot<bool, std::string> _set;
497 Gtk::ComboBoxText* _combo;
501 /** Component which provides the UI to handle a boolean option which needs
502 * to be represented as a ComboBox to be clear to the user.
504 class BoolComboOption : public Option
514 sigc::slot<bool, bool>
517 void set_state_from_config ();
518 void add_to_page (OptionEditorPage *);
520 void set_sensitive (bool);
522 Gtk::Widget& tip_widget() { return *_combo; }
526 sigc::slot<bool> _get;
527 sigc::slot<bool, bool> _set;
529 Gtk::ComboBoxText* _combo;
534 /** Component which provides the UI to handle an numeric option using a GTK SpinButton */
536 class SpinOption : public Option
539 /** Construct an SpinOption.
541 * @param n User-visible name.
542 * @param g Slot to get the variable's value.
543 * @param s Slot to set the variable's value.
544 * @param min Variable minimum value.
545 * @param max Variable maximum value.
546 * @param step Step for the spin button.
547 * @param page Page step for the spin button.
548 * @param unit Unit name.
549 * @param scale Scaling factor (such that for a value x in the spinbutton, x * scale is written to the config)
550 * @param digits Number of decimal digits to show.
553 std::string const & i,
554 std::string const & n,
556 sigc::slot<bool, T> s,
561 std::string const & unit = "",
570 _label = Gtk::manage (new Gtk::Label (n + ":"));
571 _label->set_alignment (0, 0.5);
573 _spin = Gtk::manage (new Gtk::SpinButton);
574 _spin->set_range (min, max);
575 _spin->set_increments (step, page);
576 _spin->set_digits(digits);
578 _box = Gtk::manage (new Gtk::HBox);
579 _box->pack_start (*_spin, true, true);
580 _box->set_spacing (4);
582 _box->pack_start (*Gtk::manage (new Gtk::Label (unit)), false, false);
585 _spin->signal_value_changed().connect (sigc::mem_fun (*this, &SpinOption::changed));
588 void set_state_from_config ()
590 _spin->set_value (_get () / _scale);
593 void add_to_page (OptionEditorPage* p)
595 add_widgets_to_page (p, _label, _box);
600 _set (static_cast<T> (_spin->get_value ()) * _scale);
603 Gtk::Widget& tip_widget() { return *_spin; }
607 sigc::slot<bool, T> _set;
611 Gtk::SpinButton* _spin;
614 class FaderOption : public Option
618 FaderOption (std::string const &, std::string const &, sigc::slot<ARDOUR::gain_t> g, sigc::slot<bool, ARDOUR::gain_t> s);
619 void set_state_from_config ();
620 void add_to_page (OptionEditorPage *);
622 Gtk::Widget& tip_widget() { return *_db_slider; }
627 bool on_key_press (GdkEventKey* ev);
629 Gtk::Adjustment _db_adjustment;
630 Gtkmm2ext::HSliderController* _db_slider;
631 Gtk::Entry _db_display;
634 Gtk::VBox _fader_centering_box;
635 sigc::slot<ARDOUR::gain_t> _get;
636 sigc::slot<bool, ARDOUR::gain_t> _set;
639 class ClockOption : public Option
642 ClockOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
643 void set_state_from_config ();
644 void add_to_page (OptionEditorPage *);
645 void set_session (ARDOUR::Session *);
647 Gtk::Widget& tip_widget() { return _clock; }
648 AudioClock& clock() { return _clock; }
651 void save_clock_time ();
654 sigc::slot<std::string> _get;
655 sigc::slot<bool, std::string> _set;
656 ARDOUR::Session *_session;
659 class DirectoryOption : public Option
662 DirectoryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
664 void set_state_from_config ();
665 void add_to_page (OptionEditorPage *);
667 Gtk::Widget& tip_widget() { return _file_chooser; }
670 void selection_changed ();
672 sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
673 sigc::slot<bool, std::string> _set; ///< slot to set the configuration variable's value
674 Gtk::FileChooserButton _file_chooser;
677 /** Class to represent a single page in an OptionEditor's notebook.
678 * Pages are laid out using a 3-column table; the 1st column is used
679 * to indent non-headings, and the 2nd and 3rd for actual content.
681 class OptionEditorPage
684 OptionEditorPage (Gtk::Notebook&, std::string const &);
689 std::list<OptionEditorComponent*> components;
695 /** The OptionEditor dialog base class */
696 class OptionEditor : virtual public sigc::trackable
699 OptionEditor (PBD::Configuration *);
700 virtual ~OptionEditor ();
702 void add_option (std::string const &, OptionEditorComponent *);
703 void add_page (std::string const &, Gtk::Widget& page_widget);
705 void set_current_page (std::string const &);
708 virtual void parameter_changed (std::string const &);
710 PBD::Configuration* _config;
711 Gtk::Notebook& notebook() { return _notebook; }
712 Gtk::TreeView& treeview() { return option_treeview; }
714 class OptionColumns : public Gtk::TreeModel::ColumnRecord
717 Gtk::TreeModelColumn<std::string> name;
718 Gtk::TreeModelColumn<Gtk::Widget*> widget;
726 OptionColumns option_columns;
727 Glib::RefPtr<Gtk::TreeStore> option_tree;
730 PBD::ScopedConnection config_connection;
731 Gtk::Notebook _notebook;
732 Gtk::TreeView option_treeview;
733 std::map<std::string, OptionEditorPage*> _pages;
735 void add_path_to_treeview (std::string const &, Gtk::Widget&);
736 Gtk::TreeModel::iterator find_path_in_treemodel (std::string const & pn,
737 bool create_missing = false);
738 void treeview_row_selected ();
741 /** The OptionEditor dialog-as-container base class */
742 class OptionEditorContainer : public OptionEditor, public Gtk::VBox
745 OptionEditorContainer (PBD::Configuration *, std::string const &);
746 ~OptionEditorContainer() {}
751 /** The OptionEditor dialog-as-container base class */
752 class OptionEditorWindow : public OptionEditor, public ArdourWindow
755 OptionEditorWindow (PBD::Configuration *, std::string const &);
756 ~OptionEditorWindow() {}
762 #endif /* __gtk_ardour_option_editor_h__ */