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