Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / option_editor.cc
1 /*
2  * Copyright (C) 2005-2006 Nick Mainsbridge <mainsbridge@gmail.com>
3  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2005-2019 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
6  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2008-2014 David Robillard <d@drobilla.net>
8  * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
9  * Copyright (C) 2015 John Emmas <john@creativepost.co.uk>
10  * Copyright (C) 2016 Julien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26 #include <algorithm>
27
28 #include <gtkmm/box.h>
29 #include <gtkmm/alignment.h>
30 #include "gtkmm2ext/utils.h"
31
32 #include "ardour/dB.h"
33 #include "ardour/rc_configuration.h"
34 #include "ardour/session.h"
35 #include "ardour/types.h"
36 #include "ardour/utils.h"
37
38 #include "pbd/configuration.h"
39 #include "pbd/replace_all.h"
40 #include "pbd/strsplit.h"
41
42 #include "gui_thread.h"
43 #include "option_editor.h"
44 #include "public_editor.h"
45 #include "utils.h"
46 #include "pbd/i18n.h"
47
48 using namespace std;
49 using namespace Gtk;
50 using namespace Gtkmm2ext;
51 using namespace ARDOUR;
52
53 void
54 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
55 {
56         int const n = p->table.property_n_rows();
57         int m = n + 1;
58         if (!_note.empty ()) {
59                 ++m;
60         }
61
62         p->table.resize (m, 3);
63         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
64
65         maybe_add_note (p, n + 1);
66 }
67
68 void
69 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb, bool /*notused*/)
70 {
71         int const n = p->table.property_n_rows();
72         int m = n + 1;
73         if (!_note.empty ()) {
74                 ++m;
75         }
76
77         p->table.resize (m, 3);
78         p->table.attach (*wa, 1, 2, n, n + 1, FILL);
79
80         Alignment* a = manage (new Alignment (0, 0.5, 0, 1.0));
81         a->add (*wb);
82         p->table.attach (*a, 2, 3, n, n + 1, FILL | EXPAND);
83
84         maybe_add_note (p, n + 1);
85 }
86
87 void
88 OptionEditorComponent::maybe_add_note (OptionEditorPage* p, int n)
89 {
90         if (!_note.empty ()) {
91                 Gtk::Label* l = manage (left_aligned_label (string_compose (X_("<i>%1</i>"), _note)));
92                 l->set_use_markup (true);
93                 l->set_line_wrap (true);
94                 p->table.attach (*l, 1, 3, n, n + 1, FILL | EXPAND);
95         }
96 }
97
98 void
99 OptionEditorComponent::set_note (string const & n)
100 {
101         _note = n;
102 }
103
104 /*--------------------------*/
105
106 OptionEditorHeading::OptionEditorHeading (string const & h)
107 {
108         std::stringstream s;
109         s << "<b>" << h << "</b>";
110         _label = manage (left_aligned_label (s.str()));
111         _label->set_use_markup (true);
112 }
113
114 /*--------------------------*/
115
116 void
117 OptionEditorHeading::add_to_page (OptionEditorPage* p)
118 {
119         int const n = p->table.property_n_rows();
120         if (!_note.empty ()) {
121                 p->table.resize (n + 3, 3);
122         } else {
123                 p->table.resize (n + 2, 3);
124         }
125
126         p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
127         p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
128         maybe_add_note (p, n + 2);
129 }
130
131 /*--------------------------*/
132
133 void
134 OptionEditorBlank::add_to_page (OptionEditorPage* p)
135 {
136         int const n = p->table.property_n_rows();
137         p->table.resize (n + 1, 3);
138         p->table.attach (_dummy, 2, 3, n, n + 1, FILL | EXPAND, SHRINK, 0, 0);
139         _dummy.set_size_request (-1, 1);
140         _dummy.show ();
141 }
142
143 /*--------------------------*/
144
145 RcConfigDisplay::RcConfigDisplay (string const & i, string const & n, sigc::slot<string> g, char s)
146         : _get (g)
147         , _id (i)
148         , _sep (s)
149 {
150         _label = manage (right_aligned_label (n));
151         _info = manage (new Label);
152         _info-> set_line_wrap (true);
153         set_state_from_config ();
154 }
155
156 void
157 RcConfigDisplay::set_state_from_config ()
158 {
159         string p = _get();
160         if (_sep) {
161                 std::replace (p.begin(), p.end(), _sep, '\n');
162         }
163         _info->set_text (p);
164 }
165
166 void
167 RcConfigDisplay::parameter_changed (std::string const & p)
168 {
169         if (p == _id) {
170                 set_state_from_config ();
171         }
172 }
173
174 void
175 RcConfigDisplay::add_to_page (OptionEditorPage *p)
176 {
177         int const n = p->table.property_n_rows();
178         int m = n + 1;
179         p->table.resize (m, 3);
180         p->table.attach (*_label, 1, 2, n, n + 1, FILL | EXPAND);
181         p->table.attach (*_info,  2, 3, n, n + 1, FILL | EXPAND);
182 }
183
184 /*--------------------------*/
185
186 RcActionButton::RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l)
187         : _label (NULL)
188 {
189         _button = manage (new Button (t));
190         _button->signal_clicked().connect (slot);
191         if (!l.empty ()) {
192                 _label = manage (right_aligned_label (l));
193         }
194 }
195
196 void
197 RcActionButton::add_to_page (OptionEditorPage *p)
198 {
199         int const n = p->table.property_n_rows();
200         const int m = n + 1;
201         p->table.resize (m, 3);
202         Alignment* a = manage (new Alignment (0, 0.5, 0, 1.0));
203         a->add (*_button);
204
205         if (_label) {
206                 p->table.attach (*_label,  1, 2, n, m);
207                 p->table.attach (*a, 2, 3, n, m, FILL|EXPAND);
208         } else {
209                 p->table.attach (*a, 1, 3, n, m, FILL|EXPAND);
210         }
211 }
212
213 /*--------------------------*/
214
215 CheckOption::CheckOption (string const & i, string const & n, Glib::RefPtr<Gtk::Action> act)
216 {
217         _button = manage (new CheckButton);
218         _label = manage (new Label);
219         _label->set_markup (n);
220         _button->add (*_label);
221         _button->signal_toggled().connect (sigc::mem_fun (*this, &CheckOption::toggled));
222
223         Gtkmm2ext::Activatable::set_related_action (act);
224         assert (_action);
225
226         action_sensitivity_changed ();
227
228         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
229         if (tact) {
230                 action_toggled ();
231                 tact->signal_toggled().connect (sigc::mem_fun (*this, &CheckOption::action_toggled));
232         }
233
234         _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &CheckOption::action_sensitivity_changed));
235 }
236
237 void
238 CheckOption::action_toggled ()
239 {
240         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
241         if (tact) {
242                 _button->set_active (tact->get_active());
243         }
244 }
245
246 void
247 CheckOption::add_to_page (OptionEditorPage* p)
248 {
249         add_widget_to_page (p, _button);
250 }
251
252 void
253 CheckOption::toggled ()
254 {
255         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
256
257         tact->set_active (_button->get_active ());
258 }
259
260
261 /*--------------------------*/
262
263 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
264         : Option (i, n),
265           _get (g),
266           _set (s)
267 {
268         _button = manage (new CheckButton);
269         _label = manage (new Label);
270         _label->set_markup (n);
271         _button->add (*_label);
272         _button->set_active (_get ());
273         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
274 }
275
276 void
277 BoolOption::add_to_page (OptionEditorPage* p)
278 {
279         add_widget_to_page (p, _button);
280 }
281
282 void
283 BoolOption::set_state_from_config ()
284 {
285         _button->set_active (_get ());
286 }
287
288 void
289 BoolOption::toggled ()
290 {
291         if (!_set (_button->get_active ())) {
292                 _button->set_active (_get ());
293         }
294 }
295
296 /*--------------------------*/
297
298 RouteDisplayBoolOption::RouteDisplayBoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
299         : BoolOption (i, n, g, s)
300 {
301 }
302
303 void
304 RouteDisplayBoolOption::toggled ()
305 {
306         DisplaySuspender ds;
307         BoolOption::toggled ();
308 }
309
310 /*--------------------------*/
311
312 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
313         : Option (i, n),
314           _get (g),
315           _set (s)
316 {
317         _label = manage (left_aligned_label (n + ":"));
318         _entry = manage (new Entry);
319         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
320         _entry->signal_focus_out_event().connect (sigc::mem_fun (*this, &EntryOption::focus_out));
321         _entry->signal_insert_text().connect (sigc::mem_fun (*this, &EntryOption::filter_text));
322 }
323
324 void
325 EntryOption::add_to_page (OptionEditorPage* p)
326 {
327         add_widgets_to_page (p, _label, _entry);
328 }
329
330 void
331 EntryOption::set_state_from_config ()
332 {
333         _entry->set_text (_get ());
334 }
335
336 void
337 EntryOption::set_sensitive (bool s)
338 {
339         _entry->set_sensitive (s);
340 }
341
342 void
343 EntryOption::filter_text (const Glib::ustring&, int*)
344 {
345         std::string text = _entry->get_text ();
346         for (size_t i = 0; i < _invalid.length(); ++i) {
347                 text.erase (std::remove(text.begin(), text.end(), _invalid.at(i)), text.end());
348         }
349         if (text != _entry->get_text ()) {
350                 _entry->set_text (text);
351         }
352 }
353
354 void
355 EntryOption::activated ()
356 {
357         _set (_entry->get_text ());
358 }
359
360 bool
361 EntryOption::focus_out (GdkEventFocus*)
362 {
363         _set (_entry->get_text ());
364         return true;
365 }
366
367 /*--------------------------*/
368 HSliderOption::HSliderOption (
369                 std::string const& i,
370                 std::string const& n,
371                 sigc::slot<float> g,
372                 sigc::slot<bool, float> s,
373                 double lower, double upper,
374                 double step_increment,
375                 double page_increment,
376                 double mult,
377                 bool logarithmic
378                 )
379         : Option (i, n)
380         , _get (g)
381         , _set (s)
382         , _adj (lower, lower, upper, step_increment, page_increment, 0)
383         , _hscale (_adj)
384         , _label (n + ":")
385         , _mult (mult)
386         , _log (logarithmic)
387 {
388         _label.set_alignment (0, 0.5);
389         _label.set_name ("OptionsLabel");
390         _adj.set_value (_get());
391         _adj.signal_value_changed().connect (sigc::mem_fun (*this, &HSliderOption::changed));
392         _hscale.set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
393
394         /* make the slider be a fixed, font-relative width */
395
396         _hscale.ensure_style ();
397         int width, height;
398         get_pixel_size (_hscale.create_pango_layout (X_("a long piece of text that is about as wide as we want sliders to be")), width, height);
399         _hscale.set_size_request (width, -1);
400 }
401
402 void
403 HSliderOption::set_state_from_config ()
404 {
405         if (_log) {
406                 _adj.set_value (log10(_get()) / _mult);
407         } else {
408                 _adj.set_value (_get() / _mult);
409         }
410 }
411
412 void
413 HSliderOption::changed ()
414 {
415         if (_log) {
416                 _set (pow (10, _adj.get_value () * _mult));
417         } else {
418                 _set (_adj.get_value () * _mult);
419         }
420 }
421
422 void
423 HSliderOption::add_to_page (OptionEditorPage* p)
424 {
425         add_widgets_to_page (p, &_label, &_hscale);
426 }
427
428 void
429 HSliderOption::set_sensitive (bool yn)
430 {
431         _hscale.set_sensitive (yn);
432 }
433
434 /*--------------------------*/
435
436 ComboStringOption::ComboStringOption (
437                 std::string const & i,
438                 std::string const & n,
439                 sigc::slot<std::string> g,
440                 sigc::slot<bool, std::string> s
441                 )
442         : Option (i, n)
443         , _get (g)
444         , _set (s)
445 {
446         _label = Gtk::manage (new Gtk::Label (n + ":"));
447         _label->set_alignment (0, 0.5);
448         _combo = Gtk::manage (new Gtk::ComboBoxText);
449         _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboStringOption::changed));
450 }
451
452 void
453 ComboStringOption::set_state_from_config () {
454         _combo->set_active_text (_get());
455 }
456
457 void
458 ComboStringOption::add_to_page (OptionEditorPage* p)
459 {
460         add_widgets_to_page (p, _label, _combo);
461 }
462
463 /** Set the allowed strings for this option
464  *  @param strings a vector of allowed strings
465  */
466 void
467 ComboStringOption::set_popdown_strings (const std::vector<std::string>& strings) {
468         _combo->clear_items ();
469         for (std::vector<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i) {
470                 _combo->append_text (*i);
471         }
472 }
473
474 void
475 ComboStringOption::clear () {
476         _combo->clear_items();
477 }
478
479 void
480 ComboStringOption::changed () {
481         _set (_combo->get_active_text ());
482 }
483
484 void
485 ComboStringOption::set_sensitive (bool yn) {
486         _combo->set_sensitive (yn);
487 }
488
489 /*--------------------------*/
490
491 /** Construct a BoolComboOption.
492  *  @param i id
493  *  @param n User-visible name.
494  *  @param t Text to give for the variable being true.
495  *  @param f Text to give for the variable being false.
496  *  @param g Slot to get the variable's value.
497  *  @param s Slot to set the variable's value.
498  */
499 BoolComboOption::BoolComboOption (
500         string const & i, string const & n, string const & t, string const & f,
501         sigc::slot<bool> g, sigc::slot<bool, bool> s
502         )
503         : Option (i, n)
504         , _get (g)
505         , _set (s)
506 {
507         _label = manage (new Label (n + ":"));
508         _label->set_alignment (0, 0.5);
509         _combo = manage (new ComboBoxText);
510
511         /* option 0 is the false option */
512         _combo->append_text (f);
513         /* and option 1 is the true */
514         _combo->append_text (t);
515
516         _combo->signal_changed().connect (sigc::mem_fun (*this, &BoolComboOption::changed));
517 }
518
519 void
520 BoolComboOption::set_state_from_config ()
521 {
522         _combo->set_active (_get() ? 1 : 0);
523 }
524
525 void
526 BoolComboOption::add_to_page (OptionEditorPage* p)
527 {
528         add_widgets_to_page (p, _label, _combo);
529 }
530
531 void
532 BoolComboOption::changed ()
533 {
534         _set (_combo->get_active_row_number () == 0 ? false : true);
535 }
536
537 void
538 BoolComboOption::set_sensitive (bool yn)
539 {
540         _combo->set_sensitive (yn);
541 }
542
543 /*--------------------------*/
544
545 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
546         : Option (i, n)
547         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
548         , _get (g)
549         , _set (s)
550 {
551         _db_slider = manage (new ArdourWidgets::HSliderController (&_db_adjustment, boost::shared_ptr<PBD::Controllable>(), 220, 18));
552
553         _label.set_text (n + ":");
554         _label.set_alignment (0, 0.5);
555         _label.set_name (X_("OptionsLabel"));
556
557         _fader_centering_box.pack_start (*_db_slider, true, false);
558
559         _box.set_spacing (4);
560         _box.set_homogeneous (false);
561         _box.pack_start (_fader_centering_box, false, false);
562         _box.pack_start (_db_display, false, false);
563         _box.pack_start (*manage (new Label ("dB")), false, false);
564         _box.show_all ();
565
566         set_size_request_to_display_given_text (_db_display, "-99.00", 12, 0);
567
568         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
569         _db_display.signal_activate().connect (sigc::mem_fun (*this, &FaderOption::on_activate));
570         _db_display.signal_key_press_event().connect (sigc::mem_fun (*this, &FaderOption::on_key_press), false);
571 }
572
573 void
574 FaderOption::set_state_from_config ()
575 {
576         gain_t const val = _get ();
577         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
578
579         char buf[16];
580
581         if (val == 0.0) {
582                 snprintf (buf, sizeof (buf), "-inf");
583         } else {
584                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
585         }
586
587         _db_display.set_text (buf);
588 }
589
590 void
591 FaderOption::db_changed ()
592 {
593         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
594 }
595
596 void
597 FaderOption::on_activate ()
598 {
599         float db_val = atof (_db_display.get_text ().c_str ());
600         gain_t coeff_val = dB_to_coefficient (db_val);
601
602         _db_adjustment.set_value (gain_to_slider_position_with_max (coeff_val, Config->get_max_gain ()));
603 }
604
605 bool
606 FaderOption::on_key_press (GdkEventKey* ev)
607 {
608         if (ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (ev->keyval)) {
609                 /* drop through to normal handling */
610                 return false;
611         }
612         /* illegal key for gain entry */
613         return true;
614 }
615
616 void
617 FaderOption::add_to_page (OptionEditorPage* p)
618 {
619         add_widgets_to_page (p, &_label, &_box);
620 }
621
622 /*--------------------------*/
623
624 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<std::string> g, sigc::slot<bool, std::string> s)
625         : Option (i, n)
626         , _clock (X_("timecode-offset"), true, X_(""), true, false, true, false)
627         , _get (g)
628         , _set (s)
629 {
630         _label.set_text (n + ":");
631         _label.set_alignment (0, 0.5);
632         _label.set_name (X_("OptionsLabel"));
633         _clock.ValueChanged.connect (sigc::mem_fun (*this, &ClockOption::save_clock_time));
634 }
635
636 void
637 ClockOption::set_state_from_config ()
638 {
639         Timecode::Time TC;
640         samplepos_t when;
641         if (!Timecode::parse_timecode_format(_get(), TC)) {
642                 _clock.set (0, true);
643         }
644         TC.rate = _session->samples_per_timecode_frame();
645         TC.drop = _session->timecode_drop_frames();
646         _session->timecode_to_sample(TC, when, false, false);
647         if (TC.negative) { when=-when; }
648         _clock.set (when, true);
649 }
650
651 void
652 ClockOption::save_clock_time ()
653 {
654         Timecode::Time TC;
655         _session->sample_to_timecode(_clock.current_time(), TC, false, false);
656         _set (Timecode::timecode_format_time(TC));
657 }
658
659 void
660 ClockOption::add_to_page (OptionEditorPage* p)
661 {
662         add_widgets_to_page (p, &_label, &_clock);
663 }
664
665 void
666 ClockOption::set_session (Session* s)
667 {
668         _session = s;
669         _clock.set_session (s);
670 }
671
672 /*--------------------------*/
673
674 WidgetOption::WidgetOption (string const & i, string const & n, Gtk::Widget& w)
675         : Option (i, n)
676         , _widget (&w)
677 {
678 }
679
680 void
681 WidgetOption::add_to_page (OptionEditorPage* p)
682 {
683         add_widget_to_page (p, _widget);
684 }
685
686 /*--------------------------*/
687
688 OptionEditorPage::OptionEditorPage ()
689         : table (1, 3)
690 {
691         init ();
692 }
693
694 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
695         : table (1, 3)
696 {
697         init ();
698         box.pack_start (table, false, false);
699         box.set_border_width (4);
700         n.append_page (box, t);
701 }
702
703 void
704 OptionEditorPage::init ()
705 {
706         table.set_spacings (4);
707         table.set_col_spacing (0, 32);
708 }
709
710 /*--------------------------*/
711
712 void
713 OptionEditorMiniPage::add_to_page (OptionEditorPage* p)
714 {
715         int const n = p->table.property_n_rows();
716         int m = n + 1;
717         if (!_note.empty ()) {
718                 ++m;
719         }
720         p->table.resize (m, 3);
721         p->table.attach (box, 0, 3, n, n + 1, FILL | EXPAND, SHRINK, 0, 0);
722         maybe_add_note (p, n + 1);
723 }
724
725 /*--------------------------*/
726
727 /** Construct an OptionEditor.
728  *  @param o Configuration to edit.
729  *  @param t Title for the dialog.
730  */
731 OptionEditor::OptionEditor (PBD::Configuration* c)
732         : _config (c)
733         , option_tree (TreeStore::create (option_columns))
734         , option_treeview (option_tree)
735 {
736         using namespace Notebook_Helpers;
737
738         _notebook.set_show_tabs (false);
739         _notebook.set_show_border (true);
740         _notebook.set_name ("OptionsNotebook");
741
742         option_treeview.append_column ("", option_columns.name);
743         option_treeview.set_enable_search(true);
744         option_treeview.set_search_column(0);
745         option_treeview.set_name ("OptionsTreeView");
746         option_treeview.set_headers_visible (false);
747
748         option_treeview.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
749         option_treeview.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &OptionEditor::treeview_row_selected));
750
751         /* Watch out for changes to parameters */
752         _config->ParameterChanged.connect (config_connection, invalidator (*this), boost::bind (&OptionEditor::parameter_changed, this, _1), gui_context());
753 }
754
755 OptionEditor::~OptionEditor ()
756 {
757         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
758                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
759                         delete *j;
760                 }
761                 delete i->second;
762         }
763 }
764
765 /** Called when a configuration parameter has been changed.
766  *  @param p Parameter name.
767  */
768 void
769 OptionEditor::parameter_changed (std::string const & p)
770 {
771         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
772
773         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
774                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
775                         (*j)->parameter_changed (p);
776                 }
777         }
778 }
779
780 void
781 OptionEditor::treeview_row_selected ()
782 {
783         Glib::RefPtr<Gtk::TreeSelection> selection = option_treeview.get_selection();
784         TreeModel::iterator iter = selection->get_selected();
785
786         if (iter) {
787                 TreeModel::Row row = *iter;
788                 Gtk::Widget* w = row[option_columns.widget];
789                 if (w) {
790                         _notebook.set_current_page (_notebook.page_num (*w));
791                 }
792         }
793 }
794
795 TreeModel::iterator
796 OptionEditor::find_path_in_treemodel (std::string const & pn, bool create_missing)
797 {
798         /* split page name, which is actually a path, into each component */
799
800         std::vector<std::string> components;
801         split (pn, components, '/');
802
803         /* start with top level children */
804
805         TreeModel::Children children = option_tree->children();
806         TreeModel::iterator iter;
807
808         /* foreach path component ... */
809
810         for (std::vector<std::string>::const_iterator s = components.begin(); s != components.end(); ++s) {
811
812                 for (iter = children.begin(); iter != children.end(); ++iter) {
813                         TreeModel::Row row = *iter;
814                         const std::string row_name = row[option_columns.name];
815                         if (row_name == (*s)) {
816                                 break;
817                         }
818                 }
819
820                 if (iter == children.end()) {
821                         /* the current component is missing; bail out or create it */
822                         if (!create_missing) {
823                                 return option_tree->get_iter(TreeModel::Path(""));
824                         } else {
825                                 iter = option_tree->append (children);
826                                 TreeModel::Row row = *iter;
827                                 row[option_columns.name] = *s;
828                                 row[option_columns.widget] = 0;
829                         }
830                 }
831
832                 /* from now on, iter points to a valid row, either the one we found or a new one */
833                 /* set children to the row's children to continue searching */
834                 children = (*iter)->children ();
835
836         }
837
838         return iter;
839 }
840
841 void
842 OptionEditor::add_path_to_treeview (std::string const & pn, Gtk::Widget& widget)
843 {
844         option_treeview.set_model (Glib::RefPtr<TreeStore>());
845
846         TreeModel::iterator row_iter = find_path_in_treemodel(pn, true);
847
848         assert(row_iter);
849
850         TreeModel::Row row = *row_iter;
851         row[option_columns.widget] = &widget;
852
853         option_treeview.set_model (option_tree);
854         option_treeview.expand_all ();
855 }
856
857 /** Add a component to a given page.
858  *  @param pn Page name (will be created if it doesn't already exist)
859  *  @param o Component.
860  */
861 void
862 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
863 {
864         if (_pages.find (pn) == _pages.end()) {
865                 OptionEditorPage* oep = new OptionEditorPage (_notebook, pn);
866                 _pages[pn] = oep;
867
868                 add_path_to_treeview (pn, oep->box);
869         }
870
871         OptionEditorPage* p = _pages[pn];
872         p->components.push_back (o);
873
874         o->add_to_page (p);
875         o->set_state_from_config ();
876 }
877
878 /** Add a new page
879  *  @param pn Page name (will be created if it doesn't already exist)
880  *  @param w widget that fills the page
881  */
882 void
883 OptionEditor::add_page (std::string const & pn, Gtk::Widget& w)
884 {
885         if (_pages.find (pn) == _pages.end()) {
886                 OptionEditorPage* oep = new OptionEditorPage (_notebook, pn);
887                 _pages[pn] = oep;
888                 add_path_to_treeview (pn, oep->box);
889         }
890
891         OptionEditorPage* p = _pages[pn];
892         p->box.pack_start (w, true, true);
893 }
894
895 void
896 OptionEditor::set_current_page (string const & p)
897 {
898         TreeModel::iterator row_iter = find_path_in_treemodel(p);
899
900         if (row_iter) {
901                 option_treeview.get_selection()->select(row_iter);
902         }
903
904 }
905
906 /*--------------------------*/
907
908 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
909         : Option (i, n)
910         , _get (g)
911         , _set (s)
912 {
913         Gtkmm2ext::add_volume_shortcuts (_file_chooser);
914         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
915         _file_chooser.signal_selection_changed().connect (sigc::mem_fun (*this, &DirectoryOption::selection_changed));
916 }
917
918 void
919 DirectoryOption::set_state_from_config ()
920 {
921         _file_chooser.set_current_folder (poor_mans_glob(_get ()));
922 }
923
924 void
925 DirectoryOption::add_to_page (OptionEditorPage* p)
926 {
927         Gtk::Label *label = manage (new Label (_name));
928         label->set_alignment (0, 0.5);
929         label->set_name (X_("OptionsLabel"));
930         add_widgets_to_page (p, label, &_file_chooser);
931 }
932
933 void
934 DirectoryOption::selection_changed ()
935 {
936         _set (poor_mans_glob(_file_chooser.get_filename ()));
937 }
938
939 /*--------------------------*/
940
941 OptionEditorContainer::OptionEditorContainer (PBD::Configuration* c, string const& str)
942         : OptionEditor (c)
943 {
944         set_border_width (4);
945         Frame* f = manage (new Frame ());
946         f->add (treeview());
947         f->set_shadow_type (Gtk::SHADOW_OUT);
948         f->set_border_width (0);
949         hpacker.pack_start (*f, false, false, 4);
950         hpacker.pack_start (notebook(), false, false);
951         pack_start (hpacker, true, true);
952
953         show_all ();
954 }
955
956 OptionEditorWindow::OptionEditorWindow (PBD::Configuration* c, string const& str)
957         : OptionEditor (c)
958         , ArdourWindow (str)
959 {
960         container.set_border_width (4);
961         Frame* f = manage (new Frame ());
962         f->add (treeview());
963         f->set_shadow_type (Gtk::SHADOW_OUT);
964         f->set_border_width (0);
965         hpacker.pack_start (*f, false, false);
966         hpacker.pack_start (notebook(), true, true, 4);
967
968         container.pack_start (hpacker, true, true);
969
970         hpacker.show_all ();
971         container.show ();
972
973         add (container);
974 }