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