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