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