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