PBD::strip_whitespace_edges() returns the empty string if the passed string is
[ardour.git] / gtk2_ardour / option_editor.cc
1 /*
2     Copyright (C) 2001-2006 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     $Id$
19 */
20
21 #include <pbd/whitespace.h>
22
23 #include <ardour/session.h>
24 #include <ardour/audioengine.h>
25 #include <ardour/configuration.h>
26 #include <ardour/auditioner.h>
27 #include <ardour/destructive_filesource.h>
28 #include <ardour/crossfade.h>
29 #include <midi++/manager.h>
30 #include <gtkmm2ext/stop_signal.h>
31 #include <gtkmm2ext/utils.h>
32
33 #include "public_editor.h"
34 #include "mixer_ui.h"
35 #include "ardour_ui.h"
36 #include "io_selector.h"
37 #include "gain_meter.h"
38 #include "sfdb_ui.h"
39 #include "utils.h"
40 #include "editing.h"
41 #include "option_editor.h"
42
43 #include "i18n.h"
44
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace Gtk;
48 using namespace Editing;
49 using namespace Gtkmm2ext;
50 using namespace std;
51
52 static vector<string> positional_sync_strings;
53
54 OptionEditor::OptionEditor (ARDOUR_UI& uip, PublicEditor& ed, Mixer_UI& mixui)
55         : Dialog ("option editor"),
56           ui (uip),
57           editor (ed),
58           mixer (mixui),
59
60           /* Paths */
61           path_table (11, 2),
62
63           /* Fades */
64
65           short_xfade_adjustment (0, 1.0, 500.0, 5.0, 100.0),
66           short_xfade_slider (short_xfade_adjustment),
67           destructo_xfade_adjustment (1.0, 1.0, 500.0, 1.0, 100.0),
68           destructo_xfade_slider (destructo_xfade_adjustment),
69
70           /* Sync */
71
72           smpte_offset_clock (X_("SMPTEOffsetClock"), true, true),
73           smpte_offset_negative_button (_("SMPTE offset is negative")),
74
75           /* MIDI */
76
77           /* Click */
78
79           click_table (2, 3),
80           click_browse_button (_("Browse")),
81           click_emphasis_browse_button (_("Browse")),
82
83           /* kbd/mouse */
84
85           keyboard_mouse_table (3, 4),
86           delete_button_adjustment (3, 1, 5),
87           delete_button_spin (delete_button_adjustment),
88           edit_button_adjustment (3, 1, 5),
89           edit_button_spin (edit_button_adjustment)
90           
91 {
92         using namespace Notebook_Helpers;
93
94         click_io_selector = 0;
95         auditioner_io_selector = 0;
96         session = 0;
97
98         set_default_size (300, 300);
99         set_title (_("ardour: options editor"));
100         set_wmclass (_("ardour_option_editor"), "Ardour");
101
102         set_name ("OptionsWindow");
103         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
104         
105         VBox *vbox = get_vbox();
106         set_border_width (3);
107
108         vbox->set_spacing (4);
109         vbox->pack_start(notebook);
110
111         signal_delete_event().connect (mem_fun(*this, &OptionEditor::wm_close));
112
113         notebook.set_show_tabs (true);
114         notebook.set_show_border (true);
115         notebook.set_name ("OptionsNotebook");
116
117         setup_sync_options();
118         setup_path_options();
119         setup_fade_options ();
120         setup_keyboard_options ();
121         setup_auditioner_editor ();
122
123         notebook.pages().push_back (TabElem (sync_packer, _("Sync")));
124         notebook.pages().push_back (TabElem (path_table, _("Paths/Files")));
125         notebook.pages().push_back (TabElem (keyboard_mouse_table, _("Kbd/Mouse")));
126         notebook.pages().push_back (TabElem (click_packer, _("Click")));
127         notebook.pages().push_back (TabElem (audition_packer, _("Audition")));
128         notebook.pages().push_back (TabElem (fade_packer, _("Layers & Fades")));
129
130         if (!MIDI::Manager::instance()->get_midi_ports().empty()) {
131                 setup_midi_options ();
132                 notebook.pages().push_back (TabElem (midi_packer, _("MIDI")));
133         }
134
135         set_session (0);
136         show_all_children();
137 }
138
139 void
140 OptionEditor::set_session (Session *s)
141 {
142         clear_click_editor ();
143         clear_auditioner_editor ();
144
145         click_path_entry.set_text ("");
146         click_emphasis_path_entry.set_text ("");
147         session_raid_entry.set_text ("");
148
149         click_path_entry.set_sensitive (false);
150         click_emphasis_path_entry.set_sensitive (false);
151         session_raid_entry.set_sensitive (false);
152
153         short_xfade_slider.set_sensitive (false);
154         smpte_offset_negative_button.set_sensitive (false);
155
156         smpte_offset_clock.set_session (s);
157
158         if ((session = s) == 0) {
159                 return;
160         }
161
162         click_path_entry.set_sensitive (true);
163         click_emphasis_path_entry.set_sensitive (true);
164         session_raid_entry.set_sensitive (true);
165         short_xfade_slider.set_sensitive (true);
166         smpte_offset_negative_button.set_sensitive (true);
167
168         smpte_offset_clock.set_session (s);
169         smpte_offset_clock.set (s->smpte_offset (), true);
170
171         smpte_offset_negative_button.set_active (session->smpte_offset_negative());
172
173         /* set up port assignments */
174
175         std::map<MIDI::Port*,vector<RadioButton*> >::iterator res;
176
177         if (session->mtc_port()) {
178                 if ((res = port_toggle_buttons.find (session->mtc_port())) != port_toggle_buttons.end()) {
179                         (*res).second[MtcIndex]->set_active (true);
180                 }
181         } 
182
183         if (session->mmc_port ()) {
184                 if ((res = port_toggle_buttons.find (session->mmc_port())) != port_toggle_buttons.end()) {
185                         (*res).second[MmcIndex]->set_active (true);
186                 } 
187         }
188
189         if (session->midi_port()) {
190                 if ((res = port_toggle_buttons.find (session->midi_port())) != port_toggle_buttons.end()) {
191                         (*res).second[MidiIndex]->set_active (true);
192                 }
193         }
194
195         setup_click_editor ();
196         connect_audition_editor ();
197
198         short_xfade_adjustment.set_value ((Crossfade::short_xfade_length() / (float) session->frame_rate()) * 1000.0);
199
200         add_session_paths ();
201 }
202
203 OptionEditor::~OptionEditor ()
204 {
205 }
206
207 void
208 OptionEditor::setup_path_options()
209 {
210         Gtk::Label* label;
211
212         path_table.set_homogeneous (false);
213         path_table.set_border_width (12);
214         path_table.set_row_spacings (5);
215
216         session_raid_entry.set_name ("OptionsEntry");
217
218         session_raid_entry.signal_activate().connect (mem_fun(*this, &OptionEditor::raid_path_changed));
219
220         label = manage(new Label(_("session RAID path")));
221         label->set_name ("OptionsLabel");
222         path_table.attach (*label, 0, 1, 0, 1, FILL|EXPAND, FILL);
223         path_table.attach (session_raid_entry, 1, 3, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
224
225         path_table.show_all();
226 }
227
228 void
229 OptionEditor::add_session_paths ()
230 {
231         click_path_entry.set_sensitive (true);
232         click_emphasis_path_entry.set_sensitive (true);
233         session_raid_entry.set_sensitive (true);
234
235         if (Config->get_click_sound().empty()) {
236                 click_path_entry.set_text (_("internal"));
237         } else {
238                 click_path_entry.set_text (Config->get_click_sound());
239         }
240
241         if (Config->get_click_emphasis_sound().empty()) {
242                 click_emphasis_path_entry.set_text (_("internal"));
243         } else {
244                 click_emphasis_path_entry.set_text (Config->get_click_emphasis_sound());
245         }
246
247         session_raid_entry.set_text(session->raid_path());
248 }
249
250 void
251 OptionEditor::setup_fade_options ()
252 {
253         Gtk::HBox* hbox;
254         
255         Label* label = manage (new Label (_("Short crossfade length (msecs)")));
256         label->set_name ("OptionsLabel");
257         
258         hbox = manage (new HBox);
259         hbox->set_border_width (5);
260         hbox->set_spacing (10);
261         hbox->pack_start (*label, false, false);
262         hbox->pack_start (short_xfade_slider, true, true);
263         fade_packer.pack_start (*hbox, false, false);
264
265         short_xfade_adjustment.signal_value_changed().connect (mem_fun(*this, &OptionEditor::short_xfade_adjustment_changed));
266
267         label = manage (new Label (_("Destructive crossfade length (msecs)")));
268         label->set_name ("OptionsLabel");
269         
270         hbox = manage (new HBox);
271         hbox->set_border_width (5);
272         hbox->set_spacing (10);
273         hbox->pack_start (*label, false, false);
274         hbox->pack_start (destructo_xfade_slider, true, true);
275         fade_packer.pack_start (*hbox, false, false);
276         
277         destructo_xfade_adjustment.signal_value_changed().connect (mem_fun(*this, &OptionEditor::destructo_xfade_adjustment_changed));
278
279         short_xfade_slider.set_update_policy (UPDATE_DISCONTINUOUS);
280         destructo_xfade_slider.set_update_policy (UPDATE_DISCONTINUOUS);
281
282         destructo_xfade_adjustment.set_value (Config->get_destructive_xfade_msecs());
283
284         fade_packer.show_all ();
285 }
286
287 void
288 OptionEditor::short_xfade_adjustment_changed ()
289 {
290         if (session) {
291                 float val = short_xfade_adjustment.get_value();
292                 
293                 /* val is in msecs */
294                 
295                 Crossfade::set_short_xfade_length ((nframes_t) floor (session->frame_rate() * (val / 1000.0)));
296         }
297 }
298
299 void
300 OptionEditor::destructo_xfade_adjustment_changed ()
301 {
302         float val = destructo_xfade_adjustment.get_value();
303
304         /* val is in msecs */
305
306         
307         Config->set_destructive_xfade_msecs ((uint32_t) floor (val));
308
309         if (session) {
310                 SndFileSource::setup_standard_crossfades (session->frame_rate());
311         } 
312 }
313
314 void
315 OptionEditor::setup_sync_options ()
316 {
317         HBox* hbox;
318         vector<string> dumb;
319
320         smpte_offset_clock.set_mode (AudioClock::SMPTE);
321         smpte_offset_clock.ValueChanged.connect (mem_fun(*this, &OptionEditor::smpte_offset_chosen));
322         
323         smpte_offset_negative_button.set_name ("OptionEditorToggleButton");
324
325         smpte_offset_negative_button.unset_flags (Gtk::CAN_FOCUS);
326
327         Label *smpte_offset_label = manage (new Label (_("SMPTE Offset")));
328         smpte_offset_label->set_name("OptionsLabel");
329         
330         hbox = manage (new HBox);
331         hbox->set_border_width (5);
332         hbox->set_spacing (10);
333         hbox->pack_start (*smpte_offset_label, false, false);
334         hbox->pack_start (smpte_offset_clock, false, false);
335         hbox->pack_start (smpte_offset_negative_button, false, false);
336
337         sync_packer.pack_start (*hbox, false, false);
338
339         smpte_offset_negative_button.signal_clicked().connect (mem_fun(*this, &OptionEditor::smpte_offset_negative_clicked));
340 }
341
342 void
343 OptionEditor::smpte_offset_negative_clicked ()
344 {
345         if (session) {
346                 session->set_smpte_offset_negative (smpte_offset_negative_button.get_active());
347         }
348 }
349
350 void
351 OptionEditor::smpte_offset_chosen()
352 {
353         if (session) {
354                 nframes_t frames = smpte_offset_clock.current_duration();
355                 session->set_smpte_offset (frames);
356         }
357 }
358
359
360 void
361 OptionEditor::setup_midi_options ()
362 {
363         HBox* hbox;
364         MIDI::Manager::PortMap::const_iterator i;
365         const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
366         int n;
367         ToggleButton* tb;
368         RadioButton* rb;
369
370         Gtk::Table* table = manage (new Table (ports.size() + 4, 9));
371
372         table->set_row_spacings (6);
373         table->set_col_spacings (10);
374
375         table->attach (*(manage (new Label (X_("Port")))), 0, 1, 0, 1);
376         table->attach (*(manage (new Label (X_("Offline")))), 1, 2, 0, 1);
377         table->attach (*(manage (new Label (X_("Trace\nInput")))), 2, 3, 0, 1);
378         table->attach (*(manage (new Label (X_("Trace\nOutput")))), 3, 4, 0, 1);
379         table->attach (*(manage (new Label (X_("MTC")))), 4, 5, 0, 1);
380         table->attach (*(manage (new Label (X_("MMC")))), 6, 7, 0, 1);
381         table->attach (*(manage (new Label (X_("MIDI Parameter\nControl")))), 8, 9, 0, 1);
382
383         table->attach (*(manage (new HSeparator())), 0, 9, 1, 2);
384         table->attach (*(manage (new VSeparator())), 5, 6, 0, 8);
385         table->attach (*(manage (new VSeparator())), 7, 8, 0, 8);
386         
387         for (n = 0, i = ports.begin(); i != ports.end(); ++n, ++i) {
388
389                 pair<MIDI::Port*,vector<RadioButton*> > newpair;
390
391                 newpair.first = i->second;
392
393                 table->attach (*(manage (new Label (i->first))), 0, 1, n+2, n+3,FILL|EXPAND, FILL );
394                 tb = manage (new ToggleButton (_("online")));
395                 tb->set_name ("OptionEditorToggleButton");
396
397                 /* remember, we have to handle the i18n case where the relative
398                    lengths of the strings in language N is different than in english.
399                 */
400
401                 if (strlen (_("offline")) > strlen (_("online"))) {
402                         set_size_request_to_display_given_text (*tb, _("offline"), 15, 12);
403                 } else {
404                         set_size_request_to_display_given_text (*tb, _("online"), 15, 12);
405                 }
406
407                 tb->set_active (!(*i).second->input()->offline());
408                 tb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::port_online_toggled), (*i).second, tb));
409                 (*i).second->input()->OfflineStatusChanged.connect (bind (mem_fun(*this, &OptionEditor::map_port_online), (*i).second, tb));
410                 table->attach (*tb, 1, 2, n+2, n+3, FILL|EXPAND, FILL);
411
412                 tb = manage (new ToggleButton ());
413                 tb->set_name ("OptionEditorToggleButton");
414                 tb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::port_trace_in_toggled), (*i).second, tb));
415                 tb->set_size_request (10, 10);
416                 table->attach (*tb, 2, 3, n+2, n+3, FILL|EXPAND, FILL);
417
418                 tb = manage (new ToggleButton ());
419                 tb->set_name ("OptionEditorToggleButton");
420                 tb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::port_trace_out_toggled), (*i).second, tb));
421                 tb->set_size_request (10, 10);
422                 table->attach (*tb, 3, 4, n+2, n+3, FILL|EXPAND, FILL);
423
424                 rb = manage (new RadioButton ());
425                 newpair.second.push_back (rb);
426                 rb->set_name ("OptionEditorToggleButton");
427                 if (n == 0) {
428                         mtc_button_group = rb->get_group();
429                 } else {
430                         rb->set_group (mtc_button_group);
431
432                 }
433                 table->attach (*rb, 4, 5, n+2, n+3, FILL|EXPAND, FILL);
434                 rb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::mtc_port_chosen), (*i).second, rb));
435
436                 if (Config->get_mtc_port_name() == i->first) {
437                         rb->set_active (true);
438                 }
439                 
440                 rb = manage (new RadioButton ());
441                 newpair.second.push_back (rb);
442                 rb->set_name ("OptionEditorToggleButton");
443                 if (n == 0) {
444                         mmc_button_group = rb->get_group();
445                 } else {
446                         rb->set_group (mmc_button_group);
447                 }
448                 table->attach (*rb, 6, 7, n+2, n+3, FILL|EXPAND, FILL);
449                 rb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::mmc_port_chosen), (*i).second, rb));
450
451                 if (Config->get_mmc_port_name() == i->first) {
452                         rb->set_active (true);
453                 }
454
455                 rb = manage (new RadioButton ());
456                 newpair.second.push_back (rb);
457                 rb->set_name ("OptionEditorToggleButton");
458                 if (n == 0) {
459                         midi_button_group = rb->get_group();
460                 } else {
461                         rb->set_group (midi_button_group);
462                 }
463                 table->attach (*rb, 8, 9, n+2, n+3, FILL|EXPAND, FILL);
464                 rb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::midi_port_chosen), (*i).second, rb));
465
466                 if (Config->get_midi_port_name() == i->first) {
467                         rb->set_active (true);
468                 }
469                 
470                 port_toggle_buttons.insert (newpair);
471         }
472
473         table->show_all ();
474
475         hbox = manage (new HBox);
476         hbox->set_border_width (6);
477         hbox->pack_start (*table, true, false);
478         midi_packer.pack_start (*hbox, false, false);
479 }
480
481 void
482 OptionEditor::mtc_port_chosen (MIDI::Port *port, Gtk::RadioButton* rb) 
483 {
484         if (session) {
485                 if (rb->get_active()) {
486                         cerr << "Activating MTC port " << port->name() << endl;
487                         if (port) {
488                                 session->set_mtc_port (port->name());
489                                 Config->set_mtc_port_name (port->name());
490                         } else {
491                                 session->set_mtc_port ("");
492                         }
493                         rb->set_active (true);
494                 }
495         }
496 }
497
498 void
499 OptionEditor::mmc_port_chosen (MIDI::Port* port, Gtk::RadioButton* rb)
500 {
501         if (session) {
502                 if (rb->get_active()) {
503                         cerr << "Activating MMC port " << port->name() << endl;
504                         if (port) {
505                                 session->set_mmc_port (port->name());
506                                 Config->set_mtc_port_name (port->name());
507                         } else {
508                                 session->set_mmc_port ("");
509                         }
510                         rb->set_active (true);
511                 }
512         }
513 }
514
515 void
516 OptionEditor::midi_port_chosen (MIDI::Port* port, Gtk::RadioButton* rb)
517 {
518         if (session) {
519                 if (rb->get_active()) {
520                         cerr << "Activating MIDI port " << port->name() << endl;
521                         if (port) {
522                                 session->set_midi_port (port->name());
523                                 Config->set_midi_port_name (port->name());
524                         } else {
525                                 session->set_midi_port ("");
526                         }
527                         rb->set_active (true);
528                 }
529         }
530 }
531
532 void
533 OptionEditor::port_online_toggled (MIDI::Port* port, ToggleButton* tb)
534 {
535         bool wanted = tb->get_active();
536         
537         if (wanted != port->input()->offline()) {
538                 port->input()->set_offline (wanted);
539         } 
540 }
541
542 void
543 OptionEditor::map_port_online (MIDI::Port* port, ToggleButton* tb)
544 {
545         if (port->input()->offline()) {
546                 tb->set_label (_("offline"));
547                 tb->set_active (false);
548         } else {
549                 tb->set_label (_("online"));
550                 tb->set_active (true);
551         }
552 }
553
554 void
555 OptionEditor::port_trace_in_toggled (MIDI::Port* port, ToggleButton* tb)
556 {
557         bool trace = tb->get_active();
558
559         if (port->input()->tracing() != trace) {
560                 port->output()->trace (trace, &cerr, string (port->name()) + string (" input: "));
561         }
562 }
563
564 void
565 OptionEditor::port_trace_out_toggled (MIDI::Port* port, ToggleButton* tb)
566 {
567         bool trace = tb->get_active();
568
569         if (port->output()->tracing() != trace) {
570                 port->output()->trace (trace, &cerr, string (port->name()) + string (" output: "));
571         }
572 }
573
574 void
575 OptionEditor::save ()
576 {
577         /* XXX a bit odd that we save the entire session state here */
578
579         ui.save_state ("");
580 }
581
582 gint
583 OptionEditor::wm_close (GdkEventAny *ev)
584 {
585         save ();
586         hide ();
587         return TRUE;
588 }
589
590 void
591 OptionEditor::raid_path_changed ()
592 {
593         if (session) {
594                 Config->set_raid_path (session_raid_entry.get_text());
595         }
596 }
597
598 void
599 OptionEditor::click_browse_clicked ()
600 {
601         SoundFileChooser sfdb (_("Choose Click"), session);
602         
603         int result = sfdb.run ();
604
605         if (result == Gtk::RESPONSE_OK) {
606                 click_chosen(sfdb.get_filename());
607         }
608 }
609
610 void
611 OptionEditor::click_chosen (const string & path)
612 {
613         click_path_entry.set_text (path);
614         click_sound_changed ();
615 }
616
617 void
618 OptionEditor::click_emphasis_browse_clicked ()
619 {
620         SoundFileChooser sfdb (_("Choose Click Emphasis"), session);
621
622         int result = sfdb.run ();
623
624         if (result == Gtk::RESPONSE_OK) {
625                 click_emphasis_chosen (sfdb.get_filename());
626         }
627 }
628
629 void
630 OptionEditor::click_emphasis_chosen (const string & path)
631 {       
632         click_emphasis_path_entry.set_text (path);
633         click_emphasis_sound_changed ();
634 }
635
636 void
637 OptionEditor::click_sound_changed ()
638 {
639         if (session) {
640                 string path = click_path_entry.get_text();
641
642                 if (path == Config->get_click_sound()) {
643                         return;
644                 }
645
646                 strip_whitespace_edges (path);
647
648                 if (path == _("internal")) {
649                         Config->set_click_sound ("");
650                 } else {
651                         Config->set_click_sound (path);
652                 }
653         }
654 }
655
656 void
657 OptionEditor::click_emphasis_sound_changed ()
658 {
659         if (session) {
660                 string path = click_emphasis_path_entry.get_text();
661
662                 if (path == Config->get_click_emphasis_sound()) {
663                         return;
664                 }
665
666                 strip_whitespace_edges (path);
667
668                 if (path == _("internal")) {
669                         Config->set_click_emphasis_sound ("");
670                 } else {
671                         Config->set_click_emphasis_sound (path);
672                 }
673         }
674 }
675
676 void
677 OptionEditor::clear_click_editor ()
678 {
679         if (click_io_selector) {
680                 click_packer.remove (*click_io_selector);
681                 click_packer.remove (*click_gpm);
682                 delete click_io_selector;
683                 delete click_gpm;
684                 click_io_selector = 0;
685                 click_gpm = 0;
686         }
687 }
688
689 void
690 OptionEditor::setup_click_editor ()
691 {
692         Label* label;
693         HBox* hpacker = manage (new HBox);
694
695         click_path_entry.set_sensitive (true);
696         click_emphasis_path_entry.set_sensitive (true);
697
698         click_path_entry.set_name ("OptionsEntry");
699         click_emphasis_path_entry.set_name ("OptionsEntry");
700         
701         click_path_entry.signal_activate().connect (mem_fun(*this, &OptionEditor::click_sound_changed));
702         click_emphasis_path_entry.signal_activate().connect (mem_fun(*this, &OptionEditor::click_emphasis_sound_changed));
703
704         click_path_entry.signal_focus_out_event().connect (bind (mem_fun(*this, &OptionEditor::focus_out_event_handler), &OptionEditor::click_sound_changed));
705         click_emphasis_path_entry.signal_focus_out_event().connect (bind (mem_fun(*this, &OptionEditor::focus_out_event_handler), &OptionEditor::click_emphasis_sound_changed));
706
707         click_browse_button.set_name ("EditorGTKButton");
708         click_emphasis_browse_button.set_name ("EditorGTKButton");
709         click_browse_button.signal_clicked().connect (mem_fun(*this, &OptionEditor::click_browse_clicked));
710         click_emphasis_browse_button.signal_clicked().connect (mem_fun(*this, &OptionEditor::click_emphasis_browse_clicked));
711
712         click_packer.set_border_width (12);
713         click_packer.set_spacing (5);
714
715         click_io_selector = new IOSelector (*session, session->click_io(), false);
716         click_gpm = new GainMeter (session->click_io(), *session);
717
718         click_table.set_col_spacings (10);
719         
720         label = manage(new Label(_("Click audio file")));
721         label->set_name ("OptionsLabel");
722         click_table.attach (*label, 0, 1, 0, 1, FILL|EXPAND, FILL);
723         click_table.attach (click_path_entry, 1, 2, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
724         click_table.attach (click_browse_button, 2, 3, 0, 1, FILL|EXPAND, FILL);
725         
726         label = manage(new Label(_("Click emphasis audiofile")));
727         label->set_name ("OptionsLabel");
728         click_table.attach (*label, 0, 1, 1, 2, FILL|EXPAND, FILL);
729         click_table.attach (click_emphasis_path_entry, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
730         click_table.attach (click_emphasis_browse_button, 2, 3, 1, 2, FILL|EXPAND, FILL);
731
732         hpacker->set_spacing (10);
733         hpacker->pack_start (*click_io_selector, false, false);
734         hpacker->pack_start (*click_gpm, false, false);
735
736         click_packer.pack_start (click_table, false, false);
737         click_packer.pack_start (*hpacker, false, false);
738
739         click_packer.show_all ();
740 }
741
742 void
743 OptionEditor::clear_auditioner_editor ()
744 {
745         if (auditioner_io_selector) {
746                 audition_hpacker.remove (*auditioner_io_selector);
747                 audition_hpacker.remove (*auditioner_gpm);
748                 delete auditioner_io_selector;
749                 delete auditioner_gpm;
750                 auditioner_io_selector = 0;
751                 auditioner_gpm = 0;
752         }
753 }
754
755 void
756 OptionEditor::setup_auditioner_editor ()
757 {
758         audition_packer.set_border_width (12);
759         audition_packer.set_spacing (5);
760         audition_hpacker.set_spacing (10);
761
762         audition_label.set_name ("OptionEditorAuditionerLabel");
763         audition_label.set_text (_("The auditioner is a dedicated mixer strip used\n"
764                                    "for listening to specific regions outside the context\n"
765                                    "of the overall mix. It can be connected just like any\n"
766                                    "other mixer strip."));
767         
768         audition_packer.pack_start (audition_label, false, false, 10);
769         audition_packer.pack_start (audition_hpacker, false, false);
770 }
771
772 void
773 OptionEditor::connect_audition_editor ()
774 {
775         auditioner_io_selector = new IOSelector (*session, session->the_auditioner(), false);
776         auditioner_gpm = new GainMeter (session->the_auditioner(), *session);
777
778         audition_hpacker.pack_start (*auditioner_io_selector, false, false);
779         audition_hpacker.pack_start (*auditioner_gpm, false, false);
780
781         auditioner_io_selector->show_all ();
782         auditioner_gpm->show_all ();
783 }
784
785 bool
786 OptionEditor::focus_out_event_handler (GdkEventFocus* ev, void (OptionEditor::*pmf)()) 
787 {
788         (this->*pmf)();
789         return false;
790 }
791
792 static const struct {
793     const char *name;
794     guint   modifier;
795 } modifiers[] = {
796         { "Shift", GDK_SHIFT_MASK },
797         { "Control", GDK_CONTROL_MASK },
798         { "Alt (Mod1)", GDK_MOD1_MASK },
799         { "Control-Shift", GDK_CONTROL_MASK|GDK_SHIFT_MASK },
800         { "Control-Alt", GDK_CONTROL_MASK|GDK_MOD1_MASK },
801         { "Shift-Alt", GDK_SHIFT_MASK|GDK_MOD1_MASK },
802         { "Control-Shift-Alt", GDK_CONTROL_MASK|GDK_SHIFT_MASK|GDK_MOD1_MASK },
803         { "Mod2", GDK_MOD2_MASK },
804         { "Mod3", GDK_MOD3_MASK },
805         { "Mod4", GDK_MOD4_MASK },
806         { "Mod5", GDK_MOD5_MASK },
807         { 0, 0 }
808 };
809
810 void
811 OptionEditor::setup_keyboard_options ()
812 {
813         vector<string> dumb;
814         Label* label;
815
816         keyboard_mouse_table.set_border_width (12);
817         keyboard_mouse_table.set_row_spacings (5);
818         keyboard_mouse_table.set_col_spacings (5);
819
820         /* internationalize and prepare for use with combos */
821
822         for (int i = 0; modifiers[i].name; ++i) {
823                 dumb.push_back (_(modifiers[i].name));
824         }
825
826         set_popdown_strings (edit_modifier_combo, dumb);
827         edit_modifier_combo.signal_changed().connect (mem_fun(*this, &OptionEditor::edit_modifier_chosen));
828
829         for (int x = 0; modifiers[x].name; ++x) {
830                 if (modifiers[x].modifier == Keyboard::edit_modifier ()) {
831                         edit_modifier_combo.set_active_text (_(modifiers[x].name));
832                         break;
833                 }
834         }
835
836         label = manage (new Label (_("Edit using")));
837         label->set_name ("OptionsLabel");
838         label->set_alignment (1.0, 0.5);
839                 
840         keyboard_mouse_table.attach (*label, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
841         keyboard_mouse_table.attach (edit_modifier_combo, 1, 2, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
842
843         label = manage (new Label (_("+ button")));
844         label->set_name ("OptionsLabel");
845         
846         keyboard_mouse_table.attach (*label, 3, 4, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
847         keyboard_mouse_table.attach (edit_button_spin, 4, 5, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
848
849         edit_button_spin.set_name ("OptionsEntry");
850         edit_button_adjustment.set_value (Keyboard::edit_button());
851         edit_button_adjustment.signal_value_changed().connect (mem_fun(*this, &OptionEditor::edit_button_changed));
852
853         set_popdown_strings (delete_modifier_combo, dumb);
854         delete_modifier_combo.signal_changed().connect (mem_fun(*this, &OptionEditor::delete_modifier_chosen));
855
856         for (int x = 0; modifiers[x].name; ++x) {
857                 if (modifiers[x].modifier == Keyboard::delete_modifier ()) {
858                         delete_modifier_combo.set_active_text (_(modifiers[x].name));
859                         break;
860                 }
861         }
862
863         label = manage (new Label (_("Delete using")));
864         label->set_name ("OptionsLabel");
865         label->set_alignment (1.0, 0.5);
866                 
867         keyboard_mouse_table.attach (*label, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
868         keyboard_mouse_table.attach (delete_modifier_combo, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
869
870         label = manage (new Label (_("+ button")));
871         label->set_name ("OptionsLabel");
872
873         keyboard_mouse_table.attach (*label, 3, 4, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
874         keyboard_mouse_table.attach (delete_button_spin, 4, 5, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
875
876         delete_button_spin.set_name ("OptionsEntry");
877         delete_button_adjustment.set_value (Keyboard::delete_button());
878         delete_button_adjustment.signal_value_changed().connect (mem_fun(*this, &OptionEditor::delete_button_changed));
879
880         set_popdown_strings (snap_modifier_combo, dumb);
881         snap_modifier_combo.signal_changed().connect (mem_fun(*this, &OptionEditor::snap_modifier_chosen));
882         
883         for (int x = 0; modifiers[x].name; ++x) {
884                 if (modifiers[x].modifier == (guint) Keyboard::snap_modifier ()) {
885                         snap_modifier_combo.set_active_text (_(modifiers[x].name));
886                         break;
887                 }
888         }
889
890         label = manage (new Label (_("Ignore snap using")));
891         label->set_name ("OptionsLabel");
892         label->set_alignment (1.0, 0.5);
893         
894         keyboard_mouse_table.attach (*label, 0, 1, 2, 3, Gtk::FILL|Gtk::EXPAND, FILL);
895         keyboard_mouse_table.attach (snap_modifier_combo, 1, 2, 2, 3, Gtk::FILL|Gtk::EXPAND, FILL);
896 }
897
898 void
899 OptionEditor::edit_modifier_chosen ()
900 {
901         string txt;
902         
903         txt = edit_modifier_combo.get_active_text();
904
905         for (int i = 0; modifiers[i].name; ++i) {
906                 if (txt == _(modifiers[i].name)) {
907                         Keyboard::set_edit_modifier (modifiers[i].modifier);
908                         break;
909                 }
910         }
911 }
912
913 void
914 OptionEditor::delete_modifier_chosen ()
915 {
916         string txt;
917         
918         txt = delete_modifier_combo.get_active_text();
919
920         for (int i = 0; modifiers[i].name; ++i) {
921                 if (txt == _(modifiers[i].name)) {
922                         Keyboard::set_delete_modifier (modifiers[i].modifier);
923                         break;
924                 }
925         }
926 }
927
928 void
929 OptionEditor::snap_modifier_chosen ()
930 {
931         string txt;
932         
933         txt = snap_modifier_combo.get_active_text();
934
935         for (int i = 0; modifiers[i].name; ++i) {
936                 if (txt == _(modifiers[i].name)) {
937                         Keyboard::set_snap_modifier (modifiers[i].modifier);
938                         break;
939                 }
940         }
941 }
942
943 void
944 OptionEditor::delete_button_changed ()
945 {
946         Keyboard::set_delete_button ((guint) delete_button_adjustment.get_value());
947 }
948
949 void
950 OptionEditor::edit_button_changed ()
951 {
952         Keyboard::set_edit_button ((guint) edit_button_adjustment.get_value());
953 }
954
955 void
956 OptionEditor::fixup_combo_size (Gtk::ComboBoxText& combo, vector<string>& strings)
957 {
958         /* find the widest string */
959
960         string::size_type maxlen = 0;
961         string maxstring;
962
963         for (vector<string>::iterator i = strings.begin(); i != strings.end(); ++i) {
964                 string::size_type l;
965
966                 if ((l = (*i).length()) > maxlen) {
967                         maxlen = l;
968                         maxstring = *i;
969                 }
970         }
971
972         /* try to include ascenders and descenders */
973
974         if (maxstring.length() > 2) {
975                 maxstring[0] = 'g';
976                 maxstring[1] = 'l';
977         }
978
979         const guint32 FUDGE = 10; // Combo's are stupid - they steal space from the entry for the button
980
981         set_size_request_to_display_given_text (combo, maxstring.c_str(), 10 + FUDGE, 10);
982 }
983