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