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