1331ce8759081809535dfa30387156e52b5a4218
[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         if (getenv ("ARDOUR_NON_AA_CANVAS")) {
102                 track_canvas = new ArdourCanvas::Canvas ();
103                 time_canvas = new ArdourCanvas::Canvas ();
104         } else {
105                 track_canvas = new ArdourCanvas::CanvasAA ();
106                 time_canvas = new ArdourCanvas::CanvasAA ();
107         }
108         
109         ArdourCanvas::init ();
110         ardour_canvas_type_init ();
111
112         /* don't try to center the canvas */
113
114         track_canvas->set_center_scroll_region (false);
115         track_canvas->set_dither        (Gdk::RGB_DITHER_NONE);
116
117         /* need to handle 4 specific types of events as catch-alls */
118
119         track_canvas->signal_scroll_event().connect (mem_fun (*this, &Editor::track_canvas_scroll_event));
120         track_canvas->signal_motion_notify_event().connect (mem_fun (*this, &Editor::track_canvas_motion_notify_event));
121         track_canvas->signal_button_press_event().connect (mem_fun (*this, &Editor::track_canvas_button_press_event));
122         track_canvas->signal_button_release_event().connect (mem_fun (*this, &Editor::track_canvas_button_release_event));
123
124         /* just scroll stuff for the timecanvas */
125         time_canvas->signal_scroll_event().connect (mem_fun (*this, &Editor::time_canvas_scroll_event));
126
127         track_canvas->set_name ("EditorMainCanvas");
128         track_canvas->add_events (Gdk::POINTER_MOTION_HINT_MASK|Gdk::SCROLL_MASK);
129         track_canvas->signal_leave_notify_event().connect (mem_fun(*this, &Editor::left_track_canvas));
130         track_canvas->set_flags (CAN_FOCUS);
131
132         /* set up drag-n-drop */
133
134         vector<TargetEntry> target_table;
135         
136         // Drag-N-Drop from the region list can generate this target
137         target_table.push_back (TargetEntry ("regions"));
138         target_table.push_back (TargetEntry ("routes"));
139
140         target_table.push_back (TargetEntry ("text/plain"));
141         target_table.push_back (TargetEntry ("text/uri-list"));
142         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
143
144         track_canvas->drag_dest_set (target_table);
145         track_canvas->signal_drag_data_received().connect (mem_fun(*this, &Editor::track_canvas_drag_data_received));
146
147         /* stuff for the verbose canvas cursor */
148
149         Pango::FontDescription* font = get_font_for_style (N_("VerboseCanvasCursor"));
150
151         verbose_canvas_cursor = new ArdourCanvas::Text (*track_canvas->root());
152         verbose_canvas_cursor->property_font_desc() = *font;
153         verbose_canvas_cursor->property_anchor() = ANCHOR_NW;
154         
155         delete font;
156
157         verbose_cursor_visible = false;
158         
159         if (Profile->get_sae()) {
160                 Image img (::get_icon (X_("saelogo")));
161                 logo_item = new ArdourCanvas::Pixbuf (*track_canvas->root(), 0.0, 0.0, img.get_pixbuf());
162                 // logo_item->property_height_in_pixels() = true;
163                 // logo_item->property_width_in_pixels() = true;
164                 // logo_item->property_height_set() = true;
165                 // logo_item->property_width_set() = true;
166                 logo_item->show ();
167         }
168         
169         /* a group to hold time (measure) lines */
170         
171         time_line_group = new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
172         tempo_lines = new TempoLines(*track_canvas, time_line_group);
173         cursor_group = new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
174
175         time_canvas->set_name ("EditorTimeCanvas");
176         time_canvas->add_events (Gdk::POINTER_MOTION_HINT_MASK);
177         time_canvas->set_flags (CAN_FOCUS);
178         time_canvas->set_center_scroll_region (false);
179         time_canvas->set_dither (Gdk::RGB_DITHER_NONE);
180         
181         marker_time_line_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, 0.0);
182         marker_tempo_lines = new TempoLines(*time_canvas, marker_time_line_group);
183         
184         meter_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, 0.0);
185         tempo_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height);
186         range_marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 2.0);
187         transport_marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 3.0);
188         marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 4.0);
189         cd_marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 5.0);
190         
191         tempo_bar = new ArdourCanvas::SimpleRect (*tempo_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
192
193         tempo_bar->property_outline_what() = (0x1 | 0x8);
194         tempo_bar->property_outline_pixels() = 1;
195         
196         meter_bar = new ArdourCanvas::SimpleRect (*meter_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
197
198         meter_bar->property_outline_what() = (0x1 | 0x8);
199         meter_bar->property_outline_pixels() = 1;
200         
201         marker_bar = new ArdourCanvas::SimpleRect (*marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
202
203         marker_bar->property_outline_what() = (0x1 | 0x8);
204         marker_bar->property_outline_pixels() = 1;
205
206         cd_marker_bar = new ArdourCanvas::SimpleRect (*cd_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
207         cd_marker_bar->property_outline_what() = (0x1 | 0x8);
208         cd_marker_bar->property_outline_pixels() = 1;
209         
210         range_marker_bar = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
211         range_marker_bar->property_outline_what() = (0x1 | 0x8);
212         range_marker_bar->property_outline_pixels() = 1;
213         
214         transport_marker_bar = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
215
216         transport_marker_bar->property_outline_what() = (0x1 | 0x8);
217         transport_marker_bar->property_outline_pixels() = 1;
218         
219         cd_marker_bar_drag_rect = new ArdourCanvas::SimpleRect (*cd_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
220         // cd_marker_bar_drag_rect->property_outline_pixels() = 0;
221         cd_marker_bar_drag_rect->hide ();
222
223         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
224         range_bar_drag_rect->property_outline_pixels() = 0;
225         range_bar_drag_rect->hide ();
226         
227         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
228         transport_bar_drag_rect->property_outline_pixels() = 0;
229         transport_bar_drag_rect->hide ();
230         
231         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
232         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
233
234         marker_drag_line = new ArdourCanvas::Line (*track_canvas->root());
235         marker_drag_line->property_width_pixels() = 1;
236         marker_drag_line->property_points() = marker_drag_line_points;
237         marker_drag_line->hide();
238
239         range_marker_drag_rect = new ArdourCanvas::SimpleRect (*track_canvas->root(), 0.0, 0.0, 0.0, 0.0);
240         range_marker_drag_rect->hide ();
241         
242         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
243         transport_loop_range_rect->property_outline_pixels() = 1;
244         transport_loop_range_rect->hide();
245
246         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
247         transport_punch_range_rect->property_outline_pixels() = 0;
248         transport_punch_range_rect->hide();
249         
250         transport_loop_range_rect->lower_to_bottom (); // loop on the bottom
251
252         transport_punchin_line = new ArdourCanvas::SimpleLine (*time_line_group);
253         transport_punchin_line->property_x1() = 0.0;
254         transport_punchin_line->property_y1() = 0.0;
255         transport_punchin_line->property_x2() = 0.0;
256         transport_punchin_line->property_y2() = 0.0;
257         transport_punchin_line->hide ();
258         
259         transport_punchout_line  = new ArdourCanvas::SimpleLine (*time_line_group);
260         transport_punchout_line->property_x1() = 0.0;
261         transport_punchout_line->property_y1() = 0.0;
262         transport_punchout_line->property_x2() = 0.0;
263         transport_punchout_line->property_y2() = 0.0;
264         transport_punchout_line->hide();
265         
266         // used to show zoom mode active zooming
267         zoom_rect = new ArdourCanvas::SimpleRect (*track_canvas->root(), 0.0, 0.0, 0.0, 0.0);
268         zoom_rect->property_outline_pixels() = 1;
269         zoom_rect->hide();
270         
271         zoom_rect->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
272         
273         // used as rubberband rect
274         rubberband_rect = new ArdourCanvas::SimpleRect (*track_canvas->root(), 0.0, 0.0, 0.0, 0.0);
275         rubberband_rect->property_outline_pixels() = 1;
276         rubberband_rect->hide();
277         
278         tempo_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
279         meter_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
280         marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
281         cd_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_cd_marker_bar_event), cd_marker_bar));
282         range_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
283         transport_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
284         
285         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_loop_range_view), false));
286         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_punch_range_view), false));
287         
288         double time_height = timebar_height * 5;
289         double time_width = FLT_MAX/frames_per_unit;
290         time_canvas->set_scroll_region(0.0, 0.0, time_width, time_height);
291
292         playhead_cursor = new Cursor (*this, &Editor::canvas_playhead_cursor_event);
293
294         initial_ruler_update_required = true;
295         track_canvas->signal_size_allocate().connect (mem_fun(*this, &Editor::track_canvas_allocate));
296
297         if (logo_item) {
298                 logo_item->lower_to_bottom ();
299         }
300
301         ColorsChanged.connect (mem_fun (*this, &Editor::color_handler));
302         color_handler();
303
304 }
305
306 void
307 Editor::track_canvas_allocate (Gtk::Allocation alloc)
308 {
309         canvas_allocation = alloc;
310
311         if (!initial_ruler_update_required) {
312                 if (!canvas_idle_queued) {
313                         /* call this first so that we do stuff before any pending redraw */
314                         Glib::signal_idle().connect (mem_fun (*this, &Editor::track_canvas_size_allocated), false);
315                         canvas_idle_queued = true;
316                 }
317                 return;
318         } 
319
320         initial_ruler_update_required = false;
321         track_canvas_size_allocated ();
322 }
323
324 bool
325 Editor::track_canvas_size_allocated ()
326 {
327         if (canvas_idle_queued) {
328                 canvas_idle_queued = false;
329         }
330
331         canvas_width = canvas_allocation.get_width();
332         canvas_height = canvas_allocation.get_height();
333
334         full_canvas_height = canvas_height;
335
336         if (session) {
337                 TrackViewList::iterator i;
338                 double height = 0;
339
340                 for (i = track_views.begin(); i != track_views.end(); ++i) {
341                         if ((*i)->control_parent) {
342                                 height += (*i)->effective_height;
343                         }
344                 }
345                 
346                 full_canvas_height = height;
347         }
348
349         zoom_range_clock.set ((nframes_t) floor ((canvas_width * frames_per_unit)));
350         playhead_cursor->set_position (playhead_cursor->current_frame);
351
352         reset_hscrollbar_stepping ();
353         reset_scrolling_region ();
354
355         if (playhead_cursor) playhead_cursor->set_length (canvas_height);
356
357         double y1 = vertical_adjustment.get_value ();
358         
359         for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {
360                 (*x)->set_line_vpos (y1, canvas_height);
361         }
362
363         range_marker_drag_rect->property_y1() = y1;
364         range_marker_drag_rect->property_y2() = full_canvas_height;
365         transport_loop_range_rect->property_y1() = y1;
366         transport_loop_range_rect->property_y2() = full_canvas_height;
367         transport_punch_range_rect->property_y1() = y1;
368         transport_punch_range_rect->property_y2() = full_canvas_height;
369         transport_punchin_line->property_y1() = y1;
370         transport_punchin_line->property_y2() = full_canvas_height;
371         transport_punchout_line->property_y1() = y1;
372         transport_punchout_line->property_y2() = full_canvas_height;
373         
374         update_fixed_rulers();
375         redisplay_tempo (true);
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(), -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         cerr << "drop on canvas, target = " << data.get_target() << endl;
475
476         if (data.get_target() == "regions") {
477                 drop_regions (context, x, y, data, info, time);
478         }
479         else if(data.get_target() == "routes") {
480                 drop_routes (context, x, y, data, info, time);
481         }
482         else {
483                 drop_paths (context, x, y, data, info, time);
484         }
485 }
486
487 void
488 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
489                     int x, int y, 
490                     const SelectionData& data,
491                     guint info, guint time)
492 {
493         TimeAxisView* tvp;
494         RouteTimeAxisView* tv;
495         double cy;
496         vector<ustring> paths;
497         string spath;
498         GdkEvent ev;
499         nframes64_t frame;
500
501         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
502                 goto out;
503         }
504
505         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
506          */
507
508         double wx;
509         double wy;
510
511         track_canvas->window_to_world (x, y, wx, wy);
512         wx += horizontal_adjustment.get_value();
513         wy += vertical_adjustment.get_value();
514         
515         ev.type = GDK_BUTTON_RELEASE;
516         ev.button.x = wx;
517         ev.button.y = wy;
518
519         frame = event_frame (&ev, 0, &cy);
520
521         snap_to (frame);
522
523         if ((tvp = trackview_by_y_position (cy)) == 0) {
524
525                 /* drop onto canvas background: create new tracks */
526
527                 if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
528                         do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsTrack, SrcBest, frame); 
529                 } else {
530                         do_embed (paths, Editing::ImportDistinctFiles, ImportAsTrack, frame);
531                 }
532                 
533         } else if ((tv = dynamic_cast<RouteTimeAxisView*>(tvp)) != 0) {
534
535                 /* check that its an audio track, not a bus */
536                 
537                 /* check that its an audio track, not a bus */
538                 
539                 if (tv->get_diskstream()) {
540                         /* select the track, then embed */
541                         selection->set (tv);
542
543                         if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
544                                 do_import (paths, Editing::ImportDistinctFiles, Editing::ImportToTrack, SrcBest, frame); 
545                         } else {
546                                 do_embed (paths, Editing::ImportDistinctFiles, ImportToTrack, frame);
547                         }
548                 }
549         }
550
551   out:
552         context->drag_finish (true, false, time);
553 }
554
555 void
556 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
557                       int x, int y, 
558                       const SelectionData& data,
559                       guint info, guint time)
560 {
561         const SerializedObjectPointers<boost::shared_ptr<Region> >* sr = 
562                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Region> > *> (data.get_data());
563
564         for (uint32_t i = 0; i < sr->cnt; ++i) {
565
566                 boost::shared_ptr<Region> r = sr->data[i];
567                 
568                 insert_region_list_drag (r, x, y);
569         }
570
571         context->drag_finish (true, false, time);
572 }
573
574 void
575 Editor::drop_routes (const Glib::RefPtr<Gdk::DragContext>& context,
576                      int x, int y,
577                      const Gtk::SelectionData& data,
578                      guint info, guint time) {
579         const SerializedObjectPointers<boost::shared_ptr<Route> >* sr = 
580                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Route> > *> (data.get_data());
581
582         for (uint32_t i = 0; i < sr->cnt; ++i) {
583                 boost::shared_ptr<Route> r = sr->data[i];
584                 insert_route_list_drag (r, x, y);
585         }
586
587         context->drag_finish (true, false, time);
588 }
589
590 void
591 Editor::maybe_autoscroll (GdkEvent* event)
592 {
593         nframes_t rightmost_frame = leftmost_frame + current_page_frames();
594         nframes_t frame = drag_info.current_pointer_frame;
595         bool startit = false;
596
597         static int last_autoscroll_direction = 0;
598
599         if (frame > rightmost_frame) {
600
601                 if (rightmost_frame < max_frames) {
602                         autoscroll_direction = 1;
603                         startit = true;
604                 }
605
606         } else if (frame < leftmost_frame) {
607
608                 if (leftmost_frame > 0) {
609                         autoscroll_direction = -1;
610                         startit = true;
611                 }
612
613         } else {
614
615                 if (drag_info.last_pointer_frame > drag_info.current_pointer_frame) {
616                         autoscroll_direction = -1;
617                 } else {
618                         autoscroll_direction = 1;
619                 }
620         }
621
622
623         if ((autoscroll_direction != last_autoscroll_direction) || (leftmost_frame < frame < rightmost_frame)) {
624                 stop_canvas_autoscroll ();
625         }
626         
627         if (startit && autoscroll_timeout_tag < 0) {
628                 start_canvas_autoscroll (autoscroll_direction);
629         }
630
631         last_autoscroll_direction = autoscroll_direction;
632 }
633
634 gint
635 Editor::_autoscroll_canvas (void *arg)
636 {
637         return ((Editor *) arg)->autoscroll_canvas ();
638 }
639
640 bool
641 Editor::autoscroll_canvas ()
642 {
643         nframes_t new_frame;
644         nframes_t limit = max_frames - current_page_frames();
645         GdkEventMotion ev;
646         nframes_t target_frame;
647
648         if (autoscroll_direction < 0) {
649                 if (leftmost_frame < autoscroll_distance) {
650                         new_frame = 0;
651                 } else {
652                         new_frame = leftmost_frame - autoscroll_distance;
653                 }
654                 target_frame = drag_info.current_pointer_frame - autoscroll_distance;
655         } else {
656                 if (leftmost_frame > limit - autoscroll_distance) {
657                         new_frame = limit;
658                 } else {
659                         new_frame = leftmost_frame + autoscroll_distance;
660                 }
661                 target_frame = drag_info.current_pointer_frame + autoscroll_distance;
662         }
663
664         /* now fake a motion event to get the object that is being dragged to move too */
665
666         ev.type = GDK_MOTION_NOTIFY;
667         ev.state &= Gdk::BUTTON1_MASK;
668         ev.x = frame_to_unit (target_frame);
669         ev.y = drag_info.current_pointer_y;
670         motion_handler (drag_info.item, (GdkEvent*) &ev, drag_info.item_type, true);
671
672         if (new_frame == 0 || new_frame == limit) {
673                 /* we are done */
674                 return false;
675         }
676
677         autoscroll_cnt++;
678
679         if (autoscroll_cnt == 1) {
680
681                 /* connect the timeout so that we get called repeatedly */
682
683                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
684                 return false;
685
686         } 
687
688         if (new_frame != leftmost_frame) {
689                 reset_x_origin (new_frame);
690         }
691
692         if (autoscroll_cnt == 50) { /* 0.5 seconds */
693                 
694                 /* after about a while, speed up a bit by changing the timeout interval */
695
696                 autoscroll_distance = (nframes_t) floor (current_page_frames()/30.0f);
697                 
698         } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
699
700                 autoscroll_distance = (nframes_t) floor (current_page_frames()/20.0f);
701
702         } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
703
704                 /* after about another while, speed up by increasing the shift per callback */
705
706                 autoscroll_distance =  (nframes_t) floor (current_page_frames()/10.0f);
707
708         } 
709
710         return true;
711 }
712
713 void
714 Editor::start_canvas_autoscroll (int dir)
715 {
716         if (!session || autoscroll_active) {
717                 return;
718         }
719
720         stop_canvas_autoscroll ();
721
722         autoscroll_active = true;
723         autoscroll_direction = dir;
724         autoscroll_distance = (nframes_t) floor (current_page_frames()/50.0);
725         autoscroll_cnt = 0;
726         
727         /* do it right now, which will start the repeated callbacks */
728
729         autoscroll_canvas ();
730 }
731
732 void
733 Editor::stop_canvas_autoscroll ()
734 {
735         if (autoscroll_timeout_tag >= 0) {
736                 g_source_remove (autoscroll_timeout_tag);
737                 autoscroll_timeout_tag = -1;
738         }
739
740         autoscroll_active = false;
741 }
742
743 gint
744 Editor::left_track_canvas (GdkEventCrossing *ev)
745 {
746         set_entered_track (0);
747         set_entered_regionview (0);
748         return FALSE;
749 }
750
751 void
752 Editor::tie_vertical_scrolling ()
753 {
754         double y1 = vertical_adjustment.get_value();
755
756         playhead_cursor->set_y_axis (y1);
757
758         range_marker_drag_rect->property_y1() = y1;
759         range_marker_drag_rect->property_y2() = full_canvas_height;
760         transport_loop_range_rect->property_y1() = y1;
761         transport_loop_range_rect->property_y2() = full_canvas_height;
762         transport_punch_range_rect->property_y1() = y1;
763         transport_punch_range_rect->property_y2() = full_canvas_height;
764         transport_punchin_line->property_y1() = y1;
765         transport_punchin_line->property_y2() = full_canvas_height;
766         transport_punchout_line->property_y1() = y1;
767         transport_punchout_line->property_y2() = full_canvas_height;
768
769         if (!selection->markers.empty()) {
770                 for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {            
771                         (*x)->set_line_vpos (y1, canvas_height);
772                 }
773         }
774
775         if (logo_item) {
776                 logo_item->property_y() = y1;
777         }
778
779         /* this will do an immediate redraw */
780
781         controls_layout.get_vadjustment()->set_value (y1);
782 }
783
784 void 
785 Editor::canvas_horizontally_scrolled ()
786 {
787         nframes64_t time_origin = (nframes_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
788
789         if (time_origin != leftmost_frame) {
790                 canvas_scroll_to (time_origin);
791         }
792 }
793
794 void
795 Editor::canvas_scroll_to (nframes64_t time_origin)
796 {
797         leftmost_frame = time_origin;
798         nframes_t rightmost_frame = leftmost_frame + current_page_frames ();
799
800         if (rightmost_frame > last_canvas_frame) {
801                 last_canvas_frame = rightmost_frame;
802                 reset_scrolling_region ();
803         }
804         
805         if (logo_item) {
806                 logo_item->property_x() = horizontal_adjustment.get_value ();
807         }
808
809         update_fixed_rulers ();
810
811         redisplay_tempo (!_dragging_hscrollbar);
812 }
813
814 void
815 Editor::color_handler()
816 {
817         playhead_cursor->canvas_item.property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_PlayHead.get();
818         verbose_canvas_cursor->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_VerboseCanvasCursor.get();
819         
820         meter_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MeterBar.get();
821         meter_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
822
823         tempo_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TempoBar.get();
824         tempo_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
825
826         marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBar.get();
827         marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
828
829         cd_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CDMarkerBar.get();
830         cd_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
831
832         range_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeMarkerBar.get();
833         range_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
834
835         transport_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportMarkerBar.get();
836         transport_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
837
838         cd_marker_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
839         cd_marker_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
840
841         range_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
842         range_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
843
844         transport_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
845         transport_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
846
847         marker_drag_line->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerDragLine.get();
848
849         range_marker_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
850         range_marker_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
851
852         transport_loop_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
853         transport_loop_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
854
855         transport_punch_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
856         transport_punch_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
857
858         transport_punchin_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
859         transport_punchout_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
860
861         zoom_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
862         zoom_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
863
864         rubberband_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
865         rubberband_rect->property_fill_color_rgba() = (guint32) ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
866
867         location_marker_color = ARDOUR_UI::config()->canvasvar_LocationMarker.get();
868         location_range_color = ARDOUR_UI::config()->canvasvar_LocationRange.get();
869         location_cd_marker_color = ARDOUR_UI::config()->canvasvar_LocationCDMarker.get();
870         location_loop_color = ARDOUR_UI::config()->canvasvar_LocationLoop.get();
871         location_punch_color = ARDOUR_UI::config()->canvasvar_LocationPunch.get();
872
873         refresh_location_display ();
874         redisplay_tempo (true);
875
876         if (session)
877                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
878 }
879
880
881 void
882 Editor::flush_canvas ()
883 {
884         if (is_mapped()) {
885                 track_canvas->update_now ();
886                 // gdk_window_process_updates (GTK_LAYOUT(track_canvas->gobj())->bin_window, true);
887         }
888 }