Delete trailing whitespace
[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 "gtkmm2ext/utils.h"
23 #include "ardour/configuration.h"
24 #include "ardour/utils.h"
25 #include "ardour/dB.h"
26 #include "option_editor.h"
27 #include "gui_thread.h"
28 #include "utils.h"
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34 using namespace ARDOUR;
35
36 void
37 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
38 {
39         int const n = p->table.property_n_rows();
40         p->table.resize (n + 1, 3);
41         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
42 }
43
44 void
45 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
46 {
47         int const n = p->table.property_n_rows();
48         p->table.resize (n + 1, 3);
49         p->table.attach (*wa, 1, 2, n, n + 1, FILL);
50         p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
51 }
52
53 OptionEditorHeading::OptionEditorHeading (string const & h)
54 {
55         std::stringstream s;
56         s << "<b>" << h << "</b>";
57         _label = manage (new Label (s.str()));
58         _label->set_alignment (0, 0.5);
59         _label->set_use_markup (true);
60 }
61
62 void
63 OptionEditorHeading::add_to_page (OptionEditorPage* p)
64 {
65         int const n = p->table.property_n_rows();
66         p->table.resize (n + 2, 3);
67
68         p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
69         p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
70 }
71
72 void
73 OptionEditorBox::add_to_page (OptionEditorPage* p)
74 {
75         add_widget_to_page (p, _box);
76 }
77
78 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
79         : Option (i, n),
80           _get (g),
81           _set (s)
82 {
83         _button = manage (new CheckButton (n));
84         _button->set_active (_get ());
85         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
86 }
87
88 void
89 BoolOption::add_to_page (OptionEditorPage* p)
90 {
91         add_widget_to_page (p, _button);
92 }
93
94 void
95 BoolOption::set_state_from_config ()
96 {
97         _button->set_active (_get ());
98 }
99
100 void
101 BoolOption::toggled ()
102 {
103         _set (_button->get_active ());
104 }
105
106 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
107         : Option (i, n),
108           _get (g),
109           _set (s)
110 {
111         _label = manage (new Label (n + ":"));
112         _label->set_alignment (0, 0.5);
113         _entry = manage (new Entry);
114         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
115 }
116
117 void
118 EntryOption::add_to_page (OptionEditorPage* p)
119 {
120         add_widgets_to_page (p, _label, _entry);
121 }
122
123 void
124 EntryOption::set_state_from_config ()
125 {
126         _entry->set_text (_get ());
127 }
128
129 void
130 EntryOption::activated ()
131 {
132         _set (_entry->get_text ());
133 }
134
135 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
136         : Option (i, n)
137         // 0.781787 is the value needed for gain to be set to 0.
138         , _db_adjustment (0.781787, 0, 1, 0.01, 0.1)
139         , _get (g)
140         , _set (s)
141 {
142         _pix = ::get_icon (X_("fader_belt_h"));
143         if (_pix == 0) {
144                 throw failed_constructor ();
145         }
146
147         _db_slider = manage (new HSliderController (_pix,
148                                                     &_db_adjustment,
149                                                     115,
150                                                     false));
151
152         _label.set_text (n + ":");
153         _label.set_name (X_("OptionsLabel"));
154
155         _box.set_spacing (4);
156         _box.pack_start (*_db_slider, false, false);
157         _box.pack_start (_db_display, false, false);
158         _box.show_all ();
159
160         set_size_request_to_display_given_text (_db_display, "-99.0", 12, 12);
161
162         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
163 }
164
165 void
166 FaderOption::set_state_from_config ()
167 {
168         gain_t const val = _get ();
169         _db_adjustment.set_value (gain_to_slider_position (val));
170
171         char buf[16];
172
173         if (val == 0.0) {
174                 snprintf (buf, sizeof (buf), "-inf");
175         } else {
176                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
177         }
178
179         _db_display.set_text (buf);
180 }
181
182 void
183 FaderOption::db_changed ()
184 {
185         _set (slider_position_to_gain (_db_adjustment.get_value ()));
186 }
187
188 void
189 FaderOption::add_to_page (OptionEditorPage* p)
190 {
191         add_widgets_to_page (p, &_label, &_box);
192 }
193
194 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<framecnt_t> g, sigc::slot<bool, framecnt_t> s)
195         : Option (i, n)
196         , _clock (X_("timecode-offset"), false, X_("TimecodeOffset"), true, false, true, false)
197         , _get (g)
198         , _set (s)
199 {
200         _label.set_text (n + ":");
201         _label.set_alignment (0, 0.5);
202         _label.set_name (X_("OptionsLabel"));
203 }
204
205 void
206 ClockOption::set_state_from_config ()
207 {
208         _clock.set (_get ());
209 }
210
211 void
212 ClockOption::add_to_page (OptionEditorPage* p)
213 {
214         add_widgets_to_page (p, &_label, &_clock);
215 }
216
217 void
218 ClockOption::set_session (Session* s)
219 {
220         _clock.set_session (s);
221 }
222
223 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
224         : table (1, 3)
225 {
226         table.set_spacings (4);
227         table.set_col_spacing (0, 32);
228         box.pack_start (table, false, false);
229         box.set_border_width (4);
230         n.append_page (box, t);
231 }
232
233 /** Construct an OptionEditor.
234  *  @param o Configuration to edit.
235  *  @param t Title for the dialog.
236  */
237 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
238         : ArdourDialog (t, false), _config (c)
239 {
240         using namespace Notebook_Helpers;
241
242         set_default_size (300, 300);
243         set_wmclass (X_("ardour_preferences"), PROGRAM_NAME);
244
245         set_name ("Preferences");
246         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
247
248         set_border_width (4);
249
250         get_vbox()->set_spacing (4);
251         get_vbox()->pack_start (_notebook);
252
253         _notebook.set_show_tabs (true);
254         _notebook.set_show_border (true);
255         _notebook.set_name ("OptionsNotebook");
256
257         show_all_children();
258
259         /* Watch out for changes to parameters */
260         _config->ParameterChanged.connect (config_connection, invalidator (*this), ui_bind (&OptionEditor::parameter_changed, this, _1), gui_context());
261 }
262
263 OptionEditor::~OptionEditor ()
264 {
265         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
266                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
267                         delete *j;
268                 }
269                 delete i->second;
270         }
271 }
272
273 /** Called when a configuration parameter has been changed.
274  *  @param p Parameter name.
275  */
276 void
277 OptionEditor::parameter_changed (std::string const & p)
278 {
279         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
280
281         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
282                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
283                         (*j)->parameter_changed (p);
284                 }
285         }
286 }
287
288 /** Add a component to a given page.
289  *  @param pn Page name (will be created if it doesn't already exist)
290  *  @param o Component.
291  */
292 void
293 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
294 {
295         if (_pages.find (pn) == _pages.end()) {
296                 _pages[pn] = new OptionEditorPage (_notebook, pn);
297         }
298
299         OptionEditorPage* p = _pages[pn];
300         p->components.push_back (o);
301
302         o->add_to_page (p);
303         o->set_state_from_config ();
304 }
305
306 void
307 OptionEditor::set_current_page (string const & p)
308 {
309         int i = 0;
310         while (i < _notebook.get_n_pages ()) {
311                 if (_notebook.get_tab_label_text (*_notebook.get_nth_page (i)) == p) {
312                         _notebook.set_current_page (i);
313                         return;
314                 }
315
316                 ++i;
317         }
318 }
319
320
321