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