reinstate functional clocks
[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         cerr << "drop on canvas, target = " << data.get_target() << endl;
439
440         if (data.get_target() == "regions") {
441                 drop_regions (context, x, y, data, info, time);
442         } else {
443                 drop_paths (context, x, y, data, info, time);
444         }
445 }
446
447 void
448 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
449                     int x, int y, 
450                     const SelectionData& data,
451                     guint info, guint time)
452 {
453         TimeAxisView* tvp;
454         AudioTimeAxisView* tv;
455         double cy;
456         vector<ustring> paths;
457         string spath;
458         GdkEvent ev;
459         nframes64_t frame;
460
461         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
462                 goto out;
463         }
464
465         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
466          */
467
468         double wx;
469         double wy;
470
471         track_canvas.window_to_world (x, y, wx, wy);
472         wx += horizontal_adjustment.get_value();
473         wy += vertical_adjustment.get_value();
474         
475         ev.type = GDK_BUTTON_RELEASE;
476         ev.button.x = wx;
477         ev.button.y = wy;
478
479         frame = event_frame (&ev, 0, &cy);
480
481         snap_to (frame);
482
483         if ((tvp = trackview_by_y_position (cy)) == 0) {
484
485                 /* drop onto canvas background: create new tracks */
486
487                 frame = 0;
488
489                 do_embed (paths, Editing::ImportDistinctFiles, ImportAsTrack, frame);
490                 
491         } else if ((tv = dynamic_cast<AudioTimeAxisView*>(tvp)) != 0) {
492
493                 /* check that its an audio track, not a bus */
494                 
495                 if (tv->get_diskstream()) {
496                         /* select the track, then embed */
497                         selection->set (tv);
498                         do_embed (paths, Editing::ImportDistinctFiles, ImportToTrack, frame);
499                 }
500         }
501
502   out:
503         context->drag_finish (true, false, time);
504 }
505
506 void
507 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
508                       int x, int y, 
509                       const SelectionData& data,
510                       guint info, guint time)
511 {
512         const SerializedObjectPointers<boost::shared_ptr<Region> >* sr = 
513                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Region> > *> (data.get_data());
514
515         for (uint32_t i = 0; i < sr->cnt; ++i) {
516
517                 boost::shared_ptr<Region> r = sr->data[i];
518                 boost::shared_ptr<AudioRegion> ar;
519
520                 if ((ar = boost::dynamic_pointer_cast<AudioRegion>(r)) != 0) {
521                         insert_region_list_drag (ar, x, y);
522                 }
523         }
524
525         context->drag_finish (true, false, time);
526 }
527
528 void
529 Editor::maybe_autoscroll (GdkEvent* event)
530 {
531         nframes_t rightmost_frame = leftmost_frame + current_page_frames();
532         nframes_t frame = drag_info.current_pointer_frame;
533         bool startit = false;
534
535         static int last_autoscroll_direction = 0;
536
537         if (frame > rightmost_frame) {
538
539                 if (rightmost_frame < max_frames) {
540                         autoscroll_direction = 1;
541                         startit = true;
542                 }
543
544         } else if (frame < leftmost_frame) {
545
546                 if (leftmost_frame > 0) {
547                         autoscroll_direction = -1;
548                         startit = true;
549                 }
550
551         } else {
552
553                 if (drag_info.last_pointer_frame > drag_info.current_pointer_frame) {
554                         autoscroll_direction = -1;
555                 } else {
556                         autoscroll_direction = 1;
557                 }
558         }
559
560
561         if ((autoscroll_direction != last_autoscroll_direction) || (leftmost_frame < frame < rightmost_frame)) {
562                 stop_canvas_autoscroll ();
563         }
564         
565         if (startit && autoscroll_timeout_tag < 0) {
566                 start_canvas_autoscroll (autoscroll_direction);
567         }
568
569         last_autoscroll_direction = autoscroll_direction;
570 }
571
572 gint
573 Editor::_autoscroll_canvas (void *arg)
574 {
575         return ((Editor *) arg)->autoscroll_canvas ();
576 }
577
578 bool
579 Editor::autoscroll_canvas ()
580 {
581         nframes_t new_frame;
582         nframes_t limit = max_frames - current_page_frames();
583         GdkEventMotion ev;
584         nframes_t target_frame;
585
586         if (autoscroll_direction < 0) {
587                 if (leftmost_frame < autoscroll_distance) {
588                         new_frame = 0;
589                 } else {
590                         new_frame = leftmost_frame - autoscroll_distance;
591                 }
592                 target_frame = drag_info.current_pointer_frame - autoscroll_distance;
593         } else {
594                 if (leftmost_frame > limit - autoscroll_distance) {
595                         new_frame = limit;
596                 } else {
597                         new_frame = leftmost_frame + autoscroll_distance;
598                 }
599                 target_frame = drag_info.current_pointer_frame + autoscroll_distance;
600         }
601
602         /* now fake a motion event to get the object that is being dragged to move too */
603
604         ev.type = GDK_MOTION_NOTIFY;
605         ev.state &= Gdk::BUTTON1_MASK;
606         ev.x = frame_to_unit (target_frame);
607         ev.y = drag_info.current_pointer_y;
608         motion_handler (drag_info.item, (GdkEvent*) &ev, drag_info.item_type, true);
609
610         if (new_frame == 0 || new_frame == limit) {
611                 /* we are done */
612                 return false;
613         }
614
615         autoscroll_cnt++;
616
617         if (autoscroll_cnt == 1) {
618
619                 /* connect the timeout so that we get called repeatedly */
620
621                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
622                 return false;
623
624         } 
625
626         if (new_frame != leftmost_frame) {
627                 reset_x_origin (new_frame);
628         }
629
630         if (autoscroll_cnt == 50) { /* 0.5 seconds */
631                 
632                 /* after about a while, speed up a bit by changing the timeout interval */
633
634                 autoscroll_distance = (nframes_t) floor (current_page_frames()/30.0f);
635                 
636         } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
637
638                 autoscroll_distance = (nframes_t) floor (current_page_frames()/20.0f);
639
640         } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
641
642                 /* after about another while, speed up by increasing the shift per callback */
643
644                 autoscroll_distance =  (nframes_t) floor (current_page_frames()/10.0f);
645
646         } 
647
648         return true;
649 }
650
651 void
652 Editor::start_canvas_autoscroll (int dir)
653 {
654         if (!session || autoscroll_active) {
655                 return;
656         }
657
658         stop_canvas_autoscroll ();
659
660         autoscroll_active = true;
661         autoscroll_direction = dir;
662         autoscroll_distance = (nframes_t) floor (current_page_frames()/50.0);
663         autoscroll_cnt = 0;
664         
665         /* do it right now, which will start the repeated callbacks */
666
667         autoscroll_canvas ();
668 }
669
670 void
671 Editor::stop_canvas_autoscroll ()
672 {
673         if (autoscroll_timeout_tag >= 0) {
674                 g_source_remove (autoscroll_timeout_tag);
675                 autoscroll_timeout_tag = -1;
676         }
677
678         autoscroll_active = false;
679 }
680
681 gint
682 Editor::left_track_canvas (GdkEventCrossing *ev)
683 {
684         set_entered_track (0);
685         set_entered_regionview (0);
686         return FALSE;
687 }
688
689 void
690 Editor::tie_vertical_scrolling ()
691 {
692         double y1 = vertical_adjustment.get_value();
693
694         playhead_cursor->set_y_axis (y1);
695
696         range_marker_drag_rect->property_y1() = y1;
697         range_marker_drag_rect->property_y2() = y1 + canvas_height;
698         transport_loop_range_rect->property_y1() = y1;
699         transport_loop_range_rect->property_y2() = y1 + canvas_height;
700         transport_punch_range_rect->property_y1() = y1;
701         transport_punch_range_rect->property_y2() = y1 + canvas_height;
702         transport_punchin_line->property_y1() = y1;
703         transport_punchin_line->property_y2() = y1 + canvas_height;
704         transport_punchout_line->property_y1() = y1;
705         transport_punchout_line->property_y2() = y1 + canvas_height;
706
707         if (!selection->markers.empty()) {
708                 for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {            
709                         (*x)->set_line_vpos (y1, canvas_height);
710                 }
711         }
712
713         if (logo_item) {
714                 logo_item->property_y() = y1;
715         }
716
717         /* this will do an immediate redraw */
718
719         controls_layout.get_vadjustment()->set_value (y1);
720 }
721
722 void 
723 Editor::canvas_horizontally_scrolled ()
724 {
725         nframes64_t time_origin = (nframes_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
726
727         if (time_origin != leftmost_frame) {
728                 canvas_scroll_to (time_origin);
729         }
730 }
731
732 void
733 Editor::canvas_scroll_to (nframes64_t time_origin)
734 {
735         leftmost_frame = time_origin;
736         nframes_t rightmost_frame = leftmost_frame + current_page_frames ();
737
738         if (rightmost_frame > last_canvas_frame) {
739                 last_canvas_frame = rightmost_frame;
740                 reset_scrolling_region ();
741         }
742
743         if (logo_item) {
744                 logo_item->property_x() = horizontal_adjustment.get_value ();
745         }
746
747         update_fixed_rulers ();
748
749         redisplay_tempo (!_dragging_hscrollbar);
750 }
751
752 void
753 Editor::color_handler()
754 {
755         playhead_cursor->canvas_item.property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_PlayHead.get();
756         verbose_canvas_cursor->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_VerboseCanvasCursor.get();
757
758         meter_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MeterBar.get();
759         meter_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
760
761         tempo_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TempoBar.get();
762         tempo_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
763
764         marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBar.get();
765         marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
766
767         cd_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CDMarkerBar.get();
768         cd_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
769
770         range_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeMarkerBar.get();
771         range_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
772
773         transport_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportMarkerBar.get();
774         transport_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
775
776         range_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
777         range_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
778
779         transport_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
780         transport_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
781
782         marker_drag_line->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerDragLine.get();
783
784         range_marker_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
785         range_marker_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
786
787         transport_loop_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
788         transport_loop_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
789
790         transport_punch_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
791         transport_punch_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
792
793         transport_punchin_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
794         transport_punchout_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
795
796         zoom_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
797         zoom_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
798
799         rubberband_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
800         rubberband_rect->property_fill_color_rgba() = (guint32) ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
801
802         location_marker_color = ARDOUR_UI::config()->canvasvar_LocationMarker.get();
803         location_range_color = ARDOUR_UI::config()->canvasvar_LocationRange.get();
804         location_cd_marker_color = ARDOUR_UI::config()->canvasvar_LocationCDMarker.get();
805         location_loop_color = ARDOUR_UI::config()->canvasvar_LocationLoop.get();
806         location_punch_color = ARDOUR_UI::config()->canvasvar_LocationPunch.get();
807
808         refresh_location_display ();
809         redisplay_tempo (true);
810
811         if (session)
812                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
813 }
814
815 void
816 Editor::flush_canvas ()
817 {
818         if (is_mapped()) {
819                 track_canvas.update_now ();
820                 // gdk_window_process_updates (GTK_LAYOUT(track_canvas.gobj())->bin_window, true);
821         }
822 }
823