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