f42d1da9b738aa96a2084c98382f617b7560d66c
[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 ARDOUR {
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 /** Base class for components which provide UI to change an option */
125 class Option : public OptionEditorComponent
126 {
127 public:
128         /** Construct an Option.
129          *  @param i Option id (e.g. "plugins-stop-with-transport")
130          *  @param n User-visible name (e.g. "Stop plugins when the transport is stopped")
131          */
132         Option (std::string const & i,
133                 std::string const & n
134                 )
135                 : _id (i),
136                   _name (n)
137         {}
138
139         void parameter_changed (std::string const & p)
140         {
141                 if (p == _id) {
142                         set_state_from_config ();
143                 }
144         }
145
146         virtual void set_state_from_config () = 0;
147         virtual void add_to_page (OptionEditorPage*) = 0;
148
149         std::string id () const {
150                 return _id;
151         }
152
153 protected:
154
155         std::string _id;
156         std::string _name;
157 };
158
159 /** Component which provides the UI to handle a boolean option using a GTK CheckButton */
160 class BoolOption : public Option
161 {
162 public:
163
164         BoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
165         void set_state_from_config ();
166         void add_to_page (OptionEditorPage*);
167
168         void set_sensitive (bool yn) {
169                 _button->set_sensitive (yn);
170         }
171
172         Gtk::Widget& tip_widget() { return *_button; }
173
174 private:
175
176         void toggled ();
177
178         sigc::slot<bool>       _get; ///< slot to get the configuration variable's value
179         sigc::slot<bool, bool> _set;  ///< slot to set the configuration variable's value
180         Gtk::CheckButton*      _button; ///< UI button
181         Gtk::Label*            _label; ///< label for button, so we can use markup
182 };
183
184 /** Component which allows to add any GTK Widget - intended for single buttons and custom stateless objects */
185 class FooOption : public OptionEditorComponent
186 {
187 public:
188         FooOption (Gtk::Widget *w) : _w (w) {}
189
190         void add_to_page (OptionEditorPage* p) {
191                 add_widget_to_page (p, _w);
192         }
193
194         Gtk::Widget& tip_widget() { return *_w; }
195         void set_state_from_config () {}
196         void parameter_changed (std::string const &) {}
197 private:
198         Gtk::Widget *_w;
199 };
200
201 /** Component which provides the UI to handle a string option using a GTK Entry */
202 class EntryOption : public Option
203 {
204 public:
205
206         EntryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
207         void set_state_from_config ();
208         void add_to_page (OptionEditorPage*);
209         void set_sensitive (bool);
210         void set_invalid_chars (std::string i) { _invalid = i; }
211
212         Gtk::Widget& tip_widget() { return *_entry; }
213
214 private:
215
216         void activated ();
217         bool focus_out (GdkEventFocus*);
218         void filter_text (const Glib::ustring&, int*);
219
220         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
221         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
222         Gtk::Label* _label; ///< UI label
223         Gtk::Entry* _entry; ///< UI entry
224         std::string _invalid;
225 };
226
227
228 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
229  *  The template parameter is the enumeration.
230  */
231 template <class T>
232 class ComboOption : public Option
233 {
234 public:
235
236         /** Construct an ComboOption.
237          *  @param i id
238          *  @param n User-visible name.
239          *  @param g Slot to get the variable's value.
240          *  @param s Slot to set the variable's value.
241          */
242         ComboOption (
243                 std::string const & i,
244                 std::string const & n,
245                 sigc::slot<T> g,
246                 sigc::slot<bool, T> s
247                 )
248                 : Option (i, n),
249                   _get (g),
250                   _set (s)
251         {
252                 _label = Gtk::manage (new Gtk::Label (n + ":"));
253                 _label->set_alignment (0, 0.5);
254                 _combo = Gtk::manage (new Gtk::ComboBoxText);
255                 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboOption::changed));
256         }
257
258         void set_state_from_config () {
259                 uint32_t r = 0;
260                 while (r < _options.size() && _get () != _options[r]) {
261                         ++r;
262                 }
263
264                 if (r < _options.size()) {
265                         _combo->set_active (r);
266                 }
267         }
268
269         void add_to_page (OptionEditorPage* p)
270         {
271                 add_widgets_to_page (p, _label, _combo);
272         }
273
274         /** Add an allowed value for this option.
275          *  @param e Enumeration.
276          *  @param o User-visible name for this value.
277          */
278         void add (T e, std::string const & o) {
279                 _options.push_back (e);
280                 _combo->append_text (o);
281         }
282
283         void clear () {
284                 _combo->clear_items();
285                 _options.clear ();
286         }
287
288         void changed () {
289                 uint32_t const r = _combo->get_active_row_number ();
290                 if (r < _options.size()) {
291                         _set (_options[r]);
292                 }
293         }
294
295         void set_sensitive (bool yn) {
296                 _combo->set_sensitive (yn);
297         }
298
299         Gtk::Widget& tip_widget() { return *_combo; }
300
301 private:
302
303         sigc::slot<T> _get;
304         sigc::slot<bool, T> _set;
305         Gtk::Label* _label;
306         Gtk::ComboBoxText* _combo;
307         std::vector<T> _options;
308 };
309
310
311 /** Component which provides the UI for a GTK HScale.
312  */
313 class HSliderOption : public Option
314 {
315 public:
316
317         /** Construct an ComboOption.
318          *  @param i id
319          *  @param n User-visible name.
320          *  @param g Slot to get the variable's value.
321          *  @param s Slot to set the variable's value.
322          */
323         HSliderOption (
324                 std::string const & i,
325                 std::string const & n,
326                 Gtk::Adjustment &adj
327                 )
328                 : Option (i, n)
329         {
330                 _label = Gtk::manage (new Gtk::Label (n + ":"));
331                 _label->set_alignment (0, 0.5);
332                 _hscale = Gtk::manage (new Gtk::HScale(adj));
333                 _adj = NULL;
334         }
335
336         HSliderOption (
337                 std::string const & i,
338                 std::string const & n,
339                 Gtk::Adjustment *adj,
340                 sigc::slot<float> g,
341                 sigc::slot<bool, float> s
342                 )
343                 : Option (i, n)
344                 , _get (g)
345                 , _set (s)
346                 , _adj (adj)
347         {
348                 _label = Gtk::manage (new Gtk::Label (n + ":"));
349                 _label->set_alignment (0, 0.5);
350                 _hscale = Gtk::manage (new Gtk::HScale(*_adj));
351                 _adj->signal_value_changed().connect (sigc::mem_fun (*this, &HSliderOption::changed));
352         }
353
354         void set_state_from_config () {
355                 if (_adj) _adj->set_value (_get());
356         }
357
358         void changed () {
359                 if (_adj) _set (_adj->get_value ());
360         }
361
362         void add_to_page (OptionEditorPage* p)
363         {
364                 add_widgets_to_page (p, _label, _hscale);
365         }
366
367         void set_sensitive (bool yn) {
368                 _hscale->set_sensitive (yn);
369         }
370
371         Gtk::Widget& tip_widget() { return *_hscale; }
372         Gtk::HScale& scale() { return *_hscale; }
373
374 private:
375         sigc::slot<float> _get;
376         sigc::slot<bool, float> _set;
377         Gtk::Label* _label;
378         Gtk::HScale* _hscale;
379         Gtk::Adjustment* _adj;
380 };
381
382 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
383  *  The template parameter is the enumeration.
384  */
385 class ComboStringOption : public Option
386 {
387 public:
388
389         /** Construct an ComboOption.
390          *  @param i id
391          *  @param n User-visible name.
392          *  @param g Slot to get the variable's value.
393          *  @param s Slot to set the variable's value.
394          */
395         ComboStringOption (
396                 std::string const & i,
397                 std::string const & n,
398                 sigc::slot<std::string> g,
399                 sigc::slot<bool, std::string> s
400                 )
401                 : Option (i, n),
402                   _get (g),
403                   _set (s)
404         {
405                 _label = Gtk::manage (new Gtk::Label (n + ":"));
406                 _label->set_alignment (0, 0.5);
407                 _combo = Gtk::manage (new Gtk::ComboBoxText);
408                 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboStringOption::changed));
409         }
410
411         void set_state_from_config () {
412                 _combo->set_active_text (_get());
413         }
414
415         void add_to_page (OptionEditorPage* p)
416         {
417                 add_widgets_to_page (p, _label, _combo);
418         }
419
420         /** Set the allowed strings for this option
421          *  @param strings a vector of allowed strings
422          */
423         void set_popdown_strings (const std::vector<std::string>& strings) {
424                 _combo->clear_items ();
425                 for (std::vector<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i) {
426                         _combo->append_text (*i);
427                 }
428         }
429
430         void clear () {
431                 _combo->clear_items();
432         }
433
434         void changed () {
435                 _set (_combo->get_active_text ());
436         }
437
438         void set_sensitive (bool yn) {
439                 _combo->set_sensitive (yn);
440         }
441
442         Gtk::Widget& tip_widget() { return *_combo; }
443
444 private:
445         sigc::slot<std::string> _get;
446         sigc::slot<bool, std::string> _set;
447         Gtk::Label* _label;
448         Gtk::ComboBoxText* _combo;
449 };
450
451
452 /** Component which provides the UI to handle a boolean option which needs
453  *  to be represented as a ComboBox to be clear to the user.
454  */
455 class BoolComboOption : public Option
456 {
457 public:
458
459         BoolComboOption (
460                 std::string const &,
461                 std::string const &,
462                 std::string const &,
463                 std::string const &,
464                 sigc::slot<bool>,
465                 sigc::slot<bool, bool>
466                 );
467
468         void set_state_from_config ();
469         void add_to_page (OptionEditorPage *);
470         void changed ();
471         void set_sensitive (bool);
472
473         Gtk::Widget& tip_widget() { return *_combo; }
474
475 private:
476
477         sigc::slot<bool> _get;
478         sigc::slot<bool, bool> _set;
479         Gtk::Label* _label;
480         Gtk::ComboBoxText* _combo;
481 };
482
483
484
485 /** Component which provides the UI to handle an numeric option using a GTK SpinButton */
486 template <class T>
487 class SpinOption : public Option
488 {
489 public:
490         /** Construct an SpinOption.
491          *  @param i id
492          *  @param n User-visible name.
493          *  @param g Slot to get the variable's value.
494          *  @param s Slot to set the variable's value.
495          *  @param min Variable minimum value.
496          *  @param max Variable maximum value.
497          *  @param step Step for the spin button.
498          *  @param page Page step for the spin button.
499          *  @param unit Unit name.
500          *  @param scale Scaling factor (such that for a value x in the spinbutton, x * scale is written to the config)
501          */
502         SpinOption (
503                 std::string const & i,
504                 std::string const & n,
505                 sigc::slot<T> g,
506                 sigc::slot<bool, T> s,
507                 T min,
508                 T max,
509                 T step,
510                 T page,
511                 std::string const & unit = "",
512                 float scale = 1
513                 )
514                 : Option (i, n),
515                   _get (g),
516                   _set (s),
517                   _scale (scale)
518         {
519                 _label = Gtk::manage (new Gtk::Label (n + ":"));
520                 _label->set_alignment (0, 0.5);
521
522                 _spin = Gtk::manage (new Gtk::SpinButton);
523                 _spin->set_range (min, max);
524                 _spin->set_increments (step, page);
525
526                 _box = Gtk::manage (new Gtk::HBox);
527                 _box->pack_start (*_spin, true, true);
528                 _box->set_spacing (4);
529                 if (unit.length()) {
530                         _box->pack_start (*Gtk::manage (new Gtk::Label (unit)), false, false);
531                 }
532
533                 _spin->signal_value_changed().connect (sigc::mem_fun (*this, &SpinOption::changed));
534         }
535
536         void set_state_from_config ()
537         {
538                 _spin->set_value (_get () / _scale);
539         }
540
541         void add_to_page (OptionEditorPage* p)
542         {
543                 add_widgets_to_page (p, _label, _box);
544         }
545
546         void changed ()
547         {
548                 _set (static_cast<T> (_spin->get_value ()) * _scale);
549         }
550
551         Gtk::Widget& tip_widget() { return *_spin; }
552
553 private:
554         sigc::slot<T> _get;
555         sigc::slot<bool, T> _set;
556         float _scale;
557         Gtk::Label* _label;
558         Gtk::HBox* _box;
559         Gtk::SpinButton* _spin;
560 };
561
562 class FaderOption : public Option
563 {
564 public:
565
566         FaderOption (std::string const &, std::string const &, sigc::slot<ARDOUR::gain_t> g, sigc::slot<bool, ARDOUR::gain_t> s);
567         void set_state_from_config ();
568         void add_to_page (OptionEditorPage *);
569
570         Gtk::Widget& tip_widget() { return *_db_slider; }
571
572 private:
573         void db_changed ();
574
575         Gtk::Adjustment _db_adjustment;
576         Gtkmm2ext::HSliderController* _db_slider;
577         Gtk::Entry _db_display;
578         Gtk::Label _label;
579         Gtk::HBox _box;
580         Gtk::VBox _fader_centering_box;
581         sigc::slot<ARDOUR::gain_t> _get;
582         sigc::slot<bool, ARDOUR::gain_t> _set;
583 };
584
585 class ClockOption : public Option
586 {
587 public:
588         ClockOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
589         void set_state_from_config ();
590         void add_to_page (OptionEditorPage *);
591         void set_session (ARDOUR::Session *);
592
593         Gtk::Widget& tip_widget() { return _clock; }
594         AudioClock& clock() { return _clock; }
595
596 private:
597         void save_clock_time ();
598         Gtk::Label _label;
599         AudioClock _clock;
600         sigc::slot<std::string> _get;
601         sigc::slot<bool, std::string> _set;
602         ARDOUR::Session *_session;
603 };
604
605 class DirectoryOption : public Option
606 {
607 public:
608         DirectoryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
609
610         void set_state_from_config ();
611         void add_to_page (OptionEditorPage *);
612
613         Gtk::Widget& tip_widget() { return _file_chooser; }
614
615 private:
616         void file_set ();
617         void current_folder_set ();
618         
619         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
620         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
621         Gtk::FileChooserButton _file_chooser;
622 };
623
624 /** Class to represent a single page in an OptionEditor's notebook.
625  *  Pages are laid out using a 3-column table; the 1st column is used
626  *  to indent non-headings, and the 2nd and 3rd for actual content.
627  */
628 class OptionEditorPage
629 {
630 public:
631         OptionEditorPage (Gtk::Notebook&, std::string const &);
632
633         Gtk::VBox box;
634         Gtk::Table table;
635         std::list<OptionEditorComponent*> components;
636 };
637
638 /** The OptionEditor dialog base class */
639 class OptionEditor : public ArdourWindow
640 {
641 public:
642         OptionEditor (ARDOUR::Configuration *, std::string const &);
643         ~OptionEditor ();
644
645         void add_option (std::string const &, OptionEditorComponent *);
646
647         void set_current_page (std::string const &);
648
649 protected:
650
651         virtual void parameter_changed (std::string const &);
652
653         ARDOUR::Configuration* _config;
654
655 private:
656
657         PBD::ScopedConnection config_connection;
658
659         Gtk::Notebook _notebook;
660         std::map<std::string, OptionEditorPage*> _pages;
661 };
662
663 #endif /* __gtk_ardour_option_editor_h__ */
664
665