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