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