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