more goodies, including improved duplicate-region functionality
[ardour.git] / gtk2_ardour / ardour_ui2.cc
1 /*
2     Copyright (C) 1999 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 <fcntl.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <cerrno>
25 #include <iostream>
26 #include <cmath>
27
28 #include <sigc++/bind.h>
29 #include <pbd/error.h>
30 #include <pbd/basename.h>
31 #include <pbd/fastlog.h>
32 #include <gtkmm2ext/pix.h>
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/click_box.h>
35 #include <gtkmm2ext/tearoff.h>
36
37 #include <ardour/audioengine.h>
38 #include <ardour/ardour.h>
39 #include <ardour/route.h>
40
41 #include "ardour_ui.h"
42 #include "public_editor.h"
43 #include "audio_clock.h"
44 #include "actions.h"
45 #include "utils.h"
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
52 using namespace Gtk;
53 using namespace Glib;
54 using namespace sigc;
55
56 int     
57 ARDOUR_UI::setup_windows ()
58 {
59         using namespace Menu_Helpers;
60
61         if (create_editor ()) {
62                 error << _("UI: cannot setup editor") << endmsg;
63                 return -1;
64         }
65
66         if (create_mixer ()) {
67                 error << _("UI: cannot setup mixer") << endmsg;
68                 return -1;
69         }
70
71         /* all other dialogs are created conditionally */
72
73         we_have_dependents ();
74
75         setup_clock ();
76         setup_transport();
77         setup_adjustables ();
78         build_menu_bar ();
79
80         top_packer.pack_start (menu_bar_base, false, false);
81         top_packer.pack_start (transport_frame, false, false);
82
83         editor->add_toplevel_controls (top_packer);
84
85         return 0;
86 }
87
88 void
89 ARDOUR_UI::setup_adjustables ()
90 {
91         adjuster_table.set_homogeneous (true);
92
93         online_control_strings.push_back (_("MMC + Local"));
94         online_control_strings.push_back (_("MMC"));
95         online_control_strings.push_back (_("Local"));
96
97         online_control_button = new GlobalClickBox ("CONTROL",
98                                                     online_control_strings);
99
100         online_control_button->adjustment.signal_value_changed().connect(mem_fun(*this,&ARDOUR_UI::control_methods_adjusted));
101
102         mmc_id_strings.push_back ("1");
103         mmc_id_strings.push_back ("2");
104         mmc_id_strings.push_back ("3");
105         mmc_id_strings.push_back ("4");
106         mmc_id_strings.push_back ("5");
107         mmc_id_strings.push_back ("6");
108         mmc_id_strings.push_back ("7");
109         mmc_id_strings.push_back ("8");
110         mmc_id_strings.push_back ("9");
111
112         mmc_id_button = new GlobalClickBox (_("MMC ID"), mmc_id_strings);
113
114         mmc_id_button->adjustment.signal_value_changed().connect (mem_fun(*this,&ARDOUR_UI::mmc_device_id_adjusted));
115
116         adjuster_table.attach (*online_control_button, 0, 2, 1, 2, FILL|EXPAND, FILL, 5, 5);
117         adjuster_table.attach (*mmc_id_button, 2, 3, 1, 2, FILL, FILL, 5, 5);
118 }
119
120 static const gchar * loop_xpm[] = {
121 "19 19 3 1",
122 "       c None",
123 ".      c #000000",
124 "+      c #FFFFFF",
125 "       ...         ",
126 "       .+..        ",
127 "       .++..       ",
128 "     ...+++....    ",
129 "   ...++++++++...  ",
130 "  ..+++.+++..+++.. ",
131 " ..++...++.....++..",
132 " .++.. .+..   ..++.",
133 " .+..  ...     ..+.",
134 " .+.            .+.",
135 " .+..     ...  ..+.",
136 " .++..   ..+. ..++.",
137 " ..++.....++...++..",
138 "  ..+++..+++.+++.. ",
139 "   ...++++++++...  ",
140 "     ....+++...    ",
141 "        ..++.      ",
142 "         ..+.      ",
143 "          ...      "};
144
145 void
146 ARDOUR_UI::transport_stopped ()
147 {
148         stop_button.set_active (true);
149         
150         roll_button.set_active (false);
151         play_selection_button.set_active (false);
152         auto_loop_button.set_active (false);
153
154         shuttle_fract = 0;
155         shuttle_box.queue_draw ();
156
157         update_disk_space ();
158 }
159
160 static const double SHUTTLE_FRACT_SPEED1=0.48412291827; /* derived from A1,A2 */
161
162 void
163 ARDOUR_UI::transport_rolling ()
164 {
165         stop_button.set_active (false);
166         if (session->get_play_range()) {
167
168                 play_selection_button.set_active (true);
169                 roll_button.set_active (false);
170                 auto_loop_button.set_active (false);
171
172         } else if (session->get_auto_loop ()) {
173
174                 auto_loop_button.set_active (true);
175                 play_selection_button.set_active (false);
176                 roll_button.set_active (false);
177
178         } else {
179
180                 roll_button.set_active (true);
181                 play_selection_button.set_active (false);
182                 auto_loop_button.set_active (false);
183         }
184
185         /* reset shuttle controller */
186
187         shuttle_fract = SHUTTLE_FRACT_SPEED1;  /* speed = 1.0, believe it or not */
188         shuttle_box.queue_draw ();
189 }
190
191 void
192 ARDOUR_UI::transport_rewinding ()
193 {
194         stop_button.set_active(false);
195         roll_button.set_active (true);
196         play_selection_button.set_active (false);
197         auto_loop_button.set_active (false);
198 }
199
200 void
201 ARDOUR_UI::transport_forwarding ()
202 {
203         stop_button.set_active (false);
204         roll_button.set_active (true);
205         play_selection_button.set_active (false);
206         auto_loop_button.set_active (false);
207 }
208
209 void
210 ARDOUR_UI::setup_transport ()
211 {
212         transport_tearoff = manage (new TearOff (transport_tearoff_hbox));
213         transport_tearoff->set_name ("TransportBase");
214
215         transport_hbox.pack_start (*transport_tearoff, true, false);
216
217         transport_base.set_name ("TransportBase");
218         transport_base.add (transport_hbox);
219
220         transport_frame.set_shadow_type (SHADOW_OUT);
221         transport_frame.set_name ("BaseFrame");
222         transport_frame.add (transport_base);
223
224         transport_tearoff->Detach.connect (bind (mem_fun(*this, &ARDOUR_UI::detach_tearoff), static_cast<Box*>(&top_packer), 
225                                                  static_cast<Widget*>(&transport_frame)));
226         transport_tearoff->Attach.connect (bind (mem_fun(*this, &ARDOUR_UI::reattach_tearoff), static_cast<Box*> (&top_packer), 
227                                                  static_cast<Widget*> (&transport_frame), 1));
228
229         shuttle_box.set_name ("TransportButton");
230         goto_start_button.set_name ("TransportButton");
231         goto_end_button.set_name ("TransportButton");
232         roll_button.set_name ("TransportButton");
233         stop_button.set_name ("TransportButton");
234         play_selection_button.set_name ("TransportButton");
235         rec_button.set_name ("TransportRecButton");
236         auto_loop_button.set_name ("TransportButton");
237         auto_return_button.set_name ("TransportButton");
238         auto_play_button.set_name ("TransportButton");
239         auto_input_button.set_name ("TransportButton");
240         punch_in_button.set_name ("TransportButton");
241         punch_out_button.set_name ("TransportButton");
242         click_button.set_name ("TransportButton");
243         time_master_button.set_name ("TransportButton");
244
245         vector<Gdk::Color> colors;
246         Gdk::Color c;
247
248         /* record button has 3 color states, so we set 2 extra here */
249         set_color(c, rgba_from_style ("TransportRecButton", 0xff, 0, 0, 0, "bg", Gtk::STATE_PRELIGHT, false ));
250         colors.push_back (c);
251         
252         set_color(c, rgba_from_style ("TransportRecButton", 0xff, 0, 0, 0, "bg", Gtk::STATE_ACTIVE, false ));
253         colors.push_back (c);
254         
255         rec_button.set_colors (colors);
256         colors.clear ();
257         
258         /* other buttons get 2 color states, so add one here */
259         set_color(c, rgba_from_style ("TransportButton", 0x7f, 0xff, 0x7f, 0, "bg", Gtk::STATE_ACTIVE, false ));
260         colors.push_back (c);
261
262         stop_button.set_colors (colors);
263         roll_button.set_colors (colors);
264         auto_loop_button.set_colors (colors);
265         play_selection_button.set_colors (colors);
266         goto_start_button.set_colors (colors);
267         goto_end_button.set_colors (colors);
268         
269         Widget* w;
270
271         stop_button.set_active (true);
272
273         w = manage (new Image (Stock::MEDIA_PREVIOUS, ICON_SIZE_BUTTON));
274         w->show();
275         goto_start_button.add (*w);
276         w = manage (new Image (Stock::MEDIA_NEXT, ICON_SIZE_BUTTON));
277         w->show();
278         goto_end_button.add (*w);
279         w = manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_BUTTON));
280         w->show();
281         roll_button.add (*w);
282         w = manage (new Image (Stock::MEDIA_STOP, ICON_SIZE_BUTTON));
283         w->show();
284         stop_button.add (*w);
285         w = manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_BUTTON));
286         w->show();
287         play_selection_button.add (*w);
288         w = manage (new Image (Stock::MEDIA_RECORD, ICON_SIZE_BUTTON));
289         w->show();
290         rec_button.add (*w);
291         w = manage (new Image (Gdk::Pixbuf::create_from_xpm_data(loop_xpm)));
292         w->show();
293         auto_loop_button.add (*w);
294
295         RefPtr<Action> act;
296
297         act = ActionManager::get_action (X_("Transport"), X_("Stop"));
298         act->connect_proxy (stop_button);
299         act = ActionManager::get_action (X_("Transport"), X_("Roll"));
300         act->connect_proxy (roll_button);
301         act = ActionManager::get_action (X_("Transport"), X_("Record"));
302         act->connect_proxy (rec_button);
303         act = ActionManager::get_action (X_("Transport"), X_("GotoStart"));
304         act->connect_proxy (goto_start_button);
305         act = ActionManager::get_action (X_("Transport"), X_("GotoEnd"));
306         act->connect_proxy (goto_end_button);
307         act = ActionManager::get_action (X_("Transport"), X_("Loop"));
308         act->connect_proxy (auto_loop_button);
309         act = ActionManager::get_action (X_("Transport"), X_("PlaySelection"));
310         act->connect_proxy (play_selection_button);
311         act = ActionManager::get_action (X_("Transport"), X_("ToggleTimeMaster"));
312         act->connect_proxy (time_master_button);
313
314         ARDOUR_UI::instance()->tooltips().set_tip (roll_button, _("Play from playhead"));
315         ARDOUR_UI::instance()->tooltips().set_tip (stop_button, _("Stop playback"));
316         ARDOUR_UI::instance()->tooltips().set_tip (play_selection_button, _("Play range/selection"));
317         ARDOUR_UI::instance()->tooltips().set_tip (goto_start_button, _("Go to start of session"));
318         ARDOUR_UI::instance()->tooltips().set_tip (goto_end_button, _("Go to end of session"));
319         ARDOUR_UI::instance()->tooltips().set_tip (auto_loop_button, _("Play loop range"));
320         ARDOUR_UI::instance()->tooltips().set_tip (auto_return_button, _("Return to last playback start when stopped"));
321         ARDOUR_UI::instance()->tooltips().set_tip (auto_play_button, _("Start playback after any locate"));
322         ARDOUR_UI::instance()->tooltips().set_tip (auto_input_button, _("Be sensible about input monitoring"));
323         ARDOUR_UI::instance()->tooltips().set_tip (punch_in_button, _("Start recording at auto-punch start"));
324         ARDOUR_UI::instance()->tooltips().set_tip (punch_out_button, _("Stop recording at auto-punch end"));
325         ARDOUR_UI::instance()->tooltips().set_tip (click_button, _("Enable/Disable audio click"));
326         ARDOUR_UI::instance()->tooltips().set_tip (sync_option_combo, _("Positional sync source"));
327         ARDOUR_UI::instance()->tooltips().set_tip (time_master_button, _("Does Ardour control the time?"));
328         ARDOUR_UI::instance()->tooltips().set_tip (shuttle_box, _("Shuttle speed control"));
329         ARDOUR_UI::instance()->tooltips().set_tip (shuttle_units_button, _("Select semitones or %%-age for speed display"));
330         ARDOUR_UI::instance()->tooltips().set_tip (speed_display_box, _("Current transport speed"));
331         
332         shuttle_box.set_flags (CAN_FOCUS);
333         shuttle_box.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::POINTER_MOTION_MASK);
334         shuttle_box.set_size_request (100, 15);
335
336         shuttle_box.signal_button_press_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_button_press));
337         shuttle_box.signal_button_release_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_button_release));
338         shuttle_box.signal_motion_notify_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_motion));
339         shuttle_box.signal_expose_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_expose));
340
341         /* clocks, etc. */
342
343         ARDOUR_UI::Clock.connect (bind (mem_fun (primary_clock, &AudioClock::set), false));
344         ARDOUR_UI::Clock.connect (bind (mem_fun (secondary_clock, &AudioClock::set), false));
345
346         primary_clock.set_mode (AudioClock::SMPTE);
347         secondary_clock.set_mode (AudioClock::BBT);
348
349         primary_clock.ValueChanged.connect (mem_fun(*this, &ARDOUR_UI::primary_clock_value_changed));
350         secondary_clock.ValueChanged.connect (mem_fun(*this, &ARDOUR_UI::secondary_clock_value_changed));
351
352         ARDOUR_UI::instance()->tooltips().set_tip (primary_clock, _("Primary clock"));
353         ARDOUR_UI::instance()->tooltips().set_tip (secondary_clock, _("secondary clock"));
354
355         ActionManager::get_action ("Transport", "ToggleAutoReturn")->connect_proxy (auto_return_button);
356         ActionManager::get_action ("Transport", "ToggleAutoPlay")->connect_proxy (auto_play_button);
357         ActionManager::get_action ("Transport", "ToggleAutoInput")->connect_proxy (auto_input_button);
358         ActionManager::get_action ("Transport", "ToggleClick")->connect_proxy (click_button);
359         ActionManager::get_action ("Transport", "TogglePunchIn")->connect_proxy (punch_in_button);
360         ActionManager::get_action ("Transport", "TogglePunchOut")->connect_proxy (punch_out_button);
361
362         preroll_button.unset_flags (CAN_FOCUS);
363         preroll_button.set_events (preroll_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
364         preroll_button.set_name ("TransportButton");
365
366         postroll_button.unset_flags (CAN_FOCUS);
367         postroll_button.set_events (postroll_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
368         postroll_button.set_name ("TransportButton");
369
370         preroll_clock.set_mode (AudioClock::MinSec);
371         preroll_clock.set_name ("TransportClockDisplay");
372         postroll_clock.set_mode (AudioClock::MinSec);
373         postroll_clock.set_name ("TransportClockDisplay");
374
375         /* alerts */
376
377         /* CANNOT bind these to clicked or toggled, must use pressed or released */
378
379         solo_alert_button.set_name ("TransportSoloAlert");
380         solo_alert_button.signal_pressed().connect (mem_fun(*this,&ARDOUR_UI::solo_alert_toggle));
381         auditioning_alert_button.set_name ("TransportAuditioningAlert");
382         auditioning_alert_button.signal_pressed().connect (mem_fun(*this,&ARDOUR_UI::audition_alert_toggle));
383
384         alert_box.pack_start (solo_alert_button);
385         alert_box.pack_start (auditioning_alert_button);
386
387         transport_tearoff_hbox.set_border_width (5);
388
389         transport_tearoff_hbox.pack_start (goto_start_button, false, false);
390         transport_tearoff_hbox.pack_start (goto_end_button, false, false);
391
392         Frame* sframe = manage (new Frame);
393         VBox*  svbox = manage (new VBox);
394         HBox*  shbox = manage (new HBox);
395
396         sframe->set_shadow_type (SHADOW_IN);
397         sframe->add (shuttle_box);
398
399         shuttle_box.set_name (X_("ShuttleControl"));
400
401         speed_display_box.add (speed_display_label);
402         speed_display_box.set_name (X_("ShuttleDisplay"));
403
404         shuttle_units_button.set_name (X_("ShuttleButton"));
405         shuttle_units_button.signal_clicked().connect (mem_fun(*this, &ARDOUR_UI::shuttle_unit_clicked));
406         
407         shuttle_style_button.set_name (X_("ShuttleButton"));
408
409         vector<string> shuttle_strings;
410         shuttle_strings.push_back (_("sprung"));
411         shuttle_strings.push_back (_("wheel"));
412         set_popdown_strings (shuttle_style_button, shuttle_strings);
413         shuttle_style_button.signal_changed().connect (mem_fun (*this, &ARDOUR_UI::shuttle_style_changed));
414
415         Frame* sdframe = manage (new Frame);
416
417         sdframe->set_shadow_type (SHADOW_IN);
418         sdframe->add (speed_display_box);
419
420         mtc_port_changed ();
421         sync_option_combo.set_active_text (positional_sync_strings.front());
422         sync_option_combo.signal_changed().connect (mem_fun (*this, &ARDOUR_UI::sync_option_changed));
423
424         shbox->pack_start (*sdframe, false, false);
425         shbox->pack_start (shuttle_units_button, true, true);
426         shbox->pack_start (shuttle_style_button, false, false);
427         
428         svbox->pack_start (*sframe, false, false);
429         svbox->pack_start (*shbox, false, false);
430
431         transport_tearoff_hbox.pack_start (*svbox, false, false, 5);
432
433         transport_tearoff_hbox.pack_start (auto_loop_button, false, false);
434         transport_tearoff_hbox.pack_start (play_selection_button, false, false);
435         transport_tearoff_hbox.pack_start (roll_button, false, false);
436         transport_tearoff_hbox.pack_start (stop_button, false, false);
437         transport_tearoff_hbox.pack_start (rec_button, false, false, 10);
438
439         transport_tearoff_hbox.pack_start (primary_clock, false, false, 5);
440         transport_tearoff_hbox.pack_start (secondary_clock, false, false, 5);
441
442         transport_tearoff_hbox.pack_start (sync_option_combo, false, false);
443         transport_tearoff_hbox.pack_start (time_master_button, false, false);
444         transport_tearoff_hbox.pack_start (punch_in_button, false, false);
445         transport_tearoff_hbox.pack_start (punch_out_button, false, false);
446         transport_tearoff_hbox.pack_start (auto_input_button, false, false);
447         transport_tearoff_hbox.pack_start (auto_return_button, false, false);
448         transport_tearoff_hbox.pack_start (auto_play_button, false, false);
449         transport_tearoff_hbox.pack_start (click_button, false, false);
450         
451         /* desensitize */
452
453         set_transport_sensitivity (false);
454
455 //      transport_tearoff_hbox.pack_start (preroll_button, false, false);
456 //      transport_tearoff_hbox.pack_start (preroll_clock, false, false);
457
458 //      transport_tearoff_hbox.pack_start (postroll_button, false, false);
459 //      transport_tearoff_hbox.pack_start (postroll_clock, false, false);
460
461         transport_tearoff_hbox.pack_start (alert_box, false, false, 5);
462 }
463
464 void
465 ARDOUR_UI::setup_clock ()
466 {
467         ARDOUR_UI::Clock.connect (bind (mem_fun (big_clock, &AudioClock::set), false));
468         
469         big_clock_window = new Window (WINDOW_TOPLEVEL);
470         
471         big_clock_window->set_border_width (0);
472         big_clock_window->add  (big_clock);
473         big_clock_window->set_title (_("ardour: clock"));
474         big_clock_window->set_type_hint (Gdk::WINDOW_TYPE_HINT_MENU);
475         big_clock_window->signal_realize().connect (bind (sigc::ptr_fun (set_decoration), big_clock_window,  (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
476         big_clock_window->signal_unmap().connect (bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleBigClock")));
477
478         manage_window (*big_clock_window);
479 }
480
481 void
482 ARDOUR_UI::manage_window (Window& win)
483 {
484         win.signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), &win));
485         win.signal_enter_notify_event().connect (bind (mem_fun (Keyboard::the_keyboard(), &Keyboard::enter_window), &win));
486         win.signal_leave_notify_event().connect (bind (mem_fun (Keyboard::the_keyboard(), &Keyboard::leave_window), &win));
487 }
488
489 void
490 ARDOUR_UI::detach_tearoff (Box* b, Widget* w)
491 {
492 //      editor->ensure_float (transport_tearoff->tearoff_window());
493         b->remove (*w);
494 }
495
496 void
497 ARDOUR_UI::reattach_tearoff (Box* b, Widget* w, int32_t n)
498 {
499         b->pack_start (*w);
500         b->reorder_child (*w, n);
501 }
502
503 void
504 ARDOUR_UI::soloing_changed (bool onoff)
505 {
506         if (solo_alert_button.get_active() != onoff) {
507                 solo_alert_button.set_active (onoff);
508         }
509 }
510
511 void
512 ARDOUR_UI::_auditioning_changed (bool onoff)
513 {
514         if (auditioning_alert_button.get_active() != onoff) {
515                 auditioning_alert_button.set_active (onoff);
516                 set_transport_sensitivity (!onoff);
517         }
518 }
519
520 void
521 ARDOUR_UI::auditioning_changed (bool onoff)
522 {
523         Gtkmm2ext::UI::instance()->call_slot(bind (mem_fun(*this, &ARDOUR_UI::_auditioning_changed), onoff));
524 }
525
526 void
527 ARDOUR_UI::audition_alert_toggle ()
528 {
529         if (session) {
530                 session->cancel_audition();
531         }
532 }
533
534 void
535 ARDOUR_UI::solo_alert_toggle ()
536 {
537         if (session) {
538                 session->set_all_solo (!session->soloing());
539         }
540 }
541
542 void
543 ARDOUR_UI::solo_blink (bool onoff)
544 {
545         if (session == 0) {
546                 return;
547         }
548         
549         if (session->soloing()) {
550                 if (onoff) {
551                         solo_alert_button.set_state (STATE_ACTIVE);
552                 } else {
553                         solo_alert_button.set_state (STATE_NORMAL);
554                 }
555         } else {
556                 solo_alert_button.set_active (false);
557                 solo_alert_button.set_state (STATE_NORMAL);
558         }
559 }
560
561 void
562 ARDOUR_UI::audition_blink (bool onoff)
563 {
564         if (session == 0) {
565                 return;
566         }
567         
568         if (session->is_auditioning()) {
569                 if (onoff) {
570                         auditioning_alert_button.set_state (STATE_ACTIVE);
571                 } else {
572                         auditioning_alert_button.set_state (STATE_NORMAL);
573                 }
574         } else {
575                 auditioning_alert_button.set_active (false);
576                 auditioning_alert_button.set_state (STATE_NORMAL);
577         }
578 }
579
580
581 gint
582 ARDOUR_UI::shuttle_box_button_press (GdkEventButton* ev)
583 {
584         if (!session) {
585                 return TRUE;
586         }
587
588         switch (ev->button) {
589         case 1:
590                 shuttle_box.add_modal_grab ();
591                 shuttle_grabbed = true;
592                 mouse_shuttle (ev->x, true);
593                 break;
594
595         case 2:
596         case 3:
597                 return TRUE;
598                 break;
599         }
600
601         return TRUE;
602 }
603
604 gint
605 ARDOUR_UI::shuttle_box_button_release (GdkEventButton* ev)
606 {
607         if (!session) {
608                 return TRUE;
609         }
610
611         switch (ev->button) {
612         case 1:
613                 mouse_shuttle (ev->x, true);
614                 shuttle_grabbed = false;
615                 shuttle_box.remove_modal_grab ();
616                 if (shuttle_behaviour == Sprung) {
617                         shuttle_fract = SHUTTLE_FRACT_SPEED1;
618                         session->request_transport_speed (1.0);
619                         shuttle_box.queue_draw ();
620                 }
621                 return TRUE;
622
623         case 2:
624                 if (session->transport_rolling()) {
625                         shuttle_fract = SHUTTLE_FRACT_SPEED1;
626                         session->request_transport_speed (1.0);
627                 } else {
628                         shuttle_fract = 0;
629                 }
630                 shuttle_box.queue_draw ();
631                 return TRUE;
632
633         case 3:
634                 return TRUE;
635                 
636         case 4:
637                 shuttle_fract += 0.005;
638                 break;
639         case 5:
640                 shuttle_fract -= 0.005;
641                 break;
642         }
643
644         use_shuttle_fract (true);
645
646         return TRUE;
647 }
648
649 gint
650 ARDOUR_UI::shuttle_box_motion (GdkEventMotion* ev)
651 {
652         if (!session || !shuttle_grabbed) {
653                 return TRUE;
654         }
655
656         return mouse_shuttle (ev->x, false);
657 }
658
659 gint
660 ARDOUR_UI::mouse_shuttle (double x, bool force)
661 {
662         double half_width = shuttle_box.get_width() / 2.0;
663         double distance = x - half_width;
664
665         if (distance > 0) {
666                 distance = min (distance, half_width);
667         } else {
668                 distance = max (distance, -half_width);
669         }
670
671         shuttle_fract = distance / half_width;
672         use_shuttle_fract (force);
673         return TRUE;
674 }
675
676 void
677 ARDOUR_UI::use_shuttle_fract (bool force)
678 {
679         struct timeval now;
680         struct timeval diff;
681         
682         /* do not attempt to submit a motion-driven transport speed request
683            more than once per process cycle.
684          */
685
686         gettimeofday (&now, 0);
687         timersub (&now, &last_shuttle_request, &diff);
688
689         if (!force && (diff.tv_usec + (diff.tv_sec * 1000000)) < engine->usecs_per_cycle()) {
690                 return;
691         }
692         
693         last_shuttle_request = now;
694
695         bool neg = (shuttle_fract < 0.0);
696
697         double fract = 1 - sqrt (1 - (shuttle_fract * shuttle_fract)); // Formula A1
698
699         if (neg) {
700                 fract = -fract;
701         }
702
703         session->request_transport_speed (8.0 * fract); // Formula A2
704         shuttle_box.queue_draw ();
705 }
706
707 gint
708 ARDOUR_UI::shuttle_box_expose (GdkEventExpose* event)
709 {
710         gint x;
711         Glib::RefPtr<Gdk::Window> win (shuttle_box.get_window());
712
713         /* redraw the background */
714
715         win->draw_rectangle (shuttle_box.get_style()->get_bg_gc (shuttle_box.get_state()),
716                              true,
717                              event->area.x, event->area.y,
718                              event->area.width, event->area.height);
719
720
721         x = (gint) floor ((shuttle_box.get_width() / 2.0) + (0.5 * (shuttle_box.get_width() * shuttle_fract)));
722
723         /* draw line */
724
725         win->draw_line (shuttle_box.get_style()->get_fg_gc (shuttle_box.get_state()),
726                         x,
727                         0,
728                         x,
729                         shuttle_box.get_height());
730         return TRUE;
731 }
732
733 void
734 ARDOUR_UI::shuttle_unit_clicked ()
735 {
736         if (shuttle_unit_menu == 0) {
737                 shuttle_unit_menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/ShuttleUnitPopup"));
738         }
739         shuttle_unit_menu->popup (1, 0);
740 }
741
742 void
743 ARDOUR_UI::set_shuttle_units (ShuttleUnits u)
744 {
745         switch ((shuttle_units = u)) {
746         case Percentage:
747                 static_cast<Label*>(shuttle_units_button.get_child())->set_text ("% ");
748                 break;
749         case Semitones:
750                 static_cast<Label*>(shuttle_units_button.get_child())->set_text (_("st"));
751                 break;
752         }
753 }
754
755 void
756 ARDOUR_UI::shuttle_style_changed ()
757 {
758         ustring str = shuttle_style_button.get_active_text ();
759
760         if (str == _("sprung")) {
761                 set_shuttle_behaviour (Sprung);
762         } else if (str == _("wheel")) {
763                 set_shuttle_behaviour (Wheel);
764         }
765 }
766
767
768 void
769 ARDOUR_UI::set_shuttle_behaviour (ShuttleBehaviour b)
770 {
771         switch ((shuttle_behaviour = b)) {
772         case Sprung:
773                 shuttle_style_button.set_active_text (_("sprung"));
774                 shuttle_fract = 0.0;
775                 shuttle_box.queue_draw ();
776                 if (session) {
777                         if (session->transport_rolling()) {
778                                 shuttle_fract = SHUTTLE_FRACT_SPEED1;
779                                 session->request_transport_speed (1.0);
780                         }
781                 }
782                 break;
783         case Wheel:
784                 shuttle_style_button.set_active_text (_("wheel"));
785                 break;
786         }
787 }
788
789 void
790 ARDOUR_UI::update_speed_display ()
791 {
792         if (!session) {
793                 if (last_speed_displayed != 0) {
794                         speed_display_label.set_text (_("stopped"));
795                         last_speed_displayed = 0;
796                 }
797                 return;
798         }
799
800         char buf[32];
801         float x = session->transport_speed ();
802
803         if (x != last_speed_displayed) {
804
805                 if (x != 0) {
806                         if (shuttle_units == Percentage) {
807                                 snprintf (buf, sizeof (buf), "%.4f", x);
808                         } else {
809                                 if (x < 0) {
810                                         snprintf (buf, sizeof (buf), "< %.1f", 12.0 * fast_log2 (-x));
811                                 } else {
812                                         snprintf (buf, sizeof (buf), "> %.1f", 12.0 * fast_log2 (x));
813                                 }
814                         }
815                         speed_display_label.set_text (buf);
816                 } else {
817                         speed_display_label.set_text (_("stopped"));
818                 }
819
820                 last_speed_displayed = x;
821         }
822 }       
823         
824 void
825 ARDOUR_UI::set_transport_sensitivity (bool yn)
826 {
827         ActionManager::set_sensitive (ActionManager::transport_sensitive_actions, yn);
828         shuttle_box.set_sensitive (yn);
829 }
830
831 void
832 ARDOUR_UI::editor_realized ()
833 {
834         set_size_request_to_display_given_text (speed_display_box, _("stopped"), 2, 2);
835         /* XXX: this should really be saved in instant.xml or something similar and restored from there */
836         shuttle_style_button.set_active_text (_("sprung"));
837         const guint32 FUDGE = 20; // Combo's are stupid - they steal space from the entry for the button
838         set_size_request_to_display_given_text (shuttle_style_button, _("sprung"), 2+FUDGE, 10);
839 }
840
841 void
842 ARDOUR_UI::sync_option_changed ()
843 {
844         string which;
845
846         if (session == 0) {
847                 return;
848         }
849
850         which = sync_option_combo.get_active_text();
851
852         if (which == positional_sync_strings[Session::None]) {
853                 session->request_slave_source (Session::None);
854         } else if (which == positional_sync_strings[Session::MTC]) {
855                 session->request_slave_source (Session::MTC);
856         } else if (which == positional_sync_strings[Session::JACK]) {
857                 session->request_slave_source (Session::JACK);
858         } 
859 }