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