e8ba2b8a6032bce19af73d77172725eaa0e8070e
[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     $Id$
19 */
20
21 #include <libgnomecanvasmm/init.h>
22 #include <jack/types.h>
23 #include <gtkmm2ext/utils.h>
24
25 #include <ardour/audioregion.h>
26
27 #include "ardour_ui.h"
28 #include "editor.h"
29 #include "waveview.h"
30 #include "simplerect.h"
31 #include "simpleline.h"
32 #include "imageframe.h"
33 #include "waveview_p.h"
34 #include "simplerect_p.h"
35 #include "simpleline_p.h"
36 #include "imageframe_p.h"
37 #include "canvas_impl.h"
38 #include "editing.h"
39 #include "rgb_macros.h"
40 #include "utils.h"
41 #include "time_axis_view.h"
42 #include "audio_time_axis.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace sigc;
48 using namespace ARDOUR;
49 using namespace PBD;
50 using namespace Gtk;
51 using namespace Glib;
52 using namespace Gtkmm2ext;
53 using namespace Editing;
54
55 /* XXX this is a hack. it ought to be the maximum value of an jack_nframes_t */
56
57 const double max_canvas_coordinate = (double) JACK_MAX_FRAMES;
58
59 extern "C"
60 {
61
62 GType gnome_canvas_simpleline_get_type(void);
63 GType gnome_canvas_simplerect_get_type(void);
64 GType gnome_canvas_waveview_get_type(void);
65 GType gnome_canvas_imageframe_get_type(void);
66
67 }
68
69 static void ardour_canvas_type_init() 
70 {
71         // Map gtypes to gtkmm wrapper-creation functions:
72         
73         Glib::wrap_register(gnome_canvas_simpleline_get_type(), &Gnome::Canvas::SimpleLine_Class::wrap_new);
74         Glib::wrap_register(gnome_canvas_simplerect_get_type(), &Gnome::Canvas::SimpleRect_Class::wrap_new);
75         Glib::wrap_register(gnome_canvas_waveview_get_type(), &Gnome::Canvas::WaveView_Class::wrap_new);
76         Glib::wrap_register(gnome_canvas_imageframe_get_type(), &Gnome::Canvas::ImageFrame_Class::wrap_new);
77         
78         // Register the gtkmm gtypes:
79
80         (void) Gnome::Canvas::WaveView::get_type();
81         (void) Gnome::Canvas::SimpleLine::get_type();
82         (void) Gnome::Canvas::SimpleRect::get_type();
83         (void) Gnome::Canvas::ImageFrame::get_type();
84
85
86 void
87 Editor::initialize_canvas ()
88 {
89         ArdourCanvas::init ();
90         ardour_canvas_type_init ();
91
92         /* don't try to center the canvas */
93
94         track_canvas.set_center_scroll_region (false);
95
96         track_canvas.signal_event().connect (bind (mem_fun (*this, &Editor::track_canvas_event), (ArdourCanvas::Item*) 0));
97         track_canvas.set_name ("EditorMainCanvas");
98         track_canvas.add_events (Gdk::POINTER_MOTION_HINT_MASK|Gdk::SCROLL_MASK);
99         track_canvas.signal_leave_notify_event().connect (mem_fun(*this, &Editor::left_track_canvas));
100         track_canvas.set_flags (CAN_FOCUS);
101
102         /* set up drag-n-drop */
103
104         vector<TargetEntry> target_table;
105         
106         // Drag-N-Drop from the region list can generate this target
107         target_table.push_back (TargetEntry ("regions"));
108
109         target_table.push_back (TargetEntry ("text/plain"));
110         target_table.push_back (TargetEntry ("text/uri-list"));
111         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
112
113         track_canvas.drag_dest_set (target_table);
114         track_canvas.signal_drag_data_received().connect (mem_fun(*this, &Editor::track_canvas_drag_data_received));
115
116         /* stuff for the verbose canvas cursor */
117
118         Pango::FontDescription font = get_font_for_style (N_("VerboseCanvasCursor"));
119
120         verbose_canvas_cursor = new ArdourCanvas::Text (*track_canvas.root());
121         verbose_canvas_cursor->property_font_desc() = font;
122         verbose_canvas_cursor->property_anchor() = ANCHOR_NW;
123         verbose_canvas_cursor->property_fill_color_rgba() = color_map[cVerboseCanvasCursor];
124         
125         verbose_cursor_visible = false;
126         
127         /* a group to hold time (measure) lines */
128         
129         time_line_group = new ArdourCanvas::Group (*track_canvas.root(), 0.0, 0.0);
130         cursor_group = new ArdourCanvas::Group (*track_canvas.root(), 0.0, 0.0);
131
132         time_canvas.set_name ("EditorTimeCanvas");
133         time_canvas.add_events (Gdk::POINTER_MOTION_HINT_MASK);
134         time_canvas.set_flags (CAN_FOCUS);
135         time_canvas.set_center_scroll_region (false);
136         
137         meter_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, 0.0);
138         tempo_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height);
139         marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 2.0);
140         range_marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 3.0);
141         transport_marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 4.0);
142         
143         tempo_bar = new ArdourCanvas::SimpleRect (*tempo_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
144         tempo_bar->property_fill_color_rgba() = color_map[cTempoBar];
145         tempo_bar->property_outline_pixels() = 0;
146         
147         meter_bar = new ArdourCanvas::SimpleRect (*meter_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
148         meter_bar->property_fill_color_rgba() = color_map[cMeterBar];
149         meter_bar->property_outline_pixels() = 0;
150         
151         marker_bar = new ArdourCanvas::SimpleRect (*marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
152         marker_bar->property_fill_color_rgba() = color_map[cMarkerBar];
153         marker_bar->property_outline_pixels() = 0;
154         
155         range_marker_bar = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
156         range_marker_bar->property_fill_color_rgba() = color_map[cRangeMarkerBar];
157         range_marker_bar->property_outline_pixels() = 0;
158         
159         transport_marker_bar = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
160         transport_marker_bar->property_fill_color_rgba() = color_map[cTransportMarkerBar];
161         transport_marker_bar->property_outline_pixels() = 0;
162         
163         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
164         range_bar_drag_rect->property_fill_color_rgba() = color_map[cRangeDragBarRectFill];
165         range_bar_drag_rect->property_outline_color_rgba() = color_map[cRangeDragBarRect];
166         range_bar_drag_rect->property_outline_pixels() = 0;
167         range_bar_drag_rect->hide ();
168         
169         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
170         transport_bar_drag_rect ->property_fill_color_rgba() = color_map[cTransportDragRectFill];
171         transport_bar_drag_rect->property_outline_color_rgba() = color_map[cTransportDragRect];
172         transport_bar_drag_rect->property_outline_pixels() = 0;
173         transport_bar_drag_rect->hide ();
174         
175         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
176         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
177
178         marker_drag_line = new ArdourCanvas::Line (*track_canvas.root());
179         marker_drag_line->property_width_pixels() = 1;
180         marker_drag_line->property_fill_color_rgba() = color_map[cMarkerDragLine];
181         marker_drag_line->property_points() = marker_drag_line_points;
182         marker_drag_line->hide();
183
184         range_marker_drag_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
185         range_marker_drag_rect->property_fill_color_rgba() = color_map[cRangeDragRectFill];
186         range_marker_drag_rect->property_outline_color_rgba() = color_map[cRangeDragRect];
187         range_marker_drag_rect->hide ();
188         
189         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
190         transport_loop_range_rect->property_fill_color_rgba() = color_map[cTransportLoopRectFill];
191         transport_loop_range_rect->property_outline_color_rgba() = color_map[cTransportLoopRect];
192         transport_loop_range_rect->property_outline_pixels() = 1;
193         transport_loop_range_rect->hide();
194
195         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
196         transport_punch_range_rect->property_fill_color_rgba() = color_map[cTransportPunchRectFill];
197         transport_punch_range_rect->property_outline_color_rgba() = color_map[cTransportPunchRect];
198         transport_punch_range_rect->property_outline_pixels() = 0;
199         transport_punch_range_rect->hide();
200         
201         transport_loop_range_rect->lower_to_bottom (); // loop on the bottom
202
203         transport_punchin_line = new ArdourCanvas::SimpleLine (*time_line_group);
204         transport_punchin_line->property_x1() = 0.0;
205         transport_punchin_line->property_y1() = 0.0;
206         transport_punchin_line->property_x2() = 0.0;
207         transport_punchin_line->property_y2() = 0.0;
208         transport_punchin_line->property_color_rgba() = color_map[cPunchInLine];
209         transport_punchin_line->hide ();
210         
211         transport_punchout_line  = new ArdourCanvas::SimpleLine (*time_line_group);
212         transport_punchout_line->property_x1() = 0.0;
213         transport_punchout_line->property_y1() = 0.0;
214         transport_punchout_line->property_x2() = 0.0;
215         transport_punchout_line->property_y2() = 0.0;
216         transport_punchout_line->property_color_rgba() = color_map[cPunchOutLine];
217         transport_punchout_line->hide();
218         
219         // used to show zoom mode active zooming
220         zoom_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
221         zoom_rect->property_fill_color_rgba() = color_map[cZoomRectFill];
222         zoom_rect->property_outline_color_rgba() = color_map[cZoomRect];
223         zoom_rect->property_outline_pixels() = 1;
224         zoom_rect->hide();
225         
226         zoom_rect->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
227         
228         // used as rubberband rect
229         rubberband_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
230         rubberband_rect->property_outline_color_rgba() = color_map[cRubberBandRect];
231         rubberband_rect->property_fill_color_rgba() = (guint32) color_map[cRubberBandRectFill];
232         rubberband_rect->property_outline_pixels() = 1;
233         rubberband_rect->hide();
234         
235         tempo_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
236         meter_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
237         marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
238         range_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
239         transport_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
240         
241         /* separator lines */
242         
243         tempo_line = new ArdourCanvas::SimpleLine (*tempo_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
244         tempo_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
245
246         meter_line = new ArdourCanvas::SimpleLine (*meter_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
247         meter_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
248
249         marker_line = new ArdourCanvas::SimpleLine (*marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
250         marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
251         
252         range_marker_line = new ArdourCanvas::SimpleLine (*range_marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
253         range_marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
254
255         transport_marker_line = new ArdourCanvas::SimpleLine (*transport_marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
256         transport_marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
257
258         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_loop_range_view), false));
259         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_punch_range_view), false));
260         
261         double time_height = timebar_height * 5;
262         double time_width = FLT_MAX/frames_per_unit;
263         time_canvas.set_scroll_region(0.0, 0.0, time_width, time_height);
264         
265         edit_cursor = new Cursor (*this, "blue", &Editor::canvas_edit_cursor_event);
266         playhead_cursor = new Cursor (*this, "red", &Editor::canvas_playhead_cursor_event);
267         
268         track_canvas.signal_size_allocate().connect (mem_fun(*this, &Editor::track_canvas_allocate));
269 }
270
271 void
272 Editor::track_canvas_allocate (Gtk::Allocation alloc)
273 {
274         static bool first_time = true;
275
276         canvas_width = alloc.get_width();
277         canvas_height = alloc.get_height();
278
279         zoom_range_clock.set ((jack_nframes_t) floor ((canvas_width * frames_per_unit)));
280         edit_cursor->set_position (edit_cursor->current_frame);
281         playhead_cursor->set_position (playhead_cursor->current_frame);
282
283         reset_hscrollbar_stepping ();
284         reset_scrolling_region ();
285
286         if (edit_cursor) edit_cursor->set_length (canvas_height);
287         if (playhead_cursor) playhead_cursor->set_length (canvas_height);
288
289         if (marker_drag_line) {
290                 marker_drag_line_points.back().set_x(canvas_height);
291                 marker_drag_line->property_points() = marker_drag_line_points;
292         }
293
294         if (range_marker_drag_rect) {
295                 range_marker_drag_rect->property_y1() = 0.0;
296                 range_marker_drag_rect->property_y2() = canvas_height;
297         }
298
299         if (transport_loop_range_rect) {
300                 transport_loop_range_rect->property_y1() = 0.0;
301                 transport_loop_range_rect->property_y2() = canvas_height;
302         }
303
304         if (transport_punch_range_rect) {
305                 transport_punch_range_rect->property_y1() = 0.0;
306                 transport_punch_range_rect->property_y2() = canvas_height;
307         }
308
309         if (transport_punchin_line) {
310                 transport_punchin_line->property_y1() = 0.0;
311                 transport_punchin_line->property_y2() = canvas_height;
312         }
313
314         if (transport_punchout_line) {
315                 transport_punchout_line->property_y1() = 0.0;
316                 transport_punchout_line->property_y2() = canvas_height;
317         }
318                 
319         update_fixed_rulers ();
320
321         if (is_visible() && first_time) {
322                 tempo_map_changed (Change (0));
323                 first_time = false;
324         } else {
325                 redisplay_tempo ();
326         }
327         
328         Resized (); /* EMIT_SIGNAL */
329 }
330
331 void
332 Editor::reset_scrolling_region (Gtk::Allocation* alloc)
333 {
334         TreeModel::Children rows = route_display_model->children();
335         TreeModel::Children::iterator i;
336         double pos;
337
338         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
339                 TimeAxisView *tv = (*i)[route_display_columns.tv];
340                 if (tv != 0 && !tv->hidden()) {
341                         pos += tv->effective_height;
342                         pos += track_spacing;
343                 }
344         }
345
346         double last_canvas_unit =  last_canvas_frame / frames_per_unit;
347
348         track_canvas.set_scroll_region (0.0, 0.0, max (last_canvas_unit, canvas_width), pos);
349
350         // XXX what is the correct height value for the time canvas ? this overstates it
351         time_canvas.set_scroll_region ( 0.0, 0.0, max (last_canvas_unit, canvas_width), canvas_height);
352
353         controls_layout.queue_resize();
354 }
355
356 void
357 Editor::controls_layout_size_request (Requisition* req)
358 {
359         TreeModel::Children rows = route_display_model->children();
360         TreeModel::Children::iterator i;
361         double pos;
362
363         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
364                 TimeAxisView *tv = (*i)[route_display_columns.tv];
365                 if (tv != 0) {
366                         pos += tv->effective_height;
367                         pos += track_spacing;
368                 }
369         }
370
371         RefPtr<Gdk::Screen> screen = get_screen();
372
373         if (!screen) {
374                 screen = Gdk::Screen::get_default();
375         }
376
377         /* never let the width of the controls area shrink horizontally */
378
379         edit_controls_vbox.check_resize();
380
381         req->width = max (edit_controls_vbox.get_width(),  controls_layout.get_width());
382         
383         /* don't get too big. the fudge factors here are just guesses */
384         
385         req->width = min (req->width, screen->get_width() - 300);
386         req->height = min ((gint) pos, (screen->get_height() - 400));
387
388         /* this one is important: it determines how big the layout thinks it really is, as 
389            opposed to what it displays on the screen
390         */
391
392         controls_layout.set_size (req->width, (gint) pos);
393 }
394
395 bool
396 Editor::track_canvas_map_handler (GdkEventAny* ev)
397 {
398         track_canvas.get_window()->set_cursor (*current_canvas_cursor);
399         return false;
400 }
401
402 bool
403 Editor::time_canvas_map_handler (GdkEventAny* ev)
404 {
405         time_canvas.get_window()->set_cursor (*timebar_cursor);
406         return false;
407 }
408
409 void  
410 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
411                                          int x, int y, 
412                                          const SelectionData& data,
413                                          guint info, guint time)
414 {
415         if (data.get_target() == "regions") {
416                 drop_regions (context, x, y, data, info, time);
417         } else {
418                 drop_paths (context, x, y, data, info, time);
419         }
420 }
421
422 void
423 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
424                     int x, int y, 
425                     const SelectionData& data,
426                     guint info, guint time)
427 {
428         TimeAxisView* tvp;
429         AudioTimeAxisView* tv;
430         double cy;
431         vector<ustring> paths;
432         string spath;
433         GdkEvent ev;
434         jack_nframes_t frame;
435
436         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
437                 goto out;
438         }
439
440         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
441          */
442
443         double wx;
444         double wy;
445
446         track_canvas.window_to_world (x, y, wx, wy);
447         wx += horizontal_adjustment.get_value();
448         wy += vertical_adjustment.get_value();
449         
450         ev.type = GDK_BUTTON_RELEASE;
451         ev.button.x = wx;
452         ev.button.y = wy;
453
454         frame = event_frame (&ev, 0, &cy);
455
456         snap_to (frame);
457
458         if ((tvp = trackview_by_y_position (cy)) == 0) {
459
460                 /* drop onto canvas background: create new tracks */
461
462                 jack_nframes_t pos = 0;
463                 do_embed (paths, false, ImportAsTrack, 0, pos, false);
464                 
465         } else if ((tv = dynamic_cast<AudioTimeAxisView*>(tvp)) != 0) {
466
467                 /* check that its an audio track, not a bus */
468                 
469                 if (tv->get_diskstream()) {
470                         do_embed (paths, false, ImportToTrack, tv->audio_track(), frame, true);
471                 }
472         }
473
474   out:
475         context->drag_finish (true, false, time);
476 }
477
478 void
479 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
480                       int x, int y, 
481                       const SelectionData& data,
482                       guint info, guint time)
483 {
484         const DnDTreeView::SerializedObjectPointers* sr = reinterpret_cast<const DnDTreeView::SerializedObjectPointers*> (data.get_data());
485
486         for (uint32_t i = 0; i < sr->cnt; ++i) {
487
488                 Region* r = reinterpret_cast<Region*> (sr->ptr[i]);
489                 AudioRegion* ar;
490
491                 if ((ar = dynamic_cast<AudioRegion*>(r)) != 0) {
492                         insert_region_list_drag (*ar, x, y);
493                 }
494         }
495
496         context->drag_finish (true, false, time);
497 }
498
499 void
500 Editor::maybe_autoscroll (GdkEvent* event)
501 {
502         jack_nframes_t rightmost_frame = leftmost_frame + current_page_frames();
503         jack_nframes_t frame = drag_info.current_pointer_frame;
504         bool startit = false;
505
506         static int last_autoscroll_direction = 0;
507
508         if (frame > rightmost_frame) {
509
510                 if (rightmost_frame < max_frames) {
511                         autoscroll_direction = 1;
512                         startit = true;
513                 }
514
515         } else if (frame < leftmost_frame) {
516
517                 if (leftmost_frame > 0) {
518                         autoscroll_direction = -1;
519                         startit = true;
520                 }
521
522         } else {
523
524                 if (drag_info.last_pointer_frame > drag_info.current_pointer_frame) {
525                         autoscroll_direction = -1;
526                 } else {
527                         autoscroll_direction = 1;
528                 }
529         }
530
531
532         if (autoscroll_direction != last_autoscroll_direction) {
533                 stop_canvas_autoscroll ();
534         }
535         
536         if (startit && autoscroll_timeout_tag < 0) {
537                 start_canvas_autoscroll (autoscroll_direction);
538         }
539
540         last_autoscroll_direction = autoscroll_direction;
541 }
542
543 gint
544 Editor::_autoscroll_canvas (void *arg)
545 {
546         return ((Editor *) arg)->autoscroll_canvas ();
547 }
548
549 gint
550 Editor::autoscroll_canvas ()
551 {
552         jack_nframes_t new_frame;
553         bool keep_calling = true;
554         jack_nframes_t limit = max_frames - current_page_frames();
555         GdkEventMotion ev;
556         jack_nframes_t target_frame;
557
558         if (autoscroll_direction < 0) {
559                 if (leftmost_frame < autoscroll_distance) {
560                         new_frame = 0;
561                 } else {
562                         new_frame = leftmost_frame - autoscroll_distance;
563                 }
564                 target_frame = drag_info.current_pointer_frame - autoscroll_distance;
565         } else {
566                 if (leftmost_frame > limit - autoscroll_distance) {
567                         new_frame = limit;
568                 } else {
569                         new_frame = leftmost_frame + autoscroll_distance;
570                 }
571                 target_frame = drag_info.current_pointer_frame + autoscroll_distance;
572         }
573
574         if (new_frame != leftmost_frame) {
575                 reposition_x_origin (new_frame);
576         }
577
578         /* now fake a motion event to get the object that is being dragged to move too */
579
580         ev.type = GDK_MOTION_NOTIFY;
581         ev.state &= Gdk::BUTTON1_MASK;
582         ev.x = frame_to_unit (target_frame);
583         ev.y = drag_info.current_pointer_y;
584         motion_handler (drag_info.item, (GdkEvent*) &ev, drag_info.item_type, true);
585
586         if (new_frame == 0 || new_frame == limit) {
587                 /* we are done */
588                 return false;
589         }
590
591         autoscroll_cnt++;
592
593         if (autoscroll_cnt == 1) {
594
595                 /* connect the timeout so that we get called repeatedly */
596
597                 autoscroll_timeout_tag = gtk_timeout_add (20, _autoscroll_canvas, this);
598                 keep_calling = false;
599
600         } else if (autoscroll_cnt == 50) { /* 0.5 seconds */
601                 
602                 /* after about a while, speed up a bit by changing the timeout interval */
603
604                 autoscroll_distance = (jack_nframes_t) floor (current_page_frames()/50.0f);
605                 
606         } else if (autoscroll_cnt == 75) { /* 1.0 seconds */
607
608                 autoscroll_distance = (jack_nframes_t) floor (current_page_frames()/20.0f);
609
610         } else if (autoscroll_cnt == 100) { /* 1.5 seconds */
611
612                 /* after about another while, speed up by increasing the shift per callback */
613
614                 autoscroll_distance =  (jack_nframes_t) floor (current_page_frames()/10.0f);
615
616         } 
617
618         return keep_calling;
619 }
620
621 void
622 Editor::start_canvas_autoscroll (int dir)
623 {
624         if (!session) {
625                 return;
626         }
627
628         stop_canvas_autoscroll ();
629
630         autoscroll_direction = dir;
631         autoscroll_distance = (jack_nframes_t) floor (current_page_frames()/100.0);
632         autoscroll_cnt = 0;
633         
634         /* do it right now, which will start the repeated callbacks */
635         
636         autoscroll_canvas ();
637 }
638
639 void
640 Editor::stop_canvas_autoscroll ()
641 {
642         if (autoscroll_timeout_tag >= 0) {
643                 gtk_timeout_remove (autoscroll_timeout_tag);
644                 autoscroll_timeout_tag = -1;
645         }
646 }
647
648 gint
649 Editor::left_track_canvas (GdkEventCrossing *ev)
650 {
651         set_entered_track (0);
652         set_entered_regionview (0);
653         return FALSE;
654 }
655
656