fix #5025 part 1
[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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <libgnomecanvasmm/init.h>
25 #include <libgnomecanvasmm/pixbuf.h>
26 #include <jack/types.h>
27
28 #include "gtkmm2ext/utils.h"
29
30 #include "ardour/profile.h"
31 #include "ardour/rc_configuration.h"
32
33 #include "ardour_ui.h"
34 #include "editor.h"
35 #include "global_signals.h"
36 #include "waveview.h"
37 #include "simplerect.h"
38 #include "simpleline.h"
39 #include "imageframe.h"
40 #include "waveview_p.h"
41 #include "simplerect_p.h"
42 #include "simpleline_p.h"
43 #include "imageframe_p.h"
44 #include "canvas_impl.h"
45 #include "canvas-noevent-text.h"
46 #include "editing.h"
47 #include "rgb_macros.h"
48 #include "utils.h"
49 #include "audio_time_axis.h"
50 #include "editor_drag.h"
51 #include "region_view.h"
52 #include "editor_group_tabs.h"
53 #include "editor_summary.h"
54 #include "keyboard.h"
55 #include "editor_cursors.h"
56 #include "mouse_cursors.h"
57 #include "verbose_cursor.h"
58
59 #include "i18n.h"
60
61 using namespace std;
62 using namespace ARDOUR;
63 using namespace PBD;
64 using namespace Gtk;
65 using namespace Glib;
66 using namespace Gtkmm2ext;
67 using namespace Editing;
68
69 /* XXX this is a hack. it ought to be the maximum value of an framepos_t */
70
71 const double max_canvas_coordinate = (double) JACK_MAX_FRAMES;
72
73 extern "C"
74 {
75
76 GType gnome_canvas_simpleline_get_type(void);
77 GType gnome_canvas_simplerect_get_type(void);
78 GType gnome_canvas_waveview_get_type(void);
79 GType gnome_canvas_imageframe_get_type(void);
80
81 }
82
83 static void ardour_canvas_type_init()
84 {
85         // Map gtypes to gtkmm wrapper-creation functions:
86
87         Glib::wrap_register(gnome_canvas_simpleline_get_type(), &Gnome::Canvas::SimpleLine_Class::wrap_new);
88         Glib::wrap_register(gnome_canvas_simplerect_get_type(), &Gnome::Canvas::SimpleRect_Class::wrap_new);
89         Glib::wrap_register(gnome_canvas_waveview_get_type(), &Gnome::Canvas::WaveView_Class::wrap_new);
90         // Glib::wrap_register(gnome_canvas_imageframe_get_type(), &Gnome::Canvas::ImageFrame_Class::wrap_new);
91
92         // Register the gtkmm gtypes:
93
94         (void) Gnome::Canvas::WaveView::get_type();
95         (void) Gnome::Canvas::SimpleLine::get_type();
96         (void) Gnome::Canvas::SimpleRect::get_type();
97         (void) Gnome::Canvas::ImageFrame::get_type();
98 }
99
100 void
101 Editor::initialize_canvas ()
102 {
103         if (getenv ("ARDOUR_NON_AA_CANVAS")) {
104                 track_canvas = new ArdourCanvas::Canvas ();
105         } else {
106                 track_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         gint phys_width = physical_screen_width (Glib::RefPtr<Gdk::Window>());
118         gint phys_height = physical_screen_height (Glib::RefPtr<Gdk::Window>());
119
120         _verbose_cursor = new VerboseCursor (this);
121
122         /* on the bottom, an image */
123
124         if (Profile->get_sae()) {
125                 Image img (::get_icon (X_("saelogo")));
126                 logo_item = new ArdourCanvas::Pixbuf (*track_canvas->root(), 0.0, 0.0, img.get_pixbuf());
127                 // logo_item->property_height_in_pixels() = true;
128                 // logo_item->property_width_in_pixels() = true;
129                 // logo_item->property_height_set() = true;
130                 // logo_item->property_width_set() = true;
131                 logo_item->show ();
132         }
133
134         /* a group to hold time (measure) lines */
135         time_line_group = new ArdourCanvas::Group (*track_canvas->root());
136
137 #ifdef GTKOSX
138         /*XXX please don't laugh. this actually improves canvas performance on osx */
139         bogus_background_rect =  new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, max_canvas_coordinate/3, phys_height);
140         bogus_background_rect->property_outline_pixels() = 0;
141 #endif
142         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, phys_height);
143         transport_loop_range_rect->property_outline_pixels() = 1;
144         transport_loop_range_rect->hide();
145
146         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, phys_height);
147         transport_punch_range_rect->property_outline_pixels() = 0;
148         transport_punch_range_rect->hide();
149
150         _background_group = new ArdourCanvas::Group (*track_canvas->root());
151         _master_group = new ArdourCanvas::Group (*track_canvas->root());
152
153         _trackview_group = new ArdourCanvas::Group (*_master_group);
154         _region_motion_group = new ArdourCanvas::Group (*_trackview_group);
155
156         meter_bar_group = new ArdourCanvas::Group (*track_canvas->root ());
157         meter_bar = new ArdourCanvas::SimpleRect (*meter_bar_group, 0.0, 0.0, phys_width, timebar_height - 1);
158         meter_bar->property_outline_pixels() = 1;
159         meter_bar->property_outline_what() = 0x8;
160
161         tempo_bar_group = new ArdourCanvas::Group (*track_canvas->root ());
162         tempo_bar = new ArdourCanvas::SimpleRect (*tempo_bar_group, 0.0, 0.0, phys_width, (timebar_height - 1));
163         tempo_bar->property_outline_pixels() = 1;
164         tempo_bar->property_outline_what() = 0x8;
165
166         range_marker_bar_group = new ArdourCanvas::Group (*track_canvas->root ());
167         range_marker_bar = new ArdourCanvas::SimpleRect (*range_marker_bar_group, 0.0, 0.0, phys_width, (timebar_height - 1));
168         range_marker_bar->property_outline_pixels() = 1;
169         range_marker_bar->property_outline_what() = 0x8;
170
171         transport_marker_bar_group = new ArdourCanvas::Group (*track_canvas->root ());
172         transport_marker_bar = new ArdourCanvas::SimpleRect (*transport_marker_bar_group, 0.0, 0.0,  phys_width, (timebar_height - 1));
173         transport_marker_bar->property_outline_pixels() = 1;
174         transport_marker_bar->property_outline_what() = 0x8;
175
176         marker_bar_group = new ArdourCanvas::Group (*track_canvas->root ());
177         marker_bar = new ArdourCanvas::SimpleRect (*marker_bar_group, 0.0, 0.0, phys_width, (timebar_height - 1));
178         marker_bar->property_outline_pixels() = 1;
179         marker_bar->property_outline_what() = 0x8;
180
181         cd_marker_bar_group = new ArdourCanvas::Group (*track_canvas->root ());
182         cd_marker_bar = new ArdourCanvas::SimpleRect (*cd_marker_bar_group, 0.0, 0.0, phys_width, (timebar_height - 1));
183         cd_marker_bar->property_outline_pixels() = 1;
184         cd_marker_bar->property_outline_what() = 0x8;
185
186         timebar_group =  new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
187         cursor_group = new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
188
189         meter_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 5.0);
190         tempo_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 4.0);
191         range_marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 3.0);
192         transport_marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 2.0);
193         marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height);
194         cd_marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, 0.0);
195
196         cd_marker_bar_drag_rect = new ArdourCanvas::SimpleRect (*cd_marker_group, 0.0, 0.0, 100, timebar_height);
197         cd_marker_bar_drag_rect->property_outline_pixels() = 0;
198         cd_marker_bar_drag_rect->hide ();
199
200         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, 100, timebar_height);
201         range_bar_drag_rect->property_outline_pixels() = 0;
202         range_bar_drag_rect->hide ();
203
204         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, 100, timebar_height);
205         transport_bar_drag_rect->property_outline_pixels() = 0;
206         transport_bar_drag_rect->hide ();
207
208         transport_punchin_line = new ArdourCanvas::SimpleLine (*_master_group);
209         transport_punchin_line->property_x1() = 0.0;
210         transport_punchin_line->property_y1() = 0.0;
211         transport_punchin_line->property_x2() = 0.0;
212         transport_punchin_line->property_y2() = phys_height;
213         transport_punchin_line->hide ();
214
215         transport_punchout_line  = new ArdourCanvas::SimpleLine (*_master_group);
216         transport_punchout_line->property_x1() = 0.0;
217         transport_punchout_line->property_y1() = 0.0;
218         transport_punchout_line->property_x2() = 0.0;
219         transport_punchout_line->property_y2() = phys_height;
220         transport_punchout_line->hide();
221
222         // used to show zoom mode active zooming
223         zoom_rect = new ArdourCanvas::SimpleRect (*_master_group, 0.0, 0.0, 0.0, 0.0);
224         zoom_rect->property_outline_pixels() = 1;
225         zoom_rect->hide();
226
227         zoom_rect->signal_event().connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
228
229         // used as rubberband rect
230         rubberband_rect = new ArdourCanvas::SimpleRect (*_trackview_group, 0.0, 0.0, 0.0, 0.0);
231
232         rubberband_rect->property_outline_pixels() = 1;
233         rubberband_rect->hide();
234
235         tempo_bar->signal_event().connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
236         meter_bar->signal_event().connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
237         marker_bar->signal_event().connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
238         cd_marker_bar->signal_event().connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_cd_marker_bar_event), cd_marker_bar));
239         range_marker_bar->signal_event().connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
240         transport_marker_bar->signal_event().connect (sigc::bind (sigc::mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
241
242         playhead_cursor = new EditorCursor (*this, &Editor::canvas_playhead_cursor_event);
243
244         if (logo_item) {
245                 logo_item->lower_to_bottom ();
246         }
247         /* need to handle 4 specific types of events as catch-alls */
248
249         track_canvas->signal_scroll_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_scroll_event));
250         track_canvas->signal_motion_notify_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_motion_notify_event));
251         track_canvas->signal_button_press_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_button_press_event));
252         track_canvas->signal_button_release_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_button_release_event));
253         track_canvas->signal_drag_motion().connect (sigc::mem_fun (*this, &Editor::track_canvas_drag_motion));
254         track_canvas->signal_key_press_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_key_press));
255         track_canvas->signal_key_release_event().connect (sigc::mem_fun (*this, &Editor::track_canvas_key_release));
256
257         track_canvas->set_name ("EditorMainCanvas");
258         track_canvas->add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
259         track_canvas->signal_leave_notify_event().connect (sigc::mem_fun(*this, &Editor::left_track_canvas), false);
260         track_canvas->signal_enter_notify_event().connect (sigc::mem_fun(*this, &Editor::entered_track_canvas), false);
261         track_canvas->set_flags (CAN_FOCUS);
262
263         /* set up drag-n-drop */
264
265         vector<TargetEntry> target_table;
266
267         // Drag-N-Drop from the region list can generate this target
268         target_table.push_back (TargetEntry ("regions"));
269
270         target_table.push_back (TargetEntry ("text/plain"));
271         target_table.push_back (TargetEntry ("text/uri-list"));
272         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
273
274         track_canvas->drag_dest_set (target_table);
275         track_canvas->signal_drag_data_received().connect (sigc::mem_fun(*this, &Editor::track_canvas_drag_data_received));
276
277         track_canvas->signal_size_allocate().connect (sigc::mem_fun(*this, &Editor::track_canvas_allocate));
278
279         ColorsChanged.connect (sigc::mem_fun (*this, &Editor::color_handler));
280         color_handler();
281
282 }
283
284 void
285 Editor::track_canvas_allocate (Gtk::Allocation alloc)
286 {
287         canvas_allocation = alloc;
288         track_canvas_size_allocated ();
289 }
290
291 bool
292 Editor::track_canvas_size_allocated ()
293 {
294         bool height_changed = _canvas_height != canvas_allocation.get_height();
295
296         _canvas_width = canvas_allocation.get_width();
297         _canvas_height = canvas_allocation.get_height();
298
299         if (_session) {
300                 TrackViewList::iterator i;
301
302                 for (i = track_views.begin(); i != track_views.end(); ++i) {
303                         (*i)->clip_to_viewport ();
304                 }
305         }
306
307         if (height_changed) {
308                 if (playhead_cursor) {
309                         playhead_cursor->set_length (_canvas_height);
310                 }
311
312                 for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
313                         i->second->canvas_height_set (_canvas_height);
314                 }
315
316                 vertical_adjustment.set_page_size (_canvas_height);
317                 last_trackview_group_vertical_offset = get_trackview_group_vertical_offset ();
318                 if ((vertical_adjustment.get_value() + _canvas_height) >= vertical_adjustment.get_upper()) {
319                         /*
320                            We're increasing the size of the canvas while the bottom is visible.
321                            We scroll down to keep in step with the controls layout.
322                         */
323                         vertical_adjustment.set_value (full_canvas_height - _canvas_height);
324                 }
325         }
326
327         update_fixed_rulers();
328         redisplay_tempo (false);
329         _summary->set_overlays_dirty ();
330
331         return false;
332 }
333
334 void
335 Editor::reset_controls_layout_width ()
336 {
337         GtkRequisition req;
338         gint w;
339
340         edit_controls_vbox.size_request (req);
341         w = req.width;
342
343         if (_group_tabs->is_mapped()) {
344                 _group_tabs->size_request (req);
345                 w += req.width;
346         }
347
348         /* the controls layout has no horizontal scrolling, its visible
349            width is always equal to the total width of its contents.
350         */
351
352         controls_layout.property_width() = w;
353         controls_layout.property_width_request() = w;
354 }
355
356 void
357 Editor::reset_controls_layout_height (int32_t h)
358 {
359         /* set the height of the scrollable area (i.e. the sum of all contained widgets)
360          */
361
362         controls_layout.property_height() = h;
363
364         /* size request is set elsewhere, see ::track_canvas_allocate() */
365 }
366
367 bool
368 Editor::track_canvas_map_handler (GdkEventAny* /*ev*/)
369 {
370         if (current_canvas_cursor) {
371                 set_canvas_cursor (current_canvas_cursor);
372         }
373         return false;
374 }
375
376 /** This is called when something is dropped onto the track canvas */
377 void
378 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
379                                          int x, int y,
380                                          const SelectionData& data,
381                                          guint info, guint time)
382 {
383         if (data.get_target() == "regions") {
384                 drop_regions (context, x, y, data, info, time);
385         } else {
386                 drop_paths (context, x, y, data, info, time);
387         }
388 }
389
390 bool
391 Editor::idle_drop_paths (vector<string> paths, framepos_t frame, double ypos)
392 {
393         drop_paths_part_two (paths, frame, ypos);
394         return false;
395 }
396
397 void
398 Editor::drop_paths_part_two (const vector<string>& paths, framepos_t frame, double ypos)
399 {
400         RouteTimeAxisView* tv;
401
402         std::pair<TimeAxisView*, int> const tvp = trackview_by_y_position (ypos);
403         if (tvp.first == 0) {
404
405                 /* drop onto canvas background: create new tracks */
406
407                 frame = 0;
408
409                 if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
410                         do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsTrack, SrcBest, frame);
411                 } else {
412                         do_embed (paths, Editing::ImportDistinctFiles, ImportAsTrack, frame);
413                 }
414
415         } else if ((tv = dynamic_cast<RouteTimeAxisView*> (tvp.first)) != 0) {
416
417                 /* check that its an audio track, not a bus */
418
419                 if (tv->track()) {
420                         /* select the track, then embed/import */
421                         selection->set (tv);
422
423                         if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
424                                 do_import (paths, Editing::ImportSerializeFiles, Editing::ImportToTrack, SrcBest, frame);
425                         } else {
426                                 do_embed (paths, Editing::ImportSerializeFiles, ImportToTrack, frame);
427                         }
428                 }
429         }
430 }
431
432 void
433 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
434                     int x, int y,
435                     const SelectionData& data,
436                     guint info, guint time)
437 {
438         vector<string> paths;
439         GdkEvent ev;
440         framepos_t frame;
441         double wx;
442         double wy;
443         double cy;
444
445         if (convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
446
447                 /* D-n-D coordinates are window-relative, so convert to "world" coordinates
448                  */
449
450                 track_canvas->window_to_world (x, y, wx, wy);
451
452                 ev.type = GDK_BUTTON_RELEASE;
453                 ev.button.x = wx;
454                 ev.button.y = wy;
455
456                 frame = event_frame (&ev, 0, &cy);
457
458                 snap_to (frame);
459
460 #ifdef GTKOSX
461                 /* We are not allowed to call recursive main event loops from within
462                    the main event loop with GTK/Quartz. Since import/embed wants
463                    to push up a progress dialog, defer all this till we go idle.
464                 */
465                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &Editor::idle_drop_paths), paths, frame, cy));
466 #else
467                 drop_paths_part_two (paths, frame, cy);
468 #endif
469         }
470
471         context->drag_finish (true, false, time);
472 }
473
474 void
475 Editor::drop_regions (const RefPtr<Gdk::DragContext>& /*context*/,
476                       int /*x*/, int /*y*/,
477                       const SelectionData& /*data*/,
478                       guint /*info*/, guint /*time*/)
479 {
480         _drags->end_grab (0);
481 }
482
483 /** If the editor window is arranged such that the edge of the trackview is right up
484  *  against the edge of the screen, autoscroll will not work very well.  In this situation,
485  *  we start autoscrolling some distance in from the right-hand-side of the screen edge;
486  *  this is the distance at which that happens.
487  */
488 int
489 Editor::autoscroll_fudge_threshold () const
490 {
491         return current_page_frames() / 6;
492 }
493
494 /** @param allow_horiz true to allow horizontal autoscroll, otherwise false.
495  *  @param allow_vert true to allow vertical autoscroll, otherwise false.
496  *  @param moving_left true if we are moving left, so we only want to autoscroll on the left of the canvas,
497  *  otherwise false, so we only want to autoscroll on the right of the canvas.
498  *  @param moving_up true if we are moving up, so we only want to autoscroll at the top of the canvas,
499  *  otherwise false, so we only want to autoscroll at the bottom of the canvas.
500  */
501 void
502 Editor::maybe_autoscroll (bool allow_horiz, bool allow_vert, bool moving_left, bool moving_up)
503 {
504         if (!Config->get_autoscroll_editor ()) {
505                 return;
506         }
507         
508         bool startit = false;
509
510         /* Work out the distance between the right hand edge of the trackview and the edge of
511            the monitor that it is on.
512         */
513
514         Glib::RefPtr<Gdk::Window> gdk_window = get_window ();
515         Gdk::Rectangle window_rect;
516         gdk_window->get_frame_extents (window_rect);
517         
518         Glib::RefPtr<Gdk::Screen> screen = get_screen ();
519         Gdk::Rectangle root_rect;
520         screen->get_root_window()->get_frame_extents (root_rect);
521
522         Gtk::Allocation editor_list = _the_notebook.get_allocation ();
523
524         framecnt_t distance = pixel_to_frame (root_rect.get_x() + root_rect.get_width() - window_rect.get_x() - window_rect.get_width());
525         if (_the_notebook.is_visible ()) {
526                 distance += pixel_to_frame (editor_list.get_width());
527         }
528
529         /* Note whether we're fudging the autoscroll (see autoscroll_fudge_threshold) */
530         _autoscroll_fudging = (distance < autoscroll_fudge_threshold ());
531
532         double const ty = _drags->current_pointer_y() - get_trackview_group_vertical_offset ();
533
534         autoscroll_y = 0;
535         autoscroll_x = 0;
536         if (ty < canvas_timebars_vsize && moving_up && allow_vert) {
537                 autoscroll_y = -1;
538                 startit = true;
539         } else if (ty > _canvas_height && !moving_up && allow_vert) {
540                 autoscroll_y = 1;
541                 startit = true;
542         }
543
544         framepos_t rightmost_frame = leftmost_frame + current_page_frames();
545         if (_autoscroll_fudging) {
546                 rightmost_frame -= autoscroll_fudge_threshold ();
547         }
548
549         if (_drags->current_pointer_frame() > rightmost_frame && allow_horiz) {
550                 if (rightmost_frame < max_framepos && !moving_left) {
551                         autoscroll_x = 1;
552                         startit = true;
553                 }
554         } else if (_drags->current_pointer_frame() < leftmost_frame && allow_horiz) {
555                 if (leftmost_frame > 0 && moving_left) {
556                         autoscroll_x = -1;
557                         startit = true;
558                 }
559         }
560
561         if (autoscroll_active && ((autoscroll_x != last_autoscroll_x) || (autoscroll_y != last_autoscroll_y) || (autoscroll_x == 0 && autoscroll_y == 0))) {
562                 stop_canvas_autoscroll ();
563         }
564
565         if (startit && autoscroll_timeout_tag < 0) {
566                 start_canvas_autoscroll (autoscroll_x, autoscroll_y);
567         }
568
569         last_autoscroll_x = autoscroll_x;
570         last_autoscroll_y = autoscroll_y;
571 }
572
573 gint
574 Editor::_autoscroll_canvas (void *arg)
575 {
576         return ((Editor *) arg)->autoscroll_canvas ();
577 }
578
579 bool
580 Editor::autoscroll_canvas ()
581 {
582         framepos_t new_frame;
583         framepos_t limit = max_framepos - current_page_frames();
584         GdkEventMotion ev;
585         double new_pixel;
586         double target_pixel;
587         
588         if (autoscroll_x_distance != 0) {
589
590                 if (autoscroll_x > 0) {
591                         autoscroll_x_distance = (_drags->current_pointer_frame() - (leftmost_frame + current_page_frames())) / 3;
592                         if (_autoscroll_fudging) {
593                                 autoscroll_x_distance += autoscroll_fudge_threshold () / 3;
594                         }
595                 } else if (autoscroll_x < 0) {
596                         autoscroll_x_distance = (leftmost_frame - _drags->current_pointer_frame()) / 3;
597
598                 }
599         }
600
601         if (autoscroll_y_distance != 0) {
602                 if (autoscroll_y > 0) {
603                         autoscroll_y_distance = (_drags->current_pointer_y() - (get_trackview_group_vertical_offset() + _canvas_height)) / 3;
604                 } else if (autoscroll_y < 0) {
605
606                         autoscroll_y_distance = (vertical_adjustment.get_value () - _drags->current_pointer_y()) / 3;
607                 }
608         }
609
610         if (autoscroll_x < 0) {
611                 if (leftmost_frame < autoscroll_x_distance) {
612                         new_frame = 0;
613                 } else {
614                         new_frame = leftmost_frame - autoscroll_x_distance;
615                 }
616         } else if (autoscroll_x > 0) {
617                 if (leftmost_frame > limit - autoscroll_x_distance) {
618                         new_frame = limit;
619                 } else {
620                         new_frame = leftmost_frame + autoscroll_x_distance;
621                 }
622         } else {
623                 new_frame = leftmost_frame;
624         }
625
626         double vertical_pos = vertical_adjustment.get_value();
627
628         if (autoscroll_y < 0) {
629
630                 if (vertical_pos < autoscroll_y_distance) {
631                         new_pixel = 0;
632                 } else {
633                         new_pixel = vertical_pos - autoscroll_y_distance;
634                 }
635
636                 target_pixel = _drags->current_pointer_y() - autoscroll_y_distance;
637                 target_pixel = max (target_pixel, 0.0);
638
639         } else if (autoscroll_y > 0) {
640
641                 double top_of_bottom_of_canvas = full_canvas_height - _canvas_height;
642
643                 if (vertical_pos > full_canvas_height - autoscroll_y_distance) {
644                         new_pixel = full_canvas_height;
645                 } else {
646                         new_pixel = vertical_pos + autoscroll_y_distance;
647                 }
648
649                 new_pixel = min (top_of_bottom_of_canvas, new_pixel);
650
651                 target_pixel = _drags->current_pointer_y() + autoscroll_y_distance;
652
653                 /* don't move to the full canvas height because the item will be invisible
654                    (its top edge will line up with the bottom of the visible canvas.
655                 */
656
657                 target_pixel = min (target_pixel, full_canvas_height - 10);
658
659         } else {
660                 target_pixel = _drags->current_pointer_y();
661                 new_pixel = vertical_pos;
662         }
663
664         if ((new_frame == 0 || new_frame == limit) && (new_pixel == 0 || new_pixel == DBL_MAX)) {
665                 /* we are done */
666                 return false;
667         }
668
669         if (new_frame != leftmost_frame) {
670                 reset_x_origin (new_frame);
671         }
672
673         vertical_adjustment.set_value (new_pixel);
674
675         /* fake an event. */
676
677         Glib::RefPtr<Gdk::Window> canvas_window = const_cast<Editor*>(this)->track_canvas->get_window();
678         gint x, y;
679         Gdk::ModifierType mask;
680         canvas_window->get_pointer (x, y, mask);
681         ev.type = GDK_MOTION_NOTIFY;
682         ev.state = Gdk::BUTTON1_MASK;
683         ev.x = x;
684         ev.y = y;
685
686         motion_handler (0, (GdkEvent*) &ev, true);
687
688         autoscroll_cnt++;
689
690         if (autoscroll_cnt == 1) {
691
692                 /* connect the timeout so that we get called repeatedly */
693
694                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
695                 return false;
696
697         }
698
699         return true;
700 }
701
702 void
703 Editor::start_canvas_autoscroll (int dx, int dy)
704 {
705         if (!_session || autoscroll_active) {
706                 return;
707         }
708
709         stop_canvas_autoscroll ();
710
711         autoscroll_active = true;
712         autoscroll_x = dx;
713         autoscroll_y = dy;
714         autoscroll_x_distance = (framepos_t) floor (current_page_frames()/50.0);
715         autoscroll_y_distance = fabs (dy * 5); /* pixels */
716         autoscroll_cnt = 0;
717
718         /* do it right now, which will start the repeated callbacks */
719
720         autoscroll_canvas ();
721 }
722
723 void
724 Editor::stop_canvas_autoscroll ()
725 {
726         if (autoscroll_timeout_tag >= 0) {
727                 g_source_remove (autoscroll_timeout_tag);
728                 autoscroll_timeout_tag = -1;
729         }
730
731         autoscroll_active = false;
732 }
733
734 bool
735 Editor::left_track_canvas (GdkEventCrossing */*ev*/)
736 {
737         DropDownKeys ();
738         within_track_canvas = false;
739         //cerr << "left track canvas\n";
740         set_entered_track (0);
741         set_entered_regionview (0);
742         reset_canvas_action_sensitivity (false);
743         return false;
744 }
745
746 bool
747 Editor::entered_track_canvas (GdkEventCrossing */*ev*/)
748 {
749         //cerr << "entered track canvas\n";
750         within_track_canvas = true;
751         reset_canvas_action_sensitivity (true);
752         return FALSE;
753 }
754
755 void
756 Editor::ensure_time_axis_view_is_visible (const TimeAxisView& tav)
757 {
758         double begin = tav.y_position();
759
760         double v = vertical_adjustment.get_value ();
761
762         if (begin < v || begin + tav.current_height() > v + _canvas_height - canvas_timebars_vsize) {
763                 /* try to put the TimeAxisView roughly central */
764                 if (begin >= _canvas_height/2.0) {
765                         begin -= _canvas_height/2.0;
766                 }
767                 vertical_adjustment.set_value (begin);
768         }
769 }
770
771 void
772 Editor::tie_vertical_scrolling ()
773 {
774         scroll_canvas_vertically ();
775
776         /* this will do an immediate redraw */
777
778         controls_layout.get_vadjustment()->set_value (vertical_adjustment.get_value());
779
780         if (pending_visual_change.idle_handler_id < 0) {
781                 _summary->set_overlays_dirty ();
782         }
783 }
784
785 void
786 Editor::set_horizontal_position (double p)
787 {
788         /* horizontal scrolling only */
789         double x1, y1, x2, y2, x_delta;
790         _master_group->get_bounds (x1, y1, x2, y2);
791
792         x_delta = - (x1 + p);
793
794         _master_group->move (x_delta, 0);
795         timebar_group->move (x_delta, 0);
796         time_line_group->move (x_delta, 0);
797         cursor_group->move (x_delta, 0);
798
799         leftmost_frame = (framepos_t) floor (p * frames_per_unit);
800
801         update_fixed_rulers ();
802         redisplay_tempo (true);
803
804         if (pending_visual_change.idle_handler_id < 0) {
805                 _summary->set_overlays_dirty ();
806         }
807
808         HorizontalPositionChanged (); /* EMIT SIGNAL */
809
810 #ifndef GTKOSX
811         if (!autoscroll_active && !_stationary_playhead) {
812                 /* force rulers and canvas to move in lock step */
813                 while (gtk_events_pending ()) {
814                         gtk_main_iteration ();
815                 }
816         }
817 #endif
818
819 }
820
821 void
822 Editor::scroll_canvas_vertically ()
823 {
824         /* vertical scrolling only */
825
826         double y_delta;
827
828         y_delta = last_trackview_group_vertical_offset - get_trackview_group_vertical_offset ();
829         _trackview_group->move (0, y_delta);
830         _background_group->move (0, y_delta);
831
832         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
833                 (*i)->clip_to_viewport ();
834         }
835         last_trackview_group_vertical_offset = get_trackview_group_vertical_offset ();
836         /* required to keep the controls_layout in lock step with the canvas group */
837         update_canvas_now ();
838 }
839
840 void
841 Editor::color_handler()
842 {
843         playhead_cursor->canvas_item.property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_PlayHead.get();
844         _verbose_cursor->set_color (ARDOUR_UI::config()->canvasvar_VerboseCanvasCursor.get());
845
846         meter_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MeterBar.get();
847         meter_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
848
849         tempo_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TempoBar.get();
850         tempo_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
851
852         marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBar.get();
853         marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
854
855         cd_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CDMarkerBar.get();
856         cd_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
857
858         range_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeMarkerBar.get();
859         range_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
860
861         transport_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportMarkerBar.get();
862         transport_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
863
864         cd_marker_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
865         cd_marker_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
866
867         range_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
868         range_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
869
870         transport_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
871         transport_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
872
873         transport_loop_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
874         transport_loop_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
875
876         transport_punch_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
877         transport_punch_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
878
879         transport_punchin_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
880         transport_punchout_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
881
882         zoom_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
883         zoom_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
884
885         rubberband_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
886         rubberband_rect->property_fill_color_rgba() = (guint32) ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
887
888         location_marker_color = ARDOUR_UI::config()->canvasvar_LocationMarker.get();
889         location_range_color = ARDOUR_UI::config()->canvasvar_LocationRange.get();
890         location_cd_marker_color = ARDOUR_UI::config()->canvasvar_LocationCDMarker.get();
891         location_loop_color = ARDOUR_UI::config()->canvasvar_LocationLoop.get();
892         location_punch_color = ARDOUR_UI::config()->canvasvar_LocationPunch.get();
893
894         refresh_location_display ();
895 /*
896         redisplay_tempo (true);
897
898         if (_session)
899               _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
900 */
901 }
902
903 void
904 Editor::flush_canvas ()
905 {
906         if (is_mapped()) {
907                 update_canvas_now ();
908                 // gdk_window_process_updates (GTK_LAYOUT(track_canvas->gobj())->bin_window, true);
909         }
910 }
911
912 void
913 Editor::update_canvas_now ()
914 {
915         /* GnomeCanvas has a bug whereby if its idle handler is not scheduled between
916            two calls to update_now, an assert will trip.  This wrapper works around
917            that problem by only calling update_now if the assert will not trip.
918
919            I think the GC bug is due to the fact that its code will reset need_update
920            and need_redraw to FALSE without checking to see if an idle handler is scheduled.
921            If one is scheduled, GC should probably remove it.
922         */
923
924         GnomeCanvas* c = track_canvas->gobj ();
925         if (c->need_update || c->need_redraw) {
926                 track_canvas->update_now ();
927         }
928 }
929
930 double
931 Editor::horizontal_position () const
932 {
933         return frame_to_unit (leftmost_frame);
934 }
935
936 void
937 Editor::set_canvas_cursor (Gdk::Cursor* cursor, bool save)
938 {
939         if (save) {
940                 current_canvas_cursor = cursor;
941         }
942
943         Glib::RefPtr<Gdk::Window> win = track_canvas->get_window();
944
945         if (win) {
946                 track_canvas->get_window()->set_cursor (*cursor);
947         }
948 }
949
950 bool
951 Editor::track_canvas_key_press (GdkEventKey*)
952 {
953         /* XXX: event does not report the modifier key pressed down, AFAICS, so use the Keyboard object instead */
954         if (mouse_mode == Editing::MouseZoom && Keyboard::the_keyboard().key_is_down (GDK_Control_L)) {
955                 set_canvas_cursor (_cursors->zoom_out, true);
956         }
957
958         return false;
959 }
960
961 bool
962 Editor::track_canvas_key_release (GdkEventKey*)
963 {
964         if (mouse_mode == Editing::MouseZoom && !Keyboard::the_keyboard().key_is_down (GDK_Control_L)) {
965                 set_canvas_cursor (_cursors->zoom_in, true);
966         }
967
968         return false;
969 }