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