many changes, read the diffs
[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     $Id$
19 */
20
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <cerrno>
25 #include <iostream>
26 #include <cmath>
27
28 #include <sigc++/bind.h>
29 #include <pbd/error.h>
30 #include <pbd/basename.h>
31 #include <pbd/fastlog.h>
32 #include <gtkmm2ext/pix.h>
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/click_box.h>
35 #include <gtkmm2ext/tearoff.h>
36
37 #include <ardour/audioengine.h>
38 #include <ardour/ardour.h>
39 #include <ardour/route.h>
40
41 #include "ardour_ui.h"
42 #include "public_editor.h"
43 #include "audio_clock.h"
44 #include "actions.h"
45 #include "utils.h"
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
52 using namespace Gtk;
53 using namespace Glib;
54 using namespace sigc;
55
56 int     
57 ARDOUR_UI::setup_windows ()
58 {
59         using namespace Menu_Helpers;
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         setup_adjustables ();
78         build_menu_bar ();
79
80         top_packer.pack_start (menu_bar_base, false, false);
81         top_packer.pack_start (transport_frame, false, false);
82
83         editor->add_toplevel_controls (top_packer);
84
85         return 0;
86 }
87
88 void
89 ARDOUR_UI::setup_adjustables ()
90 {
91         adjuster_table.set_homogeneous (true);
92
93         online_control_strings.push_back (_("MMC + Local"));
94         online_control_strings.push_back (_("MMC"));
95         online_control_strings.push_back (_("Local"));
96
97         online_control_button = new GlobalClickBox ("CONTROL",
98                                                     online_control_strings);
99
100         online_control_button->adjustment.signal_value_changed().connect(mem_fun(*this,&ARDOUR_UI::control_methods_adjusted));
101
102         mmc_id_strings.push_back ("1");
103         mmc_id_strings.push_back ("2");
104         mmc_id_strings.push_back ("3");
105         mmc_id_strings.push_back ("4");
106         mmc_id_strings.push_back ("5");
107         mmc_id_strings.push_back ("6");
108         mmc_id_strings.push_back ("7");
109         mmc_id_strings.push_back ("8");
110         mmc_id_strings.push_back ("9");
111
112         mmc_id_button = new GlobalClickBox (_("MMC ID"), mmc_id_strings);
113
114         mmc_id_button->adjustment.signal_value_changed().connect (mem_fun(*this,&ARDOUR_UI::mmc_device_id_adjusted));
115
116         adjuster_table.attach (*online_control_button, 0, 2, 1, 2, FILL|EXPAND, FILL, 5, 5);
117         adjuster_table.attach (*mmc_id_button, 2, 3, 1, 2, FILL, FILL, 5, 5);
118 }
119
120 static const gchar * loop_xpm[] = {
121 "19 19 3 1",
122 "       c None",
123 ".      c #000000",
124 "+      c #FFFFFF",
125 "       ...         ",
126 "       .+..        ",
127 "       .++..       ",
128 "     ...+++....    ",
129 "   ...++++++++...  ",
130 "  ..+++.+++..+++.. ",
131 " ..++...++.....++..",
132 " .++.. .+..   ..++.",
133 " .+..  ...     ..+.",
134 " .+.            .+.",
135 " .+..     ...  ..+.",
136 " .++..   ..+. ..++.",
137 " ..++.....++...++..",
138 "  ..+++..+++.+++.. ",
139 "   ...++++++++...  ",
140 "     ....+++...    ",
141 "        ..++.      ",
142 "         ..+.      ",
143 "          ...      "};
144
145 void
146 ARDOUR_UI::transport_stopped ()
147 {
148         stop_button.set_active (true);
149         roll_button.set_active (false);
150         play_selection_button.set_active (false);
151         auto_loop_button.set_active (false);
152
153         shuttle_fract = 0;
154         shuttle_box.queue_draw ();
155
156         update_disk_space ();
157 }
158
159 static const double SHUTTLE_FRACT_SPEED1=0.48412291827; /* derived from A1,A2 */
160
161 void
162 ARDOUR_UI::transport_rolling ()
163 {
164         stop_button.set_active (false);
165
166         if (session->get_play_range()) {
167
168                 play_selection_button.set_active (true);
169                 roll_button.set_active (false);
170                 auto_loop_button.set_active (false);
171
172         } else if (session->get_auto_loop ()) {
173
174                 auto_loop_button.set_active (true);
175                 play_selection_button.set_active (false);
176                 roll_button.set_active (false);
177
178         } else {
179
180                 roll_button.set_active (true);
181                 play_selection_button.set_active (false);
182                 auto_loop_button.set_active (false);
183         }
184
185         /* reset shuttle controller */
186
187         shuttle_fract = SHUTTLE_FRACT_SPEED1;  /* speed = 1.0, believe it or not */
188         shuttle_box.queue_draw ();
189 }
190
191 void
192 ARDOUR_UI::transport_rewinding ()
193 {
194         stop_button.set_active (false);
195         roll_button.set_active (true);
196         play_selection_button.set_active (false);
197         auto_loop_button.set_active (false);
198 }
199
200 void
201 ARDOUR_UI::transport_forwarding ()
202 {
203         stop_button.set_active (false);
204         roll_button.set_active (true);
205         play_selection_button.set_active (false);
206         auto_loop_button.set_active (false);
207 }
208
209 void
210 ARDOUR_UI::setup_transport ()
211 {
212         transport_tearoff = manage (new TearOff (transport_tearoff_hbox));
213         transport_tearoff->set_name ("TransportBase");
214
215         transport_hbox.pack_start (*transport_tearoff, true, false);
216
217         transport_base.set_name ("TransportBase");
218         transport_base.add (transport_hbox);
219
220         transport_frame.set_shadow_type (SHADOW_OUT);
221         transport_frame.set_name ("BaseFrame");
222         transport_frame.add (transport_base);
223
224         transport_tearoff->Detach.connect (bind (mem_fun(*this, &ARDOUR_UI::detach_tearoff), static_cast<Box*>(&top_packer), 
225                                                  static_cast<Widget*>(&transport_frame)));
226         transport_tearoff->Attach.connect (bind (mem_fun(*this, &ARDOUR_UI::reattach_tearoff), static_cast<Box*> (&top_packer), 
227                                                  static_cast<Widget*> (&transport_frame), 1));
228
229         vector<Gdk::Color> colors;
230         Gdk::Color c;
231
232         /* record button has 3 color states, so we set 2 extra here */
233
234         c.set_rgb_p (0.91, 0.68, 0.68);
235         colors.push_back (c);
236         c.set_rgb_p (1, 0, 0);
237         colors.push_back (c);
238         rec_button.set_colors (colors);
239
240         colors.clear ();
241
242         /* other buttons get 2 color states, so add one here */
243
244         c.set_rgb_p (0.66, 0.97, 0.18);
245         colors.push_back (c);
246
247         stop_button.set_colors (colors);
248         roll_button.set_colors (colors);
249         auto_loop_button.set_colors (colors);
250         play_selection_button.set_colors (colors);
251         goto_start_button.set_colors (colors);
252         goto_end_button.set_colors (colors);
253         
254         Widget* w;
255
256         stop_button.set_active (true);
257
258         w = manage (new Image (Stock::MEDIA_PREVIOUS, ICON_SIZE_BUTTON));
259         w->show();
260         goto_start_button.add (*w);
261         w = manage (new Image (Stock::MEDIA_NEXT, ICON_SIZE_BUTTON));
262         w->show();
263         goto_end_button.add (*w);
264         w = manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_BUTTON));
265         w->show();
266         roll_button.add (*w);
267         w = manage (new Image (Stock::MEDIA_STOP, ICON_SIZE_BUTTON));
268         w->show();
269         stop_button.add (*w);
270         w = manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_BUTTON));
271         w->show();
272         play_selection_button.add (*w);
273         w = manage (new Image (Stock::MEDIA_RECORD, ICON_SIZE_BUTTON));
274         w->show();
275         rec_button.add (*w);
276         w = manage (new Image (Gdk::Pixbuf::create_from_xpm_data(loop_xpm)));
277         w->show();
278         auto_loop_button.add (*w);
279
280         RefPtr<Action> act;
281
282         act = ActionManager::get_action (X_("Transport"), X_("Stop"));
283         act->connect_proxy (stop_button);
284         act = ActionManager::get_action (X_("Transport"), X_("Roll"));
285         act->connect_proxy (roll_button);
286         act = ActionManager::get_action (X_("Transport"), X_("Record"));
287         act->connect_proxy (rec_button);
288         act = ActionManager::get_action (X_("Transport"), X_("GotoStart"));
289         act->connect_proxy (goto_start_button);
290         act = ActionManager::get_action (X_("Transport"), X_("GotoEnd"));
291         act->connect_proxy (goto_end_button);
292         act = ActionManager::get_action (X_("Transport"), X_("Loop"));
293         act->connect_proxy (auto_loop_button);
294         act = ActionManager::get_action (X_("Transport"), X_("PlaySelection"));
295         act->connect_proxy (play_selection_button);
296
297         ARDOUR_UI::instance()->tooltips().set_tip (roll_button, _("Play from playhead"));
298         ARDOUR_UI::instance()->tooltips().set_tip (stop_button, _("Stop playback"));
299         ARDOUR_UI::instance()->tooltips().set_tip (play_selection_button, _("Play range/selection"));
300         ARDOUR_UI::instance()->tooltips().set_tip (goto_start_button, _("Go to start of session"));
301         ARDOUR_UI::instance()->tooltips().set_tip (goto_end_button, _("Go to end of session"));
302         ARDOUR_UI::instance()->tooltips().set_tip (auto_loop_button, _("Play loop range"));
303         ARDOUR_UI::instance()->tooltips().set_tip (auto_return_button, _("Return to last playback start when stopped"));
304         ARDOUR_UI::instance()->tooltips().set_tip (auto_play_button, _("Start playback after any locate"));
305         ARDOUR_UI::instance()->tooltips().set_tip (auto_input_button, _("Be sensible about input monitoring"));
306         ARDOUR_UI::instance()->tooltips().set_tip (punch_in_button, _("Start recording at auto-punch start"));
307         ARDOUR_UI::instance()->tooltips().set_tip (punch_out_button, _("Stop recording at auto-punch end"));
308         ARDOUR_UI::instance()->tooltips().set_tip (click_button, _("Enable/Disable audio click"));
309         ARDOUR_UI::instance()->tooltips().set_tip (follow_button, _("Enable/Disable follow playhead"));
310         ARDOUR_UI::instance()->tooltips().set_tip (shuttle_box, _("Shuttle speed control"));
311         ARDOUR_UI::instance()->tooltips().set_tip (shuttle_units_button, _("Select semitones or %%-age for speed display"));
312         ARDOUR_UI::instance()->tooltips().set_tip (speed_display_box, _("Current transport speed"));
313         
314         shuttle_box.set_flags (CAN_FOCUS);
315         shuttle_box.set_events (shuttle_box.get_events() | Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::POINTER_MOTION_MASK);
316         shuttle_box.set_size_request (100, 15);
317
318         shuttle_box.set_name ("TransportButton");
319         goto_start_button.set_name ("TransportButton");
320         goto_end_button.set_name ("TransportButton");
321         roll_button.set_name ("TransportButton");
322         stop_button.set_name ("TransportButton");
323         play_selection_button.set_name ("TransportButton");
324         rec_button.set_name ("TransportRecButton");
325         auto_loop_button.set_name ("TransportButton");
326         auto_return_button.set_name ("TransportButton");
327         auto_play_button.set_name ("TransportButton");
328         auto_input_button.set_name ("TransportButton");
329         punch_in_button.set_name ("TransportButton");
330         punch_out_button.set_name ("TransportButton");
331         click_button.set_name ("TransportButton");
332         follow_button.set_name ("TransportButton");
333         
334         goto_start_button.unset_flags (CAN_FOCUS);
335         goto_end_button.unset_flags (CAN_FOCUS);
336         roll_button.unset_flags (CAN_FOCUS);
337         stop_button.unset_flags (CAN_FOCUS);
338         play_selection_button.unset_flags (CAN_FOCUS);
339         rec_button.unset_flags (CAN_FOCUS);
340         auto_loop_button.unset_flags (CAN_FOCUS);
341         auto_return_button.unset_flags (CAN_FOCUS);
342         auto_play_button.unset_flags (CAN_FOCUS);
343         auto_input_button.unset_flags (CAN_FOCUS);
344         punch_out_button.unset_flags (CAN_FOCUS);
345         punch_in_button.unset_flags (CAN_FOCUS);
346         click_button.unset_flags (CAN_FOCUS);
347         follow_button.unset_flags (CAN_FOCUS);
348         
349         goto_start_button.set_events (goto_start_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
350         goto_end_button.set_events (goto_end_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
351         roll_button.set_events (roll_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
352         stop_button.set_events (stop_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
353         play_selection_button.set_events (play_selection_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
354         rec_button.set_events (rec_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
355         auto_loop_button.set_events (auto_loop_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
356         auto_return_button.set_events (auto_return_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
357         auto_play_button.set_events (auto_play_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
358         auto_input_button.set_events (auto_input_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
359         click_button.set_events (click_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
360         follow_button.set_events (click_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
361         punch_in_button.set_events (punch_in_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
362         punch_out_button.set_events (punch_out_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
363
364         shuttle_box.signal_button_press_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_button_press));
365         shuttle_box.signal_button_release_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_button_release));
366         shuttle_box.signal_motion_notify_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_motion));
367         shuttle_box.signal_expose_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_expose));
368
369         /* clocks, etc. */
370
371         ARDOUR_UI::Clock.connect (bind (mem_fun (primary_clock, &AudioClock::set), false));
372         ARDOUR_UI::Clock.connect (bind (mem_fun (secondary_clock, &AudioClock::set), false));
373
374         primary_clock.set_mode (AudioClock::SMPTE);
375         secondary_clock.set_mode (AudioClock::BBT);
376
377         primary_clock.ValueChanged.connect (mem_fun(*this, &ARDOUR_UI::primary_clock_value_changed));
378         secondary_clock.ValueChanged.connect (mem_fun(*this, &ARDOUR_UI::secondary_clock_value_changed));
379
380         ARDOUR_UI::instance()->tooltips().set_tip (primary_clock, _("Primary clock"));
381         ARDOUR_UI::instance()->tooltips().set_tip (secondary_clock, _("secondary clock"));
382
383         /* options */
384
385         auto_return_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_auto_return));
386         auto_play_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_auto_play));
387         auto_input_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_auto_input));
388         click_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_click));
389         follow_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_follow));
390         punch_in_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_punch_in));
391         punch_out_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_punch_out));
392
393         preroll_button.unset_flags (CAN_FOCUS);
394         preroll_button.set_events (preroll_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
395         preroll_button.set_name ("TransportButton");
396
397         postroll_button.unset_flags (CAN_FOCUS);
398         postroll_button.set_events (postroll_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
399         postroll_button.set_name ("TransportButton");
400
401         preroll_clock.set_mode (AudioClock::MinSec);
402         preroll_clock.set_name ("TransportClockDisplay");
403         postroll_clock.set_mode (AudioClock::MinSec);
404         postroll_clock.set_name ("TransportClockDisplay");
405
406         /* alerts */
407
408         /* CANNOT bind these to clicked or toggled, must use pressed or released */
409
410         solo_alert_button.set_name ("TransportSoloAlert");
411         solo_alert_button.signal_pressed().connect (mem_fun(*this,&ARDOUR_UI::solo_alert_toggle));
412         auditioning_alert_button.set_name ("TransportAuditioningAlert");
413         auditioning_alert_button.signal_pressed().connect (mem_fun(*this,&ARDOUR_UI::audition_alert_toggle));
414
415         alert_box.pack_start (solo_alert_button);
416         alert_box.pack_start (auditioning_alert_button);
417
418         transport_tearoff_hbox.set_border_width (5);
419
420         transport_tearoff_hbox.pack_start (goto_start_button, false, false);
421         transport_tearoff_hbox.pack_start (goto_end_button, false, false);
422
423         Frame* sframe = manage (new Frame);
424         VBox*  svbox = manage (new VBox);
425         HBox*  shbox = manage (new HBox);
426
427         sframe->set_shadow_type (SHADOW_IN);
428         sframe->add (shuttle_box);
429
430         shuttle_box.set_name (X_("ShuttleControl"));
431
432         speed_display_box.add (speed_display_label);
433         speed_display_box.set_name (X_("ShuttleDisplay"));
434
435         shuttle_units_button.set_name (X_("ShuttleButton"));
436         shuttle_units_button.signal_clicked().connect (mem_fun(*this, &ARDOUR_UI::shuttle_unit_clicked));
437         
438         shuttle_style_button.set_name (X_("ShuttleButton"));
439
440         vector<string> shuttle_strings;
441         shuttle_strings.push_back (_("sprung"));
442         shuttle_strings.push_back (_("wheel"));
443         set_popdown_strings (shuttle_style_button, shuttle_strings);
444         shuttle_style_button.signal_changed().connect (mem_fun (*this, &ARDOUR_UI::shuttle_style_changed));
445
446         Frame* sdframe = manage (new Frame);
447
448         sdframe->set_shadow_type (SHADOW_IN);
449         sdframe->add (speed_display_box);
450
451         shbox->pack_start (*sdframe, false, false);
452         shbox->pack_start (shuttle_units_button, true, true);
453         shbox->pack_start (shuttle_style_button, false, false);
454         
455         svbox->pack_start (*sframe, false, false);
456         svbox->pack_start (*shbox, false, false);
457
458         transport_tearoff_hbox.pack_start (*svbox, false, false, 5);
459
460         transport_tearoff_hbox.pack_start (auto_loop_button, false, false);
461         transport_tearoff_hbox.pack_start (play_selection_button, false, false);
462         transport_tearoff_hbox.pack_start (roll_button, false, false);
463         transport_tearoff_hbox.pack_start (stop_button, false, false);
464         transport_tearoff_hbox.pack_start (rec_button, false, false, 10);
465
466         transport_tearoff_hbox.pack_start (primary_clock, false, false, 5);
467         transport_tearoff_hbox.pack_start (secondary_clock, false, false, 5);
468
469         transport_tearoff_hbox.pack_start (punch_in_button, false, false);
470         transport_tearoff_hbox.pack_start (punch_out_button, false, false);
471         transport_tearoff_hbox.pack_start (auto_input_button, false, false);
472         transport_tearoff_hbox.pack_start (auto_return_button, false, false);
473         transport_tearoff_hbox.pack_start (auto_play_button, false, false);
474         transport_tearoff_hbox.pack_start (click_button, false, false);
475         transport_tearoff_hbox.pack_start (follow_button, false, false);
476         
477         /* desensitize */
478
479         set_transport_sensitivity (false);
480
481         /* catch up with editor state */
482
483         follow_changed ();
484
485 //      transport_tearoff_hbox.pack_start (preroll_button, false, false);
486 //      transport_tearoff_hbox.pack_start (preroll_clock, false, false);
487
488 //      transport_tearoff_hbox.pack_start (postroll_button, false, false);
489 //      transport_tearoff_hbox.pack_start (postroll_clock, false, false);
490
491         transport_tearoff_hbox.pack_start (alert_box, false, false, 5);
492 }
493
494 void
495 ARDOUR_UI::setup_clock ()
496 {
497         ARDOUR_UI::Clock.connect (bind (mem_fun (big_clock, &AudioClock::set), false));
498         
499         big_clock_window = new Window (WINDOW_TOPLEVEL);
500         
501         big_clock_window->set_border_width (0);
502         big_clock_window->add  (big_clock);
503         big_clock_window->set_title (_("ardour: clock"));
504         big_clock_window->set_type_hint (Gdk::WINDOW_TYPE_HINT_MENU);
505         big_clock_window->signal_realize().connect (bind (sigc::ptr_fun (set_decoration), big_clock_window,  (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
506         big_clock_window->signal_unmap().connect (bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleBigClock")));
507
508         manage_window (*big_clock_window);
509 }
510
511 void
512 ARDOUR_UI::manage_window (Window& win)
513 {
514         win.signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), &win));
515         win.signal_enter_notify_event().connect (bind (mem_fun (Keyboard::the_keyboard(), &Keyboard::enter_window), &win));
516         win.signal_leave_notify_event().connect (bind (mem_fun (Keyboard::the_keyboard(), &Keyboard::leave_window), &win));
517 }
518
519 void
520 ARDOUR_UI::detach_tearoff (Box* b, Widget* w)
521 {
522         editor->ensure_float (*transport_tearoff->tearoff_window());
523         b->remove (*w);
524 }
525
526 void
527 ARDOUR_UI::reattach_tearoff (Box* b, Widget* w, int32_t n)
528 {
529         b->pack_start (*w);
530         b->reorder_child (*w, n);
531 }
532
533 void
534 ARDOUR_UI::soloing_changed (bool onoff)
535 {
536         if (solo_alert_button.get_active() != onoff) {
537                 solo_alert_button.set_active (onoff);
538         }
539 }
540
541 void
542 ARDOUR_UI::_auditioning_changed (bool onoff)
543 {
544         if (auditioning_alert_button.get_active() != onoff) {
545                 auditioning_alert_button.set_active (onoff);
546                 set_transport_sensitivity (!onoff);
547         }
548 }
549
550 void
551 ARDOUR_UI::auditioning_changed (bool onoff)
552 {
553         Gtkmm2ext::UI::instance()->call_slot(bind (mem_fun(*this, &ARDOUR_UI::_auditioning_changed), onoff));
554 }
555
556 void
557 ARDOUR_UI::audition_alert_toggle ()
558 {
559         if (session) {
560                 session->cancel_audition();
561         }
562 }
563
564 void
565 ARDOUR_UI::solo_alert_toggle ()
566 {
567         if (session) {
568                 session->set_all_solo (!session->soloing());
569         }
570 }
571
572 void
573 ARDOUR_UI::solo_blink (bool onoff)
574 {
575         if (session == 0) {
576                 return;
577         }
578         
579         if (session->soloing()) {
580                 if (onoff) {
581                         solo_alert_button.set_state (STATE_ACTIVE);
582                 } else {
583                         solo_alert_button.set_state (STATE_NORMAL);
584                 }
585         } else {
586                 solo_alert_button.set_active (false);
587                 solo_alert_button.set_state (STATE_NORMAL);
588         }
589 }
590
591 void
592 ARDOUR_UI::audition_blink (bool onoff)
593 {
594         if (session == 0) {
595                 return;
596         }
597         
598         if (session->is_auditioning()) {
599                 if (onoff) {
600                         auditioning_alert_button.set_state (STATE_ACTIVE);
601                 } else {
602                         auditioning_alert_button.set_state (STATE_NORMAL);
603                 }
604         } else {
605                 auditioning_alert_button.set_active (false);
606                 auditioning_alert_button.set_state (STATE_NORMAL);
607         }
608 }
609
610
611 gint
612 ARDOUR_UI::shuttle_box_button_press (GdkEventButton* ev)
613 {
614         if (!session) {
615                 return TRUE;
616         }
617
618         switch (ev->button) {
619         case 1:
620                 shuttle_box.add_modal_grab ();
621                 shuttle_grabbed = true;
622                 mouse_shuttle (ev->x, true);
623                 break;
624
625         case 2:
626         case 3:
627                 return TRUE;
628                 break;
629         }
630
631         return TRUE;
632 }
633
634 gint
635 ARDOUR_UI::shuttle_box_button_release (GdkEventButton* ev)
636 {
637         if (!session) {
638                 return TRUE;
639         }
640
641         switch (ev->button) {
642         case 1:
643                 mouse_shuttle (ev->x, true);
644                 shuttle_grabbed = false;
645                 shuttle_box.remove_modal_grab ();
646                 if (shuttle_behaviour == Sprung) {
647                         shuttle_fract = SHUTTLE_FRACT_SPEED1;
648                         session->request_transport_speed (1.0);
649                         shuttle_box.queue_draw ();
650                 }
651                 return TRUE;
652
653         case 2:
654                 if (session->transport_rolling()) {
655                         shuttle_fract = SHUTTLE_FRACT_SPEED1;
656                         session->request_transport_speed (1.0);
657                 } else {
658                         shuttle_fract = 0;
659                 }
660                 shuttle_box.queue_draw ();
661                 return TRUE;
662
663         case 3:
664                 return TRUE;
665                 
666         case 4:
667                 shuttle_fract += 0.005;
668                 break;
669         case 5:
670                 shuttle_fract -= 0.005;
671                 break;
672         }
673
674         use_shuttle_fract (true);
675
676         return TRUE;
677 }
678
679 gint
680 ARDOUR_UI::shuttle_box_motion (GdkEventMotion* ev)
681 {
682         if (!session || !shuttle_grabbed) {
683                 return TRUE;
684         }
685
686         return mouse_shuttle (ev->x, false);
687 }
688
689 gint
690 ARDOUR_UI::mouse_shuttle (double x, bool force)
691 {
692         double half_width = shuttle_box.get_width() / 2.0;
693         double distance = x - half_width;
694
695         if (distance > 0) {
696                 distance = min (distance, half_width);
697         } else {
698                 distance = max (distance, -half_width);
699         }
700
701         shuttle_fract = distance / half_width;
702         use_shuttle_fract (force);
703         return TRUE;
704 }
705
706 void
707 ARDOUR_UI::use_shuttle_fract (bool force)
708 {
709         struct timeval now;
710         struct timeval diff;
711         
712         /* do not attempt to submit a motion-driven transport speed request
713            more than once per process cycle.
714          */
715
716         gettimeofday (&now, 0);
717         timersub (&now, &last_shuttle_request, &diff);
718
719         if (!force && (diff.tv_usec + (diff.tv_sec * 1000000)) < engine->usecs_per_cycle()) {
720                 return;
721         }
722         
723         last_shuttle_request = now;
724
725         bool neg = (shuttle_fract < 0.0);
726
727         double fract = 1 - sqrt (1 - (shuttle_fract * shuttle_fract)); // Formula A1
728
729         if (neg) {
730                 fract = -fract;
731         }
732
733         session->request_transport_speed (8.0 * fract); // Formula A2
734         shuttle_box.queue_draw ();
735 }
736
737 gint
738 ARDOUR_UI::shuttle_box_expose (GdkEventExpose* event)
739 {
740         gint x;
741         Glib::RefPtr<Gdk::Window> win (shuttle_box.get_window());
742
743         /* redraw the background */
744
745         win->draw_rectangle (shuttle_box.get_style()->get_bg_gc (shuttle_box.get_state()),
746                              true,
747                              event->area.x, event->area.y,
748                              event->area.width, event->area.height);
749
750
751         x = (gint) floor ((shuttle_box.get_width() / 2.0) + (0.5 * (shuttle_box.get_width() * shuttle_fract)));
752
753         /* draw line */
754
755         win->draw_line (shuttle_box.get_style()->get_fg_gc (shuttle_box.get_state()),
756                         x,
757                         0,
758                         x,
759                         shuttle_box.get_height());
760         return TRUE;
761 }
762
763 void
764 ARDOUR_UI::shuttle_unit_clicked ()
765 {
766         if (shuttle_unit_menu == 0) {
767                 shuttle_unit_menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/ShuttleUnitPopup"));
768         }
769         shuttle_unit_menu->popup (1, 0);
770 }
771
772 void
773 ARDOUR_UI::set_shuttle_units (ShuttleUnits u)
774 {
775         switch ((shuttle_units = u)) {
776         case Percentage:
777                 static_cast<Label*>(shuttle_units_button.get_child())->set_text ("% ");
778                 break;
779         case Semitones:
780                 static_cast<Label*>(shuttle_units_button.get_child())->set_text (_("st"));
781                 break;
782         }
783 }
784
785 void
786 ARDOUR_UI::shuttle_style_changed ()
787 {
788         ustring str = shuttle_style_button.get_active_text ();
789
790         if (str == _("sprung")) {
791                 set_shuttle_behaviour (Sprung);
792         } else if (str == _("wheel")) {
793                 set_shuttle_behaviour (Wheel);
794         }
795 }
796
797
798 void
799 ARDOUR_UI::set_shuttle_behaviour (ShuttleBehaviour b)
800 {
801         switch ((shuttle_behaviour = b)) {
802         case Sprung:
803                 shuttle_style_button.set_active_text (_("sprung"));
804                 shuttle_fract = 0.0;
805                 shuttle_box.queue_draw ();
806                 if (session) {
807                         if (session->transport_rolling()) {
808                                 shuttle_fract = SHUTTLE_FRACT_SPEED1;
809                                 session->request_transport_speed (1.0);
810                         }
811                 }
812                 break;
813         case Wheel:
814                 shuttle_style_button.set_active_text (_("wheel"));
815                 break;
816         }
817 }
818
819 void
820 ARDOUR_UI::update_speed_display ()
821 {
822         if (!session) {
823                 speed_display_label.set_text (_("stopped"));
824                 return;
825         }
826
827         char buf[32];
828         float x = session->transport_speed ();
829
830         if (x != 0) {
831                 if (shuttle_units == Percentage) {
832                         snprintf (buf, sizeof (buf), "%.4f", x);
833                 } else {
834                         if (x < 0) {
835                                 snprintf (buf, sizeof (buf), "< %.1f", 12.0 * fast_log2 (-x));
836                         } else {
837                                 snprintf (buf, sizeof (buf), "> %.1f", 12.0 * fast_log2 (x));
838                         }
839                 }
840                 speed_display_label.set_text (buf);
841         } else {
842                 speed_display_label.set_text (_("stopped"));
843         }
844 }       
845         
846 void
847 ARDOUR_UI::set_transport_sensitivity (bool yn)
848 {
849         ActionManager::set_sensitive (ActionManager::transport_sensitive_actions, yn);
850         shuttle_box.set_sensitive (yn);
851 }
852
853 void
854 ARDOUR_UI::editor_realized ()
855 {
856         set_size_request_to_display_given_text (speed_display_box, _("stopped"), 2, 2);
857         /* XXX: this should really be saved in instant.xml or something similar and restored from there */
858         shuttle_style_button.set_active_text (_("sprung"));
859         const guint32 FUDGE = 20; // Combo's are stupid - they steal space from the entry for the button
860         set_size_request_to_display_given_text (shuttle_style_button, _("sprung"), 2+FUDGE, 10);
861 }