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