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