add Action-Button for rc-config dialog
[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
141 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
142         : Option (i, n),
143           _get (g),
144           _set (s)
145 {
146         _button = manage (new CheckButton);
147         _label = manage (new Label);
148         _label->set_markup (n);
149         _button->add (*_label);
150         _button->set_active (_get ());
151         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
152 }
153
154 void
155 BoolOption::add_to_page (OptionEditorPage* p)
156 {
157         add_widget_to_page (p, _button);
158 }
159
160 void
161 BoolOption::set_state_from_config ()
162 {
163         _button->set_active (_get ());
164 }
165
166 void
167 BoolOption::toggled ()
168 {
169         if (!_set (_button->get_active ())) {
170                 _button->set_active (_get ());
171         }
172 }
173
174 RouteDisplayBoolOption::RouteDisplayBoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
175         : BoolOption (i, n, g, s)
176 {
177 }
178
179 void
180 RouteDisplayBoolOption::toggled ()
181 {
182         DisplaySuspender ds;
183         BoolOption::toggled ();
184 }
185
186 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
187         : Option (i, n),
188           _get (g),
189           _set (s)
190 {
191         _label = manage (left_aligned_label (n + ":"));
192         _entry = manage (new Entry);
193         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
194         _entry->signal_focus_out_event().connect (sigc::mem_fun (*this, &EntryOption::focus_out));
195         _entry->signal_insert_text().connect (sigc::mem_fun (*this, &EntryOption::filter_text));
196 }
197
198 void
199 EntryOption::add_to_page (OptionEditorPage* p)
200 {
201         add_widgets_to_page (p, _label, _entry);
202 }
203
204 void
205 EntryOption::set_state_from_config ()
206 {
207         _entry->set_text (_get ());
208 }
209
210 void
211 EntryOption::set_sensitive (bool s)
212 {
213         _entry->set_sensitive (s);
214 }
215
216 void
217 EntryOption::filter_text (const Glib::ustring&, int*)
218 {
219         std::string text = _entry->get_text ();
220         for (size_t i = 0; i < _invalid.length(); ++i) {
221                 text.erase (std::remove(text.begin(), text.end(), _invalid.at(i)), text.end());
222         }
223         if (text != _entry->get_text ()) {
224                 _entry->set_text (text);
225         }
226 }
227
228 void
229 EntryOption::activated ()
230 {
231         _set (_entry->get_text ());
232 }
233
234 bool
235 EntryOption::focus_out (GdkEventFocus*)
236 {
237         _set (_entry->get_text ());
238         return true;
239 }
240
241 /** Construct a BoolComboOption.
242  *  @param i id
243  *  @param n User-visible name.
244  *  @param t Text to give for the variable being true.
245  *  @param f Text to give for the variable being false.
246  *  @param g Slot to get the variable's value.
247  *  @param s Slot to set the variable's value.
248  */
249 BoolComboOption::BoolComboOption (
250         string const & i, string const & n, string const & t, string const & f,
251         sigc::slot<bool> g, sigc::slot<bool, bool> s
252         )
253         : Option (i, n)
254         , _get (g)
255         , _set (s)
256 {
257         _label = manage (new Label (n + ":"));
258         _label->set_alignment (0, 0.5);
259         _combo = manage (new ComboBoxText);
260
261         /* option 0 is the false option */
262         _combo->append_text (f);
263         /* and option 1 is the true */
264         _combo->append_text (t);
265
266         _combo->signal_changed().connect (sigc::mem_fun (*this, &BoolComboOption::changed));
267 }
268
269 void
270 BoolComboOption::set_state_from_config ()
271 {
272         _combo->set_active (_get() ? 1 : 0);
273 }
274
275 void
276 BoolComboOption::add_to_page (OptionEditorPage* p)
277 {
278         add_widgets_to_page (p, _label, _combo);
279 }
280
281 void
282 BoolComboOption::changed ()
283 {
284         _set (_combo->get_active_row_number () == 0 ? false : true);
285 }
286
287 void
288 BoolComboOption::set_sensitive (bool yn)
289 {
290         _combo->set_sensitive (yn);
291 }
292
293
294
295 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
296         : Option (i, n)
297         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
298         , _get (g)
299         , _set (s)
300 {
301         _db_slider = manage (new HSliderController (&_db_adjustment, boost::shared_ptr<PBD::Controllable>(), 115, 18));
302
303         _label.set_text (n + ":");
304         _label.set_alignment (0, 0.5);
305         _label.set_name (X_("OptionsLabel"));
306
307         _fader_centering_box.pack_start (*_db_slider, true, false);
308
309         _box.set_spacing (4);
310         _box.set_homogeneous (false);
311         _box.pack_start (_fader_centering_box, false, false);
312         _box.pack_start (_db_display, false, false);
313         _box.show_all ();
314
315         set_size_request_to_display_given_text (_db_display, "-99.00", 12, 12);
316
317         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
318         _db_display.signal_activate().connect (sigc::mem_fun (*this, &FaderOption::on_activate));
319         _db_display.signal_key_press_event().connect (sigc::mem_fun (*this, &FaderOption::on_key_press), false);
320 }
321
322 void
323 FaderOption::set_state_from_config ()
324 {
325         gain_t const val = _get ();
326         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
327
328         char buf[16];
329
330         if (val == 0.0) {
331                 snprintf (buf, sizeof (buf), "-inf");
332         } else {
333                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
334         }
335
336         _db_display.set_text (buf);
337 }
338
339 void
340 FaderOption::db_changed ()
341 {
342         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
343 }
344
345 void
346 FaderOption::on_activate ()
347 {
348         float db_val = atof (_db_display.get_text ().c_str ());
349         gain_t coeff_val = dB_to_coefficient (db_val);
350
351         _db_adjustment.set_value (gain_to_slider_position_with_max (coeff_val, Config->get_max_gain ()));
352 }
353
354 bool
355 FaderOption::on_key_press (GdkEventKey* ev)
356 {
357         if (ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (ev->keyval)) {
358                 /* drop through to normal handling */
359                 return false;
360         }
361         /* illegal key for gain entry */
362         return true;
363 }
364
365 void
366 FaderOption::add_to_page (OptionEditorPage* p)
367 {
368         add_widgets_to_page (p, &_label, &_box);
369 }
370
371 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<std::string> g, sigc::slot<bool, std::string> s)
372         : Option (i, n)
373         , _clock (X_("timecode-offset"), true, X_(""), true, false, true, false)
374         , _get (g)
375         , _set (s)
376 {
377         _label.set_text (n + ":");
378         _label.set_alignment (0, 0.5);
379         _label.set_name (X_("OptionsLabel"));
380         _clock.ValueChanged.connect (sigc::mem_fun (*this, &ClockOption::save_clock_time));
381 }
382
383 void
384 ClockOption::set_state_from_config ()
385 {
386         Timecode::Time TC;
387         framepos_t when;
388         if (!Timecode::parse_timecode_format(_get(), TC)) {
389                 _clock.set (0, true);
390         }
391         TC.rate = _session->frames_per_timecode_frame();
392         TC.drop = _session->timecode_drop_frames();
393         _session->timecode_to_sample(TC, when, false, false);
394         if (TC.negative) { when=-when; }
395         _clock.set (when, true);
396 }
397
398 void
399 ClockOption::save_clock_time ()
400 {
401         Timecode::Time TC;
402         _session->sample_to_timecode(_clock.current_time(), TC, false, false);
403         _set (Timecode::timecode_format_time(TC));
404 }
405
406 void
407 ClockOption::add_to_page (OptionEditorPage* p)
408 {
409         add_widgets_to_page (p, &_label, &_clock);
410 }
411
412 void
413 ClockOption::set_session (Session* s)
414 {
415         _session = s;
416         _clock.set_session (s);
417 }
418
419 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
420         : table (1, 3)
421 {
422         table.set_spacings (4);
423         table.set_col_spacing (0, 32);
424         box.pack_start (table, false, false);
425         box.set_border_width (4);
426         n.append_page (box, t);
427 }
428
429 /** Construct an OptionEditor.
430  *  @param o Configuration to edit.
431  *  @param t Title for the dialog.
432  */
433 OptionEditor::OptionEditor (PBD::Configuration* c, std::string const & t)
434         : ArdourWindow (t), _config (c)
435 {
436         using namespace Notebook_Helpers;
437
438         set_default_size (300, 300);
439         // set_wmclass (X_("ardour_preferences"), PROGRAM_NAME);
440
441         set_name ("Preferences");
442         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
443
444         set_border_width (4);
445
446         add (_notebook);
447
448         _notebook.set_show_tabs (true);
449         _notebook.set_show_border (true);
450         _notebook.set_name ("OptionsNotebook");
451
452         show_all_children();
453
454         /* Watch out for changes to parameters */
455         _config->ParameterChanged.connect (config_connection, invalidator (*this), boost::bind (&OptionEditor::parameter_changed, this, _1), gui_context());
456 }
457
458 OptionEditor::~OptionEditor ()
459 {
460         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
461                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
462                         delete *j;
463                 }
464                 delete i->second;
465         }
466 }
467
468 /** Called when a configuration parameter has been changed.
469  *  @param p Parameter name.
470  */
471 void
472 OptionEditor::parameter_changed (std::string const & p)
473 {
474         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
475
476         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
477                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
478                         (*j)->parameter_changed (p);
479                 }
480         }
481 }
482
483 /** Add a component to a given page.
484  *  @param pn Page name (will be created if it doesn't already exist)
485  *  @param o Component.
486  */
487 void
488 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
489 {
490         if (_pages.find (pn) == _pages.end()) {
491                 _pages[pn] = new OptionEditorPage (_notebook, pn);
492         }
493
494         OptionEditorPage* p = _pages[pn];
495         p->components.push_back (o);
496
497         o->add_to_page (p);
498         o->set_state_from_config ();
499 }
500
501 /** Add a new page
502  *  @param pn Page name (will be created if it doesn't already exist)
503  *  @param w widget that fills the page
504  */
505 void
506 OptionEditor::add_page (std::string const & pn, Gtk::Widget& w)
507 {
508         if (_pages.find (pn) == _pages.end()) {
509                 _pages[pn] = new OptionEditorPage (_notebook, pn);
510         }
511
512         OptionEditorPage* p = _pages[pn];
513         p->box.pack_start (w, true, true);
514 }
515
516 void
517 OptionEditor::set_current_page (string const & p)
518 {
519         int i = 0;
520         while (i < _notebook.get_n_pages ()) {
521                 if (_notebook.get_tab_label_text (*_notebook.get_nth_page (i)) == p) {
522                         _notebook.set_current_page (i);
523                         return;
524                 }
525
526                 ++i;
527         }
528 }
529
530
531 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
532         : Option (i, n)
533         , _get (g)
534         , _set (s)
535 {
536         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
537         _file_chooser.signal_selection_changed().connect (sigc::mem_fun (*this, &DirectoryOption::selection_changed));
538 }
539
540
541 void
542 DirectoryOption::set_state_from_config ()
543 {
544         _file_chooser.set_current_folder (poor_mans_glob(_get ()));
545 }
546
547 void
548 DirectoryOption::add_to_page (OptionEditorPage* p)
549 {
550         Gtk::Label *label = manage (new Label (_name));
551         label->set_alignment (0, 0.5);
552         label->set_name (X_("OptionsLabel"));
553         add_widgets_to_page (p, label, &_file_chooser);
554 }
555
556 void
557 DirectoryOption::selection_changed ()
558 {
559         _set (poor_mans_glob(_file_chooser.get_filename ()));
560 }