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