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