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