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