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