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