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