accept text-entry-change w/o enter and add filter_text option
[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 "option_editor.h"
32 #include "gui_thread.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);
117         _label = manage (new Label);
118         _label->set_markup (n);
119         _button->add (*_label);
120         _button->set_active (_get ());
121         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
122 }
123
124 void
125 BoolOption::add_to_page (OptionEditorPage* p)
126 {
127         add_widget_to_page (p, _button);
128 }
129
130 void
131 BoolOption::set_state_from_config ()
132 {
133         _button->set_active (_get ());
134 }
135
136 void
137 BoolOption::toggled ()
138 {
139         _set (_button->get_active ());
140 }
141
142 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
143         : Option (i, n),
144           _get (g),
145           _set (s)
146 {
147         _label = manage (left_aligned_label (n + ":"));
148         _entry = manage (new Entry);
149         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
150         _entry->signal_focus_out_event().connect (sigc::mem_fun (*this, &EntryOption::focus_out));
151         _entry->signal_insert_text().connect (sigc::mem_fun (*this, &EntryOption::filter_text));
152 }
153
154 void
155 EntryOption::add_to_page (OptionEditorPage* p)
156 {
157         add_widgets_to_page (p, _label, _entry);
158 }
159
160 void
161 EntryOption::set_state_from_config ()
162 {
163         _entry->set_text (_get ());
164 }
165
166 void
167 EntryOption::set_sensitive (bool s)
168 {
169         _entry->set_sensitive (s);
170 }
171
172 void
173 EntryOption::filter_text (const Glib::ustring&, int*)
174 {
175         std::string text = _entry->get_text ();
176         for (size_t i = 0; i < _invalid.length(); ++i) {
177                 text.erase (std::remove(text.begin(), text.end(), _invalid.at(i)), text.end());
178         }
179         if (text != _entry->get_text ()) {
180                 _entry->set_text (text);
181         }
182 }
183
184 void
185 EntryOption::activated ()
186 {
187         _set (_entry->get_text ());
188 }
189
190 bool
191 EntryOption::focus_out (GdkEventFocus*)
192 {
193         _set (_entry->get_text ());
194         return true;
195 }
196
197 /** Construct a BoolComboOption.
198  *  @param i id
199  *  @param n User-visible name.
200  *  @param t Text to give for the variable being true.
201  *  @param f Text to give for the variable being false.
202  *  @param g Slot to get the variable's value.
203  *  @param s Slot to set the variable's value.
204  */
205 BoolComboOption::BoolComboOption (
206         string const & i, string const & n, string const & t, string const & f, 
207         sigc::slot<bool> g, sigc::slot<bool, bool> s
208         )
209         : Option (i, n)
210         , _get (g)
211         , _set (s)
212 {
213         _label = manage (new Label (n + ":"));
214         _label->set_alignment (0, 0.5);
215         _combo = manage (new ComboBoxText);
216
217         /* option 0 is the false option */
218         _combo->append_text (f);
219         /* and option 1 is the true */
220         _combo->append_text (t);
221         
222         _combo->signal_changed().connect (sigc::mem_fun (*this, &BoolComboOption::changed));
223 }
224
225 void
226 BoolComboOption::set_state_from_config ()
227 {
228         _combo->set_active (_get() ? 1 : 0);
229 }
230
231 void
232 BoolComboOption::add_to_page (OptionEditorPage* p)
233 {
234         add_widgets_to_page (p, _label, _combo);
235 }
236
237 void
238 BoolComboOption::changed ()
239 {
240         _set (_combo->get_active_row_number () == 0 ? false : true);
241 }
242
243 void
244 BoolComboOption::set_sensitive (bool yn)
245 {
246         _combo->set_sensitive (yn);
247 }
248         
249
250           
251 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
252         : Option (i, n)
253         , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
254         , _get (g)
255         , _set (s)
256 {
257         _db_slider = manage (new HSliderController (&_db_adjustment, 115, 18, false));
258
259         _label.set_text (n + ":");
260         _label.set_name (X_("OptionsLabel"));
261
262         _fader_centering_box.pack_start (*_db_slider, true, false);
263
264         _box.set_spacing (4);
265         _box.set_homogeneous (false);
266         _box.pack_start (_fader_centering_box, false, false);
267         _box.pack_start (_db_display, false, false);
268         _box.show_all ();
269
270         set_size_request_to_display_given_text (_db_display, "-99.00", 12, 12);
271
272         _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
273 }
274
275 void
276 FaderOption::set_state_from_config ()
277 {
278         gain_t const val = _get ();
279         _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
280
281         char buf[16];
282
283         if (val == 0.0) {
284                 snprintf (buf, sizeof (buf), "-inf");
285         } else {
286                 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
287         }
288
289         _db_display.set_text (buf);
290 }
291
292 void
293 FaderOption::db_changed ()
294 {
295         _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
296 }
297
298 void
299 FaderOption::add_to_page (OptionEditorPage* p)
300 {
301         add_widgets_to_page (p, &_label, &_box);
302 }
303
304 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<std::string> g, sigc::slot<bool, std::string> s)
305         : Option (i, n)
306         , _clock (X_("timecode-offset"), true, X_(""), true, false, true, false)
307         , _get (g)
308         , _set (s)
309 {
310         _label.set_text (n + ":");
311         _label.set_alignment (0, 0.5);
312         _label.set_name (X_("OptionsLabel"));
313         _clock.ValueChanged.connect (sigc::mem_fun (*this, &ClockOption::save_clock_time));
314 }
315
316 void
317 ClockOption::set_state_from_config ()
318 {
319         Timecode::Time TC;
320         framepos_t when;
321         if (!Timecode::parse_timecode_format(_get(), TC)) {
322                 _clock.set (0, true);
323         }
324         TC.rate = _session->frames_per_timecode_frame();
325         TC.drop = _session->timecode_drop_frames();
326         _session->timecode_to_sample(TC, when, false, false);
327         if (TC.negative) { when=-when; }
328         _clock.set (when, true);
329 }
330
331 void
332 ClockOption::save_clock_time ()
333 {
334         Timecode::Time TC;
335         _session->sample_to_timecode(_clock.current_time(), TC, false, false);
336         _set (Timecode::timecode_format_time(TC));
337 }
338
339 void
340 ClockOption::add_to_page (OptionEditorPage* p)
341 {
342         add_widgets_to_page (p, &_label, &_clock);
343 }
344
345 void
346 ClockOption::set_session (Session* s)
347 {
348         _session = s;
349         _clock.set_session (s);
350 }
351
352 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
353         : table (1, 3)
354 {
355         table.set_spacings (4);
356         table.set_col_spacing (0, 32);
357         box.pack_start (table, false, false);
358         box.set_border_width (4);
359         n.append_page (box, t);
360 }
361
362 /** Construct an OptionEditor.
363  *  @param o Configuration to edit.
364  *  @param t Title for the dialog.
365  */
366 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
367         : ArdourWindow (t), _config (c)
368 {
369         using namespace Notebook_Helpers;
370
371         set_default_size (300, 300);
372         // set_wmclass (X_("ardour_preferences"), PROGRAM_NAME);
373
374         set_name ("Preferences");
375         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
376
377         set_border_width (4);
378
379         add (_notebook);
380
381         _notebook.set_show_tabs (true);
382         _notebook.set_show_border (true);
383         _notebook.set_name ("OptionsNotebook");
384
385         show_all_children();
386
387         /* Watch out for changes to parameters */
388         _config->ParameterChanged.connect (config_connection, invalidator (*this), boost::bind (&OptionEditor::parameter_changed, this, _1), gui_context());
389 }
390
391 OptionEditor::~OptionEditor ()
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                         delete *j;
396                 }
397                 delete i->second;
398         }
399 }
400
401 /** Called when a configuration parameter has been changed.
402  *  @param p Parameter name.
403  */
404 void
405 OptionEditor::parameter_changed (std::string const & p)
406 {
407         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
408
409         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
410                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
411                         (*j)->parameter_changed (p);
412                 }
413         }
414 }
415
416 /** Add a component to a given page.
417  *  @param pn Page name (will be created if it doesn't already exist)
418  *  @param o Component.
419  */
420 void
421 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
422 {
423         if (_pages.find (pn) == _pages.end()) {
424                 _pages[pn] = new OptionEditorPage (_notebook, pn);
425         }
426
427         OptionEditorPage* p = _pages[pn];
428         p->components.push_back (o);
429
430         o->add_to_page (p);
431         o->set_state_from_config ();
432 }
433
434 void
435 OptionEditor::set_current_page (string const & p)
436 {
437         int i = 0;
438         while (i < _notebook.get_n_pages ()) {
439                 if (_notebook.get_tab_label_text (*_notebook.get_nth_page (i)) == p) {
440                         _notebook.set_current_page (i);
441                         return;
442                 }
443
444                 ++i;
445         }
446 }
447
448
449 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
450         : Option (i, n)
451         , _get (g)
452         , _set (s)
453 {
454         _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
455         _file_chooser.signal_file_set().connect (sigc::mem_fun (*this, &DirectoryOption::file_set));
456         _file_chooser.signal_current_folder_changed().connect (sigc::mem_fun (*this, &DirectoryOption::current_folder_set));
457 }
458
459
460 void
461 DirectoryOption::set_state_from_config ()
462 {
463         _file_chooser.set_current_folder (_get ());
464 }
465
466 void
467 DirectoryOption::add_to_page (OptionEditorPage* p)
468 {
469         add_widgets_to_page (p, manage (new Label (_name)), &_file_chooser);
470 }
471
472 void
473 DirectoryOption::file_set ()
474 {
475         _set (_file_chooser.get_filename ());
476 }
477
478 void
479 DirectoryOption::current_folder_set ()
480 {
481         _set (_file_chooser.get_current_folder ());
482 }