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