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