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