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