all new implementation of audio clocks, with entirely new editing model. not entirely...
[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         p->table.resize (n + 1, 3);
44         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
45 }
46
47 void
48 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
49 {
50         int const n = p->table.property_n_rows();
51         p->table.resize (n + 1, 3);
52         p->table.attach (*wa, 1, 2, n, n + 1, FILL);
53         p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
54 }
55
56 OptionEditorHeading::OptionEditorHeading (string const & h)
57 {
58         std::stringstream s;
59         s << "<b>" << h << "</b>";
60         _label = manage (new Label (s.str()));
61         _label->set_alignment (0, 0.5);
62         _label->set_use_markup (true);
63 }
64
65 void
66 OptionEditorHeading::add_to_page (OptionEditorPage* p)
67 {
68         int const n = p->table.property_n_rows();
69         p->table.resize (n + 2, 3);
70
71         p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
72         p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
73 }
74
75 void
76 OptionEditorBox::add_to_page (OptionEditorPage* p)
77 {
78         add_widget_to_page (p, _box);
79 }
80
81 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
82         : Option (i, n),
83           _get (g),
84           _set (s)
85 {
86         _button = manage (new CheckButton (n));
87         _button->set_active (_get ());
88         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
89 }
90
91 void
92 BoolOption::add_to_page (OptionEditorPage* p)
93 {
94         add_widget_to_page (p, _button);
95 }
96
97 void
98 BoolOption::set_state_from_config ()
99 {
100         _button->set_active (_get ());
101 }
102
103 void
104 BoolOption::toggled ()
105 {
106         _set (_button->get_active ());
107 }
108
109 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
110         : Option (i, n),
111           _get (g),
112           _set (s)
113 {
114         _label = manage (new Label (n + ":"));
115         _label->set_alignment (0, 0.5);
116         _entry = manage (new Entry);
117         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
118 }
119
120 void
121 EntryOption::add_to_page (OptionEditorPage* p)
122 {
123         add_widgets_to_page (p, _label, _entry);
124 }
125
126 void
127 EntryOption::set_state_from_config ()
128 {
129         _entry->set_text (_get ());
130 }
131
132 void
133 EntryOption::activated ()
134 {
135         _set (_entry->get_text ());
136 }
137
138 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
139         : Option (i, n)
140         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
141         , _get (g)
142         , _set (s)
143 {
144         _pix = ::get_icon (X_("fader_belt_h"));
145         if (_pix == 0) {
146                 throw failed_constructor ();
147         }
148
149         _db_slider = manage (new HSliderController (_pix,
150                                                     &_db_adjustment,
151                                                     115,
152                                                     false));
153
154         _label.set_text (n + ":");
155         _label.set_name (X_("OptionsLabel"));
156
157         _box.set_spacing (4);
158         _box.pack_start (*_db_slider, false, false);
159         _box.pack_start (_db_display, false, false);
160         _box.show_all ();
161
162         set_size_request_to_display_given_text (_db_display, "-99.0", 12, 12);
163
164         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
165 }
166
167 void
168 FaderOption::set_state_from_config ()
169 {
170         gain_t const val = _get ();
171         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
172
173         char buf[16];
174
175         if (val == 0.0) {
176                 snprintf (buf, sizeof (buf), "-inf");
177         } else {
178                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
179         }
180
181         _db_display.set_text (buf);
182 }
183
184 void
185 FaderOption::db_changed ()
186 {
187         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
188 }
189
190 void
191 FaderOption::add_to_page (OptionEditorPage* p)
192 {
193         add_widgets_to_page (p, &_label, &_box);
194 }
195
196 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<framecnt_t> g, sigc::slot<bool, framecnt_t> s)
197         : Option (i, n)
198         , _clock (X_("timecode-offset"), false, X_(""), true, false, true, false)
199         , _get (g)
200         , _set (s)
201 {
202         _label.set_text (n + ":");
203         _label.set_alignment (0, 0.5);
204         _label.set_name (X_("OptionsLabel"));
205 }
206
207 void
208 ClockOption::set_state_from_config ()
209 {
210         _clock.set (_get ());
211 }
212
213 void
214 ClockOption::add_to_page (OptionEditorPage* p)
215 {
216         add_widgets_to_page (p, &_label, &_clock);
217 }
218
219 void
220 ClockOption::set_session (Session* s)
221 {
222         _clock.set_session (s);
223 }
224
225 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
226         : table (1, 3)
227 {
228         table.set_spacings (4);
229         table.set_col_spacing (0, 32);
230         box.pack_start (table, false, false);
231         box.set_border_width (4);
232         n.append_page (box, t);
233 }
234
235 /** Construct an OptionEditor.
236  *  @param o Configuration to edit.
237  *  @param t Title for the dialog.
238  */
239 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
240         : ArdourDialog (t, false), _config (c)
241 {
242         using namespace Notebook_Helpers;
243
244         set_default_size (300, 300);
245         set_wmclass (X_("ardour_preferences"), PROGRAM_NAME);
246
247         set_name ("Preferences");
248         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
249
250         set_border_width (4);
251
252         get_vbox()->set_spacing (4);
253         get_vbox()->pack_start (_notebook);
254
255         _notebook.set_show_tabs (true);
256         _notebook.set_show_border (true);
257         _notebook.set_name ("OptionsNotebook");
258
259         show_all_children();
260
261         /* Watch out for changes to parameters */
262         _config->ParameterChanged.connect (config_connection, invalidator (*this), ui_bind (&OptionEditor::parameter_changed, this, _1), gui_context());
263 }
264
265 OptionEditor::~OptionEditor ()
266 {
267         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
268                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
269                         delete *j;
270                 }
271                 delete i->second;
272         }
273 }
274
275 /** Called when a configuration parameter has been changed.
276  *  @param p Parameter name.
277  */
278 void
279 OptionEditor::parameter_changed (std::string const & p)
280 {
281         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
282
283         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
284                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
285                         (*j)->parameter_changed (p);
286                 }
287         }
288 }
289
290 /** Add a component to a given page.
291  *  @param pn Page name (will be created if it doesn't already exist)
292  *  @param o Component.
293  */
294 void
295 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
296 {
297         if (_pages.find (pn) == _pages.end()) {
298                 _pages[pn] = new OptionEditorPage (_notebook, pn);
299         }
300
301         OptionEditorPage* p = _pages[pn];
302         p->components.push_back (o);
303
304         o->add_to_page (p);
305         o->set_state_from_config ();
306 }
307
308 void
309 OptionEditor::set_current_page (string const & p)
310 {
311         int i = 0;
312         while (i < _notebook.get_n_pages ()) {
313                 if (_notebook.get_tab_label_text (*_notebook.get_nth_page (i)) == p) {
314                         _notebook.set_current_page (i);
315                         return;
316                 }
317
318                 ++i;
319         }
320 }
321
322
323 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
324         : Option (i, n)
325         , _get (g)
326         , _set (s)
327 {
328         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
329         _file_chooser.signal_file_set().connect (sigc::mem_fun (*this, &DirectoryOption::file_set));
330 }
331
332
333 void
334 DirectoryOption::set_state_from_config ()
335 {
336         _file_chooser.set_filename (_get ());
337 }
338
339 void
340 DirectoryOption::add_to_page (OptionEditorPage* p)
341 {
342         add_widgets_to_page (p, manage (new Label (_name)), &_file_chooser);
343 }
344
345 void
346 DirectoryOption::file_set ()
347 {
348         _set (_file_chooser.get_filename ());
349 }