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