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