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