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