more NSD madness curing; fix up canvas range rects and marker line height mgmt
[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         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
188         range_bar_drag_rect->property_outline_pixels() = 0;
189         range_bar_drag_rect->hide ();
190         
191         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
192         transport_bar_drag_rect->property_outline_pixels() = 0;
193         transport_bar_drag_rect->hide ();
194         
195         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
196         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
197
198         marker_drag_line = new ArdourCanvas::Line (*track_canvas.root());
199         marker_drag_line->property_width_pixels() = 1;
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->hide ();
205         
206         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
207         transport_loop_range_rect->property_outline_pixels() = 1;
208         transport_loop_range_rect->hide();
209
210         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
211         transport_punch_range_rect->property_outline_pixels() = 0;
212         transport_punch_range_rect->hide();
213         
214         transport_loop_range_rect->lower_to_bottom (); // loop on the bottom
215
216         transport_punchin_line = new ArdourCanvas::SimpleLine (*time_line_group);
217         transport_punchin_line->property_x1() = 0.0;
218         transport_punchin_line->property_y1() = 0.0;
219         transport_punchin_line->property_x2() = 0.0;
220         transport_punchin_line->property_y2() = 0.0;
221         transport_punchin_line->hide ();
222         
223         transport_punchout_line  = new ArdourCanvas::SimpleLine (*time_line_group);
224         transport_punchout_line->property_x1() = 0.0;
225         transport_punchout_line->property_y1() = 0.0;
226         transport_punchout_line->property_x2() = 0.0;
227         transport_punchout_line->property_y2() = 0.0;
228         transport_punchout_line->hide();
229         
230         // used to show zoom mode active zooming
231         zoom_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
232         zoom_rect->property_outline_pixels() = 1;
233         zoom_rect->hide();
234         
235         zoom_rect->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
236         
237         // used as rubberband rect
238         rubberband_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
239         rubberband_rect->property_outline_pixels() = 1;
240         rubberband_rect->hide();
241         
242         tempo_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
243         meter_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
244         marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
245         cd_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_cd_marker_bar_event), cd_marker_bar));
246         range_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
247         transport_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
248         
249         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_loop_range_view), false));
250         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_punch_range_view), false));
251         
252         double time_height = timebar_height * 5;
253         double time_width = FLT_MAX/frames_per_unit;
254         time_canvas.set_scroll_region(0.0, 0.0, time_width, time_height);
255
256         playhead_cursor = new Cursor (*this, &Editor::canvas_playhead_cursor_event);
257
258         initial_ruler_update_required = true;
259         track_canvas.signal_size_allocate().connect (mem_fun(*this, &Editor::track_canvas_allocate));
260
261         if (logo_item) {
262                 logo_item->lower_to_bottom ();
263         }
264
265         ColorsChanged.connect (mem_fun (*this, &Editor::color_handler));
266         color_handler();
267
268 }
269
270 void
271 Editor::track_canvas_allocate (Gtk::Allocation alloc)
272 {
273         canvas_allocation = alloc;
274
275         if (!initial_ruler_update_required) {
276                 if (!canvas_idle_queued) {
277                         /* call this first so that we do stuff before any pending redraw */
278                         Glib::signal_idle().connect (mem_fun (*this, &Editor::track_canvas_size_allocated), false);
279                         canvas_idle_queued = true;
280                 }
281                 return;
282         } 
283
284         initial_ruler_update_required = false;
285         track_canvas_size_allocated ();
286 }
287
288 bool
289 Editor::track_canvas_size_allocated ()
290 {
291         if (canvas_idle_queued) {
292                 canvas_idle_queued = false;
293         }
294
295         canvas_width = canvas_allocation.get_width();
296         canvas_height = canvas_allocation.get_height();
297
298         full_canvas_height = canvas_height;
299
300         if (session) {
301                 TrackViewList::iterator i;
302                 double height = 0;
303
304                 for (i = track_views.begin(); i != track_views.end(); ++i) {
305                         if ((*i)->control_parent) {
306                                 height += (*i)->effective_height;
307                         }
308                 }
309                 
310                 full_canvas_height = height;
311         }
312
313         zoom_range_clock.set ((nframes_t) floor ((canvas_width * frames_per_unit)));
314         playhead_cursor->set_position (playhead_cursor->current_frame);
315
316         reset_hscrollbar_stepping ();
317         reset_scrolling_region ();
318
319         if (playhead_cursor) playhead_cursor->set_length (canvas_height);
320
321         double y1 = vertical_adjustment.get_value ();
322         
323         for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {
324                 (*x)->set_line_vpos (y1, canvas_height);
325         }
326
327         range_marker_drag_rect->property_y1() = y1;
328         range_marker_drag_rect->property_y2() = y1 + canvas_height;
329         transport_loop_range_rect->property_y1() = y1;
330         transport_loop_range_rect->property_y2() = y1 + canvas_height;
331         transport_punch_range_rect->property_y1() = y1;
332         transport_punch_range_rect->property_y2() = y1 + canvas_height;
333         transport_punchin_line->property_y1() = y1;
334         transport_punchin_line->property_y2() = y1 + canvas_height;
335         transport_punchout_line->property_y1() = y1;
336         transport_punchout_line->property_y2() = y1 + canvas_height;
337         
338         update_fixed_rulers();
339         redisplay_tempo (true);
340
341         Resized (); /* EMIT_SIGNAL */
342
343         return false;
344 }
345
346 void
347 Editor::reset_scrolling_region (Gtk::Allocation* alloc)
348 {
349         TreeModel::Children rows = route_display_model->children();
350         TreeModel::Children::iterator i;
351         double pos;
352
353         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
354                 TimeAxisView *tv = (*i)[route_display_columns.tv];
355                 if (tv != 0 && !tv->hidden()) {
356                         pos += tv->effective_height;
357                 }
358         }
359
360         double last_canvas_unit =  max ((last_canvas_frame / frames_per_unit), canvas_width);
361
362         track_canvas.set_scroll_region (0.0, 0.0, last_canvas_unit, pos);
363
364         // XXX what is the correct height value for the time canvas ? this overstates it
365         time_canvas.set_scroll_region ( 0.0, 0.0, last_canvas_unit, canvas_height);
366
367         range_marker_drag_rect->property_y2() = canvas_height;
368         transport_loop_range_rect->property_y2() = canvas_height;
369         transport_punch_range_rect->property_y2() = canvas_height;
370         transport_punchin_line->property_y2() = canvas_height;
371         transport_punchout_line->property_y2() = canvas_height;
372
373         update_punch_range_view (true);
374
375         controls_layout.queue_resize();
376 }
377
378 void
379 Editor::controls_layout_size_request (Requisition* req)
380 {
381         TreeModel::Children rows = route_display_model->children();
382         TreeModel::Children::iterator i;
383         double pos;
384
385         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
386                 TimeAxisView *tv = (*i)[route_display_columns.tv];
387                 if (tv != 0) {
388                         pos += tv->effective_height;
389                 }
390         }
391
392         RefPtr<Gdk::Screen> screen = get_screen();
393
394         if (!screen) {
395                 screen = Gdk::Screen::get_default();
396         }
397
398         edit_controls_vbox.check_resize();
399         req->width = max (edit_controls_vbox.get_width(),  controls_layout.get_width());
400
401         /* don't get too big. the fudge factors here are just guesses */
402         
403         req->width = min (req->width, screen->get_width() - 300);
404         req->height = min ((gint) pos, (screen->get_height() - 400));
405
406         /* this one is important: it determines how big the layout thinks it really is, as 
407            opposed to what it displays on the screen
408         */
409         
410         controls_layout.set_size (edit_controls_vbox.get_width(), (gint) pos);
411         controls_layout.set_size_request(edit_controls_vbox.get_width(), -1);
412         zoom_box.set_size_request(edit_controls_vbox.get_width(), -1);
413         time_button_frame.set_size_request(edit_controls_vbox.get_width() + edit_vscrollbar.get_width(), -1);
414
415         //cerr << "sizes = " << req->width << " " << edit_controls_vbox.get_width() << " " << controls_layout.get_width() << " " << zoom_box.get_width() << " " << time_button_frame.get_width() << endl;//DEBUG        
416 }
417
418 bool
419 Editor::track_canvas_map_handler (GdkEventAny* ev)
420 {
421         track_canvas.get_window()->set_cursor (*current_canvas_cursor);
422         return false;
423 }
424
425 bool
426 Editor::time_canvas_map_handler (GdkEventAny* ev)
427 {
428         time_canvas.get_window()->set_cursor (*timebar_cursor);
429         return false;
430 }
431
432 void  
433 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
434                                          int x, int y, 
435                                          const SelectionData& data,
436                                          guint info, guint time)
437 {
438         if (data.get_target() == "regions") {
439                 drop_regions (context, x, y, data, info, time);
440         } else {
441                 drop_paths (context, x, y, data, info, time);
442         }
443 }
444
445 void
446 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
447                     int x, int y, 
448                     const SelectionData& data,
449                     guint info, guint time)
450 {
451         TimeAxisView* tvp;
452         AudioTimeAxisView* tv;
453         double cy;
454         vector<ustring> paths;
455         string spath;
456         GdkEvent ev;
457         nframes64_t frame;
458
459         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
460                 goto out;
461         }
462
463         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
464          */
465
466         double wx;
467         double wy;
468
469         track_canvas.window_to_world (x, y, wx, wy);
470         wx += horizontal_adjustment.get_value();
471         wy += vertical_adjustment.get_value();
472         
473         ev.type = GDK_BUTTON_RELEASE;
474         ev.button.x = wx;
475         ev.button.y = wy;
476
477         frame = event_frame (&ev, 0, &cy);
478
479         snap_to (frame);
480
481         if ((tvp = trackview_by_y_position (cy)) == 0) {
482
483                 /* drop onto canvas background: create new tracks */
484
485                 frame = 0;
486
487                 do_embed (paths, Editing::ImportDistinctFiles, ImportAsTrack, frame);
488                 
489         } else if ((tv = dynamic_cast<AudioTimeAxisView*>(tvp)) != 0) {
490
491                 /* check that its an audio track, not a bus */
492                 
493                 if (tv->get_diskstream()) {
494                         /* select the track, then embed */
495                         selection->set (tv);
496                         do_embed (paths, Editing::ImportDistinctFiles, ImportToTrack, frame);
497                 }
498         }
499
500   out:
501         context->drag_finish (true, false, time);
502 }
503
504 void
505 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
506                       int x, int y, 
507                       const SelectionData& data,
508                       guint info, guint time)
509 {
510         const SerializedObjectPointers<boost::shared_ptr<Region> >* sr = 
511                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Region> > *> (data.get_data());
512
513         for (uint32_t i = 0; i < sr->cnt; ++i) {
514
515                 boost::shared_ptr<Region> r = sr->data[i];
516                 boost::shared_ptr<AudioRegion> ar;
517
518                 if ((ar = boost::dynamic_pointer_cast<AudioRegion>(r)) != 0) {
519                         insert_region_list_drag (ar, x, y);
520                 }
521         }
522
523         context->drag_finish (true, false, time);
524 }
525
526 void
527 Editor::maybe_autoscroll (GdkEvent* event)
528 {
529         nframes_t rightmost_frame = leftmost_frame + current_page_frames();
530         nframes_t frame = drag_info.current_pointer_frame;
531         bool startit = false;
532
533         static int last_autoscroll_direction = 0;
534
535         if (frame > rightmost_frame) {
536
537                 if (rightmost_frame < max_frames) {
538                         autoscroll_direction = 1;
539                         startit = true;
540                 }
541
542         } else if (frame < leftmost_frame) {
543
544                 if (leftmost_frame > 0) {
545                         autoscroll_direction = -1;
546                         startit = true;
547                 }
548
549         } else {
550
551                 if (drag_info.last_pointer_frame > drag_info.current_pointer_frame) {
552                         autoscroll_direction = -1;
553                 } else {
554                         autoscroll_direction = 1;
555                 }
556         }
557
558
559         if ((autoscroll_direction != last_autoscroll_direction) || (leftmost_frame < frame < rightmost_frame)) {
560                 stop_canvas_autoscroll ();
561         }
562         
563         if (startit && autoscroll_timeout_tag < 0) {
564                 start_canvas_autoscroll (autoscroll_direction);
565         }
566
567         last_autoscroll_direction = autoscroll_direction;
568 }
569
570 gint
571 Editor::_autoscroll_canvas (void *arg)
572 {
573         return ((Editor *) arg)->autoscroll_canvas ();
574 }
575
576 bool
577 Editor::autoscroll_canvas ()
578 {
579         nframes_t new_frame;
580         nframes_t limit = max_frames - current_page_frames();
581         GdkEventMotion ev;
582         nframes_t target_frame;
583
584         if (autoscroll_direction < 0) {
585                 if (leftmost_frame < autoscroll_distance) {
586                         new_frame = 0;
587                 } else {
588                         new_frame = leftmost_frame - autoscroll_distance;
589                 }
590                 target_frame = drag_info.current_pointer_frame - autoscroll_distance;
591         } else {
592                 if (leftmost_frame > limit - autoscroll_distance) {
593                         new_frame = limit;
594                 } else {
595                         new_frame = leftmost_frame + autoscroll_distance;
596                 }
597                 target_frame = drag_info.current_pointer_frame + autoscroll_distance;
598         }
599
600         /* now fake a motion event to get the object that is being dragged to move too */
601
602         ev.type = GDK_MOTION_NOTIFY;
603         ev.state &= Gdk::BUTTON1_MASK;
604         ev.x = frame_to_unit (target_frame);
605         ev.y = drag_info.current_pointer_y;
606         motion_handler (drag_info.item, (GdkEvent*) &ev, drag_info.item_type, true);
607
608         if (new_frame == 0 || new_frame == limit) {
609                 /* we are done */
610                 return false;
611         }
612
613         autoscroll_cnt++;
614
615         if (autoscroll_cnt == 1) {
616
617                 /* connect the timeout so that we get called repeatedly */
618
619                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
620                 return false;
621
622         } 
623
624         if (new_frame != leftmost_frame) {
625                 reset_x_origin (new_frame);
626         }
627
628         if (autoscroll_cnt == 50) { /* 0.5 seconds */
629                 
630                 /* after about a while, speed up a bit by changing the timeout interval */
631
632                 autoscroll_distance = (nframes_t) floor (current_page_frames()/30.0f);
633                 
634         } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
635
636                 autoscroll_distance = (nframes_t) floor (current_page_frames()/20.0f);
637
638         } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
639
640                 /* after about another while, speed up by increasing the shift per callback */
641
642                 autoscroll_distance =  (nframes_t) floor (current_page_frames()/10.0f);
643
644         } 
645
646         return true;
647 }
648
649 void
650 Editor::start_canvas_autoscroll (int dir)
651 {
652         if (!session || autoscroll_active) {
653                 return;
654         }
655
656         stop_canvas_autoscroll ();
657
658         autoscroll_active = true;
659         autoscroll_direction = dir;
660         autoscroll_distance = (nframes_t) floor (current_page_frames()/50.0);
661         autoscroll_cnt = 0;
662         
663         /* do it right now, which will start the repeated callbacks */
664
665         autoscroll_canvas ();
666 }
667
668 void
669 Editor::stop_canvas_autoscroll ()
670 {
671         if (autoscroll_timeout_tag >= 0) {
672                 g_source_remove (autoscroll_timeout_tag);
673                 autoscroll_timeout_tag = -1;
674         }
675
676         autoscroll_active = false;
677 }
678
679 gint
680 Editor::left_track_canvas (GdkEventCrossing *ev)
681 {
682         set_entered_track (0);
683         set_entered_regionview (0);
684         return FALSE;
685 }
686
687 void
688 Editor::tie_vertical_scrolling ()
689 {
690         double y1 = vertical_adjustment.get_value();
691
692         playhead_cursor->set_y_axis (y1);
693
694         range_marker_drag_rect->property_y1() = y1;
695         range_marker_drag_rect->property_y2() = y1 + canvas_height;
696         transport_loop_range_rect->property_y1() = y1;
697         transport_loop_range_rect->property_y2() = y1 + canvas_height;
698         transport_punch_range_rect->property_y1() = y1;
699         transport_punch_range_rect->property_y2() = y1 + canvas_height;
700         transport_punchin_line->property_y1() = y1;
701         transport_punchin_line->property_y2() = y1 + canvas_height;
702         transport_punchout_line->property_y1() = y1;
703         transport_punchout_line->property_y2() = y1 + canvas_height;
704
705         if (!selection->markers.empty()) {
706                 for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {            
707                         (*x)->set_line_vpos (y1, canvas_height);
708                 }
709         }
710
711         if (logo_item) {
712                 logo_item->property_y() = y1;
713         }
714
715         /* this will do an immediate redraw */
716
717         controls_layout.get_vadjustment()->set_value (y1);
718 }
719
720 void 
721 Editor::canvas_horizontally_scrolled ()
722 {
723         nframes64_t time_origin = (nframes_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
724
725         if (time_origin != leftmost_frame) {
726                 canvas_scroll_to (time_origin);
727         }
728 }
729
730 void
731 Editor::canvas_scroll_to (nframes64_t time_origin)
732 {
733         leftmost_frame = time_origin;
734         nframes_t rightmost_frame = leftmost_frame + current_page_frames ();
735
736         if (rightmost_frame > last_canvas_frame) {
737                 last_canvas_frame = rightmost_frame;
738                 reset_scrolling_region ();
739         }
740
741         if (logo_item) {
742                 logo_item->property_x() = horizontal_adjustment.get_value ();
743         }
744
745         update_fixed_rulers ();
746
747         redisplay_tempo (!_dragging_hscrollbar);
748 }
749
750 void
751 Editor::color_handler()
752 {
753         playhead_cursor->canvas_item.property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_PlayHead.get();
754         verbose_canvas_cursor->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_VerboseCanvasCursor.get();
755
756         meter_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MeterBar.get();
757         meter_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
758
759         tempo_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TempoBar.get();
760         tempo_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
761
762         marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBar.get();
763         marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
764
765         cd_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CDMarkerBar.get();
766         cd_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
767
768         range_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeMarkerBar.get();
769         range_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
770
771         transport_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportMarkerBar.get();
772         transport_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
773
774         range_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
775         range_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
776
777         transport_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
778         transport_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
779
780         marker_drag_line->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerDragLine.get();
781
782         range_marker_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
783         range_marker_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
784
785         transport_loop_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
786         transport_loop_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
787
788         transport_punch_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
789         transport_punch_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
790
791         transport_punchin_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
792         transport_punchout_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
793
794         zoom_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
795         zoom_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
796
797         rubberband_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
798         rubberband_rect->property_fill_color_rgba() = (guint32) ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
799
800         location_marker_color = ARDOUR_UI::config()->canvasvar_LocationMarker.get();
801         location_range_color = ARDOUR_UI::config()->canvasvar_LocationRange.get();
802         location_cd_marker_color = ARDOUR_UI::config()->canvasvar_LocationCDMarker.get();
803         location_loop_color = ARDOUR_UI::config()->canvasvar_LocationLoop.get();
804         location_punch_color = ARDOUR_UI::config()->canvasvar_LocationPunch.get();
805
806         refresh_location_display ();
807         redisplay_tempo (true);
808
809         if (session)
810                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
811 }
812
813 void
814 Editor::flush_canvas ()
815 {
816         if (is_mapped()) {
817                 track_canvas.update_now ();
818                 // gdk_window_process_updates (GTK_LAYOUT(track_canvas.gobj())->bin_window, true);
819         }
820 }
821