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