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