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