Bring up the click tab of the RC options window when right-clicking on the click...
[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_dialog.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
76 /** A component which provides a subheading within the dialog */
77 class OptionEditorHeading : public OptionEditorComponent
78 {
79 public:
80         OptionEditorHeading (std::string const &);
81
82         void parameter_changed (std::string const &) {}
83         void set_state_from_config () {}
84         void add_to_page (OptionEditorPage *);
85
86 private:
87         Gtk::Label* _label; ///< the label used for the heading
88 };
89
90 /** A component which provides a box into which a subclass can put arbitrary widgets */
91 class OptionEditorBox : public OptionEditorComponent
92 {
93 public:
94
95         /** Construct an OpenEditorBox */
96         OptionEditorBox ()
97         {
98                 _box = Gtk::manage (new Gtk::VBox);
99                 _box->set_spacing (4);
100         }
101
102         void parameter_changed (std::string const &) = 0;
103         void set_state_from_config () = 0;
104         void add_to_page (OptionEditorPage *);
105
106 protected:
107
108         Gtk::VBox* _box; ///< constituent box for subclasses to add widgets to
109 };
110
111 /** Base class for components which provide UI to change an option */
112 class Option : public OptionEditorComponent
113 {
114 public:
115         /** Construct an Option.
116          *  @param i Option id (e.g. "plugins-stop-with-transport")
117          *  @param n User-visible name (e.g. "Stop plugins when the transport is stopped")
118          */
119         Option (std::string const & i,
120                 std::string const & n
121                 )
122                 : _id (i),
123                   _name (n)
124         {}
125
126         void parameter_changed (std::string const & p)
127         {
128                 if (p == _id) {
129                         set_state_from_config ();
130                 }
131         }
132
133         virtual void set_state_from_config () = 0;
134         virtual void add_to_page (OptionEditorPage*) = 0;
135
136         std::string id () const {
137                 return _id;
138         }
139
140 protected:
141
142         std::string _id;
143         std::string _name;
144 };
145
146 /** Component which provides the UI to handle a boolean option using a GTK CheckButton */
147 class BoolOption : public Option
148 {
149 public:
150
151         BoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
152         void set_state_from_config ();
153         void add_to_page (OptionEditorPage*);
154
155         void set_sensitive (bool yn) {
156                 _button->set_sensitive (yn);
157         }
158         
159 private:
160
161         void toggled ();
162
163         sigc::slot<bool> _get; ///< slot to get the configuration variable's value
164         sigc::slot<bool, bool> _set;  ///< slot to set the configuration variable's value
165         Gtk::CheckButton* _button; ///< UI button
166 };
167
168 /** Component which provides the UI to handle a string option using a GTK Entry */
169 class EntryOption : public Option
170 {
171 public:
172
173         EntryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
174         void set_state_from_config ();
175         void add_to_page (OptionEditorPage*);
176
177 private:
178
179         void activated ();
180
181         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
182         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
183         Gtk::Label* _label; ///< UI label
184         Gtk::Entry* _entry; ///< UI entry
185 };
186
187
188 /** Component which provides the UI to handle an enumerated option using a GTK CheckButton.
189  *  The template parameter is the enumeration.
190  */
191 template <class T>
192 class ComboOption : public Option
193 {
194 public:
195
196         /** Construct an ComboOption.
197          *  @param i id
198          *  @param n User-visible name.
199          *  @param g Slot to get the variable's value.
200          *  @param s Slot to set the variable's value.
201          */
202         ComboOption (
203                 std::string const & i,
204                 std::string const & n,
205                 sigc::slot<T> g,
206                 sigc::slot<bool, T> s
207                 )
208                 : Option (i, n),
209                   _get (g),
210                   _set (s)
211         {
212                 _label = manage (new Gtk::Label (n + ":"));
213                 _label->set_alignment (0, 0.5);
214                 _combo = manage (new Gtk::ComboBoxText);
215                 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboOption::changed));
216         }
217
218         void set_state_from_config () {
219                 uint32_t r = 0;
220                 while (r < _options.size() && _get () != _options[r]) {
221                         ++r;
222                 }
223
224                 if (r < _options.size()) {
225                         _combo->set_active (r);
226                 }
227         }
228
229         void add_to_page (OptionEditorPage* p)
230         {
231                 add_widgets_to_page (p, _label, _combo);
232         }
233
234         /** Add an allowed value for this option.
235          *  @param e Enumeration.
236          *  @param o User-visible name for this value.
237          */
238         void add (T e, std::string const & o) {
239                 _options.push_back (e);
240                 _combo->append_text (o);
241         }
242
243         void clear () {
244                 _combo->clear_items();
245                 _options.clear ();
246         }
247
248         void changed () {
249                 uint32_t const r = _combo->get_active_row_number ();
250                 if (r < _options.size()) {
251                         _set (_options[r]);
252                 }
253         }
254
255         void set_sensitive (bool yn) {
256                 _combo->set_sensitive (yn);
257         }
258
259 private:
260
261         sigc::slot<T> _get;
262         sigc::slot<bool, T> _set;
263         Gtk::Label* _label;
264         Gtk::ComboBoxText* _combo;
265         std::vector<T> _options;
266 };
267
268
269 /** Component which provides the UI to handle an numeric option using a GTK SpinButton */
270 template <class T>
271 class SpinOption : public Option
272 {
273 public:
274         /** Construct an SpinOption.
275          *  @param i id
276          *  @param n User-visible name.
277          *  @param g Slot to get the variable's value.
278          *  @param s Slot to set the variable's value.
279          *  @param min Variable minimum value.
280          *  @param max Variable maximum value.
281          *  @param step Step for the spin button.
282          *  @param page Page step for the spin button.
283          *  @param unit Unit name.
284          *  @param scale Scaling factor (such that for a value x in the spinbutton, x * scale is written to the config)
285          */
286         SpinOption (
287                 std::string const & i,
288                 std::string const & n,
289                 sigc::slot<T> g,
290                 sigc::slot<bool, T> s,
291                 T min,
292                 T max,
293                 T step,
294                 T page,
295                 std::string const & unit = "",
296                 float scale = 1
297                 )
298                 : Option (i, n),
299                   _get (g),
300                   _set (s),
301                   _scale (scale)
302         {
303                 _label = manage (new Gtk::Label (n + ":"));
304                 _label->set_alignment (0, 0.5);
305
306                 _spin = manage (new Gtk::SpinButton);
307                 _spin->set_range (min, max);
308                 _spin->set_increments (step, page);
309
310                 _box = manage (new Gtk::HBox);
311                 _box->pack_start (*_spin, true, true);
312                 _box->set_spacing (4);
313                 if (unit.length()) {
314                         _box->pack_start (*manage (new Gtk::Label (unit)), false, false);
315                 }
316
317                 _spin->signal_value_changed().connect (sigc::mem_fun (*this, &SpinOption::changed));
318         }
319
320         void set_state_from_config ()
321         {
322                 _spin->set_value (_get () / _scale);
323         }
324
325         void add_to_page (OptionEditorPage* p)
326         {
327                 add_widgets_to_page (p, _label, _box);
328         }
329
330         void changed ()
331         {
332                 _set (static_cast<T> (_spin->get_value ()) * _scale);
333         }
334
335 private:
336         sigc::slot<T> _get;
337         sigc::slot<bool, T> _set;
338         float _scale;
339         Gtk::Label* _label;
340         Gtk::HBox* _box;
341         Gtk::SpinButton* _spin;
342 };
343
344 class FaderOption : public Option
345 {
346 public:
347
348         FaderOption (std::string const &, std::string const &, sigc::slot<ARDOUR::gain_t> g, sigc::slot<bool, ARDOUR::gain_t> s);
349         void set_state_from_config ();
350         void add_to_page (OptionEditorPage *);
351
352 private:
353         void db_changed ();
354         
355         Gtk::Adjustment _db_adjustment;
356         Gtkmm2ext::HSliderController* _db_slider;
357         Glib::RefPtr<Gdk::Pixbuf> _pix;
358         Gtk::Entry _db_display;
359         Gtk::Label _label;
360         Gtk::HBox _box;
361         sigc::slot<ARDOUR::gain_t> _get;
362         sigc::slot<bool, ARDOUR::gain_t> _set;
363 };
364
365 class ClockOption : public Option
366 {
367 public:
368         ClockOption (std::string const &, std::string const &, sigc::slot<ARDOUR::framecnt_t>, sigc::slot<bool, ARDOUR::framecnt_t>);
369         void set_state_from_config ();
370         void add_to_page (OptionEditorPage *);
371         void set_session (ARDOUR::Session *);
372
373 private:
374         Gtk::Label _label;
375         AudioClock _clock;
376         sigc::slot<ARDOUR::framecnt_t> _get;
377         sigc::slot<bool, ARDOUR::framecnt_t> _set;
378 };
379
380 /** Class to represent a single page in an OptionEditor's notebook.
381  *  Pages are laid out using a 3-column table; the 1st column is used
382  *  to indent non-headings, and the 2nd and 3rd for actual content.
383  */
384 class OptionEditorPage
385 {
386 public:
387         OptionEditorPage (Gtk::Notebook&, std::string const &);
388
389         Gtk::VBox box;
390         Gtk::Table table;
391         std::list<OptionEditorComponent*> components;
392 };
393
394 /** The OptionEditor dialog base class */
395 class OptionEditor : public ArdourDialog
396 {
397 public:
398         OptionEditor (ARDOUR::Configuration *, std::string const &);
399         ~OptionEditor ();
400
401         void add_option (std::string const &, OptionEditorComponent *);
402
403         void set_current_page (std::string const &);
404
405 protected:
406
407         virtual void parameter_changed (std::string const &);
408         
409         ARDOUR::Configuration* _config;
410
411 private:
412
413         PBD::ScopedConnection config_connection;
414
415         Gtk::Notebook _notebook;
416         std::map<std::string, OptionEditorPage*> _pages;
417 };
418
419 #endif /* __gtk_ardour_option_editor_h__ */
420
421