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