Add Locale config preferences and rearrange UI prefs
[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 #include "pbd/strsplit.h"
34
35 #include "gui_thread.h"
36 #include "option_editor.h"
37 #include "public_editor.h"
38 #include "utils.h"
39 #include "pbd/i18n.h"
40
41 using namespace std;
42 using namespace Gtk;
43 using namespace Gtkmm2ext;
44 using namespace ARDOUR;
45
46 void
47 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
48 {
49         int const n = p->table.property_n_rows();
50         int m = n + 1;
51         if (!_note.empty ()) {
52                 ++m;
53         }
54
55         p->table.resize (m, 3);
56         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
57
58         maybe_add_note (p, n + 1);
59 }
60
61 void
62 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
63 {
64         int const n = p->table.property_n_rows();
65         int m = n + 1;
66         if (!_note.empty ()) {
67                 ++m;
68         }
69
70         p->table.resize (m, 3);
71         p->table.attach (*wa, 1, 2, n, n + 1, FILL);
72         p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
73
74         maybe_add_note (p, n + 1);
75 }
76
77 void
78 OptionEditorComponent::maybe_add_note (OptionEditorPage* p, int n)
79 {
80         if (!_note.empty ()) {
81                 Gtk::Label* l = manage (new Gtk::Label (string_compose (X_("<i>%1</i>"), _note)));
82                 l->set_use_markup (true);
83                 p->table.attach (*l, 1, 3, n, n + 1, FILL | EXPAND);
84         }
85 }
86
87 void
88 OptionEditorComponent::set_note (string const & n)
89 {
90         _note = n;
91 }
92
93 OptionEditorHeading::OptionEditorHeading (string const & h)
94 {
95         std::stringstream s;
96         s << "<b>" << h << "</b>";
97         _label = manage (left_aligned_label (s.str()));
98         _label->set_use_markup (true);
99 }
100
101 void
102 OptionEditorHeading::add_to_page (OptionEditorPage* p)
103 {
104         int const n = p->table.property_n_rows();
105         p->table.resize (n + 2, 3);
106         if (!_note.empty ()) {
107                 p->table.resize (n + 3, 3);
108         } else {
109                 p->table.resize (n + 2, 3);
110         }
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         maybe_add_note (p, n + 2);
115 }
116
117 void
118 OptionEditorBox::add_to_page (OptionEditorPage* p)
119 {
120         add_widget_to_page (p, _box);
121 }
122
123 RcActionButton::RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l)
124         : _label (NULL)
125 {
126         _button = manage (new Button (t));
127         _button->signal_clicked().connect (slot);
128         if (!l.empty ()) {
129                 _label = manage (right_aligned_label (l));
130         }
131 }
132
133 void
134 RcActionButton::add_to_page (OptionEditorPage *p)
135 {
136         int const n = p->table.property_n_rows();
137         int m = n + 1;
138         p->table.resize (m, 3);
139         if (_label) {
140                 p->table.attach (*_label,  1, 2, n, n + 1, FILL | EXPAND);
141                 p->table.attach (*_button, 2, 3, n, n + 1, FILL | EXPAND);
142         } else {
143                 p->table.attach (*_button, 1, 3, n, n + 1, FILL | EXPAND);
144         }
145 }
146
147 RcConfigDisplay::RcConfigDisplay (string const & i, string const & n, sigc::slot<string> g, char s)
148         : _get (g)
149         , _id (i)
150         , _sep (s)
151 {
152         _label = manage (right_aligned_label (n));
153         _info = manage (new Label);
154         _info-> set_line_wrap (true);
155         set_state_from_config ();
156 }
157
158 void
159 RcConfigDisplay::set_state_from_config ()
160 {
161         string p = _get();
162         if (_sep) {
163                 std::replace (p.begin(), p.end(), _sep, '\n');
164         }
165         _info->set_text (p);
166 }
167
168 void
169 RcConfigDisplay::parameter_changed (std::string const & p)
170 {
171         if (p == _id) {
172                 set_state_from_config ();
173         }
174 }
175
176 void
177 RcConfigDisplay::add_to_page (OptionEditorPage *p)
178 {
179         int const n = p->table.property_n_rows();
180         int m = n + 1;
181         p->table.resize (m, 3);
182         p->table.attach (*_label, 1, 2, n, n + 1, FILL | EXPAND);
183         p->table.attach (*_info,  2, 3, n, n + 1, FILL | EXPAND);
184 }
185
186
187 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
188         : Option (i, n),
189           _get (g),
190           _set (s)
191 {
192         _button = manage (new CheckButton);
193         _label = manage (new Label);
194         _label->set_markup (n);
195         _button->add (*_label);
196         _button->set_active (_get ());
197         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
198 }
199
200 void
201 BoolOption::add_to_page (OptionEditorPage* p)
202 {
203         add_widget_to_page (p, _button);
204 }
205
206 void
207 BoolOption::set_state_from_config ()
208 {
209         _button->set_active (_get ());
210 }
211
212 void
213 BoolOption::toggled ()
214 {
215         if (!_set (_button->get_active ())) {
216                 _button->set_active (_get ());
217         }
218 }
219
220 RouteDisplayBoolOption::RouteDisplayBoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
221         : BoolOption (i, n, g, s)
222 {
223 }
224
225 void
226 RouteDisplayBoolOption::toggled ()
227 {
228         DisplaySuspender ds;
229         BoolOption::toggled ();
230 }
231
232 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
233         : Option (i, n),
234           _get (g),
235           _set (s)
236 {
237         _label = manage (left_aligned_label (n + ":"));
238         _entry = manage (new Entry);
239         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
240         _entry->signal_focus_out_event().connect (sigc::mem_fun (*this, &EntryOption::focus_out));
241         _entry->signal_insert_text().connect (sigc::mem_fun (*this, &EntryOption::filter_text));
242 }
243
244 void
245 EntryOption::add_to_page (OptionEditorPage* p)
246 {
247         add_widgets_to_page (p, _label, _entry);
248 }
249
250 void
251 EntryOption::set_state_from_config ()
252 {
253         _entry->set_text (_get ());
254 }
255
256 void
257 EntryOption::set_sensitive (bool s)
258 {
259         _entry->set_sensitive (s);
260 }
261
262 void
263 EntryOption::filter_text (const Glib::ustring&, int*)
264 {
265         std::string text = _entry->get_text ();
266         for (size_t i = 0; i < _invalid.length(); ++i) {
267                 text.erase (std::remove(text.begin(), text.end(), _invalid.at(i)), text.end());
268         }
269         if (text != _entry->get_text ()) {
270                 _entry->set_text (text);
271         }
272 }
273
274 void
275 EntryOption::activated ()
276 {
277         _set (_entry->get_text ());
278 }
279
280 bool
281 EntryOption::focus_out (GdkEventFocus*)
282 {
283         _set (_entry->get_text ());
284         return true;
285 }
286
287 /** Construct a BoolComboOption.
288  *  @param i id
289  *  @param n User-visible name.
290  *  @param t Text to give for the variable being true.
291  *  @param f Text to give for the variable being false.
292  *  @param g Slot to get the variable's value.
293  *  @param s Slot to set the variable's value.
294  */
295 BoolComboOption::BoolComboOption (
296         string const & i, string const & n, string const & t, string const & f,
297         sigc::slot<bool> g, sigc::slot<bool, bool> s
298         )
299         : Option (i, n)
300         , _get (g)
301         , _set (s)
302 {
303         _label = manage (new Label (n + ":"));
304         _label->set_alignment (0, 0.5);
305         _combo = manage (new ComboBoxText);
306
307         /* option 0 is the false option */
308         _combo->append_text (f);
309         /* and option 1 is the true */
310         _combo->append_text (t);
311
312         _combo->signal_changed().connect (sigc::mem_fun (*this, &BoolComboOption::changed));
313 }
314
315 void
316 BoolComboOption::set_state_from_config ()
317 {
318         _combo->set_active (_get() ? 1 : 0);
319 }
320
321 void
322 BoolComboOption::add_to_page (OptionEditorPage* p)
323 {
324         add_widgets_to_page (p, _label, _combo);
325 }
326
327 void
328 BoolComboOption::changed ()
329 {
330         _set (_combo->get_active_row_number () == 0 ? false : true);
331 }
332
333 void
334 BoolComboOption::set_sensitive (bool yn)
335 {
336         _combo->set_sensitive (yn);
337 }
338
339
340
341 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
342         : Option (i, n)
343         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
344         , _get (g)
345         , _set (s)
346 {
347         _db_slider = manage (new HSliderController (&_db_adjustment, boost::shared_ptr<PBD::Controllable>(), 115, 18));
348
349         _label.set_text (n + ":");
350         _label.set_alignment (0, 0.5);
351         _label.set_name (X_("OptionsLabel"));
352
353         _fader_centering_box.pack_start (*_db_slider, true, false);
354
355         _box.set_spacing (4);
356         _box.set_homogeneous (false);
357         _box.pack_start (_fader_centering_box, false, false);
358         _box.pack_start (_db_display, false, false);
359         _box.show_all ();
360
361         set_size_request_to_display_given_text (_db_display, "-99.00", 12, 12);
362
363         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
364         _db_display.signal_activate().connect (sigc::mem_fun (*this, &FaderOption::on_activate));
365         _db_display.signal_key_press_event().connect (sigc::mem_fun (*this, &FaderOption::on_key_press), false);
366 }
367
368 void
369 FaderOption::set_state_from_config ()
370 {
371         gain_t const val = _get ();
372         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
373
374         char buf[16];
375
376         if (val == 0.0) {
377                 snprintf (buf, sizeof (buf), "-inf");
378         } else {
379                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
380         }
381
382         _db_display.set_text (buf);
383 }
384
385 void
386 FaderOption::db_changed ()
387 {
388         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
389 }
390
391 void
392 FaderOption::on_activate ()
393 {
394         float db_val = atof (_db_display.get_text ().c_str ());
395         gain_t coeff_val = dB_to_coefficient (db_val);
396
397         _db_adjustment.set_value (gain_to_slider_position_with_max (coeff_val, Config->get_max_gain ()));
398 }
399
400 bool
401 FaderOption::on_key_press (GdkEventKey* ev)
402 {
403         if (ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (ev->keyval)) {
404                 /* drop through to normal handling */
405                 return false;
406         }
407         /* illegal key for gain entry */
408         return true;
409 }
410
411 void
412 FaderOption::add_to_page (OptionEditorPage* p)
413 {
414         add_widgets_to_page (p, &_label, &_box);
415 }
416
417 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<std::string> g, sigc::slot<bool, std::string> s)
418         : Option (i, n)
419         , _clock (X_("timecode-offset"), true, X_(""), true, false, true, false)
420         , _get (g)
421         , _set (s)
422 {
423         _label.set_text (n + ":");
424         _label.set_alignment (0, 0.5);
425         _label.set_name (X_("OptionsLabel"));
426         _clock.ValueChanged.connect (sigc::mem_fun (*this, &ClockOption::save_clock_time));
427 }
428
429 void
430 ClockOption::set_state_from_config ()
431 {
432         Timecode::Time TC;
433         framepos_t when;
434         if (!Timecode::parse_timecode_format(_get(), TC)) {
435                 _clock.set (0, true);
436         }
437         TC.rate = _session->samples_per_timecode_frame();
438         TC.drop = _session->timecode_drop_frames();
439         _session->timecode_to_sample(TC, when, false, false);
440         if (TC.negative) { when=-when; }
441         _clock.set (when, true);
442 }
443
444 void
445 ClockOption::save_clock_time ()
446 {
447         Timecode::Time TC;
448         _session->sample_to_timecode(_clock.current_time(), TC, false, false);
449         _set (Timecode::timecode_format_time(TC));
450 }
451
452 void
453 ClockOption::add_to_page (OptionEditorPage* p)
454 {
455         add_widgets_to_page (p, &_label, &_clock);
456 }
457
458 void
459 ClockOption::set_session (Session* s)
460 {
461         _session = s;
462         _clock.set_session (s);
463 }
464
465 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
466         : table (1, 3)
467 {
468         table.set_spacings (4);
469         table.set_col_spacing (0, 32);
470         box.pack_start (table, false, false);
471         box.set_border_width (4);
472         n.append_page (box, t);
473 }
474
475 /** Construct an OptionEditor.
476  *  @param o Configuration to edit.
477  *  @param t Title for the dialog.
478  */
479 OptionEditor::OptionEditor (PBD::Configuration* c)
480         : _config (c)
481         , option_tree (TreeStore::create (option_columns))
482         , option_treeview (option_tree)
483 {
484         using namespace Notebook_Helpers;
485
486         _notebook.set_show_tabs (false);
487         _notebook.set_show_border (true);
488         _notebook.set_name ("OptionsNotebook");
489
490         option_treeview.append_column ("", option_columns.name);
491         option_treeview.set_enable_search(true);
492         option_treeview.set_search_column(0);
493         option_treeview.set_name ("OptionsTreeView");
494         option_treeview.set_headers_visible (false);
495
496         option_treeview.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
497         option_treeview.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &OptionEditor::treeview_row_selected));
498
499         /* Watch out for changes to parameters */
500         _config->ParameterChanged.connect (config_connection, invalidator (*this), boost::bind (&OptionEditor::parameter_changed, this, _1), gui_context());
501 }
502
503 OptionEditor::~OptionEditor ()
504 {
505         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
506                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
507                         delete *j;
508                 }
509                 delete i->second;
510         }
511 }
512
513 /** Called when a configuration parameter has been changed.
514  *  @param p Parameter name.
515  */
516 void
517 OptionEditor::parameter_changed (std::string const & p)
518 {
519         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
520
521         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
522                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
523                         (*j)->parameter_changed (p);
524                 }
525         }
526 }
527
528 void
529 OptionEditor::treeview_row_selected ()
530 {
531         Glib::RefPtr<Gtk::TreeSelection> selection = option_treeview.get_selection();
532         TreeModel::iterator iter = selection->get_selected();
533
534         if (iter) {
535                 TreeModel::Row row = *iter;
536                 Gtk::Widget* w = row[option_columns.widget];
537                 if (w) {
538                         _notebook.set_current_page (_notebook.page_num (*w));
539                 }
540         }
541 }
542
543 TreeModel::iterator
544 OptionEditor::find_path_in_treemodel (std::string const & pn, bool create_missing)
545 {
546         /* split page name, which is actually a path, into each component */
547
548         std::vector<std::string> components;
549         split (pn, components, '/');
550
551         /* start with top level children */
552
553         TreeModel::Children children = option_tree->children();
554         TreeModel::iterator iter;
555
556         /* foreach path component ... */
557
558         for (std::vector<std::string>::const_iterator s = components.begin(); s != components.end(); ++s) {
559
560                 for (iter = children.begin(); iter != children.end(); ++iter) {
561                         TreeModel::Row row = *iter;
562                         const std::string row_name = row[option_columns.name];
563                         if (row_name == (*s)) {
564                                 break;
565                         }
566                 }
567
568                 if (iter == children.end()) {
569                         /* the current component is missing; bail out or create it */
570                         if (!create_missing) {
571                                 return option_tree->get_iter(TreeModel::Path(""));
572                         } else {
573                                 iter = option_tree->append (children);
574                                 TreeModel::Row row = *iter;
575                                 row[option_columns.name] = *s;
576                                 row[option_columns.widget] = 0;
577                         }
578                 }
579
580                 /* from now on, iter points to a valid row, either the one we found or a new one */
581                 /* set children to the row's children to continue searching */
582                 children = (*iter)->children ();
583
584         }
585
586         return iter;
587 }
588
589 void
590 OptionEditor::add_path_to_treeview (std::string const & pn, Gtk::Widget& widget)
591 {
592         option_treeview.set_model (Glib::RefPtr<TreeStore>());
593
594         TreeModel::iterator row_iter = find_path_in_treemodel(pn, true);
595
596         assert(row_iter);
597
598         TreeModel::Row row = *row_iter;
599         row[option_columns.widget] = &widget;
600
601         option_treeview.set_model (option_tree);
602         option_treeview.expand_all ();
603 }
604
605 /** Add a component to a given page.
606  *  @param pn Page name (will be created if it doesn't already exist)
607  *  @param o Component.
608  */
609 void
610 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
611 {
612         if (_pages.find (pn) == _pages.end()) {
613                 OptionEditorPage* oep = new OptionEditorPage (_notebook, pn);
614                 _pages[pn] = oep;
615
616                 add_path_to_treeview (pn, oep->box);
617         }
618
619         OptionEditorPage* p = _pages[pn];
620         p->components.push_back (o);
621
622         o->add_to_page (p);
623         o->set_state_from_config ();
624 }
625
626 /** Add a new page
627  *  @param pn Page name (will be created if it doesn't already exist)
628  *  @param w widget that fills the page
629  */
630 void
631 OptionEditor::add_page (std::string const & pn, Gtk::Widget& w)
632 {
633         if (_pages.find (pn) == _pages.end()) {
634                 OptionEditorPage* oep = new OptionEditorPage (_notebook, pn);
635                 _pages[pn] = oep;
636                 add_path_to_treeview (pn, oep->box);
637         }
638
639         OptionEditorPage* p = _pages[pn];
640         p->box.pack_start (w, true, true);
641 }
642
643 void
644 OptionEditor::set_current_page (string const & p)
645 {
646         TreeModel::iterator row_iter = find_path_in_treemodel(p);
647
648         if (row_iter) {
649                 option_treeview.get_selection()->select(row_iter);
650         }
651
652 }
653
654
655 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
656         : Option (i, n)
657         , _get (g)
658         , _set (s)
659 {
660         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
661         _file_chooser.signal_selection_changed().connect (sigc::mem_fun (*this, &DirectoryOption::selection_changed));
662 }
663
664
665 void
666 DirectoryOption::set_state_from_config ()
667 {
668         _file_chooser.set_current_folder (poor_mans_glob(_get ()));
669 }
670
671 void
672 DirectoryOption::add_to_page (OptionEditorPage* p)
673 {
674         Gtk::Label *label = manage (new Label (_name));
675         label->set_alignment (0, 0.5);
676         label->set_name (X_("OptionsLabel"));
677         add_widgets_to_page (p, label, &_file_chooser);
678 }
679
680 void
681 DirectoryOption::selection_changed ()
682 {
683         _set (poor_mans_glob(_file_chooser.get_filename ()));
684 }
685
686 /*--------------------------*/
687
688 OptionEditorContainer::OptionEditorContainer (PBD::Configuration* c, string const& str)
689         : OptionEditor (c)
690 {
691         set_border_width (4);
692         hpacker.pack_start (treeview(), false, false);
693         hpacker.pack_start (notebook(), true, true);
694         pack_start (hpacker, true, true);
695
696         show_all ();
697 }
698
699 OptionEditorWindow::OptionEditorWindow (PBD::Configuration* c, string const& str)
700         : OptionEditor (c)
701         , ArdourWindow (str)
702 {
703         container.set_border_width (4);
704         hpacker.pack_start (treeview(), false, false);
705         hpacker.pack_start (notebook(), true, true);
706
707         container.pack_start (hpacker, true, true);
708
709         hpacker.show_all ();
710         container.show ();
711
712         add (container);
713 }