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