Re-expose timecode offset in the session option editor.
[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 private:
156
157         void toggled ();
158
159         sigc::slot<bool> _get; ///< slot to get the configuration variable's value
160         sigc::slot<bool, bool> _set;  ///< slot to set the configuration variable's value
161         Gtk::CheckButton* _button; ///< UI button
162 };
163
164 /** Component which provides the UI to handle a string option using a GTK Entry */
165 class EntryOption : public Option
166 {
167 public:
168
169         EntryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
170         void set_state_from_config ();
171         void add_to_page (OptionEditorPage*);
172
173 private:
174
175         void activated ();
176
177         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
178         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
179         Gtk::Label* _label; ///< UI label
180         Gtk::Entry* _entry; ///< UI entry
181 };
182
183
184 /** Component which provides the UI to handle an enumerated option using a GTK CheckButton.
185  *  The template parameter is the enumeration.
186  */
187 template <class T>
188 class ComboOption : public Option
189 {
190 public:
191
192         /** Construct an ComboOption.
193          *  @param i id
194          *  @param n User-visible name.
195          *  @param g Slot to get the variable's value.
196          *  @param s Slot to set the variable's value.
197          */
198         ComboOption (
199                 std::string const & i,
200                 std::string const & n,
201                 sigc::slot<T> g,
202                 sigc::slot<bool, T> s
203                 )
204                 : Option (i, n),
205                   _get (g),
206                   _set (s)
207         {
208                 _label = manage (new Gtk::Label (n + ":"));
209                 _label->set_alignment (0, 0.5);
210                 _combo = manage (new Gtk::ComboBoxText);
211                 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboOption::changed));
212         }
213
214         void set_state_from_config () {
215                 uint32_t r = 0;
216                 while (r < _options.size() && _get () != _options[r]) {
217                         ++r;
218                 }
219
220                 if (r < _options.size()) {
221                         _combo->set_active (r);
222                 }
223         }
224
225         void add_to_page (OptionEditorPage* p)
226         {
227                 add_widgets_to_page (p, _label, _combo);
228         }
229
230         /** Add an allowed value for this option.
231          *  @param e Enumeration.
232          *  @param o User-visible name for this value.
233          */
234         void add (T e, std::string const & o) {
235                 _options.push_back (e);
236                 _combo->append_text (o);
237         }
238
239         void clear () {
240                 _combo->clear_items();
241                 _options.clear ();
242         }
243
244         void changed () {
245                 uint32_t const r = _combo->get_active_row_number ();
246                 if (r < _options.size()) {
247                         _set (_options[r]);
248                 }
249         }
250
251         void set_sensitive (bool yn) {
252                 _combo->set_sensitive (yn);
253         }
254
255 private:
256
257         sigc::slot<T> _get;
258         sigc::slot<bool, T> _set;
259         Gtk::Label* _label;
260         Gtk::ComboBoxText* _combo;
261         std::vector<T> _options;
262 };
263
264
265 /** Component which provides the UI to handle an numeric option using a GTK SpinButton */
266 template <class T>
267 class SpinOption : public Option
268 {
269 public:
270         /** Construct an SpinOption.
271          *  @param i id
272          *  @param n User-visible name.
273          *  @param g Slot to get the variable's value.
274          *  @param s Slot to set the variable's value.
275          *  @param min Variable minimum value.
276          *  @param max Variable maximum value.
277          *  @param step Step for the spin button.
278          *  @param page Page step for the spin button.
279          *  @param unit Unit name.
280          *  @param scale Scaling factor (such that for a value x in the spinbutton, x * scale is written to the config)
281          */
282         SpinOption (
283                 std::string const & i,
284                 std::string const & n,
285                 sigc::slot<T> g,
286                 sigc::slot<bool, T> s,
287                 T min,
288                 T max,
289                 T step,
290                 T page,
291                 std::string const & unit = "",
292                 float scale = 1
293                 )
294                 : Option (i, n),
295                   _get (g),
296                   _set (s),
297                   _scale (scale)
298         {
299                 _label = manage (new Gtk::Label (n + ":"));
300                 _label->set_alignment (0, 0.5);
301
302                 _spin = manage (new Gtk::SpinButton);
303                 _spin->set_range (min, max);
304                 _spin->set_increments (step, page);
305
306                 _box = manage (new Gtk::HBox);
307                 _box->pack_start (*_spin, true, true);
308                 _box->set_spacing (4);
309                 if (unit.length()) {
310                         _box->pack_start (*manage (new Gtk::Label (unit)), false, false);
311                 }
312
313                 _spin->signal_value_changed().connect (sigc::mem_fun (*this, &SpinOption::changed));
314         }
315
316         void set_state_from_config ()
317         {
318                 _spin->set_value (_get () / _scale);
319         }
320
321         void add_to_page (OptionEditorPage* p)
322         {
323                 add_widgets_to_page (p, _label, _box);
324         }
325
326         void changed ()
327         {
328                 _set (static_cast<T> (_spin->get_value ()) * _scale);
329         }
330
331 private:
332         sigc::slot<T> _get;
333         sigc::slot<bool, T> _set;
334         float _scale;
335         Gtk::Label* _label;
336         Gtk::HBox* _box;
337         Gtk::SpinButton* _spin;
338 };
339
340 class FaderOption : public Option
341 {
342 public:
343
344         FaderOption (std::string const &, std::string const &, sigc::slot<ARDOUR::gain_t> g, sigc::slot<bool, ARDOUR::gain_t> s);
345         void set_state_from_config ();
346         void add_to_page (OptionEditorPage *);
347
348 private:
349         void db_changed ();
350         
351         Gtk::Adjustment _db_adjustment;
352         Gtkmm2ext::HSliderController* _db_slider;
353         Glib::RefPtr<Gdk::Pixbuf> _pix;
354         Gtk::Entry _db_display;
355         Gtk::Label _label;
356         Gtk::HBox _box;
357         sigc::slot<ARDOUR::gain_t> _get;
358         sigc::slot<bool, ARDOUR::gain_t> _set;
359 };
360
361 class ClockOption : public Option
362 {
363 public:
364         ClockOption (std::string const &, std::string const &, sigc::slot<ARDOUR::framecnt_t>, sigc::slot<bool, ARDOUR::framecnt_t>);
365         void set_state_from_config ();
366         void add_to_page (OptionEditorPage *);
367         void set_session (ARDOUR::Session *);
368
369 private:
370         Gtk::Label _label;
371         AudioClock _clock;
372         sigc::slot<ARDOUR::framecnt_t> _get;
373         sigc::slot<bool, ARDOUR::framecnt_t> _set;
374 };
375
376 /** Class to represent a single page in an OptionEditor's notebook.
377  *  Pages are laid out using a 3-column table; the 1st column is used
378  *  to indent non-headings, and the 2nd and 3rd for actual content.
379  */
380 class OptionEditorPage
381 {
382 public:
383         OptionEditorPage (Gtk::Notebook&, std::string const &);
384
385         Gtk::VBox box;
386         Gtk::Table table;
387         std::list<OptionEditorComponent*> components;
388 };
389
390 /** The OptionEditor dialog base class */
391 class OptionEditor : public ArdourDialog
392 {
393 public:
394         OptionEditor (ARDOUR::Configuration *, std::string const &);
395         ~OptionEditor ();
396
397         void add_option (std::string const &, OptionEditorComponent *);
398
399 protected:
400
401         ARDOUR::Configuration* _config;
402
403 private:
404
405         void parameter_changed (std::string const &);
406         PBD::ScopedConnection config_connection;
407
408         Gtk::Notebook _notebook;
409         std::map<std::string, OptionEditorPage*> _pages;
410 };
411
412 #endif /* __gtk_ardour_option_editor_h__ */
413
414