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.
19 #ifndef __gtk_ardour_option_editor_h__
20 #define __gtk_ardour_option_editor_h__
22 #include <gtkmm/checkbutton.h>
23 #include <gtkmm/comboboxtext.h>
24 #include <gtkmm/filechooserbutton.h>
25 #include <gtkmm/label.h>
26 #include <gtkmm/notebook.h>
27 #include <gtkmm/scale.h>
28 #include <gtkmm/spinbutton.h>
29 #include <gtkmm/table.h>
30 #include <gtkmm/treestore.h>
31 #include <gtkmm/treeview.h>
32 #include <gtkmm/window.h>
34 #include "widgets/slider_controller.h"
36 #include "ardour_window.h"
37 #include "audio_clock.h"
38 #include "ardour/types.h"
40 /** @file option_editor.h
41 * @brief Base class for option editing dialog boxes.
43 * Code to provided the basis for dialogs which allow the user to edit options
44 * from an ARDOUR::Configuration class.
46 * The idea is that we have an OptionEditor class which is the dialog box.
47 * This is essentially a GTK Notebook. OptionEditorComponent objects can
48 * then be added to the OptionEditor, and these components are arranged on
49 * the pages of the Notebook. There is also an OptionEditorComponent hierarchy
50 * here, providing things like boolean and combobox option components.
52 * It is intended that OptionEditor be subclassed to implement a particular
60 class OptionEditorPage;
62 /** Base class for components of an OptionEditor dialog */
63 class OptionEditorComponent
66 virtual ~OptionEditorComponent() {}
68 /** Called when a configuration parameter's value has changed.
69 * @param p parameter name
71 virtual void parameter_changed (std::string const & p) = 0;
73 /** Called to instruct the object to set its UI state from the configuration */
74 virtual void set_state_from_config () = 0;
76 /** Called to instruct the object to add itself to an OptionEditorPage */
77 virtual void add_to_page (OptionEditorPage *) = 0;
79 void add_widget_to_page (OptionEditorPage*, Gtk::Widget*);
80 void add_widgets_to_page (OptionEditorPage*, Gtk::Widget*, Gtk::Widget*, bool expand = true);
82 void set_note (std::string const &);
84 virtual Gtk::Widget& tip_widget() = 0;
87 void maybe_add_note (OptionEditorPage *, int);
92 /** A component which provides a subheading within the dialog */
93 class OptionEditorHeading : public OptionEditorComponent
96 OptionEditorHeading (std::string const &);
98 void parameter_changed (std::string const &) {}
99 void set_state_from_config () {}
100 void add_to_page (OptionEditorPage *);
102 Gtk::Widget& tip_widget() { return *_label; }
105 Gtk::Label* _label; ///< the label used for the heading
108 /** Expanding layout helper to push elements to the left on a single column page */
109 class OptionEditorBlank : public OptionEditorComponent
112 OptionEditorBlank () {}
114 void parameter_changed (std::string const &) {}
115 void set_state_from_config () {}
116 void add_to_page (OptionEditorPage *);
118 Gtk::Widget& tip_widget() { return _dummy; }
121 Gtk::EventBox _dummy;
124 class RcConfigDisplay : public OptionEditorComponent
127 RcConfigDisplay (std::string const &, std::string const &, sigc::slot<std::string>, char s = '\0');
128 void add_to_page (OptionEditorPage *);
129 void parameter_changed (std::string const & p);
130 void set_state_from_config ();
131 Gtk::Widget& tip_widget() { return *_info; }
133 sigc::slot<std::string> _get;
140 class RcActionButton : public OptionEditorComponent
143 RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l = "");
144 void add_to_page (OptionEditorPage *);
146 void parameter_changed (std::string const & p) {}
147 void set_state_from_config () {}
148 Gtk::Widget& tip_widget() { return *_button; }
151 Gtk::Button* _button;
156 /** Base class for components which provide UI to change an option */
157 class Option : public OptionEditorComponent
160 /** Construct an Option.
161 * @param i Option id (e.g. "plugins-stop-with-transport")
162 * @param n User-visible name (e.g. "Stop plugins when the transport is stopped")
164 Option (std::string const & i,
165 std::string const & n
171 void parameter_changed (std::string const & p)
174 set_state_from_config ();
178 virtual void set_state_from_config () = 0;
179 virtual void add_to_page (OptionEditorPage*) = 0;
181 std::string id () const {
190 /** Component which provides the UI to handle a boolean option using a GTK CheckButton */
191 class BoolOption : public Option
194 BoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
195 void set_state_from_config ();
196 void add_to_page (OptionEditorPage*);
198 void set_sensitive (bool yn) {
199 _button->set_sensitive (yn);
202 Gtk::Widget& tip_widget() { return *_button; }
205 virtual void toggled ();
207 sigc::slot<bool> _get; ///< slot to get the configuration variable's value
208 sigc::slot<bool, bool> _set; ///< slot to set the configuration variable's value
209 Gtk::CheckButton* _button; ///< UI button
210 Gtk::Label* _label; ///< label for button, so we can use markup
213 class RouteDisplayBoolOption : public BoolOption
216 RouteDisplayBoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
219 virtual void toggled ();
222 /** Component which allows to add any GTK Widget - intended for single buttons and custom stateless objects */
223 class FooOption : public OptionEditorComponent
226 FooOption (Gtk::Widget *w) : _w (w) {}
228 void add_to_page (OptionEditorPage* p) {
229 add_widget_to_page (p, _w);
232 Gtk::Widget& tip_widget() { return *_w; }
233 void set_state_from_config () {}
234 void parameter_changed (std::string const &) {}
239 /** Component which provides the UI to handle a string option using a GTK Entry */
240 class EntryOption : public Option
243 EntryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
244 void set_state_from_config ();
245 void add_to_page (OptionEditorPage*);
246 void set_sensitive (bool);
247 void set_invalid_chars (std::string i) { _invalid = i; }
249 Gtk::Widget& tip_widget() { return *_entry; }
253 bool focus_out (GdkEventFocus*);
254 void filter_text (const Glib::ustring&, int*);
256 sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
257 sigc::slot<bool, std::string> _set; ///< slot to set the configuration variable's value
258 Gtk::Label* _label; ///< UI label
259 Gtk::Entry* _entry; ///< UI entry
260 std::string _invalid;
264 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
265 * The template parameter is the enumeration.
268 class ComboOption : public Option
271 /** Construct an ComboOption.
273 * @param n User-visible name.
274 * @param g Slot to get the variable's value.
275 * @param s Slot to set the variable's value.
278 std::string const & i,
279 std::string const & n,
281 sigc::slot<bool, T> s
287 _label = Gtk::manage (new Gtk::Label (n + ":"));
288 _label->set_alignment (0, 0.5);
289 _combo = Gtk::manage (new Gtk::ComboBoxText);
290 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboOption::changed));
293 void set_state_from_config ()
296 while (r < _options.size() && _get () != _options[r]) {
300 if (r < _options.size()) {
301 _combo->set_active (r);
305 void add_to_page (OptionEditorPage* p)
307 add_widgets_to_page (p, _label, _combo);
310 /** Add an allowed value for this option.
311 * @param e Enumeration.
312 * @param o User-visible name for this value.
314 void add (T e, std::string const & o)
316 _options.push_back (e);
317 _combo->append_text (o);
322 _combo->clear_items();
328 uint32_t const r = _combo->get_active_row_number ();
329 if (r < _options.size()) {
333 void set_sensitive (bool yn)
335 _combo->set_sensitive (yn);
338 Gtk::Widget& tip_widget() { return *_combo; }
342 sigc::slot<bool, T> _set;
344 Gtk::ComboBoxText* _combo;
345 std::vector<T> _options;
349 /** Component which provides the UI for a GTK HScale.
351 class HSliderOption : public Option
355 std::string const& i,
356 std::string const& n,
358 sigc::slot<bool, float> s,
359 double lower, double upper,
360 double step_increment = 1,
361 double page_increment = 10,
363 bool logarithmic = false
366 void set_state_from_config ();
367 virtual void changed ();
368 void add_to_page (OptionEditorPage* p);
369 void set_sensitive (bool yn);
371 Gtk::Widget& tip_widget() { return _hscale; }
372 Gtk::HScale& scale() { return _hscale; }
375 sigc::slot<float> _get;
376 sigc::slot<bool, float> _set;
377 Gtk::Adjustment _adj;
385 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
386 * The template parameter is the enumeration.
388 class ComboStringOption : public Option
391 /** Construct an ComboOption.
393 * @param n User-visible name.
394 * @param g Slot to get the variable's value.
395 * @param s Slot to set the variable's value.
398 std::string const & i,
399 std::string const & n,
400 sigc::slot<std::string> g,
401 sigc::slot<bool, std::string> s
404 void set_state_from_config ();
405 void add_to_page (OptionEditorPage* p);
407 /** Set the allowed strings for this option
408 * @param strings a vector of allowed strings
410 void set_popdown_strings (const std::vector<std::string>& strings);
414 void set_sensitive (bool yn);
416 Gtk::Widget& tip_widget() { return *_combo; }
419 sigc::slot<std::string> _get;
420 sigc::slot<bool, std::string> _set;
422 Gtk::ComboBoxText* _combo;
426 /** Component which provides the UI to handle a boolean option which needs
427 * to be represented as a ComboBox to be clear to the user.
429 class BoolComboOption : public Option
438 sigc::slot<bool, bool>
441 void set_state_from_config ();
442 void add_to_page (OptionEditorPage *);
444 void set_sensitive (bool);
446 Gtk::Widget& tip_widget() { return *_combo; }
449 sigc::slot<bool> _get;
450 sigc::slot<bool, bool> _set;
452 Gtk::ComboBoxText* _combo;
456 /** Component which provides the UI to handle an numeric option using a GTK SpinButton */
458 class SpinOption : public Option
461 /** Construct an SpinOption.
463 * @param n User-visible name.
464 * @param g Slot to get the variable's value.
465 * @param s Slot to set the variable's value.
466 * @param min Variable minimum value.
467 * @param max Variable maximum value.
468 * @param step Step for the spin button.
469 * @param page Page step for the spin button.
470 * @param unit Unit name.
471 * @param scale Scaling factor (such that for a value x in the spinbutton, x * scale is written to the config)
472 * @param digits Number of decimal digits to show.
475 std::string const & i,
476 std::string const & n,
478 sigc::slot<bool, T> s,
483 std::string const & unit = "",
492 _label = Gtk::manage (new Gtk::Label (n + ":"));
493 _label->set_alignment (0, 0.5);
495 _spin = Gtk::manage (new Gtk::SpinButton);
496 _spin->set_range (min, max);
497 _spin->set_increments (step, page);
498 _spin->set_digits(digits);
500 _box = Gtk::manage (new Gtk::HBox);
501 _box->pack_start (*_spin, true, true);
502 _box->set_spacing (4);
504 _box->pack_start (*Gtk::manage (new Gtk::Label (unit)), false, false);
507 _spin->signal_value_changed().connect (sigc::mem_fun (*this, &SpinOption::changed));
510 void set_state_from_config ()
512 _spin->set_value (_get () / _scale);
515 void add_to_page (OptionEditorPage* p)
517 add_widgets_to_page (p, _label, _box, false);
522 _set (static_cast<T> (_spin->get_value ()) * _scale);
525 Gtk::Widget& tip_widget() { return *_spin; }
529 sigc::slot<bool, T> _set;
533 Gtk::SpinButton* _spin;
536 class FaderOption : public Option
540 FaderOption (std::string const &, std::string const &, sigc::slot<ARDOUR::gain_t> g, sigc::slot<bool, ARDOUR::gain_t> s);
541 void set_state_from_config ();
542 void add_to_page (OptionEditorPage *);
544 Gtk::Widget& tip_widget() { return *_db_slider; }
549 bool on_key_press (GdkEventKey* ev);
551 Gtk::Adjustment _db_adjustment;
552 ArdourWidgets::HSliderController* _db_slider;
553 Gtk::Entry _db_display;
556 Gtk::VBox _fader_centering_box;
557 sigc::slot<ARDOUR::gain_t> _get;
558 sigc::slot<bool, ARDOUR::gain_t> _set;
561 class ClockOption : public Option
564 ClockOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
565 void set_state_from_config ();
566 void add_to_page (OptionEditorPage *);
567 void set_session (ARDOUR::Session *);
569 Gtk::Widget& tip_widget() { return _clock; }
570 AudioClock& clock() { return _clock; }
573 void save_clock_time ();
576 sigc::slot<std::string> _get;
577 sigc::slot<bool, std::string> _set;
578 ARDOUR::Session *_session;
581 class DirectoryOption : public Option
584 DirectoryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
586 void set_state_from_config ();
587 void add_to_page (OptionEditorPage *);
589 Gtk::Widget& tip_widget() { return _file_chooser; }
592 void selection_changed ();
594 sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
595 sigc::slot<bool, std::string> _set; ///< slot to set the configuration variable's value
596 Gtk::FileChooserButton _file_chooser;
599 /** Class to represent a single page in an OptionEditor's notebook.
600 * Pages are laid out using a 3-column table; the 1st column is used
601 * to indent non-headings, and the 2nd and 3rd for actual content.
603 class OptionEditorPage
606 OptionEditorPage (Gtk::Notebook&, std::string const &);
611 std::list<OptionEditorComponent*> components;
617 class OptionEditorMiniPage : public OptionEditorComponent, public OptionEditorPage
620 OptionEditorMiniPage ()
622 box.pack_start (table, false, false);
623 box.set_border_width (0);
626 void parameter_changed (std::string const &) = 0;
627 void set_state_from_config () = 0;
628 virtual void add_to_page (OptionEditorPage*);
630 Gtk::Widget& tip_widget() { return *table.children().front().get_widget(); }
633 /** The OptionEditor dialog base class */
634 class OptionEditor : virtual public sigc::trackable
637 OptionEditor (PBD::Configuration *);
638 virtual ~OptionEditor ();
640 void add_option (std::string const &, OptionEditorComponent *);
641 void add_page (std::string const &, Gtk::Widget& page_widget);
643 void set_current_page (std::string const &);
646 virtual void parameter_changed (std::string const &);
648 PBD::Configuration* _config;
649 Gtk::Notebook& notebook() { return _notebook; }
650 Gtk::TreeView& treeview() { return option_treeview; }
652 class OptionColumns : public Gtk::TreeModel::ColumnRecord
655 Gtk::TreeModelColumn<std::string> name;
656 Gtk::TreeModelColumn<Gtk::Widget*> widget;
664 OptionColumns option_columns;
665 Glib::RefPtr<Gtk::TreeStore> option_tree;
668 PBD::ScopedConnection config_connection;
669 Gtk::Notebook _notebook;
670 Gtk::TreeView option_treeview;
671 std::map<std::string, OptionEditorPage*> _pages;
673 void add_path_to_treeview (std::string const &, Gtk::Widget&);
674 Gtk::TreeModel::iterator find_path_in_treemodel (std::string const & pn,
675 bool create_missing = false);
676 void treeview_row_selected ();
679 /** The OptionEditor dialog-as-container base class */
680 class OptionEditorContainer : public OptionEditor, public Gtk::VBox
683 OptionEditorContainer (PBD::Configuration *, std::string const &);
684 ~OptionEditorContainer() {}
689 /** The OptionEditor dialog-as-container base class */
690 class OptionEditorWindow : public OptionEditor, public ArdourWindow
693 OptionEditorWindow (PBD::Configuration *, std::string const &);
694 ~OptionEditorWindow() {}
700 #endif /* __gtk_ardour_option_editor_h__ */