Add a label to the add midi port button in the options editor. Give the Add MIDI...
[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         add_midi_port_button.set_label ("Add MIDI port");
391         midi_packer.pack_start (add_midi_port_button, false, false);
392
393         hbox = manage (new HBox);
394         hbox->set_border_width (6);
395         hbox->set_spacing (6);
396         label = (manage (new Label (_("Inbound MMC Device ID")))); 
397         hbox->pack_start (mmc_receive_device_id_spinner, false, false);
398         hbox->pack_start (*label, false, false);
399         midi_packer.pack_start (*hbox, false, false);
400
401         hbox = manage (new HBox);
402         hbox->set_border_width (6);
403         hbox->set_spacing (6);
404         label = (manage (new Label (_("Outbound MMC Device ID")))); 
405         hbox->pack_start (mmc_send_device_id_spinner, false, false);
406         hbox->pack_start (*label, false, false);
407         midi_packer.pack_start (*hbox, false, false);
408
409         add_midi_port_button.signal_clicked().connect (mem_fun (*this, &OptionEditor::add_midi_port));
410 }
411
412 void
413 OptionEditor::redisplay_midi_ports ()
414 {
415         MIDI::Manager::PortMap::const_iterator i;
416         const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
417         int n;
418
419         /* remove all existing widgets */
420
421         // XXX broken in gtkmm 2.10
422         // midi_port_table.clear ();
423
424         for (vector<Widget*>::iterator w = midi_port_table_widgets.begin(); w != midi_port_table_widgets.end(); ++w) {
425                 midi_port_table.remove (**w);
426         }
427
428         midi_port_table_widgets.clear ();
429
430         midi_port_table.resize (ports.size() + 4, 11);
431
432         Gtk::Label* label;
433
434         label = (manage (new Label (_("Port")))); 
435         label->show ();
436         midi_port_table_widgets.push_back (label);
437         midi_port_table.attach (*label, 0, 1, 0, 1);
438         label = (manage (new Label (_("Offline")))); 
439         label->show ();
440         midi_port_table_widgets.push_back (label);
441         midi_port_table.attach (*label, 1, 2, 0, 1);
442         label = (manage (new Label (_("Trace\nInput")))); 
443         label->show ();
444         midi_port_table_widgets.push_back (label);
445         midi_port_table.attach (*label, 2, 3, 0, 1);
446         label = (manage (new Label (_("Trace\nOutput")))); 
447         label->show ();
448         midi_port_table_widgets.push_back (label);
449         midi_port_table.attach (*label, 3, 4, 0, 1);
450         label = (manage (new Label (_("MTC")))); 
451         label->show ();
452         midi_port_table_widgets.push_back (label);
453         midi_port_table.attach (*label, 4, 5, 0, 1);
454         label = (manage (new Label (_("MMC")))); 
455         label->show ();
456         midi_port_table_widgets.push_back (label);
457         midi_port_table.attach (*label, 6, 7, 0, 1);
458         label = (manage (new Label (_("MIDI Parameter\nControl")))); 
459         label->show ();
460         midi_port_table_widgets.push_back (label);
461         midi_port_table.attach (*label, 8, 9, 0, 1);
462
463         Gtk::HSeparator* hsep = (manage (new HSeparator())); 
464         hsep->show ();
465         midi_port_table_widgets.push_back (hsep);
466         midi_port_table.attach (*hsep, 0, 9, 1, 2);
467         Gtk::VSeparator* vsep = (manage (new VSeparator())); 
468         vsep->show ();
469         midi_port_table_widgets.push_back (vsep);
470         midi_port_table.attach (*vsep, 5, 6, 0, 8);
471         vsep = (manage (new VSeparator())); 
472         vsep->show ();
473         midi_port_table_widgets.push_back (vsep);
474         midi_port_table.attach (*vsep, 7, 8, 0, 8);
475         
476         for (n = 0, i = ports.begin(); i != ports.end(); ++n, ++i) {
477
478                 ToggleButton* tb;
479                 RadioButton* rb;
480                 Button* bb;
481
482                 /* the remove button. create early so we can pass it to various callbacks */
483                 
484                 bb = manage (new Button (Stock::REMOVE));
485                 bb->set_name ("OptionEditorToggleButton");
486                 bb->show ();
487                 midi_port_table_widgets.push_back (bb);
488                 midi_port_table.attach (*bb, 9, 10, n+2, n+3, FILL|EXPAND, FILL);
489                 bb->signal_clicked().connect (bind (mem_fun(*this, &OptionEditor::remove_midi_port), i->second));
490                 bb->set_sensitive (port_removable (i->second));
491
492                 label = (manage (new Label (i->first))); 
493                 label->show ();
494                 midi_port_table_widgets.push_back (label);
495                 midi_port_table.attach (*label, 0, 1, n+2, n+3,FILL|EXPAND, FILL );
496                 
497                 tb = manage (new ToggleButton (_("online")));
498                 tb->set_name ("OptionEditorToggleButton");
499
500                 /* remember, we have to handle the i18n case where the relative
501                    lengths of the strings in language N is different than in english.
502                 */
503
504                 if (strlen (_("offline")) > strlen (_("online"))) {
505                         set_size_request_to_display_given_text (*tb, _("offline"), 15, 12);
506                 } else {
507                         set_size_request_to_display_given_text (*tb, _("online"), 15, 12);
508                 }
509
510                 if (i->second->input()) {
511                         tb->set_active (!i->second->input()->offline());
512                         tb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::port_online_toggled), i->second, tb));
513                         i->second->input()->OfflineStatusChanged.connect (bind (mem_fun(*this, &OptionEditor::map_port_online), (*i).second, tb));
514                 }
515                 tb->show ();
516                 midi_port_table_widgets.push_back (tb);
517                 midi_port_table.attach (*tb, 1, 2, n+2, n+3, FILL|EXPAND, FILL);
518
519                 tb = manage (new ToggleButton ());
520                 tb->set_name ("OptionEditorToggleButton");
521                 tb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::port_trace_in_toggled), (*i).second, tb));
522                 tb->set_size_request (10, 10);
523                 tb->show ();
524                 midi_port_table_widgets.push_back (tb);
525                 midi_port_table.attach (*tb, 2, 3, n+2, n+3, FILL|EXPAND, FILL);
526
527                 tb = manage (new ToggleButton ());
528                 tb->set_name ("OptionEditorToggleButton");
529                 tb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::port_trace_out_toggled), (*i).second, tb));
530                 tb->set_size_request (10, 10);
531                 tb->show ();
532                 midi_port_table_widgets.push_back (tb);
533                 midi_port_table.attach (*tb, 3, 4, n+2, n+3, FILL|EXPAND, FILL);
534
535                 rb = manage (new RadioButton ());
536                 rb->set_name ("OptionEditorToggleButton");
537                 if (n == 0) {
538                         mtc_button_group = rb->get_group();
539                 } else {
540                         rb->set_group (mtc_button_group);
541
542                 }
543                 rb->show ();
544                 midi_port_table_widgets.push_back (rb);
545                 midi_port_table.attach (*rb, 4, 5, n+2, n+3, FILL|EXPAND, FILL);
546                 rb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::mtc_port_chosen), (*i).second, rb, bb));
547
548                 if (session && i->second == session->mtc_port()) {
549                         rb->set_active (true);
550                 }
551                 
552                 rb = manage (new RadioButton ());
553                 rb->set_name ("OptionEditorToggleButton");
554                 if (n == 0) {
555                         mmc_button_group = rb->get_group();
556                 } else {
557                         rb->set_group (mmc_button_group);
558                 }
559                 rb->show ();
560                 midi_port_table_widgets.push_back (rb);
561                 midi_port_table.attach (*rb, 6, 7, n+2, n+3, FILL|EXPAND, FILL);
562                 rb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::mmc_port_chosen), (*i).second, rb, bb));
563
564                 if (session && i->second == session->mmc_port()) {
565                         rb->set_active (true);
566                 }
567
568                 rb = manage (new RadioButton ());
569                 rb->set_name ("OptionEditorToggleButton");
570                 if (n == 0) {
571                         midi_button_group = rb->get_group();
572                 } else {
573                         rb->set_group (midi_button_group);
574                 }
575                 rb->show ();
576                 midi_port_table_widgets.push_back (rb);
577                 midi_port_table.attach (*rb, 8, 9, n+2, n+3, FILL|EXPAND, FILL);
578                 rb->signal_toggled().connect (bind (mem_fun(*this, &OptionEditor::midi_port_chosen), (*i).second, rb, bb));
579
580                 if (session && i->second == session->midi_port()) {
581                         rb->set_active (true);
582                 }
583
584         }
585
586         midi_port_table.show();
587 }
588
589 void
590 OptionEditor::remove_midi_port (MIDI::Port* port)
591 {
592         MIDI::Manager::instance()->remove_port (port);
593         redisplay_midi_ports ();
594 }
595
596 void
597 OptionEditor::add_midi_port ()
598 {
599         MidiPortDialog dialog;
600
601         dialog.set_position (WIN_POS_MOUSE);
602         dialog.set_transient_for (*this);
603
604         dialog.show ();
605
606         int ret = dialog.run ();
607
608         switch (ret) {
609         case RESPONSE_ACCEPT:
610                 break;
611         default:
612                 return;
613                 break;
614         }
615
616         Glib::ustring mode = dialog.port_mode_combo.get_active_text();
617         std::string smod;
618
619         if (mode == _("input")) {
620                 smod = X_("input");
621         } else if (mode == (_("output"))) {
622                 smod = X_("output");
623         } else {
624                 smod = "duplex";
625         }
626
627         MIDI::PortRequest req (X_("ardour"),
628                                dialog.port_name.get_text(),
629                                smod,
630                                MIDI::PortFactory::default_port_type());
631
632         if (MIDI::Manager::instance()->add_port (req) != 0) {
633                 redisplay_midi_ports ();
634         }
635 }
636
637 bool
638 OptionEditor::port_removable (MIDI::Port *port)
639 {
640         if (!session) {
641                 return true;
642         }
643
644         if (port == session->mtc_port() ||
645             port == session->mmc_port() ||
646             port == session->midi_port()) {
647                 return false;
648         }
649         return true;
650 }
651
652 void
653 OptionEditor::mtc_port_chosen (MIDI::Port *port, Gtk::RadioButton* rb, Gtk::Button* bb) 
654 {
655         if (session) {
656                 if (rb->get_active()) {
657                         session->set_mtc_port (port->name());
658                         Config->set_mtc_port_name (port->name());
659                 } else {
660                         session->set_mtc_port ("");
661                 }
662                 bb->set_sensitive (port_removable (port));
663         }
664 }
665
666 void
667 OptionEditor::mmc_port_chosen (MIDI::Port* port, Gtk::RadioButton* rb, Gtk::Button* bb)
668 {
669         if (session) {
670                 if (rb->get_active()) {
671                         session->set_mmc_port (port->name());
672                         Config->set_mtc_port_name (port->name());
673                 } else {
674                         session->set_mmc_port ("");
675                 }
676                 bb->set_sensitive (port_removable (port));
677         }
678 }
679
680 void
681 OptionEditor::midi_port_chosen (MIDI::Port* port, Gtk::RadioButton* rb, Gtk::Button* bb)
682 {
683         if (session) {
684                 if (rb->get_active()) {
685                         session->set_midi_port (port->name());
686                         Config->set_midi_port_name (port->name());
687                 } else {
688                         session->set_midi_port ("");
689                 }
690                 bb->set_sensitive (port_removable (port));
691         }
692 }
693
694 void
695 OptionEditor::port_online_toggled (MIDI::Port* port, ToggleButton* tb)
696 {
697         bool wanted = tb->get_active();
698
699         if (wanted != port->input()->offline()) {
700                 port->input()->set_offline (wanted);
701         } 
702 }
703
704 void
705 OptionEditor::map_port_online (MIDI::Port* port, ToggleButton* tb)
706 {
707         bool bstate = tb->get_active ();
708
709         if (bstate != port->input()->offline()) {
710                 if (port->input()->offline()) {
711                         tb->set_label (_("offline"));
712                         tb->set_active (false);
713                 } else {
714                         tb->set_label (_("online"));
715                         tb->set_active (true);
716                 }
717         }
718 }
719
720 void
721 OptionEditor::mmc_receive_device_id_adjusted ()
722 {
723         uint8_t id = (uint8_t) mmc_receive_device_id_spinner.get_value();
724
725         if (id != Config->get_mmc_receive_device_id()) {
726                 Config->set_mmc_receive_device_id (id);
727         }
728 }
729
730 void
731 OptionEditor::mmc_send_device_id_adjusted ()
732 {
733         uint8_t id = (uint8_t) mmc_send_device_id_spinner.get_value();
734
735         if (id != Config->get_mmc_send_device_id()) {
736                 Config->set_mmc_send_device_id (id);
737         }
738 }
739
740 void
741 OptionEditor::port_trace_in_toggled (MIDI::Port* port, ToggleButton* tb)
742 {
743         bool trace = tb->get_active();
744
745         if (port->input()->tracing() != trace) {
746                 port->input()->trace (trace, &cerr, string (port->name()) + string (" input: "));
747         }
748 }
749
750 void
751 OptionEditor::port_trace_out_toggled (MIDI::Port* port, ToggleButton* tb)
752 {
753         bool trace = tb->get_active();
754
755         if (port->output()->tracing() != trace) {
756                 port->output()->trace (trace, &cerr, string (port->name()) + string (" output: "));
757         }
758 }
759
760 void
761 OptionEditor::save ()
762 {
763         /* XXX a bit odd that we save the entire session state here */
764
765         ui.save_state ("");
766 }
767
768 gint
769 OptionEditor::wm_close (GdkEventAny *ev)
770 {
771         save ();
772         hide ();
773         return TRUE;
774 }
775
776 void
777 OptionEditor::raid_path_changed ()
778 {
779         if (session) {
780                 Config->set_raid_path (session_raid_entry.get_text());
781         }
782 }
783
784 void
785 OptionEditor::click_browse_clicked ()
786 {
787         SoundFileChooser sfdb (_("Choose Click"), session);
788         
789         int result = sfdb.run ();
790
791         if (result == Gtk::RESPONSE_OK) {
792                 click_chosen(sfdb.get_filename());
793         }
794 }
795
796 void
797 OptionEditor::click_chosen (const string & path)
798 {
799         click_path_entry.set_text (path);
800         click_sound_changed ();
801 }
802
803 void
804 OptionEditor::click_emphasis_browse_clicked ()
805 {
806         SoundFileChooser sfdb (_("Choose Click Emphasis"), session);
807
808         int result = sfdb.run ();
809
810         if (result == Gtk::RESPONSE_OK) {
811                 click_emphasis_chosen (sfdb.get_filename());
812         }
813 }
814
815 void
816 OptionEditor::click_emphasis_chosen (const string & path)
817 {       
818         click_emphasis_path_entry.set_text (path);
819         click_emphasis_sound_changed ();
820 }
821
822 void
823 OptionEditor::click_sound_changed ()
824 {
825         if (session) {
826                 string path = click_path_entry.get_text();
827
828                 if (path == Config->get_click_sound()) {
829                         return;
830                 }
831
832                 strip_whitespace_edges (path);
833
834                 if (path == _("internal")) {
835                         Config->set_click_sound ("");
836                 } else {
837                         Config->set_click_sound (path);
838                 }
839         }
840 }
841
842 void
843 OptionEditor::click_emphasis_sound_changed ()
844 {
845         if (session) {
846                 string path = click_emphasis_path_entry.get_text();
847
848                 if (path == Config->get_click_emphasis_sound()) {
849                         return;
850                 }
851
852                 strip_whitespace_edges (path);
853
854                 if (path == _("internal")) {
855                         Config->set_click_emphasis_sound ("");
856                 } else {
857                         Config->set_click_emphasis_sound (path);
858                 }
859         }
860 }
861
862 void
863 OptionEditor::clear_click_editor ()
864 {
865         if (click_io_selector) {
866                 click_packer.remove (*click_io_selector);
867                 click_packer.remove (*click_gpm);
868                 delete click_io_selector;
869                 delete click_gpm;
870                 click_io_selector = 0;
871                 click_gpm = 0;
872         }
873 }
874
875 void
876 OptionEditor::setup_click_editor ()
877 {
878         Label* label;
879         HBox* hpacker = manage (new HBox);
880
881         click_path_entry.set_sensitive (true);
882         click_emphasis_path_entry.set_sensitive (true);
883
884         click_path_entry.set_name ("OptionsEntry");
885         click_emphasis_path_entry.set_name ("OptionsEntry");
886         
887         click_path_entry.signal_activate().connect (mem_fun(*this, &OptionEditor::click_sound_changed));
888         click_emphasis_path_entry.signal_activate().connect (mem_fun(*this, &OptionEditor::click_emphasis_sound_changed));
889
890         click_path_entry.signal_focus_out_event().connect (bind (mem_fun(*this, &OptionEditor::focus_out_event_handler), &OptionEditor::click_sound_changed));
891         click_emphasis_path_entry.signal_focus_out_event().connect (bind (mem_fun(*this, &OptionEditor::focus_out_event_handler), &OptionEditor::click_emphasis_sound_changed));
892
893         click_browse_button.set_name ("EditorGTKButton");
894         click_emphasis_browse_button.set_name ("EditorGTKButton");
895         click_browse_button.signal_clicked().connect (mem_fun(*this, &OptionEditor::click_browse_clicked));
896         click_emphasis_browse_button.signal_clicked().connect (mem_fun(*this, &OptionEditor::click_emphasis_browse_clicked));
897
898         click_packer.set_border_width (12);
899         click_packer.set_spacing (5);
900
901         click_io_selector = new IOSelector (*session, session->click_io(), false);
902         click_gpm = new GainMeter (session->click_io(), *session);
903
904         click_table.set_col_spacings (10);
905         
906         label = manage(new Label(_("Click audio file")));
907         label->set_name ("OptionsLabel");
908         click_table.attach (*label, 0, 1, 0, 1, FILL|EXPAND, FILL);
909         click_table.attach (click_path_entry, 1, 2, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
910         click_table.attach (click_browse_button, 2, 3, 0, 1, FILL|EXPAND, FILL);
911         
912         label = manage(new Label(_("Click emphasis audiofile")));
913         label->set_name ("OptionsLabel");
914         click_table.attach (*label, 0, 1, 1, 2, FILL|EXPAND, FILL);
915         click_table.attach (click_emphasis_path_entry, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
916         click_table.attach (click_emphasis_browse_button, 2, 3, 1, 2, FILL|EXPAND, FILL);
917
918         hpacker->set_spacing (10);
919         hpacker->pack_start (*click_io_selector, false, false);
920         hpacker->pack_start (*click_gpm, false, false);
921
922         click_packer.pack_start (click_table, false, false);
923         click_packer.pack_start (*hpacker, false, false);
924
925         click_packer.show_all ();
926 }
927
928 void
929 OptionEditor::clear_auditioner_editor ()
930 {
931         if (auditioner_io_selector) {
932                 audition_hpacker.remove (*auditioner_io_selector);
933                 audition_hpacker.remove (*auditioner_gpm);
934                 delete auditioner_io_selector;
935                 delete auditioner_gpm;
936                 auditioner_io_selector = 0;
937                 auditioner_gpm = 0;
938         }
939 }
940
941 void
942 OptionEditor::setup_auditioner_editor ()
943 {
944         audition_packer.set_border_width (12);
945         audition_packer.set_spacing (5);
946         audition_hpacker.set_spacing (10);
947
948         audition_label.set_name ("OptionEditorAuditionerLabel");
949         audition_label.set_text (_("The auditioner is a dedicated mixer strip used\n"
950                                    "for listening to specific regions outside the context\n"
951                                    "of the overall mix. It can be connected just like any\n"
952                                    "other mixer strip."));
953         
954         audition_packer.pack_start (audition_label, false, false, 10);
955         audition_packer.pack_start (audition_hpacker, false, false);
956 }
957
958 void
959 OptionEditor::connect_audition_editor ()
960 {
961         auditioner_io_selector = new IOSelector (*session, session->the_auditioner(), false);
962         auditioner_gpm = new GainMeter (session->the_auditioner(), *session);
963
964         audition_hpacker.pack_start (*auditioner_io_selector, false, false);
965         audition_hpacker.pack_start (*auditioner_gpm, false, false);
966
967         auditioner_io_selector->show_all ();
968         auditioner_gpm->show_all ();
969 }
970
971 bool
972 OptionEditor::focus_out_event_handler (GdkEventFocus* ev, void (OptionEditor::*pmf)()) 
973 {
974         (this->*pmf)();
975         return false;
976 }
977
978 static const struct {
979     const char *name;
980     guint   modifier;
981 } modifiers[] = {
982         { "Shift", GDK_SHIFT_MASK },
983         { "Control", GDK_CONTROL_MASK },
984         { "Alt (Mod1)", GDK_MOD1_MASK },
985         { "Control-Shift", GDK_CONTROL_MASK|GDK_SHIFT_MASK },
986         { "Control-Alt", GDK_CONTROL_MASK|GDK_MOD1_MASK },
987         { "Shift-Alt", GDK_SHIFT_MASK|GDK_MOD1_MASK },
988         { "Control-Shift-Alt", GDK_CONTROL_MASK|GDK_SHIFT_MASK|GDK_MOD1_MASK },
989         { "Mod2", GDK_MOD2_MASK },
990         { "Mod3", GDK_MOD3_MASK },
991         { "Mod4", GDK_MOD4_MASK },
992         { "Mod5", GDK_MOD5_MASK },
993         { 0, 0 }
994 };
995
996 void
997 OptionEditor::setup_keyboard_options ()
998 {
999         vector<string> dumb;
1000         Label* label;
1001
1002         keyboard_mouse_table.set_border_width (12);
1003         keyboard_mouse_table.set_row_spacings (5);
1004         keyboard_mouse_table.set_col_spacings (5);
1005
1006         /* internationalize and prepare for use with combos */
1007
1008         for (int i = 0; modifiers[i].name; ++i) {
1009                 dumb.push_back (_(modifiers[i].name));
1010         }
1011
1012         set_popdown_strings (edit_modifier_combo, dumb);
1013         edit_modifier_combo.signal_changed().connect (mem_fun(*this, &OptionEditor::edit_modifier_chosen));
1014
1015         for (int x = 0; modifiers[x].name; ++x) {
1016                 if (modifiers[x].modifier == Keyboard::edit_modifier ()) {
1017                         edit_modifier_combo.set_active_text (_(modifiers[x].name));
1018                         break;
1019                 }
1020         }
1021
1022         label = manage (new Label (_("Edit using")));
1023         label->set_name ("OptionsLabel");
1024         label->set_alignment (1.0, 0.5);
1025                 
1026         keyboard_mouse_table.attach (*label, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
1027         keyboard_mouse_table.attach (edit_modifier_combo, 1, 2, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
1028
1029         label = manage (new Label (_("+ button")));
1030         label->set_name ("OptionsLabel");
1031         
1032         keyboard_mouse_table.attach (*label, 3, 4, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
1033         keyboard_mouse_table.attach (edit_button_spin, 4, 5, 0, 1, Gtk::FILL|Gtk::EXPAND, FILL);
1034
1035         edit_button_spin.set_name ("OptionsEntry");
1036         edit_button_adjustment.set_value (Keyboard::edit_button());
1037         edit_button_adjustment.signal_value_changed().connect (mem_fun(*this, &OptionEditor::edit_button_changed));
1038
1039         set_popdown_strings (delete_modifier_combo, dumb);
1040         delete_modifier_combo.signal_changed().connect (mem_fun(*this, &OptionEditor::delete_modifier_chosen));
1041
1042         for (int x = 0; modifiers[x].name; ++x) {
1043                 if (modifiers[x].modifier == Keyboard::delete_modifier ()) {
1044                         delete_modifier_combo.set_active_text (_(modifiers[x].name));
1045                         break;
1046                 }
1047         }
1048
1049         label = manage (new Label (_("Delete using")));
1050         label->set_name ("OptionsLabel");
1051         label->set_alignment (1.0, 0.5);
1052                 
1053         keyboard_mouse_table.attach (*label, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
1054         keyboard_mouse_table.attach (delete_modifier_combo, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
1055
1056         label = manage (new Label (_("+ button")));
1057         label->set_name ("OptionsLabel");
1058
1059         keyboard_mouse_table.attach (*label, 3, 4, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
1060         keyboard_mouse_table.attach (delete_button_spin, 4, 5, 1, 2, Gtk::FILL|Gtk::EXPAND, FILL);
1061
1062         delete_button_spin.set_name ("OptionsEntry");
1063         delete_button_adjustment.set_value (Keyboard::delete_button());
1064         delete_button_adjustment.signal_value_changed().connect (mem_fun(*this, &OptionEditor::delete_button_changed));
1065
1066         set_popdown_strings (snap_modifier_combo, dumb);
1067         snap_modifier_combo.signal_changed().connect (mem_fun(*this, &OptionEditor::snap_modifier_chosen));
1068         
1069         for (int x = 0; modifiers[x].name; ++x) {
1070                 if (modifiers[x].modifier == (guint) Keyboard::snap_modifier ()) {
1071                         snap_modifier_combo.set_active_text (_(modifiers[x].name));
1072                         break;
1073                 }
1074         }
1075
1076         label = manage (new Label (_("Ignore snap using")));
1077         label->set_name ("OptionsLabel");
1078         label->set_alignment (1.0, 0.5);
1079         
1080         keyboard_mouse_table.attach (*label, 0, 1, 2, 3, Gtk::FILL|Gtk::EXPAND, FILL);
1081         keyboard_mouse_table.attach (snap_modifier_combo, 1, 2, 2, 3, Gtk::FILL|Gtk::EXPAND, FILL);
1082 }
1083
1084 void
1085 OptionEditor::edit_modifier_chosen ()
1086 {
1087         string txt;
1088         
1089         txt = edit_modifier_combo.get_active_text();
1090
1091         for (int i = 0; modifiers[i].name; ++i) {
1092                 if (txt == _(modifiers[i].name)) {
1093                         Keyboard::set_edit_modifier (modifiers[i].modifier);
1094                         break;
1095                 }
1096         }
1097 }
1098
1099 void
1100 OptionEditor::delete_modifier_chosen ()
1101 {
1102         string txt;
1103         
1104         txt = delete_modifier_combo.get_active_text();
1105
1106         for (int i = 0; modifiers[i].name; ++i) {
1107                 if (txt == _(modifiers[i].name)) {
1108                         Keyboard::set_delete_modifier (modifiers[i].modifier);
1109                         break;
1110                 }
1111         }
1112 }
1113
1114 void
1115 OptionEditor::snap_modifier_chosen ()
1116 {
1117         string txt;
1118         
1119         txt = snap_modifier_combo.get_active_text();
1120
1121         for (int i = 0; modifiers[i].name; ++i) {
1122                 if (txt == _(modifiers[i].name)) {
1123                         Keyboard::set_snap_modifier (modifiers[i].modifier);
1124                         break;
1125                 }
1126         }
1127 }
1128
1129 void
1130 OptionEditor::delete_button_changed ()
1131 {
1132         Keyboard::set_delete_button ((guint) delete_button_adjustment.get_value());
1133 }
1134
1135 void
1136 OptionEditor::edit_button_changed ()
1137 {
1138         Keyboard::set_edit_button ((guint) edit_button_adjustment.get_value());
1139 }
1140
1141 void
1142 OptionEditor::fixup_combo_size (Gtk::ComboBoxText& combo, vector<string>& strings)
1143 {
1144         /* find the widest string */
1145
1146         string::size_type maxlen = 0;
1147         string maxstring;
1148
1149         for (vector<string>::iterator i = strings.begin(); i != strings.end(); ++i) {
1150                 string::size_type l;
1151
1152                 if ((l = (*i).length()) > maxlen) {
1153                         maxlen = l;
1154                         maxstring = *i;
1155                 }
1156         }
1157
1158         /* try to include ascenders and descenders */
1159
1160         if (maxstring.length() > 2) {
1161                 maxstring[0] = 'g';
1162                 maxstring[1] = 'l';
1163         }
1164
1165         const guint32 FUDGE = 10; // Combo's are stupid - they steal space from the entry for the button
1166
1167         set_size_request_to_display_given_text (combo, maxstring.c_str(), 10 + FUDGE, 10);
1168 }
1169