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