convert canvas_event_frame() and window_event_frame() to canvas_event_sample() and...
[ardour.git] / gtk2_ardour / editor_canvas.cc
1 /*
2     Copyright (C) 2005 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 */
19
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <jack/types.h>
25
26 #include "gtkmm2ext/utils.h"
27
28 #include "ardour/profile.h"
29 #include "ardour/rc_configuration.h"
30 #include "ardour/smf_source.h"
31
32 #include "canvas/canvas.h"
33 #include "canvas/rectangle.h"
34 #include "canvas/pixbuf.h"
35 #include "canvas/text.h"
36 #include "canvas/debug.h"
37
38 #include "ardour_ui.h"
39 #include "editor.h"
40 #include "global_signals.h"
41 #include "editing.h"
42 #include "rgb_macros.h"
43 #include "utils.h"
44 #include "audio_time_axis.h"
45 #include "editor_drag.h"
46 #include "region_view.h"
47 #include "editor_group_tabs.h"
48 #include "editor_summary.h"
49 #include "video_timeline.h"
50 #include "keyboard.h"
51 #include "editor_cursors.h"
52 #include "mouse_cursors.h"
53 #include "verbose_cursor.h"
54
55 #include "i18n.h"
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace PBD;
60 using namespace Gtk;
61 using namespace Glib;
62 using namespace Gtkmm2ext;
63 using namespace Editing;
64
65 /* XXX this is a hack. it ought to be the maximum value of an framepos_t */
66
67 const double max_canvas_coordinate = (double) JACK_MAX_FRAMES;
68
69 void
70 Editor::initialize_canvas ()
71 {
72         _track_canvas_viewport = new ArdourCanvas::GtkCanvasViewport (horizontal_adjustment, vertical_adjustment);
73         _track_canvas = _track_canvas_viewport->canvas ();
74
75         _time_bars_canvas_viewport = new ArdourCanvas::GtkCanvasViewport (horizontal_adjustment, unused_adjustment);
76         _time_bars_canvas = _time_bars_canvas_viewport->canvas ();
77         
78         _verbose_cursor = new VerboseCursor (this);
79
80         /* on the bottom, an image */
81
82         if (Profile->get_sae()) {
83                 Image img (::get_icon (X_("saelogo")));
84                 // logo_item = new ArdourCanvas::Pixbuf (_track_canvas->root(), 0.0, 0.0, img.get_pixbuf());
85                 // logo_item->property_height_in_pixels() = true;
86                 // logo_item->property_width_in_pixels() = true;
87                 // logo_item->property_height_set() = true;
88                 // logo_item->property_width_set() = true;
89                 logo_item->show ();
90         }
91         
92         /*a group to hold global rects like punch/loop indicators */
93         global_rect_group = new ArdourCanvas::Group (_track_canvas->root());
94         CANVAS_DEBUG_NAME (global_rect_group, "global rect group");
95
96         transport_loop_range_rect = new ArdourCanvas::Rectangle (global_rect_group, ArdourCanvas::Rect (0.0, 0.0, 0.0, ArdourCanvas::COORD_MAX));
97         CANVAS_DEBUG_NAME (transport_loop_range_rect, "loop rect");
98         transport_loop_range_rect->hide();
99
100         transport_punch_range_rect = new ArdourCanvas::Rectangle (global_rect_group, ArdourCanvas::Rect (0.0, 0.0, 0.0, ArdourCanvas::COORD_MAX));
101         CANVAS_DEBUG_NAME (transport_punch_range_rect, "punch rect");
102         transport_punch_range_rect->hide();
103
104         /*a group to hold time (measure) lines */
105         time_line_group = new ArdourCanvas::Group (_track_canvas->root());
106         CANVAS_DEBUG_NAME (time_line_group, "time line group");
107
108         _trackview_group = new ArdourCanvas::Group (_track_canvas->root());
109         CANVAS_DEBUG_NAME (_trackview_group, "Canvas TrackViews");
110         _region_motion_group = new ArdourCanvas::Group (_trackview_group);
111         CANVAS_DEBUG_NAME (_region_motion_group, "Canvas Region Motion");
112
113         meter_bar_group = new ArdourCanvas::Group (_time_bars_canvas->root ());
114         meter_bar = new ArdourCanvas::Rectangle (meter_bar_group, ArdourCanvas::Rect (0.0, 0.0, ArdourCanvas::COORD_MAX, timebar_height));
115         CANVAS_DEBUG_NAME (meter_bar, "meter Bar");
116         meter_bar->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
117
118         tempo_bar_group = new ArdourCanvas::Group (_time_bars_canvas->root ());
119         tempo_bar = new ArdourCanvas::Rectangle (tempo_bar_group, ArdourCanvas::Rect (0.0, 0.0, ArdourCanvas::COORD_MAX, timebar_height));
120         CANVAS_DEBUG_NAME (tempo_bar, "Tempo  Bar");
121         tempo_bar->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
122
123         range_marker_bar_group = new ArdourCanvas::Group (_time_bars_canvas->root ());
124         range_marker_bar = new ArdourCanvas::Rectangle (range_marker_bar_group, ArdourCanvas::Rect (0.0, 0.0, ArdourCanvas::COORD_MAX, timebar_height));
125         CANVAS_DEBUG_NAME (range_marker_bar, "Range Marker Bar");
126         range_marker_bar->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
127
128         transport_marker_bar_group = new ArdourCanvas::Group (_time_bars_canvas->root ());
129         transport_marker_bar = new ArdourCanvas::Rectangle (transport_marker_bar_group, ArdourCanvas::Rect (0.0, 0.0, ArdourCanvas::COORD_MAX, timebar_height));
130         CANVAS_DEBUG_NAME (transport_marker_bar, "transport Marker Bar");
131         transport_marker_bar->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
132
133         marker_bar_group = new ArdourCanvas::Group (_time_bars_canvas->root ());
134         marker_bar = new ArdourCanvas::Rectangle (marker_bar_group, ArdourCanvas::Rect (0.0, 0.0, ArdourCanvas::COORD_MAX, timebar_height));
135         CANVAS_DEBUG_NAME (marker_bar, "Marker Bar");
136         marker_bar->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
137
138         cd_marker_bar_group = new ArdourCanvas::Group (_time_bars_canvas->root ());
139         cd_marker_bar = new ArdourCanvas::Rectangle (cd_marker_bar_group, ArdourCanvas::Rect (0.0, 0.0, ArdourCanvas::COORD_MAX, timebar_height));
140         CANVAS_DEBUG_NAME (cd_marker_bar, "CD Marker Bar");
141         cd_marker_bar->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
142         
143         _time_markers_group = new ArdourCanvas::Group (_time_bars_canvas->root());
144
145         cd_marker_group = new ArdourCanvas::Group (_time_markers_group, ArdourCanvas::Duple (0.0, 0.0));
146         CANVAS_DEBUG_NAME (cd_marker_group, "cd marker group");
147         /* the vide is temporarily placed a the same location as the
148            cd_marker_group, but is moved later.
149         */
150         videotl_group = new ArdourCanvas::Group (_time_markers_group, ArdourCanvas::Duple(0.0, 0.0));
151         CANVAS_DEBUG_NAME (videotl_group, "videotl group");
152         marker_group = new ArdourCanvas::Group (_time_markers_group, ArdourCanvas::Duple (0.0, timebar_height + 1.0));
153         CANVAS_DEBUG_NAME (marker_group, "marker group");
154         transport_marker_group = new ArdourCanvas::Group (_time_markers_group, ArdourCanvas::Duple (0.0, (timebar_height * 2.0) + 1.0));
155         CANVAS_DEBUG_NAME (transport_marker_group, "transport marker group");
156         range_marker_group = new ArdourCanvas::Group (_time_markers_group, ArdourCanvas::Duple (0.0, (timebar_height * 3.0) + 1.0));
157         CANVAS_DEBUG_NAME (range_marker_group, "range marker group");
158         tempo_group = new ArdourCanvas::Group (_time_markers_group, ArdourCanvas::Duple (0.0, (timebar_height * 4.0) + 1.0));
159         CANVAS_DEBUG_NAME (tempo_group, "tempo group");
160         meter_group = new ArdourCanvas::Group (_time_markers_group, ArdourCanvas::Duple (0.0, (timebar_height * 5.0) + 1.0));
161         CANVAS_DEBUG_NAME (meter_group, "meter group");
162
163         ARDOUR_UI::instance()->video_timeline = new VideoTimeLine(this, videotl_group, (timebar_height * videotl_bar_height));
164
165         cd_marker_bar_drag_rect = new ArdourCanvas::Rectangle (cd_marker_group, ArdourCanvas::Rect (0.0, 0.0, 100, timebar_height));
166         CANVAS_DEBUG_NAME (cd_marker_bar_drag_rect, "cd marker drag");
167         cd_marker_bar_drag_rect->set_outline (false);
168         cd_marker_bar_drag_rect->hide ();
169
170         range_bar_drag_rect = new ArdourCanvas::Rectangle (range_marker_group, ArdourCanvas::Rect (0.0, 0.0, 100, timebar_height));
171         CANVAS_DEBUG_NAME (range_bar_drag_rect, "range drag");
172         range_bar_drag_rect->set_outline (false);
173         range_bar_drag_rect->hide ();
174
175         transport_bar_drag_rect = new ArdourCanvas::Rectangle (transport_marker_group, ArdourCanvas::Rect (0.0, 0.0, 100, timebar_height));
176         CANVAS_DEBUG_NAME (transport_bar_drag_rect, "transport drag");
177         transport_bar_drag_rect->set_outline (false);
178         transport_bar_drag_rect->hide ();
179
180         transport_punchin_line = new ArdourCanvas::Line (_track_canvas->root());
181         transport_punchin_line->set_x0 (0);
182         transport_punchin_line->set_y0 (0);
183         transport_punchin_line->set_x1 (0);
184         transport_punchin_line->set_y1 (ArdourCanvas::COORD_MAX);
185         transport_punchin_line->hide ();
186
187         transport_punchout_line  = new ArdourCanvas::Line (_track_canvas->root());
188         transport_punchout_line->set_x0 (0);
189         transport_punchout_line->set_y0 (0);
190         transport_punchout_line->set_x1 (0);
191         transport_punchout_line->set_y1 (ArdourCanvas::COORD_MAX);
192         transport_punchout_line->hide();
193
194         // used to show zoom mode active zooming
195         zoom_rect = new ArdourCanvas::Rectangle (_track_canvas->root(), ArdourCanvas::Rect (0.0, 0.0, 0.0, 0.0));
196         zoom_rect->hide();
197
198         zoom_rect->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
199
200         // used as rubberband rect
201         rubberband_rect = new ArdourCanvas::Rectangle (_trackview_group, ArdourCanvas::Rect (0.0, 0.0, 0.0, 0.0));
202
203         rubberband_rect->hide();
204
205         tempo_bar->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
206         meter_bar->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
207         marker_bar->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
208         cd_marker_bar->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_cd_marker_bar_event), cd_marker_bar));
209         videotl_group->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_videotl_bar_event), videotl_group));
210         range_marker_bar->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
211         transport_marker_bar->Event.connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
212
213         playhead_cursor = new EditorCursor (*this, &Editor::canvas_playhead_cursor_event);
214
215         if (logo_item) {
216                 logo_item->lower_to_bottom ();
217         }
218         /* need to handle 4 specific types of events as catch-alls */
219
220         _track_canvas->signal_scroll_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_scroll_event));
221         _track_canvas->signal_motion_notify_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_motion_notify_event));
222         _track_canvas->signal_button_press_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_button_press_event));
223         _track_canvas->signal_button_release_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_button_release_event));
224         _track_canvas->signal_drag_motion().connect (sigc::mem_fun (*this, &Editor::track_canvas_drag_motion));
225         _track_canvas->signal_key_press_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_key_press));
226         _track_canvas->signal_key_release_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_key_release));
227
228         _track_canvas->set_name ("EditorMainCanvas");
229         _track_canvas->add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
230         _track_canvas->signal_leave_notify_event().connect (sigc::mem_fun(*this, &Editor::left_track_canvas), false);
231         _track_canvas->signal_enter_notify_event().connect (sigc::mem_fun(*this, &Editor::entered_track_canvas), false);
232         _track_canvas->set_flags (CAN_FOCUS);
233
234         /* set up drag-n-drop */
235
236         vector<TargetEntry> target_table;
237
238         // Drag-N-Drop from the region list can generate this target
239         target_table.push_back (TargetEntry ("regions"));
240
241         target_table.push_back (TargetEntry ("text/plain"));
242         target_table.push_back (TargetEntry ("text/uri-list"));
243         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
244
245         _track_canvas->drag_dest_set (target_table);
246         _track_canvas->signal_drag_data_received().connect (sigc::mem_fun(*this, &Editor::track_canvas_drag_data_received));
247
248         _track_canvas_viewport->signal_size_allocate().connect (sigc::mem_fun(*this, &Editor::track_canvas_viewport_allocate));
249
250         ColorsChanged.connect (sigc::mem_fun (*this, &Editor::color_handler));
251         color_handler();
252
253 }
254
255 void
256 Editor::track_canvas_viewport_allocate (Gtk::Allocation alloc)
257 {
258         _canvas_viewport_allocation = alloc;
259         track_canvas_viewport_size_allocated ();
260 }
261
262 bool
263 Editor::track_canvas_viewport_size_allocated ()
264 {
265         bool height_changed = _visible_canvas_height != _canvas_viewport_allocation.get_height();
266
267         _visible_canvas_width  = _canvas_viewport_allocation.get_width ();
268         _visible_canvas_height = _canvas_viewport_allocation.get_height ();
269
270         // SHOWTRACKS
271
272         if (height_changed) {
273
274                 for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
275                         i->second->canvas_height_set (_visible_canvas_height);
276                 }
277
278                 vertical_adjustment.set_page_size (_visible_canvas_height);
279                 if ((vertical_adjustment.get_value() + _visible_canvas_height) >= vertical_adjustment.get_upper()) {
280                         /*
281                            We're increasing the size of the canvas while the bottom is visible.
282                            We scroll down to keep in step with the controls layout.
283                         */
284                         vertical_adjustment.set_value (_full_canvas_height - _visible_canvas_height);
285                 }
286         }
287
288         update_fixed_rulers();
289         redisplay_tempo (false);
290         _summary->set_overlays_dirty ();
291
292         return false;
293 }
294
295 void
296 Editor::reset_controls_layout_width ()
297 {
298         GtkRequisition req;
299         gint w;
300
301         edit_controls_vbox.size_request (req);
302         w = req.width;
303
304         if (_group_tabs->is_mapped()) {
305                 _group_tabs->size_request (req);
306                 w += req.width;
307         }
308
309         /* the controls layout has no horizontal scrolling, its visible
310            width is always equal to the total width of its contents.
311         */
312
313         controls_layout.property_width() = w;
314         controls_layout.property_width_request() = w;
315 }
316
317 void
318 Editor::reset_controls_layout_height (int32_t h)
319 {
320         /* set the height of the scrollable area (i.e. the sum of all contained widgets)
321          */
322
323         controls_layout.property_height() = h;
324
325         /* size request is set elsewhere, see ::track_canvas_allocate() */
326 }
327
328 bool
329 Editor::track_canvas_map_handler (GdkEventAny* /*ev*/)
330 {
331         if (current_canvas_cursor) {
332                 set_canvas_cursor (current_canvas_cursor);
333         }
334         return false;
335 }
336
337 /** This is called when something is dropped onto the track canvas */
338 void
339 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
340                                          int x, int y,
341                                          const SelectionData& data,
342                                          guint info, guint time)
343 {
344         if (data.get_target() == "regions") {
345                 drop_regions (context, x, y, data, info, time);
346         } else {
347                 drop_paths (context, x, y, data, info, time);
348         }
349 }
350
351 bool
352 Editor::idle_drop_paths (vector<string> paths, framepos_t frame, double ypos, bool copy)
353 {
354         drop_paths_part_two (paths, frame, ypos, copy);
355         return false;
356 }
357
358 void
359 Editor::drop_paths_part_two (const vector<string>& paths, framepos_t frame, double ypos, bool copy)
360 {
361         RouteTimeAxisView* tv;
362         
363         /* MIDI files must always be imported, because we consider them
364          * writable. So split paths into two vectors, and follow the import
365          * path on the MIDI part.
366          */
367
368         vector<string> midi_paths;
369         vector<string> audio_paths;
370
371         for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
372                 if (SMFSource::safe_midi_file_extension (*i)) {
373                         midi_paths.push_back (*i);
374                 } else {
375                         audio_paths.push_back (*i);
376                 }
377         }
378
379
380         std::pair<TimeAxisView*, int> const tvp = trackview_by_y_position (ypos);
381         if (tvp.first == 0) {
382
383                 /* drop onto canvas background: create new tracks */
384
385                 frame = 0;
386
387                 do_import (midi_paths, Editing::ImportDistinctFiles, ImportAsTrack, SrcBest, frame);
388                 
389                 if (Profile->get_sae() || Config->get_only_copy_imported_files() || copy) {
390                         do_import (audio_paths, Editing::ImportDistinctFiles, Editing::ImportAsTrack, SrcBest, frame);
391                 } else {
392                         do_embed (audio_paths, Editing::ImportDistinctFiles, ImportAsTrack, frame);
393                 }
394
395         } else if ((tv = dynamic_cast<RouteTimeAxisView*> (tvp.first)) != 0) {
396
397                 /* check that its a track, not a bus */
398
399                 if (tv->track()) {
400                         /* select the track, then embed/import */
401                         selection->set (tv);
402
403                         do_import (midi_paths, Editing::ImportSerializeFiles, ImportToTrack, SrcBest, frame);
404
405                         if (Profile->get_sae() || Config->get_only_copy_imported_files() || copy) {
406                                 do_import (audio_paths, Editing::ImportSerializeFiles, Editing::ImportToTrack, SrcBest, frame);
407                         } else {
408                                 do_embed (audio_paths, Editing::ImportSerializeFiles, ImportToTrack, frame);
409                         }
410                 }
411         }
412 }
413
414 void
415 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
416                     int x, int y,
417                     const SelectionData& data,
418                     guint info, guint time)
419 {
420         vector<string> paths;
421         GdkEvent ev;
422         framepos_t frame;
423         double cy;
424
425         if (convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
426
427                 /* D-n-D coordinates are window-relative, so convert to "world" coordinates
428                  */
429
430                 ev.type = GDK_BUTTON_RELEASE;
431                 ev.button.x = x;
432                 ev.button.y = y;
433
434                 frame = window_event_sample (&ev, 0, &cy);
435
436                 snap_to (frame);
437
438                 bool copy = ((context->get_actions() & (Gdk::ACTION_COPY | Gdk::ACTION_LINK | Gdk::ACTION_MOVE)) == Gdk::ACTION_COPY);
439 #ifdef GTKOSX
440                 /* We are not allowed to call recursive main event loops from within
441                    the main event loop with GTK/Quartz. Since import/embed wants
442                    to push up a progress dialog, defer all this till we go idle.
443                 */
444                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &Editor::idle_drop_paths), paths, frame, cy, copy));
445 #else
446                 drop_paths_part_two (paths, frame, cy, copy);
447 #endif
448         }
449
450         context->drag_finish (true, false, time);
451 }
452
453 /** If the editor window is arranged such that the edge of the trackview is right up
454  *  against the edge of the screen, autoscroll will not work very well.  In this situation,
455  *  we start autoscrolling some distance in from the right-hand-side of the screen edge;
456  *  this is the distance at which that happens.
457  */
458 int
459 Editor::autoscroll_fudge_threshold () const
460 {
461         return current_page_samples() / 6;
462 }
463
464 /** @param allow_horiz true to allow horizontal autoscroll, otherwise false.
465  *
466  *  @param allow_vert true to allow vertical autoscroll, otherwise false.
467  *
468  *  @param moving_left true if we are moving left, so we only want to autoscroll on the left of the canvas,
469  *  otherwise false, so we only want to autoscroll on the right of the canvas.
470  *
471  *  @param moving_up true if we are moving up, so we only want to autoscroll at the top of the canvas,
472  *  otherwise false, so we only want to autoscroll at the bottom of the canvas.
473  */
474 void
475 Editor::maybe_autoscroll (bool allow_horiz, bool allow_vert, bool moving_left, bool moving_up)
476 {
477         if (!Config->get_autoscroll_editor ()) {
478                 return;
479         }
480         
481         bool startit = false;
482
483         /* Work out the distance between the right hand edge of the trackview and the edge of
484            the monitor that it is on.
485         */
486
487         Glib::RefPtr<Gdk::Window> gdk_window = get_window ();
488         Gdk::Rectangle window_rect;
489         gdk_window->get_frame_extents (window_rect);
490         
491         Glib::RefPtr<Gdk::Screen> screen = get_screen ();
492         Gdk::Rectangle root_rect;
493         screen->get_root_window()->get_frame_extents (root_rect);
494
495         Gtk::Allocation editor_list = _the_notebook.get_allocation ();
496
497         framecnt_t distance = pixel_to_sample (root_rect.get_x() + root_rect.get_width() - window_rect.get_x() - window_rect.get_width());
498         if (_the_notebook.is_visible ()) {
499                 distance += pixel_to_sample (editor_list.get_width());
500         }
501
502         /* Note whether we're fudging the autoscroll (see autoscroll_fudge_threshold) */
503         _autoscroll_fudging = (distance < autoscroll_fudge_threshold ());
504
505         /* ty is in canvas-coordinate space */
506
507         double const ty = _drags->current_pointer_y();
508         ArdourCanvas::Rect visible = _track_canvas->visible_area();
509
510         autoscroll_y = 0;
511         autoscroll_x = 0;
512         if (ty < visible.y0 && moving_up && allow_vert) {
513                 autoscroll_y = -1;
514                 startit = true;
515         } else if (ty > visible.y1 && !moving_up && allow_vert) {
516                 autoscroll_y = 1;
517                 startit = true;
518         }
519
520         framepos_t rightmost_frame = leftmost_frame + current_page_samples();
521         if (_autoscroll_fudging) {
522                 rightmost_frame -= autoscroll_fudge_threshold ();
523         }
524
525         if (_drags->current_pointer_frame() > rightmost_frame && allow_horiz) {
526                 if (rightmost_frame < max_framepos && !moving_left) {
527                         autoscroll_x = 1;
528                         startit = true;
529                 }
530         } else if (_drags->current_pointer_frame() < leftmost_frame && allow_horiz) {
531                 if (leftmost_frame > 0 && moving_left) {
532                         autoscroll_x = -1;
533                         startit = true;
534                 }
535         }
536
537         if (autoscroll_active && ((autoscroll_x != last_autoscroll_x) || (autoscroll_y != last_autoscroll_y) || (autoscroll_x == 0 && autoscroll_y == 0))) {
538                 stop_canvas_autoscroll ();
539         }
540
541         if (startit && autoscroll_timeout_tag < 0) {
542                 start_canvas_autoscroll (autoscroll_x, autoscroll_y);
543         }
544
545         last_autoscroll_x = autoscroll_x;
546         last_autoscroll_y = autoscroll_y;
547 }
548
549 gint
550 Editor::_autoscroll_canvas (void *arg)
551 {
552         return ((Editor *) arg)->autoscroll_canvas ();
553 }
554
555 bool
556 Editor::autoscroll_canvas ()
557 {
558         framepos_t new_frame;
559         framepos_t limit = max_framepos - current_page_samples();
560         double new_pixel;
561
562         if (autoscroll_x_distance != 0) {
563
564                 if (autoscroll_x > 0) {
565                         autoscroll_x_distance = (_drags->current_pointer_frame() - (leftmost_frame + current_page_samples())) / 3;
566                         if (_autoscroll_fudging) {
567                                 autoscroll_x_distance += autoscroll_fudge_threshold () / 3;
568                         }
569                 } else if (autoscroll_x < 0) {
570                         autoscroll_x_distance = (leftmost_frame - _drags->current_pointer_frame()) / 3;
571
572                 }
573         }
574
575         if (autoscroll_y_distance != 0) {
576                 if (autoscroll_y > 0) {
577                         autoscroll_y_distance = (_drags->current_pointer_y() - _visible_canvas_height) / 3;
578                 } else if (autoscroll_y < 0) {
579
580                         autoscroll_y_distance = (vertical_adjustment.get_value () - _drags->current_pointer_y()) / 3;
581                 }
582         }
583
584         if (autoscroll_x < 0) {
585                 if (leftmost_frame < autoscroll_x_distance) {
586                         new_frame = 0;
587                 } else {
588                         new_frame = leftmost_frame - autoscroll_x_distance;
589                 }
590         } else if (autoscroll_x > 0) {
591                 if (leftmost_frame > limit - autoscroll_x_distance) {
592                         new_frame = limit;
593                 } else {
594                         new_frame = leftmost_frame + autoscroll_x_distance;
595                 }
596         } else {
597                 new_frame = leftmost_frame;
598         }
599
600         double vertical_pos = vertical_adjustment.get_value();
601
602         if (autoscroll_y < 0) {
603
604                 if (vertical_pos < autoscroll_y_distance) {
605                         new_pixel = 0;
606                 } else {
607                         new_pixel = vertical_pos - autoscroll_y_distance;
608                 }
609
610         } else if (autoscroll_y > 0) {
611
612                 new_pixel = min (_full_canvas_height - _visible_canvas_height, min (_full_canvas_height, (vertical_adjustment.get_value() + autoscroll_y_distance)));
613
614         } else {
615                 new_pixel = vertical_pos;
616         }
617
618         if ((new_frame == 0 || new_frame == limit) && (new_pixel == 0 || new_pixel == DBL_MAX)) {
619                 /* we are done */
620                 return false;
621         }
622
623         if (new_frame != leftmost_frame) {
624                 reset_x_origin (new_frame);
625         }
626
627         if (new_pixel != vertical_pos) {
628                 vertical_adjustment.set_value (new_pixel);
629         }
630
631         /* fake an event. */
632
633         Glib::RefPtr<Gdk::Window> canvas_window = const_cast<Editor*>(this)->_track_canvas->get_window();
634         gint x, y;
635         Gdk::ModifierType mask;
636         GdkEventMotion ev;
637         canvas_window->get_pointer (x, y, mask);
638         ev.type = GDK_MOTION_NOTIFY;
639         ev.state = Gdk::BUTTON1_MASK;
640
641         /* the motion handler expects events in canvas coordinate space */
642         ArdourCanvas::Duple d = _track_canvas->window_to_canvas (ArdourCanvas::Duple (x, y));
643         ev.x = d.x;
644         ev.y = d.y;
645
646         motion_handler (0, (GdkEvent*) &ev, true);
647
648         autoscroll_cnt++;
649
650         if (autoscroll_cnt == 1) {
651
652                 /* connect the timeout so that we get called repeatedly */
653
654                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
655                 return false;
656
657         }
658
659         return true;
660 }
661
662 void
663 Editor::start_canvas_autoscroll (int dx, int dy)
664 {
665         if (!_session || autoscroll_active) {
666                 return;
667         }
668
669         stop_canvas_autoscroll ();
670
671         autoscroll_active = true;
672         autoscroll_x = dx;
673         autoscroll_y = dy;
674         autoscroll_x_distance = (framepos_t) floor (current_page_samples()/50.0);
675         autoscroll_y_distance = fabs ((double)dy * 5); /* pixels */
676         autoscroll_cnt = 0;
677
678         /* do it right now, which will start the repeated callbacks */
679
680         autoscroll_canvas ();
681 }
682
683 void
684 Editor::stop_canvas_autoscroll ()
685 {
686         if (autoscroll_timeout_tag >= 0) {
687                 g_source_remove (autoscroll_timeout_tag);
688                 autoscroll_timeout_tag = -1;
689         }
690
691         autoscroll_active = false;
692 }
693
694 bool
695 Editor::left_track_canvas (GdkEventCrossing */*ev*/)
696 {
697         DropDownKeys ();
698         within_track_canvas = false;
699         set_entered_track (0);
700         set_entered_regionview (0);
701         reset_canvas_action_sensitivity (false);
702         return false;
703 }
704
705 bool
706 Editor::entered_track_canvas (GdkEventCrossing */*ev*/)
707 {
708         within_track_canvas = true;
709         reset_canvas_action_sensitivity (true);
710         return FALSE;
711 }
712
713 void
714 Editor::ensure_time_axis_view_is_visible (const TimeAxisView& tav)
715 {
716         double begin = tav.y_position();
717
718         double v = vertical_adjustment.get_value ();
719
720         if (begin < v || begin + tav.current_height() > v + _visible_canvas_height) {
721                 /* try to put the TimeAxisView roughly central */
722                 if (begin >= _visible_canvas_height/2.0) {
723                         begin -= _visible_canvas_height/2.0;
724                 }
725                 vertical_adjustment.set_value (begin);
726         }
727 }
728
729 /** Called when the main vertical_adjustment has changed */
730 void
731 Editor::tie_vertical_scrolling ()
732 {
733         if (pending_visual_change.idle_handler_id < 0) {
734                 _summary->set_overlays_dirty ();
735         }
736 }
737
738 void
739 Editor::set_horizontal_position (double p)
740 {
741         horizontal_adjustment.set_value (p);
742
743         leftmost_frame = (framepos_t) floor (p * samples_per_pixel);
744
745         update_fixed_rulers ();
746         redisplay_tempo (true);
747
748         if (pending_visual_change.idle_handler_id < 0) {
749                 _summary->set_overlays_dirty ();
750         }
751
752         update_video_timeline();
753
754         HorizontalPositionChanged (); /* EMIT SIGNAL */
755 }
756
757 void
758 Editor::color_handler()
759 {
760         playhead_cursor->set_color (ARDOUR_UI::config()->get_canvasvar_PlayHead());
761         _verbose_cursor->set_color (ARDOUR_UI::config()->get_canvasvar_VerboseCanvasCursor());
762
763         meter_bar->set_fill_color (ARDOUR_UI::config()->get_canvasvar_MeterBar());
764         meter_bar->set_outline_color (ARDOUR_UI::config()->get_canvasvar_MarkerBarSeparator());
765
766         tempo_bar->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TempoBar());
767         tempo_bar->set_outline_color (ARDOUR_UI::config()->get_canvasvar_MarkerBarSeparator());
768
769         marker_bar->set_fill_color (ARDOUR_UI::config()->get_canvasvar_MarkerBar());
770         marker_bar->set_outline_color (ARDOUR_UI::config()->get_canvasvar_MarkerBarSeparator());
771
772         cd_marker_bar->set_fill_color (ARDOUR_UI::config()->get_canvasvar_CDMarkerBar());
773         cd_marker_bar->set_outline_color (ARDOUR_UI::config()->get_canvasvar_MarkerBarSeparator());
774
775         range_marker_bar->set_fill_color (ARDOUR_UI::config()->get_canvasvar_RangeMarkerBar());
776         range_marker_bar->set_outline_color (ARDOUR_UI::config()->get_canvasvar_MarkerBarSeparator());
777
778         transport_marker_bar->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TransportMarkerBar());
779         transport_marker_bar->set_outline_color (ARDOUR_UI::config()->get_canvasvar_MarkerBarSeparator());
780
781         cd_marker_bar_drag_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_RangeDragBarRect());
782         cd_marker_bar_drag_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_RangeDragBarRect());
783
784         range_bar_drag_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_RangeDragBarRect());
785         range_bar_drag_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_RangeDragBarRect());
786
787         transport_bar_drag_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TransportDragRect());
788         transport_bar_drag_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_TransportDragRect());
789
790         transport_loop_range_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TransportLoopRect());
791         transport_loop_range_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_TransportLoopRect());
792
793         transport_punch_range_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TransportPunchRect());
794         transport_punch_range_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_TransportPunchRect());
795
796         transport_punchin_line->set_outline_color (ARDOUR_UI::config()->get_canvasvar_PunchLine());
797         transport_punchout_line->set_outline_color (ARDOUR_UI::config()->get_canvasvar_PunchLine());
798
799         zoom_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ZoomRect());
800         zoom_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_ZoomRect());
801
802         rubberband_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_RubberBandRect());
803         rubberband_rect->set_fill_color ((guint32) ARDOUR_UI::config()->get_canvasvar_RubberBandRect());
804
805         location_marker_color = ARDOUR_UI::config()->get_canvasvar_LocationMarker();
806         location_range_color = ARDOUR_UI::config()->get_canvasvar_LocationRange();
807         location_cd_marker_color = ARDOUR_UI::config()->get_canvasvar_LocationCDMarker();
808         location_loop_color = ARDOUR_UI::config()->get_canvasvar_LocationLoop();
809         location_punch_color = ARDOUR_UI::config()->get_canvasvar_LocationPunch();
810
811         refresh_location_display ();
812 /*
813         redisplay_tempo (true);
814
815         if (_session)
816               _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
817 */
818 }
819
820 double
821 Editor::horizontal_position () const
822 {
823         return sample_to_pixel (leftmost_frame);
824 }
825
826 void
827 Editor::set_canvas_cursor (Gdk::Cursor* cursor, bool save)
828 {
829         if (save) {
830                 current_canvas_cursor = cursor;
831         }
832
833         Glib::RefPtr<Gdk::Window> win = _track_canvas->get_window();
834
835         if (win) {
836                 _track_canvas->get_window()->set_cursor (*cursor);
837         }
838 }
839
840 bool
841 Editor::track_canvas_key_press (GdkEventKey*)
842 {
843         /* XXX: event does not report the modifier key pressed down, AFAICS, so use the Keyboard object instead */
844         if (mouse_mode == Editing::MouseZoom && Keyboard::the_keyboard().key_is_down (GDK_Control_L)) {
845                 set_canvas_cursor (_cursors->zoom_out, true);
846         }
847
848         return false;
849 }
850
851 bool
852 Editor::track_canvas_key_release (GdkEventKey*)
853 {
854         if (mouse_mode == Editing::MouseZoom && !Keyboard::the_keyboard().key_is_down (GDK_Control_L)) {
855                 set_canvas_cursor (_cursors->zoom_in, true);
856         }
857
858         return false;
859 }
860
861 double
862 Editor::clamp_verbose_cursor_x (double x)
863 {
864         if (x < 0) {
865                 x = 0;
866         } else {
867                 x = min (_visible_canvas_width - 200.0, x);
868         }
869         return x;
870 }
871
872 double
873 Editor::clamp_verbose_cursor_y (double y)
874 {
875         y = max (0.0, y);
876         y = min (_visible_canvas_height - 50, y);
877         return y;
878 }
879
880 ArdourCanvas::Group*
881 Editor::get_time_bars_group () const
882 {
883         return _time_bars_canvas->root();
884 }
885
886 ArdourCanvas::Group*
887 Editor::get_track_canvas_group() const
888 {
889         return _track_canvas->root();
890 }
891
892 ArdourCanvas::GtkCanvasViewport*
893 Editor::get_time_bars_canvas() const
894 {
895         return _time_bars_canvas_viewport;
896 }
897
898 ArdourCanvas::GtkCanvasViewport*
899 Editor::get_track_canvas() const
900 {
901         return _track_canvas_viewport;
902 }