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