ef6dc25777140ce99a58fe8de060491d6a859448
[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 #include <libgnomecanvasmm/init.h>
21 #include <jack/types.h>
22 #include <gtkmm2ext/utils.h>
23
24 #include <ardour/audioregion.h>
25 #include <ardour/profile.h>
26
27 #include "ardour_ui.h"
28 #include "editor.h"
29 #include "waveview.h"
30 #include "simplerect.h"
31 #include "simpleline.h"
32 #include "waveview_p.h"
33 #include "simplerect_p.h"
34 #include "simpleline_p.h"
35 #include "canvas_impl.h"
36 #include "editing.h"
37 #include "rgb_macros.h"
38 #include "utils.h"
39 #include "time_axis_view.h"
40 #include "audio_time_axis.h"
41
42 #ifdef WITH_CMT
43 #include "imageframe.h"
44 #include "imageframe_p.h"
45 #endif
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace sigc;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Gtk;
54 using namespace Glib;
55 using namespace Gtkmm2ext;
56 using namespace Editing;
57
58 /* XXX this is a hack. it ought to be the maximum value of an nframes64_t */
59
60 const double max_canvas_coordinate = (double) JACK_MAX_FRAMES;
61
62 extern "C"
63 {
64
65 GType gnome_canvas_simpleline_get_type(void);
66 GType gnome_canvas_simplerect_get_type(void);
67 GType gnome_canvas_waveview_get_type(void);
68
69 #ifdef WITH_CMT
70 GType gnome_canvas_imageframe_get_type(void);
71 #endif
72
73 }
74
75 static void ardour_canvas_type_init() 
76 {
77         // Map gtypes to gtkmm wrapper-creation functions:
78         
79         Glib::wrap_register(gnome_canvas_simpleline_get_type(), &Gnome::Canvas::SimpleLine_Class::wrap_new);
80         Glib::wrap_register(gnome_canvas_simplerect_get_type(), &Gnome::Canvas::SimpleRect_Class::wrap_new);
81         Glib::wrap_register(gnome_canvas_waveview_get_type(), &Gnome::Canvas::WaveView_Class::wrap_new);
82
83 #ifdef WITH_CMT
84         Glib::wrap_register(gnome_canvas_imageframe_get_type(), &Gnome::Canvas::ImageFrame_Class::wrap_new);
85 #endif
86         
87         // Register the gtkmm gtypes:
88
89         (void) Gnome::Canvas::WaveView::get_type();
90         (void) Gnome::Canvas::SimpleLine::get_type();
91         (void) Gnome::Canvas::SimpleRect::get_type();
92         
93 #ifdef WITH_CMT
94         (void) Gnome::Canvas::ImageFrame::get_type();
95 #endif
96
97
98
99 void
100 Editor::initialize_canvas ()
101 {
102         if (getenv ("ARDOUR_NON_AA_CANVAS")) {
103                 track_canvas = new ArdourCanvas::Canvas ();
104         } else {
105                 track_canvas = new ArdourCanvas::CanvasAA ();
106         }
107         
108         ArdourCanvas::init ();
109         ardour_canvas_type_init ();
110
111         /* don't try to center the canvas */
112
113         track_canvas->set_center_scroll_region (false);
114         track_canvas->set_dither (Gdk::RGB_DITHER_NONE);
115
116         /* stuff for the verbose canvas cursor */
117
118         Pango::FontDescription* font = get_font_for_style (N_("VerboseCanvasCursor"));
119
120         verbose_canvas_cursor = new ArdourCanvas::Text (*track_canvas->root());
121         verbose_canvas_cursor->property_font_desc() = *font;
122         verbose_canvas_cursor->property_anchor() = ANCHOR_NW;
123         
124         delete font;
125
126         verbose_cursor_visible = false;
127         
128         if (Profile->get_sae()) {
129                 Image img (::get_icon (X_("saelogo")));
130                 logo_item = new ArdourCanvas::Pixbuf (*track_canvas->root(), 0.0, 0.0, img.get_pixbuf());
131                 // logo_item->property_height_in_pixels() = true;
132                 // logo_item->property_width_in_pixels() = true;
133                 // logo_item->property_height_set() = true;
134                 // logo_item->property_width_set() = true;
135                 logo_item->show ();
136         }
137
138         _master_group = new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
139
140         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*_master_group, 0.0, 0.0, 0.0, 0.0);
141         transport_loop_range_rect->property_outline_pixels() = 1;
142         transport_loop_range_rect->hide();
143
144         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*_master_group, 0.0, 0.0, 0.0, 0.0);
145         transport_punch_range_rect->property_outline_pixels() = 0;
146         transport_punch_range_rect->hide();
147
148         /* a group to hold time (measure) lines */
149         time_line_group = new ArdourCanvas::Group (*_master_group, 0.0, 0.0);
150
151         range_marker_drag_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
152         range_marker_drag_rect->hide ();
153
154         _trackview_group = new ArdourCanvas::Group (*_master_group, 0.0, 0.0);
155         _region_motion_group = new ArdourCanvas::Group (*_trackview_group, 0.0, 0.0);
156
157         /* el barrio */
158
159         meter_bar_group = new ArdourCanvas::Group (*track_canvas->root());
160         meter_bar = new ArdourCanvas::SimpleRect (*meter_bar_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
161         meter_bar->property_outline_what() = (0x1 | 0x8);
162         meter_bar->property_outline_pixels() = 1;
163
164         tempo_bar_group = new ArdourCanvas::Group (*track_canvas->root());
165         tempo_bar = new ArdourCanvas::SimpleRect (*tempo_bar_group, 0.0, 0.0, max_canvas_coordinate, (timebar_height-1.0));
166         tempo_bar->property_outline_what() = (0x1 | 0x8);
167         tempo_bar->property_outline_pixels() = 1;
168
169         range_marker_bar_group = new ArdourCanvas::Group (*track_canvas->root());
170         range_marker_bar = new ArdourCanvas::SimpleRect (*range_marker_bar_group, 0.0, 0.0, max_canvas_coordinate, (timebar_height-1.0));
171         range_marker_bar->property_outline_what() = (0x1 | 0x8);
172         range_marker_bar->property_outline_pixels() = 1;
173         
174         transport_marker_bar_group = new ArdourCanvas::Group (*track_canvas->root());
175         transport_marker_bar = new ArdourCanvas::SimpleRect (*transport_marker_bar_group, 0.0, 0.0, max_canvas_coordinate, (timebar_height-1.0));
176         transport_marker_bar->property_outline_what() = (0x1 | 0x8);
177         transport_marker_bar->property_outline_pixels() = 1;
178
179         marker_bar_group = new ArdourCanvas::Group (*track_canvas->root());
180         marker_bar = new ArdourCanvas::SimpleRect (*marker_bar_group, 0.0, 0.0, max_canvas_coordinate, (timebar_height-1.0));
181         marker_bar->property_outline_what() = (0x1 | 0x8);
182         marker_bar->property_outline_pixels() = 1;
183         
184         cd_marker_bar_group = new ArdourCanvas::Group (*track_canvas->root());
185         cd_marker_bar = new ArdourCanvas::SimpleRect (*cd_marker_bar_group, 0.0, 0.0, max_canvas_coordinate, (timebar_height-1.0));
186         cd_marker_bar->property_outline_what() = (0x1 | 0x8);
187         cd_marker_bar->property_outline_pixels() = 1;
188
189         timebar_group =  new ArdourCanvas::Group (*track_canvas->root());
190         cursor_group = new ArdourCanvas::Group (*track_canvas->root(), 0.0, 0.0);
191
192         meter_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 5.0);
193         tempo_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 4.0);
194         range_marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 3.0);
195         transport_marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height * 2.0);
196         marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, timebar_height);
197         cd_marker_group = new ArdourCanvas::Group (*timebar_group, 0.0, 0.0);
198
199         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
200         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 1.0));
201
202         marker_drag_line = new ArdourCanvas::Line (*timebar_group);
203         marker_drag_line->property_width_pixels() = 1;
204         marker_drag_line->property_points() = marker_drag_line_points;
205         marker_drag_line->hide();
206
207         cd_marker_bar_drag_rect = new ArdourCanvas::SimpleRect (*cd_marker_group, 0.0, 0.0, 100, timebar_height);
208         cd_marker_bar_drag_rect->property_outline_pixels() = 0;
209         cd_marker_bar_drag_rect->hide ();
210
211         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, 100, timebar_height);
212         range_bar_drag_rect->property_outline_pixels() = 0;
213         range_bar_drag_rect->hide ();
214
215         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, 100, timebar_height);
216         transport_bar_drag_rect->property_outline_pixels() = 0;
217         transport_bar_drag_rect->hide ();
218
219         transport_punchin_line = new ArdourCanvas::SimpleLine (*_master_group);
220         transport_punchin_line->property_x1() = 0.0;
221         transport_punchin_line->property_y1() = 0.0;
222         transport_punchin_line->property_x2() = 0.0;
223         transport_punchin_line->property_y2() = 0.0;
224         transport_punchin_line->hide ();
225         
226         transport_punchout_line = new ArdourCanvas::SimpleLine (*_master_group);
227         transport_punchout_line->property_x1() = 0.0;
228         transport_punchout_line->property_y1() = 0.0;
229         transport_punchout_line->property_x2() = 0.0;
230         transport_punchout_line->property_y2() = 0.0;
231         transport_punchout_line->hide();
232         
233         // used to show zoom mode active zooming
234         zoom_rect = new ArdourCanvas::SimpleRect (*_master_group, 0.0, 0.0, 0.0, 0.0);
235         zoom_rect->property_outline_pixels() = 1;
236         zoom_rect->hide();
237         
238         zoom_rect->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
239         
240         // used as rubberband rect
241         rubberband_rect = new ArdourCanvas::SimpleRect (*_trackview_group, 0.0, 0.0, 0.0, 0.0);
242
243         rubberband_rect->property_outline_pixels() = 1;
244         rubberband_rect->hide();
245         
246         tempo_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
247         meter_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
248         marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
249         cd_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_cd_marker_bar_event), cd_marker_bar));
250         range_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
251         transport_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
252
253         playhead_cursor = new Cursor (*this, &Editor::canvas_playhead_cursor_event);
254
255         if (logo_item) {
256                 logo_item->lower_to_bottom ();
257         }
258
259         /* need to handle 4 specific types of events as catch-alls */
260
261         track_canvas->signal_scroll_event().connect (mem_fun (*this, &Editor::track_canvas_scroll_event));
262         track_canvas->signal_motion_notify_event().connect (mem_fun (*this, &Editor::track_canvas_motion_notify_event));
263         track_canvas->signal_button_press_event().connect (mem_fun (*this, &Editor::track_canvas_button_press_event));
264         track_canvas->signal_button_release_event().connect (mem_fun (*this, &Editor::track_canvas_button_release_event));
265
266         track_canvas->set_name ("EditorMainCanvas");
267         track_canvas->add_events (Gdk::POINTER_MOTION_HINT_MASK|Gdk::SCROLL_MASK);
268         track_canvas->signal_leave_notify_event().connect (mem_fun(*this, &Editor::left_track_canvas));
269         track_canvas->signal_enter_notify_event().connect (mem_fun(*this, &Editor::entered_track_canvas));
270         track_canvas->set_flags (CAN_FOCUS);
271
272         /* set up drag-n-drop */
273
274         vector<TargetEntry> target_table;
275         
276         // Drag-N-Drop from the region list can generate this target
277         target_table.push_back (TargetEntry ("regions"));
278         target_table.push_back (TargetEntry ("routes"));
279
280         target_table.push_back (TargetEntry ("text/plain"));
281         target_table.push_back (TargetEntry ("text/uri-list"));
282         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
283
284         track_canvas->drag_dest_set (target_table);
285         track_canvas->signal_drag_data_received().connect (mem_fun(*this, &Editor::track_canvas_drag_data_received));
286
287         track_canvas->signal_size_allocate().connect (mem_fun(*this, &Editor::track_canvas_allocate));
288
289         ColorsChanged.connect (mem_fun (*this, &Editor::color_handler));
290         color_handler();
291
292 }
293
294 void
295 Editor::track_canvas_allocate (Gtk::Allocation alloc)
296 {
297         canvas_allocation = alloc;
298         track_canvas_size_allocated ();
299 }
300
301 bool
302 Editor::track_canvas_size_allocated ()
303 {
304         bool height_changed = canvas_height != canvas_allocation.get_height();
305
306         canvas_width = canvas_allocation.get_width();
307         canvas_height = canvas_allocation.get_height();
308
309         full_canvas_height = canvas_height;
310
311         if (session) {
312                 TrackViewList::iterator i;
313                 double height = 0;
314
315                 for (i = track_views.begin(); i != track_views.end(); ++i) {
316                         if ((*i)->control_parent) {
317                                 height += (*i)->effective_height;
318                         }
319                 }
320                 
321                 full_canvas_height = height + canvas_timebars_vsize;
322         } else {
323                 return true;
324         }
325
326         if (height_changed) {
327                 if (playhead_cursor) {
328                         playhead_cursor->set_length (canvas_height);
329                 }
330                 
331                 vertical_adjustment.set_page_size (canvas_height);
332                 
333                 for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {
334                         (*x)->set_line_vpos (0, canvas_height);
335                 }
336                 
337                 //      marker_drag_line_points.back().set_y(canvas_height);
338                 range_marker_drag_rect->property_y2() = canvas_height;
339                 transport_loop_range_rect->property_y2() = canvas_height;
340                 transport_punch_range_rect->property_y2() = canvas_height;
341                 transport_punchin_line->property_y2() = canvas_height;
342                 transport_punchout_line->property_y2() = canvas_height;
343         }
344         horizontal_adjustment.set_upper (session->current_end_frame()/frames_per_unit);
345         horizontal_adjustment.set_page_size (current_page_frames()/frames_per_unit);
346         reset_hscrollbar_stepping ();
347         update_fixed_rulers();
348         redisplay_tempo (false);
349         
350         Resized (); /* EMIT_SIGNAL */
351                 
352         return false;
353 }
354
355 void
356 Editor::controls_layout_size_request (Requisition* req)
357 {
358         TreeModel::Children rows = route_display_model->children();
359         TreeModel::Children::iterator i;
360         double pos;
361         bool changed = false;
362
363         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
364                 TimeAxisView *tv = (*i)[route_display_columns.tv];
365                 if (tv != 0) {
366                         pos += tv->effective_height;
367                 }
368         }
369
370         RefPtr<Gdk::Screen> screen = get_screen();
371
372         if (!screen) {
373                 screen = Gdk::Screen::get_default();
374         }
375         gint height = min ( (gint) pos, (screen->get_height() - 400));
376         gint width = max (edit_controls_vbox.get_width(),  controls_layout.get_width());
377
378         /* don't get too big. the fudge factors here are just guesses */
379
380         width =  min (width, screen->get_width() - 300);
381
382         if ((req->width != width) || (req->height != height)) {
383                 changed = true;
384                 controls_layout_size_request_connection.disconnect ();
385         }
386
387         if (req->width != width) {
388                 gint vbox_width = edit_controls_vbox.get_width();
389                 req->width = width;
390
391                 /* this one is important: it determines how big the layout thinks it really is, as 
392                    opposed to what it displays on the screen
393                 */
394                 controls_layout.property_width () = vbox_width;
395                 controls_layout.property_width_request () = vbox_width;
396
397                 time_button_event_box.property_width_request () = vbox_width;
398                 zoom_box.property_width_request () = vbox_width;
399         }
400
401         if (req->height != height) {
402                 req->height = height;
403                 controls_layout.property_height () = (guint) floor (pos);
404         }
405
406         if (changed) {
407                 controls_layout_size_request_connection = controls_layout.signal_size_request().connect (mem_fun (*this, &Editor::controls_layout_size_request));
408         }
409 }
410
411 bool
412 Editor::track_canvas_map_handler (GdkEventAny* ev)
413 {
414         track_canvas->get_window()->set_cursor (*current_canvas_cursor);
415         return false;
416 }
417
418 void  
419 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
420                                          int x, int y, 
421                                          const SelectionData& data,
422                                          guint info, guint time)
423 {
424         cerr << "drop on canvas, target = " << data.get_target() << endl;
425
426         if (data.get_target() == "regions") {
427                 drop_regions (context, x, y, data, info, time);
428         }
429         else if(data.get_target() == "routes") {
430                 drop_routes (context, x, y, data, info, time);
431         }
432         else {
433                 drop_paths (context, x, y, data, info, time);
434         }
435 }
436
437 void
438 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
439                     int x, int y, 
440                     const SelectionData& data,
441                     guint info, guint time)
442 {
443         TimeAxisView* tvp;
444         RouteTimeAxisView* tv;
445         double cy;
446         vector<ustring> paths;
447         string spath;
448         GdkEvent ev;
449         nframes64_t frame;
450
451         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
452                 goto out;
453         }
454
455         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
456          */
457
458         double wx;
459         double wy;
460
461         track_canvas->window_to_world (x, y, wx, wy);
462         //wx += horizontal_adjustment.get_value();
463         //wy += vertical_adjustment.get_value();
464         
465         ev.type = GDK_BUTTON_RELEASE;
466         ev.button.x = wx;
467         ev.button.y = wy;
468
469         frame = event_frame (&ev, 0, &cy);
470
471         snap_to (frame);
472
473         if ((tvp = trackview_by_y_position (cy)) == 0) {
474
475                 /* drop onto canvas background: create new tracks */
476
477                 frame = 0;
478
479                 if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
480                         do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsTrack, SrcBest, frame); 
481                 } else {
482                         do_embed (paths, Editing::ImportDistinctFiles, ImportAsTrack, frame);
483                 }
484                 
485         } else if ((tv = dynamic_cast<RouteTimeAxisView*>(tvp)) != 0) {
486
487                 /* check that its an audio track, not a bus */
488                 
489                 /* check that its an audio track, not a bus */
490                 
491                 if (tv->get_diskstream()) {
492                         /* select the track, then embed */
493                         selection->set (tv);
494
495                         if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
496                                 do_import (paths, Editing::ImportDistinctFiles, Editing::ImportToTrack, SrcBest, frame); 
497                         } else {
498                                 do_embed (paths, Editing::ImportDistinctFiles, ImportToTrack, frame);
499                         }
500                 }
501         }
502
503   out:
504         context->drag_finish (true, false, time);
505 }
506
507 void
508 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
509                       int x, int y, 
510                       const SelectionData& data,
511                       guint info, guint time)
512 {
513         const SerializedObjectPointers<boost::shared_ptr<Region> >* sr = 
514                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Region> > *> (data.get_data());
515
516         for (uint32_t i = 0; i < sr->cnt; ++i) {
517
518                 boost::shared_ptr<Region> r = sr->data[i];
519                 
520                 insert_region_list_drag (r, x, y);
521         }
522
523         context->drag_finish (true, false, time);
524 }
525
526 void
527 Editor::drop_routes (const Glib::RefPtr<Gdk::DragContext>& context,
528                      int x, int y,
529                      const Gtk::SelectionData& data,
530                      guint info, guint time) {
531         const SerializedObjectPointers<boost::shared_ptr<Route> >* sr = 
532                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Route> > *> (data.get_data());
533
534         for (uint32_t i = 0; i < sr->cnt; ++i) {
535                 boost::shared_ptr<Route> r = sr->data[i];
536                 insert_route_list_drag (r, x, y);
537         }
538
539         context->drag_finish (true, false, time);
540 }
541
542 void
543 Editor::maybe_autoscroll (GdkEventMotion* event)
544 {
545         nframes64_t rightmost_frame = leftmost_frame + current_page_frames();
546         nframes64_t frame = drag_info.current_pointer_frame;
547         bool startit = false;
548         double vertical_pos = vertical_adjustment.get_value();
549         double upper = vertical_adjustment.get_upper();
550
551         /* 
552            adjust the event.y to take account of the bounds 
553            of the _trackview_group 
554         */
555
556         double vsx1, vsx2, vsy1, vsy2;
557         _trackview_group->get_bounds (vsx1, vsy1, vsx2, vsy2);
558
559         autoscroll_y = 0;
560         autoscroll_x = 0;
561
562         if ((event->y - vsy1) < vertical_pos) {
563                 autoscroll_y = -1;
564                 startit = true;
565         }
566
567         if ((event->y - vsy1) > (vertical_pos + canvas_height - canvas_timebars_vsize) && vertical_pos <= upper) {
568                 autoscroll_y = 1;
569                 startit = true;
570         }
571
572         if (frame > rightmost_frame) {
573
574                 if (rightmost_frame < max_frames) {
575                         autoscroll_x = 1;
576                         startit = true;
577                 }
578
579         } else if (frame < leftmost_frame) {
580                 if (leftmost_frame > 0) {
581                         autoscroll_x = -1;
582                         startit = true;
583                 }
584
585         }
586
587         if (!allow_vertical_scroll) {
588                 autoscroll_y = 0;
589         }
590
591         if ((autoscroll_x != last_autoscroll_x) || (autoscroll_y != last_autoscroll_y) || (autoscroll_x == 0 && autoscroll_y == 0)) {
592                 stop_canvas_autoscroll ();
593         }
594         
595         if (startit && autoscroll_timeout_tag < 0) {
596                 start_canvas_autoscroll (autoscroll_x, autoscroll_y);
597         }
598
599         last_autoscroll_x = autoscroll_x;
600         last_autoscroll_y = autoscroll_y;
601 }
602
603 void
604 Editor::maybe_autoscroll_horizontally (GdkEventMotion* event)
605 {
606         nframes64_t rightmost_frame = leftmost_frame + current_page_frames();
607         nframes64_t frame = drag_info.current_pointer_frame;
608         bool startit = false;
609
610         autoscroll_y = 0;
611         autoscroll_x = 0;
612
613         if (frame > rightmost_frame) {
614
615                 if (rightmost_frame < max_frames) {
616                         autoscroll_x = 1;
617                         startit = true;
618                 }
619
620         } else if (frame < leftmost_frame) {
621                 if (leftmost_frame > 0) {
622                         autoscroll_x = -1;
623                         startit = true;
624                 }
625
626         }
627
628         if ((autoscroll_x != last_autoscroll_x) || (autoscroll_y != last_autoscroll_y) || (autoscroll_x == 0 && autoscroll_y == 0)) {
629                 stop_canvas_autoscroll ();
630         }
631         
632         if (startit && autoscroll_timeout_tag < 0) {
633                 start_canvas_autoscroll (autoscroll_x, autoscroll_y);
634         }
635
636         last_autoscroll_x = autoscroll_x;
637         last_autoscroll_y = autoscroll_y;
638 }
639
640 gint
641 Editor::_autoscroll_canvas (void *arg)
642 {
643         return ((Editor *) arg)->autoscroll_canvas ();
644 }
645
646 bool
647 Editor::autoscroll_canvas ()
648 {
649         nframes64_t new_frame;
650         nframes64_t limit = max_frames - current_page_frames();
651         GdkEventMotion ev;
652         double new_pixel;
653         double target_pixel;
654
655         if (autoscroll_x_distance != 0) {
656
657                 if (autoscroll_cnt == 50) { /* 0.5 seconds */
658                         
659                         /* after about a while, speed up a bit by changing the timeout interval */
660                         
661                         autoscroll_x_distance = (nframes64_t) floor (current_page_frames()/30.0f);
662                         
663                 } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
664                         
665                         autoscroll_x_distance = (nframes64_t) floor (current_page_frames()/20.0f);
666                         
667                 } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
668                         
669                         /* after about another while, speed up by increasing the shift per callback */
670                         
671                         autoscroll_x_distance =  (nframes64_t) floor (current_page_frames()/10.0f);
672                         
673                 } 
674         }
675
676         if (autoscroll_y_distance != 0) {
677
678                 if (autoscroll_cnt == 50) { /* 0.5 seconds */
679                         
680                         /* after about a while, speed up a bit by changing the timeout interval */
681                         
682                         autoscroll_y_distance = 10;
683                         
684                 } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
685                         
686                         autoscroll_y_distance = 20;
687                         
688                 } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
689                         
690                         /* after about another while, speed up by increasing the shift per callback */
691                         
692                         autoscroll_y_distance =  40;
693                 } 
694         }
695
696         if (autoscroll_x < 0) {
697                 if (leftmost_frame < autoscroll_x_distance) {
698                         new_frame = 0;
699                 } else {
700                         new_frame = leftmost_frame - autoscroll_x_distance;
701                 }
702         } else if (autoscroll_x > 0) {
703                 if (leftmost_frame > limit - autoscroll_x_distance) {
704                         new_frame = limit;
705                 } else {
706                         new_frame = leftmost_frame + autoscroll_x_distance;
707                 }
708         } else {
709                 new_frame = leftmost_frame;
710         }
711
712         double vertical_pos = vertical_adjustment.get_value();
713
714         if (autoscroll_y < 0) {
715
716                 if (vertical_pos < autoscroll_y_distance) {
717                         new_pixel = 0;
718                 } else {
719                         new_pixel = vertical_pos - autoscroll_y_distance;
720                 }
721
722                 target_pixel = drag_info.current_pointer_y - autoscroll_y_distance;
723                 target_pixel = max (target_pixel, 0.0);
724
725         } else if (autoscroll_y > 0) {
726
727                 double top_of_bottom_of_canvas = full_canvas_height - canvas_height;
728
729                 if (vertical_pos > full_canvas_height - autoscroll_y_distance) {
730                         new_pixel = full_canvas_height;
731                 } else {
732                         new_pixel = vertical_pos + autoscroll_y_distance;
733                 }
734
735                 new_pixel = min (top_of_bottom_of_canvas, new_pixel);
736
737                 target_pixel = drag_info.current_pointer_y + autoscroll_y_distance;
738                 
739                 /* don't move to the full canvas height because the item will be invisible
740                    (its top edge will line up with the bottom of the visible canvas.
741                 */
742
743                 target_pixel = min (target_pixel, full_canvas_height - 10);
744                 
745         } else {
746                 target_pixel = drag_info.current_pointer_y;
747                 new_pixel = vertical_pos;
748         }
749
750
751
752         if ((new_frame == 0 || new_frame == limit) && (new_pixel == 0 || new_pixel == DBL_MAX)) {
753                 /* we are done */
754                 return false;
755         }
756
757         if (new_frame != leftmost_frame) {
758                 reset_x_origin (new_frame);
759         }
760
761         vertical_adjustment.set_value (new_pixel);
762
763         /* fake an event. */
764
765         Glib::RefPtr<Gdk::Window> canvas_window = const_cast<Editor*>(this)->track_canvas->get_window();
766         gint x, y;
767         Gdk::ModifierType mask;
768         canvas_window->get_pointer (x, y, mask);
769         ev.type = GDK_MOTION_NOTIFY;
770         ev.state &= Gdk::BUTTON1_MASK;
771         ev.x = x;
772         ev.y = y;
773
774         motion_handler (drag_info.item, (GdkEvent*) &ev, drag_info.item_type, true);
775
776         autoscroll_cnt++;
777
778         if (autoscroll_cnt == 1) {
779
780                 /* connect the timeout so that we get called repeatedly */
781
782                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
783                 return false;
784
785         } 
786
787         return true;
788 }
789
790 void
791 Editor::start_canvas_autoscroll (int dx, int dy)
792 {
793         if (!session || autoscroll_active) {
794                 return;
795         }
796
797         stop_canvas_autoscroll ();
798
799         autoscroll_active = true;
800         autoscroll_x = dx;
801         autoscroll_y = dy;
802         autoscroll_x_distance = (nframes64_t) floor (current_page_frames()/50.0);
803         autoscroll_y_distance = fabs (dy * 5); /* pixels */
804         autoscroll_cnt = 0;
805         
806         /* do it right now, which will start the repeated callbacks */
807
808         autoscroll_canvas ();
809 }
810
811 void
812 Editor::stop_canvas_autoscroll ()
813 {
814
815         if (autoscroll_timeout_tag >= 0) {
816                 g_source_remove (autoscroll_timeout_tag);
817                 autoscroll_timeout_tag = -1;
818         }
819
820         autoscroll_active = false;
821 }
822
823 bool
824 Editor::left_track_canvas (GdkEventCrossing *ev)
825 {
826         set_entered_track (0);
827         set_entered_regionview (0);
828         reset_canvas_action_sensitivity (false);
829         return false;
830 }
831
832 bool
833 Editor::entered_track_canvas (GdkEventCrossing *ev)
834 {
835         reset_canvas_action_sensitivity (true);
836         return FALSE;
837 }
838
839 void
840 Editor::tie_vertical_scrolling ()
841 {
842         scroll_canvas_vertically ();
843
844         /* this will do an immediate redraw */
845
846         controls_layout.get_vadjustment()->set_value (vertical_adjustment.get_value());
847 }
848
849 void
850 Editor::scroll_canvas_horizontally ()
851 {
852         nframes64_t time_origin = (nframes64_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
853
854         if (time_origin != leftmost_frame) {
855                 canvas_scroll_to (time_origin);
856         }
857
858         /* horizontal scrolling only */
859         double x1, x2, y1, y2, x_delta;
860
861         _master_group->get_bounds(x1, y1, x2, y2);
862         x_delta = x1 + horizontal_adjustment.get_value();
863
864         _master_group->move (-x_delta, 0);
865         timebar_group->move (-x_delta, 0);
866         cursor_group->move (-x_delta, 0);
867         update_fixed_rulers ();
868         redisplay_tempo (true);
869 }
870
871 void
872 Editor::scroll_canvas_vertically ()
873 {
874         /* vertical scrolling only */
875         double x1, x2, y1, y2, y_delta;
876
877         _trackview_group->get_bounds(x1, y1, x2, y2);
878         y_delta = y1 + get_trackview_group_vertical_offset ();
879
880         _trackview_group->move (0, -y_delta);
881
882         /* required to keep the controls_layout in lock step with the canvas group */
883         track_canvas->update_now ();
884 }
885
886 void 
887 Editor::canvas_horizontally_scrolled ()
888 {
889         nframes64_t time_origin = (nframes64_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
890
891         if (time_origin != leftmost_frame) {
892                 canvas_scroll_to (time_origin);
893         }
894         redisplay_tempo (true);
895 }
896
897 void
898 Editor::canvas_scroll_to (nframes64_t time_origin)
899 {
900         leftmost_frame = time_origin;
901         nframes64_t rightmost_frame = leftmost_frame + current_page_frames ();
902
903         if (rightmost_frame > last_canvas_frame) {
904                 last_canvas_frame = rightmost_frame;
905                 //reset_scrolling_region ();
906         }
907 }
908
909 void
910 Editor::color_handler()
911 {
912         playhead_cursor->canvas_item.property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_PlayHead.get();
913         verbose_canvas_cursor->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_VerboseCanvasCursor.get();
914         
915         meter_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MeterBar.get();
916         meter_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
917
918         tempo_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TempoBar.get();
919         tempo_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
920
921         marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBar.get();
922         marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
923
924         cd_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CDMarkerBar.get();
925         cd_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
926
927         range_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeMarkerBar.get();
928         range_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
929
930         transport_marker_bar->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportMarkerBar.get();
931         transport_marker_bar->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerBarSeparator.get();
932
933         cd_marker_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
934         cd_marker_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
935
936         range_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
937         range_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragBarRect.get();
938
939         transport_bar_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
940         transport_bar_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportDragRect.get();
941
942         marker_drag_line->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MarkerDragLine.get();
943
944         range_marker_drag_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
945         range_marker_drag_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RangeDragRect.get();
946
947         transport_loop_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
948         transport_loop_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportLoopRect.get();
949
950         transport_punch_range_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
951         transport_punch_range_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TransportPunchRect.get();
952
953         transport_punchin_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
954         transport_punchout_line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_PunchLine.get();
955
956         zoom_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
957         zoom_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ZoomRect.get();
958
959         rubberband_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
960         rubberband_rect->property_fill_color_rgba() = (guint32) ARDOUR_UI::config()->canvasvar_RubberBandRect.get();
961
962         location_marker_color = ARDOUR_UI::config()->canvasvar_LocationMarker.get();
963         location_range_color = ARDOUR_UI::config()->canvasvar_LocationRange.get();
964         location_cd_marker_color = ARDOUR_UI::config()->canvasvar_LocationCDMarker.get();
965         location_loop_color = ARDOUR_UI::config()->canvasvar_LocationLoop.get();
966         location_punch_color = ARDOUR_UI::config()->canvasvar_LocationPunch.get();
967
968         refresh_location_display ();
969         redisplay_tempo (true);
970
971         if (session)
972                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
973 }
974
975
976 void
977 Editor::flush_canvas ()
978 {
979         if (is_mapped()) {
980                 track_canvas->update_now ();
981                 // gdk_window_process_updates (GTK_LAYOUT(track_canvas->gobj())->bin_window, true);
982         }
983 }