remove static Pango::FontDescriptions, they cause glib errors because of initializati...
[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
26 #include "ardour_ui.h"
27 #include "editor.h"
28 #include "waveview.h"
29 #include "simplerect.h"
30 #include "simpleline.h"
31 #include "imageframe.h"
32 #include "waveview_p.h"
33 #include "simplerect_p.h"
34 #include "simpleline_p.h"
35 #include "imageframe_p.h"
36 #include "canvas_impl.h"
37 #include "editing.h"
38 #include "rgb_macros.h"
39 #include "utils.h"
40 #include "time_axis_view.h"
41 #include "audio_time_axis.h"
42
43 #include "i18n.h"
44
45 using namespace std;
46 using namespace sigc;
47 using namespace ARDOUR;
48 using namespace PBD;
49 using namespace Gtk;
50 using namespace Glib;
51 using namespace Gtkmm2ext;
52 using namespace Editing;
53
54 /* XXX this is a hack. it ought to be the maximum value of an nframes_t */
55
56 const double max_canvas_coordinate = (double) JACK_MAX_FRAMES;
57
58 extern "C"
59 {
60
61 GType gnome_canvas_simpleline_get_type(void);
62 GType gnome_canvas_simplerect_get_type(void);
63 GType gnome_canvas_waveview_get_type(void);
64 GType gnome_canvas_imageframe_get_type(void);
65
66 }
67
68 static void ardour_canvas_type_init() 
69 {
70         // Map gtypes to gtkmm wrapper-creation functions:
71         
72         Glib::wrap_register(gnome_canvas_simpleline_get_type(), &Gnome::Canvas::SimpleLine_Class::wrap_new);
73         Glib::wrap_register(gnome_canvas_simplerect_get_type(), &Gnome::Canvas::SimpleRect_Class::wrap_new);
74         Glib::wrap_register(gnome_canvas_waveview_get_type(), &Gnome::Canvas::WaveView_Class::wrap_new);
75         Glib::wrap_register(gnome_canvas_imageframe_get_type(), &Gnome::Canvas::ImageFrame_Class::wrap_new);
76         
77         // Register the gtkmm gtypes:
78
79         (void) Gnome::Canvas::WaveView::get_type();
80         (void) Gnome::Canvas::SimpleLine::get_type();
81         (void) Gnome::Canvas::SimpleRect::get_type();
82         (void) Gnome::Canvas::ImageFrame::get_type();
83
84
85 void
86 Editor::initialize_canvas ()
87 {
88         ArdourCanvas::init ();
89         ardour_canvas_type_init ();
90
91         /* don't try to center the canvas */
92
93         track_canvas.set_center_scroll_region (false);
94         track_canvas.set_dither         (Gdk::RGB_DITHER_NONE);
95
96         /* need to handle 4 specific types of events as catch-alls */
97
98         track_canvas.signal_scroll_event().connect (mem_fun (*this, &Editor::track_canvas_scroll_event));
99         track_canvas.signal_motion_notify_event().connect (mem_fun (*this, &Editor::track_canvas_motion_notify_event));
100         track_canvas.signal_button_press_event().connect (mem_fun (*this, &Editor::track_canvas_button_press_event));
101         track_canvas.signal_button_release_event().connect (mem_fun (*this, &Editor::track_canvas_button_release_event));
102
103         track_canvas.set_name ("EditorMainCanvas");
104         track_canvas.add_events (Gdk::POINTER_MOTION_HINT_MASK|Gdk::SCROLL_MASK);
105         track_canvas.signal_leave_notify_event().connect (mem_fun(*this, &Editor::left_track_canvas));
106         track_canvas.set_flags (CAN_FOCUS);
107
108         /* set up drag-n-drop */
109
110         vector<TargetEntry> target_table;
111         
112         // Drag-N-Drop from the region list can generate this target
113         target_table.push_back (TargetEntry ("regions"));
114
115         target_table.push_back (TargetEntry ("text/plain"));
116         target_table.push_back (TargetEntry ("text/uri-list"));
117         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
118
119         track_canvas.drag_dest_set (target_table);
120         track_canvas.signal_drag_data_received().connect (mem_fun(*this, &Editor::track_canvas_drag_data_received));
121
122         /* stuff for the verbose canvas cursor */
123
124         Pango::FontDescription* font = get_font_for_style (N_("VerboseCanvasCursor"));
125
126         verbose_canvas_cursor = new ArdourCanvas::Text (*track_canvas.root());
127         verbose_canvas_cursor->property_font_desc() = *font;
128         verbose_canvas_cursor->property_anchor() = ANCHOR_NW;
129         verbose_canvas_cursor->property_fill_color_rgba() = color_map[cVerboseCanvasCursor];
130
131         delete font;
132         
133         verbose_cursor_visible = false;
134         
135         /* a group to hold time (measure) lines */
136         
137         time_line_group = new ArdourCanvas::Group (*track_canvas.root(), 0.0, 0.0);
138         cursor_group = new ArdourCanvas::Group (*track_canvas.root(), 0.0, 0.0);
139
140         time_canvas.set_name ("EditorTimeCanvas");
141         time_canvas.add_events (Gdk::POINTER_MOTION_HINT_MASK);
142         time_canvas.set_flags (CAN_FOCUS);
143         time_canvas.set_center_scroll_region (false);
144         time_canvas.set_dither          (Gdk::RGB_DITHER_NONE);
145         
146         meter_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, 0.0);
147         tempo_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height);
148         marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 2.0);
149         range_marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 3.0);
150         transport_marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 4.0);
151         
152         tempo_bar = new ArdourCanvas::SimpleRect (*tempo_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
153         tempo_bar->property_fill_color_rgba() = color_map[cTempoBar];
154         tempo_bar->property_outline_pixels() = 0;
155         
156         meter_bar = new ArdourCanvas::SimpleRect (*meter_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
157         meter_bar->property_fill_color_rgba() = color_map[cMeterBar];
158         meter_bar->property_outline_pixels() = 0;
159         
160         marker_bar = new ArdourCanvas::SimpleRect (*marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
161         marker_bar->property_fill_color_rgba() = color_map[cMarkerBar];
162         marker_bar->property_outline_pixels() = 0;
163         
164         range_marker_bar = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
165         range_marker_bar->property_fill_color_rgba() = color_map[cRangeMarkerBar];
166         range_marker_bar->property_outline_pixels() = 0;
167         
168         transport_marker_bar = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
169         transport_marker_bar->property_fill_color_rgba() = color_map[cTransportMarkerBar];
170         transport_marker_bar->property_outline_pixels() = 0;
171         
172         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
173         range_bar_drag_rect->property_fill_color_rgba() = color_map[cRangeDragBarRectFill];
174         range_bar_drag_rect->property_outline_color_rgba() = color_map[cRangeDragBarRect];
175         range_bar_drag_rect->property_outline_pixels() = 0;
176         range_bar_drag_rect->hide ();
177         
178         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height-1.0);
179         transport_bar_drag_rect ->property_fill_color_rgba() = color_map[cTransportDragRectFill];
180         transport_bar_drag_rect->property_outline_color_rgba() = color_map[cTransportDragRect];
181         transport_bar_drag_rect->property_outline_pixels() = 0;
182         transport_bar_drag_rect->hide ();
183         
184         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
185         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
186
187         marker_drag_line = new ArdourCanvas::Line (*track_canvas.root());
188         marker_drag_line->property_width_pixels() = 1;
189         marker_drag_line->property_fill_color_rgba() = color_map[cMarkerDragLine];
190         marker_drag_line->property_points() = marker_drag_line_points;
191         marker_drag_line->hide();
192
193         range_marker_drag_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
194         range_marker_drag_rect->property_fill_color_rgba() = color_map[cRangeDragRectFill];
195         range_marker_drag_rect->property_outline_color_rgba() = color_map[cRangeDragRect];
196         range_marker_drag_rect->hide ();
197         
198         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
199         transport_loop_range_rect->property_fill_color_rgba() = color_map[cTransportLoopRectFill];
200         transport_loop_range_rect->property_outline_color_rgba() = color_map[cTransportLoopRect];
201         transport_loop_range_rect->property_outline_pixels() = 1;
202         transport_loop_range_rect->hide();
203
204         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
205         transport_punch_range_rect->property_fill_color_rgba() = color_map[cTransportPunchRectFill];
206         transport_punch_range_rect->property_outline_color_rgba() = color_map[cTransportPunchRect];
207         transport_punch_range_rect->property_outline_pixels() = 0;
208         transport_punch_range_rect->hide();
209         
210         transport_loop_range_rect->lower_to_bottom (); // loop on the bottom
211
212         transport_punchin_line = new ArdourCanvas::SimpleLine (*time_line_group);
213         transport_punchin_line->property_x1() = 0.0;
214         transport_punchin_line->property_y1() = 0.0;
215         transport_punchin_line->property_x2() = 0.0;
216         transport_punchin_line->property_y2() = 0.0;
217         transport_punchin_line->property_color_rgba() = color_map[cPunchInLine];
218         transport_punchin_line->hide ();
219         
220         transport_punchout_line  = new ArdourCanvas::SimpleLine (*time_line_group);
221         transport_punchout_line->property_x1() = 0.0;
222         transport_punchout_line->property_y1() = 0.0;
223         transport_punchout_line->property_x2() = 0.0;
224         transport_punchout_line->property_y2() = 0.0;
225         transport_punchout_line->property_color_rgba() = color_map[cPunchOutLine];
226         transport_punchout_line->hide();
227         
228         // used to show zoom mode active zooming
229         zoom_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
230         zoom_rect->property_fill_color_rgba() = color_map[cZoomRectFill];
231         zoom_rect->property_outline_color_rgba() = color_map[cZoomRect];
232         zoom_rect->property_outline_pixels() = 1;
233         zoom_rect->hide();
234         
235         zoom_rect->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
236         
237         // used as rubberband rect
238         rubberband_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
239         rubberband_rect->property_outline_color_rgba() = color_map[cRubberBandRect];
240         rubberband_rect->property_fill_color_rgba() = (guint32) color_map[cRubberBandRectFill];
241         rubberband_rect->property_outline_pixels() = 1;
242         rubberband_rect->hide();
243         
244         tempo_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
245         meter_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
246         marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
247         range_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
248         transport_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
249         
250         /* separator lines */
251         
252         tempo_line = new ArdourCanvas::SimpleLine (*tempo_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
253         tempo_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
254
255         meter_line = new ArdourCanvas::SimpleLine (*meter_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
256         meter_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
257
258         marker_line = new ArdourCanvas::SimpleLine (*marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
259         marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
260         
261         range_marker_line = new ArdourCanvas::SimpleLine (*range_marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
262         range_marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
263
264         transport_marker_line = new ArdourCanvas::SimpleLine (*transport_marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
265         transport_marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
266
267         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_loop_range_view), false));
268         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_punch_range_view), false));
269         
270         double time_height = timebar_height * 5;
271         double time_width = FLT_MAX/frames_per_unit;
272         time_canvas.set_scroll_region(0.0, 0.0, time_width, time_height);
273         
274         if (!color_map[cEditCursor]) {
275                  warning << _("edit cursor color not defined, check your ardour.colors file!") << endmsg;
276                 color_map[cEditCursor] = RGBA_TO_UINT (30,30,30,255);
277         }
278
279         if (!color_map[cPlayHead]) {
280                 warning << _("playhead color not defined, check your ardour.colors file!") << endmsg;
281                 color_map[cPlayHead] = RGBA_TO_UINT (0,0,0,255);
282         }
283
284         edit_cursor = new Cursor (*this, &Editor::canvas_edit_cursor_event);
285         edit_cursor->canvas_item.property_fill_color_rgba() = color_map[cEditCursor];
286         playhead_cursor = new Cursor (*this, &Editor::canvas_playhead_cursor_event);
287         playhead_cursor->canvas_item.property_fill_color_rgba() = color_map[cPlayHead];
288
289         initial_ruler_update_required = true;
290         track_canvas.signal_size_allocate().connect (mem_fun(*this, &Editor::track_canvas_allocate));
291
292 }
293
294 void
295 Editor::track_canvas_allocate (Gtk::Allocation alloc)
296 {
297         canvas_allocation = alloc;
298
299         if (!initial_ruler_update_required) {
300                 if (!canvas_idle_queued) {
301                         /* call this first so that we do stuff before any pending redraw */
302                         Glib::signal_idle().connect (mem_fun (*this, &Editor::track_canvas_size_allocated), false);
303                         canvas_idle_queued = true;
304                 }
305                 return;
306         } 
307
308         initial_ruler_update_required = false;
309         
310         track_canvas_size_allocated ();
311 }
312
313 bool
314 Editor::track_canvas_size_allocated ()
315 {
316         if (canvas_idle_queued) {
317                 canvas_idle_queued = false;
318         }
319
320         canvas_width = canvas_allocation.get_width();
321         canvas_height = canvas_allocation.get_height();
322
323         full_canvas_height = canvas_height;
324
325         if (session) {
326                 TrackViewList::iterator i;
327                 double height = 0;
328
329                 for (i = track_views.begin(); i != track_views.end(); ++i) {
330                         if ((*i)->control_parent) {
331                                 height += (*i)->effective_height;
332                         }
333                 }
334                 
335                 full_canvas_height = height;
336         }
337
338         zoom_range_clock.set ((nframes_t) floor ((canvas_width * frames_per_unit)));
339         edit_cursor->set_position (edit_cursor->current_frame);
340         playhead_cursor->set_position (playhead_cursor->current_frame);
341
342         reset_hscrollbar_stepping ();
343         reset_scrolling_region ();
344
345         if (edit_cursor) edit_cursor->set_length (canvas_height);
346         if (playhead_cursor) playhead_cursor->set_length (canvas_height);
347
348         if (marker_drag_line) {
349                 marker_drag_line_points.back().set_y(canvas_height);
350                 marker_drag_line->property_points() = marker_drag_line_points;
351         }
352
353         if (range_marker_drag_rect) {
354                 range_marker_drag_rect->property_y1() = 0.0;
355                 range_marker_drag_rect->property_y2() = canvas_height;
356         }
357
358         if (transport_loop_range_rect) {
359                 transport_loop_range_rect->property_y1() = 0.0;
360                 transport_loop_range_rect->property_y2() = canvas_height;
361         }
362
363         if (transport_punch_range_rect) {
364                 transport_punch_range_rect->property_y1() = 0.0;
365                 transport_punch_range_rect->property_y2() = canvas_height;
366         }
367
368         if (transport_punchin_line) {
369                 transport_punchin_line->property_y1() = 0.0;
370                 transport_punchin_line->property_y2() = canvas_height;
371         }
372
373         if (transport_punchout_line) {
374                 transport_punchout_line->property_y1() = 0.0;
375                 transport_punchout_line->property_y2() = canvas_height;
376         }
377                 
378         update_fixed_rulers();
379         redisplay_tempo (true);
380         
381         Resized (); /* EMIT_SIGNAL */
382
383         return false;
384 }
385
386 void
387 Editor::reset_scrolling_region (Gtk::Allocation* alloc)
388 {
389         TreeModel::Children rows = route_display_model->children();
390         TreeModel::Children::iterator i;
391         double pos;
392
393         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
394                 TimeAxisView *tv = (*i)[route_display_columns.tv];
395                 if (tv != 0 && !tv->hidden()) {
396                         pos += tv->effective_height;
397                 }
398         }
399
400         double last_canvas_unit =  last_canvas_frame / frames_per_unit;
401
402         track_canvas.set_scroll_region (0.0, 0.0, max (last_canvas_unit, canvas_width), pos);
403
404         // XXX what is the correct height value for the time canvas ? this overstates it
405         time_canvas.set_scroll_region ( 0.0, 0.0, max (last_canvas_unit, canvas_width), canvas_height);
406
407         controls_layout.queue_resize();
408 }
409
410 void
411 Editor::controls_layout_size_request (Requisition* req)
412 {
413         TreeModel::Children rows = route_display_model->children();
414         TreeModel::Children::iterator i;
415         double pos;
416
417         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
418                 TimeAxisView *tv = (*i)[route_display_columns.tv];
419                 if (tv != 0) {
420                         pos += tv->effective_height;
421                 }
422         }
423
424         RefPtr<Gdk::Screen> screen = get_screen();
425
426         if (!screen) {
427                 screen = Gdk::Screen::get_default();
428         }
429
430         /* never let the width of the controls area shrink horizontally */
431
432         edit_controls_vbox.check_resize();
433
434         req->width = max (edit_controls_vbox.get_width(),  controls_layout.get_width());
435         
436         /* don't get too big. the fudge factors here are just guesses */
437         
438         req->width = min (req->width, screen->get_width() - 300);
439         req->height = min ((gint) pos, (screen->get_height() - 400));
440
441         /* this one is important: it determines how big the layout thinks it really is, as 
442            opposed to what it displays on the screen
443         */
444
445         controls_layout.set_size (req->width, (gint) pos);
446 }
447
448 bool
449 Editor::track_canvas_map_handler (GdkEventAny* ev)
450 {
451         track_canvas.get_window()->set_cursor (*current_canvas_cursor);
452         return false;
453 }
454
455 bool
456 Editor::time_canvas_map_handler (GdkEventAny* ev)
457 {
458         time_canvas.get_window()->set_cursor (*timebar_cursor);
459         return false;
460 }
461
462 void  
463 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
464                                          int x, int y, 
465                                          const SelectionData& data,
466                                          guint info, guint time)
467 {
468         if (data.get_target() == "regions") {
469                 drop_regions (context, x, y, data, info, time);
470         } else {
471                 drop_paths (context, x, y, data, info, time);
472         }
473 }
474
475 void
476 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
477                     int x, int y, 
478                     const SelectionData& data,
479                     guint info, guint time)
480 {
481         TimeAxisView* tvp;
482         AudioTimeAxisView* tv;
483         double cy;
484         vector<ustring> paths;
485         string spath;
486         GdkEvent ev;
487         nframes_t frame;
488
489         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
490                 goto out;
491         }
492
493         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
494          */
495
496         double wx;
497         double wy;
498
499         track_canvas.window_to_world (x, y, wx, wy);
500         wx += horizontal_adjustment.get_value();
501         wy += vertical_adjustment.get_value();
502         
503         ev.type = GDK_BUTTON_RELEASE;
504         ev.button.x = wx;
505         ev.button.y = wy;
506
507         frame = event_frame (&ev, 0, &cy);
508
509         snap_to (frame);
510
511         if ((tvp = trackview_by_y_position (cy)) == 0) {
512
513                 /* drop onto canvas background: create new tracks */
514
515                 nframes_t pos = 0;
516                 do_embed (paths, false, ImportAsTrack, 0, pos, false);
517                 
518         } else if ((tv = dynamic_cast<AudioTimeAxisView*>(tvp)) != 0) {
519
520                 /* check that its an audio track, not a bus */
521                 
522                 if (tv->get_diskstream()) {
523                         do_embed (paths, false, ImportToTrack, tv->audio_track(), frame, true);
524                 }
525         }
526
527   out:
528         context->drag_finish (true, false, time);
529 }
530
531 void
532 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
533                       int x, int y, 
534                       const SelectionData& data,
535                       guint info, guint time)
536 {
537         const SerializedObjectPointers<boost::shared_ptr<Region> >* sr = 
538                 reinterpret_cast<const SerializedObjectPointers<boost::shared_ptr<Region> > *> (data.get_data());
539
540         for (uint32_t i = 0; i < sr->cnt; ++i) {
541
542                 boost::shared_ptr<Region> r = sr->data[i];
543                 boost::shared_ptr<AudioRegion> ar;
544
545                 if ((ar = boost::dynamic_pointer_cast<AudioRegion>(r)) != 0) {
546                         insert_region_list_drag (ar, x, y);
547                 }
548         }
549
550         context->drag_finish (true, false, time);
551 }
552
553 void
554 Editor::maybe_autoscroll (GdkEvent* event)
555 {
556         nframes_t rightmost_frame = leftmost_frame + current_page_frames();
557         nframes_t frame = drag_info.current_pointer_frame;
558         bool startit = false;
559
560         static int last_autoscroll_direction = 0;
561
562         if (frame > rightmost_frame) {
563
564                 if (rightmost_frame < max_frames) {
565                         autoscroll_direction = 1;
566                         startit = true;
567                 }
568
569         } else if (frame < leftmost_frame) {
570
571                 if (leftmost_frame > 0) {
572                         autoscroll_direction = -1;
573                         startit = true;
574                 }
575
576         } else {
577
578                 if (drag_info.last_pointer_frame > drag_info.current_pointer_frame) {
579                         autoscroll_direction = -1;
580                 } else {
581                         autoscroll_direction = 1;
582                 }
583         }
584
585
586         if ((autoscroll_direction != last_autoscroll_direction) || (leftmost_frame < frame < rightmost_frame)) {
587                 stop_canvas_autoscroll ();
588         }
589         
590         if (startit && autoscroll_timeout_tag < 0) {
591                 start_canvas_autoscroll (autoscroll_direction);
592         }
593
594         last_autoscroll_direction = autoscroll_direction;
595 }
596
597 gint
598 Editor::_autoscroll_canvas (void *arg)
599 {
600         return ((Editor *) arg)->autoscroll_canvas ();
601 }
602
603 bool
604 Editor::autoscroll_canvas ()
605 {
606         nframes_t new_frame;
607         nframes_t limit = max_frames - current_page_frames();
608         GdkEventMotion ev;
609         nframes_t target_frame;
610
611         if (autoscroll_direction < 0) {
612                 if (leftmost_frame < autoscroll_distance) {
613                         new_frame = 0;
614                 } else {
615                         new_frame = leftmost_frame - autoscroll_distance;
616                 }
617                 target_frame = drag_info.current_pointer_frame - autoscroll_distance;
618         } else {
619                 if (leftmost_frame > limit - autoscroll_distance) {
620                         new_frame = limit;
621                 } else {
622                         new_frame = leftmost_frame + autoscroll_distance;
623                 }
624                 target_frame = drag_info.current_pointer_frame + autoscroll_distance;
625         }
626
627         /* now fake a motion event to get the object that is being dragged to move too */
628
629         ev.type = GDK_MOTION_NOTIFY;
630         ev.state &= Gdk::BUTTON1_MASK;
631         ev.x = frame_to_unit (target_frame);
632         ev.y = drag_info.current_pointer_y;
633         motion_handler (drag_info.item, (GdkEvent*) &ev, drag_info.item_type, true);
634
635         if (new_frame == 0 || new_frame == limit) {
636                 /* we are done */
637                 return false;
638         }
639
640         autoscroll_cnt++;
641
642         if (autoscroll_cnt == 1) {
643
644                 /* connect the timeout so that we get called repeatedly */
645
646                 autoscroll_timeout_tag = g_idle_add ( _autoscroll_canvas, this);
647                 return false;
648
649         } 
650
651         if (new_frame != leftmost_frame) {
652                 reset_x_origin (new_frame);
653         }
654
655         if (autoscroll_cnt == 50) { /* 0.5 seconds */
656                 
657                 /* after about a while, speed up a bit by changing the timeout interval */
658
659                 autoscroll_distance = (nframes_t) floor (current_page_frames()/30.0f);
660                 
661         } else if (autoscroll_cnt == 150) { /* 1.0 seconds */
662
663                 autoscroll_distance = (nframes_t) floor (current_page_frames()/20.0f);
664
665         } else if (autoscroll_cnt == 300) { /* 1.5 seconds */
666
667                 /* after about another while, speed up by increasing the shift per callback */
668
669                 autoscroll_distance =  (nframes_t) floor (current_page_frames()/10.0f);
670
671         } 
672
673         return true;
674 }
675
676 void
677 Editor::start_canvas_autoscroll (int dir)
678 {
679         if (!session || autoscroll_active) {
680                 return;
681         }
682
683         stop_canvas_autoscroll ();
684
685         autoscroll_active = true;
686         autoscroll_direction = dir;
687         autoscroll_distance = (nframes_t) floor (current_page_frames()/50.0);
688         autoscroll_cnt = 0;
689         
690         /* do it right now, which will start the repeated callbacks */
691
692         autoscroll_canvas ();
693 }
694
695 void
696 Editor::stop_canvas_autoscroll ()
697 {
698         if (autoscroll_timeout_tag >= 0) {
699                 g_source_remove (autoscroll_timeout_tag);
700                 autoscroll_timeout_tag = -1;
701         }
702
703         autoscroll_active = false;
704 }
705
706 gint
707 Editor::left_track_canvas (GdkEventCrossing *ev)
708 {
709         set_entered_track (0);
710         set_entered_regionview (0);
711         return FALSE;
712 }
713
714
715 void 
716 Editor::canvas_horizontally_scrolled ()
717 {
718         /* this is the core function that controls horizontal scrolling of the canvas. it is called
719            whenever the horizontal_adjustment emits its "value_changed" signal. it typically executes in an
720            idle handler, which is important because tempo_map_changed() should issue redraws immediately
721            and not defer them to an idle handler.
722         */
723
724         leftmost_frame = (nframes_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
725         nframes_t rightmost_frame = leftmost_frame + current_page_frames ();
726         
727         if (rightmost_frame > last_canvas_frame) {
728                 last_canvas_frame = rightmost_frame;
729                 reset_scrolling_region ();
730         }
731         
732         update_fixed_rulers ();
733
734         redisplay_tempo (!_dragging_hscrollbar);
735 }
736