remove the apparently unnecessary "ui_bind()" macro from entire source base
[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 (new Label (s.str()));
91         _label->set_alignment (0, 0.5);
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 (new Label (n + ":"));
145         _label->set_alignment (0, 0.5);
146         _entry = manage (new Entry);
147         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
148 }
149
150 void
151 EntryOption::add_to_page (OptionEditorPage* p)
152 {
153         add_widgets_to_page (p, _label, _entry);
154 }
155
156 void
157 EntryOption::set_state_from_config ()
158 {
159         _entry->set_text (_get ());
160 }
161
162 void
163 EntryOption::activated ()
164 {
165         _set (_entry->get_text ());
166 }
167
168 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
169         : Option (i, n)
170         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
171         , _get (g)
172         , _set (s)
173 {
174         _pix = ::get_icon (X_("fader_belt_h"));
175         if (_pix == 0) {
176                 throw failed_constructor ();
177         }
178
179         _db_slider = manage (new HSliderController (_pix,
180                                                     &_db_adjustment,
181                                                     115,
182                                                     false));
183
184         _label.set_text (n + ":");
185         _label.set_name (X_("OptionsLabel"));
186
187         _fader_centering_box.pack_start (*_db_slider, true, false);
188
189         _box.set_spacing (4);
190         _box.set_homogeneous (false);
191         _box.pack_start (_fader_centering_box, false, false);
192         _box.pack_start (_db_display, false, false);
193         _box.show_all ();
194
195         set_size_request_to_display_given_text (_db_display, "-99.00", 12, 12);
196
197         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
198 }
199
200 void
201 FaderOption::set_state_from_config ()
202 {
203         gain_t const val = _get ();
204         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
205
206         char buf[16];
207
208         if (val == 0.0) {
209                 snprintf (buf, sizeof (buf), "-inf");
210         } else {
211                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
212         }
213
214         _db_display.set_text (buf);
215 }
216
217 void
218 FaderOption::db_changed ()
219 {
220         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
221 }
222
223 void
224 FaderOption::add_to_page (OptionEditorPage* p)
225 {
226         add_widgets_to_page (p, &_label, &_box);
227 }
228
229 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<framecnt_t> g, sigc::slot<bool, framecnt_t> s)
230         : Option (i, n)
231         , _clock (X_("timecode-offset"), false, X_(""), true, false, true, false)
232         , _get (g)
233         , _set (s)
234 {
235         _label.set_text (n + ":");
236         _label.set_alignment (0, 0.5);
237         _label.set_name (X_("OptionsLabel"));
238 }
239
240 void
241 ClockOption::set_state_from_config ()
242 {
243         _clock.set (_get ());
244 }
245
246 void
247 ClockOption::add_to_page (OptionEditorPage* p)
248 {
249         add_widgets_to_page (p, &_label, &_clock);
250 }
251
252 void
253 ClockOption::set_session (Session* s)
254 {
255         _clock.set_session (s);
256 }
257
258 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
259         : table (1, 3)
260 {
261         table.set_spacings (4);
262         table.set_col_spacing (0, 32);
263         box.pack_start (table, false, false);
264         box.set_border_width (4);
265         n.append_page (box, t);
266 }
267
268 /** Construct an OptionEditor.
269  *  @param o Configuration to edit.
270  *  @param t Title for the dialog.
271  */
272 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
273         : ArdourWindow (t), _config (c)
274 {
275         using namespace Notebook_Helpers;
276
277         set_default_size (300, 300);
278         set_wmclass (X_("ardour_preferences"), PROGRAM_NAME);
279
280         set_name ("Preferences");
281         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
282
283         set_border_width (4);
284
285         add (_notebook);
286
287         _notebook.set_show_tabs (true);
288         _notebook.set_show_border (true);
289         _notebook.set_name ("OptionsNotebook");
290
291         show_all_children();
292
293         /* Watch out for changes to parameters */
294         _config->ParameterChanged.connect (config_connection, invalidator (*this), boost::bind (&OptionEditor::parameter_changed, this, _1), gui_context());
295 }
296
297 OptionEditor::~OptionEditor ()
298 {
299         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
300                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
301                         delete *j;
302                 }
303                 delete i->second;
304         }
305 }
306
307 /** Called when a configuration parameter has been changed.
308  *  @param p Parameter name.
309  */
310 void
311 OptionEditor::parameter_changed (std::string const & p)
312 {
313         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
314
315         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
316                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
317                         (*j)->parameter_changed (p);
318                 }
319         }
320 }
321
322 /** Add a component to a given page.
323  *  @param pn Page name (will be created if it doesn't already exist)
324  *  @param o Component.
325  */
326 void
327 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
328 {
329         if (_pages.find (pn) == _pages.end()) {
330                 _pages[pn] = new OptionEditorPage (_notebook, pn);
331         }
332
333         OptionEditorPage* p = _pages[pn];
334         p->components.push_back (o);
335
336         o->add_to_page (p);
337         o->set_state_from_config ();
338 }
339
340 void
341 OptionEditor::set_current_page (string const & p)
342 {
343         int i = 0;
344         while (i < _notebook.get_n_pages ()) {
345                 if (_notebook.get_tab_label_text (*_notebook.get_nth_page (i)) == p) {
346                         _notebook.set_current_page (i);
347                         return;
348                 }
349
350                 ++i;
351         }
352 }
353
354
355 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
356         : Option (i, n)
357         , _get (g)
358         , _set (s)
359 {
360         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
361         _file_chooser.signal_file_set().connect (sigc::mem_fun (*this, &DirectoryOption::file_set));
362         _file_chooser.signal_current_folder_changed().connect (sigc::mem_fun (*this, &DirectoryOption::current_folder_set));
363 }
364
365
366 void
367 DirectoryOption::set_state_from_config ()
368 {
369         _file_chooser.set_current_folder (_get ());
370 }
371
372 void
373 DirectoryOption::add_to_page (OptionEditorPage* p)
374 {
375         add_widgets_to_page (p, manage (new Label (_name)), &_file_chooser);
376 }
377
378 void
379 DirectoryOption::file_set ()
380 {
381         _set (_file_chooser.get_filename ());
382 }
383
384 void
385 DirectoryOption::current_folder_set ()
386 {
387         _set (_file_chooser.get_current_folder ());
388 }