change the way MIDI note colors are defined (2 3-point color ranges, one for selected...
[ardour2.git] / gtk2_ardour / ardour_ui2.cc
blob9991a1dc1427924e3e4ada4e8d2e483c34e3c4cc
1 /*
2 Copyright (C) 1999 Paul Davis
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.
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.
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.
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <cerrno>
24 #include <iostream>
25 #include <cmath>
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>
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"
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"
49 #include "i18n.h"
51 using namespace std;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Gtkmm2ext;
55 using namespace Gtk;
56 using namespace Glib;
58 int
59 ARDOUR_UI::setup_windows ()
61 if (create_editor ()) {
62 error << _("UI: cannot setup editor") << endmsg;
63 return -1;
66 if (create_mixer ()) {
67 error << _("UI: cannot setup mixer") << endmsg;
68 return -1;
71 /* all other dialogs are created conditionally */
73 we_have_dependents ();
75 theme_manager->signal_unmap().connect (sigc::bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleThemeManager")));
77 #ifdef TOP_MENUBAR
78 HBox* status_bar_packer = manage (new HBox);
79 EventBox* status_bar_event_box = manage (new EventBox);
81 status_bar_event_box->add (status_bar_label);
82 status_bar_event_box->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
83 status_bar_label.set_size_request (300, -1);
84 status_bar_packer->pack_start (*status_bar_event_box, true, true, 6);
85 status_bar_packer->pack_start (error_log_button, false, false);
87 status_bar_label.show ();
88 status_bar_event_box->show ();
89 status_bar_packer->show ();
90 error_log_button.show ();
92 error_log_button.signal_clicked().connect (mem_fun (*this, &UI::toggle_errors));
93 status_bar_event_box->signal_button_press_event().connect (mem_fun (*this, &ARDOUR_UI::status_bar_button_press));
95 editor->get_status_bar_packer().pack_start (*status_bar_packer, true, true);
96 editor->get_status_bar_packer().pack_start (menu_bar_base, false, false, 6);
97 #else
98 top_packer.pack_start (menu_bar_base, false, false);
99 #endif
101 top_packer.pack_start (transport_frame, false, false);
103 editor->add_toplevel_controls (top_packer);
105 setup_clock ();
106 setup_transport();
107 build_menu_bar ();
109 setup_tooltips ();
111 return 0;
114 void
115 ARDOUR_UI::setup_tooltips ()
117 set_tip (roll_button, _("Play from playhead"));
118 set_tip (stop_button, _("Stop playback"));
119 set_tip (rec_button, _("Toggle record"));
120 set_tip (play_selection_button, _("Play range/selection"));
121 set_tip (join_play_range_button, _("Always play range/selection"));
122 set_tip (goto_start_button, _("Go to start of session"));
123 set_tip (goto_end_button, _("Go to end of session"));
124 set_tip (auto_loop_button, _("Play loop range"));
126 set_tip (auto_return_button, _("Return to last playback start when stopped"));
127 set_tip (auto_play_button, _("Start playback after any locate"));
128 set_tip (auto_input_button, _("Be sensible about input monitoring"));
129 set_tip (punch_in_button, _("Start recording at auto-punch start"));
130 set_tip (punch_out_button, _("Stop recording at auto-punch end"));
131 set_tip (click_button, _("Enable/Disable audio click"));
132 set_tip (sync_button, _("Enable/Disable external positional sync"));
133 set_tip (time_master_button, string_compose (_("Does %1 control the time?"), PROGRAM_NAME));
134 set_tip (shuttle_box, _("Shuttle speed control"));
135 set_tip (shuttle_units_button, _("Select semitones or %%-age for speed display"));
136 set_tip (speed_display_box, _("Current transport speed"));
137 set_tip (solo_alert_button, _("When active, something is soloed.\nClick to de-solo everything"));
138 set_tip (auditioning_alert_button, _("When active, auditioning is taking place\nClick to stop the audition"));
139 set_tip (primary_clock, _("Primary clock"));
140 set_tip (secondary_clock, _("secondary clock"));
142 editor->setup_tooltips ();
145 bool
146 ARDOUR_UI::status_bar_button_press (GdkEventButton* ev)
148 bool handled = false;
150 switch (ev->button) {
151 case 1:
152 status_bar_label.set_text ("");
153 handled = true;
154 break;
155 default:
156 break;
159 return handled;
162 void
163 ARDOUR_UI::display_message (const char *prefix, gint prefix_len, RefPtr<TextBuffer::Tag> ptag, RefPtr<TextBuffer::Tag> mtag, const char *msg)
165 ustring text;
167 UI::display_message (prefix, prefix_len, ptag, mtag, msg);
168 #ifdef TOP_MENUBAR
170 if (strcmp (prefix, _("[ERROR]: ")) == 0) {
171 text = "<span color=\"red\" weight=\"bold\">";
172 } else if (strcmp (prefix, _("[WARNING]: ")) == 0) {
173 text = "<span color=\"yellow\" weight=\"bold\">";
174 } else if (strcmp (prefix, _("[INFO]: ")) == 0) {
175 text = "<span color=\"green\" weight=\"bold\">";
176 } else {
177 text = "<span color=\"white\" weight=\"bold\">???";
180 text += prefix;
181 text += "</span>";
182 text += msg;
184 status_bar_label.set_markup (text);
185 #endif
188 XMLNode*
189 ARDOUR_UI::tearoff_settings (const char* name) const
191 XMLNode* ui_node = Config->extra_xml(X_("UI"));
193 if (ui_node) {
194 XMLNode* tearoff_node = ui_node->child (X_("Tearoffs"));
195 if (tearoff_node) {
196 XMLNode* mnode = tearoff_node->child (name);
197 return mnode;
201 return 0;
204 void
205 ARDOUR_UI::setup_transport ()
207 transport_tearoff = manage (new TearOff (transport_tearoff_hbox));
208 transport_tearoff->set_name ("TransportBase");
209 transport_tearoff->tearoff_window().signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), &transport_tearoff->tearoff_window()), false);
211 if (Profile->get_sae()) {
212 transport_tearoff->set_can_be_torn_off (false);
215 transport_hbox.pack_start (*transport_tearoff, true, false);
217 transport_base.set_name ("TransportBase");
218 transport_base.add (transport_hbox);
220 transport_frame.set_shadow_type (SHADOW_OUT);
221 transport_frame.set_name ("BaseFrame");
222 transport_frame.add (transport_base);
224 transport_tearoff->Detach.connect (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::detach_tearoff), static_cast<Box*>(&top_packer),
225 static_cast<Widget*>(&transport_frame)));
226 transport_tearoff->Attach.connect (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::reattach_tearoff), static_cast<Box*> (&top_packer),
227 static_cast<Widget*> (&transport_frame), 1));
228 transport_tearoff->Hidden.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->Visible.connect (sigc::bind (sigc::mem_fun(*this, &ARDOUR_UI::reattach_tearoff), static_cast<Box*> (&top_packer),
231 static_cast<Widget*> (&transport_frame), 1));
233 shuttle_box.set_name ("TransportButton");
234 goto_start_button.set_name ("TransportButton");
235 goto_end_button.set_name ("TransportButton");
236 roll_button.set_name ("TransportButton");
237 stop_button.set_name ("TransportButton");
238 play_selection_button.set_name ("TransportButton");
239 rec_button.set_name ("TransportRecButton");
240 auto_loop_button.set_name ("TransportButton");
241 join_play_range_button.set_name ("TransportButton");
243 auto_return_button.set_name ("TransportButton");
244 auto_play_button.set_name ("TransportButton");
245 auto_input_button.set_name ("TransportButton");
246 punch_in_button.set_name ("TransportButton");
247 punch_out_button.set_name ("TransportButton");
248 click_button.set_name ("TransportButton");
249 time_master_button.set_name ("TransportButton");
250 sync_button.set_name ("TransportSyncButton");
252 stop_button.set_size_request(29, -1);
253 roll_button.set_size_request(29, -1);
254 auto_loop_button.set_size_request(29, -1);
255 play_selection_button.set_size_request(29, -1);
256 goto_start_button.set_size_request(29, -1);
257 goto_end_button.set_size_request(29, -1);
258 rec_button.set_size_request(29, -1);
260 Widget* w;
262 stop_button.set_visual_state (1);
264 w = manage (new Image (get_icon (X_("transport_start"))));
265 w->show();
266 goto_start_button.add (*w);
267 w = manage (new Image (get_icon (X_("transport_end"))));
268 w->show();
269 goto_end_button.add (*w);
270 w = manage (new Image (get_icon (X_("transport_play"))));
271 w->show();
272 roll_button.add (*w);
273 w = manage (new Image (get_icon (X_("transport_stop"))));
274 w->show();
275 stop_button.add (*w);
276 w = manage (new Image (get_icon (X_("transport_range"))));
277 w->show();
278 play_selection_button.add (*w);
279 w = manage (new Image (get_icon (X_("transport_record"))));
280 w->show();
281 rec_button.add (*w);
282 w = manage (new Image (get_icon (X_("transport_loop"))));
283 w->show();
284 auto_loop_button.add (*w);
285 w = manage (new Image (get_icon (X_("join_tools"))));
286 w->show ();
287 join_play_range_button.add (*w);
289 RefPtr<Action> act;
291 act = ActionManager::get_action (X_("Transport"), X_("Stop"));
292 act->connect_proxy (stop_button);
293 act = ActionManager::get_action (X_("Transport"), X_("Roll"));
294 act->connect_proxy (roll_button);
295 act = ActionManager::get_action (X_("Transport"), X_("Record"));
296 act->connect_proxy (rec_button);
297 act = ActionManager::get_action (X_("Transport"), X_("GotoStart"));
298 act->connect_proxy (goto_start_button);
299 act = ActionManager::get_action (X_("Transport"), X_("GotoEnd"));
300 act->connect_proxy (goto_end_button);
301 act = ActionManager::get_action (X_("Transport"), X_("Loop"));
302 act->connect_proxy (auto_loop_button);
303 act = ActionManager::get_action (X_("Transport"), X_("PlaySelection"));
304 act->connect_proxy (play_selection_button);
305 act = ActionManager::get_action (X_("Transport"), X_("ToggleTimeMaster"));
306 act->connect_proxy (time_master_button);
307 act = ActionManager::get_action (X_("Transport"), X_("ToggleExternalSync"));
308 act->connect_proxy (sync_button);
310 shuttle_box.set_flags (CAN_FOCUS);
311 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);
312 shuttle_box.set_size_request (100, 15);
314 shuttle_box.signal_button_press_event().connect (sigc::mem_fun(*this, &ARDOUR_UI::shuttle_box_button_press));
315 shuttle_box.signal_button_release_event().connect (sigc::mem_fun(*this, &ARDOUR_UI::shuttle_box_button_release));
316 shuttle_box.signal_scroll_event().connect (sigc::mem_fun(*this, &ARDOUR_UI::shuttle_box_scroll));
317 shuttle_box.signal_motion_notify_event().connect (sigc::mem_fun(*this, &ARDOUR_UI::shuttle_box_motion));
318 shuttle_box.signal_expose_event().connect (sigc::mem_fun(*this, &ARDOUR_UI::shuttle_box_expose));
320 /* clocks, etc. */
322 ARDOUR_UI::Clock.connect (sigc::bind (sigc::mem_fun (primary_clock, &AudioClock::set), 'p'));
323 ARDOUR_UI::Clock.connect (sigc::bind (sigc::mem_fun (secondary_clock, &AudioClock::set), 's'));
325 primary_clock.ValueChanged.connect (sigc::mem_fun(*this, &ARDOUR_UI::primary_clock_value_changed));
326 secondary_clock.ValueChanged.connect (sigc::mem_fun(*this, &ARDOUR_UI::secondary_clock_value_changed));
327 big_clock.ValueChanged.connect (sigc::mem_fun(*this, &ARDOUR_UI::big_clock_value_changed));
329 ActionManager::get_action ("Transport", "ToggleAutoReturn")->connect_proxy (auto_return_button);
330 ActionManager::get_action ("Transport", "ToggleAutoPlay")->connect_proxy (auto_play_button);
331 ActionManager::get_action ("Transport", "ToggleAutoInput")->connect_proxy (auto_input_button);
332 ActionManager::get_action ("Transport", "ToggleClick")->connect_proxy (click_button);
333 ActionManager::get_action ("Transport", "TogglePunchIn")->connect_proxy (punch_in_button);
334 ActionManager::get_action ("Transport", "TogglePunchOut")->connect_proxy (punch_out_button);
336 preroll_button.set_name ("TransportButton");
337 postroll_button.set_name ("TransportButton");
339 preroll_clock.set_mode (AudioClock::MinSec);
340 preroll_clock.set_name ("TransportClockDisplay");
341 postroll_clock.set_mode (AudioClock::MinSec);
342 postroll_clock.set_name ("TransportClockDisplay");
344 /* alerts */
346 /* CANNOT sigc::bind these to clicked or toggled, must use pressed or released */
348 solo_alert_button.set_name ("TransportSoloAlert");
349 solo_alert_button.signal_button_press_event().connect (sigc::mem_fun(*this,&ARDOUR_UI::solo_alert_press), false);
350 auditioning_alert_button.set_name ("TransportAuditioningAlert");
351 auditioning_alert_button.signal_button_press_event().connect (sigc::mem_fun(*this,&ARDOUR_UI::audition_alert_press), false);
353 alert_box.pack_start (solo_alert_button, false, false);
354 alert_box.pack_start (auditioning_alert_button, false, false);
356 transport_tearoff_hbox.set_border_width (3);
358 transport_tearoff_hbox.pack_start (goto_start_button, false, false);
359 transport_tearoff_hbox.pack_start (goto_end_button, false, false);
361 Frame* sframe = manage (new Frame);
362 VBox* svbox = manage (new VBox);
363 HBox* shbox = manage (new HBox);
365 sframe->set_shadow_type (SHADOW_IN);
366 sframe->add (shuttle_box);
368 shuttle_box.set_name (X_("ShuttleControl"));
370 speed_display_box.add (speed_display_label);
371 speed_display_box.set_name (X_("ShuttleDisplay"));
372 set_size_request_to_display_given_text (speed_display_label, X_("> 24.0"), 2, 2);
374 shuttle_units_button.set_name (X_("ShuttleButton"));
375 shuttle_units_button.signal_clicked().connect (sigc::mem_fun(*this, &ARDOUR_UI::shuttle_unit_clicked));
377 shuttle_style_button.set_name (X_("ShuttleStyleButton"));
379 vector<string> shuttle_strings;
380 shuttle_strings.push_back (_("sprung"));
381 shuttle_strings.push_back (_("wheel"));
382 set_popdown_strings (shuttle_style_button, shuttle_strings, true);
383 shuttle_style_button.signal_changed().connect (sigc::mem_fun (*this, &ARDOUR_UI::shuttle_style_changed));
385 Frame* sdframe = manage (new Frame);
387 sdframe->set_shadow_type (SHADOW_IN);
388 sdframe->add (speed_display_box);
390 /* translators: Egternal is "External" with a descender character */
391 set_size_request_to_display_given_text (sync_button, X_("Egternal"), 4, 10);
393 shbox->pack_start (*sdframe, false, false);
394 shbox->pack_start (shuttle_units_button, true, true);
395 shbox->pack_start (shuttle_style_button, false, false);
397 svbox->pack_start (*sframe, false, false);
398 svbox->pack_start (*shbox, false, false);
400 transport_tearoff_hbox.pack_start (*svbox, false, false, 3);
402 if (Profile->get_sae()) {
403 transport_tearoff_hbox.pack_start (auto_loop_button);
404 transport_tearoff_hbox.pack_start (roll_button);
405 } else {
406 transport_tearoff_hbox.pack_start (auto_loop_button, false, false);
407 play_range_hbox.pack_start (play_selection_button, false, false);
408 play_range_hbox.pack_start (roll_button, false, false);
409 play_range_vbox.pack_start (play_range_hbox, false, false);
410 play_range_vbox.pack_start (join_play_range_button, false, false);
411 transport_tearoff_hbox.pack_start (play_range_vbox, false, false);
413 transport_tearoff_hbox.pack_start (stop_button, false, false);
414 transport_tearoff_hbox.pack_start (rec_button, false, false, 6);
416 HBox* clock_box = manage (new HBox);
417 clock_box->pack_start (primary_clock, false, false);
418 if (!ARDOUR::Profile->get_small_screen()) {
419 clock_box->pack_start (secondary_clock, false, false);
422 if (!Profile->get_sae()) {
423 VBox* time_controls_box = manage (new VBox);
424 time_controls_box->pack_start (sync_button, false, false);
425 time_controls_box->pack_start (time_master_button, false, false);
426 clock_box->pack_start (*time_controls_box, false, false, 1);
429 transport_tearoff_hbox.pack_start (*clock_box, false, false, 0);
431 HBox* toggle_box = manage(new HBox);
433 VBox* punch_box = manage (new VBox);
434 punch_box->pack_start (punch_in_button, false, false);
435 punch_box->pack_start (punch_out_button, false, false);
436 toggle_box->pack_start (*punch_box, false, false);
438 VBox* auto_box = manage (new VBox);
439 auto_box->pack_start (auto_play_button, false, false);
440 auto_box->pack_start (auto_return_button, false, false);
441 toggle_box->pack_start (*auto_box, false, false);
443 VBox* io_box = manage (new VBox);
444 io_box->pack_start (auto_input_button, false, false);
445 io_box->pack_start (click_button, false, false);
446 toggle_box->pack_start (*io_box, false, false);
448 /* desensitize */
450 set_transport_sensitivity (false);
452 // toggle_box->pack_start (preroll_button, false, false);
453 // toggle_box->pack_start (preroll_clock, false, false);
455 // toggle_box->pack_start (postroll_button, false, false);
456 // toggle_box->pack_start (postroll_clock, false, false);
458 transport_tearoff_hbox.pack_start (*toggle_box, false, false, 4);
459 transport_tearoff_hbox.pack_start (alert_box, false, false);
461 if (Profile->get_sae()) {
462 Image* img = manage (new Image ((::get_icon (X_("sae")))));
463 transport_tearoff_hbox.pack_end (*img, false, false, 6);
466 XMLNode* tnode = tearoff_settings ("transport");
467 if (tnode) {
468 transport_tearoff->set_state (*tnode);
472 void
473 ARDOUR_UI::manage_window (Window& win)
475 win.signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), &win));
476 win.signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (Keyboard::the_keyboard(), &Keyboard::enter_window), &win));
477 win.signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (Keyboard::the_keyboard(), &Keyboard::leave_window), &win));
480 void
481 ARDOUR_UI::detach_tearoff (Box* b, Widget* w)
483 // editor->ensure_float (transport_tearoff->tearoff_window());
484 b->remove (*w);
487 void
488 ARDOUR_UI::reattach_tearoff (Box* b, Widget* w, int32_t n)
490 b->pack_start (*w);
491 b->reorder_child (*w, n);
494 void
495 ARDOUR_UI::soloing_changed (bool onoff)
497 if (solo_alert_button.get_active() != onoff) {
498 solo_alert_button.set_active (onoff);
502 void
503 ARDOUR_UI::_auditioning_changed (bool onoff)
505 if (auditioning_alert_button.get_active() != onoff) {
506 auditioning_alert_button.set_active (onoff);
507 set_transport_sensitivity (!onoff);
511 void
512 ARDOUR_UI::auditioning_changed (bool onoff)
514 UI::instance()->call_slot (MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::_auditioning_changed, this, onoff));
517 bool
518 ARDOUR_UI::audition_alert_press (GdkEventButton* ev)
520 if (_session) {
521 _session->cancel_audition();
523 return true;
526 bool
527 ARDOUR_UI::solo_alert_press (GdkEventButton* ev)
529 if (_session) {
530 if (_session->soloing()) {
531 _session->set_solo (_session->get_routes(), false);
532 } else if (_session->listening()) {
533 _session->set_listen (_session->get_routes(), false);
536 return true;
539 void
540 ARDOUR_UI::solo_blink (bool onoff)
542 if (_session == 0) {
543 return;
546 if (_session->soloing() || _session->listening()) {
547 if (onoff) {
548 solo_alert_button.set_state (STATE_ACTIVE);
549 } else {
550 solo_alert_button.set_state (STATE_NORMAL);
552 } else {
553 solo_alert_button.set_active (false);
554 solo_alert_button.set_state (STATE_NORMAL);
558 void
559 ARDOUR_UI::sync_blink (bool onoff)
561 if (_session == 0 || !_session->config.get_external_sync()) {
562 /* internal sync */
563 sync_button.set_visual_state (0);
564 return;
567 if (!_session->transport_locked()) {
568 /* not locked, so blink on and off according to the onoff argument */
570 if (onoff) {
571 sync_button.set_visual_state (1); // "-active"
572 } else {
573 sync_button.set_visual_state (0); // normal
575 } else {
576 /* locked */
577 sync_button.set_visual_state (1); // "-active"
581 void
582 ARDOUR_UI::audition_blink (bool onoff)
584 if (_session == 0) {
585 return;
588 if (_session->is_auditioning()) {
589 if (onoff) {
590 auditioning_alert_button.set_state (STATE_ACTIVE);
591 } else {
592 auditioning_alert_button.set_state (STATE_NORMAL);
594 } else {
595 auditioning_alert_button.set_active (false);
596 auditioning_alert_button.set_state (STATE_NORMAL);
600 void
601 ARDOUR_UI::build_shuttle_context_menu ()
603 using namespace Menu_Helpers;
605 shuttle_context_menu = new Menu();
606 MenuList& items = shuttle_context_menu->items();
608 Menu* speed_menu = manage (new Menu());
609 MenuList& speed_items = speed_menu->items();
611 RadioMenuItem::Group group;
613 speed_items.push_back (RadioMenuElem (group, "8", sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::set_shuttle_max_speed), 8.0f)));
614 if (shuttle_max_speed == 8.0) {
615 static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
617 speed_items.push_back (RadioMenuElem (group, "6", sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::set_shuttle_max_speed), 6.0f)));
618 if (shuttle_max_speed == 6.0) {
619 static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
621 speed_items.push_back (RadioMenuElem (group, "4", sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::set_shuttle_max_speed), 4.0f)));
622 if (shuttle_max_speed == 4.0) {
623 static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
625 speed_items.push_back (RadioMenuElem (group, "3", sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::set_shuttle_max_speed), 3.0f)));
626 if (shuttle_max_speed == 3.0) {
627 static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
629 speed_items.push_back (RadioMenuElem (group, "2", sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::set_shuttle_max_speed), 2.0f)));
630 if (shuttle_max_speed == 2.0) {
631 static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
633 speed_items.push_back (RadioMenuElem (group, "1.5", sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::set_shuttle_max_speed), 1.5f)));
634 if (shuttle_max_speed == 1.5) {
635 static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
638 items.push_back (MenuElem (_("Maximum speed"), *speed_menu));
641 void
642 ARDOUR_UI::show_shuttle_context_menu ()
644 if (shuttle_context_menu == 0) {
645 build_shuttle_context_menu ();
648 shuttle_context_menu->popup (1, gtk_get_current_event_time());
651 void
652 ARDOUR_UI::set_shuttle_max_speed (float speed)
654 shuttle_max_speed = speed;
657 gint
658 ARDOUR_UI::shuttle_box_button_press (GdkEventButton* ev)
660 if (!_session) {
661 return true;
664 if (shuttle_controller_binding_proxy.button_press_handler (ev)) {
665 return true;
668 if (Keyboard::is_context_menu_event (ev)) {
669 show_shuttle_context_menu ();
670 return true;
673 switch (ev->button) {
674 case 1:
675 shuttle_box.add_modal_grab ();
676 shuttle_grabbed = true;
677 mouse_shuttle (ev->x, true);
678 break;
680 case 2:
681 case 3:
682 return true;
683 break;
686 return true;
689 gint
690 ARDOUR_UI::shuttle_box_button_release (GdkEventButton* ev)
692 if (!_session) {
693 return true;
696 switch (ev->button) {
697 case 1:
698 mouse_shuttle (ev->x, true);
699 shuttle_grabbed = false;
700 shuttle_box.remove_modal_grab ();
701 if (Config->get_shuttle_behaviour() == Sprung) {
702 if (_session->config.get_auto_play() || roll_button.get_visual_state()) {
703 shuttle_fract = SHUTTLE_FRACT_SPEED1;
704 _session->request_transport_speed (1.0);
705 stop_button.set_visual_state (0);
706 roll_button.set_visual_state (1);
707 } else {
708 shuttle_fract = 0;
709 _session->request_transport_speed (0.0);
711 shuttle_box.queue_draw ();
713 return true;
715 case 2:
716 if (_session->transport_rolling()) {
717 shuttle_fract = SHUTTLE_FRACT_SPEED1;
718 _session->request_transport_speed (1.0);
719 stop_button.set_visual_state (0);
720 roll_button.set_visual_state (1);
721 } else {
722 shuttle_fract = 0;
724 shuttle_box.queue_draw ();
725 return true;
727 case 3:
728 default:
729 return true;
733 use_shuttle_fract (true);
735 return true;
738 gint
739 ARDOUR_UI::shuttle_box_scroll (GdkEventScroll* ev)
741 if (!_session) {
742 return true;
745 switch (ev->direction) {
747 case GDK_SCROLL_UP:
748 shuttle_fract += 0.005;
749 break;
750 case GDK_SCROLL_DOWN:
751 shuttle_fract -= 0.005;
752 break;
753 default:
754 /* scroll left/right */
755 return false;
758 use_shuttle_fract (true);
760 return true;
763 gint
764 ARDOUR_UI::shuttle_box_motion (GdkEventMotion* ev)
766 if (!_session || !shuttle_grabbed) {
767 return true;
770 return mouse_shuttle (ev->x, false);
773 gint
774 ARDOUR_UI::mouse_shuttle (double x, bool force)
776 double half_width = shuttle_box.get_width() / 2.0;
777 double distance = x - half_width;
779 if (distance > 0) {
780 distance = min (distance, half_width);
781 } else {
782 distance = max (distance, -half_width);
785 shuttle_fract = distance / half_width;
786 use_shuttle_fract (force);
787 return true;
790 void
791 ARDOUR_UI::set_shuttle_fract (double f)
793 shuttle_fract = f;
794 use_shuttle_fract (false);
797 void
798 ARDOUR_UI::use_shuttle_fract (bool force)
800 microseconds_t now = get_microseconds();
802 /* do not attempt to submit a motion-driven transport speed request
803 more than once per process cycle.
806 if (!force && (last_shuttle_request - now) < (microseconds_t) engine->usecs_per_cycle()) {
807 return;
810 last_shuttle_request = now;
812 if (Config->get_shuttle_units() == Semitones) {
814 const double step = 1.0 / 24.0; // range is 24 semitones up & down
815 double semitones;
816 double speed;
818 semitones = round (shuttle_fract / step);
819 speed = pow (2.0, (semitones / 12.0));
821 _session->request_transport_speed (speed);
823 } else {
825 bool neg;
826 double fract;
828 neg = (shuttle_fract < 0.0);
830 fract = 1 - sqrt (1 - (shuttle_fract * shuttle_fract)); // Formula A1
832 if (neg) {
833 fract = -fract;
836 _session->request_transport_speed (shuttle_max_speed * fract);
839 shuttle_box.queue_draw ();
842 gint
843 ARDOUR_UI::shuttle_box_expose (GdkEventExpose* event)
845 gint x;
846 Glib::RefPtr<Gdk::Window> win (shuttle_box.get_window());
848 /* redraw the background */
850 win->draw_rectangle (shuttle_box.get_style()->get_bg_gc (shuttle_box.get_state()),
851 true,
852 event->area.x, event->area.y,
853 event->area.width, event->area.height);
856 x = (gint) floor ((shuttle_box.get_width() / 2.0) + (0.5 * (shuttle_box.get_width() * shuttle_fract)));
858 /* draw line */
860 win->draw_line (shuttle_box.get_style()->get_fg_gc (shuttle_box.get_state()),
864 shuttle_box.get_height());
865 return true;
868 void
869 ARDOUR_UI::shuttle_unit_clicked ()
871 if (shuttle_unit_menu == 0) {
872 shuttle_unit_menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/ShuttleUnitPopup"));
874 shuttle_unit_menu->popup (1, gtk_get_current_event_time());
877 void
878 ARDOUR_UI::shuttle_style_changed ()
880 ustring str = shuttle_style_button.get_active_text ();
882 if (str == _("sprung")) {
883 Config->set_shuttle_behaviour (Sprung);
884 } else if (str == _("wheel")) {
885 Config->set_shuttle_behaviour (Wheel);
889 void
890 ARDOUR_UI::update_speed_display ()
892 if (!_session) {
893 if (last_speed_displayed != 0) {
894 speed_display_label.set_text (_("stop"));
895 last_speed_displayed = 0;
897 return;
900 char buf[32];
901 float x = _session->transport_speed ();
903 if (x != last_speed_displayed) {
905 if (x != 0) {
906 if (Config->get_shuttle_units() == Percentage) {
907 snprintf (buf, sizeof (buf), "%d", (int) round (x * 100));
908 } else {
910 if (x < 0) {
911 snprintf (buf, sizeof (buf), "< %d", (int) round (12.0 * fast_log2 (-x)));
912 } else {
913 snprintf (buf, sizeof (buf), "> %d", (int) round (12.0 * fast_log2 (x)));
916 speed_display_label.set_text (buf);
917 } else {
918 speed_display_label.set_text (_("stop"));
921 last_speed_displayed = x;
925 void
926 ARDOUR_UI::set_transport_sensitivity (bool yn)
928 ActionManager::set_sensitive (ActionManager::transport_sensitive_actions, yn);
929 shuttle_box.set_sensitive (yn);
932 void
933 ARDOUR_UI::editor_realized ()
935 boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
936 Config->map_parameters (pc);
938 set_size_request_to_display_given_text (speed_display_box, _("-0.55"), 2, 2);
939 reset_dpi ();
942 void
943 ARDOUR_UI::maximise_editing_space ()
945 if (!editor) {
946 return;
949 transport_tearoff->set_visible (false);
950 editor->maximise_editing_space ();
953 void
954 ARDOUR_UI::restore_editing_space ()
956 if (!editor) {
957 return;
960 transport_tearoff->set_visible (true);
961 editor->restore_editing_space ();