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