move utility functions into a dedicated namespace
[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
24 #include "ardour/configuration.h"
25 #include "ardour/rc_configuration.h"
26 #include "ardour/utils.h"
27 #include "ardour/dB.h"
28 #include "ardour/session.h"
29
30 #include "option_editor.h"
31 #include "gui_thread.h"
32 #include "i18n.h"
33
34 using namespace std;
35 using namespace Gtk;
36 using namespace Gtkmm2ext;
37 using namespace ARDOUR;
38
39 void
40 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
41 {
42         int const n = p->table.property_n_rows();
43         int m = n + 1;
44         if (!_note.empty ()) {
45                 ++m;
46         }
47
48         p->table.resize (m, 3);
49         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
50
51         maybe_add_note (p, n + 1);
52 }
53
54 void
55 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
56 {
57         int const n = p->table.property_n_rows();
58         int m = n + 1;
59         if (!_note.empty ()) {
60                 ++m;
61         }
62         
63         p->table.resize (m, 3);
64         p->table.attach (*wa, 1, 2, n, n + 1, FILL);
65         p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
66         
67         maybe_add_note (p, n + 1);
68 }
69
70 void
71 OptionEditorComponent::maybe_add_note (OptionEditorPage* p, int n)
72 {
73         if (!_note.empty ()) {
74                 Gtk::Label* l = manage (new Gtk::Label (string_compose (X_("<i>%1</i>"), _note)));
75                 l->set_use_markup (true);
76                 p->table.attach (*l, 1, 3, n, n + 1, FILL | EXPAND);
77         }
78 }
79
80 void
81 OptionEditorComponent::set_note (string const & n)
82 {
83         _note = n;
84 }
85
86 OptionEditorHeading::OptionEditorHeading (string const & h)
87 {
88         std::stringstream s;
89         s << "<b>" << h << "</b>";
90         _label = manage (left_aligned_label (s.str()));
91         _label->set_use_markup (true);
92 }
93
94 void
95 OptionEditorHeading::add_to_page (OptionEditorPage* p)
96 {
97         int const n = p->table.property_n_rows();
98         p->table.resize (n + 2, 3);
99
100         p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
101         p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
102 }
103
104 void
105 OptionEditorBox::add_to_page (OptionEditorPage* p)
106 {
107         add_widget_to_page (p, _box);
108 }
109
110 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
111         : Option (i, n),
112           _get (g),
113           _set (s)
114 {
115         _button = manage (new CheckButton);
116         _label = manage (new Label);
117         _label->set_markup (n);
118         _button->add (*_label);
119         _button->set_active (_get ());
120         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
121 }
122
123 void
124 BoolOption::add_to_page (OptionEditorPage* p)
125 {
126         add_widget_to_page (p, _button);
127 }
128
129 void
130 BoolOption::set_state_from_config ()
131 {
132         _button->set_active (_get ());
133 }
134
135 void
136 BoolOption::toggled ()
137 {
138         _set (_button->get_active ());
139 }
140
141 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
142         : Option (i, n),
143           _get (g),
144           _set (s)
145 {
146         _label = manage (left_aligned_label (n + ":"));
147         _entry = manage (new Entry);
148         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
149 }
150
151 void
152 EntryOption::add_to_page (OptionEditorPage* p)
153 {
154         add_widgets_to_page (p, _label, _entry);
155 }
156
157 void
158 EntryOption::set_state_from_config ()
159 {
160         _entry->set_text (_get ());
161 }
162
163 void
164 EntryOption::activated ()
165 {
166         _set (_entry->get_text ());
167 }
168
169 /** Construct a BoolComboOption.
170  *  @param i id
171  *  @param n User-visible name.
172  *  @param t Text to give for the variable being true.
173  *  @param f Text to give for the variable being false.
174  *  @param g Slot to get the variable's value.
175  *  @param s Slot to set the variable's value.
176  */
177 BoolComboOption::BoolComboOption (
178         string const & i, string const & n, string const & t, string const & f, 
179         sigc::slot<bool> g, sigc::slot<bool, bool> s
180         )
181         : Option (i, n)
182         , _get (g)
183         , _set (s)
184 {
185         _label = manage (new Label (n + ":"));
186         _label->set_alignment (0, 0.5);
187         _combo = manage (new ComboBoxText);
188
189         /* option 0 is the false option */
190         _combo->append_text (f);
191         /* and option 1 is the true */
192         _combo->append_text (t);
193         
194         _combo->signal_changed().connect (sigc::mem_fun (*this, &BoolComboOption::changed));
195 }
196
197 void
198 BoolComboOption::set_state_from_config ()
199 {
200         _combo->set_active (_get() ? 1 : 0);
201 }
202
203 void
204 BoolComboOption::add_to_page (OptionEditorPage* p)
205 {
206         add_widgets_to_page (p, _label, _combo);
207 }
208
209 void
210 BoolComboOption::changed ()
211 {
212         _set (_combo->get_active_row_number () == 0 ? false : true);
213 }
214
215 void
216 BoolComboOption::set_sensitive (bool yn)
217 {
218         _combo->set_sensitive (yn);
219 }
220         
221
222           
223 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
224         : Option (i, n)
225         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
226         , _get (g)
227         , _set (s)
228 {
229         _db_slider = manage (new HSliderController (&_db_adjustment, 115, 18, false));
230
231         _label.set_text (n + ":");
232         _label.set_name (X_("OptionsLabel"));
233
234         _fader_centering_box.pack_start (*_db_slider, true, false);
235
236         _box.set_spacing (4);
237         _box.set_homogeneous (false);
238         _box.pack_start (_fader_centering_box, false, false);
239         _box.pack_start (_db_display, false, false);
240         _box.show_all ();
241
242         set_size_request_to_display_given_text (_db_display, "-99.00", 12, 12);
243
244         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
245 }
246
247 void
248 FaderOption::set_state_from_config ()
249 {
250         gain_t const val = _get ();
251         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
252
253         char buf[16];
254
255         if (val == 0.0) {
256                 snprintf (buf, sizeof (buf), "-inf");
257         } else {
258                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
259         }
260
261         _db_display.set_text (buf);
262 }
263
264 void
265 FaderOption::db_changed ()
266 {
267         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
268 }
269
270 void
271 FaderOption::add_to_page (OptionEditorPage* p)
272 {
273         add_widgets_to_page (p, &_label, &_box);
274 }
275
276 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<std::string> g, sigc::slot<bool, std::string> s)
277         : Option (i, n)
278         , _clock (X_("timecode-offset"), true, X_(""), true, false, true, false)
279         , _get (g)
280         , _set (s)
281 {
282         _label.set_text (n + ":");
283         _label.set_alignment (0, 0.5);
284         _label.set_name (X_("OptionsLabel"));
285         _clock.ValueChanged.connect (sigc::mem_fun (*this, &ClockOption::save_clock_time));
286 }
287
288 void
289 ClockOption::set_state_from_config ()
290 {
291         Timecode::Time TC;
292         framepos_t when;
293         if (!Timecode::parse_timecode_format(_get(), TC)) {
294                 _clock.set (0, true);
295         }
296         TC.rate = _session->frames_per_timecode_frame();
297         TC.drop = _session->timecode_drop_frames();
298         _session->timecode_to_sample(TC, when, false, false);
299         if (TC.negative) { when=-when; }
300         _clock.set (when, true);
301 }
302
303 void
304 ClockOption::save_clock_time ()
305 {
306         Timecode::Time TC;
307         _session->sample_to_timecode(_clock.current_time(), TC, false, false);
308         _set (Timecode::timecode_format_time(TC));
309 }
310
311 void
312 ClockOption::add_to_page (OptionEditorPage* p)
313 {
314         add_widgets_to_page (p, &_label, &_clock);
315 }
316
317 void
318 ClockOption::set_session (Session* s)
319 {
320         _session = s;
321         _clock.set_session (s);
322 }
323
324 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
325         : table (1, 3)
326 {
327         table.set_spacings (4);
328         table.set_col_spacing (0, 32);
329         box.pack_start (table, false, false);
330         box.set_border_width (4);
331         n.append_page (box, t);
332 }
333
334 /** Construct an OptionEditor.
335  *  @param o Configuration to edit.
336  *  @param t Title for the dialog.
337  */
338 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
339         : ArdourWindow (t), _config (c)
340 {
341         using namespace Notebook_Helpers;
342
343         set_default_size (300, 300);
344         // set_wmclass (X_("ardour_preferences"), PROGRAM_NAME);
345
346         set_name ("Preferences");
347         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
348
349         set_border_width (4);
350
351         add (_notebook);
352
353         _notebook.set_show_tabs (true);
354         _notebook.set_show_border (true);
355         _notebook.set_name ("OptionsNotebook");
356
357         show_all_children();
358
359         /* Watch out for changes to parameters */
360         _config->ParameterChanged.connect (config_connection, invalidator (*this), boost::bind (&OptionEditor::parameter_changed, this, _1), gui_context());
361 }
362
363 OptionEditor::~OptionEditor ()
364 {
365         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
366                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
367                         delete *j;
368                 }
369                 delete i->second;
370         }
371 }
372
373 /** Called when a configuration parameter has been changed.
374  *  @param p Parameter name.
375  */
376 void
377 OptionEditor::parameter_changed (std::string const & p)
378 {
379         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
380
381         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
382                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
383                         (*j)->parameter_changed (p);
384                 }
385         }
386 }
387
388 /** Add a component to a given page.
389  *  @param pn Page name (will be created if it doesn't already exist)
390  *  @param o Component.
391  */
392 void
393 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
394 {
395         if (_pages.find (pn) == _pages.end()) {
396                 _pages[pn] = new OptionEditorPage (_notebook, pn);
397         }
398
399         OptionEditorPage* p = _pages[pn];
400         p->components.push_back (o);
401
402         o->add_to_page (p);
403         o->set_state_from_config ();
404 }
405
406 void
407 OptionEditor::set_current_page (string const & p)
408 {
409         int i = 0;
410         while (i < _notebook.get_n_pages ()) {
411                 if (_notebook.get_tab_label_text (*_notebook.get_nth_page (i)) == p) {
412                         _notebook.set_current_page (i);
413                         return;
414                 }
415
416                 ++i;
417         }
418 }
419
420
421 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
422         : Option (i, n)
423         , _get (g)
424         , _set (s)
425 {
426         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
427         _file_chooser.signal_file_set().connect (sigc::mem_fun (*this, &DirectoryOption::file_set));
428         _file_chooser.signal_current_folder_changed().connect (sigc::mem_fun (*this, &DirectoryOption::current_folder_set));
429 }
430
431
432 void
433 DirectoryOption::set_state_from_config ()
434 {
435         _file_chooser.set_current_folder (_get ());
436 }
437
438 void
439 DirectoryOption::add_to_page (OptionEditorPage* p)
440 {
441         add_widgets_to_page (p, manage (new Label (_name)), &_file_chooser);
442 }
443
444 void
445 DirectoryOption::file_set ()
446 {
447         _set (_file_chooser.get_filename ());
448 }
449
450 void
451 DirectoryOption::current_folder_set ()
452 {
453         _set (_file_chooser.get_current_folder ());
454 }