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