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