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