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