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