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