Add session preferences dialog.
[ardour.git] / gtk2_ardour / option_editor.cc
1  /*
2     Copyright (C) 2001-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 #include <gtkmm/box.h>
21 #include <gtkmm/alignment.h>
22 #include "ardour/configuration.h"
23 #include "option_editor.h"
24 #include "gui_thread.h"
25 #include "i18n.h"
26
27 using namespace std;
28 using namespace sigc;
29 using namespace Gtk;
30 using namespace ARDOUR;
31
32 void
33 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
34 {
35         int const n = p->table.property_n_rows();
36         p->table.resize (n + 1, 3);
37         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
38 }
39
40 void
41 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
42 {
43         int const n = p->table.property_n_rows();
44         p->table.resize (n + 1, 3);
45         p->table.attach (*wa, 1, 2, n, n + 1, FILL | EXPAND);
46         p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
47 }
48
49 OptionEditorHeading::OptionEditorHeading (string const & h)
50 {
51         std::stringstream s;
52         s << "<b>" << h << "</b>";
53         _label = manage (new Label (s.str()));
54         _label->set_alignment (0, 0.5);
55         _label->set_use_markup (true);
56 }
57
58 void
59 OptionEditorHeading::add_to_page (OptionEditorPage* p)
60 {
61         int const n = p->table.property_n_rows();
62         p->table.resize (n + 2, 3);
63
64         p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
65         p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
66 }
67
68 void
69 OptionEditorBox::add_to_page (OptionEditorPage* p)
70 {
71         add_widget_to_page (p, _box);
72 }
73
74 BoolOption::BoolOption (string const & i, string const & n, slot<bool> g, slot<bool, bool> s)
75         : Option (i, n),
76           _get (g),
77           _set (s)
78 {
79         _button = manage (new CheckButton (n));
80         _button->set_active (_get ());
81         _button->signal_toggled().connect (mem_fun (*this, &BoolOption::toggled));
82 }
83
84 void
85 BoolOption::add_to_page (OptionEditorPage* p)
86 {
87         add_widget_to_page (p, _button);
88 }
89
90 void
91 BoolOption::set_state_from_config ()
92 {
93         _button->set_active (_get ());
94 }
95
96 void
97 BoolOption::toggled ()
98 {
99         _set (_button->get_active ());
100 }
101
102 EntryOption::EntryOption (string const & i, string const & n, slot<string> g, slot<bool, string> s)
103         : Option (i, n),
104           _get (g),
105           _set (s)
106 {
107         _label = manage (new Label (n + ":"));
108         _label->set_alignment (1, 0.5);
109         _entry = manage (new Entry);
110         _entry->signal_activate().connect (mem_fun (*this, &EntryOption::activated));
111 }
112
113 void
114 EntryOption::add_to_page (OptionEditorPage* p)
115 {
116         add_widgets_to_page (p, _label, _entry);
117 }
118
119 void
120 EntryOption::set_state_from_config ()
121 {
122         _entry->set_text (_get ());
123 }
124
125 void
126 EntryOption::activated ()
127 {
128         _set (_entry->get_text ());
129 }
130
131 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
132         : table (1, 3)
133 {
134         table.set_spacings (4);
135         table.set_col_spacing (0, 32);
136         box.pack_start (table, false, false);
137         box.set_border_width (4);
138         n.append_page (box, t);
139 }
140
141 /** Construct an OptionEditor.
142  *  @param o Configuration to edit.
143  *  @param t Title for the dialog.
144  */
145 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
146         : ArdourDialog (t, false), _config (c)
147 {
148         using namespace Notebook_Helpers;
149
150         set_default_size (300, 300);
151         set_wmclass (X_("ardour_preferences"), "Ardour");
152
153         set_name ("Preferences");
154         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
155
156         set_border_width (4);
157
158         get_vbox()->set_spacing (4);
159         get_vbox()->pack_start (_notebook);
160
161         _notebook.set_show_tabs (true);
162         _notebook.set_show_border (true);
163         _notebook.set_name ("OptionsNotebook");
164
165         show_all_children();
166
167         /* Watch out for changes to parameters */
168         _config->ParameterChanged.connect (mem_fun (*this, &OptionEditor::parameter_changed));
169 }
170
171 OptionEditor::~OptionEditor ()
172 {
173         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
174                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
175                         delete *j;
176                 }
177                 delete i->second;
178         }
179 }
180
181 /** Called when a configuration parameter has been changed.
182  *  @param p Parameter name.
183  */
184 void
185 OptionEditor::parameter_changed (std::string const & p)
186 {
187         ENSURE_GUI_THREAD (bind (mem_fun (*this, &OptionEditor::parameter_changed), p));
188
189         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
190                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
191                         (*j)->parameter_changed (p);
192                 }
193         }
194 }
195
196 /** Add a component to a given page.
197  *  @param pn Page name (will be created if it doesn't already exist)
198  *  @param o Component.
199  */
200 void
201 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
202 {
203         if (_pages.find (pn) == _pages.end()) {
204                 _pages[pn] = new OptionEditorPage (_notebook, pn);
205         }
206
207         OptionEditorPage* p = _pages[pn];
208         p->components.push_back (o);
209
210         o->add_to_page (p);
211         o->set_state_from_config ();
212 }