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