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