07e7fbeca85caf3f2dec1e08c9972741a505c0cb
[ardour.git] / gtk2_ardour / option_editor.h
1 /*
2     Copyright (C) 2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef __gtk_ardour_option_editor_h__
21 #define __gtk_ardour_option_editor_h__
22
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 "gtkmm2ext/slider_controller.h"
29 #include "ardour_window.h"
30 #include "audio_clock.h"
31 #include "ardour/types.h"
32
33 /** @file option_editor.h
34  *  @brief Base class for option editing dialog boxes.
35  *
36  *  Code to provided the basis for dialogs which allow the user to edit options
37  *  from an ARDOUR::Configuration class.
38  *
39  *  The idea is that we have an OptionEditor class which is the dialog box.
40  *  This is essentially a GTK Notebook.  OptionEditorComponent objects can
41  *  then be added to the OptionEditor, and these components are arranged on
42  *  the pages of the Notebook.  There is also an OptionEditorComponent hierarchy
43  *  here, providing things like boolean and combobox option components.
44  *
45  *  It is intended that OptionEditor be subclassed to implement a particular
46  *  options dialog.
47  */
48
49 namespace PBD {
50         class Configuration;
51 }
52
53 class OptionEditorPage;
54
55 /** Base class for components of an OptionEditor dialog */
56 class OptionEditorComponent
57 {
58 public:
59         virtual ~OptionEditorComponent() {}
60
61         /** Called when a configuration parameter's value has changed.
62          *  @param p parameter name
63          */
64         virtual void parameter_changed (std::string const & p) = 0;
65
66         /** Called to instruct the object to set its UI state from the configuration */
67         virtual void set_state_from_config () = 0;
68
69         /** Called to instruct the object to add itself to an OptionEditorPage */
70         virtual void add_to_page (OptionEditorPage *) = 0;
71
72         void add_widget_to_page (OptionEditorPage*, Gtk::Widget*);
73         void add_widgets_to_page (OptionEditorPage*, Gtk::Widget*, Gtk::Widget*);
74
75         void set_note (std::string const &);
76
77         virtual Gtk::Widget& tip_widget() = 0;
78
79 private:
80         void maybe_add_note (OptionEditorPage *, int);
81
82         std::string _note;
83 };
84
85 /** A component which provides a subheading within the dialog */
86 class OptionEditorHeading : public OptionEditorComponent
87 {
88 public:
89         OptionEditorHeading (std::string const &);
90
91         void parameter_changed (std::string const &) {}
92         void set_state_from_config () {}
93         void add_to_page (OptionEditorPage *);
94
95         Gtk::Widget& tip_widget() { return *_label; }
96
97 private:
98         Gtk::Label* _label; ///< the label used for the heading
99 };
100
101 /** A component which provides a box into which a subclass can put arbitrary widgets */
102 class OptionEditorBox : public OptionEditorComponent
103 {
104 public:
105
106         /** Construct an OpenEditorBox */
107         OptionEditorBox ()
108         {
109                 _box = Gtk::manage (new Gtk::VBox);
110                 _box->set_spacing (4);
111         }
112
113         void parameter_changed (std::string const &) = 0;
114         void set_state_from_config () = 0;
115         void add_to_page (OptionEditorPage *);
116
117         Gtk::Widget& tip_widget() { return *_box->children().front().get_widget(); }
118
119 protected:
120
121         Gtk::VBox* _box; ///< constituent box for subclasses to add widgets to
122 };
123
124 class RcActionButton : public OptionEditorComponent
125 {
126 public:
127         RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l = "");
128         void add_to_page (OptionEditorPage *);
129
130         void parameter_changed (std::string const & p) {}
131         void set_state_from_config () {}
132         Gtk::Widget& tip_widget() { return *_button; }
133
134 protected:
135         Gtk::Button* _button;
136         Gtk::Label* _label;
137         std::string _name;
138 };
139
140 /** Base class for components which provide UI to change an option */
141 class Option : public OptionEditorComponent
142 {
143 public:
144         /** Construct an Option.
145          *  @param i Option id (e.g. "plugins-stop-with-transport")
146          *  @param n User-visible name (e.g. "Stop plugins when the transport is stopped")
147          */
148         Option (std::string const & i,
149                 std::string const & n
150                 )
151                 : _id (i),
152                   _name (n)
153         {}
154
155         void parameter_changed (std::string const & p)
156         {
157                 if (p == _id) {
158                         set_state_from_config ();
159                 }
160         }
161
162         virtual void set_state_from_config () = 0;
163         virtual void add_to_page (OptionEditorPage*) = 0;
164
165         std::string id () const {
166                 return _id;
167         }
168
169 protected:
170
171         std::string _id;
172         std::string _name;
173 };
174
175 /** Component which provides the UI to handle a boolean option using a GTK CheckButton */
176 class BoolOption : public Option
177 {
178 public:
179
180         BoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
181         void set_state_from_config ();
182         void add_to_page (OptionEditorPage*);
183
184         void set_sensitive (bool yn) {
185                 _button->set_sensitive (yn);
186         }
187
188         Gtk::Widget& tip_widget() { return *_button; }
189
190 protected:
191
192         virtual void toggled ();
193
194         sigc::slot<bool>       _get; ///< slot to get the configuration variable's value
195         sigc::slot<bool, bool> _set;  ///< slot to set the configuration variable's value
196         Gtk::CheckButton*      _button; ///< UI button
197         Gtk::Label*            _label; ///< label for button, so we can use markup
198 };
199
200 class RouteDisplayBoolOption : public BoolOption
201 {
202 public:
203         RouteDisplayBoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
204 protected:
205         virtual void toggled ();
206 };
207
208 /** Component which allows to add any GTK Widget - intended for single buttons and custom stateless objects */
209 class FooOption : public OptionEditorComponent
210 {
211 public:
212         FooOption (Gtk::Widget *w) : _w (w) {}
213
214         void add_to_page (OptionEditorPage* p) {
215                 add_widget_to_page (p, _w);
216         }
217
218         Gtk::Widget& tip_widget() { return *_w; }
219         void set_state_from_config () {}
220         void parameter_changed (std::string const &) {}
221 private:
222         Gtk::Widget *_w;
223 };
224
225 /** Component which provides the UI to handle a string option using a GTK Entry */
226 class EntryOption : public Option
227 {
228 public:
229
230         EntryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
231         void set_state_from_config ();
232         void add_to_page (OptionEditorPage*);
233         void set_sensitive (bool);
234         void set_invalid_chars (std::string i) { _invalid = i; }
235
236         Gtk::Widget& tip_widget() { return *_entry; }
237
238 private:
239
240         void activated ();
241         bool focus_out (GdkEventFocus*);
242         void filter_text (const Glib::ustring&, int*);
243
244         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
245         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
246         Gtk::Label* _label; ///< UI label
247         Gtk::Entry* _entry; ///< UI entry
248         std::string _invalid;
249 };
250
251
252 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
253  *  The template parameter is the enumeration.
254  */
255 template <class T>
256 class ComboOption : public Option
257 {
258 public:
259
260         /** Construct an ComboOption.
261          *  @param i id
262          *  @param n User-visible name.
263          *  @param g Slot to get the variable's value.
264          *  @param s Slot to set the variable's value.
265          */
266         ComboOption (
267                 std::string const & i,
268                 std::string const & n,
269                 sigc::slot<T> g,
270                 sigc::slot<bool, T> s
271                 )
272                 : Option (i, n),
273                   _get (g),
274                   _set (s)
275         {
276                 _label = Gtk::manage (new Gtk::Label (n + ":"));
277                 _label->set_alignment (0, 0.5);
278                 _combo = Gtk::manage (new Gtk::ComboBoxText);
279                 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboOption::changed));
280         }
281
282         void set_state_from_config () {
283                 uint32_t r = 0;
284                 while (r < _options.size() && _get () != _options[r]) {
285                         ++r;
286                 }
287
288                 if (r < _options.size()) {
289                         _combo->set_active (r);
290                 }
291         }
292
293         void add_to_page (OptionEditorPage* p)
294         {
295                 add_widgets_to_page (p, _label, _combo);
296         }
297
298         /** Add an allowed value for this option.
299          *  @param e Enumeration.
300          *  @param o User-visible name for this value.
301          */
302         void add (T e, std::string const & o) {
303                 _options.push_back (e);
304                 _combo->append_text (o);
305         }
306
307         void clear () {
308                 _combo->clear_items();
309                 _options.clear ();
310         }
311
312         void changed () {
313                 uint32_t const r = _combo->get_active_row_number ();
314                 if (r < _options.size()) {
315                         _set (_options[r]);
316                 }
317         }
318
319         void set_sensitive (bool yn) {
320                 _combo->set_sensitive (yn);
321         }
322
323         Gtk::Widget& tip_widget() { return *_combo; }
324
325 private:
326
327         sigc::slot<T> _get;
328         sigc::slot<bool, T> _set;
329         Gtk::Label* _label;
330         Gtk::ComboBoxText* _combo;
331         std::vector<T> _options;
332 };
333
334
335 /** Component which provides the UI for a GTK HScale.
336  */
337 class HSliderOption : public Option
338 {
339 public:
340
341         /** Construct an ComboOption.
342          *  @param i id
343          *  @param n User-visible name.
344          *  @param g Slot to get the variable's value.
345          *  @param s Slot to set the variable's value.
346          */
347         HSliderOption (
348                 std::string const & i,
349                 std::string const & n,
350                 Gtk::Adjustment &adj
351                 )
352                 : Option (i, n)
353         {
354                 _label = Gtk::manage (new Gtk::Label (n + ":"));
355                 _label->set_alignment (0, 0.5);
356                 _hscale = Gtk::manage (new Gtk::HScale(adj));
357                 _adj = NULL;
358         }
359
360         HSliderOption (
361                 std::string const & i,
362                 std::string const & n,
363                 Gtk::Adjustment *adj,
364                 sigc::slot<float> g,
365                 sigc::slot<bool, float> s
366                 )
367                 : Option (i, n)
368                 , _get (g)
369                 , _set (s)
370                 , _adj (adj)
371         {
372                 _label = Gtk::manage (new Gtk::Label (n + ":"));
373                 _label->set_alignment (0, 0.5);
374                 _hscale = Gtk::manage (new Gtk::HScale(*_adj));
375                 _adj->signal_value_changed().connect (sigc::mem_fun (*this, &HSliderOption::changed));
376         }
377
378         void set_state_from_config () {
379                 if (_adj) _adj->set_value (_get());
380         }
381
382         void changed () {
383                 if (_adj) _set (_adj->get_value ());
384         }
385
386         void add_to_page (OptionEditorPage* p)
387         {
388                 add_widgets_to_page (p, _label, _hscale);
389         }
390
391         void set_sensitive (bool yn) {
392                 _hscale->set_sensitive (yn);
393         }
394
395         Gtk::Widget& tip_widget() { return *_hscale; }
396         Gtk::HScale& scale() { return *_hscale; }
397
398 private:
399         sigc::slot<float> _get;
400         sigc::slot<bool, float> _set;
401         Gtk::Label* _label;
402         Gtk::HScale* _hscale;
403         Gtk::Adjustment* _adj;
404 };
405
406 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
407  *  The template parameter is the enumeration.
408  */
409 class ComboStringOption : public Option
410 {
411 public:
412
413         /** Construct an ComboOption.
414          *  @param i id
415          *  @param n User-visible name.
416          *  @param g Slot to get the variable's value.
417          *  @param s Slot to set the variable's value.
418          */
419         ComboStringOption (
420                 std::string const & i,
421                 std::string const & n,
422                 sigc::slot<std::string> g,
423                 sigc::slot<bool, std::string> s
424                 )
425                 : Option (i, n),
426                   _get (g),
427                   _set (s)
428         {
429                 _label = Gtk::manage (new Gtk::Label (n + ":"));
430                 _label->set_alignment (0, 0.5);
431                 _combo = Gtk::manage (new Gtk::ComboBoxText);
432                 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboStringOption::changed));
433         }
434
435         void set_state_from_config () {
436                 _combo->set_active_text (_get());
437         }
438
439         void add_to_page (OptionEditorPage* p)
440         {
441                 add_widgets_to_page (p, _label, _combo);
442         }
443
444         /** Set the allowed strings for this option
445          *  @param strings a vector of allowed strings
446          */
447         void set_popdown_strings (const std::vector<std::string>& strings) {
448                 _combo->clear_items ();
449                 for (std::vector<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i) {
450                         _combo->append_text (*i);
451                 }
452         }
453
454         void clear () {
455                 _combo->clear_items();
456         }
457
458         void changed () {
459                 _set (_combo->get_active_text ());
460         }
461
462         void set_sensitive (bool yn) {
463                 _combo->set_sensitive (yn);
464         }
465
466         Gtk::Widget& tip_widget() { return *_combo; }
467
468 private:
469         sigc::slot<std::string> _get;
470         sigc::slot<bool, std::string> _set;
471         Gtk::Label* _label;
472         Gtk::ComboBoxText* _combo;
473 };
474
475
476 /** Component which provides the UI to handle a boolean option which needs
477  *  to be represented as a ComboBox to be clear to the user.
478  */
479 class BoolComboOption : public Option
480 {
481 public:
482
483         BoolComboOption (
484                 std::string const &,
485                 std::string const &,
486                 std::string const &,
487                 std::string const &,
488                 sigc::slot<bool>,
489                 sigc::slot<bool, bool>
490                 );
491
492         void set_state_from_config ();
493         void add_to_page (OptionEditorPage *);
494         void changed ();
495         void set_sensitive (bool);
496
497         Gtk::Widget& tip_widget() { return *_combo; }
498
499 private:
500
501         sigc::slot<bool> _get;
502         sigc::slot<bool, bool> _set;
503         Gtk::Label* _label;
504         Gtk::ComboBoxText* _combo;
505 };
506
507
508
509 /** Component which provides the UI to handle an numeric option using a GTK SpinButton */
510 template <class T>
511 class SpinOption : public Option
512 {
513 public:
514         /** Construct an SpinOption.
515          *  @param i id
516          *  @param n User-visible name.
517          *  @param g Slot to get the variable's value.
518          *  @param s Slot to set the variable's value.
519          *  @param min Variable minimum value.
520          *  @param max Variable maximum value.
521          *  @param step Step for the spin button.
522          *  @param page Page step for the spin button.
523          *  @param unit Unit name.
524          *  @param scale Scaling factor (such that for a value x in the spinbutton, x * scale is written to the config)
525          *  @param digits Number of decimal digits to show.
526          */
527         SpinOption (
528                 std::string const & i,
529                 std::string const & n,
530                 sigc::slot<T> g,
531                 sigc::slot<bool, T> s,
532                 T min,
533                 T max,
534                 T step,
535                 T page,
536                 std::string const & unit = "",
537                 float scale = 1,
538                 unsigned digits = 0
539                 )
540                 : Option (i, n),
541                   _get (g),
542                   _set (s),
543                   _scale (scale)
544         {
545                 _label = Gtk::manage (new Gtk::Label (n + ":"));
546                 _label->set_alignment (0, 0.5);
547
548                 _spin = Gtk::manage (new Gtk::SpinButton);
549                 _spin->set_range (min, max);
550                 _spin->set_increments (step, page);
551                 _spin->set_digits(digits);
552
553                 _box = Gtk::manage (new Gtk::HBox);
554                 _box->pack_start (*_spin, true, true);
555                 _box->set_spacing (4);
556                 if (unit.length()) {
557                         _box->pack_start (*Gtk::manage (new Gtk::Label (unit)), false, false);
558                 }
559
560                 _spin->signal_value_changed().connect (sigc::mem_fun (*this, &SpinOption::changed));
561         }
562
563         void set_state_from_config ()
564         {
565                 _spin->set_value (_get () / _scale);
566         }
567
568         void add_to_page (OptionEditorPage* p)
569         {
570                 add_widgets_to_page (p, _label, _box);
571         }
572
573         void changed ()
574         {
575                 _set (static_cast<T> (_spin->get_value ()) * _scale);
576         }
577
578         Gtk::Widget& tip_widget() { return *_spin; }
579
580 private:
581         sigc::slot<T> _get;
582         sigc::slot<bool, T> _set;
583         float _scale;
584         Gtk::Label* _label;
585         Gtk::HBox* _box;
586         Gtk::SpinButton* _spin;
587 };
588
589 class FaderOption : public Option
590 {
591 public:
592
593         FaderOption (std::string const &, std::string const &, sigc::slot<ARDOUR::gain_t> g, sigc::slot<bool, ARDOUR::gain_t> s);
594         void set_state_from_config ();
595         void add_to_page (OptionEditorPage *);
596
597         Gtk::Widget& tip_widget() { return *_db_slider; }
598
599 private:
600         void db_changed ();
601         void on_activate ();
602         bool on_key_press (GdkEventKey* ev);
603
604         Gtk::Adjustment _db_adjustment;
605         Gtkmm2ext::HSliderController* _db_slider;
606         Gtk::Entry _db_display;
607         Gtk::Label _label;
608         Gtk::HBox _box;
609         Gtk::VBox _fader_centering_box;
610         sigc::slot<ARDOUR::gain_t> _get;
611         sigc::slot<bool, ARDOUR::gain_t> _set;
612 };
613
614 class ClockOption : public Option
615 {
616 public:
617         ClockOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
618         void set_state_from_config ();
619         void add_to_page (OptionEditorPage *);
620         void set_session (ARDOUR::Session *);
621
622         Gtk::Widget& tip_widget() { return _clock; }
623         AudioClock& clock() { return _clock; }
624
625 private:
626         void save_clock_time ();
627         Gtk::Label _label;
628         AudioClock _clock;
629         sigc::slot<std::string> _get;
630         sigc::slot<bool, std::string> _set;
631         ARDOUR::Session *_session;
632 };
633
634 class DirectoryOption : public Option
635 {
636 public:
637         DirectoryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
638
639         void set_state_from_config ();
640         void add_to_page (OptionEditorPage *);
641
642         Gtk::Widget& tip_widget() { return _file_chooser; }
643
644 private:
645         void selection_changed ();
646
647         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
648         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
649         Gtk::FileChooserButton _file_chooser;
650 };
651
652 /** Class to represent a single page in an OptionEditor's notebook.
653  *  Pages are laid out using a 3-column table; the 1st column is used
654  *  to indent non-headings, and the 2nd and 3rd for actual content.
655  */
656 class OptionEditorPage
657 {
658 public:
659         OptionEditorPage (Gtk::Notebook&, std::string const &);
660
661         Gtk::VBox box;
662         Gtk::Table table;
663         std::list<OptionEditorComponent*> components;
664 };
665
666 /** The OptionEditor dialog base class */
667 class OptionEditor : public ArdourWindow
668 {
669 public:
670         OptionEditor (PBD::Configuration *, std::string const &);
671         ~OptionEditor ();
672
673         void add_option (std::string const &, OptionEditorComponent *);
674         void add_page (std::string const &, Gtk::Widget& page_widget);
675
676         void set_current_page (std::string const &);
677
678 protected:
679
680         virtual void parameter_changed (std::string const &);
681
682         PBD::Configuration* _config;
683
684 private:
685
686         PBD::ScopedConnection config_connection;
687
688         Gtk::Notebook _notebook;
689         std::map<std::string, OptionEditorPage*> _pages;
690 };
691
692 #endif /* __gtk_ardour_option_editor_h__ */
693
694