a metric ton of changes
[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         
150         roll_button.set_active (false);
151         play_selection_button.set_active (false);
152         auto_loop_button.set_active (false);
153
154         shuttle_fract = 0;
155         shuttle_box.queue_draw ();
156
157         update_disk_space ();
158 }
159
160 static const double SHUTTLE_FRACT_SPEED1=0.48412291827; /* derived from A1,A2 */
161
162 void
163 ARDOUR_UI::transport_rolling ()
164 {
165         stop_button.set_active (false);
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         shuttle_box.set_name ("TransportButton");
230         goto_start_button.set_name ("TransportButton");
231         goto_end_button.set_name ("TransportButton");
232         roll_button.set_name ("TransportButton");
233         stop_button.set_name ("TransportButton");
234         play_selection_button.set_name ("TransportButton");
235         rec_button.set_name ("TransportRecButton");
236         auto_loop_button.set_name ("TransportButton");
237         auto_return_button.set_name ("TransportButton");
238         auto_play_button.set_name ("TransportButton");
239         auto_input_button.set_name ("TransportButton");
240         punch_in_button.set_name ("TransportButton");
241         punch_out_button.set_name ("TransportButton");
242         click_button.set_name ("TransportButton");
243         time_master_button.set_name ("TransportButton");
244
245         vector<Gdk::Color> colors;
246         Gdk::Color c;
247
248         /* record button has 3 color states, so we set 2 extra here */
249         set_color(c, rgba_from_style ("TransportRecButton", 0xff, 0, 0, 0, "bg", Gtk::STATE_PRELIGHT, false ));
250         colors.push_back (c);
251         
252         set_color(c, rgba_from_style ("TransportRecButton", 0xff, 0, 0, 0, "bg", Gtk::STATE_ACTIVE, false ));
253         colors.push_back (c);
254         
255         rec_button.set_colors (colors);
256         colors.clear ();
257         
258         /* other buttons get 2 color states, so add one here */
259         set_color(c, rgba_from_style ("TransportButton", 0x7f, 0xff, 0x7f, 0, "bg", Gtk::STATE_ACTIVE, false ));
260         colors.push_back (c);
261
262         stop_button.set_colors (colors);
263         roll_button.set_colors (colors);
264         auto_loop_button.set_colors (colors);
265         play_selection_button.set_colors (colors);
266         goto_start_button.set_colors (colors);
267         goto_end_button.set_colors (colors);
268         
269         Widget* w;
270
271         stop_button.set_active (true);
272
273         w = manage (new Image (Stock::MEDIA_PREVIOUS, ICON_SIZE_BUTTON));
274         w->show();
275         goto_start_button.add (*w);
276         w = manage (new Image (Stock::MEDIA_NEXT, ICON_SIZE_BUTTON));
277         w->show();
278         goto_end_button.add (*w);
279         w = manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_BUTTON));
280         w->show();
281         roll_button.add (*w);
282         w = manage (new Image (Stock::MEDIA_STOP, ICON_SIZE_BUTTON));
283         w->show();
284         stop_button.add (*w);
285         w = manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_BUTTON));
286         w->show();
287         play_selection_button.add (*w);
288         w = manage (new Image (Stock::MEDIA_RECORD, ICON_SIZE_BUTTON));
289         w->show();
290         rec_button.add (*w);
291         w = manage (new Image (Gdk::Pixbuf::create_from_xpm_data(loop_xpm)));
292         w->show();
293         auto_loop_button.add (*w);
294
295         RefPtr<Action> act;
296
297         act = ActionManager::get_action (X_("Transport"), X_("Stop"));
298         act->connect_proxy (stop_button);
299         act = ActionManager::get_action (X_("Transport"), X_("Roll"));
300         act->connect_proxy (roll_button);
301         act = ActionManager::get_action (X_("Transport"), X_("Record"));
302         act->connect_proxy (rec_button);
303         act = ActionManager::get_action (X_("Transport"), X_("GotoStart"));
304         act->connect_proxy (goto_start_button);
305         act = ActionManager::get_action (X_("Transport"), X_("GotoEnd"));
306         act->connect_proxy (goto_end_button);
307         act = ActionManager::get_action (X_("Transport"), X_("Loop"));
308         act->connect_proxy (auto_loop_button);
309         act = ActionManager::get_action (X_("Transport"), X_("PlaySelection"));
310         act->connect_proxy (play_selection_button);
311         act = ActionManager::get_action (X_("Transport"), X_("ToggleTimeMaster"));
312         act->connect_proxy (time_master_button);
313
314         ARDOUR_UI::instance()->tooltips().set_tip (roll_button, _("Play from playhead"));
315         ARDOUR_UI::instance()->tooltips().set_tip (stop_button, _("Stop playback"));
316         ARDOUR_UI::instance()->tooltips().set_tip (play_selection_button, _("Play range/selection"));
317         ARDOUR_UI::instance()->tooltips().set_tip (goto_start_button, _("Go to start of session"));
318         ARDOUR_UI::instance()->tooltips().set_tip (goto_end_button, _("Go to end of session"));
319         ARDOUR_UI::instance()->tooltips().set_tip (auto_loop_button, _("Play loop range"));
320         ARDOUR_UI::instance()->tooltips().set_tip (auto_return_button, _("Return to last playback start when stopped"));
321         ARDOUR_UI::instance()->tooltips().set_tip (auto_play_button, _("Start playback after any locate"));
322         ARDOUR_UI::instance()->tooltips().set_tip (auto_input_button, _("Be sensible about input monitoring"));
323         ARDOUR_UI::instance()->tooltips().set_tip (punch_in_button, _("Start recording at auto-punch start"));
324         ARDOUR_UI::instance()->tooltips().set_tip (punch_out_button, _("Stop recording at auto-punch end"));
325         ARDOUR_UI::instance()->tooltips().set_tip (click_button, _("Enable/Disable audio click"));
326         ARDOUR_UI::instance()->tooltips().set_tip (time_master_button, _("Does Ardour control the time?"));
327         ARDOUR_UI::instance()->tooltips().set_tip (shuttle_box, _("Shuttle speed control"));
328         ARDOUR_UI::instance()->tooltips().set_tip (shuttle_units_button, _("Select semitones or %%-age for speed display"));
329         ARDOUR_UI::instance()->tooltips().set_tip (speed_display_box, _("Current transport speed"));
330         
331         shuttle_box.set_flags (CAN_FOCUS);
332         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);
333         shuttle_box.set_size_request (100, 15);
334
335         goto_start_button.unset_flags (CAN_FOCUS);
336         goto_end_button.unset_flags (CAN_FOCUS);
337         roll_button.unset_flags (CAN_FOCUS);
338         stop_button.unset_flags (CAN_FOCUS);
339         play_selection_button.unset_flags (CAN_FOCUS);
340         rec_button.unset_flags (CAN_FOCUS);
341         auto_loop_button.unset_flags (CAN_FOCUS);
342         auto_return_button.unset_flags (CAN_FOCUS);
343         auto_play_button.unset_flags (CAN_FOCUS);
344         auto_input_button.unset_flags (CAN_FOCUS);
345         punch_out_button.unset_flags (CAN_FOCUS);
346         punch_in_button.unset_flags (CAN_FOCUS);
347         click_button.unset_flags (CAN_FOCUS);
348         time_master_button.unset_flags (CAN_FOCUS);
349         
350         goto_start_button.set_events (goto_start_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
351         goto_end_button.set_events (goto_end_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
352         roll_button.set_events (roll_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
353         stop_button.set_events (stop_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
354         play_selection_button.set_events (play_selection_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
355         rec_button.set_events (rec_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
356         auto_loop_button.set_events (auto_loop_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
357         auto_return_button.set_events (auto_return_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
358         auto_play_button.set_events (auto_play_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
359         auto_input_button.set_events (auto_input_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
360         click_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         time_master_button.set_events (punch_out_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
364
365         shuttle_box.signal_button_press_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_button_press));
366         shuttle_box.signal_button_release_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_button_release));
367         shuttle_box.signal_motion_notify_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_motion));
368         shuttle_box.signal_expose_event().connect (mem_fun(*this, &ARDOUR_UI::shuttle_box_expose));
369
370         /* clocks, etc. */
371
372         ARDOUR_UI::Clock.connect (bind (mem_fun (primary_clock, &AudioClock::set), false));
373         ARDOUR_UI::Clock.connect (bind (mem_fun (secondary_clock, &AudioClock::set), false));
374
375         primary_clock.set_mode (AudioClock::SMPTE);
376         secondary_clock.set_mode (AudioClock::BBT);
377
378         primary_clock.ValueChanged.connect (mem_fun(*this, &ARDOUR_UI::primary_clock_value_changed));
379         secondary_clock.ValueChanged.connect (mem_fun(*this, &ARDOUR_UI::secondary_clock_value_changed));
380
381         ARDOUR_UI::instance()->tooltips().set_tip (primary_clock, _("Primary clock"));
382         ARDOUR_UI::instance()->tooltips().set_tip (secondary_clock, _("secondary clock"));
383
384         /* options: XXX these should all be actions with the buttons as proxies */
385
386         auto_return_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_auto_return));
387         auto_play_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_auto_play));
388         auto_input_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_auto_input));
389         click_button.signal_toggled().connect (mem_fun(*this,&ARDOUR_UI::toggle_click));
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         mtc_port_changed ();
452         sync_option_combo.set_active_text (positional_sync_strings.front());
453         sync_option_combo.signal_changed().connect (mem_fun (*this, &ARDOUR_UI::sync_option_changed));
454
455         shbox->pack_start (*sdframe, false, false);
456         shbox->pack_start (shuttle_units_button, true, true);
457         shbox->pack_start (shuttle_style_button, false, false);
458         
459         svbox->pack_start (*sframe, false, false);
460         svbox->pack_start (*shbox, false, false);
461
462         transport_tearoff_hbox.pack_start (*svbox, false, false, 5);
463
464         transport_tearoff_hbox.pack_start (auto_loop_button, false, false);
465         transport_tearoff_hbox.pack_start (play_selection_button, false, false);
466         transport_tearoff_hbox.pack_start (roll_button, false, false);
467         transport_tearoff_hbox.pack_start (stop_button, false, false);
468         transport_tearoff_hbox.pack_start (rec_button, false, false, 10);
469
470         transport_tearoff_hbox.pack_start (primary_clock, false, false, 5);
471         transport_tearoff_hbox.pack_start (secondary_clock, false, false, 5);
472
473         transport_tearoff_hbox.pack_start (sync_option_combo, false, false);
474         transport_tearoff_hbox.pack_start (time_master_button, false, false);
475         transport_tearoff_hbox.pack_start (punch_in_button, false, false);
476         transport_tearoff_hbox.pack_start (punch_out_button, false, false);
477         transport_tearoff_hbox.pack_start (auto_input_button, false, false);
478         transport_tearoff_hbox.pack_start (auto_return_button, false, false);
479         transport_tearoff_hbox.pack_start (auto_play_button, false, false);
480         transport_tearoff_hbox.pack_start (click_button, false, false);
481         
482         /* desensitize */
483
484         set_transport_sensitivity (false);
485
486 //      transport_tearoff_hbox.pack_start (preroll_button, false, false);
487 //      transport_tearoff_hbox.pack_start (preroll_clock, false, false);
488
489 //      transport_tearoff_hbox.pack_start (postroll_button, false, false);
490 //      transport_tearoff_hbox.pack_start (postroll_clock, false, false);
491
492         transport_tearoff_hbox.pack_start (alert_box, false, false, 5);
493 }
494
495 void
496 ARDOUR_UI::setup_clock ()
497 {
498         ARDOUR_UI::Clock.connect (bind (mem_fun (big_clock, &AudioClock::set), false));
499         
500         big_clock_window = new Window (WINDOW_TOPLEVEL);
501         
502         big_clock_window->set_border_width (0);
503         big_clock_window->add  (big_clock);
504         big_clock_window->set_title (_("ardour: clock"));
505         big_clock_window->set_type_hint (Gdk::WINDOW_TYPE_HINT_MENU);
506         big_clock_window->signal_realize().connect (bind (sigc::ptr_fun (set_decoration), big_clock_window,  (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
507         big_clock_window->signal_unmap().connect (bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("<Actions>/Common/ToggleBigClock")));
508
509         manage_window (*big_clock_window);
510 }
511
512 void
513 ARDOUR_UI::manage_window (Window& win)
514 {
515         win.signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), &win));
516         win.signal_enter_notify_event().connect (bind (mem_fun (Keyboard::the_keyboard(), &Keyboard::enter_window), &win));
517         win.signal_leave_notify_event().connect (bind (mem_fun (Keyboard::the_keyboard(), &Keyboard::leave_window), &win));
518 }
519
520 void
521 ARDOUR_UI::detach_tearoff (Box* b, Widget* w)
522 {
523 //      editor->ensure_float (transport_tearoff->tearoff_window());
524         b->remove (*w);
525 }
526
527 void
528 ARDOUR_UI::reattach_tearoff (Box* b, Widget* w, int32_t n)
529 {
530         b->pack_start (*w);
531         b->reorder_child (*w, n);
532 }
533
534 void
535 ARDOUR_UI::soloing_changed (bool onoff)
536 {
537         if (solo_alert_button.get_active() != onoff) {
538                 solo_alert_button.set_active (onoff);
539         }
540 }
541
542 void
543 ARDOUR_UI::_auditioning_changed (bool onoff)
544 {
545         if (auditioning_alert_button.get_active() != onoff) {
546                 auditioning_alert_button.set_active (onoff);
547                 set_transport_sensitivity (!onoff);
548         }
549 }
550
551 void
552 ARDOUR_UI::auditioning_changed (bool onoff)
553 {
554         Gtkmm2ext::UI::instance()->call_slot(bind (mem_fun(*this, &ARDOUR_UI::_auditioning_changed), onoff));
555 }
556
557 void
558 ARDOUR_UI::audition_alert_toggle ()
559 {
560         if (session) {
561                 session->cancel_audition();
562         }
563 }
564
565 void
566 ARDOUR_UI::solo_alert_toggle ()
567 {
568         if (session) {
569                 session->set_all_solo (!session->soloing());
570         }
571 }
572
573 void
574 ARDOUR_UI::solo_blink (bool onoff)
575 {
576         if (session == 0) {
577                 return;
578         }
579         
580         if (session->soloing()) {
581                 if (onoff) {
582                         solo_alert_button.set_state (STATE_ACTIVE);
583                 } else {
584                         solo_alert_button.set_state (STATE_NORMAL);
585                 }
586         } else {
587                 solo_alert_button.set_active (false);
588                 solo_alert_button.set_state (STATE_NORMAL);
589         }
590 }
591
592 void
593 ARDOUR_UI::audition_blink (bool onoff)
594 {
595         if (session == 0) {
596                 return;
597         }
598         
599         if (session->is_auditioning()) {
600                 if (onoff) {
601                         auditioning_alert_button.set_state (STATE_ACTIVE);
602                 } else {
603                         auditioning_alert_button.set_state (STATE_NORMAL);
604                 }
605         } else {
606                 auditioning_alert_button.set_active (false);
607                 auditioning_alert_button.set_state (STATE_NORMAL);
608         }
609 }
610
611
612 gint
613 ARDOUR_UI::shuttle_box_button_press (GdkEventButton* ev)
614 {
615         if (!session) {
616                 return TRUE;
617         }
618
619         switch (ev->button) {
620         case 1:
621                 shuttle_box.add_modal_grab ();
622                 shuttle_grabbed = true;
623                 mouse_shuttle (ev->x, true);
624                 break;
625
626         case 2:
627         case 3:
628                 return TRUE;
629                 break;
630         }
631
632         return TRUE;
633 }
634
635 gint
636 ARDOUR_UI::shuttle_box_button_release (GdkEventButton* ev)
637 {
638         if (!session) {
639                 return TRUE;
640         }
641
642         switch (ev->button) {
643         case 1:
644                 mouse_shuttle (ev->x, true);
645                 shuttle_grabbed = false;
646                 shuttle_box.remove_modal_grab ();
647                 if (shuttle_behaviour == Sprung) {
648                         shuttle_fract = SHUTTLE_FRACT_SPEED1;
649                         session->request_transport_speed (1.0);
650                         shuttle_box.queue_draw ();
651                 }
652                 return TRUE;
653
654         case 2:
655                 if (session->transport_rolling()) {
656                         shuttle_fract = SHUTTLE_FRACT_SPEED1;
657                         session->request_transport_speed (1.0);
658                 } else {
659                         shuttle_fract = 0;
660                 }
661                 shuttle_box.queue_draw ();
662                 return TRUE;
663
664         case 3:
665                 return TRUE;
666                 
667         case 4:
668                 shuttle_fract += 0.005;
669                 break;
670         case 5:
671                 shuttle_fract -= 0.005;
672                 break;
673         }
674
675         use_shuttle_fract (true);
676
677         return TRUE;
678 }
679
680 gint
681 ARDOUR_UI::shuttle_box_motion (GdkEventMotion* ev)
682 {
683         if (!session || !shuttle_grabbed) {
684                 return TRUE;
685         }
686
687         return mouse_shuttle (ev->x, false);
688 }
689
690 gint
691 ARDOUR_UI::mouse_shuttle (double x, bool force)
692 {
693         double half_width = shuttle_box.get_width() / 2.0;
694         double distance = x - half_width;
695
696         if (distance > 0) {
697                 distance = min (distance, half_width);
698         } else {
699                 distance = max (distance, -half_width);
700         }
701
702         shuttle_fract = distance / half_width;
703         use_shuttle_fract (force);
704         return TRUE;
705 }
706
707 void
708 ARDOUR_UI::use_shuttle_fract (bool force)
709 {
710         struct timeval now;
711         struct timeval diff;
712         
713         /* do not attempt to submit a motion-driven transport speed request
714            more than once per process cycle.
715          */
716
717         gettimeofday (&now, 0);
718         timersub (&now, &last_shuttle_request, &diff);
719
720         if (!force && (diff.tv_usec + (diff.tv_sec * 1000000)) < engine->usecs_per_cycle()) {
721                 return;
722         }
723         
724         last_shuttle_request = now;
725
726         bool neg = (shuttle_fract < 0.0);
727
728         double fract = 1 - sqrt (1 - (shuttle_fract * shuttle_fract)); // Formula A1
729
730         if (neg) {
731                 fract = -fract;
732         }
733
734         session->request_transport_speed (8.0 * fract); // Formula A2
735         shuttle_box.queue_draw ();
736 }
737
738 gint
739 ARDOUR_UI::shuttle_box_expose (GdkEventExpose* event)
740 {
741         gint x;
742         Glib::RefPtr<Gdk::Window> win (shuttle_box.get_window());
743
744         /* redraw the background */
745
746         win->draw_rectangle (shuttle_box.get_style()->get_bg_gc (shuttle_box.get_state()),
747                              true,
748                              event->area.x, event->area.y,
749                              event->area.width, event->area.height);
750
751
752         x = (gint) floor ((shuttle_box.get_width() / 2.0) + (0.5 * (shuttle_box.get_width() * shuttle_fract)));
753
754         /* draw line */
755
756         win->draw_line (shuttle_box.get_style()->get_fg_gc (shuttle_box.get_state()),
757                         x,
758                         0,
759                         x,
760                         shuttle_box.get_height());
761         return TRUE;
762 }
763
764 void
765 ARDOUR_UI::shuttle_unit_clicked ()
766 {
767         if (shuttle_unit_menu == 0) {
768                 shuttle_unit_menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/ShuttleUnitPopup"));
769         }
770         shuttle_unit_menu->popup (1, 0);
771 }
772
773 void
774 ARDOUR_UI::set_shuttle_units (ShuttleUnits u)
775 {
776         switch ((shuttle_units = u)) {
777         case Percentage:
778                 static_cast<Label*>(shuttle_units_button.get_child())->set_text ("% ");
779                 break;
780         case Semitones:
781                 static_cast<Label*>(shuttle_units_button.get_child())->set_text (_("st"));
782                 break;
783         }
784 }
785
786 void
787 ARDOUR_UI::shuttle_style_changed ()
788 {
789         ustring str = shuttle_style_button.get_active_text ();
790
791         if (str == _("sprung")) {
792                 set_shuttle_behaviour (Sprung);
793         } else if (str == _("wheel")) {
794                 set_shuttle_behaviour (Wheel);
795         }
796 }
797
798
799 void
800 ARDOUR_UI::set_shuttle_behaviour (ShuttleBehaviour b)
801 {
802         switch ((shuttle_behaviour = b)) {
803         case Sprung:
804                 shuttle_style_button.set_active_text (_("sprung"));
805                 shuttle_fract = 0.0;
806                 shuttle_box.queue_draw ();
807                 if (session) {
808                         if (session->transport_rolling()) {
809                                 shuttle_fract = SHUTTLE_FRACT_SPEED1;
810                                 session->request_transport_speed (1.0);
811                         }
812                 }
813                 break;
814         case Wheel:
815                 shuttle_style_button.set_active_text (_("wheel"));
816                 break;
817         }
818 }
819
820 void
821 ARDOUR_UI::update_speed_display ()
822 {
823         if (!session) {
824                 if (last_speed_displayed != 0) {
825                         speed_display_label.set_text (_("stopped"));
826                         last_speed_displayed = 0;
827                 }
828                 return;
829         }
830
831         char buf[32];
832         float x = session->transport_speed ();
833
834         if (x != last_speed_displayed) {
835
836                 if (x != 0) {
837                         if (shuttle_units == Percentage) {
838                                 snprintf (buf, sizeof (buf), "%.4f", x);
839                         } else {
840                                 if (x < 0) {
841                                         snprintf (buf, sizeof (buf), "< %.1f", 12.0 * fast_log2 (-x));
842                                 } else {
843                                         snprintf (buf, sizeof (buf), "> %.1f", 12.0 * fast_log2 (x));
844                                 }
845                         }
846                         speed_display_label.set_text (buf);
847                 } else {
848                         speed_display_label.set_text (_("stopped"));
849                 }
850
851                 last_speed_displayed = x;
852         }
853 }       
854         
855 void
856 ARDOUR_UI::set_transport_sensitivity (bool yn)
857 {
858         ActionManager::set_sensitive (ActionManager::transport_sensitive_actions, yn);
859         shuttle_box.set_sensitive (yn);
860 }
861
862 void
863 ARDOUR_UI::editor_realized ()
864 {
865         set_size_request_to_display_given_text (speed_display_box, _("stopped"), 2, 2);
866         /* XXX: this should really be saved in instant.xml or something similar and restored from there */
867         shuttle_style_button.set_active_text (_("sprung"));
868         const guint32 FUDGE = 20; // Combo's are stupid - they steal space from the entry for the button
869         set_size_request_to_display_given_text (shuttle_style_button, _("sprung"), 2+FUDGE, 10);
870 }
871
872 void
873 ARDOUR_UI::sync_option_changed ()
874 {
875         string which;
876
877         if (session == 0) {
878                 return;
879         }
880
881         which = sync_option_combo.get_active_text();
882
883         if (which == positional_sync_strings[Session::None]) {
884                 session->request_slave_source (Session::None);
885         } else if (which == positional_sync_strings[Session::MTC]) {
886                 session->request_slave_source (Session::MTC);
887         } else if (which == positional_sync_strings[Session::JACK]) {
888                 session->request_slave_source (Session::JACK);
889         } 
890 }