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