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