Fix CheckOption crash on session-reload
[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 #ifndef __gtk_ardour_option_editor_h__
20 #define __gtk_ardour_option_editor_h__
21
22 #include <gtkmm/checkbutton.h>
23 #include <gtkmm/comboboxtext.h>
24 #include <gtkmm/filechooserbutton.h>
25 #include <gtkmm/label.h>
26 #include <gtkmm/notebook.h>
27 #include <gtkmm/scale.h>
28 #include <gtkmm/spinbutton.h>
29 #include <gtkmm/table.h>
30 #include <gtkmm/treestore.h>
31 #include <gtkmm/treeview.h>
32 #include <gtkmm/window.h>
33
34 #include "widgets/slider_controller.h"
35
36 #include "actions.h"
37 #include "ardour_window.h"
38 #include "audio_clock.h"
39 #include "ardour/types.h"
40
41 /** @file option_editor.h
42  *  @brief Base class for option editing dialog boxes.
43  *
44  *  Code to provided the basis for dialogs which allow the user to edit options
45  *  from an ARDOUR::Configuration class.
46  *
47  *  The idea is that we have an OptionEditor class which is the dialog box.
48  *  This is essentially a GTK Notebook.  OptionEditorComponent objects can
49  *  then be added to the OptionEditor, and these components are arranged on
50  *  the pages of the Notebook.  There is also an OptionEditorComponent hierarchy
51  *  here, providing things like boolean and combobox option components.
52  *
53  *  It is intended that OptionEditor be subclassed to implement a particular
54  *  options dialog.
55  */
56
57 namespace PBD {
58         class Configuration;
59 }
60
61 class OptionEditorPage;
62
63 /** Base class for components of an OptionEditor dialog */
64 class OptionEditorComponent
65 {
66 public:
67         virtual ~OptionEditorComponent() {}
68
69         /** Called when a configuration parameter's value has changed.
70          *  @param p parameter name
71          */
72         virtual void parameter_changed (std::string const & p) = 0;
73
74         /** Called to instruct the object to set its UI state from the configuration */
75         virtual void set_state_from_config () = 0;
76
77         /** Called to instruct the object to add itself to an OptionEditorPage */
78         virtual void add_to_page (OptionEditorPage *) = 0;
79
80         void add_widget_to_page (OptionEditorPage*, Gtk::Widget*);
81         void add_widgets_to_page (OptionEditorPage*, Gtk::Widget*, Gtk::Widget*, bool expand = true);
82
83         void set_note (std::string const &);
84
85         virtual Gtk::Widget& tip_widget() = 0;
86
87 protected:
88         void maybe_add_note (OptionEditorPage *, int);
89
90         std::string _note;
91 };
92
93 /** A component which provides a subheading within the dialog */
94 class OptionEditorHeading : public OptionEditorComponent
95 {
96 public:
97         OptionEditorHeading (std::string const &);
98
99         void parameter_changed (std::string const &) {}
100         void set_state_from_config () {}
101         void add_to_page (OptionEditorPage *);
102
103         Gtk::Widget& tip_widget() { return *_label; }
104
105 private:
106         Gtk::Label* _label; ///< the label used for the heading
107 };
108
109 /** Expanding layout helper to push elements to the left on a single column page  */
110 class OptionEditorBlank : public OptionEditorComponent
111 {
112 public:
113         OptionEditorBlank () {}
114
115         void parameter_changed (std::string const &) {}
116         void set_state_from_config () {}
117         void add_to_page (OptionEditorPage *);
118
119         Gtk::Widget& tip_widget() { return _dummy; }
120
121 private:
122         Gtk::EventBox _dummy;
123 };
124
125 class RcConfigDisplay : public OptionEditorComponent
126 {
127 public:
128         RcConfigDisplay (std::string const &, std::string const &, sigc::slot<std::string>, char s = '\0');
129         void add_to_page (OptionEditorPage *);
130         void parameter_changed (std::string const & p);
131         void set_state_from_config ();
132         Gtk::Widget& tip_widget() { return *_info; }
133 protected:
134         sigc::slot<std::string> _get;
135         Gtk::Label* _label;
136         Gtk::Label* _info;
137         std::string _id;
138         char _sep;
139 };
140
141 class RcActionButton : public OptionEditorComponent
142 {
143 public:
144         RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l = "");
145         void add_to_page (OptionEditorPage *);
146
147         void parameter_changed (std::string const & p) {}
148         void set_state_from_config () {}
149         Gtk::Widget& tip_widget() { return *_button; }
150
151 protected:
152         Gtk::Button* _button;
153         Gtk::Label* _label;
154         std::string _name;
155 };
156
157 /** Base class for components which provide UI to change an option */
158 class Option : public OptionEditorComponent
159 {
160 public:
161         /** Construct an Option.
162          *  @param i Option id (e.g. "plugins-stop-with-transport")
163          *  @param n User-visible name (e.g. "Stop plugins when the transport is stopped")
164          */
165         Option (std::string const & i,
166                 std::string const & n
167                 )
168                 : _id (i),
169                   _name (n)
170         {}
171
172         void parameter_changed (std::string const & p)
173         {
174                 if (p == _id) {
175                         set_state_from_config ();
176                 }
177         }
178
179         virtual void set_state_from_config () = 0;
180         virtual void add_to_page (OptionEditorPage*) = 0;
181
182         std::string id () const {
183                 return _id;
184         }
185
186 protected:
187         std::string _id;
188         std::string _name;
189 };
190
191 /** Just a Gtk Checkbutton, masquerading as an option component */
192 class CheckOption : public OptionEditorComponent , public Gtkmm2ext::Activatable
193 {
194 public:
195         CheckOption (std::string const &, std::string const &, Glib::RefPtr<Gtk::Action> act );
196         virtual ~CheckOption ();
197         void set_state_from_config () {}
198         void parameter_changed (std::string const &) {}
199         void add_to_page (OptionEditorPage*);
200
201         void set_sensitive (bool yn) {
202                 _button->set_sensitive (yn);
203         }
204
205         Gtk::Widget& tip_widget() { return *_button; }
206
207 protected:
208         void action_toggled ();
209         void action_sensitivity_changed () {}
210         void action_visibility_changed () {}
211
212         virtual void toggled ();
213
214         Gtk::CheckButton*      _button; ///< UI button
215         Gtk::Label*            _label; ///< label for button, so we can use markup
216
217 private:
218         sigc::connection       _callback_connection;
219 };
220
221 /** Component which provides the UI to handle a boolean option using a GTK CheckButton */
222 class BoolOption : public Option
223 {
224 public:
225         BoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
226         void set_state_from_config ();
227         void add_to_page (OptionEditorPage*);
228
229         void set_sensitive (bool yn) {
230                 _button->set_sensitive (yn);
231         }
232
233         Gtk::Widget& tip_widget() { return *_button; }
234
235 protected:
236         virtual void toggled ();
237
238         sigc::slot<bool>       _get; ///< slot to get the configuration variable's value
239         sigc::slot<bool, bool> _set;  ///< slot to set the configuration variable's value
240         Gtk::CheckButton*      _button; ///< UI button
241         Gtk::Label*            _label; ///< label for button, so we can use markup
242 };
243
244 class RouteDisplayBoolOption : public BoolOption
245 {
246 public:
247         RouteDisplayBoolOption (std::string const &, std::string const &, sigc::slot<bool>, sigc::slot<bool, bool>);
248
249 protected:
250         virtual void toggled ();
251 };
252
253 /** Component which allows to add any GTK Widget - intended for single buttons and custom stateless objects */
254 class FooOption : public OptionEditorComponent
255 {
256 public:
257         FooOption (Gtk::Widget *w) : _w (w) {}
258
259         void add_to_page (OptionEditorPage* p) {
260                 add_widget_to_page (p, _w);
261         }
262
263         Gtk::Widget& tip_widget() { return *_w; }
264         void set_state_from_config () {}
265         void parameter_changed (std::string const &) {}
266 private:
267         Gtk::Widget *_w;
268 };
269
270 /** Component which provides the UI to handle a string option using a GTK Entry */
271 class EntryOption : public Option
272 {
273 public:
274         EntryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
275         void set_state_from_config ();
276         void add_to_page (OptionEditorPage*);
277         void set_sensitive (bool);
278         void set_invalid_chars (std::string i) { _invalid = i; }
279
280         Gtk::Widget& tip_widget() { return *_entry; }
281
282 private:
283         void activated ();
284         bool focus_out (GdkEventFocus*);
285         void filter_text (const Glib::ustring&, int*);
286
287         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
288         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
289         Gtk::Label* _label; ///< UI label
290         Gtk::Entry* _entry; ///< UI entry
291         std::string _invalid;
292 };
293
294
295 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
296  *  The template parameter is the enumeration.
297  */
298 template <class T>
299 class ComboOption : public Option
300 {
301 public:
302         /** Construct an ComboOption.
303          *  @param i id
304          *  @param n User-visible name.
305          *  @param g Slot to get the variable's value.
306          *  @param s Slot to set the variable's value.
307          */
308         ComboOption (
309                 std::string const & i,
310                 std::string const & n,
311                 sigc::slot<T> g,
312                 sigc::slot<bool, T> s
313                 )
314                 : Option (i, n)
315                 , _get (g)
316                 , _set (s)
317         {
318                 _label = Gtk::manage (new Gtk::Label (n + ":"));
319                 _label->set_alignment (0, 0.5);
320                 _combo = Gtk::manage (new Gtk::ComboBoxText);
321                 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboOption::changed));
322         }
323
324         void set_state_from_config ()
325         {
326                 uint32_t r = 0;
327                 while (r < _options.size() && _get () != _options[r]) {
328                         ++r;
329                 }
330
331                 if (r < _options.size()) {
332                         _combo->set_active (r);
333                 }
334         }
335
336         void add_to_page (OptionEditorPage* p)
337         {
338                 add_widgets_to_page (p, _label, _combo);
339         }
340
341         /** Add an allowed value for this option.
342          *  @param e Enumeration.
343          *  @param o User-visible name for this value.
344          */
345         void add (T e, std::string const & o)
346         {
347                 _options.push_back (e);
348                 _combo->append_text (o);
349         }
350
351         void clear ()
352         {
353                 _combo->clear_items();
354                 _options.clear ();
355         }
356
357         void changed ()
358         {
359                 uint32_t const r = _combo->get_active_row_number ();
360                 if (r < _options.size()) {
361                         _set (_options[r]);
362                 }
363         }
364         void set_sensitive (bool yn)
365         {
366                 _combo->set_sensitive (yn);
367         }
368
369         Gtk::Widget& tip_widget() { return *_combo; }
370
371 private:
372         sigc::slot<T> _get;
373         sigc::slot<bool, T> _set;
374         Gtk::Label* _label;
375         Gtk::ComboBoxText* _combo;
376         std::vector<T> _options;
377 };
378
379
380 /** Component which provides the UI for a GTK HScale.
381  */
382 class HSliderOption : public Option
383 {
384 public:
385         HSliderOption (
386                         std::string const& i,
387                         std::string const& n,
388                         sigc::slot<float> g,
389                         sigc::slot<bool, float> s,
390                         double lower, double upper,
391                         double step_increment = 1,
392                         double page_increment = 10,
393                         double mult = 1.0,
394                         bool logarithmic = false
395                 );
396
397         void set_state_from_config ();
398         virtual void changed ();
399         void add_to_page (OptionEditorPage* p);
400         void set_sensitive (bool yn);
401
402         Gtk::Widget& tip_widget() { return _hscale; }
403         Gtk::HScale& scale() { return _hscale; }
404
405 protected:
406         sigc::slot<float> _get;
407         sigc::slot<bool, float> _set;
408         Gtk::Adjustment _adj;
409         Gtk::HScale _hscale;
410         Gtk::Label _label;
411         double _mult;
412         bool _log;
413 };
414
415
416 /** Component which provides the UI to handle an enumerated option using a GTK ComboBox.
417  *  The template parameter is the enumeration.
418  */
419 class ComboStringOption : public Option
420 {
421 public:
422         /** Construct an ComboOption.
423          *  @param i id
424          *  @param n User-visible name.
425          *  @param g Slot to get the variable's value.
426          *  @param s Slot to set the variable's value.
427          */
428         ComboStringOption (
429                 std::string const & i,
430                 std::string const & n,
431                 sigc::slot<std::string> g,
432                 sigc::slot<bool, std::string> s
433                 );
434
435         void set_state_from_config ();
436         void add_to_page (OptionEditorPage* p);
437
438         /** Set the allowed strings for this option
439          *  @param strings a vector of allowed strings
440          */
441         void set_popdown_strings (const std::vector<std::string>& strings);
442
443         void clear ();
444         void changed ();
445         void set_sensitive (bool yn);
446
447         Gtk::Widget& tip_widget() { return *_combo; }
448
449 private:
450         sigc::slot<std::string> _get;
451         sigc::slot<bool, std::string> _set;
452         Gtk::Label* _label;
453         Gtk::ComboBoxText* _combo;
454 };
455
456
457 /** Component which provides the UI to handle a boolean option which needs
458  *  to be represented as a ComboBox to be clear to the user.
459  */
460 class BoolComboOption : public Option
461 {
462 public:
463         BoolComboOption (
464                 std::string const &,
465                 std::string const &,
466                 std::string const &,
467                 std::string const &,
468                 sigc::slot<bool>,
469                 sigc::slot<bool, bool>
470                 );
471
472         void set_state_from_config ();
473         void add_to_page (OptionEditorPage *);
474         void changed ();
475         void set_sensitive (bool);
476
477         Gtk::Widget& tip_widget() { return *_combo; }
478
479 private:
480         sigc::slot<bool> _get;
481         sigc::slot<bool, bool> _set;
482         Gtk::Label* _label;
483         Gtk::ComboBoxText* _combo;
484 };
485
486
487 /** Component which provides the UI to handle an numeric option using a GTK SpinButton */
488 template <class T>
489 class SpinOption : public Option
490 {
491 public:
492         /** Construct an SpinOption.
493          *  @param i id
494          *  @param n User-visible name.
495          *  @param g Slot to get the variable's value.
496          *  @param s Slot to set the variable's value.
497          *  @param min Variable minimum value.
498          *  @param max Variable maximum value.
499          *  @param step Step for the spin button.
500          *  @param page Page step for the spin button.
501          *  @param unit Unit name.
502          *  @param scale Scaling factor (such that for a value x in the spinbutton, x * scale is written to the config)
503          *  @param digits Number of decimal digits to show.
504          */
505         SpinOption (
506                 std::string const & i,
507                 std::string const & n,
508                 sigc::slot<T> g,
509                 sigc::slot<bool, T> s,
510                 T min,
511                 T max,
512                 T step,
513                 T page,
514                 std::string const & unit = "",
515                 float scale = 1,
516                 unsigned digits = 0
517                 )
518                 : Option (i, n)
519                 , _get (g)
520                 , _set (s)
521                 , _scale (scale)
522         {
523                 _label = Gtk::manage (new Gtk::Label (n + ":"));
524                 _label->set_alignment (0, 0.5);
525
526                 _spin = Gtk::manage (new Gtk::SpinButton);
527                 _spin->set_range (min, max);
528                 _spin->set_increments (step, page);
529                 _spin->set_digits(digits);
530
531                 _box = Gtk::manage (new Gtk::HBox);
532                 _box->pack_start (*_spin, true, true);
533                 _box->set_spacing (4);
534                 if (unit.length()) {
535                         _box->pack_start (*Gtk::manage (new Gtk::Label (unit)), false, false);
536                 }
537
538                 _spin->signal_value_changed().connect (sigc::mem_fun (*this, &SpinOption::changed));
539         }
540
541         void set_state_from_config ()
542         {
543                 _spin->set_value (_get () / _scale);
544         }
545
546         void add_to_page (OptionEditorPage* p)
547         {
548                 add_widgets_to_page (p, _label, _box, false);
549         }
550
551         void changed ()
552         {
553                 _set (static_cast<T> (_spin->get_value ()) * _scale);
554         }
555
556         Gtk::Widget& tip_widget() { return *_spin; }
557
558 private:
559         sigc::slot<T> _get;
560         sigc::slot<bool, T> _set;
561         float _scale;
562         Gtk::Label* _label;
563         Gtk::HBox* _box;
564         Gtk::SpinButton* _spin;
565 };
566
567 class FaderOption : public Option
568 {
569 public:
570
571         FaderOption (std::string const &, std::string const &, sigc::slot<ARDOUR::gain_t> g, sigc::slot<bool, ARDOUR::gain_t> s);
572         void set_state_from_config ();
573         void add_to_page (OptionEditorPage *);
574
575         Gtk::Widget& tip_widget() { return *_db_slider; }
576
577 private:
578         void db_changed ();
579         void on_activate ();
580         bool on_key_press (GdkEventKey* ev);
581
582         Gtk::Adjustment _db_adjustment;
583         ArdourWidgets::HSliderController* _db_slider;
584         Gtk::Entry _db_display;
585         Gtk::Label _label;
586         Gtk::HBox _box;
587         Gtk::VBox _fader_centering_box;
588         sigc::slot<ARDOUR::gain_t> _get;
589         sigc::slot<bool, ARDOUR::gain_t> _set;
590 };
591
592 class WidgetOption : public Option
593 {
594   public:
595         WidgetOption (std::string const & i, std::string const & n, Gtk::Widget& w);
596
597         void add_to_page (OptionEditorPage*);
598         void parameter_changed (std::string const &) {}
599         void set_state_from_config () {}
600
601         Gtk::Widget& tip_widget() { return *_widget; }
602
603   private:
604         Gtk::Widget* _widget;
605 };
606
607 class ClockOption : public Option
608 {
609 public:
610         ClockOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
611         void set_state_from_config ();
612         void add_to_page (OptionEditorPage *);
613         void set_session (ARDOUR::Session *);
614
615         Gtk::Widget& tip_widget() { return _clock; }
616         AudioClock& clock() { return _clock; }
617
618 private:
619         void save_clock_time ();
620         Gtk::Label _label;
621         AudioClock _clock;
622         sigc::slot<std::string> _get;
623         sigc::slot<bool, std::string> _set;
624         ARDOUR::Session *_session;
625 };
626
627 class DirectoryOption : public Option
628 {
629 public:
630         DirectoryOption (std::string const &, std::string const &, sigc::slot<std::string>, sigc::slot<bool, std::string>);
631
632         void set_state_from_config ();
633         void add_to_page (OptionEditorPage *);
634
635         Gtk::Widget& tip_widget() { return _file_chooser; }
636
637 private:
638         void selection_changed ();
639
640         sigc::slot<std::string> _get; ///< slot to get the configuration variable's value
641         sigc::slot<bool, std::string> _set;  ///< slot to set the configuration variable's value
642         Gtk::FileChooserButton _file_chooser;
643 };
644
645 /** Class to represent a single page in an OptionEditor's notebook.
646  *  Pages are laid out using a 3-column table; the 1st column is used
647  *  to indent non-headings, and the 2nd and 3rd for actual content.
648  */
649 class OptionEditorPage
650 {
651 public:
652         OptionEditorPage (Gtk::Notebook&, std::string const &);
653         OptionEditorPage ();
654
655         Gtk::VBox box;
656         Gtk::Table table;
657         std::list<OptionEditorComponent*> components;
658
659 private:
660         void init ();
661 };
662
663 class OptionEditorMiniPage : public OptionEditorComponent, public OptionEditorPage
664 {
665 public:
666         OptionEditorMiniPage ()
667         {
668                 box.pack_start (table, false, false);
669                 box.set_border_width (0);
670         }
671
672         void parameter_changed (std::string const &) = 0;
673         void set_state_from_config () = 0;
674         virtual void add_to_page (OptionEditorPage*);
675
676         Gtk::Widget& tip_widget() { return *table.children().front().get_widget(); }
677 };
678
679 /** The OptionEditor dialog base class */
680 class OptionEditor : virtual public sigc::trackable
681 {
682 public:
683         OptionEditor (PBD::Configuration *);
684         virtual ~OptionEditor ();
685
686         void add_option (std::string const &, OptionEditorComponent *);
687         void add_page (std::string const &, Gtk::Widget& page_widget);
688
689         void set_current_page (std::string const &);
690
691 protected:
692         virtual void parameter_changed (std::string const &);
693
694         PBD::Configuration* _config;
695         Gtk::Notebook& notebook() { return _notebook; }
696         Gtk::TreeView& treeview() { return option_treeview; }
697
698         class OptionColumns : public Gtk::TreeModel::ColumnRecord
699         {
700                 public:
701                         Gtk::TreeModelColumn<std::string> name;
702                         Gtk::TreeModelColumn<Gtk::Widget*> widget;
703
704                         OptionColumns() {
705                                 add (name);
706                                 add (widget);
707                         }
708         };
709
710         OptionColumns option_columns;
711         Glib::RefPtr<Gtk::TreeStore> option_tree;
712
713 private:
714         PBD::ScopedConnection config_connection;
715         Gtk::Notebook _notebook;
716         Gtk::TreeView option_treeview;
717         std::map<std::string, OptionEditorPage*> _pages;
718
719         void add_path_to_treeview (std::string const &, Gtk::Widget&);
720         Gtk::TreeModel::iterator find_path_in_treemodel (std::string const & pn,
721                                                          bool create_missing = false);
722         void treeview_row_selected ();
723 };
724
725 /** The OptionEditor dialog-as-container base class */
726 class OptionEditorContainer : public OptionEditor, public Gtk::VBox
727 {
728 public:
729         OptionEditorContainer (PBD::Configuration *, std::string const &);
730         ~OptionEditorContainer() {}
731 private:
732         Gtk::HBox hpacker;
733 };
734
735 /** The OptionEditor dialog-as-container base class */
736 class OptionEditorWindow : public OptionEditor, public ArdourWindow
737 {
738 public:
739         OptionEditorWindow (PBD::Configuration *, std::string const &);
740         ~OptionEditorWindow() {}
741 private:
742         Gtk::VBox container;
743         Gtk::HBox hpacker;
744 };
745
746 #endif /* __gtk_ardour_option_editor_h__ */