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