merge 3.0 from 2.0-ongoing@3243
[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->signal_enter_notify_event().connect (mem_fun(*this, &Editor::entered_track_canvas));
131         track_canvas->set_flags (CAN_FOCUS);
132
133         /* set up drag-n-drop */
134
135         vector<TargetEntry> target_table;
136         
137         // Drag-N-Drop from the region list can generate this target
138         target_table.push_back (TargetEntry ("regions"));
139         target_table.push_back (TargetEntry ("routes"));
140
141         target_table.push_back (TargetEntry ("text/plain"));
142         target_table.push_back (TargetEntry ("text/uri-list"));
143         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
144
145         track_canvas->drag_dest_set (target_table);
146         track_canvas->signal_drag_data_received().connect (mem_fun(*this, &Editor::track_canvas_drag_data_received));
147
148         /* stuff for the verbose canvas cursor */
149
150         Pango::FontDescription* font = get_font_for_style (N_("VerboseCanvasCursor"));
151
152         verbose_canvas_cursor = new ArdourCanvas::Text (*track_canvas->root());
153         verbose_canvas_cursor->property_font_desc() = *font;
154         verbose_canvas_cursor->property_anchor() = ANCHOR_NW;
155         
156         delete font;
157
158         verbose_cursor_visible = false;
159         
160         if (Profile->get_sae()) {
161                 Image img (::get_icon (X_("saelogo")));
162                 logo_item = new ArdourCanvas::Pixbuf (*track_canvas->root(), 0.0, 0.0, img.get_pixbuf());
163                 // logo_item->property_height_in_pixels() = true;
164                 // logo_item->property_width_in_pixels() = true;
165                 // logo_item->property_height_set() = true;
166                 // logo_item->property_width_set() = true;
167                 logo_item->show ();
168         }
169         
170         /* a group to hold time (measure) lines */
171         
172         time_line_group = new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
173         tempo_lines = new TempoLines(*track_canvas, time_line_group);
174         cursor_group = new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
175
176         time_canvas->set_name ("EditorTimeCanvas");
177         time_canvas->add_events (Gdk::POINTER_MOTION_HINT_MASK);
178         time_canvas->set_flags (CAN_FOCUS);
179         time_canvas->set_center_scroll_region (false);
180         time_canvas->set_dither (Gdk::RGB_DITHER_NONE);
181         
182         marker_time_line_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, 0.0);
183         marker_tempo_lines = new TempoLines(*time_canvas, marker_time_line_group);
184         
185         meter_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, 0.0);
186         tempo_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height);
187         range_marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 2.0);
188         transport_marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 3.0);
189         marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 4.0);
190         cd_marker_group = new ArdourCanvas::Group (*time_canvas->root(), 0.0, timebar_height * 5.0);
191         
192         tempo_bar = new ArdourCanvas::SimpleRect (*tempo_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
193         tempo_bar->property_outline_what() = (0x1 | 0x8);
194         tempo_bar->property_outline_pixels() = 1;
195
196         
197         meter_bar = new ArdourCanvas::SimpleRect (*meter_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
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         marker_bar->property_outline_what() = (0x1 | 0x8);
203         marker_bar->property_outline_pixels() = 1;
204
205         cd_marker_bar = new ArdourCanvas::SimpleRect (*cd_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
206         cd_marker_bar->property_outline_what() = (0x1 | 0x8);
207         cd_marker_bar->property_outline_pixels() = 1;
208         
209         range_marker_bar = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
210         range_marker_bar->property_outline_what() = (0x1 | 0x8);
211         range_marker_bar->property_outline_pixels() = 0;
212         
213         transport_marker_bar = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
214         transport_marker_bar->property_outline_what() = (0x1 | 0x8);
215         transport_marker_bar->property_outline_pixels() = 1;
216         
217         cd_marker_bar_drag_rect = new ArdourCanvas::SimpleRect (*cd_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
218         cd_marker_bar_drag_rect->property_outline_pixels() = 0;
219         cd_marker_bar_drag_rect->hide ();
220
221         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
222         range_bar_drag_rect->property_outline_pixels() = 0;
223         
224         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
225         transport_bar_drag_rect->property_outline_pixels() = 0;
226         transport_bar_drag_rect->hide ();
227         
228         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
229         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
230
231         marker_drag_line = new ArdourCanvas::Line (*track_canvas->root());
232         marker_drag_line->property_width_pixels() = 1;
233         marker_drag_line->property_points() = marker_drag_line_points;
234         marker_drag_line->hide();
235
236         range_marker_drag_rect = new ArdourCanvas::SimpleRect (*track_canvas->root(), 0.0, 0.0, 0.0, 0.0);
237         range_marker_drag_rect->hide ();
238         
239         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
240         transport_loop_range_rect->property_outline_pixels() = 1;
241         transport_loop_range_rect->hide();
242
243         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
244         transport_punch_range_rect->property_outline_pixels() = 0;
245         transport_punch_range_rect->hide();
246         
247         transport_loop_range_rect->lower_to_bottom (); // loop on the bottom
248
249         transport_punchin_line = new ArdourCanvas::SimpleLine (*time_line_group);
250         transport_punchin_line->property_x1() = 0.0;
251         transport_punchin_line->property_y1() = 0.0;
252         transport_punchin_line->property_x2() = 0.0;
253         transport_punchin_line->property_y2() = 0.0;
254         transport_punchin_line->hide ();
255         
256         transport_punchout_line  = new ArdourCanvas::SimpleLine (*time_line_group);
257         transport_punchout_line->property_x1() = 0.0;
258         transport_punchout_line->property_y1() = 0.0;
259         transport_punchout_line->property_x2() = 0.0;
260         transport_punchout_line->property_y2() = 0.0;
261         transport_punchout_line->hide();
262         
263         // used to show zoom mode active zooming
264         zoom_rect = new ArdourCanvas::SimpleRect (*track_canvas->root(), 0.0, 0.0, 0.0, 0.0);
265         zoom_rect->property_outline_pixels() = 1;
266         zoom_rect->hide();
267         
268         zoom_rect->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
269         
270         // used as rubberband rect
271         rubberband_rect = new ArdourCanvas::SimpleRect (*track_canvas->root(), 0.0, 0.0, 0.0, 0.0);
272         rubberband_rect->property_outline_pixels() = 1;
273         rubberband_rect->hide();
274         
275         tempo_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
276         meter_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
277         marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
278         cd_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_cd_marker_bar_event), cd_marker_bar));
279         range_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
280         transport_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
281         
282         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_loop_range_view), false));
283         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_punch_range_view), false));
284         
285         double time_height = timebar_height * 5;
286         double time_width = FLT_MAX/frames_per_unit;
287         time_canvas->set_scroll_region(0.0, 0.0, time_width, time_height);
288
289         playhead_cursor = new Cursor (*this, &Editor::canvas_playhead_cursor_event);
290
291         initial_ruler_update_required = true;
292         track_canvas->signal_size_allocate().connect (mem_fun(*this, &Editor::track_canvas_allocate));
293
294         if (logo_item) {
295                 logo_item->lower_to_bottom ();
296         }
297
298         ColorsChanged.connect (mem_fun (*this, &Editor::color_handler));
299         color_handler();
300
301 }
302
303 void
304 Editor::track_canvas_allocate (Gtk::Allocation alloc)
305 {
306         canvas_allocation = alloc;
307
308         if (!initial_ruler_update_required) {
309                 if (!canvas_idle_queued) {
310                         /* call this first so that we do stuff before any pending redraw */
311                         Glib::signal_idle().connect (mem_fun (*this, &Editor::track_canvas_size_allocated), false);
312                         canvas_idle_queued = true;
313                 }
314                 return;
315         } 
316
317         initial_ruler_update_required = false;
318         track_canvas_size_allocated ();
319 }
320
321 bool
322 Editor::track_canvas_size_allocated ()
323 {
324         if (canvas_idle_queued) {
325                 canvas_idle_queued = false;
326         }
327
328         canvas_width = canvas_allocation.get_width();
329         canvas_height = canvas_allocation.get_height();
330
331         full_canvas_height = canvas_height;
332
333         if (session) {
334                 TrackViewList::iterator i;
335                 double height = 0;
336
337                 for (i = track_views.begin(); i != track_views.end(); ++i) {
338                         if ((*i)->control_parent) {
339                                 height += (*i)->effective_height;
340                         }
341                 }
342                 
343                 full_canvas_height = height;
344         }
345
346         zoom_range_clock.set ((nframes_t) floor ((canvas_width * frames_per_unit)));
347         playhead_cursor->set_position (playhead_cursor->current_frame);
348
349         reset_hscrollbar_stepping ();
350         reset_scrolling_region ();
351
352         if (playhead_cursor) playhead_cursor->set_length (canvas_height);
353
354         double y1 = vertical_adjustment.get_value ();
355         
356         for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {
357                 (*x)->set_line_vpos (y1, canvas_height);
358         }
359
360         range_marker_drag_rect->property_y1() = y1;
361         range_marker_drag_rect->property_y2() = full_canvas_height;
362         transport_loop_range_rect->property_y1() = y1;
363         transport_loop_range_rect->property_y2() = full_canvas_height;
364         transport_punch_range_rect->property_y1() = y1;
365         transport_punch_range_rect->property_y2() = full_canvas_height;
366         transport_punchin_line->property_y1() = y1;
367         transport_punchin_line->property_y2() = full_canvas_height;
368         transport_punchout_line->property_y1() = y1;
369         transport_punchout_line->property_y2() = full_canvas_height;
370         
371         update_fixed_rulers();
372         redisplay_tempo (true);
373
374         Resized (); /* EMIT_SIGNAL */
375
376         return false;
377 }
378
379 void
380 Editor::reset_scrolling_region (Gtk::Allocation* alloc)
381 {
382         TreeModel::Children rows = route_display_model->children();
383         TreeModel::Children::iterator i;
384         double pos;
385
386         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
387                 TimeAxisView *tv = (*i)[route_display_columns.tv];
388                 if (tv != 0 && !tv->hidden()) {
389                         pos += tv->effective_height;
390                 }
391         }
392
393         double last_canvas_unit =  max ((last_canvas_frame / frames_per_unit), canvas_width);
394
395         track_canvas->set_scroll_region (0.0, 0.0, last_canvas_unit, pos);
396
397         // XXX what is the correct height value for the time canvas ? this overstates it
398         time_canvas->set_scroll_region ( 0.0, 0.0, last_canvas_unit, canvas_height);
399
400         range_marker_drag_rect->property_y2() = canvas_height;
401         transport_loop_range_rect->property_y2() = canvas_height;
402         transport_punch_range_rect->property_y2() = canvas_height;
403         transport_punchin_line->property_y2() = canvas_height;
404         transport_punchout_line->property_y2() = canvas_height;
405
406         update_punch_range_view (true);
407
408         controls_layout.queue_resize();
409 }
410
411 void
412 Editor::controls_layout_size_request (Requisition* req)
413 {
414         TreeModel::Children rows = route_display_model->children();
415         TreeModel::Children::iterator i;
416         double pos;
417
418         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
419                 TimeAxisView *tv = (*i)[route_display_columns.tv];
420                 if (tv != 0) {
421                         pos += tv->effective_height;
422                 }
423         }
424
425         RefPtr<Gdk::Screen> screen = get_screen();
426
427         if (!screen) {
428                 screen = Gdk::Screen::get_default();
429         }
430
431         edit_controls_vbox.check_resize();
432         req->width = max (edit_controls_vbox.get_width(),  controls_layout.get_width());
433
434         /* don't get too big. the fudge factors here are just guesses */
435         
436         req->width = min (req->width, screen->get_width() - 300);
437         req->height = min ((gint) pos, (screen->get_height() - 400));
438
439         /* this one is important: it determines how big the layout thinks it really is, as 
440            opposed to what it displays on the screen
441         */
442         
443         controls_layout.set_size (edit_controls_vbox.get_width(), (gint) pos);
444         controls_layout.set_size_request(edit_controls_vbox.get_width(), -1);
445         zoom_box.set_size_request(edit_controls_vbox.get_width(), -1);
446         time_button_frame.set_size_request(edit_controls_vbox.get_width(), -1);
447
448         //cerr << "sizes = " << req->width << " " << edit_controls_vbox.get_width() << " " << controls_layout.get_width() << " " << zoom_box.get_width() << " " << time_button_frame.get_width() << endl;//DEBUG        
449 }
450
451 bool
452 Editor::track_canvas_map_handler (GdkEventAny* ev)
453 {
454         track_canvas->get_window()->set_cursor (*current_canvas_cursor);
455         return false;
456 }
457
458 bool
459 Editor::time_canvas_map_handler (GdkEventAny* ev)
460 {
461         time_canvas->get_window()->set_cursor (*timebar_cursor);
462         return false;
463 }
464
465 void  
466 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
467                                          int x, int y, 
468                                          const SelectionData& data,
469                                          guint info, guint time)
470 {
471         cerr << "drop on canvas, target = " << data.get_target() << endl;
472
473         if (data.get_target() == "regions") {
474                 drop_regions (context, x, y, data, info, time);
475         }
476         else if(data.get_target() == "routes") {
477                 drop_routes (context, x, y, data, info, time);
478         }
479         else {
480                 drop_paths (context, x, y, data, info, time);
481         }
482 }
483
484 void
485 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
486                     int x, int y, 
487                     const SelectionData& data,
488                     guint info, guint time)
489 {
490         TimeAxisView* tvp;
491         RouteTimeAxisView* tv;
492         double cy;
493         vector<ustring> paths;
494         string spath;
495         GdkEvent ev;
496         nframes64_t frame;
497
498         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
499                 goto out;
500         }
501
502         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
503          */
504
505         double wx;
506         double wy;
507
508         track_canvas->window_to_world (x, y, wx, wy);
509         wx += horizontal_adjustment.get_value();
510         wy += vertical_adjustment.get_value();
511         
512         ev.type = GDK_BUTTON_RELEASE;
513         ev.button.x = wx;
514         ev.button.y = wy;
515
516         frame = event_frame (&ev, 0, &cy);
517
518         snap_to (frame);
519
520         if ((tvp = trackview_by_y_position (cy)) == 0) {
521
522                 /* drop onto canvas background: create new tracks */
523
524                 if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
525                         do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsTrack, SrcBest, frame); 
526                 } else {
527                         do_embed (paths, Editing::ImportDistinctFiles, ImportAsTrack, frame);
528                 }
529                 
530         } else if ((tv = dynamic_cast<RouteTimeAxisView*>(tvp)) != 0) {
531
532                 /* check that its an audio track, not a bus */
533                 
534                 /* check that its an audio track, not a bus */
535                 
536                 if (tv->get_diskstream()) {
537                         /* select the track, then embed */
538                         selection->set (tv);
539
540                         if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
541                                 do_import (paths, Editing::ImportDistinctFiles, Editing::ImportToTrack, SrcBest, frame); 
542                         } else {
543                                 do_embed (paths, Editing::ImportDistinctFiles, ImportToTrack, frame);
544                         }
545                 }
546         }
547
548   out:
549         context->drag_finish (true, false, time);
550 }
551
552 void
553 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
554                       int x, int y, 
555                       const SelectionData& data,
556                       guint info, guint time)
557 {
558         const SerializedObjectPointers<boost::shared_ptr<Region> >* sr = 
559                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Region> > *> (data.get_data());
560
561         for (uint32_t i = 0; i < sr->cnt; ++i) {
562
563                 boost::shared_ptr<Region> r = sr->data[i];
564                 
565                 insert_region_list_drag (r, x, y);
566         }
567
568         context->drag_finish (true, false, time);
569 }
570
571 void
572 Editor::drop_routes (const Glib::RefPtr<Gdk::DragContext>& context,
573                      int x, int y,
574                      const Gtk::SelectionData& data,
575                      guint info, guint time) {
576         const SerializedObjectPointers<boost::shared_ptr<Route> >* sr = 
577                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Route> > *> (data.get_data());
578
579         for (uint32_t i = 0; i < sr->cnt; ++i) {
580                 boost::shared_ptr<Route> r = sr->data[i];
581                 insert_route_list_drag (r, x, y);
582         }
583
584         context->drag_finish (true, false, time);
585 }
586
587 void
588 Editor::maybe_autoscroll (GdkEventMotion* event)
589 {
590         nframes_t rightmost_frame = leftmost_frame + current_page_frames();
591         nframes_t frame = drag_info.current_pointer_frame;
592         bool startit = false;
593         double vertical_pos = vertical_adjustment.get_value();
594
595         autoscroll_y = 0;
596         autoscroll_x = 0;
597
598         if (event->y < vertical_pos) {
599                 autoscroll_y = -1;
600                 startit = true;
601         }
602
603         if (event->y > vertical_pos + canvas_height) {
604                 autoscroll_y = 1;
605                 startit = true;
606         }
607
608         if (frame > rightmost_frame) {
609
610                 if (rightmost_frame < max_frames) {
611                         autoscroll_x = 1;
612                         startit = true;
613                 }
614
615         } else if (frame < leftmost_frame) {
616                 
617                 if (leftmost_frame > 0) {
618                         autoscroll_x = -1;
619                         startit = true;
620                 }
621
622         }
623
624         if ((autoscroll_x != last_autoscroll_x) || (autoscroll_y != last_autoscroll_y) || (autoscroll_x == 0 && autoscroll_y == 0)) {
625                 stop_canvas_autoscroll ();
626         }
627         
628         if (startit && autoscroll_timeout_tag < 0) {
629                 start_canvas_autoscroll (autoscroll_x, autoscroll_y);
630         }
631
632         last_autoscroll_x = autoscroll_x;
633         last_autoscroll_y = autoscroll_y;
634 }
635
636 gint
637 Editor::_autoscroll_canvas (void *arg)
638 {
639         return ((Editor *) arg)->autoscroll_canvas ();
640 }
641
642 bool
643 Editor::autoscroll_canvas ()
644 {
645         nframes_t new_frame;
646         nframes_t limit = max_frames - current_page_frames();
647         GdkEventMotion ev;
648         nframes_t target_frame;
649         double new_pixel;
650         double target_pixel;
651
652         if (autoscroll_x < 0) {
653                 if (leftmost_frame < autoscroll_x_distance) {
654                         new_frame = 0;
655                 } else {
656                         new_frame = leftmost_frame - autoscroll_x_distance;
657                 }
658                 target_frame = drag_info.current_pointer_frame - autoscroll_x_distance;
659         } else if (autoscroll_x > 0) {
660                 if (leftmost_frame > limit - autoscroll_x_distance) {
661                         new_frame = limit;
662                 } else {
663                         new_frame = leftmost_frame + autoscroll_x_distance;
664                 }
665                 target_frame = drag_info.current_pointer_frame + autoscroll_x_distance;
666         } else {
667                 target_frame = drag_info.current_pointer_frame;
668                 new_frame = leftmost_frame;
669         }
670
671         double vertical_pos = vertical_adjustment.get_value();
672
673         if (autoscroll_y < 0) {
674
675                 if (vertical_pos < autoscroll_y_distance) {
676                         new_pixel = 0;
677                 } else {
678                         new_pixel = vertical_pos - autoscroll_y_distance;
679                 }
680
681                 target_pixel = drag_info.current_pointer_y - autoscroll_y_distance;
682                 target_pixel = max (target_pixel, 0.0);
683
684         } else if (autoscroll_y > 0) {
685
686                 double top_of_bottom_of_canvas = full_canvas_height - canvas_height;
687
688                 if (vertical_pos > full_canvas_height - autoscroll_y_distance) {
689                         new_pixel = full_canvas_height;
690                 } else {
691                         new_pixel = vertical_pos + autoscroll_y_distance;
692                 }
693
694                 new_pixel = min (top_of_bottom_of_canvas, new_pixel);
695
696                 target_pixel = drag_info.current_pointer_y + autoscroll_y_distance;
697                 
698                 /* don't move to the full canvas height because the item will be invisible
699                    (its top edge will line up with the bottom of the visible canvas.
700                 */
701
702                 target_pixel = min (target_pixel, full_canvas_height - 10);
703                 
704         } else {
705                 target_pixel = drag_info.current_pointer_y;
706                 new_pixel = vertical_pos;
707         }
708
709         /* now fake a motion event to get the object that is being dragged to move too */
710
711         ev.type = GDK_MOTION_NOTIFY;
712         ev.state &= Gdk::BUTTON1_MASK;
713         ev.x = frame_to_unit (target_frame);
714         ev.y = target_pixel;
715         motion_handler (drag_info.item, (GdkEvent*) &ev, drag_info.item_type, true);
716
717         if ((new_frame == 0 || new_frame == limit) && (new_pixel == 0 || new_pixel == DBL_MAX)) {
718                 /* we are done */
719                 return false;
720         }
721
722         autoscroll_cnt++;
723
724         if (autoscroll_cnt == 1) {
725
726                 /* connect the timeout so that we get called repeatedly */
727
728                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
729                 return false;
730
731         } 
732
733         if (new_frame != leftmost_frame) {
734                 reset_x_origin (new_frame);
735         }
736
737         vertical_adjustment.set_value (new_pixel);
738
739         if (autoscroll_x_distance != 0) {
740
741                 if (autoscroll_cnt == 50) { /* 0.5 seconds */
742                         
743                         /* after about a while, speed up a bit by changing the timeout interval */
744                         
745                         autoscroll_x_distance = (nframes_t) floor (current_page_frames()/30.0f);
746                         
747                 } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
748                         
749                         autoscroll_x_distance = (nframes_t) floor (current_page_frames()/20.0f);
750                         
751                 } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
752                         
753                         /* after about another while, speed up by increasing the shift per callback */
754                         
755                         autoscroll_x_distance =  (nframes_t) floor (current_page_frames()/10.0f);
756                         
757                 } 
758         }
759
760         if (autoscroll_y_distance != 0) {
761
762                 if (autoscroll_cnt == 50) { /* 0.5 seconds */
763                         
764                         /* after about a while, speed up a bit by changing the timeout interval */
765                         
766                         autoscroll_y_distance = 10;
767                         
768                 } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
769                         
770                         autoscroll_y_distance = 20;
771                         
772                 } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
773                         
774                         /* after about another while, speed up by increasing the shift per callback */
775                         
776                         autoscroll_y_distance =  40;
777                 } 
778         }
779
780         return true;
781 }
782
783 void
784 Editor::start_canvas_autoscroll (int dx, int dy)
785 {
786         if (!session || autoscroll_active) {
787                 return;
788         }
789
790         stop_canvas_autoscroll ();
791
792         autoscroll_active = true;
793         autoscroll_x = dx;
794         autoscroll_y = dy;
795         autoscroll_x_distance = (nframes_t) floor (current_page_frames()/50.0);
796         autoscroll_y_distance = fabs (dy * 5); /* pixels */
797         autoscroll_cnt = 0;
798         
799         /* do it right now, which will start the repeated callbacks */
800
801         autoscroll_canvas ();
802 }
803
804 void
805 Editor::stop_canvas_autoscroll ()
806 {
807         if (autoscroll_timeout_tag >= 0) {
808                 g_source_remove (autoscroll_timeout_tag);
809                 autoscroll_timeout_tag = -1;
810         }
811
812         autoscroll_active = false;
813 }
814
815 bool
816 Editor::left_track_canvas (GdkEventCrossing *ev)
817 {
818         set_entered_track (0);
819         set_entered_regionview (0);
820         reset_canvas_action_sensitivity (false);
821         return false;
822 }
823
824 bool
825 Editor::entered_track_canvas (GdkEventCrossing *ev)
826 {
827         reset_canvas_action_sensitivity (true);
828         return FALSE;
829 }
830
831 void
832 Editor::tie_vertical_scrolling ()
833 {
834         double y1 = vertical_adjustment.get_value();
835
836         playhead_cursor->set_y_axis (y1);
837
838         range_marker_drag_rect->property_y1() = y1;
839         range_marker_drag_rect->property_y2() = full_canvas_height;
840         transport_loop_range_rect->property_y1() = y1;
841         transport_loop_range_rect->property_y2() = full_canvas_height;
842         transport_punch_range_rect->property_y1() = y1;
843         transport_punch_range_rect->property_y2() = full_canvas_height;
844         transport_punchin_line->property_y1() = y1;
845         transport_punchin_line->property_y2() = full_canvas_height;
846         transport_punchout_line->property_y1() = y1;
847         transport_punchout_line->property_y2() = full_canvas_height;
848
849         if (!selection->markers.empty()) {
850                 for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {            
851                         (*x)->set_line_vpos (y1, canvas_height);
852                 }
853         }
854
855         if (logo_item) {
856                 logo_item->property_y() = y1;
857         }
858
859         /* this will do an immediate redraw */
860
861         controls_layout.get_vadjustment()->set_value (y1);
862 }
863
864 void 
865 Editor::canvas_horizontally_scrolled ()
866 {
867         nframes64_t time_origin = (nframes_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
868
869         if (time_origin != leftmost_frame) {
870                 canvas_scroll_to (time_origin);
871         }
872 }
873
874 void
875 Editor::canvas_scroll_to (nframes64_t time_origin)
876 {
877         leftmost_frame = time_origin;
878         nframes_t rightmost_frame = leftmost_frame + current_page_frames ();
879
880         if (rightmost_frame > last_canvas_frame) {
881                 last_canvas_frame = rightmost_frame;
882                 reset_scrolling_region ();
883         }
884         
885         if (logo_item) {
886                 logo_item->property_x() = horizontal_adjustment.get_value ();
887         }
888
889         update_fixed_rulers ();
890
891         redisplay_tempo (!_dragging_hscrollbar);
892 }
893
894 void
895 Editor::color_handler()
896 {
897         playhead_cursor->canvas_item.property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_PlayHead.get();
898         verbose_canvas_cursor->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_VerboseCanvasCursor.get();
899         
900         meter_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MeterBar.get();
901         meter_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
902
903         tempo_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TempoBar.get();
904         tempo_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
905
906         marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBar.get();
907         marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
908
909         cd_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CDMarkerBar.get();
910         cd_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
911
912         range_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeMarkerBar.get();
913         range_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
914
915         transport_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportMarkerBar.get();
916         transport_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
917
918         cd_marker_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
919         cd_marker_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
920
921         range_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
922         range_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
923
924         transport_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
925         transport_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
926
927         marker_drag_line->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerDragLine.get();
928
929         range_marker_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
930         range_marker_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
931
932         transport_loop_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
933         transport_loop_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
934
935         transport_punch_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
936         transport_punch_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
937
938         transport_punchin_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
939         transport_punchout_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
940
941         zoom_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
942         zoom_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
943
944         rubberband_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
945         rubberband_rect->property_fill_color_rgba() = (guint32) ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
946
947         location_marker_color = ARDOUR_UI::config()->canvasvar_LocationMarker.get();
948         location_range_color = ARDOUR_UI::config()->canvasvar_LocationRange.get();
949         location_cd_marker_color = ARDOUR_UI::config()->canvasvar_LocationCDMarker.get();
950         location_loop_color = ARDOUR_UI::config()->canvasvar_LocationLoop.get();
951         location_punch_color = ARDOUR_UI::config()->canvasvar_LocationPunch.get();
952
953         refresh_location_display ();
954         redisplay_tempo (true);
955
956         if (session)
957                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
958 }
959
960
961 void
962 Editor::flush_canvas ()
963 {
964         if (is_mapped()) {
965                 track_canvas->update_now ();
966                 // gdk_window_process_updates (GTK_LAYOUT(track_canvas->gobj())->bin_window, true);
967         }
968 }