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