add rc-config widget to display text-config/paths (read only)
[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
34 #include "gui_thread.h"
35 #include "option_editor.h"
36 #include "public_editor.h"
37 #include "utils.h"
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace Gtk;
42 using namespace Gtkmm2ext;
43 using namespace ARDOUR;
44
45 void
46 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
47 {
48         int const n = p->table.property_n_rows();
49         int m = n + 1;
50         if (!_note.empty ()) {
51                 ++m;
52         }
53
54         p->table.resize (m, 3);
55         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
56
57         maybe_add_note (p, n + 1);
58 }
59
60 void
61 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
62 {
63         int const n = p->table.property_n_rows();
64         int m = n + 1;
65         if (!_note.empty ()) {
66                 ++m;
67         }
68
69         p->table.resize (m, 3);
70         p->table.attach (*wa, 1, 2, n, n + 1, FILL);
71         p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
72
73         maybe_add_note (p, n + 1);
74 }
75
76 void
77 OptionEditorComponent::maybe_add_note (OptionEditorPage* p, int n)
78 {
79         if (!_note.empty ()) {
80                 Gtk::Label* l = manage (new Gtk::Label (string_compose (X_("<i>%1</i>"), _note)));
81                 l->set_use_markup (true);
82                 p->table.attach (*l, 1, 3, n, n + 1, FILL | EXPAND);
83         }
84 }
85
86 void
87 OptionEditorComponent::set_note (string const & n)
88 {
89         _note = n;
90 }
91
92 OptionEditorHeading::OptionEditorHeading (string const & h)
93 {
94         std::stringstream s;
95         s << "<b>" << h << "</b>";
96         _label = manage (left_aligned_label (s.str()));
97         _label->set_use_markup (true);
98 }
99
100 void
101 OptionEditorHeading::add_to_page (OptionEditorPage* p)
102 {
103         int const n = p->table.property_n_rows();
104         p->table.resize (n + 2, 3);
105
106         p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
107         p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
108 }
109
110 void
111 OptionEditorBox::add_to_page (OptionEditorPage* p)
112 {
113         add_widget_to_page (p, _box);
114 }
115
116 RcActionButton::RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l)
117         : _label (NULL)
118 {
119         _button = manage (new Button (t));
120         _button->signal_clicked().connect (slot);
121         if (!l.empty ()) {
122                 _label = manage (right_aligned_label (l));
123         }
124 }
125
126 void
127 RcActionButton::add_to_page (OptionEditorPage *p)
128 {
129         int const n = p->table.property_n_rows();
130         int m = n + 1;
131         p->table.resize (m, 3);
132         if (_label) {
133                 p->table.attach (*_label,  1, 2, n, n + 1, FILL | EXPAND);
134                 p->table.attach (*_button, 2, 3, n, n + 1, FILL | EXPAND);
135         } else {
136                 p->table.attach (*_button, 1, 3, n, n + 1, FILL | EXPAND);
137         }
138 }
139
140 RcConfigDisplay::RcConfigDisplay (string const & i, string const & n, sigc::slot<string> g, char s)
141         : _get (g)
142         , _id (i)
143         , _sep (s)
144 {
145         _label = manage (right_aligned_label (n));
146         _info = manage (new Label);
147         _info-> set_line_wrap (true);
148         set_state_from_config ();
149 }
150
151 void
152 RcConfigDisplay::set_state_from_config ()
153 {
154         string p = _get();
155         if (_sep) {
156                 std::replace (p.begin(), p.end(), _sep, '\n');
157         }
158         _info->set_text (p);
159 }
160
161 void
162 RcConfigDisplay::parameter_changed (std::string const & p)
163 {
164         if (p == _id) {
165                 set_state_from_config ();
166         }
167 }
168
169 void
170 RcConfigDisplay::add_to_page (OptionEditorPage *p)
171 {
172         int const n = p->table.property_n_rows();
173         int m = n + 1;
174         p->table.resize (m, 3);
175         p->table.attach (*_label, 1, 2, n, n + 1, FILL | EXPAND);
176         p->table.attach (*_info,  2, 3, n, n + 1, FILL | EXPAND);
177 }
178
179
180 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
181         : Option (i, n),
182           _get (g),
183           _set (s)
184 {
185         _button = manage (new CheckButton);
186         _label = manage (new Label);
187         _label->set_markup (n);
188         _button->add (*_label);
189         _button->set_active (_get ());
190         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
191 }
192
193 void
194 BoolOption::add_to_page (OptionEditorPage* p)
195 {
196         add_widget_to_page (p, _button);
197 }
198
199 void
200 BoolOption::set_state_from_config ()
201 {
202         _button->set_active (_get ());
203 }
204
205 void
206 BoolOption::toggled ()
207 {
208         if (!_set (_button->get_active ())) {
209                 _button->set_active (_get ());
210         }
211 }
212
213 RouteDisplayBoolOption::RouteDisplayBoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
214         : BoolOption (i, n, g, s)
215 {
216 }
217
218 void
219 RouteDisplayBoolOption::toggled ()
220 {
221         DisplaySuspender ds;
222         BoolOption::toggled ();
223 }
224
225 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
226         : Option (i, n),
227           _get (g),
228           _set (s)
229 {
230         _label = manage (left_aligned_label (n + ":"));
231         _entry = manage (new Entry);
232         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
233         _entry->signal_focus_out_event().connect (sigc::mem_fun (*this, &EntryOption::focus_out));
234         _entry->signal_insert_text().connect (sigc::mem_fun (*this, &EntryOption::filter_text));
235 }
236
237 void
238 EntryOption::add_to_page (OptionEditorPage* p)
239 {
240         add_widgets_to_page (p, _label, _entry);
241 }
242
243 void
244 EntryOption::set_state_from_config ()
245 {
246         _entry->set_text (_get ());
247 }
248
249 void
250 EntryOption::set_sensitive (bool s)
251 {
252         _entry->set_sensitive (s);
253 }
254
255 void
256 EntryOption::filter_text (const Glib::ustring&, int*)
257 {
258         std::string text = _entry->get_text ();
259         for (size_t i = 0; i < _invalid.length(); ++i) {
260                 text.erase (std::remove(text.begin(), text.end(), _invalid.at(i)), text.end());
261         }
262         if (text != _entry->get_text ()) {
263                 _entry->set_text (text);
264         }
265 }
266
267 void
268 EntryOption::activated ()
269 {
270         _set (_entry->get_text ());
271 }
272
273 bool
274 EntryOption::focus_out (GdkEventFocus*)
275 {
276         _set (_entry->get_text ());
277         return true;
278 }
279
280 /** Construct a BoolComboOption.
281  *  @param i id
282  *  @param n User-visible name.
283  *  @param t Text to give for the variable being true.
284  *  @param f Text to give for the variable being false.
285  *  @param g Slot to get the variable's value.
286  *  @param s Slot to set the variable's value.
287  */
288 BoolComboOption::BoolComboOption (
289         string const & i, string const & n, string const & t, string const & f,
290         sigc::slot<bool> g, sigc::slot<bool, bool> s
291         )
292         : Option (i, n)
293         , _get (g)
294         , _set (s)
295 {
296         _label = manage (new Label (n + ":"));
297         _label->set_alignment (0, 0.5);
298         _combo = manage (new ComboBoxText);
299
300         /* option 0 is the false option */
301         _combo->append_text (f);
302         /* and option 1 is the true */
303         _combo->append_text (t);
304
305         _combo->signal_changed().connect (sigc::mem_fun (*this, &BoolComboOption::changed));
306 }
307
308 void
309 BoolComboOption::set_state_from_config ()
310 {
311         _combo->set_active (_get() ? 1 : 0);
312 }
313
314 void
315 BoolComboOption::add_to_page (OptionEditorPage* p)
316 {
317         add_widgets_to_page (p, _label, _combo);
318 }
319
320 void
321 BoolComboOption::changed ()
322 {
323         _set (_combo->get_active_row_number () == 0 ? false : true);
324 }
325
326 void
327 BoolComboOption::set_sensitive (bool yn)
328 {
329         _combo->set_sensitive (yn);
330 }
331
332
333
334 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
335         : Option (i, n)
336         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
337         , _get (g)
338         , _set (s)
339 {
340         _db_slider = manage (new HSliderController (&_db_adjustment, boost::shared_ptr<PBD::Controllable>(), 115, 18));
341
342         _label.set_text (n + ":");
343         _label.set_alignment (0, 0.5);
344         _label.set_name (X_("OptionsLabel"));
345
346         _fader_centering_box.pack_start (*_db_slider, true, false);
347
348         _box.set_spacing (4);
349         _box.set_homogeneous (false);
350         _box.pack_start (_fader_centering_box, false, false);
351         _box.pack_start (_db_display, false, false);
352         _box.show_all ();
353
354         set_size_request_to_display_given_text (_db_display, "-99.00", 12, 12);
355
356         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
357         _db_display.signal_activate().connect (sigc::mem_fun (*this, &FaderOption::on_activate));
358         _db_display.signal_key_press_event().connect (sigc::mem_fun (*this, &FaderOption::on_key_press), false);
359 }
360
361 void
362 FaderOption::set_state_from_config ()
363 {
364         gain_t const val = _get ();
365         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
366
367         char buf[16];
368
369         if (val == 0.0) {
370                 snprintf (buf, sizeof (buf), "-inf");
371         } else {
372                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
373         }
374
375         _db_display.set_text (buf);
376 }
377
378 void
379 FaderOption::db_changed ()
380 {
381         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
382 }
383
384 void
385 FaderOption::on_activate ()
386 {
387         float db_val = atof (_db_display.get_text ().c_str ());
388         gain_t coeff_val = dB_to_coefficient (db_val);
389
390         _db_adjustment.set_value (gain_to_slider_position_with_max (coeff_val, Config->get_max_gain ()));
391 }
392
393 bool
394 FaderOption::on_key_press (GdkEventKey* ev)
395 {
396         if (ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (ev->keyval)) {
397                 /* drop through to normal handling */
398                 return false;
399         }
400         /* illegal key for gain entry */
401         return true;
402 }
403
404 void
405 FaderOption::add_to_page (OptionEditorPage* p)
406 {
407         add_widgets_to_page (p, &_label, &_box);
408 }
409
410 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<std::string> g, sigc::slot<bool, std::string> s)
411         : Option (i, n)
412         , _clock (X_("timecode-offset"), true, X_(""), true, false, true, false)
413         , _get (g)
414         , _set (s)
415 {
416         _label.set_text (n + ":");
417         _label.set_alignment (0, 0.5);
418         _label.set_name (X_("OptionsLabel"));
419         _clock.ValueChanged.connect (sigc::mem_fun (*this, &ClockOption::save_clock_time));
420 }
421
422 void
423 ClockOption::set_state_from_config ()
424 {
425         Timecode::Time TC;
426         framepos_t when;
427         if (!Timecode::parse_timecode_format(_get(), TC)) {
428                 _clock.set (0, true);
429         }
430         TC.rate = _session->frames_per_timecode_frame();
431         TC.drop = _session->timecode_drop_frames();
432         _session->timecode_to_sample(TC, when, false, false);
433         if (TC.negative) { when=-when; }
434         _clock.set (when, true);
435 }
436
437 void
438 ClockOption::save_clock_time ()
439 {
440         Timecode::Time TC;
441         _session->sample_to_timecode(_clock.current_time(), TC, false, false);
442         _set (Timecode::timecode_format_time(TC));
443 }
444
445 void
446 ClockOption::add_to_page (OptionEditorPage* p)
447 {
448         add_widgets_to_page (p, &_label, &_clock);
449 }
450
451 void
452 ClockOption::set_session (Session* s)
453 {
454         _session = s;
455         _clock.set_session (s);
456 }
457
458 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
459         : table (1, 3)
460 {
461         table.set_spacings (4);
462         table.set_col_spacing (0, 32);
463         box.pack_start (table, false, false);
464         box.set_border_width (4);
465         n.append_page (box, t);
466 }
467
468 /** Construct an OptionEditor.
469  *  @param o Configuration to edit.
470  *  @param t Title for the dialog.
471  */
472 OptionEditor::OptionEditor (PBD::Configuration* c, std::string const & t)
473         : ArdourWindow (t), _config (c)
474 {
475         using namespace Notebook_Helpers;
476
477         set_default_size (300, 300);
478         // set_wmclass (X_("ardour_preferences"), PROGRAM_NAME);
479
480         set_name ("Preferences");
481         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
482
483         set_border_width (4);
484
485         add (_notebook);
486
487         _notebook.set_show_tabs (true);
488         _notebook.set_show_border (true);
489         _notebook.set_name ("OptionsNotebook");
490
491         show_all_children();
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 /** Add a component to a given page.
523  *  @param pn Page name (will be created if it doesn't already exist)
524  *  @param o Component.
525  */
526 void
527 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
528 {
529         if (_pages.find (pn) == _pages.end()) {
530                 _pages[pn] = new OptionEditorPage (_notebook, pn);
531         }
532
533         OptionEditorPage* p = _pages[pn];
534         p->components.push_back (o);
535
536         o->add_to_page (p);
537         o->set_state_from_config ();
538 }
539
540 /** Add a new page
541  *  @param pn Page name (will be created if it doesn't already exist)
542  *  @param w widget that fills the page
543  */
544 void
545 OptionEditor::add_page (std::string const & pn, Gtk::Widget& w)
546 {
547         if (_pages.find (pn) == _pages.end()) {
548                 _pages[pn] = new OptionEditorPage (_notebook, pn);
549         }
550
551         OptionEditorPage* p = _pages[pn];
552         p->box.pack_start (w, true, true);
553 }
554
555 void
556 OptionEditor::set_current_page (string const & p)
557 {
558         int i = 0;
559         while (i < _notebook.get_n_pages ()) {
560                 if (_notebook.get_tab_label_text (*_notebook.get_nth_page (i)) == p) {
561                         _notebook.set_current_page (i);
562                         return;
563                 }
564
565                 ++i;
566         }
567 }
568
569
570 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
571         : Option (i, n)
572         , _get (g)
573         , _set (s)
574 {
575         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
576         _file_chooser.signal_selection_changed().connect (sigc::mem_fun (*this, &DirectoryOption::selection_changed));
577 }
578
579
580 void
581 DirectoryOption::set_state_from_config ()
582 {
583         _file_chooser.set_current_folder (poor_mans_glob(_get ()));
584 }
585
586 void
587 DirectoryOption::add_to_page (OptionEditorPage* p)
588 {
589         Gtk::Label *label = manage (new Label (_name));
590         label->set_alignment (0, 0.5);
591         label->set_name (X_("OptionsLabel"));
592         add_widgets_to_page (p, label, &_file_chooser);
593 }
594
595 void
596 DirectoryOption::selection_changed ()
597 {
598         _set (poor_mans_glob(_file_chooser.get_filename ()));
599 }