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