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