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