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