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