098090a693c4d419f0ebfbf17b26ebd76fef2981
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / trackcanvas.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "apatchgui.inc"
23 #include "asset.h"
24 #include "autoconf.h"
25 #include "automation.h"
26 #include "bcsignals.h"
27 #include "bctimer.h"
28 #include "clip.h"
29 #include "bccolors.h"
30 #include "cache.h"
31 #include "canvas.h"
32 #include "cplayback.h"
33 #include "cursors.h"
34 #include "cwindowgui.h"
35 #include "cwindow.h"
36 #include "edithandles.h"
37 #include "editpopup.h"
38 #include "edits.h"
39 #include "edl.h"
40 #include "edlsession.h"
41 #include "floatauto.h"
42 #include "floatautos.h"
43 #include "gwindowgui.h"
44 #include "indexstate.h"
45 #include "intauto.h"
46 #include "intautos.h"
47 #include "keyframe.h"
48 #include "keyframepopup.h"
49 #include "keyframes.h"
50 #include "keys.h"
51 #include "labels.h"
52 #include "localsession.h"
53 #include "mainclock.h"
54 #include "maincursor.h"
55 #include "mainsession.h"
56 #include "mainundo.h"
57 #include "maskautos.h"
58 #include "mbuttons.h"
59 #include "mtimebar.h"
60 #include "mwindowgui.h"
61 #include "mwindow.h"
62 #include "panautos.h"
63 #include "patchbay.h"
64 #include "playbackengine.h"
65 #include "playtransport.h"
66 #include "plugin.h"
67 #include "pluginpopup.h"
68 #include "pluginserver.h"
69 #include "pluginset.h"
70 #include "plugintoggles.h"
71 #include "preferences.h"
72 #include "renderengine.h"
73 #include "resourcepixmap.h"
74 #include "resourcethread.h"
75 #include "swindow.h"
76 #include "theme.h"
77 #include "trackcanvas.h"
78 #include "tracking.h"
79 #include "trackpopup.h"
80 #include "tracks.h"
81 #include "transition.h"
82 #include "transitionhandles.h"
83 #include "transitionpopup.h"
84 #include "transportque.h"
85 #include "vframe.h"
86 #include "vpatchgui.inc"
87 #include "vrender.h"
88 #include "zoombar.h"
89
90 #include <string.h>
91
92 //#define PIXMAP_AGE -5
93 #define PIXMAP_AGE -32
94
95 TrackCanvas::TrackCanvas(MWindow *mwindow,
96         TimelinePane *pane,
97         int x,
98         int y,
99         int w,
100         int h)
101  : BC_SubWindow(x,
102         y,
103         w,
104         h)
105 {
106         this->mwindow = mwindow;
107         this->gui = mwindow->gui;
108         this->pane = pane;
109
110         selection_midpoint = 0;
111         drag_scroll = 0;
112         active = 0;
113         temp_picon = 0;
114         resource_timer = new Timer;
115         render_timer = new Timer;
116         hourglass_enabled = 0;
117         timebar_position = -1;
118         snapped = 0;
119 }
120
121 TrackCanvas::~TrackCanvas()
122 {
123 //      delete transition_handles;
124         delete edit_handles;
125         delete keyframe_pixmap;
126         delete camerakeyframe_pixmap;
127         delete modekeyframe_pixmap;
128         delete pankeyframe_pixmap;
129         delete projectorkeyframe_pixmap;
130         delete maskkeyframe_pixmap;
131         delete background_pixmap;
132         if(temp_picon) delete temp_picon;
133         delete render_timer;
134         delete resource_timer;
135 }
136
137 void TrackCanvas::create_objects()
138 {
139         background_pixmap = new BC_Pixmap(this, get_w(), get_h());
140 //      transition_handles = new TransitionHandles(mwindow, this);
141         edit_handles = new EditHandles(mwindow, this);
142         keyframe_pixmap = new BC_Pixmap(this, mwindow->theme->keyframe_data, PIXMAP_ALPHA);
143         camerakeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->camerakeyframe_data, PIXMAP_ALPHA);
144         modekeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->modekeyframe_data, PIXMAP_ALPHA);
145         pankeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->pankeyframe_data, PIXMAP_ALPHA);
146         projectorkeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->projectorkeyframe_data, PIXMAP_ALPHA);
147         maskkeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->maskkeyframe_data, PIXMAP_ALPHA);
148         draw(NORMAL_DRAW, 1);
149         update_cursor(0);
150         flash(0);
151 }
152
153 void TrackCanvas::resize_event()
154 {
155 //printf("TrackCanvas::resize_event 1\n");
156         draw(NORMAL_DRAW, 0);
157         flash(0);
158 //printf("TrackCanvas::resize_event 2\n");
159 }
160
161 int TrackCanvas::keypress_event()
162 {
163         return 0;
164 }
165
166 int TrackCanvas::cursor_leave_event()
167 {
168 // Because drag motion calls get_cursor_over_window we can be sure that
169 // all highlights get deleted now.
170 // This ended up blocking keyboard input from the drag operations.
171         if( timebar_position >= 0 )
172         {
173                 timebar_position = -1;
174                 if( pane->timebar )
175                         pane->timebar->update(1);
176         }
177
178         return 0;
179 //      return drag_motion();
180 }
181
182 int TrackCanvas::drag_motion_event()
183 {
184         return gui->drag_motion();
185 }
186
187 int TrackCanvas::drag_motion(
188                 Track **over_track, Edit **over_edit,
189                 PluginSet **over_pluginset, Plugin **over_plugin)
190 {
191         int cursor_x = get_relative_cursor_x();
192         int cursor_y = get_relative_cursor_y();
193         if( get_cursor_over_window() ) {
194                 drag_cursor_motion(cursor_x, cursor_y,
195                         over_track, over_edit, over_pluginset, over_plugin);
196         }
197         if( over_track && !*over_track )
198                 *over_track = pane->over_patchbay();
199         return 0;
200 }
201
202 int TrackCanvas::drag_cursor_motion(int cursor_x, int cursor_y,
203                 Track **over_track, Edit **over_edit,
204                 PluginSet **over_pluginset, Plugin **over_plugin)
205 {
206         if( cursor_x >= 0 && cursor_y >= 0 &&
207             cursor_x < get_w() && cursor_y < get_h() )
208         {
209 //printf("TrackCanvas::drag_motion %d %d\n", __LINE__, pane->number);
210 // Find the edit and track the cursor is over
211                 for(Track *track = mwindow->edl->tracks->first; track; track = track->next)
212                 {
213                         if( track->is_hidden() ) continue;
214                         int64_t track_x, track_y, track_w, track_h;
215                         track_dimensions(track, track_x, track_y, track_w, track_h);
216
217                         if(cursor_y >= track_y &&
218                                 cursor_y < track_y + track_h)
219                         {
220                                 *over_track = track;
221                                 for(Edit *edit = track->edits->first; edit; edit = edit->next)
222                                 {
223                                         int64_t edit_x, edit_y, edit_w, edit_h;
224                                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
225                                         if( cursor_y < edit_y || cursor_y >= edit_y + edit_h ) continue;
226                                         if( cursor_x >= edit_x && cursor_x < edit_x + edit_w ) {
227                                                 *over_edit = edit;
228                                                 break;
229                                         }
230                                         if( edit != track->edits->last ) continue;
231                                         if( mwindow->session->current_operation != DRAG_ATRANSITION &&
232                                             mwindow->session->current_operation != DRAG_VTRANSITION ) continue;
233                                         if( !edit->silence() ) {
234                                                 // add silence to allow drag transition past last edit
235                                                 //  will be deleted by Edits::optimize if not used
236                                                 double length = mwindow->edl->session->default_transition_length;
237                                                 int64_t start = edit->startproject+edit->length;
238                                                 int64_t units = track->to_units(length, 1);
239                                                 track->edits->create_silence(start, start+units);
240                                                 continue;
241                                         }
242                                         if( cursor_x >= edit_x ) { *over_edit = edit; break; }
243                                 }
244
245                                 for(int i = 0; i < track->plugin_set.total; i++)
246                                 {
247                                         PluginSet *pluginset = track->plugin_set.values[i];
248                                         for(Plugin *plugin = (Plugin*)pluginset->first;
249                                                 plugin;
250                                                 plugin = (Plugin*)plugin->next)
251                                         {
252                                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
253                                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
254
255                                                 if(cursor_y >= plugin_y &&
256                                                         cursor_y < plugin_y + plugin_h)
257                                                 {
258                                                         *over_pluginset = plugin->plugin_set;
259
260                                                         if(cursor_x >= plugin_x &&
261                                                                 cursor_x < plugin_x + plugin_w)
262                                                         {
263                                                                 *over_plugin = plugin;
264                                                                 break;
265                                                         }
266                                                 }
267                                         }
268                                 }
269                                 break;
270                         }
271                 }
272         }
273
274         return 0;
275 }
276
277
278 int TrackCanvas::drag_stop_event()
279 {
280         int result = gui->drag_stop();
281         if( !result && mwindow->session->current_operation ) {
282                 mwindow->session->current_operation = NO_OPERATION;
283                 drag_scroll = 0;
284         }
285         return result;
286 }
287
288 int TrackCanvas::drag_stop(int *redraw)
289 {
290         int result = 0;
291         int insertion  = 0;
292         int over_window = 0;
293         int cursor_x = -1, cursor_y = -1;
294
295         if( get_cursor_over_window() ) {
296                 if( (cursor_x = get_relative_cursor_x()) >= 0 && cursor_x < get_w() &&
297                     (cursor_y = get_relative_cursor_y()) >= 0 && cursor_y < get_h() )
298                         over_window = 1;
299                 else {
300                         Track *track = pane->over_patchbay();
301                         if( track && mwindow->session->track_highlighted == track )
302                                 over_window = 1;
303                 }
304         }
305
306         if( over_window ) {
307                 switch(mwindow->session->current_operation) {
308                 case DRAG_VTRANSITION:
309                 case DRAG_ATRANSITION:
310                         if(mwindow->session->edit_highlighted) {
311                                 if( (mwindow->session->current_operation == DRAG_ATRANSITION &&
312                                      mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
313                                     (mwindow->session->current_operation == DRAG_VTRANSITION &&
314                                      mwindow->session->track_highlighted->data_type == TRACK_VIDEO) ) {
315                                         mwindow->session->current_operation = NO_OPERATION;
316                                         mwindow->paste_transition();
317                                         result = 1;
318                                 }
319                         }
320                         *redraw = 1;
321                         break;
322
323 // Behavior for dragged plugins is limited by the fact that a shared plugin
324 // can only refer to a standalone plugin that exists in the same position in
325 // time.  Dragging a plugin from one point in time to another can't produce
326 // a shared plugin to the original plugin.  In this case we relocate the
327 // plugin instead of sharing it.
328                 case DRAG_AEFFECT_COPY:
329                 case DRAG_VEFFECT_COPY:
330                         if( mwindow->session->track_highlighted &&
331                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
332                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
333                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
334                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) ) {
335                                 mwindow->session->current_operation = NO_OPERATION;
336
337                                 int64_t drop_position = -1;
338                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
339                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
340                                 Track *hi_track = mwindow->session->track_highlighted;
341
342 // Insert shared plugin in source
343 // Move source to different location
344                                 if( hi_plugin_set && hi_plugin_set->track == drag_plugin->track ) {
345 //printf("TrackCanvas::drag_stop 6\n");
346                                         drop_position = drop_plugin_position(hi_plugin_set, drag_plugin);
347                                         if( drop_position < 0 ) {
348                                                 result = 1;
349                                                 break;          // Do not do anything
350                                         }
351
352                                         Track *track = mwindow->session->track_highlighted;
353                                         drop_position = track->frame_align(drop_position, 0);
354                                         mwindow->move_effect(drag_plugin, hi_plugin_set, drop_position);
355                                         result = 1;
356                                 }
357                                 else if( hi_track ) {
358 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
359                                         Edit *hi_edit = mwindow->session->edit_highlighted;
360                                         drop_position = hi_edit ? hi_edit->startproject : 0;
361                                         if( drop_position < 0 ) {
362                                                 result = 1;
363                                                 break;          // Do not do anything
364                                         }
365
366                                         Track *track = mwindow->session->track_highlighted;
367                                         drop_position = track->frame_align(drop_position, 0);
368                                         mwindow->move_effect(drag_plugin, hi_track, drop_position);
369                                         result = 1;
370                                 }
371                         }
372                         break;
373
374                 case DRAG_AEFFECT:
375                 case DRAG_VEFFECT:
376                         if( mwindow->session->track_highlighted &&
377                             ((mwindow->session->current_operation == DRAG_AEFFECT &&
378                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
379                              (mwindow->session->current_operation == DRAG_VEFFECT &&
380                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) ) {
381 // Drop all the effects
382                                 PluginSet *plugin_set = mwindow->session->pluginset_highlighted;
383                                 Track *track = mwindow->session->track_highlighted;
384                                 double start = 0;
385                                 double length = track->get_length();
386
387                                 if(mwindow->session->plugin_highlighted) {
388                                         start = track->from_units(mwindow->session->plugin_highlighted->startproject);
389                                         length = track->from_units(mwindow->session->plugin_highlighted->length);
390                                         if(length <= 0) length = track->get_length();
391                                 }
392                                 else if(mwindow->session->pluginset_highlighted) {
393                                                 start = track->from_units(plugin_set->length());
394                                         length = track->get_length() - start;
395                                         if(length <= 0) length = track->get_length();
396                                 }
397                                 else if(mwindow->edl->local_session->get_selectionend() >
398                                         mwindow->edl->local_session->get_selectionstart()) {
399                                         start = mwindow->edl->local_session->get_selectionstart();
400                                         length = mwindow->edl->local_session->get_selectionend() -
401                                                 mwindow->edl->local_session->get_selectionstart();
402                                 }
403 // Move to a point between two edits
404                                 else if(mwindow->session->edit_highlighted) {
405                                         start = mwindow->session->track_highlighted->from_units(
406                                                 mwindow->session->edit_highlighted->startproject);
407                                         length = mwindow->session->track_highlighted->from_units(
408                                                 mwindow->session->edit_highlighted->length);
409                                 }
410                                 start = mwindow->edl->align_to_frame(start, 0);
411                                 mwindow->insert_effects_canvas(track, start, length);
412                                 *redraw = 1;
413                         }
414                         if( mwindow->session->track_highlighted )
415                                 result = 1;  // we have to cleanup
416                         break;
417
418                 case DRAG_ASSET:
419                         if(mwindow->session->track_highlighted) {
420                                 double asset_duration = 0;
421                                 int64_t asset_length_units = 0;
422
423                                 if(mwindow->session->current_operation == DRAG_ASSET &&
424                                         mwindow->session->drag_assets->total) {
425                                         Indexable *indexable = mwindow->session->drag_assets->values[0];
426                                         // we use video if we are over video and audio if we are over audio
427                                         if( indexable->have_video() &&
428                                             mwindow->session->track_highlighted->data_type == TRACK_VIDEO ) {
429                                                 // Images have length -1
430                                                 int64_t video_length = indexable->get_video_frames();
431                                                 if (video_length < 0) {
432                                                         if(mwindow->edl->session->si_useduration)
433                                                                 video_length = mwindow->edl->session->si_duration;
434                                                         else
435                                                                 video_length = 1.0 / mwindow->edl->session->frame_rate ;
436                                                 }
437                                                 asset_duration = video_length / indexable->get_frame_rate();
438                                         }
439                                         else if( indexable->have_audio() &&
440                                                  mwindow->session->track_highlighted->data_type == TRACK_AUDIO ) {
441                                                 int64_t audio_length = indexable->get_audio_samples();
442                                                 asset_duration = (double)audio_length / indexable->get_sample_rate();
443                                         }
444                                         else {
445                                                 result = 1;
446                                                 break;  // Do not do anything
447                                         }
448                                 }
449                                 else if( mwindow->session->current_operation == DRAG_ASSET &&
450                                          mwindow->session->drag_clips->total ) {
451                                         EDL *clip = mwindow->session->drag_clips->values[0];
452                                         asset_duration = clip->tracks->total_length();
453                                 }
454                                 else {
455                                         printf("DRAG_ASSET error: Asset dropped, but both drag_clips and drag_assets total is zero\n");
456                                 }
457
458                                 asset_length_units = mwindow->session->track_highlighted->to_units(asset_duration, 1);
459                                 int64_t drop_position = drop_edit_position (&insertion, NULL, asset_length_units);
460                                 if( drop_position < 0 ) {
461                                         result = 1;
462                                         break;          // Do not do anything
463                                 }
464
465                                 Track *track = mwindow->session->track_highlighted;
466                                 double track_position = track->from_units(drop_position);
467                                 track_position = mwindow->edl->align_to_frame(track_position, 0);
468
469 //                              if (!insertion) {
470 //                                      // FIXME, we should create an mwindow/EDL method that overwrites, without clearing the keyframes and autos
471 //                                      // Unfortunately, this is _a lot_ of work to do right
472 //                                      printf("Problematic insertion\n");
473 //                                      mwindow->edl->tracks->clear(track_position,
474 //                                              track_position + asset_duration, 0);
475 //                              }
476                                 mwindow->paste_assets(track_position, track, !insertion);
477                                 result = 1;    // need to be one no matter what, since we have track highlited so we have to cleanup....
478                         }
479                         break;
480
481                 case DRAG_EDIT:
482                         mwindow->session->current_operation = NO_OPERATION;
483                         if(mwindow->session->track_highlighted) {
484                                 if( mwindow->session->track_highlighted->data_type ==
485                                         mwindow->session->drag_edit->track->data_type) {
486                                         int64_t drop_position = drop_edit_position(&insertion,
487                                                         mwindow->session->drag_edit,
488                                                         mwindow->session->drag_edit->length);
489                                         if( drop_position < 0 ) {
490                                                 result = 1;
491                                                 break;          // Do not do anything
492                                         }
493                                         Track *track = mwindow->session->track_highlighted;
494                                         double track_position = track->from_units(drop_position);
495                                         track_position = mwindow->edl->align_to_frame(track_position, 0);
496                                         mwindow->move_edits(mwindow->session->drag_edits,
497                                                         track, track_position, insertion);
498                                 }
499
500                                 result = 1;
501                         }
502                         break;
503                 case DRAG_GROUP:
504                         mwindow->session->current_operation = NO_OPERATION;
505                         EDL *drag_group = mwindow->session->drag_group;
506                         if( drag_group ) {
507                                 Track *drop_track = mwindow->session->track_highlighted;
508                                 Track *drag_track = mwindow->session->drag_group_first_track;
509                                 Edit *drag_edit = mwindow->session->drag_group_edit;
510                                 Track *edit_track = drag_edit ? drag_edit->track : 0;
511                                 while( drop_track && edit_track ) {
512                                         if( edit_track == drag_track ) break;
513                                         edit_track = edit_track->previous;
514                                         drop_track = drop_track->previous;
515                                 }
516                                 if( drop_track ) {
517                                         double cur_pos = mwindow->session->drag_group_position;
518                                         double new_pos = mwindow->edl->get_cursor_position(cursor_x, pane->number);
519                                         new_pos -= mwindow->session->drag_position - cur_pos;
520                                         new_pos = mwindow->edl->align_to_frame(new_pos, 0);
521                                         double drop_position = new_pos;
522                                         int ret = test_track_group(drag_group, drop_track, new_pos);
523                                         if( !ret && new_pos != drop_position ) {
524                                                 drop_position = new_pos;
525                                                 ret = test_track_group(drag_group, drop_track, new_pos);
526                                         }
527                                         if( ret >= 0 )
528                                                 mwindow->move_group(drag_group, drop_track, drop_position,
529                                                         ret > 0 ? !shift_down() : shift_down());
530                                         drag_group->remove_user();
531                                         mwindow->session->drag_group = 0;
532                                 }
533                         }
534                         result = 1;
535                         break;
536                 }
537         }
538
539         return result;
540 }
541
542 int TrackCanvas::drag_start_event()
543 {
544         int result = 0;
545         int redraw = 0;
546         int rerender = 0;
547         int new_cursor, update_cursor;
548
549         if( mwindow->session->current_operation == GROUP_TOGGLE )
550                 mwindow->session->current_operation = NO_OPERATION;
551         else if( mwindow->session->current_operation != NO_OPERATION )
552                 return 0;
553
554         if(is_event_win()) {
555                 if(do_plugins(get_drag_x(), get_drag_y(),
556                                 1, 0, redraw, rerender)) {
557                         result = 1;
558                 }
559                 else if(do_edits(get_drag_x(), get_drag_y(),
560                                 0, 1, redraw, rerender, new_cursor, update_cursor)) {
561                         result = 1;
562                 }
563         }
564
565         return result;
566 }
567
568 int64_t TrackCanvas::drop_edit_position(int *is_insertion, Edit *moved_edit, int64_t moved_edit_length)
569 {
570         // get the canvas/track position
571         Track *track = mwindow->session->track_highlighted;
572         int cursor_x = get_relative_cursor_x();
573         double zoom_scale = (double)mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample;
574         double cur_pos = (cursor_x + mwindow->edl->local_session->view_start[pane->number]) / zoom_scale;
575         double position = mwindow->edl->align_to_frame(cur_pos, 1);
576         if( position <= 0 ) {
577                 *is_insertion = 1;
578                 return 0;
579         }
580         double cursor_position = position;
581         int64_t drop_position = track->to_units(cursor_position, 1);
582         if( moved_edit ) { // relative cursor position depends upon drop point
583                 double moved_edit_start = moved_edit->track->from_units(moved_edit->startproject);
584                 position -= mwindow->session->drag_position - moved_edit_start;
585         }
586         int64_t edit_position = track->to_units(position, 1);
587         int64_t grab_position = edit_position;
588         if( !moved_edit )  // for clips and assets acts as they were grabbed in the middle
589                 grab_position -= moved_edit_length / 2;
590         Edit *last_edit = track->edits->last;
591         if( !last_edit || edit_position >= (last_edit->startproject+last_edit->length) ) {
592                 *is_insertion = 0;
593                 return edit_position;
594         }
595
596         int64_t drop_start = 0, drop_end = 0;  // drop zone boundries
597         Edit *next_edit = track->edits->first;
598         while( next_edit ) {
599                 Edit *edit = next_edit;  next_edit = (Edit *)edit->next;
600                 int64_t edit_start = edit->startproject;
601                 int64_t edit_end = edit_start + edit->length;
602                 double edit_start_pos = edit->track->from_units(edit_start);
603                 if( (fabs(edit_start_pos-cursor_position)*zoom_scale) < HANDLE_W ) {
604                         *is_insertion = 1; // cursor is close to the beginning of an edit -> insertion
605                         return edit_start;
606                 }
607                 double edit_end_pos = edit->track->from_units(edit_end);
608                 if( (fabs(edit_end_pos-cursor_position)*zoom_scale) < HANDLE_W ) {
609                         *is_insertion = 1; // cursor is close to the end of an edit -> insertion
610                         return edit_end;
611                 }
612                 if( edit != moved_edit && !edit->silence() )
613                         drop_start = edit_end; // reset drop zone
614                 if( next_edit ) {
615                         if( next_edit == moved_edit || next_edit->silence() ) continue;
616                         drop_end = edit_end;
617                 }
618                 else
619                         drop_end = INT64_MAX;
620                 if( edit_position >= drop_start &&
621                     edit_position+moved_edit_length < drop_end ) {
622                         *is_insertion = 0; // fits in the zone
623                         return edit_position;
624                 }
625                 if( drop_position < drop_end ) { // drop in the zone
626                         if( (drop_end - drop_start) >= moved_edit_length ) {
627                                 *is_insertion = 0; // fits in the zone, but over the edge
628                                 int64_t dx0 = llabs(drop_position - drop_start);
629                                 int64_t dx1 = llabs(drop_position - drop_end);
630                                 return dx0 < dx1 ? drop_start : drop_end - moved_edit_length;
631                         }
632                         *is_insertion = 1;
633                         int64_t edit_center = (edit_start + edit_end) / 2;
634                         return position < edit_center ? drop_start : drop_end;
635                 }
636         }
637
638         *is_insertion = 0;
639         return -1;
640 }
641
642 int64_t TrackCanvas::drop_plugin_position(PluginSet *plugin_set, Plugin *moved_plugin)
643 {
644         // get the canvas/track position
645         Track *track = plugin_set->track;
646         double moved_plugin_length = moved_plugin->track->from_units(moved_plugin->length);
647         int64_t track_plugin_length = track->to_units(moved_plugin_length, 0);
648         int cursor_x = get_relative_cursor_x();
649         double zoom_scale = (double)mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample;
650         double cur_pos = (cursor_x + mwindow->edl->local_session->view_start[pane->number]) / zoom_scale;
651         double position = mwindow->edl->align_to_frame(cur_pos, 1);
652         if( position <= 0 ) return 0;
653         int64_t drop_position = track->to_units(position, 1);
654         Plugin *last_plugin = (Plugin *)plugin_set->last;
655         if( !last_plugin ) return drop_position;
656         double plugin_set_end = last_plugin->track->from_units(last_plugin->startproject+last_plugin->length);
657         if( position >= plugin_set_end ) return drop_position;
658         double moved_plugin_start = moved_plugin->track->from_units(moved_plugin->startproject);
659         position -= mwindow->session->drag_position - moved_plugin_start;
660         int64_t plugin_position = track->to_units(position, 1);
661
662         int64_t drop_start = 0, drop_end = 0;  // drop zone boundries
663         Plugin *next_plugin = (Plugin *)plugin_set->first;
664         while( next_plugin ) {
665                 Plugin *plugin = next_plugin;  next_plugin = (Plugin *)plugin->next;
666                 int64_t plugin_start = plugin->startproject;
667                 int64_t plugin_end = plugin_start + plugin->length;
668                 double plugin_end_pos = plugin->track->from_units(plugin_end);
669                 int64_t track_plugin_end = track->to_units(plugin_end_pos, 0);
670                 if( plugin != moved_plugin && !plugin->silence() )
671                         drop_start = track_plugin_end;
672                 if( next_plugin ) {
673                         if( next_plugin == moved_plugin || next_plugin->silence() ) continue;
674                         drop_end = track_plugin_end;
675                 }
676                 else
677                         drop_end = INT64_MAX;
678                 if( plugin_position >= drop_start && // fits in the zone
679                     plugin_position+track_plugin_length < drop_end ) {
680                         return plugin_position;
681                 }
682                 if( drop_position < drop_end ) { // drop in the zone
683                         if( (drop_end - drop_start) >= track_plugin_length ) {
684                                 int64_t dx0 = llabs(drop_position - drop_start);
685                                 int64_t dx1 = llabs(drop_position - drop_end);
686                                 return dx0 < dx1 ? drop_start : drop_end - track_plugin_length;
687                         }
688                 }
689         }
690
691         return -1;
692 }
693
694 void TrackCanvas::draw(int mode, int hide_cursor)
695 {
696         const int debug = 0;
697
698 // Swap pixmap layers
699         if(get_w() != background_pixmap->get_w() ||
700                 get_h() != background_pixmap->get_h())
701         {
702                 delete background_pixmap;
703                 background_pixmap = new BC_Pixmap(this, get_w(), get_h());
704         }
705
706 // Cursor disappears after resize when this is called.
707 // Cursor doesn't redraw after editing when this isn't called.
708         if(pane->cursor && hide_cursor) pane->cursor->hide();
709         draw_top_background(get_parent(), 0, 0, get_w(), get_h(), background_pixmap);
710
711         if(debug) PRINT_TRACE
712         draw_resources(mode);
713
714         if(debug) PRINT_TRACE
715         draw_overlays();
716         if(debug) PRINT_TRACE
717 }
718
719 void TrackCanvas::update_cursor(int flush)
720 {
721         if( arrow_mode() )
722                 set_cursor(ARROW_CURSOR, 0, flush);
723         else if( ibeam_mode() )
724                 set_cursor(IBEAM_CURSOR, 0, flush);
725 }
726
727
728 void TrackCanvas::test_timer()
729 {
730         if( resource_timer->get_difference() > 1000 && !hourglass_enabled ) {
731                 start_hourglass();
732                 hourglass_enabled = 1;
733         }
734 }
735
736
737 void TrackCanvas::draw_indexes(Indexable *indexable)
738 {
739         IndexState *index_state = indexable->index_state;
740 // Don't redraw raw samples
741         if(index_state->index_zoom > mwindow->edl->local_session->zoom_sample)
742                 return;
743         draw_resources(NORMAL_DRAW, 1, indexable);
744         draw_overlays();
745         flash(0);
746 }
747
748 void TrackCanvas::draw_resources(int mode,
749         int indexes_only,
750         Indexable *indexable)
751 {
752         const int debug = 0;
753
754         if(debug) PRINT_TRACE
755
756 // can't stop thread here, because this is called for every pane
757 //      if(mode != IGNORE_THREAD && !indexes_only)
758 //              gui->resource_thread->stop_draw(!indexes_only);
759
760         if(mode != IGNORE_THREAD &&
761                 !indexes_only &&
762                 !gui->resource_thread->interrupted)
763         {
764                 printf("TrackCanvas::draw_resources %d: called without stopping ResourceThread\n",
765                         __LINE__);
766
767                 BC_Signals::dump_stack();
768         }
769
770         if(debug) PRINT_TRACE
771
772         resource_timer->update();
773
774 // Age resource pixmaps for deletion
775         if(!indexes_only)
776                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
777                         gui->resource_pixmaps.values[i]->visible--;
778
779         if(mode == FORCE_REDRAW)
780                 gui->resource_pixmaps.remove_all_objects();
781
782         if(debug) PRINT_TRACE
783         if(mode != IGNORE_THREAD)
784                 gui->resource_thread->reset(pane->number, indexes_only);
785
786 // Search every edit
787         for(Track *current = mwindow->edl->tracks->first;
788                 current;
789                 current = NEXT)
790         {
791                 if( current->is_hidden() ) continue;
792                 if(debug) PRINT_TRACE
793                 for(Edit *edit = current->edits->first; edit; edit = edit->next)
794                 {
795                         if(debug) PRINT_TRACE
796                         if( current->data_type != TRACK_SUBTITLE )
797                                 if(!edit->asset && !edit->nested_edl) continue;
798                         if(indexes_only)
799                         {
800                                 if(edit->track->data_type != TRACK_AUDIO) continue;
801
802                                 if(edit->nested_edl &&
803                                         strcmp(indexable->path, edit->nested_edl->path)) continue;
804
805                                 if(edit->asset &&
806                                         strcmp(indexable->path, edit->asset->path)) continue;
807                         }
808
809                         if(debug) PRINT_TRACE
810
811                         int64_t edit_x, edit_y, edit_w, edit_h;
812                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
813
814 // Edit is visible
815                         if(MWindowGUI::visible(edit_x, edit_x + edit_w, 0, get_w()) &&
816                                 MWindowGUI::visible(edit_y, edit_y + edit_h, 0, get_h()))
817                         {
818                                 int64_t pixmap_x, pixmap_w, pixmap_h;
819                                 if(debug) PRINT_TRACE
820
821 // Search for existing pixmap containing edit
822                                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
823                                 {
824                                         ResourcePixmap* pixmap = gui->resource_pixmaps.values[i];
825 // Same pointer can be different edit if editing took place
826                                         if(pixmap->edit_id == edit->id &&
827                                                 pixmap->pane_number == pane->number)
828                                         {
829                                                 pixmap->visible = 1;
830                                                 break;
831                                         }
832                                 }
833                                 if(debug) PRINT_TRACE
834
835 // Get new size, offset of pixmap needed
836                                 get_pixmap_size(edit,
837                                         edit_x,
838                                         edit_w,
839                                         pixmap_x,
840                                         pixmap_w,
841                                         pixmap_h);
842                                 if(debug) PRINT_TRACE
843
844 // Draw new data
845                                 if(pixmap_w && pixmap_h)
846                                 {
847 // Create pixmap if it doesn't exist
848                                         ResourcePixmap* pixmap = create_pixmap(edit, edit_x,
849                                                 pixmap_x, pixmap_w, pixmap_h);
850 // Resize it if it's bigger
851                                         if( pixmap_w > pixmap->pixmap_w ||
852                                             pixmap_h > pixmap->pixmap_h)
853                                                 pixmap->resize(pixmap_w, pixmap_h);
854                                         pixmap->update_settings(edit, edit_x, edit_w,
855                                                 pixmap_x, pixmap_w, pixmap_h);
856 // Draw data
857                                         if( current->show_assets() )
858                                                 pixmap->draw_data(this,
859                                                         edit, edit_x, edit_w,
860                                                         pixmap_x, pixmap_w, pixmap_h,
861                                                         mode, indexes_only);
862                                         else {
863                                                 set_bg_color(BLACK);
864                                                 clear_box(0,0, pixmap_w,pixmap_h, pixmap);
865                                         }
866 // Draw title
867                                         if( current->show_titles() )
868                                                 pixmap->draw_title(this,
869                                                         edit, edit_x, edit_w,
870                                                         pixmap_x, pixmap_w);
871 // Resize it if it's smaller
872                                         if(pixmap_w < pixmap->pixmap_w ||
873                                                 pixmap_h < pixmap->pixmap_h)
874                                                 pixmap->resize(pixmap_w, pixmap_h);
875
876 // Copy pixmap to background canvas
877                                         background_pixmap->draw_pixmap(pixmap,
878                                                 pixmap->pixmap_x,
879                                                 current->y_pixel - mwindow->edl->local_session->track_start[pane->number],
880                                                 pixmap->pixmap_w,
881                                                 edit_h);
882                                 }
883                                 if(debug) PRINT_TRACE
884
885                         }
886                 }
887                 int64_t track_x, track_y, track_w, track_h;
888                 track_dimensions(current,
889                         track_x, track_y, track_w, track_h);
890                 set_color((~get_resources()->get_bg_color()) & 0xffffff);
891                 set_opaque();
892                 int x1 = track_x, x2 = x1+track_w;
893                 int y1 = track_y+track_h-1;
894                 draw_line(x1,y1, x2,y1, background_pixmap);
895         }
896
897
898 // Delete unused pixmaps
899         if(debug) PRINT_TRACE
900         if(!indexes_only)
901                 for(int i = gui->resource_pixmaps.total - 1; i >= 0; i--)
902                         if(gui->resource_pixmaps.values[i]->visible < PIXMAP_AGE)
903                         {
904 //printf("TrackCanvas::draw_resources %d\n", __LINE__);
905                                 delete gui->resource_pixmaps.values[i];
906                                 gui->resource_pixmaps.remove(gui->resource_pixmaps.values[i]);
907                         }
908         if(debug) PRINT_TRACE
909
910         if(hourglass_enabled)
911         {
912                 stop_hourglass();
913                 hourglass_enabled = 0;
914         }
915         if(debug) PRINT_TRACE
916
917 // can't stop thread here, because this is called for every pane
918 //      if(mode != IGNORE_THREAD && !indexes_only)
919 //              gui->resource_thread->start_draw();
920         if(debug) PRINT_TRACE
921
922
923 }
924
925 ResourcePixmap* TrackCanvas::create_pixmap(Edit *edit,
926         int64_t edit_x,
927         int64_t pixmap_x,
928         int64_t pixmap_w,
929         int64_t pixmap_h)
930 {
931         ResourcePixmap *result = 0;
932
933         for(int i = 0; i < gui->resource_pixmaps.total; i++)
934         {
935 //printf("TrackCanvas::create_pixmap 1 %d %d\n", edit->id, resource_pixmaps.values[i]->edit->id);
936                 if(gui->resource_pixmaps.values[i]->edit_id == edit->id &&
937                         gui->resource_pixmaps.values[i]->pane_number == pane->number)
938                 {
939                         result = gui->resource_pixmaps.values[i];
940                         break;
941                 }
942         }
943
944         if(!result)
945         {
946 //SET_TRACE
947                 result = new ResourcePixmap(mwindow,
948                         gui,
949                         edit,
950                         pane->number,
951                         pixmap_w,
952                         pixmap_h);
953 //SET_TRACE
954                 gui->resource_pixmaps.append(result);
955         }
956
957 //      result->resize(pixmap_w, pixmap_h);
958         return result;
959 }
960
961 void TrackCanvas::get_pixmap_size(Edit *edit,
962         int64_t edit_x,
963         int64_t edit_w,
964         int64_t &pixmap_x,
965         int64_t &pixmap_w,
966         int64_t &pixmap_h)
967 {
968
969 // Align x on frame boundaries
970
971
972 //      switch(edit->edits->track->data_type)
973 //      {
974 //              case TRACK_AUDIO:
975
976                         pixmap_x = edit_x;
977                         pixmap_w = edit_w;
978                         if(pixmap_x < 0)
979                         {
980                                 pixmap_w -= -edit_x;
981                                 pixmap_x = 0;
982                         }
983
984                         if(pixmap_x + pixmap_w > get_w())
985                         {
986                                 pixmap_w = get_w() - pixmap_x;
987                         }
988
989 //                      break;
990 //
991 //              case TRACK_VIDEO:
992 //              {
993 //                      int64_t picon_w = (int64_t)(edit->picon_w() + 0.5);
994 //                      int64_t frame_w = (int64_t)(edit->frame_w() + 0.5);
995 //                      int64_t pixel_increment = MAX(picon_w, frame_w);
996 //                      int64_t pixmap_x1 = edit_x;
997 //                      int64_t pixmap_x2 = edit_x + edit_w;
998 //
999 //                      if(pixmap_x1 < 0)
1000 //                      {
1001 //                              pixmap_x1 = (int64_t)((double)-edit_x / pixel_increment) *
1002 //                                      pixel_increment +
1003 //                                      edit_x;
1004 //                      }
1005 //
1006 //                      if(pixmap_x2 > get_w())
1007 //                      {
1008 //                              pixmap_x2 = (int64_t)((double)(get_w() - edit_x) / pixel_increment + 1) *
1009 //                                      pixel_increment +
1010 //                                      edit_x;
1011 //                      }
1012 //                      pixmap_x = pixmap_x1;
1013 //                      pixmap_w = pixmap_x2 - pixmap_x1;
1014 //                      break;
1015 //              }
1016 //      }
1017
1018         Track *track = edit->edits->track;
1019         pixmap_h = track->data_h;
1020         if( track->show_titles() )
1021                 pixmap_h += mwindow->theme->get_image("title_bg_data")->get_h();
1022 //printf("get_pixmap_size %d %d %d %d\n", edit_x, edit_w, pixmap_x, pixmap_w);
1023 }
1024
1025 void TrackCanvas::edit_dimensions(Edit *edit,
1026         int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1027 {
1028         x = Units::round(edit->track->from_units(edit->startproject) *
1029                         mwindow->edl->session->sample_rate /
1030                         mwindow->edl->local_session->zoom_sample -
1031                         mwindow->edl->local_session->view_start[pane->number]);
1032
1033         y = edit->edits->track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1034
1035 // Method for calculating w so when edits are together we never get off by one error due to rounding
1036         int64_t x_next = Units::round(edit->track->from_units(edit->startproject + edit->length) *
1037                         mwindow->edl->session->sample_rate /
1038                         mwindow->edl->local_session->zoom_sample -
1039                         mwindow->edl->local_session->view_start[pane->number]);
1040         w = x_next - x;
1041
1042         int edit_h = 0;
1043         if( edit->track->show_titles() )
1044                 edit_h += mwindow->theme->get_image("title_bg_data")->get_h();
1045         if( edit->track->show_assets() )
1046                 edit_h += edit->track->data_h;
1047         h = edit_h;
1048 }
1049
1050 void TrackCanvas::track_dimensions(Track *track, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1051 {
1052         x = 0;
1053         w = get_w();
1054         y = track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1055         h = track->vertical_span(mwindow->theme);
1056 }
1057
1058
1059 void TrackCanvas::draw_paste_destination()
1060 {
1061         //int cursor_x = get_relative_cursor_x();
1062         //int cursor_y = get_relative_cursor_y();
1063         int current_atrack = 0;
1064         int current_vtrack = 0;
1065         int current_aedit = 0;
1066         int current_vedit = 0;
1067         int64_t w = 0;
1068         int64_t x;
1069         double position;
1070         int insertion  = 0;
1071
1072 //if(pane->number == BOTTOM_RIGHT_PANE)
1073 //printf("TrackCanvas::draw_paste_destination %d %p\n", __LINE__, mwindow->session->track_highlighted);
1074
1075         if((mwindow->session->current_operation == DRAG_ASSET &&
1076                         (mwindow->session->drag_assets->total ||
1077                         mwindow->session->drag_clips->total)) ||
1078                 (mwindow->session->current_operation == DRAG_EDIT &&
1079                         mwindow->session->drag_edits->total)) {
1080                 Indexable *indexable = 0;
1081                 EDL *clip = 0;
1082                 //int draw_box = 0;
1083
1084                 if(mwindow->session->current_operation == DRAG_ASSET &&
1085                         mwindow->session->drag_assets->size())
1086                         indexable = mwindow->session->drag_assets->get(0);
1087
1088                 if(mwindow->session->current_operation == DRAG_ASSET &&
1089                         mwindow->session->drag_clips->size())
1090                         clip = mwindow->session->drag_clips->get(0);
1091
1092                 int has_audio = 0, has_video = 0;
1093                 double paste_audio_length = 0, paste_video_length = 0;
1094                 double paste_audio_position = -1, paste_video_position = -1;
1095
1096                 if( indexable ) {
1097                         has_audio = indexable->have_audio();
1098                         paste_audio_length = !has_audio ? 0 :
1099                                  (double)indexable->get_audio_samples() / indexable->get_sample_rate();
1100                         has_video = indexable->have_video();
1101                         Asset *asset = indexable->is_asset ? (Asset *)indexable : 0;
1102                         // Images have length -1
1103                         if( asset && asset->video_data && asset->video_length < 0 ) {
1104                                 paste_video_length = mwindow->edl->session->si_useduration ?
1105                                         mwindow->edl->session->si_duration / asset->frame_rate :
1106                                         1.0 / asset->frame_rate ;  // 1 frame
1107                         }
1108                         else
1109                                 paste_video_length = !has_video ? 0 :
1110                                         (double)indexable->get_video_frames() / indexable->get_frame_rate();
1111                 }
1112
1113                 if( clip ) {
1114                         has_audio = clip->tracks->total_audio_tracks() > 0 ? 1 : 0;
1115                         paste_audio_length = !has_audio ? 0 : clip->tracks->total_audio_length();
1116                         has_video = clip->tracks->total_video_tracks() > 0 ? 1 : 0;
1117                         paste_video_length = !has_video ? 0 : clip->tracks->total_video_length();
1118                 }
1119
1120 // 'Align cursor on frame' lengths calculations
1121                 if( mwindow->edl->session->cursor_on_frames ) {
1122                         double fps = mwindow->edl->session->frame_rate;
1123                         double aud = floor(paste_audio_length * fps) / fps;
1124                         double vid = floor(paste_video_length * fps) / fps;
1125                         double length = has_video && has_audio ?
1126                                         aud < vid ? aud : vid :
1127                                 has_video ? vid :
1128                                 has_audio ? aud : 0;
1129                         paste_video_length = paste_audio_length = length;
1130                 }
1131
1132                 if( has_audio ) { // we use video if we are over video and audio if we are over audio
1133                         int64_t length = 0;
1134                         switch( mwindow->session->track_highlighted->data_type ) {
1135                         case TRACK_VIDEO: length = paste_video_length;  break;
1136                         case TRACK_AUDIO: length = paste_audio_length;  break;
1137                         }
1138                         int64_t asset_length = mwindow->session->track_highlighted->
1139                                 to_units(length, 1);
1140                         paste_audio_position = mwindow->session->track_highlighted->
1141                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1142                 }
1143                 if( has_video ) {
1144                         int64_t asset_length = mwindow->session->track_highlighted->
1145                                 to_units(paste_video_length, 1);
1146                         paste_video_position = mwindow->session->track_highlighted->
1147                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1148                 }
1149
1150 // Get destination track
1151                 for(Track *dest = mwindow->session->track_highlighted;
1152                         dest;
1153                         dest = dest->next) {
1154                         if( dest->is_hidden() ) continue;
1155                         if(dest->is_armed()) {
1156 // Get source width in pixels
1157                                 w = -1;
1158 // Use start of highlighted edit
1159                                 if(mwindow->session->edit_highlighted) {
1160                                         position = mwindow->session->track_highlighted->from_units(
1161                                                 mwindow->session->edit_highlighted->startproject);
1162                                 }
1163                                 else {
1164 // Use end of highlighted track, disregarding effects
1165                                         position = mwindow->session->track_highlighted->from_units(
1166                                                 mwindow->session->track_highlighted->edits->length());
1167                                 }
1168
1169 // Get the x coordinate
1170                                 x = Units::to_int64(position *
1171                                         mwindow->edl->session->sample_rate /
1172                                         mwindow->edl->local_session->zoom_sample) -
1173                                         mwindow->edl->local_session->view_start[pane->number];
1174
1175                                 double paste_position = -1.;
1176                                 if(dest->data_type == TRACK_AUDIO) {
1177                                         if(indexable && current_atrack < indexable->get_audio_channels()) {
1178 //printf("TrackCanvas::draw_paste_destination %d %d\n", __LINE__, current_atrack);
1179                                                 w = Units::to_int64((double)indexable->get_audio_samples() /
1180                                                         indexable->get_sample_rate() *
1181                                                         mwindow->edl->session->sample_rate /
1182                                                         mwindow->edl->local_session->zoom_sample);
1183                                                 paste_position = paste_audio_position;
1184                                         }
1185                                         else if(clip && current_atrack < clip->tracks->total_audio_tracks()) {
1186                                                 w = Units::to_int64((double)clip->tracks->total_length() *
1187                                                         mwindow->edl->session->sample_rate /
1188                                                         mwindow->edl->local_session->zoom_sample);
1189                                                 paste_position = paste_audio_position;
1190 //printf("draw_paste_destination %d\n", x);
1191                                         }
1192                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1193                                                 current_aedit < mwindow->session->drag_edits->total) {
1194                                                 Edit *edit;
1195                                                 while(current_aedit < mwindow->session->drag_edits->total &&
1196                                                         mwindow->session->drag_edits->values[current_aedit]->track->data_type != TRACK_AUDIO)
1197                                                         current_aedit++;
1198
1199                                                 if(current_aedit < mwindow->session->drag_edits->total) {
1200                                                         edit = mwindow->session->drag_edits->values[current_aedit];
1201                                                         w = Units::to_int64(edit->length / mwindow->edl->local_session->zoom_sample);
1202                                                         paste_position = mwindow->session->track_highlighted->
1203                                                                 from_units(drop_edit_position(&insertion,
1204                                                                         mwindow->session->drag_edit,
1205                                                                         mwindow->session->drag_edit->length));
1206                                                         current_aedit++;
1207                                                 }
1208                                         }
1209                                         if( paste_position >= 0 ) {
1210                                                 position = paste_position;
1211                                                 current_atrack++;
1212                                                 //draw_box = 1;
1213                                         }
1214                                         else
1215                                                 w = -1;
1216                                 }
1217                                 else if(dest->data_type == TRACK_VIDEO) {
1218 //printf("draw_paste_destination 1\n");
1219                                         if(indexable && current_vtrack < indexable->get_video_layers())
1220                                         {
1221                                                 w = Units::to_int64((double)indexable->get_video_frames() /
1222                                                         indexable->get_frame_rate() *
1223                                                         mwindow->edl->session->sample_rate /
1224                                                         mwindow->edl->local_session->zoom_sample);
1225                                                 paste_position = paste_video_position;
1226                                         }
1227                                         else if(clip && current_vtrack < clip->tracks->total_video_tracks()) {
1228                                                 w = Units::to_int64(clip->tracks->total_length() *
1229                                                         mwindow->edl->session->sample_rate /
1230                                                         mwindow->edl->local_session->zoom_sample);
1231                                                 paste_position = paste_video_position;
1232                                         }
1233                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1234                                                 current_vedit < mwindow->session->drag_edits->total) {
1235                                                 Edit *edit;
1236                                                 while(current_vedit < mwindow->session->drag_edits->total &&
1237                                                         mwindow->session->drag_edits->values[current_vedit]->track->data_type != TRACK_VIDEO)
1238                                                         current_vedit++;
1239
1240                                                 if(current_vedit < mwindow->session->drag_edits->total) {
1241                                                         edit = mwindow->session->drag_edits->values[current_vedit];
1242                                                         w = Units::to_int64(edit->track->from_units(edit->length) *
1243                                                                 mwindow->edl->session->sample_rate /
1244                                                                 mwindow->edl->local_session->zoom_sample);
1245                                                         paste_position = mwindow->session->track_highlighted->
1246                                                                 from_units(drop_edit_position(&insertion,
1247                                                                         mwindow->session->drag_edit,
1248                                                                         mwindow->session->drag_edit->length));
1249                                                         current_vedit++;
1250                                                 }
1251                                         }
1252                                         if( paste_position >= 0 ) {
1253                                                 position = paste_position;
1254                                                 current_vtrack++;
1255                                                 //draw_box = 1;
1256                                         }
1257                                         else
1258                                                 w = -1;
1259                                 }
1260
1261                                 if(w >= 0) {
1262 // Get the x coordinate
1263                                         x = Units::to_int64(position *
1264                                                 mwindow->edl->session->sample_rate /
1265                                                 mwindow->edl->local_session->zoom_sample) -
1266                                                 mwindow->edl->local_session->view_start[pane->number];
1267                                         int y = dest->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1268                                         int h = dest->vertical_span(mwindow->theme);
1269
1270 //printf("TrackCanvas::draw_paste_destination 2 %d %d %d %d\n", x, y, w, h);
1271                                         if(x < -BC_INFINITY) {
1272                                                 w -= -BC_INFINITY - x;
1273                                                 x += -BC_INFINITY - x;
1274                                         }
1275                                         w = MIN(65535, w);
1276 // if(pane->number == TOP_RIGHT_PANE)
1277 // printf("TrackCanvas::draw_paste_destination %d %d %d %d %d\n",
1278 // __LINE__, x, y, w, h);
1279                                         if (insertion)
1280                                                 draw_highlight_insertion(x, y, w, h);
1281                                         else
1282                                                 draw_highlight_rectangle(x, y, w, h);
1283                                 }
1284                         }
1285                 }
1286         }
1287 }
1288
1289 void TrackCanvas::plugin_dimensions(Plugin *plugin, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1290 {
1291         x = Units::round(plugin->track->from_units(plugin->startproject) *
1292                 mwindow->edl->session->sample_rate /
1293                 mwindow->edl->local_session->zoom_sample -
1294                 mwindow->edl->local_session->view_start[pane->number]);
1295         w = Units::round(plugin->track->from_units(plugin->length) *
1296                 mwindow->edl->session->sample_rate /
1297                 mwindow->edl->local_session->zoom_sample);
1298         y = plugin->track->y_pixel
1299                 - mwindow->edl->local_session->track_start[pane->number];
1300         if( plugin->track->show_titles() )
1301                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1302         if( plugin->track->show_assets() )
1303                 y += plugin->track->data_h;
1304         y += plugin->plugin_set->get_number() *
1305                         mwindow->theme->get_image("plugin_bg_data")->get_h();
1306         h = mwindow->theme->get_image("plugin_bg_data")->get_h();
1307 }
1308
1309
1310 void TrackCanvas::draw_highlight_rectangle(int x, int y, int w, int h)
1311 {
1312
1313 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1314 // just draw arrows, so user has indication that something is there
1315 // FIXME: get better colors
1316
1317         if (x + w <= 0)
1318         {
1319                 draw_triangle_left(0, y + h /6,
1320                         h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1321                 return;
1322         } else
1323         if (x >= get_w())
1324         {
1325                 draw_triangle_right(get_w() - h * 2/3, y + h /6,
1326                         h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1327                 return;
1328         }
1329
1330 // Fix bug in heroines & cvs version as of 22.8.2005:
1331 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1332         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1333         if(x < -10) { w += x - -10;  x = -10; }
1334         if(y < -10) { h += y - -10;  y = -10; }
1335
1336         w = MIN(w, get_w() + 20);
1337         h = MIN(h, get_h() + 20);
1338         if(w > 0 && h > 0)
1339         {
1340                 set_color(mwindow->preferences->highlight_inverse);
1341                 set_inverse();
1342                 //draw_rectangle(x, y, w, h);
1343                 draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1344                 draw_rectangle(x + 2, y + 2, w - 4, h - 4);
1345                 set_opaque();
1346                 draw_rectangle(x, y, w, h);
1347         }
1348 //if(pane->number == TOP_RIGHT_PANE)
1349 //printf("TrackCanvas::draw_highlight_rectangle %d %d %d %d %d\n", __LINE__, x, y, w, h);
1350 }
1351
1352 void TrackCanvas::draw_highlight_insertion(int x, int y, int w, int h)
1353 {
1354
1355 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1356 // just draw arrows, so user has indication that something is there
1357 // FIXME: get better colors
1358
1359
1360
1361         int h1 = h / 8;
1362         int h2 = h / 4;
1363
1364         set_inverse();
1365
1366 /* these don't look so good
1367
1368         draw_line(x, y, x, y+h);
1369         draw_line(x - h2 * 2, y + h1*2,   x - h2, y+h1*2);
1370         draw_line(x - h2 * 2, y + h1*2+1, x - h2, y+h1*2+1);
1371         draw_line(x - h2 * 2, y + h1*6,   x - h2, y+h1*6);
1372         draw_line(x - h2 * 2, y + h1*6+1, x - h2, y+h1*6+1);
1373 */
1374         draw_triangle_right(x - h2, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1375         draw_triangle_right(x - h2, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1376
1377 /*      draw_line(x + h2 * 2, y + h1*2,   x + h2, y+h1*2);
1378         draw_line(x + h2 * 2, y + h1*2+1, x + h2, y+h1*2+1);
1379         draw_line(x + h2 * 2, y + h1*6,   x + h2, y+h1*6);
1380         draw_line(x - h2 * 2, y + h1*6+1, x + h2, y+h1*6+1);
1381 */
1382         draw_triangle_left(x, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1383         draw_triangle_left(x, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1384
1385 // draw the box centred around x
1386         x -= w / 2;
1387 // Fix bug in heroines & cvs version as of 22.8.2005:
1388 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1389         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1390         if(x < -10) { w += x - -10;  x = -10; }
1391         if(y < -10) { h += y - -10;  y = -10; }
1392         w = MIN(w, get_w() + 20);
1393         h = MIN(h, get_h() + 20);
1394         set_color(mwindow->preferences->highlight_inverse);
1395         set_inverse();
1396         draw_rectangle(x, y, w, h);
1397         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1398         set_opaque();
1399 //printf("TrackCanvas::draw_highlight_insertion %d %d %d %d\n", x, y, w, h);
1400 }
1401
1402 void TrackCanvas::draw_playback_cursor()
1403 {
1404 // Called before playback_cursor exists
1405 //      if(mwindow->playback_cursor && mwindow->playback_cursor->visible)
1406 //      {
1407 //              mwindow->playback_cursor->visible = 0;
1408 //              mwindow->playback_cursor->draw();
1409 //      }
1410 }
1411
1412 void TrackCanvas::get_handle_coords(Edit *edit, int64_t &x, int64_t &y, int64_t &w, int64_t &h, int side)
1413 {
1414         int handle_w = mwindow->theme->edithandlein_data[0]->get_w();
1415         int handle_h = mwindow->theme->edithandlein_data[0]->get_h();
1416
1417         edit_dimensions(edit, x, y, w, h);
1418
1419         if( edit->track->show_titles() )
1420                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1421
1422         if(side == EDIT_OUT)
1423                 x += w - handle_w;
1424
1425         h = handle_h;
1426         w = handle_w;
1427 }
1428
1429 void TrackCanvas::get_transition_coords(Edit *edit,
1430                 int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1431 {
1432         int transition_w = xS(30), transition_h = yS(30);
1433         int has_titles = edit->track->show_titles();
1434         int has_assets = edit->track->show_assets();
1435         double title_bg_h = mwindow->theme->get_image("title_bg_data")->get_h();
1436         int data_h = edit->track->data_h;
1437         double ys = has_assets ? data_h : has_titles ? title_bg_h : 0;
1438         double dy = has_titles ?
1439                 ( has_assets ? title_bg_h + data_h/2 : title_bg_h/2 ) :
1440                 ( has_assets ? data_h/2 : 0) ;
1441         double title_h = mwindow->theme->title_h;
1442         if( dy < title_h / 2 ) { ys = title_h;  dy = ys / 2; }
1443         y += dy;
1444
1445         x -= transition_w / 2;
1446         y -= transition_h / 2;
1447         w = transition_w;
1448         h = transition_h;
1449 }
1450
1451 void TrackCanvas::draw_highlighting()
1452 {
1453         int64_t x, y, w, h;
1454         int draw_box = 0;
1455
1456         switch(mwindow->session->current_operation)
1457         {
1458                 case DRAG_ATRANSITION:
1459                 case DRAG_VTRANSITION:
1460 //printf("TrackCanvas::draw_highlighting 1 %p %p\n",
1461 //      mwindow->session->track_highlighted, mwindow->session->edit_highlighted);
1462                         if(mwindow->session->edit_highlighted) {
1463                                 if((mwindow->session->current_operation == DRAG_ATRANSITION &&
1464                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1465                                         (mwindow->session->current_operation == DRAG_VTRANSITION &&
1466                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) {
1467                                         edit_dimensions(mwindow->session->edit_highlighted,
1468                                                 x, y, w, h);
1469                                         if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1470                                                 MWindowGUI::visible(y, y + h, 0, get_h())) {
1471                                                 draw_box = 1;
1472                                                 get_transition_coords(mwindow->session->edit_highlighted,
1473                                                         x, y, w, h);
1474                                         }
1475                                 }
1476                         }
1477                         break;
1478
1479 // Dragging a new effect from the Resource window
1480                 case DRAG_AEFFECT:
1481                 case DRAG_VEFFECT:
1482                         if(mwindow->session->track_highlighted &&
1483                                 ((mwindow->session->current_operation == DRAG_AEFFECT &&
1484                                   mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1485                                  (mwindow->session->current_operation == DRAG_VEFFECT &&
1486                                   mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1487 // Put it before another plugin
1488                                 if(mwindow->session->plugin_highlighted) {
1489                                         plugin_dimensions(mwindow->session->plugin_highlighted,
1490                                                 x, y, w, h);
1491 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1492                                 }
1493 // Put it after a plugin set
1494                                 else if( mwindow->session->pluginset_highlighted &&
1495                                          mwindow->session->pluginset_highlighted->last ) {
1496                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last,
1497                                                 x, y, w, h);
1498 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1499                                         int64_t track_x, track_y, track_w, track_h;
1500                                         track_dimensions(mwindow->session->track_highlighted,
1501                                                 track_x, track_y, track_w, track_h);
1502
1503                                         x += w;
1504                                         w = Units::round(
1505                                                         mwindow->session->track_highlighted->get_length() *
1506                                                         mwindow->edl->session->sample_rate /
1507                                                         mwindow->edl->local_session->zoom_sample -
1508                                                         mwindow->edl->local_session->view_start[pane->number]) -
1509                                                 x;
1510 //printf("TrackCanvas::draw_highlighting 2 %d\n", w);
1511                                         if(w <= 0) w = track_w;
1512                                 }
1513                                 else {
1514                                         track_dimensions(mwindow->session->track_highlighted,
1515                                                 x, y, w, h);
1516
1517 //printf("TrackCanvas::draw_highlighting 1 %d %d %d %d\n", x, y, w, h);
1518 // Put it in a new plugin set determined by the selected range
1519                                         if(mwindow->edl->local_session->get_selectionend() >
1520                                                 mwindow->edl->local_session->get_selectionstart()) {
1521                                                 x = Units::to_int64(mwindow->edl->local_session->get_selectionstart() *
1522                                                         mwindow->edl->session->sample_rate /
1523                                                         mwindow->edl->local_session->zoom_sample -
1524                                                         mwindow->edl->local_session->view_start[pane->number]);
1525                                                 w = Units::to_int64((mwindow->edl->local_session->get_selectionend() -
1526                                                         mwindow->edl->local_session->get_selectionstart()) *
1527                                                         mwindow->edl->session->sample_rate /
1528                                                         mwindow->edl->local_session->zoom_sample);
1529                                         }
1530 // Put it in a new plugin set determined by an edit boundary
1531                                         else if(mwindow->session->edit_highlighted) {
1532                                                 int64_t temp_y, temp_h;
1533                                                 edit_dimensions(mwindow->session->edit_highlighted,
1534                                                         x, temp_y, w, temp_h);
1535                                         }
1536 // Put it at the beginning of the track in a new plugin set
1537                                 }
1538
1539                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1540                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1541 //printf("TrackCanvas::draw_highlighting 1\n");
1542                                         draw_box = 1;
1543                                 }
1544                         }
1545                         break;
1546
1547                 case DRAG_ASSET:
1548                         if(mwindow->session->track_highlighted) {
1549 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1550
1551 //                              if(MWindowGUI::visible(y, y + h, 0, get_h()))
1552 //                              {
1553                                         draw_paste_destination();
1554 //                              }
1555                         }
1556                         break;
1557
1558                 case DRAG_EDIT:
1559                         if(mwindow->session->track_highlighted) {
1560 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1561 //
1562 //                              if(MWindowGUI::visible(y, y + h, 0, get_h())) {
1563                                         draw_paste_destination();
1564 //                              }
1565                         }
1566                         break;
1567                 case DRAG_GROUP:
1568                         if( mwindow->session->track_highlighted && mwindow->session->drag_group &&
1569                             mwindow->session->drag_group_edit && mwindow->session->drag_group_first_track ) {
1570                                 Track *track = mwindow->session->track_highlighted;
1571                                 EDL *drag_group = mwindow->session->drag_group;
1572                                 Track *edit_track = mwindow->session->drag_group_edit->track;
1573                                 Track *drag_track = mwindow->session->drag_group_first_track;
1574                                 while( track && edit_track && edit_track != drag_track ) {
1575                                         edit_track = edit_track->previous;
1576                                         track = track->previous;
1577                                 }
1578                                 int cx = get_cursor_x();
1579                                 double cur_pos = mwindow->session->drag_group_position;
1580                                 double new_pos = mwindow->edl->get_cursor_position(cx, pane->number);
1581                                 new_pos -= mwindow->session->drag_position - cur_pos;
1582                                 new_pos = mwindow->edl->align_to_frame(new_pos, 0);
1583                                 double drop_position = new_pos;
1584                                 int color = GREEN;
1585                                 int ret = test_track_group(drag_group, track, new_pos);
1586                                 if( ret < 0 )
1587                                         color = ORANGE;
1588                                 else if( !ret ) {
1589                                         if( new_pos != drop_position ) {
1590                                                 double pos = new_pos;
1591                                                 pos += mwindow->session->drag_position - cur_pos;
1592                                                 cx = mwindow->edl->get_position_cursorx(pos, pane->number);
1593                                                 ret = test_track_group(drag_group, track, new_pos);
1594                                         }
1595                                         color = ret > 0 ? YELLOW : shift_down() ? RED : BLUE;
1596                                 }
1597                                 track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1598                                 int dx = cx - mwindow->session->drag_origin_x;
1599                                 int dy = y - mwindow->session->drag_origin_y;
1600                                 draw_selected_edits(mwindow->edl, dx, dy, color, -1);
1601                         }
1602                         break;
1603 // Dragging an effect from the timeline
1604                 case DRAG_AEFFECT_COPY:
1605                 case DRAG_VEFFECT_COPY:
1606                         if( (mwindow->session->plugin_highlighted || mwindow->session->track_highlighted) &&
1607                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
1608                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1609                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
1610                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1611
1612                                 int64_t plugin_position = -1;
1613                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
1614                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
1615                                 Track *hi_track = mwindow->session->track_highlighted;
1616                                 Edit *hi_edit = mwindow->session->edit_highlighted;
1617 // Put it into a silence zone
1618                                 if( hi_plugin_set && hi_plugin_set->last &&
1619                                     hi_plugin_set->track == drag_plugin->track ) {
1620                                         plugin_dimensions((Plugin *)hi_plugin_set->last, x, y, w, h);
1621                                         plugin_position = drop_plugin_position(hi_plugin_set, drag_plugin);
1622                                         hi_track = drag_plugin->track;
1623                                 }
1624                                 else if( hi_track ) {
1625 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
1626                                         track_dimensions(hi_track, x, y, w, h);
1627                                         plugin_position = hi_edit ? hi_edit->startproject : 0;
1628                                 }
1629
1630                                 if( plugin_position < 0 ) break;
1631
1632 // Calculate length of plugin based on data type of track and units
1633                                 double zoom_scale = (double)mwindow->edl->session->sample_rate /
1634                                                 mwindow->edl->local_session->zoom_sample;
1635                                 x = Units::round(hi_track->from_units(plugin_position) * zoom_scale) -
1636                                                 mwindow->edl->local_session->view_start[pane->number];
1637                                 w = Units::round(hi_track->from_units(drag_plugin->length) * zoom_scale);
1638
1639                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1640                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1641                                         draw_box = 1;
1642                                 }
1643                         }
1644                         break;
1645
1646                 case DRAG_PLUGINKEY:
1647                         if(mwindow->session->plugin_highlighted &&
1648                            mwindow->session->current_operation == DRAG_PLUGINKEY)
1649                         {
1650 // Just highlight the plugin
1651                                 plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1652
1653                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1654                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1655                                 {
1656                                         draw_box = 1;
1657                                 }
1658                         }
1659                         break;
1660
1661                 case DRAG_SPEED:
1662                         draw_speed_highlight();
1663                         break;
1664         }
1665
1666         if( draw_box )
1667                 draw_highlight_rectangle(x, y, w, h);
1668
1669         draw_selected_edits(mwindow->edl, 0, 0, GREEN+BLUE, RED);
1670 }
1671
1672 void TrackCanvas::draw_speed_highlight()
1673 {
1674         FloatAuto *drag_speed = (FloatAuto*)mwindow->session->drag_auto;
1675         if( !drag_speed ) return;
1676         draw_speed_track(drag_speed->autos->track);
1677         ArrayList<Auto*> &speed_gang = *mwindow->session->drag_auto_gang;
1678         for( int i=0, sz=speed_gang.size(); i<sz; ++i ) {
1679                 Track *track = speed_gang[i]->autos->track;
1680                 if( track->is_hidden() ) continue;
1681                 draw_speed_track(track);
1682         }
1683 }
1684
1685 void TrackCanvas::draw_speed_track(Track *track)
1686 {
1687         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1688                 int64_t x, y, w, h;
1689                 edit_dimensions(edit, x, y, w, h);
1690                 if( !MWindowGUI::visible(x, x + w, 0, get_w()) ) continue;
1691                 if( !MWindowGUI::visible(y, y + h, 0, get_h()) ) continue;
1692                 int color = 0xc00cc0;
1693                 set_color(color);
1694                 set_opaque();
1695                 draw_selected(x, y, w, h);
1696         }
1697 }
1698
1699 // x does not reliably draw a really big rectangle
1700 void TrackCanvas::draw_selected(int x, int y, int w, int h)
1701 {
1702         int x1 = bmax(x, 0), x2 = bmin(x+w, get_w());
1703         if( x1 > x2 ) return;
1704         int y1 = bmax(y, 0), y2 = bmin(y+h, get_h());
1705         if( y1 > y2 ) return;
1706         if( x   >= 0 && x   < get_w() ) draw_line(x,y1,   x,y2);
1707         if( x+w >= 0 && x+w < get_w() ) draw_line(x+w,y1, x+w,y2);
1708         if( y   >= 0 && y   < get_h() ) draw_line(x1,y,   x2,y);
1709         if( y+h >= 0 && y+h < get_h() ) draw_line(x1,y+h, x2,y+h);
1710 }
1711
1712 void TrackCanvas::draw_selected_edits(EDL *edl, int dx, int dy, int color0, int color1)
1713 {
1714         int dropping = 0;
1715         for( Track *track=edl->tracks->first; track; track=track->next ) {
1716                 if( track->is_hidden() ) continue;
1717                 if( !track->is_armed() && color1 < 0 ) {
1718                         if( dropping )
1719                                 dy -= track->vertical_span(mwindow->theme);
1720                         continue;
1721                 }
1722                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1723                         if( !edit->is_selected ) continue;
1724                         dropping = 1;
1725                         int64_t x, y, w, h;
1726                         edit_dimensions(edit, x, y, w, h);
1727                         x += dx;  y += dy;
1728                         if( !MWindowGUI::visible(x, x + w, 0, get_w()) ) continue;
1729                         if( !MWindowGUI::visible(y, y + h, 0, get_h()) ) continue;
1730                         set_opaque();
1731                         int inner = color1 < 0 ? color0 : !edit->group_id ? color0 :
1732                                 mwindow->get_group_color(edit->group_id);
1733                         int outer = color1 < 0 ? color0 : !edit->group_id ? color1 : inner;
1734                         set_color(track->is_armed() ? inner : outer);
1735                         draw_selected(x, y, w, h);
1736                         set_color(track->is_armed() ? outer : inner);
1737                         draw_selected(x-1, y-1, w+2, h+2);
1738                         draw_selected(x-2, y-2, w+1, h+1);
1739                 }
1740         }
1741 }
1742
1743 void TrackCanvas::draw_plugins()
1744 {
1745         char string[BCTEXTLEN];
1746         int current_on = 0;
1747         int current_show = 0;
1748         int current_preset = 0;
1749
1750         for(int i = 0; i < plugin_on_toggles.total; i++)
1751                 plugin_on_toggles.values[i]->in_use = 0;
1752         for(int i = 0; i < plugin_show_toggles.total; i++)
1753                 plugin_show_toggles.values[i]->in_use = 0;
1754         for(int i = 0; i < preset_edit_buttons.total; i++)
1755                 plugin_show_toggles.values[i]->in_use = 0;
1756
1757
1758         for(Track *track = mwindow->edl->tracks->first;
1759                 track;
1760                 track = track->next)
1761         {
1762                 if( track->is_hidden() ) continue;
1763                 if(track->expand_view)
1764                 {
1765                         for(int i = 0; i < track->plugin_set.total; i++)
1766                         {
1767                                 PluginSet *pluginset = track->plugin_set.values[i];
1768
1769                                 for(Plugin *plugin = (Plugin*)pluginset->first; plugin; plugin = (Plugin*)plugin->next)
1770                                 {
1771                                         int64_t total_x, y, total_w, h;
1772                                         plugin_dimensions(plugin, total_x, y, total_w, h);
1773
1774                                         if(MWindowGUI::visible(total_x, total_x + total_w, 0, get_w()) &&
1775                                                 MWindowGUI::visible(y, y + h, 0, get_h()) &&
1776                                                 plugin->plugin_type != PLUGIN_NONE)
1777                                         {
1778                                                 int x = total_x, w = total_w, left_margin = 5;
1779                                                 int right_margin = 5;
1780                                                 if(x < 0)
1781                                                 {
1782                                                         w -= -x;
1783                                                         x = 0;
1784                                                 }
1785                                                 if(w + x > get_w()) w -= (w + x) - get_w();
1786
1787                                                 draw_3segmenth(x,
1788                                                         y,
1789                                                         w,
1790                                                         total_x,
1791                                                         total_w,
1792                                                         mwindow->theme->get_image("plugin_bg_data"),
1793                                                         0);
1794                                                 set_color(mwindow->theme->title_color);
1795                                                 set_font(mwindow->theme->title_font);
1796                                                 plugin->calculate_title(string, 0);
1797
1798 // Truncate string to int64_test visible in background
1799                                                 int len = strlen(string), j;
1800                                                 for(j = len; j >= 0; j--)
1801                                                 {
1802                                                         if(left_margin + get_text_width(mwindow->theme->title_font, string) > w)
1803                                                         {
1804                                                                 string[j] = 0;
1805                                                         }
1806                                                         else
1807                                                                 break;
1808                                                 }
1809
1810 // Justify the text on the left boundary of the edit if it is visible.
1811 // Otherwise justify it on the left side of the screen.
1812                                                 int64_t text_x = total_x + left_margin;
1813                                                 int64_t text_w = get_text_width(mwindow->theme->title_font, string, strlen(string));
1814                                                 text_x = MAX(left_margin, text_x);
1815                                                 draw_text(text_x,
1816                                                         y + get_text_ascent(mwindow->theme->title_font) + 2,
1817                                                         string,
1818                                                         strlen(string),
1819                                                         0);
1820                                                 int64_t min_x = total_x + text_w;
1821
1822
1823 // Update plugin toggles
1824                                                 int toggle_x = total_x + total_w;
1825                                                 int toggle_y = y;
1826                                                 toggle_x = MIN(get_w() - right_margin, toggle_x);
1827
1828 // On toggle
1829                                                 toggle_x -= PluginOn::calculate_w(mwindow) + xS(10);
1830                                                 if(toggle_x > min_x)
1831                                                 {
1832                                                         if(current_on >= plugin_on_toggles.total)
1833                                                         {
1834                                                                 PluginOn *plugin_on = new PluginOn(mwindow, toggle_x, toggle_y, plugin);
1835                                                                 add_subwindow(plugin_on);
1836                                                                 plugin_on_toggles.append(plugin_on);
1837                                                         }
1838                                                         else
1839                                                         {
1840                                                                 plugin_on_toggles.values[current_on]->update(toggle_x, toggle_y, plugin);
1841                                                         }
1842                                                         current_on++;
1843                                                 }
1844
1845 // Toggles for standalone plugins only
1846                                                 if(plugin->plugin_type == PLUGIN_STANDALONE)
1847                                                 {
1848 // Show
1849                                                         toggle_x -= PluginShow::calculate_w(mwindow) + xS(10);
1850                                                         if(toggle_x > min_x)
1851                                                         {
1852                                                                 if(current_show >= plugin_show_toggles.total)
1853                                                                 {
1854                                                                         PluginShow *plugin_show = new PluginShow(mwindow, toggle_x, toggle_y, plugin);
1855                                                                         add_subwindow(plugin_show);
1856                                                                         plugin_show_toggles.append(plugin_show);
1857                                                                 }
1858                                                                 else
1859                                                                 {
1860                                                                         plugin_show_toggles.values[current_show]->update(toggle_x, toggle_y, plugin);
1861                                                                 }
1862                                                                 current_show++;
1863                                                         }
1864                                                         toggle_x -= PluginPresetEdit::calculate_w(mwindow) + xS(10);
1865                                                         if(toggle_x > min_x)
1866                                                         {
1867                                                                 if(current_preset >= preset_edit_buttons.total)
1868                                                                 {
1869                                                                         PluginPresetEdit *preset_edit = new PluginPresetEdit(mwindow, toggle_x, toggle_y, plugin);
1870                                                                         add_subwindow(preset_edit);
1871                                                                         preset_edit_buttons.append(preset_edit);
1872                                                                 }
1873                                                                 else
1874                                                                 {
1875                                                                         preset_edit_buttons.values[current_preset]->update(toggle_x, toggle_y, plugin);
1876                                                                 }
1877                                                                 current_preset++;
1878                                                         }
1879                                                 }
1880                                         }
1881                                 }
1882                         }
1883                 }
1884         }
1885
1886 // Remove unused toggles
1887
1888         while(current_preset < preset_edit_buttons.total)
1889         {
1890                 preset_edit_buttons.remove_object_number(current_preset);
1891         }
1892         while(current_show < plugin_show_toggles.total)
1893         {
1894                 plugin_show_toggles.remove_object_number(current_show);
1895         }
1896
1897         while(current_on < plugin_on_toggles.total)
1898         {
1899                 plugin_on_toggles.remove_object_number(current_on);
1900         }
1901 }
1902
1903 void TrackCanvas::refresh_plugintoggles()
1904 {
1905         for(int i = 0; i < plugin_on_toggles.total; i++)
1906         {
1907                 PluginOn *on = plugin_on_toggles.values[i];
1908                 on->reposition_window(on->get_x(), on->get_y());
1909         }
1910         for(int i = 0; i < plugin_show_toggles.total; i++)
1911         {
1912                 PluginShow *show = plugin_show_toggles.values[i];
1913                 show->reposition_window(show->get_x(), show->get_y());
1914         }
1915         for(int i = 0; i < preset_edit_buttons.total; i++)
1916         {
1917                 PluginPresetEdit *preset_edit = preset_edit_buttons.values[i];
1918                 preset_edit->reposition_window(preset_edit->get_x(), preset_edit->get_y());
1919         }
1920 }
1921
1922 void TrackCanvas::draw_hard_edges()
1923 {
1924         if( !mwindow->edl->session->auto_conf->hard_edges )
1925                 return;
1926         int64_t x, y, w, h;
1927
1928         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1929                 if( track->is_hidden() ) continue;
1930                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1931                         if( !edit->hard_left && !edit->hard_right ) continue;
1932                         edit_dimensions(edit, x, y, w, h);
1933                         if( !MWindowGUI::visible(x, x + w, 0, get_w()) ||
1934                             !MWindowGUI::visible(y, y + h, 0, get_h()) ) continue;
1935                         set_color(GREEN);
1936                         set_opaque();
1937                         int y1 = y;
1938                         if( track->show_titles() )
1939                                 y1 += mwindow->theme->get_image("title_bg_data")->get_h();
1940                         if( track->show_assets() )
1941                                 y1 += track->data_h;
1942                         if( y1 == y )
1943                                 y1 += mwindow->theme->title_h;
1944                         if( edit->hard_left ) {
1945                                 ArrayList<int> xpt, ypt;
1946                                 xpt.append(x);              ypt.append(y1);
1947                                 xpt.append(x+HANDLE_W);     ypt.append(y1);
1948                                 xpt.append(x);              ypt.append(y1-HANDLE_H);
1949                                 fill_polygon(&xpt, &ypt);
1950                         }
1951                         if( edit->hard_right ) {
1952                                 ArrayList<int> xpt, ypt;   int x1 = x+w-1;
1953                                 xpt.append(x1);            ypt.append(y1);
1954                                 xpt.append(x1-HANDLE_W);   ypt.append(y1);
1955                                 xpt.append(x1);            ypt.append(y1-HANDLE_H);
1956                                 fill_polygon(&xpt, &ypt);
1957                         }
1958                 }
1959         }
1960 }
1961
1962 void TrackCanvas::draw_inout_points()
1963 {
1964 }
1965
1966
1967 void TrackCanvas::draw_drag_handle()
1968 {
1969         if( mwindow->session->current_operation != DRAG_EDITHANDLE2 &&
1970             mwindow->session->current_operation != DRAG_PLUGINHANDLE2 &&
1971             mwindow->session->current_operation != DRAG_TRANSNHANDLE2 ) return;
1972         int64_t pixel1 = Units::round(mwindow->session->drag_position *
1973                 mwindow->edl->session->sample_rate /
1974                 mwindow->edl->local_session->zoom_sample -
1975                 mwindow->edl->local_session->view_start[pane->number]);
1976         set_color(!snapped ? GREEN : (snapped=0, YELLOW));
1977         set_inverse();
1978         draw_line(pixel1, 0, pixel1, get_h());
1979         set_opaque();
1980
1981         if( mwindow->session->current_operation != DRAG_EDITHANDLE2 ) return;
1982         if( !mwindow->session->drag_edit ) return;
1983         int group_id = mwindow->session->drag_edit->group_id;
1984         if( !group_id ) return;
1985         int64_t dx, dy, dw, dh;
1986         edit_dimensions(mwindow->session->drag_edit, dx, dy, dw, dh);
1987         int drag_handle = mwindow->session->drag_handle;
1988         int64_t pixel0 = !drag_handle ? dx : dx + dw;
1989         int delta = pixel1 - pixel0;
1990         set_color(LTPURPLE);
1991         set_line_width(3);
1992
1993         for( Track *track=mwindow->edl->tracks->first; track; track=track->next ) {
1994                 if( track->is_hidden() ) continue;
1995                 Edit *left = 0, *right = 0;
1996                 double start = DBL_MAX, end = DBL_MIN;
1997                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1998                         if( edit->group_id != group_id ) continue;
1999                         double edit_start = edit->track->from_units(edit->startproject);
2000                         if( edit_start < start ) { start = edit_start;  left = edit; }
2001                         double edit_end = edit->track->from_units(edit->startproject+edit->length);
2002                         if( edit_end > end ) { end = edit_end;  right = edit; }
2003                 }
2004                 Edit *edit = !drag_handle ? left : right;
2005                 if( !edit ) continue;
2006                 Indexable *idxbl = edit->asset;
2007                 if( !idxbl ) idxbl = edit->nested_edl;
2008                 int can_drag = idxbl ? 1 : 0;
2009                 if( drag_handle ) {
2010                         int64_t source_len = !idxbl ? -1 :
2011                                 edit->track->data_type == TRACK_AUDIO ?
2012                                         idxbl->get_audio_samples() :
2013                                 edit->track->data_type == TRACK_VIDEO ?
2014                                         idxbl->get_video_frames() : -1;
2015                         int64_t speed_start = edit->startproject;
2016                         int64_t speed_end = speed_start + edit->length;
2017                         int64_t speed_length = track->speed_length(speed_start, speed_end);
2018                         if( edit->startsource + speed_length >= source_len )
2019                                 can_drag = 0;
2020                 }
2021                 else if( !edit->startsource )
2022                         can_drag = 0;
2023                 int64_t x, y, w, h;
2024                 edit_dimensions(edit, x, y, w, h);
2025                 if( y+h < 0 || y >= get_h() ) continue;
2026                 int edge_x = !drag_handle ? x : x + w;
2027                 int edge_y = y + h/2, xs10 = xS(10), ys10 = yS(10);
2028                 if( edge_x >= 0 && edge_x < get_w() ) {
2029                         if( !can_drag ) {
2030                                 draw_line(edge_x-xs10,edge_y-ys10, edge_x+xs10,edge_y+ys10);
2031                                 draw_line(edge_x-xs10,edge_y+ys10, edge_x+xs10,edge_y-ys10);
2032                         }
2033                         else if( !drag_handle ) {
2034                                 draw_line(edge_x+xs10,edge_y-ys10, edge_x,edge_y);
2035                                 draw_line(edge_x+xs10,edge_y+ys10, edge_x,edge_y);
2036                         }
2037                         else {
2038                                 draw_line(edge_x,edge_y, edge_x-xs10,edge_y-ys10);
2039                                 draw_line(edge_x,edge_y, edge_x-xs10,edge_y+ys10);
2040                         }
2041                 }
2042                 edge_x += delta;
2043                 if( edge_x >= 0 && edge_x < get_w() ) {
2044                         draw_line(edge_x, y, edge_x, y+h);
2045                 }
2046         }
2047         set_line_width(1);
2048 }
2049
2050
2051 void TrackCanvas::draw_transitions()
2052 {
2053         int64_t x, y, w, h;
2054         if( !mwindow->edl->session->auto_conf->transitions ) return;
2055
2056         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
2057                 if( track->is_hidden() ) continue;
2058                 if( !track->show_transitions() ) continue;
2059
2060                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
2061                         if(!edit->transition) continue;
2062                         edit_dimensions(edit, x, y, w, h);
2063                         int strip_x = x, edit_y = y;
2064                         get_transition_coords(edit, x, y, w, h);
2065                         int strip_y = y - mwindow->theme->get_image("plugin_bg_data")->get_h();
2066                         if( track->show_assets() && track->show_titles() )
2067                                 edit_y += mwindow->theme->get_image("title_bg_data")->get_h();
2068                         if( strip_y < edit_y ) strip_y = edit_y;
2069
2070                         int strip_w = Units::round(edit->track->from_units(edit->transition->length) *
2071                                 mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample);
2072                         if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
2073                             MWindowGUI::visible(y, y + h, 0, get_h()) ) {
2074                                 PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
2075                                                 track->data_type);
2076                                 if( !server ) continue;
2077                                 VFrame *picon = server->get_picon();
2078                                 if( !picon ) continue;
2079                                 int picon_w = picon->get_w(), picon_h = picon->get_h();
2080                                 int track_h = edit->track->vertical_span(mwindow->theme);
2081                                 if( picon_h > track_h ) picon_h = track_h;
2082                                 draw_vframe(picon, x, y, w, h, 0, 0, picon_w, picon_h);
2083                         }
2084                         if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
2085                                 MWindowGUI::visible(strip_y, strip_y + h, 0, get_h())) {
2086                                 int x = strip_x, w = strip_w;
2087                                 if( x < 0 ) {  w -= -x;  x = 0; }
2088                                 if( w + x > get_w() ) w -= (w + x) - get_w();
2089
2090                                 draw_3segmenth( x, strip_y, w, strip_x, strip_w,
2091                                         mwindow->theme->get_image("plugin_bg_data"), 0);
2092                         }
2093                 }
2094         }
2095 }
2096
2097 void TrackCanvas::draw_loop_points()
2098 {
2099 //printf("TrackCanvas::draw_loop_points 1\n");
2100         if(mwindow->edl->local_session->loop_playback)
2101         {
2102 //printf("TrackCanvas::draw_loop_points 2\n");
2103                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
2104                         mwindow->edl->session->sample_rate /
2105                         mwindow->edl->local_session->zoom_sample -
2106                         mwindow->edl->local_session->view_start[pane->number]);
2107 //printf("TrackCanvas::draw_loop_points 3\n");
2108
2109                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
2110                 {
2111                         set_color(GREEN);
2112                         draw_line(x, 0, x, get_h());
2113                 }
2114 //printf("TrackCanvas::draw_loop_points 4\n");
2115
2116                 x = Units::round(mwindow->edl->local_session->loop_end *
2117                         mwindow->edl->session->sample_rate /
2118                         mwindow->edl->local_session->zoom_sample -
2119                         mwindow->edl->local_session->view_start[pane->number]);
2120 //printf("TrackCanvas::draw_loop_points 5\n");
2121
2122                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
2123                 {
2124                         set_color(GREEN);
2125                         draw_line(x, 0, x, get_h());
2126                 }
2127 //printf("TrackCanvas::draw_loop_points 6\n");
2128         }
2129 //printf("TrackCanvas::draw_loop_points 7\n");
2130 }
2131
2132 void TrackCanvas::draw_brender_range()
2133 {
2134         if( !mwindow->preferences->use_brender || !mwindow->brender_active ) return;
2135         if( mwindow->edl->session->brender_start >= mwindow->edl->session->brender_end ) return;
2136         if( mwindow->edl->session->brender_end > 0 )
2137         {
2138                 int64_t x1 = Units::round(mwindow->edl->session->brender_start *
2139                         mwindow->edl->session->sample_rate /
2140                         mwindow->edl->local_session->zoom_sample -
2141                         mwindow->edl->local_session->view_start[pane->number]);
2142                 if(MWindowGUI::visible(x1, x1 + 1, 0, get_w()))
2143                 {
2144                         set_color(RED);
2145                         draw_line(x1, 0, x1, get_h());
2146                 }
2147                 int64_t x2 = Units::round(mwindow->edl->session->brender_end *
2148                         mwindow->edl->session->sample_rate /
2149                         mwindow->edl->local_session->zoom_sample -
2150                         mwindow->edl->local_session->view_start[pane->number]);
2151
2152                 if(MWindowGUI::visible(x2, x2 + 1, 0, get_w()))
2153                 {
2154                         set_color(RED);
2155                         draw_line(x2, 0, x2, get_h());
2156                 }
2157         }
2158 }
2159
2160 // The operations which correspond to each automation type
2161 int TrackCanvas::auto_operations[AUTOMATION_TOTAL] =
2162 {
2163         DRAG_MUTE,
2164         DRAG_CAMERA_X,
2165         DRAG_CAMERA_Y,
2166         DRAG_CAMERA_Z,
2167         DRAG_PROJECTOR_X,
2168         DRAG_PROJECTOR_Y,
2169         DRAG_PROJECTOR_Z,
2170         DRAG_FADE,
2171         DRAG_PAN,
2172         DRAG_MODE,
2173         DRAG_MASK,
2174         DRAG_SPEED
2175 };
2176
2177 // The buttonpress operations, so nothing changes unless the mouse moves
2178 // a certain amount.  This allows the keyframe to be used to position the
2179 // insertion point without moving itself.
2180 static int pre_auto_operations[AUTOMATION_TOTAL] =
2181 {
2182         DRAG_MUTE,
2183         DRAG_CAMERA_X,
2184         DRAG_CAMERA_Y,
2185         DRAG_CAMERA_Z,
2186         DRAG_PROJECTOR_X,
2187         DRAG_PROJECTOR_Y,
2188         DRAG_PROJECTOR_Z,
2189         DRAG_FADE,
2190         DRAG_PAN_PRE,
2191         DRAG_MODE_PRE,
2192         DRAG_MASK_PRE,
2193         DRAG_SPEED
2194 };
2195
2196
2197 int TrackCanvas::do_keyframes(int cursor_x,
2198         int cursor_y,
2199         int draw,
2200         int buttonpress,
2201         int &new_cursor,
2202         int &update_cursor,
2203         int &rerender)
2204 {
2205 // Note: button 3 (right mouse button) is not eaten to allow
2206 // track context menu to appear
2207         int result = 0;
2208         EDLSession *session = mwindow->edl->session;
2209         int gang = session->gang_tracks != GANG_NONE || get_double_click() ? 1 : 0;
2210
2211         static BC_Pixmap *auto_pixmaps[AUTOMATION_TOTAL] =
2212         {
2213                 0, 0, 0, 0, 0, 0, 0, 0,
2214                 pankeyframe_pixmap,
2215                 modekeyframe_pixmap,
2216                 maskkeyframe_pixmap,
2217                 0,
2218         };
2219
2220
2221
2222         for(Track *track = mwindow->edl->tracks->first;
2223                 track && !result;
2224                 track = track->next) {
2225                 if( track->is_hidden() ) continue;
2226                 Auto *auto_keyframe = 0;
2227                 Automation *automation = track->automation;
2228
2229
2230 // Handle keyframes in reverse drawing order if a button press
2231                 int start = 0;
2232                 int end = AUTOMATION_TOTAL;
2233                 int step = 1;
2234                 if(buttonpress)
2235                 {
2236                         start = AUTOMATION_TOTAL - 1;
2237                         end = -1;
2238                         step = -1;
2239                 }
2240                 for(int i = start; i != end && !result; i += step)
2241                 {
2242 // Event not trapped and automation visible
2243                         Autos *autos = automation->autos[i];
2244                         if(!result && session->auto_conf->autos[i] && autos) {
2245                                 switch(i) {
2246                                 case AUTOMATION_MODE:
2247                                 case AUTOMATION_PAN:
2248                                 case AUTOMATION_MASK:
2249                                         result = do_autos(track, automation->autos[i],
2250                                                 cursor_x, cursor_y, draw,
2251                                                 buttonpress, auto_pixmaps[i],
2252                                                 auto_keyframe, rerender);
2253                                                 break;
2254
2255                                 default: {
2256                                         switch(autos->get_type()) {
2257                                         case Autos::AUTOMATION_TYPE_FLOAT: {
2258                                                 Automation automation(0, track);
2259                                                 int grouptype = automation.autogrouptype(i, track);
2260                                                 if( buttonpress && i == AUTOMATION_SPEED ) {
2261                                                         mwindow->speed_before();
2262                                                 }
2263
2264                                                 if(draw) // Do dropshadow
2265                                                         result = do_float_autos(track, autos,
2266                                                                 cursor_x, cursor_y, draw,
2267                                                                 buttonpress, 1, 1, MDGREY,
2268                                                                 auto_keyframe, grouptype);
2269                                                 result = do_float_autos(track, autos,
2270                                                         cursor_x, cursor_y, draw, buttonpress,
2271                                                         0, 0, GWindowGUI::auto_colors[i],
2272                                                         auto_keyframe, grouptype);
2273
2274                                                 if( !result && buttonpress && i == AUTOMATION_SPEED )
2275                                                         mwindow->speed_after(-1, 0);
2276                                                 if( result && buttonpress ) {
2277                                                         int color = GWindowGUI::auto_colors[i];
2278                                                         mwindow->gui->zoombar->update_autozoom(grouptype, color);
2279                                                 }
2280                                                 break; }
2281
2282                                         case Autos::AUTOMATION_TYPE_INT: {
2283                                                 if(draw) // Do dropshadow
2284                                                         result = do_int_autos(track, autos,
2285                                                                 cursor_x, cursor_y, draw,
2286                                                                 buttonpress, 1, 1, MDGREY,
2287                                                                 auto_keyframe);
2288                                                 result = do_int_autos(track, autos,
2289                                                         cursor_x, cursor_y, draw, buttonpress,
2290                                                         0, 0, GWindowGUI::auto_colors[i],
2291                                                         auto_keyframe);
2292                                                 break; }
2293                                         }
2294                                         break; }
2295                                 }
2296
2297                                 if(result)
2298                                 {
2299                                         if(mwindow->session->current_operation == auto_operations[i])
2300                                                 rerender = 1;
2301
2302 // printf("TrackCanvas::do_keyframes %d %d %d\n",
2303 // __LINE__,
2304 // mwindow->session->current_operation,
2305 // auto_operations[i]);
2306                                         if(buttonpress)
2307                                         {
2308                                                 if (buttonpress == 2 && auto_keyframe )
2309                                                 {
2310                                                         double position = track->from_units(auto_keyframe->position);
2311                                                         mwindow->edl->local_session->set_selectionstart(position);
2312                                                         mwindow->edl->local_session->set_selectionend(position);
2313                                                 }
2314                                                 if (buttonpress != 3)
2315                                                 {
2316                                                         if(i == AUTOMATION_FADE || i == AUTOMATION_SPEED)
2317                                                                 fill_ganged_autos(gang, 0, track,
2318                                                                         (FloatAuto*)mwindow->session->drag_auto);
2319                                                         mwindow->session->current_operation = pre_auto_operations[i];
2320                                                         update_drag_caption();
2321                                                         rerender = 1;
2322                                                 }
2323                                                 else if( auto_keyframe )
2324                                                 {
2325                                                         gui->keyframe_menu->update(automation,
2326                                                                 autos,
2327                                                                 auto_keyframe);
2328                                                         gui->keyframe_menu->activate_menu();
2329                                                         rerender = 1; // the position changes
2330                                                 }
2331                                                 else if( autos )
2332                                                 {
2333                                                         gui->keyframe_hide->update(autos);
2334                                                         gui->keyframe_hide->activate_menu();
2335                                                         rerender = 1; // the position changes
2336                                                 }
2337                                                 if(buttonpress == 1 && ctrl_down() &&
2338                                                    AUTOMATION_TYPE_FLOAT == autos->get_type())
2339                                                         rerender = 1; // special case: curve mode changed
2340                                         }
2341                                 }
2342                         }
2343                 }
2344
2345                 if(!result && session->auto_conf->plugins) {
2346                         Plugin *plugin;
2347                         KeyFrame *keyframe;
2348                         result = do_plugin_autos(track, cursor_x, cursor_y,
2349                                 draw, buttonpress, plugin, keyframe);
2350                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY) {
2351                                 rerender = 1;
2352                         }
2353                         if(result && (buttonpress == 1)) {
2354                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2355                                 update_drag_caption();
2356                                 rerender = 1;
2357                         }
2358                         else if (result && (buttonpress == 3)) {
2359                                 gui->keyframe_menu->update(plugin, keyframe);
2360                                 gui->keyframe_menu->activate_menu();
2361                                 rerender = 1; // the position changes
2362                         }
2363                 }
2364         }
2365
2366 // Final pass to trap event
2367         for(int i = 0; i < AUTOMATION_TOTAL; i++) {
2368                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2369                         mwindow->session->current_operation == auto_operations[i]) {
2370                         result = 1;
2371                         break;
2372                 }
2373         }
2374
2375         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2376                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE) {
2377                 result = 1;
2378         }
2379
2380         update_cursor = 1;
2381         if(result) {
2382                 new_cursor = UPRIGHT_ARROW_CURSOR;
2383         }
2384
2385         return result;
2386 }
2387
2388 void TrackCanvas::draw_keyframe_reticle()
2389 {
2390         int keyframe_hairline = mwindow->preferences->keyframe_reticle;
2391         if( keyframe_hairline == HAIRLINE_NEVER ) return;
2392
2393         int current_op = mwindow->session->current_operation, dragging = 0;
2394         for( int i=0; !dragging && i<AUTOMATION_TOTAL; ++i )
2395                 if( current_op == auto_operations[i] ) dragging = 1;
2396         if( dragging && !mwindow->session->drag_auto ) dragging = 0;
2397
2398         int autoidx = dragging && keyframe_hairline != HAIRLINE_ALWAYS ?
2399                 mwindow->session->drag_auto->autos->autoidx : -1;
2400
2401         if( get_buttonpress() == LEFT_BUTTON && dragging &&
2402             keyframe_hairline == HAIRLINE_DRAGGING ) {
2403                 draw_hairline(mwindow->session->drag_auto, RED, 1);
2404                 return;
2405         }
2406
2407         if( keyframe_hairline == HAIRLINE_ALWAYS || ( get_buttonpress() == MIDDLE_BUTTON &&
2408             keyframe_hairline == HAIRLINE_DRAGGING && dragging ) ) {
2409                 int show = dragging || keyframe_hairline == HAIRLINE_ALWAYS ? 1 : 0;
2410                 for( Track *track = mwindow->edl->tracks->first; track; track=track->next ) {
2411                         if( track->is_hidden() ) continue;
2412                         Automation *automation = track->automation;
2413                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2414                                 if( !mwindow->edl->session->auto_conf->autos[i] ) continue;
2415                                 // automation visible
2416                                 Autos *autos = automation->autos[i];
2417                                 if( !autos ) continue;
2418                                 if( autoidx >= 0 && autos->autoidx != autoidx ) continue;
2419                                 for( Auto *auto_keyframe=autos->first; auto_keyframe;
2420                                      auto_keyframe = auto_keyframe->next ) {
2421                                         draw_hairline(auto_keyframe, BLUE, show);
2422                                 }
2423                         }
2424                 }
2425
2426                 if( dragging )
2427                         draw_hairline(mwindow->session->drag_auto, RED, 1);
2428         }
2429 }
2430
2431 void TrackCanvas::draw_auto(Auto *current, int x, int y,
2432                 int center_pixel, int data_h)
2433 {
2434         int x1, y1, x2, y2;
2435
2436         x1 = x - HANDLE_W / 2;
2437         x2 = x + HANDLE_W / 2;
2438         y1 = center_pixel + y - HANDLE_W / 2;
2439         y2 = center_pixel + y + HANDLE_W / 2;
2440
2441         if( y1 < center_pixel - data_h/2 ) y1 = center_pixel - data_h/2;
2442         if( y2 > center_pixel + data_h/2 ) y2 = center_pixel + data_h/2;
2443
2444         draw_box(x1, y1, x2 - x1, y2 - y1);
2445 }
2446
2447
2448 // This draws lines for bezier in & out controls
2449 void TrackCanvas::draw_cropped_line(int x1,
2450         int y1,
2451         int x2,
2452         int y2,
2453         int min_y,
2454         int max_y)
2455 {
2456
2457
2458 // Don't care about x since it is clipped by the window.
2459 // Put y coords in ascending order
2460         if(y2 < y1) {
2461                 y2 ^= y1; y1 ^= y2; y2 ^= y1;
2462                 x2 ^= x1; x1 ^= x2; x2 ^= x1;
2463         }
2464
2465
2466
2467         double slope = (double)(x2 - x1) / (y2 - y1);
2468 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2469         if(y1 < min_y) {
2470                 x1 = (int)(x1 + (min_y - y1) * slope);
2471                 y1 = min_y;
2472         }
2473         else if(y1 >= max_y) {
2474                 x1 = (int)(x1 + (max_y - 1 - y1) * slope);
2475                 y1 = max_y - 1;
2476         }
2477
2478         if(y2 >= max_y) {
2479                 x2 = (int)(x2 + (max_y - 1 - y2) * slope);
2480                 y2 = max_y - 1;
2481         }
2482         else if(y2 < min_y) {
2483                 x2 = (int)(x2 + (min_y - y2) * slope);
2484                 y1 = min_y;
2485         }
2486
2487
2488 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2489         if( y1 >= min_y && y1 < max_y &&
2490             y2 >= min_y && y2 < max_y )
2491                 draw_line(x1, y1, x2, y2);
2492 }
2493
2494
2495 void TrackCanvas::draw_floatauto(FloatAuto *current,
2496                 int x_offset, int center_pixel, int data_h, int color,
2497                 double unit_start, double zoom_units, double yscale,
2498                 int autogrouptype)
2499 {
2500         double xx, yy;
2501         calculate_auto_position(1, &xx, &yy, 0, 0, 0, 0,
2502                 current, unit_start, zoom_units, yscale, autogrouptype);
2503         int x = (int)xx + x_offset;
2504         int x1 = x - HANDLE_W / 2; // Center
2505         int x2 = x + HANDLE_W / 2;
2506         int y = (int)yy + center_pixel;
2507         int y1 = y - HANDLE_H / 2;
2508         int y2 = y + HANDLE_H / 2;
2509         int ymin = center_pixel - data_h / 2;
2510         int ymax = center_pixel + data_h / 2;
2511         CLAMP(y1, ymin, ymax);
2512         CLAMP(y2, ymin, ymax);
2513
2514         if( y2-1 > y1 ) {
2515                 if( current->curve_mode == FloatAuto::LINEAR ) {
2516                         draw_box(x1, y1, x2 - x1, y2 - y1);
2517                 }
2518                 else {
2519                         ArrayList<int> polygon_x;
2520                         ArrayList<int> polygon_y;
2521                         polygon_x.append((x1 + x2) / 2 + 1);
2522                         polygon_y.append(y1 + 1);
2523                         polygon_x.append(x2 + 1);
2524                         polygon_y.append((y1 + y2) / 2 + 1);
2525                         polygon_x.append((x1 + x2) / 2 + 1);
2526                         polygon_y.append(y2 + 1);
2527                         polygon_x.append(x1 + 1);
2528                         polygon_y.append((y1 + y2) / 2 + 1);
2529                         fill_polygon(&polygon_x, &polygon_y);
2530                 }
2531         }
2532
2533 // show bezier control points (only) if this
2534 // floatauto doesn't adjust it's tangents automatically
2535         if(current->curve_mode != FloatAuto::FREE &&
2536            current->curve_mode != FloatAuto::TFREE &&
2537            current->curve_mode != FloatAuto::BUMP)
2538                 return;
2539         double in_xx, in_yy, out_xx, out_yy;
2540         calculate_auto_position(1, &xx, &yy, &in_xx, &in_yy, 0, 0,
2541                 current, unit_start, zoom_units, yscale, autogrouptype);
2542         int ix = xx, iy = yy, in_x = in_xx, in_y = in_yy;
2543         if( in_x != ix )
2544                 draw_floatauto_ctrlpoint(ix, iy, in_x, in_y, center_pixel, data_h, color);
2545         calculate_auto_position(0, &xx, &yy, 0, 0, &out_xx, &out_yy,
2546                 current, unit_start, zoom_units, yscale, autogrouptype);
2547         int ox = xx, oy = yy, out_x = out_xx, out_y = out_yy;
2548         if( out_x != ox )
2549                 draw_floatauto_ctrlpoint(ox, oy, out_x, out_y, center_pixel, data_h, color);
2550         if( current->curve_mode == FloatAuto::BUMP && iy != oy )
2551                 draw_bline(ix, iy+center_pixel, ox, oy+center_pixel);
2552 }
2553
2554 inline int quantize(float f) { return (int)floor(f + 0.5); }
2555
2556 inline void TrackCanvas::draw_floatauto_ctrlpoint(
2557                 int x, int y, int cp_x, int cp_y, int center_pixel,
2558                 int data_h, int color)
2559 // draw the tangent and a handle for given bézier ctrl point
2560 {
2561         bool handle_visible = (abs(cp_y) <= data_h/2);
2562
2563         float slope = (float)(cp_y - y)/(cp_x - x);
2564         CLAMP(cp_y, -data_h/2, data_h/2);
2565         if(slope != 0)
2566                 cp_x = x + quantize((cp_y - y) / slope);
2567
2568         y    += center_pixel;
2569         cp_y += center_pixel;
2570
2571         // drawing the tangent as a dashed line...
2572         int const dash = HANDLE_W;
2573         int const gap  = HANDLE_W / 2;
2574         float sx = 3 * (cp_x - x) / 4.;
2575         float ex = 0;
2576
2577         // q is the x displacement for a unit line of slope
2578         float q = (sx > 0 ? 1 : -1) / sqrt(1 + slope * slope);
2579
2580         float dist = 1/q * sx;
2581         if( dist > dash )
2582                 ex = sx - q * dash;
2583
2584         set_color(color);
2585         do {
2586                 float sy = slope * sx, ey = slope * ex;
2587                 draw_line(quantize(sx + x), quantize(sy + y), quantize(ex + x), quantize(ey + y));
2588                 sx = ex - q * gap;
2589                 ex = sx - q * dash;
2590         } while(q*ex > 0);
2591
2592         if(handle_visible)
2593         {
2594                 int r = HANDLE_W / 2;
2595                 int cp_x1 = cp_x - r;
2596                 int cp_y1 = cp_y - r;
2597                 set_color(BLACK);
2598                 draw_disc(cp_x1, cp_y1, 2 * r, 2 * r);
2599                 set_color(color);
2600                 draw_circle(cp_x1, cp_y1, 2 * r, 2 * r);
2601         }
2602 }
2603
2604
2605 int TrackCanvas::test_auto(Auto *current,
2606         int x, int y, int center_pixel, int data_h,
2607         int cursor_x, int cursor_y, int buttonpress)
2608 {
2609         int x1, y1, x2, y2;
2610         int result = 0;
2611
2612         x1 = x - HANDLE_W / 2;
2613         x2 = x + HANDLE_W / 2;
2614         y1 = center_pixel + y - HANDLE_H / 2;
2615         y2 = center_pixel + y + HANDLE_H / 2;
2616         int ymin = center_pixel - data_h/2;
2617         int ymax = center_pixel + data_h/2;
2618         CLAMP(y1, ymin, ymax);
2619         CLAMP(y2, ymin, ymax);
2620
2621         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2622         {
2623                 if(buttonpress && buttonpress != 3)
2624                 {
2625                         mwindow->session->drag_auto = current;
2626                         mwindow->session->drag_start_percentage = (float)((IntAuto*)current)->value;
2627                         // Toggle Autos don't respond to vertical zoom, they always show up
2628                         // with "on" == 100% == line on top
2629                         mwindow->session->drag_start_position = current->position;
2630                         mwindow->session->drag_origin_x = cursor_x;
2631                         mwindow->session->drag_origin_y = cursor_y;
2632                 }
2633                 result = 1;
2634         }
2635
2636         if(buttonpress && buttonpress != 3 && result)
2637         {
2638 //printf("TrackCanvas::test_auto %d\n", __LINE__);
2639                 mwindow->undo->update_undo_before();
2640         }
2641
2642         return result;
2643 }
2644
2645 // some Helpers for test_floatauto(..)
2646 // and for dragging the tangents/ctrl points
2647 inline float test_curve_line( int x0, int y0, int ctrl_x, int ctrl_y,
2648         float cursor_x, float cursor_y)
2649 {
2650 // Control point switched off?
2651         if( x0 == ctrl_x ) return 0.0;
2652         double x1 = ctrl_x-x0, y1 = ctrl_y-y0;
2653         double x = cursor_x-x0, y = cursor_y-y0;
2654 // wrong side of ctrl handle
2655         if( x*x1 < 0 ) return 0.0;
2656 // outside handle radius
2657         if( (x*x + y*y) > (x1*x1 + y1*y1) ) return 0;
2658         double xx1 = x1*x1, yy1 = y1*y1;
2659         double xx = x*x, yy = y*y;
2660 // distance squared from cursor to cursor projected to line from ctrl to handle
2661 //   along a line perpendicular to line from ctrl to handle (closest approach)
2662 // (x**2*y1**2 - 2*x*x1*y*y1 + x1**2*y**2)/(x1**2 + y1**2)
2663         double dist2 = (xx*yy1 - 2*x*x1*y*y1 + xx1*yy)/(xx1 + yy1);
2664         return dist2 < (HANDLE_W*HANDLE_W)/4. ? x/x1 : 0.;
2665 }
2666
2667
2668 inline
2669 float levered_position(float position, float ref_pos)
2670 {
2671         if( 1e-6 > fabs(ref_pos) || isnan(ref_pos))
2672                 return 0.0;
2673         return ref_pos / position;
2674 }
2675
2676
2677 float TrackCanvas::value_to_percentage(float auto_value, int autogrouptype)
2678 // transforms automation value into current display coords,
2679 // dependant on current automation display range for the given kind of automation
2680 {
2681         if(!mwindow || !mwindow->edl) return 0;
2682         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2683         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2684         float automation_range = automation_max - automation_min;
2685         if( 0 >= automation_range || isnan(auto_value) || isinf(auto_value) )
2686                 return 0;
2687         return (auto_value - automation_min) / automation_range;
2688 }
2689
2690
2691
2692 int TrackCanvas::test_floatauto(FloatAuto *current, int buttonpress,
2693                 int center_pixel, int data_h, int cursor_x, int cursor_y,
2694                 double unit_start, double zoom_units, double yscale,
2695                 int autogrouptype)
2696 {
2697         int result = 0;
2698         double xx, yy, in_xx, in_yy, out_xx, out_yy;
2699         calculate_auto_position(1, &xx, &yy, &in_xx, &in_yy, 0, 0,
2700                 current, unit_start, zoom_units, yscale, autogrouptype);
2701         int ix = xx, iy = yy, in_x = in_xx, in_y = in_yy;
2702
2703         int x1 = ix - HANDLE_W / 2;
2704         int x2 = ix + HANDLE_W / 2;
2705         int y1 = center_pixel + iy - HANDLE_W / 2;
2706         int y2 = center_pixel + iy + HANDLE_W / 2;
2707         int ymin = center_pixel - data_h/2;
2708         int ymax = center_pixel + data_h/2;
2709         CLAMP(y1, ymin, ymax);
2710         CLAMP(y2, ymin, ymax);
2711
2712         int in_x1 = in_x - HANDLE_W / 2;
2713         int in_x2 = in_x + HANDLE_W / 2;
2714         int in_y1 = center_pixel + in_y - HANDLE_W / 2;
2715         int in_y2 = center_pixel + in_y + HANDLE_W / 2;
2716         CLAMP(in_y1, ymin, ymax);
2717         CLAMP(in_y2, ymin, ymax);
2718
2719         calculate_auto_position(0, &xx, &yy, 0, 0, &out_xx, &out_yy,
2720                 current, unit_start, zoom_units, yscale, autogrouptype);
2721         int ox = xx, oy = yy, out_x = out_xx, out_y = out_yy;
2722         int out_x1 = out_x - HANDLE_W / 2;
2723         int out_x2 = out_x + HANDLE_W / 2;
2724         int out_y1 = center_pixel + out_y - HANDLE_W / 2;
2725         int out_y2 = center_pixel + out_y + HANDLE_W / 2;
2726         CLAMP(out_y1, ymin, ymax);
2727         CLAMP(out_y2, ymin, ymax);
2728
2729
2730 //printf("TrackCanvas::test_floatauto %d %d %d %d %d %d\n", cursor_x, cursor_y, x1, x2, y1, y2);
2731 // buttonpress could be the start of a drag operation
2732 #define INIT_DRAG(POS,VAL) \
2733                 mwindow->session->drag_auto = current;      \
2734                 mwindow->session->drag_origin_x = cursor_x;  \
2735                 mwindow->session->drag_origin_y = cursor_y;   \
2736                 mwindow->session->drag_start_position = (POS); \
2737                 mwindow->session->drag_start_percentage = (VAL);
2738
2739 #define WITHIN(X1,X2,Y1,Y2) (cursor_x >=(X1) && cursor_x <(X2) && cursor_y >=(Y1) && cursor_y <(Y2) )
2740
2741
2742 // without modifier we are manipulating the automation node
2743 // with ALT it's about dragging only the value of the node
2744 // with SHIFT the value snaps to the value of neighbouring nodes
2745 // CTRL indicates we are rather manipulating the tangent(s) of the node
2746
2747         if(!ctrl_down())
2748         {
2749                 if( WITHIN(x1,x2,y1,y2))
2750                 {       // cursor hits node
2751                         result = 1;
2752
2753                         if(buttonpress && (buttonpress != 3))
2754                         {
2755                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2756                                 mwindow->session->drag_handle = 0;
2757                 }       }
2758         }
2759         else // ctrl_down()
2760         {
2761                 if( WITHIN(x1,x2,y1,y2))
2762                 {
2763                         result = 1;
2764                         if(buttonpress && (buttonpress != 3))
2765                         {
2766                                 // could be ctrl-click or ctrl-drag
2767                                 // click would cycle through tangent modes
2768                                 ((FloatAuto*)current)->toggle_curve_mode();
2769
2770                                 // drag will start dragging the tangent, if applicable
2771                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2772                                 mwindow->session->drag_handle = 0;
2773                         }
2774                 }
2775
2776                 float lever = 0.0; // we use the tangent as a draggable lever. 1.0 is at the ctrl point
2777
2778 // Test in control
2779                 if( in_x != ix && current->position > 0 &&
2780                         (FloatAuto::FREE == current->curve_mode ||
2781                          FloatAuto::TFREE == current->curve_mode ||
2782                          FloatAuto::BUMP == current->curve_mode))
2783 // act on in control handle only if
2784 // tangent is significant and is editable (not automatically choosen)
2785                 {
2786                         lever = test_curve_line(ix, iy, in_x, in_y, cursor_x, cursor_y-center_pixel);
2787  // either cursor in ctrl-point handle or cursor on tangent line
2788                         if( WITHIN(in_x1,in_x2,in_y1,in_y2) || lever > 0.0 ) {
2789                                 result = 1;
2790                                 if(buttonpress && (buttonpress != 3)) {
2791                                         if(lever == 0.0) lever=1.0; // we entered by dragging the handle...
2792 //                                      lever = 1.0;
2793                                         mwindow->session->drag_handle = 1;
2794                                         float new_invalue = current->get_value() + lever * current->get_control_in_value();
2795                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_in_position()),
2796                                                   value_to_percentage(new_invalue, autogrouptype))
2797                                 }
2798                         }
2799                 }
2800
2801 // Test out control
2802                 if(out_x != ox &&
2803                         (FloatAuto::FREE == current->curve_mode ||
2804                          FloatAuto::TFREE == current->curve_mode ||
2805                          FloatAuto::BUMP == current->curve_mode))
2806 // act on out control only if tangent is significant and is editable
2807                 {
2808                         lever = test_curve_line(ox, oy, out_x, out_y, cursor_x, cursor_y-center_pixel);
2809                         if(WITHIN(out_x1,out_x2,out_y1,out_y2) || lever > 0.0 ) {
2810                                 result = 1;
2811                                 if(buttonpress && (buttonpress != 3)) {
2812                                         if(lever == 0.0) lever=1.0;
2813 //                                      lever = 1.0;
2814                                         mwindow->session->drag_handle = 2;
2815                                         float new_outvalue = current->get_value() + lever * current->get_control_out_value();
2816                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_out_position()),
2817                                                   value_to_percentage(new_outvalue, autogrouptype))
2818                                 }
2819                         }
2820                 }
2821         } // end ctrl_down()
2822
2823 #undef WITHIN
2824 #undef INIT_DRAG
2825
2826 // if(buttonpress)
2827 // printf("TrackCanvas::test_floatauto 2 drag_handle=%d ctrl_down=%d cursor_x=%d cursor_y=%d x1=%d x2=%d y1=%d y2=%d\n",
2828 // mwindow->session->drag_handle,
2829 // ctrl_down(),
2830 // cursor_x,
2831 // cursor_y,
2832 // x1, x2, y1, y2);
2833         if(buttonpress && (buttonpress != 3) && result)
2834         {
2835                 mwindow->undo->update_undo_before();
2836                 double position = current->autos->track->from_units(current->position);
2837                 mwindow->edl->local_session->set_selectionstart(position);
2838                 mwindow->edl->local_session->set_selectionend(position);
2839         }
2840
2841         return result;
2842 }
2843
2844 static int is_linear(FloatAuto *prev, FloatAuto *next)
2845 {
2846         if( !prev || !next ) return 0;
2847         if( prev->curve_mode == FloatAuto::LINEAR ) return 1;
2848         int64_t ipos = prev->get_control_in_position();
2849         int64_t opos = prev->get_control_out_position();
2850         if( !ipos && !opos ) return 1;
2851         if( !ipos || !opos ) return 0;
2852         float ival = next->get_control_in_value();
2853         float oval = prev->get_control_out_value();
2854         float cval = prev->get_value(0), nval = next->get_value(1);
2855         if( !ival && !oval && EQUIV(cval, nval) ) return 1;
2856         float ig = ival / ipos, og = oval / opos;
2857         int64_t cpos = prev->position, npos = next->position;
2858         float g = (nval - cval) / (npos - cpos);
2859         if( !EQUIV(g, 0) && EQUIV(ig, g) && EQUIV(og, g) ) return 1;
2860         return 0;
2861 }
2862
2863 // Get the float value & y for position x on the canvas
2864 #define X_TO_FLOATLINE(x) \
2865         double position = unit_start + x * zoom_units; \
2866         int64_t position1 = (int64_t)position, position2 = position1 + 1; \
2867 /* Call by reference fails for some reason here */ \
2868         float value1 = autos->get_value(position1, PLAY_FORWARD, previous1, next1); \
2869         float value2 = autos->get_value(position2, PLAY_FORWARD, previous1, next1); \
2870         double value = value1 + (value2 - value1) * (position - position1); \
2871         AUTOMATIONCLAMPS(value, autogrouptype); \
2872         int y = center_pixel + \
2873                 (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2874
2875
2876 void TrackCanvas::draw_floatline(int center_pixel,
2877         FloatAuto *previous, FloatAuto *next, FloatAutos *autos,
2878         double unit_start, double zoom_units, double yscale,
2879         int x1, int y1, int x2, int y2,
2880         int color, int autogrouptype)
2881 {
2882         int ytop = center_pixel - yscale / 2;
2883         int ybot = center_pixel + yscale / 2 - 1;
2884         y1 += center_pixel;  y2 += center_pixel;
2885         if( (y1 < ytop && y1 >= ybot) && (y2 < ytop || y2 >= ybot) ) return;
2886 // check for line draw
2887         if( is_linear(previous, next) ) {
2888                 if( previous && previous->curve_mode == FloatAuto::BUMP ) {
2889                         double ax, ay;
2890                         calculate_auto_position(0, &ax, &ay, 0, 0, 0, 0, previous,
2891                                 unit_start, zoom_units, yscale, autogrouptype);
2892                         x1 = ax;  y1 = ay;  y1 += center_pixel;
2893                 }
2894                 draw_line(x1, y1, x2, y2);
2895                 return;
2896         }
2897
2898 // Solve bezier equation for either every pixel or a certain large number of
2899 // points.
2900
2901 // Not using slope intercept
2902         x1 = MAX(0, x1);
2903         draw_pixel(x1, y1);
2904         int prev_y = y1;
2905
2906 // Call by reference fails for some reason here
2907         FloatAuto *previous1 = previous, *next1 = next;
2908         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2909         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2910         float automation_range = automation_max - automation_min;
2911         if( autogrouptype == AUTOGROUPTYPE_SPEED && automation_range < SPEED_MIN )
2912                 automation_range = SPEED_MIN;
2913         for( int x=x1; x<x2; ++x ) {
2914 // Interpolate value between frames
2915                 X_TO_FLOATLINE(x)
2916
2917                 if( /* x > x1 && */ y >= ytop && y < ybot ) {
2918                         int x1 = x-1, y1 = prev_y, x2 = x, y2 = y;
2919                         if( abs(y2-y1) < 2 )
2920                                 draw_pixel(x2, y2);
2921                         else
2922                                 draw_bline(x1, y1, x2, y2);
2923                 }
2924                 prev_y = y;
2925         }
2926 }
2927
2928
2929 int TrackCanvas::test_floatline(int center_pixel,
2930                 FloatAutos *autos,
2931                 double unit_start,
2932                 double zoom_units,
2933                 double yscale,
2934                 int x1,
2935                 int x2,
2936                 int cursor_x,
2937                 int cursor_y,
2938                 int buttonpress,
2939                 int autogrouptype)
2940 {
2941         int result = 0;
2942
2943
2944         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2945         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2946         float automation_range = automation_max - automation_min;
2947         if( autogrouptype == AUTOGROUPTYPE_SPEED && automation_range < SPEED_MIN )
2948                 automation_range = SPEED_MIN;
2949         FloatAuto *previous1 = 0, *next1 = 0;
2950         X_TO_FLOATLINE(cursor_x);
2951
2952         if(cursor_x >= x1 &&
2953                 cursor_x < x2 &&
2954                 cursor_y >= y - HANDLE_W / 2 &&
2955                 cursor_y < y + HANDLE_W / 2 &&
2956                 !ctrl_down())
2957         {
2958                 result = 1;
2959
2960 // Menu
2961                 if(buttonpress == 3)
2962                 {
2963                 }
2964                 else
2965 // Create keyframe
2966                 if(buttonpress)
2967                 {
2968                         Auto *current;
2969                         mwindow->undo->update_undo_before();
2970                         double position = autos->track->from_units(position1);
2971                         position = mwindow->edl->align_to_frame(position, 0);
2972                         int64_t new_position = autos->track->to_units(position,0);
2973                         current = mwindow->session->drag_auto = autos->insert_auto(new_position);
2974                         ((FloatAuto*)current)->set_value(value);
2975                         mwindow->session->drag_start_percentage = value_to_percentage(value, autogrouptype);
2976                         mwindow->session->drag_start_position = current->position;
2977                         mwindow->session->drag_origin_x = cursor_x;
2978                         mwindow->session->drag_origin_y = cursor_y;
2979                         mwindow->session->drag_handle = 0;
2980                 }
2981         }
2982
2983
2984         return result;
2985 }
2986
2987 // gang=-1 for keyframepopup update, all tracks where fautos exist
2988 // gang=0 for trackcanvas drag update, all gang matching tracks, create new fautos if needed
2989 // gang=1 for trackcanvas drag update, all gang tracks, create new fautos if needed
2990 void TrackCanvas::fill_ganged_autos(int gang, float change, Track *skip, FloatAuto *fauto)
2991 {
2992         if( !skip->is_ganged() ) return;
2993 // Handles the special case of modifying a fadeauto
2994 // when there are ganged faders on several tracks
2995         double position = skip->from_units(fauto->position);
2996         int autoidx = fauto->autos->autoidx;
2997         PatchGUI *patch = gang < 0 ? pane->patchbay->get_patch_of(skip) : 0;
2998         int edge = patch ? patch->edge : 0;
2999         int span = patch ? patch->span : 0;
3000         for(Track *current = mwindow->edl->tracks->first; current; current = NEXT) {
3001                 if( current == skip || !current->is_armed() ) continue;
3002                 if( !gang && current->data_type != skip->data_type ) continue;
3003                 if( skip->armed_gang(current) || get_double_click() ) {
3004                         FloatAutos *fade_autos = (FloatAutos*)current->automation->autos[autoidx];
3005                         FloatAuto *keyframe = (FloatAuto*)fade_autos->get_auto_at_position(position);
3006                         int64_t current_position = current->to_units(position, 1);
3007                         if( keyframe ) {
3008 // keyframe exists, just change it
3009                                 keyframe->bump_update(current_position, change, edge, span);
3010                         }
3011                         else if( gang >= 0 ) {
3012 // create keyframe on neighbouring track at the point in time given by fauto
3013                                 FloatAuto *previous = 0, *next = 0;
3014                                 float value = fade_autos->get_value(current_position, PLAY_FORWARD, previous, next);
3015                                 float new_value = value + change;
3016                                 float auto_min = mwindow->edl->local_session->automation_mins[fade_autos->autogrouptype];
3017                                 float auto_max = mwindow->edl->local_session->automation_maxs[fade_autos->autogrouptype];
3018                                 bclamp(new_value, auto_min, auto_max);
3019                                 keyframe = (FloatAuto*)fade_autos->insert_auto(current_position);
3020                                 keyframe->set_value(new_value, edge);
3021                         }
3022                         else
3023                                 continue;
3024                         mwindow->session->drag_auto_gang->append((Auto *)keyframe);
3025                 }
3026         }
3027 }
3028
3029 void TrackCanvas::update_ganged_autos(float change, Track *skip, FloatAuto *fauto)
3030 {
3031         double position = skip->from_units(fauto->position);
3032 // Move the gangs
3033         for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
3034                 FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
3035                 int64_t keyframe_position = keyframe->autos->track->to_units(position, 1);
3036                 keyframe->bump_update(keyframe_position, change, 0, 0);
3037                 keyframe->set_control_in_value(fauto->get_control_in_value());
3038                 keyframe->set_control_out_value(fauto->get_control_out_value());
3039         }
3040 }
3041
3042 void TrackCanvas::clear_ganged_autos()
3043 {
3044 // remove the gangs
3045         for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
3046                 FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
3047                 keyframe->autos->remove_nonsequential(keyframe);
3048         }
3049         mwindow->session->drag_auto_gang->remove_all();
3050 }
3051
3052
3053 void TrackCanvas::draw_toggleline(int center_pixel,
3054         int x1,
3055         int y1,
3056         int x2,
3057         int y2)
3058 {
3059         draw_line(x1, center_pixel + y1, x2, center_pixel + y1);
3060
3061         if(y2 != y1)
3062         {
3063                 draw_line(x2, center_pixel + y1, x2, center_pixel + y2);
3064         }
3065 }
3066
3067 int TrackCanvas::test_toggleline(Autos *autos,
3068         int center_pixel,
3069         int x1,
3070         int y1,
3071         int x2,
3072         int y2,
3073         int cursor_x,
3074         int cursor_y,
3075         int buttonpress)
3076 {
3077         int result = 0;
3078         if(cursor_x >= x1 && cursor_x < x2)
3079         {
3080                 int miny = center_pixel + y1 - HANDLE_W / 2;
3081                 int maxy = center_pixel + y1 + HANDLE_W / 2;
3082                 if(cursor_y >= miny && cursor_y < maxy)
3083                 {
3084                         result = 1;
3085
3086 // Menu
3087                         if(buttonpress == 3)
3088                         {
3089                         }
3090                         else
3091 // Insert keyframe
3092                         if(buttonpress)
3093                         {
3094                                 Auto *current;
3095                                 double position = (double)(cursor_x +
3096                                                 mwindow->edl->local_session->view_start[pane->number]) *
3097                                         mwindow->edl->local_session->zoom_sample /
3098                                         mwindow->edl->session->sample_rate;
3099                                 int64_t unit_position = autos->track->to_units(position, 0);
3100                                 int new_value = (int)((IntAutos*)autos)->get_automation_constant(unit_position, unit_position);
3101
3102                                 mwindow->undo->update_undo_before();
3103
3104                                 current = mwindow->session->drag_auto = autos->insert_auto(unit_position);
3105                                 ((IntAuto*)current)->value = new_value;
3106                                 // Toggle Autos don't respond to vertical zoom, they always show up
3107                                 // with "on" == 100% == line on top
3108                                 mwindow->session->drag_start_percentage = (float)new_value;
3109                                 mwindow->session->drag_start_position = current->position;
3110                                 mwindow->session->drag_origin_x = cursor_x;
3111                                 mwindow->session->drag_origin_y = cursor_y;
3112                         }
3113                 }
3114         };
3115
3116         return result;
3117 }
3118
3119 void TrackCanvas::calculate_viewport(Track *track,
3120         double &view_start,   // Seconds
3121         double &unit_start,
3122         double &view_end,     // Seconds
3123         double &unit_end,
3124         double &yscale,
3125         int &center_pixel,
3126         double &zoom_sample,
3127         double &zoom_units)
3128 {
3129
3130         view_start = (double)mwindow->edl->local_session->view_start[pane->number] *
3131                 mwindow->edl->local_session->zoom_sample /
3132                 mwindow->edl->session->sample_rate;
3133         unit_start = track->to_doubleunits(view_start);
3134         view_end = (double)(mwindow->edl->local_session->view_start[pane->number] +
3135                 get_w()) *
3136                 mwindow->edl->local_session->zoom_sample /
3137                 mwindow->edl->session->sample_rate;
3138         unit_end = track->to_doubleunits(view_end);
3139
3140         int y = track->y_pixel
3141                         - mwindow->edl->local_session->track_start[pane->number];
3142         int has_titles = track->show_titles();
3143         int has_assets = track->show_assets();
3144         double title_bg_h = mwindow->theme->get_image("title_bg_data")->get_h();
3145         double title_h = mwindow->theme->title_h;
3146         double data_h = track->data_h;
3147         double ys = has_assets ? data_h : has_titles ? title_bg_h : 0;
3148         double dy = has_titles ?
3149                 ( has_assets ? title_bg_h + data_h/2 : title_bg_h/2) :
3150                 ( has_assets ? data_h/2 : 0) ;
3151         if( dy < title_h/2 ) { ys = title_h;  dy = ys / 2; }
3152         yscale = ys;
3153         center_pixel = y + dy;
3154
3155         zoom_sample = mwindow->edl->local_session->zoom_sample;
3156         zoom_units = track->to_doubleunits(zoom_sample / mwindow->edl->session->sample_rate);
3157 }
3158
3159 float TrackCanvas::percentage_to_value(float percentage,
3160         int is_toggle,
3161         Auto *reference,
3162         int autogrouptype)
3163 {
3164         float result;
3165         if(is_toggle)
3166         {
3167                 if(percentage > 0.5)
3168                         result = 1;
3169                 else
3170                         result = 0;
3171         }
3172         else
3173         {
3174                 float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
3175                 float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
3176                 float automation_range = automation_max - automation_min;
3177
3178                 result = percentage * automation_range + automation_min;
3179                 if(reference)
3180                 {
3181                         FloatAuto *ptr = (FloatAuto*)reference;
3182                         result -= ptr->get_value();
3183                 }
3184 //printf("TrackCanvas::percentage_to_value %d %f\n", __LINE__, result);
3185         }
3186         return result;
3187 }
3188
3189
3190 void TrackCanvas::calculate_auto_position(int edge, double *x, double *y,
3191         double *in_x, double *in_y, double *out_x, double *out_y,
3192         Auto *current, double unit_start, double zoom_units, double yscale,
3193         int autogrouptype)
3194 {
3195         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
3196         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
3197         float automation_range = automation_max - automation_min;
3198         if( autogrouptype == AUTOGROUPTYPE_SPEED && automation_range < SPEED_MIN )
3199                 automation_range = SPEED_MIN;
3200         FloatAuto *ptr = (FloatAuto*)current;
3201         *x = (double)(ptr->position - unit_start) / zoom_units;
3202         *y = ((ptr->get_value(edge) - automation_min) / automation_range - 0.5) * -yscale;
3203
3204         if(in_x) {
3205 //              *in_x = EQUIV(ptr->control_in_value, 0.0) ? *x : *x - mwindow->theme->control_pixels;
3206                 *in_x = (double)(ptr->position + ptr->get_control_in_position() - unit_start) / zoom_units;
3207         }
3208
3209         if(in_y) {
3210                 *in_y = (((ptr->get_value(edge) + ptr->get_control_in_value()) -
3211                                 automation_min) / automation_range - 0.5) * -yscale;
3212         }
3213
3214         if(out_x) {
3215 //              *out_x = EQUIV(ptr->control_out_value, 0.0) ? *x : *x + mwindow->theme->control_pixels;
3216                 *out_x = (double)(ptr->position + ptr->get_control_out_position() - unit_start) / zoom_units;
3217         }
3218
3219         if(out_y) {
3220                 *out_y = (((ptr->get_value(edge) + ptr->get_control_out_value()) -
3221                                  automation_min) / automation_range - 0.5) * -yscale;
3222         }
3223 }
3224
3225
3226 int TrackCanvas::do_float_autos(Track *track, Autos *autos, int cursor_x, int cursor_y,
3227                 int draw, int buttonpress, int x_offset, int y_offset, int color,
3228                 Auto* &auto_instance, int autogrouptype)
3229 {
3230         int result = 0;
3231         int center_pixel, draw_auto;
3232         double view_start, unit_start;
3233         double view_end, unit_end, yscale;
3234         double zoom_sample, zoom_units;
3235         double slope;
3236         //int skip = 0;
3237
3238         auto_instance = 0;
3239
3240         if(draw) set_color(color);
3241
3242         calculate_viewport(track, view_start, unit_start, view_end, unit_end,
3243                         yscale, center_pixel, zoom_sample, zoom_units);
3244
3245 // Get first auto before start
3246         Auto *current = 0, *previous = 0;
3247
3248         for( current = autos->last;
3249                 current && current->position >= unit_start;
3250                 current = PREVIOUS ) ;
3251
3252         Auto *first_auto = current ? current :
3253                  autos->first ? autos->first : autos->default_auto;
3254
3255         double ax = 0, ay = 0, ax2 = 0, ay2 = 0;
3256         if( first_auto ) {
3257                 calculate_auto_position(0, &ax, &ay, 0, 0, 0, 0,
3258                         first_auto, unit_start, zoom_units, yscale, autogrouptype);
3259         }
3260         if( current )
3261                 current = NEXT;
3262         else {
3263                 current = autos->first;
3264                 ax = 0;
3265         }
3266
3267         do {
3268                 //skip = 0;
3269                 draw_auto = 1;
3270
3271                 if(current) {
3272                         calculate_auto_position(1, &ax2, &ay2, 0, 0, 0, 0,
3273                                 current, unit_start, zoom_units, yscale, autogrouptype);
3274                 }
3275                 else {
3276                         ax2 = get_w();
3277                         ay2 = ay;
3278                         //skip = 1;
3279                 }
3280
3281                 slope = ax2 > ax ? (ay2 - ay) / (ax2 - ax) : 0;
3282
3283                 if(ax2 > get_w()) {
3284                         draw_auto = 0;
3285                         ax2 = get_w();
3286                         ay2 = ay + slope * (get_w() - ax);
3287                 }
3288
3289                 if(ax < 0) {
3290                         ay = ay + slope * (0 - ax);
3291                         ax = 0;
3292                 }
3293
3294 // Draw or test handle
3295                 if( current && !result && current != autos->default_auto ) {
3296                         if( !draw && track->is_armed() ) {
3297                                 result = test_floatauto((FloatAuto*)current, buttonpress,
3298                                         (int)center_pixel, (int)yscale, cursor_x, cursor_y,
3299                                         unit_start, zoom_units, yscale, autogrouptype);
3300                                 if( result )
3301                                         auto_instance = current;
3302                         }
3303                         if( draw && draw_auto ) {
3304                                 draw_floatauto((FloatAuto*)current, x_offset,
3305                                         (int)center_pixel + y_offset, (int)yscale, color,
3306                                         unit_start, zoom_units, yscale, autogrouptype);
3307                         }
3308                 }
3309
3310 // Draw or test joining line
3311                 if( !draw && !result && track->is_armed() /* && buttonpress != 3 */ ) {
3312                         result = test_floatline(center_pixel,
3313                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3314 // Exclude auto coverage from the end of the line.  The auto overlaps
3315                                 (int)ax, (int)ax2 - HANDLE_W / 2, cursor_x, cursor_y,
3316                                 buttonpress, autogrouptype);
3317                 }
3318                 if( draw )
3319                         draw_floatline(center_pixel, (FloatAuto*)previous, (FloatAuto*)current,
3320                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3321                                 (int)ax, (int)ay, (int)ax2, (int)ay2,
3322                                 color, autogrouptype);
3323
3324                 if( current ) {
3325                         previous = current;
3326                         calculate_auto_position(0, &ax2, &ay2, 0, 0, 0, 0, previous,
3327                                 unit_start, zoom_units, yscale, autogrouptype);
3328                         current = NEXT;
3329                 }
3330                 ax = ax2;  ay = ay2;
3331         } while( current && current->position <= unit_end && !result );
3332
3333         if( ax < get_w() && !result ) {
3334                 ax2 = get_w();  ay2 = ay;
3335                 if(!draw && track->is_armed() /* && buttonpress != 3 */ ) {
3336                         result = test_floatline(center_pixel,
3337                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3338                                 (int)ax, (int)ax2, cursor_x, cursor_y,
3339                                 buttonpress, autogrouptype);
3340                 }
3341                 if( draw )
3342                         draw_floatline(center_pixel,
3343                                 (FloatAuto*)previous, (FloatAuto*)current,
3344                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3345                                 (int)ax, (int)ay, (int)ax2, (int)ay2,
3346                                 color, autogrouptype);
3347         }
3348
3349         return result;
3350 }
3351
3352
3353 int TrackCanvas::do_int_autos(Track *track,
3354                 Autos *autos,
3355                 int cursor_x,
3356                 int cursor_y,
3357                 int draw,
3358                 int buttonpress,
3359                 int x_offset,
3360                 int y_offset,
3361                 int color,
3362                 Auto *&auto_instance)
3363 {
3364         int result = 0;
3365         double view_start;
3366         double unit_start;
3367         double view_end;
3368         double unit_end;
3369         double yscale;
3370         int center_pixel;
3371         double zoom_sample;
3372         double zoom_units;
3373         double ax, ay, ax2, ay2;
3374
3375         auto_instance = 0;
3376
3377         if(draw) set_color(color);
3378
3379         calculate_viewport(track,
3380                 view_start,
3381                 unit_start,
3382                 view_end,
3383                 unit_end,
3384                 yscale,
3385                 center_pixel,
3386                 zoom_sample,
3387                 zoom_units);
3388
3389
3390         double high = -yscale * 0.8 / 2;
3391         double low = yscale * 0.8 / 2;
3392
3393 // Get first auto before start
3394         Auto *current;
3395         for(current = autos->last; current && current->position >= unit_start; current = PREVIOUS)
3396                 ;
3397
3398         if(current)
3399         {
3400                 ax = 0;
3401                 ay = ((IntAuto*)current)->value > 0 ? high : low;
3402                 current = NEXT;
3403         }
3404         else
3405         {
3406                 current = autos->first ? autos->first : autos->default_auto;
3407                 if(current)
3408                 {
3409                         ax = 0;
3410                         ay = ((IntAuto*)current)->value > 0 ? high : low;
3411                 }
3412                 else
3413                 {
3414                         ax = 0;
3415                         ay = yscale;
3416                 }
3417         }
3418
3419         do
3420         {
3421                 if(current)
3422                 {
3423                         ax2 = (double)(current->position - unit_start) / zoom_units;
3424                         ay2 = ((IntAuto*)current)->value > 0 ? high : low;
3425                 }
3426                 else
3427                 {
3428                         ax2 = get_w();
3429                         ay2 = ay;
3430                 }
3431
3432                 if(ax2 > get_w()) ax2 = get_w();
3433
3434                 if(current && !result)
3435                 {
3436                         if(current != autos->default_auto)
3437                         {
3438                                 if(!draw)
3439                                 {
3440                                         if(track->is_armed())
3441                                         {
3442                                                 result = test_auto(current,
3443                                                         (int)ax2,
3444                                                         (int)ay2,
3445                                                         (int)center_pixel,
3446                                                         (int)yscale,
3447                                                         cursor_x,
3448                                                         cursor_y,
3449                                                         buttonpress);
3450                                                 if (result)
3451                                                         auto_instance = current;
3452                                         }
3453                                 }
3454                                 else
3455                                         draw_auto(current,
3456                                                 (int)ax2 + x_offset,
3457                                                 (int)ay2 + y_offset,
3458                                                 (int)center_pixel,
3459                                                 (int)yscale);
3460                         }
3461
3462                         current = NEXT;
3463                 }
3464
3465                 if(!draw)
3466                 {
3467                         if(!result)
3468                         {
3469                                 if(track->is_armed() /* && buttonpress != 3 */)
3470                                 {
3471                                         result = test_toggleline(autos,
3472                                                 center_pixel,
3473                                                 (int)ax,
3474                                                 (int)ay,
3475                                                 (int)ax2,
3476                                                 (int)ay2,
3477                                                 cursor_x,
3478                                                 cursor_y,
3479                                                 buttonpress);
3480                                 }
3481                         }
3482                 }
3483                 else
3484                         draw_toggleline(center_pixel + y_offset,
3485                                 (int)ax,
3486                                 (int)ay,
3487                                 (int)ax2,
3488                                 (int)ay2);
3489
3490                 ax = ax2;
3491                 ay = ay2;
3492         }while(current && current->position <= unit_end && !result);
3493
3494         if(ax < get_w() && !result)
3495         {
3496                 ax2 = get_w();
3497                 ay2 = ay;
3498                 if(!draw)
3499                 {
3500                         if(track->is_armed() /* && buttonpress != 3 */)
3501                         {
3502                                 result = test_toggleline(autos,
3503                                         center_pixel,
3504                                         (int)ax,
3505                                         (int)ay,
3506                                         (int)ax2,
3507                                         (int)ay2,
3508                                         cursor_x,
3509                                         cursor_y,
3510                                         buttonpress);
3511                         }
3512                 }
3513                 else
3514                         draw_toggleline(center_pixel + y_offset,
3515                                 (int)ax,
3516                                 (int)ay,
3517                                 (int)ax2,
3518                                 (int)ay2);
3519         }
3520         return result;
3521 }
3522
3523 int TrackCanvas::do_autos(Track *track,
3524                 Autos *autos,
3525                 int cursor_x,
3526                 int cursor_y,
3527                 int draw,
3528                 int buttonpress,
3529                 BC_Pixmap *pixmap,
3530                 Auto * &auto_instance,
3531                 int &rerender)
3532 {
3533         int result = 0;
3534
3535         double view_start;
3536         double unit_start;
3537         double view_end;
3538         double unit_end;
3539         double yscale;
3540         int center_pixel;
3541         double zoom_sample;
3542         double zoom_units;
3543
3544         calculate_viewport(track,
3545                 view_start,
3546                 unit_start,
3547                 view_end,
3548                 unit_end,
3549                 yscale,
3550                 center_pixel,
3551                 zoom_sample,
3552                 zoom_units);
3553
3554         Auto *current;
3555         auto_instance = 0;
3556
3557         for(current = autos->first; current && !result; current = NEXT)
3558         {
3559                 if(current->position >= unit_start && current->position < unit_end)
3560                 {
3561                         int64_t x, y;
3562                         x = (int64_t)((double)(current->position - unit_start) /
3563                                 zoom_units - (pixmap->get_w() / 2.0 + 0.5));
3564                         y = center_pixel - pixmap->get_h() / 2;
3565
3566                         if(!draw)
3567                         {
3568                                 if(cursor_x >= x && cursor_y >= y &&
3569                                         cursor_x < x + pixmap->get_w() &&
3570                                         cursor_y < y + pixmap->get_h())
3571                                 {
3572                                         result = 1;
3573                                         auto_instance = current;
3574
3575                                         if(buttonpress && (buttonpress != 3))
3576                                         {
3577                                                 mwindow->session->drag_auto = current;
3578                                                 mwindow->session->drag_start_position = current->position;
3579                                                 mwindow->session->drag_origin_x = cursor_x;
3580                                                 mwindow->session->drag_origin_y = cursor_y;
3581
3582                                                 double position = autos->track->from_units(current->position);
3583                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3584                                                         mwindow->edl->local_session->get_selectionend(1)) /
3585                                                         2;
3586
3587                                                 if(!shift_down())
3588                                                 {
3589                                                         mwindow->edl->local_session->set_selectionstart(position);
3590                                                         mwindow->edl->local_session->set_selectionend(position);
3591                                                 }
3592                                                 else
3593                                                 if(position < center)
3594                                                 {
3595                                                         mwindow->edl->local_session->set_selectionstart(position);
3596                                                 }
3597                                                 else
3598                                                         mwindow->edl->local_session->set_selectionend(position);
3599
3600                                                 rerender = 1;
3601                                         }
3602                                 }
3603                         }
3604                         else
3605                                 draw_pixmap(pixmap, x, y);
3606                 }
3607         }
3608
3609
3610         return result;
3611 }
3612
3613 // so this means it is always >0 when keyframe is found
3614 int TrackCanvas::do_plugin_autos(Track *track, int cursor_x, int cursor_y,
3615                 int draw, int buttonpress, Plugin* &keyframe_plugin,
3616                 KeyFrame* &keyframe_instance)
3617 {
3618         int result = 0;
3619
3620         double view_start;
3621         double unit_start;
3622         double view_end;
3623         double unit_end;
3624         double yscale;
3625         int center_pixel;
3626         double zoom_sample;
3627         double zoom_units;
3628
3629         if(!track->expand_view) return 0;
3630
3631         calculate_viewport(track,
3632                 view_start,
3633                 unit_start,
3634                 view_end,
3635                 unit_end,
3636                 yscale,
3637                 center_pixel,
3638                 zoom_sample,
3639                 zoom_units);
3640
3641
3642
3643         for(int i = 0; i < track->plugin_set.total && !result; i++)
3644         {
3645                 PluginSet *plugin_set = track->plugin_set.values[i];
3646                 int center_pixel = track->y_pixel -
3647                         mwindow->edl->local_session->track_start[pane->number];
3648                 if( track->show_titles() )
3649                         center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
3650                 if( track->show_assets() )
3651                         center_pixel += track->data_h;
3652                 center_pixel += (i + 0.5) * mwindow->theme->get_image("plugin_bg_data")->get_h();
3653
3654                 for(Plugin *plugin = (Plugin*)plugin_set->first;
3655                         plugin && !result;
3656                         plugin = (Plugin*)plugin->next)
3657                 {
3658                         for(KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
3659                                 keyframe && !result;
3660                                 keyframe = (KeyFrame*)keyframe->next)
3661                         {
3662 //printf("TrackCanvas::draw_plugin_autos 3 %d\n", keyframe->position);
3663                                 if(keyframe->position >= unit_start && keyframe->position < unit_end)
3664                                 {
3665                                         int64_t x = (int64_t)((keyframe->position - unit_start) / zoom_units);
3666                                         int y = center_pixel - keyframe_pixmap->get_h() / 2;
3667
3668 //printf("TrackCanvas::draw_plugin_autos 4 %d %d\n", x, center_pixel);
3669                                         if(!draw)
3670                                         {
3671                                                 if(cursor_x >= x && cursor_y >= y &&
3672                                                         cursor_x < x + keyframe_pixmap->get_w() &&
3673                                                         cursor_y < y + keyframe_pixmap->get_h())
3674                                                 {
3675                                                         result = 1;
3676                                                         keyframe_plugin = plugin;
3677                                                         keyframe_instance = keyframe;
3678
3679                                                         if(buttonpress)
3680                                                         {
3681                                                                 mwindow->session->drag_auto = keyframe;
3682                                                                 mwindow->session->drag_start_position = keyframe->position;
3683                                                                 mwindow->session->drag_origin_x = cursor_x;
3684                                                                 mwindow->session->drag_origin_y = cursor_y;
3685
3686                                                                 double position = track->from_units(keyframe->position);
3687                                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3688                                                                         mwindow->edl->local_session->get_selectionend(1)) /
3689                                                                         2;
3690
3691                                                                 if(!shift_down())
3692                                                                 {
3693                                                                         mwindow->edl->local_session->set_selectionstart(position);
3694                                                                         mwindow->edl->local_session->set_selectionend(position);
3695                                                                 }
3696                                                                 else
3697                                                                 if(position < center)
3698                                                                 {
3699                                                                         mwindow->edl->local_session->set_selectionstart(position);
3700                                                                 }
3701                                                                 else
3702                                                                         mwindow->edl->local_session->set_selectionend(position);
3703                                                         }
3704                                                 }
3705                                         }
3706                                         else
3707                                                 draw_pixmap(keyframe_pixmap,
3708                                                         x,
3709                                                         y);
3710                                 }
3711                         }
3712                 }
3713         }
3714
3715
3716
3717 //      if(buttonpress && buttonpress != 3 && result)
3718 //      {
3719 //              mwindow->undo->update_undo_before();
3720 //      }
3721
3722         return result;
3723 }
3724
3725 int TrackCanvas::draw_hairline(Auto *auto_keyframe, int color, int show)
3726 {
3727         Track *track = auto_keyframe->autos->track;
3728         int autogrouptype = auto_keyframe->autos->autogrouptype;
3729
3730         int center_pixel;
3731         double view_start, unit_start;
3732         double view_end, unit_end, yscale;
3733         double zoom_sample, zoom_units;
3734
3735         calculate_viewport(track, view_start, unit_start, view_end, unit_end,
3736                         yscale, center_pixel, zoom_sample, zoom_units);
3737         if( auto_keyframe->position < unit_start ||
3738             auto_keyframe->position >= unit_end )
3739                 return 0;
3740
3741         double ax = 0, ay = 0;
3742         calculate_auto_position(0, &ax, &ay, 0, 0, 0, 0,
3743                 auto_keyframe, unit_start, zoom_units, yscale, autogrouptype);
3744
3745         set_color(color);
3746         draw_line(ax, 0, ax, get_h());
3747
3748         char text[BCSTRLEN];
3749         if( show ) {
3750                 if( auto_keyframe->is_floatauto() ) {
3751                         FloatAuto *float_auto = (FloatAuto *)auto_keyframe;
3752                         sprintf(text, "%0.2f", float_auto->get_value());
3753                 }
3754                 else if( auto_keyframe->is_intauto() ) {
3755                         IntAuto *int_auto = (IntAuto *)auto_keyframe;
3756                         sprintf(text, "%d", int_auto->value);
3757                 }
3758                 else
3759                         show = 0;
3760         }
3761         if( show ) {
3762                 int font = MEDIUMFONT;
3763                 int tw = get_text_width(font, text)  + xS(TOOLTIP_MARGIN) * 2;
3764                 int th = get_text_height(font, text) + yS(TOOLTIP_MARGIN) * 2;
3765                 set_color(get_resources()->tooltip_bg_color);
3766                 ax += HANDLE_W/2;
3767                 ay += center_pixel + HANDLE_W/2;
3768                 draw_box(ax, ay, tw, th);
3769                 set_color(BLACK);
3770                 draw_rectangle(ax, ay, tw, th);
3771                 set_font(font);
3772                 ax += xS(TOOLTIP_MARGIN);
3773                 ay += yS(TOOLTIP_MARGIN) + get_text_ascent(font);
3774                 draw_text(ax, ay, text);
3775         }
3776         return 0;
3777 }
3778
3779 void TrackCanvas::draw_overlays()
3780 {
3781         int new_cursor, update_cursor, rerender;
3782
3783 // Move background pixmap to foreground pixmap
3784         draw_pixmap(background_pixmap, 0, 0, get_w(), get_h(), 0, 0);
3785
3786 // In/Out points
3787         draw_inout_points();
3788
3789 // Transitions
3790         draw_transitions();
3791
3792 // Plugins
3793         draw_plugins();
3794         draw_hard_edges();
3795
3796 // Loop points
3797         draw_loop_points();
3798         draw_brender_range();
3799
3800 // Highlighted areas
3801         draw_highlighting();
3802
3803 // Automation
3804         do_keyframes(0, 0, 1, 0, new_cursor, update_cursor, rerender);
3805
3806 // Selection cursor
3807         if(pane->cursor) pane->cursor->restore(1);
3808
3809 // Handle dragging
3810         draw_drag_handle();
3811
3812 // Playback cursor
3813         draw_playback_cursor();
3814
3815         draw_keyframe_reticle();
3816
3817         show_window(0);
3818 }
3819
3820 int TrackCanvas::activate()
3821 {
3822         if(!active)
3823         {
3824 //printf("TrackCanvas::activate %d %d\n", __LINE__, pane->number);
3825 //BC_Signals::dump_stack();
3826                 get_top_level()->deactivate();
3827                 active = 1;
3828                 set_active_subwindow(this);
3829                 pane->cursor->activate();
3830                 gui->focused_pane = pane->number;
3831         }
3832         return 0;
3833 }
3834
3835 int TrackCanvas::deactivate()
3836 {
3837         if(active)
3838         {
3839                 active = 0;
3840                 pane->cursor->deactivate();
3841         }
3842         return 0;
3843 }
3844
3845
3846 void TrackCanvas::update_drag_handle()
3847 {
3848         double new_position;
3849         int cursor_x = get_cursor_x();
3850
3851         new_position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
3852         new_position = mwindow->edl->align_to_frame(new_position, 0);
3853
3854         if( ctrl_down() && alt_down() ) {
3855 #define snapper(v) do { \
3856         double pos = (v); \
3857         if( pos < 0 ) break; \
3858         double dist = fabs(new_position - pos); \
3859         if( dist >= snap_min ) break; \
3860         snap_position = pos;  snap_min = dist; \
3861 } while(0)
3862                 double snap_position = new_position;
3863                 double snap_min = DBL_MAX;
3864                 if( mwindow->edl->local_session->inpoint_valid() )
3865                         snapper(mwindow->edl->local_session->get_inpoint());
3866                 if( mwindow->edl->local_session->outpoint_valid() )
3867                         snapper(mwindow->edl->local_session->get_outpoint());
3868                 snapper(mwindow->edl->prev_edit(new_position));
3869                 snapper(mwindow->edl->next_edit(new_position));
3870                 Label *prev_label = mwindow->edl->labels->prev_label(new_position);
3871                 if( prev_label ) snapper(prev_label->position);
3872                 Label *next_label = mwindow->edl->labels->next_label(new_position);
3873                 if( next_label ) snapper(next_label->position);
3874                 int snap_x = snap_position * mwindow->edl->session->sample_rate /
3875                         mwindow->edl->local_session->zoom_sample -
3876                         mwindow->edl->local_session->view_start[pane->number];
3877                 if( abs(snap_x - cursor_x) < HANDLE_W ) {
3878                         snapped = 1;
3879                         new_position = snap_position;
3880                 }
3881 #undef snapper
3882         }
3883
3884         if(new_position != mwindow->session->drag_position)
3885         {
3886                 mwindow->session->drag_position = new_position;
3887                 gui->mainclock->update(new_position);
3888                 timebar_position = new_position;
3889                 gui->update_timebar(0);
3890
3891                 EDL *edl = new EDL;
3892                 edl->create_objects();
3893                 edl->copy_all(mwindow->edl);
3894                 MainSession *session = mwindow->session;
3895                 int handle_mode = mwindow->edl->session->edit_handle_mode[session->drag_button];
3896                 edl->modify_edithandles(session->drag_start,
3897                         session->drag_position, session->drag_handle, handle_mode,
3898                         edl->session->labels_follow_edits,
3899                         edl->session->plugins_follow_edits,
3900                         edl->session->autos_follow_edits,
3901                         !session->drag_edit ? 0 : session->drag_edit->group_id);
3902
3903                 double position = -1;
3904                 int show_edge = !session->drag_handle ? 1 : 2;
3905                 switch( handle_mode ) {
3906                 case MOVE_RIPPLE:
3907                 case MOVE_EDGE:
3908                         position = session->drag_handle ?
3909                                 session->drag_position : session->drag_start;
3910                         show_edge = 3 - show_edge;
3911                         break;
3912                 case MOVE_ROLL:
3913                 case MOVE_SLIDE:
3914                         position = session->drag_position;
3915                         break;
3916                 case MOVE_SLIP:
3917                         position = session->drag_start;
3918                         show_edge = 3 - show_edge;
3919                         break;
3920                 }
3921
3922                 if( position < 0 ) position = 0;
3923                 Track *track = session->drag_handle_track();
3924                 int64_t pos = track->to_units(position, 0);
3925                 render_handle_frame(edl, pos, shift_down() ? 0 : show_edge);
3926                 edl->remove_user();
3927         }
3928 }
3929
3930 int TrackCanvas::render_handle_frame(EDL *edl, int64_t pos, int mode)
3931 {
3932         int result = 0;
3933         int64_t left = pos-1;
3934         if( left < 0 ) left = 0;
3935         switch( mode ) {
3936         case 0: {
3937                 VFrame vlt(edl->get_w(), edl->get_h(), edl->session->color_model);
3938                 VFrame vrt(edl->get_w(), edl->get_h(), edl->session->color_model);
3939                 TransportCommand command;
3940                 command.command = CURRENT_FRAME;
3941                 command.get_edl()->copy_all((EDL *)edl);
3942                 command.change_type = CHANGE_ALL;
3943                 command.realtime = 0;
3944                 Preferences *preferences = mwindow->preferences;
3945                 RenderEngine *render_engine = new RenderEngine(0, preferences, 0, 0);
3946                 CICache *video_cache = new CICache(preferences);
3947                 render_engine->set_vcache(video_cache);
3948                 render_engine->arm_command(&command);
3949                 VRender *vrender = render_engine->vrender;
3950                 result = vrender &&
3951                         !vrender->process_buffer(&vlt, left, 0) &&
3952                         !vrender->process_buffer(&vrt, pos , 0) ? 0 : 1;
3953                 delete render_engine;
3954                 video_cache->remove_user();
3955                 mwindow->cwindow->gui->lock_window("TrackCanvas::render_handle_frame 0");
3956                 Canvas *canvas = mwindow->cwindow->gui->canvas;
3957                 float ox1, oy1, ox2, oy2, cx1, cy1, cx2, cy2;
3958                 canvas->get_transfers(edl, ox1, oy1, ox2, oy2, cx1, cy1, cx2, cy2);
3959                 float cw = canvas->w/2, ch = canvas->h/2;
3960                 float cdx = (cx2 - cx1)/2, cdy = (cy2 - cy1)/2;
3961                 int cx = cx1/2, cy = cy1/2 + ch/2;
3962                 int ow = ox2 - ox2, oh = oy2 - oy1;
3963                 BC_WindowBase *window = canvas->get_canvas();
3964                 window->set_color(BLACK);
3965                 window->clear_box(0,0, window->get_w(),window->get_h());
3966                 window->draw_vframe(&vlt, cx,cy, cdx,cdy, ox1,oy1, ow,oh);  cx += cw;
3967                 window->draw_vframe(&vrt, cx,cy, cdx,cdy, ox1,oy1, ow,oh);
3968                 window->flash(1);
3969                 mwindow->cwindow->gui->unlock_window();
3970                 break; }
3971         case 1:
3972         case 2: {
3973                 Track *track = mwindow->session->drag_handle_track();
3974                 double position = track->from_units(mode == 1 ? left : pos);
3975                 if( position < 0 ) position = 0;
3976                 PlaybackEngine *playback_engine = mwindow->cwindow->playback_engine;
3977                 if( playback_engine->is_playing_back )
3978                         playback_engine->stop_playback(1);
3979                 edl->local_session->set_selectionstart(position);
3980                 edl->local_session->set_selectionend(position);
3981                 mwindow->cwindow->playback_engine->refresh_frame(CHANGE_EDL, edl, 0);
3982                 break; }
3983         }
3984         return result;
3985 }
3986
3987 int TrackCanvas::update_drag_edit()
3988 {
3989         int result = 0;
3990
3991
3992
3993         return result;
3994 }
3995
3996 int TrackCanvas::get_drag_values(float *percentage,
3997         int64_t *position,
3998         int do_clamp,
3999         int cursor_x,
4000         int cursor_y,
4001         Auto *current)
4002 {
4003         //int x = cursor_x - mwindow->session->drag_origin_x;
4004         //int y = cursor_y - mwindow->session->drag_origin_y;
4005         *percentage = 0;
4006         *position = 0;
4007
4008         if(!current->autos->track->is_armed()) return 1;
4009         double view_start;
4010         double unit_start;
4011         double view_end;
4012         double unit_end;
4013         double yscale;
4014         int center_pixel;
4015         double zoom_sample;
4016         double zoom_units;
4017
4018         calculate_viewport(current->autos->track,
4019                 view_start,
4020                 unit_start,
4021                 view_end,
4022                 unit_end,
4023                 yscale,
4024                 center_pixel,
4025                 zoom_sample,
4026                 zoom_units);
4027
4028         *percentage = (float)(mwindow->session->drag_origin_y - cursor_y) /
4029                 yscale +
4030                 mwindow->session->drag_start_percentage;
4031         if(do_clamp) CLAMP(*percentage, 0, 1);
4032
4033         *position = Units::to_int64(zoom_units *
4034                 (cursor_x - mwindow->session->drag_origin_x) +
4035                 mwindow->session->drag_start_position + 0.5);
4036
4037         if((do_clamp) && *position < 0) *position = 0;
4038         return 0;
4039 }
4040
4041
4042 #define UPDATE_DRAG_HEAD(do_clamp) \
4043         int result = 0, center_pixel; \
4044         if(!current->autos->track->is_armed()) return 0; \
4045         double view_start, unit_start, view_end, unit_end; \
4046         double yscale, zoom_sample, zoom_units; \
4047  \
4048         calculate_viewport(current->autos->track,  \
4049                 view_start, unit_start, view_end, unit_end, \
4050                 yscale, center_pixel, zoom_sample, zoom_units); \
4051  \
4052         float percentage = (float)(mwindow->session->drag_origin_y - cursor_y) / \
4053                 yscale +  mwindow->session->drag_start_percentage; \
4054         if(do_clamp) CLAMP(percentage, 0, 1); \
4055  \
4056         int64_t position = Units::to_int64(zoom_units * \
4057                 (cursor_x - mwindow->session->drag_origin_x) + \
4058                 mwindow->session->drag_start_position); \
4059         if((do_clamp) && position < 0) position = 0;
4060
4061
4062 int TrackCanvas::update_drag_floatauto(int cursor_x, int cursor_y)
4063 {
4064         FloatAuto *current = (FloatAuto*)mwindow->session->drag_auto;
4065
4066         UPDATE_DRAG_HEAD(mwindow->session->drag_handle == 0);
4067         int x = cursor_x - mwindow->session->drag_origin_x;
4068         int y = cursor_y - mwindow->session->drag_origin_y;
4069         float value, old_value;
4070
4071         if( mwindow->session->drag_handle == 0 && (ctrl_down() && !shift_down()) ) {
4072 // not really editing the node, rather start editing the curve
4073 // tangent is editable and drag movement is significant
4074                 if( (FloatAuto::FREE == current->curve_mode ||
4075                      FloatAuto::TFREE==current->curve_mode ||
4076                      FloatAuto::BUMP==current->curve_mode) &&
4077                     (fabs(x) > HANDLE_W / 2 || fabs(y) > HANDLE_W / 2))
4078                         mwindow->session->drag_handle = x < 0 ? 1 : 2;
4079         }
4080
4081         switch(mwindow->session->drag_handle) {
4082         case 0: // Center
4083 // Snap to nearby values
4084                 old_value = current->get_value();
4085                 if(shift_down()) {
4086                         double distance1=-1, distance2=-1;
4087                         if(current->previous) {
4088                                 int autogrouptype = current->previous->autos->autogrouptype;
4089                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
4090                                 double value1 = ((FloatAuto*)current->previous)->get_value(0);
4091                                 distance1 = fabs(value - value1);
4092                                 current->set_value(value1, 1);
4093                         }
4094
4095                         if(current->next) {
4096                                 int autogrouptype = current->next->autos->autogrouptype;
4097                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
4098                                 double value2 = ((FloatAuto*)current->next)->get_value(1);
4099                                 distance2 = fabs(value - value2);
4100                                 if(!current->previous || distance2 < distance1) {
4101                                         current->set_value(value2, 0);
4102                                 }
4103                         }
4104
4105                         if(!current->previous && !current->next) {
4106                                 current->set_value(((FloatAutos*)current->autos)->default_, 0);
4107                         }
4108                         value = current->get_value();
4109                 }
4110                 else {
4111                         int autogrouptype = current->autos->autogrouptype;
4112                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
4113                 }
4114
4115                 if(alt_down() && !shift_down())
4116 // ALT constrains movement: fixed position, only changing the value
4117                         position = mwindow->session->drag_start_position;
4118
4119                 if(value != old_value || position != current->position) {
4120                         result = 1;
4121                         float change = value - old_value;
4122                         current->adjust_to_new_coordinates(position, value);
4123                         update_ganged_autos(change, current->autos->track, current);
4124                         int color = GWindowGUI::auto_colors[current->autos->autoidx];
4125                         show_message(current, color, ", %.2f", current->get_value());
4126                 }
4127                 break;
4128
4129 // In control
4130         case 1: {
4131                 int autogrouptype = current->autos->autogrouptype;
4132                 value = percentage_to_value(percentage, 0, current, autogrouptype);
4133                 if(value != current->get_control_in_value())
4134                 {
4135                         result = 1;
4136                         // note: (position,value) need not be at the location of the ctrl point,
4137                         // but could be somewhere in between on the curve (or even outward or
4138                         // on the opposit side). We set the new control point such as
4139                         // to point the curve through (position,value)
4140                         current->set_control_in_value(
4141                                 value * levered_position(position - current->position,
4142                                                          current->get_control_in_position()));
4143                         update_ganged_autos(0, current->autos->track, current);
4144                         int color = GWindowGUI::auto_colors[current->autos->autoidx];
4145                         show_message(current, color, ", %.2f", current->get_control_in_value());
4146                 }
4147                 break; }
4148
4149 // Out control
4150         case 2: {
4151                 int autogrouptype = current->autos->autogrouptype;
4152                 value = percentage_to_value(percentage, 0, current, autogrouptype);
4153                 if(value != current->get_control_out_value()) {
4154                         result = 1;
4155                         current->set_control_out_value(
4156                                 value * levered_position(position - current->position,
4157                                                          current->get_control_out_position()));
4158                         update_ganged_autos(0, current->autos->track, current);
4159                         int color = GWindowGUI::auto_colors[current->autos->autoidx];
4160                         show_message(current, color, ", %.2f", current->get_control_out_value());
4161                 }
4162                 break; }
4163         }
4164
4165         return result;
4166 }
4167
4168 int TrackCanvas::update_drag_toggleauto(int cursor_x, int cursor_y)
4169 {
4170         IntAuto *current = (IntAuto*)mwindow->session->drag_auto;
4171
4172         UPDATE_DRAG_HEAD(1);
4173         int value = (int)percentage_to_value(percentage, 1, 0, AUTOGROUPTYPE_INT255);
4174
4175         if(value != current->value || position != current->position)
4176         {
4177                 result = 1;
4178                 current->value = value;
4179                 current->position = position;
4180                 show_message(current, -1, ", %d", current->value);
4181         }
4182
4183         return result;
4184 }
4185
4186 // Autos which can't change value through dragging.
4187
4188 int TrackCanvas::update_drag_auto(int cursor_x, int cursor_y)
4189 {
4190         Auto *current = (Auto*)mwindow->session->drag_auto;
4191
4192         UPDATE_DRAG_HEAD(1)
4193         if(position != current->position)
4194         {
4195                 result = 1;
4196                 current->position = position;
4197                 show_message(current, -1, "");
4198
4199                 double position_f = current->autos->track->from_units(current->position);
4200                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
4201                         mwindow->edl->local_session->get_selectionend(1)) /
4202                         2;
4203                 if(!shift_down())
4204                 {
4205                         mwindow->edl->local_session->set_selectionstart(position_f);
4206                         mwindow->edl->local_session->set_selectionend(position_f);
4207                 }
4208                 else
4209                 if(position_f < center_f)
4210                 {
4211                         mwindow->edl->local_session->set_selectionstart(position_f);
4212                 }
4213                 else
4214                         mwindow->edl->local_session->set_selectionend(position_f);
4215         }
4216
4217
4218         return result;
4219 }
4220
4221 int TrackCanvas::update_drag_pluginauto(int cursor_x, int cursor_y)
4222 {
4223         KeyFrame *current = (KeyFrame*)mwindow->session->drag_auto;
4224
4225         UPDATE_DRAG_HEAD(1)
4226         if(position != current->position)
4227         {
4228 //      printf("uida: autos: %p, track: %p ta: %p\n", current->autos, current->autos->track, current->autos->track->automation);
4229                 Track *track = current->autos->track;
4230                 //PluginAutos *pluginautos = (PluginAutos *)current->autos;
4231                 PluginSet *pluginset;
4232                 Plugin *plugin = 0;
4233 // figure out the correct pluginset & correct plugin
4234                 int found = 0;
4235                 for(int i = 0; i < track->plugin_set.total; i++)
4236                 {
4237                         pluginset = track->plugin_set.values[i];
4238                         for(plugin = (Plugin *)pluginset->first; plugin; plugin = (Plugin *)plugin->next)
4239                         {
4240                                 KeyFrames *keyframes = plugin->keyframes;
4241                                 for(KeyFrame *currentkeyframe = (KeyFrame *)keyframes->first;
4242                                         currentkeyframe;
4243                                         currentkeyframe = (KeyFrame *) currentkeyframe->next)
4244                                 {
4245                                         if (currentkeyframe == current)
4246                                         {
4247                                                 found = 1;
4248                                                 break;
4249                                         }
4250
4251                                 }
4252                                 if (found) break;
4253                         }
4254                         if (found) break;
4255                 }
4256
4257                 mwindow->session->plugin_highlighted = plugin;
4258                 mwindow->session->track_highlighted = track;
4259                 result = 1;
4260                 current->position = position;
4261                 show_message(current, -1, "");
4262
4263                 double position_f = current->autos->track->from_units(current->position);
4264                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
4265                         mwindow->edl->local_session->get_selectionend(1)) /
4266                         2;
4267                 if(!shift_down())
4268                 {
4269                         mwindow->edl->local_session->set_selectionstart(position_f);
4270                         mwindow->edl->local_session->set_selectionend(position_f);
4271                 }
4272                 else
4273                 if(position_f < center_f)
4274                 {
4275                         mwindow->edl->local_session->set_selectionstart(position_f);
4276                 }
4277                 else
4278                         mwindow->edl->local_session->set_selectionend(position_f);
4279         }
4280
4281
4282         return result;
4283 }
4284
4285 void TrackCanvas::update_drag_caption()
4286 {
4287         switch(mwindow->session->current_operation)
4288         {
4289                 case DRAG_FADE:
4290
4291                         break;
4292         }
4293 }
4294
4295
4296
4297 int TrackCanvas::cursor_update(int in_motion)
4298 {
4299         int result = 0;
4300         int cursor_x = 0;
4301         int cursor_y = 0;
4302         int update_clock = 0;
4303         int update_zoom = 0;
4304         int update_scroll = 0;
4305         int update_overlay = 0;
4306         int update_cursor = 0;
4307         int rerender = 0;
4308         double position = 0.;
4309 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
4310
4311 // Default cursor
4312         int new_cursor =
4313                 arrow_mode() ? ARROW_CURSOR :
4314                 ibeam_mode() ? IBEAM_CURSOR : 0;
4315
4316         switch(mwindow->session->current_operation)
4317         {
4318                 case DRAG_EDITHANDLE1:
4319 // Outside threshold.  Upgrade status
4320                         if( !active ) break;
4321                         if( labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W ) {
4322                                 mwindow->session->current_operation = DRAG_EDITHANDLE2;
4323                                 update_overlay = 1;
4324                         }
4325                         break;
4326                 case DRAG_EDITHANDLE2:
4327                         if( !active ) break;
4328                         update_drag_handle();
4329                         update_overlay = 1;
4330                         break;
4331
4332                 case DRAG_PLUGINHANDLE1:
4333                         if( !active ) break;
4334                         if( labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W ) {
4335                                 mwindow->session->current_operation = DRAG_PLUGINHANDLE2;
4336                                 update_overlay = 1;
4337                         }
4338                         break;
4339                 case DRAG_PLUGINHANDLE2:
4340                         if( !active ) break;
4341                         update_drag_handle();
4342                         update_overlay = 1;
4343                         break;
4344
4345                 case DRAG_TRANSNHANDLE1:
4346                         if( !active ) break;
4347                         if( labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W ) {
4348                                 mwindow->session->current_operation = DRAG_TRANSNHANDLE2;
4349                                 update_overlay = 1;
4350                         }
4351                         break;
4352                 case DRAG_TRANSNHANDLE2: {
4353                         if( !active ) break;
4354                         position = mwindow->edl->get_cursor_position(get_cursor_x(), pane->number);
4355                         position = mwindow->edl->align_to_frame(position, 1);
4356                         drag_transition_handle(position);
4357                         rerender = 1;
4358                         update_overlay = 1;
4359                         break; }
4360
4361 // Rubber band curves
4362                 case DRAG_FADE:
4363                 case DRAG_SPEED:
4364                 case DRAG_CZOOM:
4365                 case DRAG_PZOOM:
4366                 case DRAG_CAMERA_X:
4367                 case DRAG_CAMERA_Y:
4368                 case DRAG_CAMERA_Z:
4369                 case DRAG_PROJECTOR_X:
4370                 case DRAG_PROJECTOR_Y:
4371                 case DRAG_PROJECTOR_Z:
4372                         if(active) rerender = update_overlay =
4373                                 update_drag_floatauto(get_cursor_x(), get_cursor_y());
4374                         if( rerender && mwindow->session->current_operation == DRAG_SPEED )
4375                                 mwindow->speed_after(!in_motion ? 1 : 0, 0);
4376                         break;
4377
4378                 case DRAG_PLAY:
4379                         if(active) rerender = update_overlay =
4380                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4381                         break;
4382
4383                 case DRAG_MUTE:
4384                         if(active) rerender = update_overlay =
4385                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4386                         break;
4387
4388 // Keyframe icons are sticky
4389                 case DRAG_PAN_PRE:
4390                 case DRAG_MASK_PRE:
4391                 case DRAG_MODE_PRE:
4392                 case DRAG_PLUGINKEY_PRE:
4393                         if(active) {
4394                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W) {
4395                                         mwindow->session->current_operation++;
4396                                         update_overlay = 1;
4397
4398                                         mwindow->undo->update_undo_before();
4399                                 }
4400                         }
4401                         break;
4402
4403                 case DRAG_PAN:
4404                 case DRAG_MASK:
4405                 case DRAG_MODE:
4406                         if(active) rerender = update_overlay =
4407                                 update_drag_auto(get_cursor_x(), get_cursor_y());
4408                         break;
4409
4410                 case DRAG_PLUGINKEY:
4411                         if(active) rerender = update_overlay =
4412                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4413                         break;
4414
4415                 case SELECT_REGION:
4416                         if(active) {
4417                                 cursor_x = get_cursor_x();
4418                                 cursor_y = get_cursor_y();
4419                                 position = (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
4420                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
4421
4422                                 position = mwindow->edl->align_to_frame(position, 0);
4423                                 position = MAX(position, 0);
4424
4425                                 double start = mwindow->edl->local_session->get_selectionstart(1);
4426                                 double end = mwindow->edl->local_session->get_selectionend(1);
4427                                 if(position < selection_midpoint) {
4428                                         mwindow->edl->local_session->set_selectionend(selection_midpoint);
4429                                         mwindow->edl->local_session->set_selectionstart(position);
4430                                 }
4431                                 else {
4432                                         mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4433                                         mwindow->edl->local_session->set_selectionend(position);
4434                                 }
4435         // Que the CWindow
4436                                 gui->unlock_window();
4437                                 int dir =
4438                                         start != mwindow->edl->local_session->get_selectionstart(1) ? 1 :
4439                                         end != mwindow->edl->local_session->get_selectionend(1) ? -1 : 0;
4440                                 mwindow->cwindow->update(dir, 0, 0, 0, 1);
4441                                 gui->lock_window("TrackCanvas::cursor_update 1");
4442         // Update the faders
4443                                 mwindow->update_plugin_guis();
4444                                 gui->update_patchbay();
4445
4446                                 timebar_position = mwindow->edl->local_session->get_selectionend(1);
4447
4448                                 gui->hide_cursor(0);
4449                                 gui->draw_cursor(1);
4450                                 gui->update_timebar(0);
4451                                 gui->flash_canvas(1);
4452                                 result = 1;
4453                                 update_clock = 1;
4454                                 update_zoom = 1;
4455                                 update_scroll = 1;
4456                         }
4457                         break;
4458
4459                 case DROP_TARGETING:
4460                         new_cursor = GRABBED_CURSOR;
4461                         result = 1;
4462                         break;
4463
4464                 default:
4465                         if(is_event_win() && cursor_inside()) {
4466 // Update clocks
4467                                 cursor_x = get_cursor_x();
4468                                 position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
4469                                 position = mwindow->edl->align_to_frame(position, 0);
4470                                 update_clock = 1;
4471 // set all timebars
4472                                 for(int i = 0; i < TOTAL_PANES; i++)
4473                                         if(gui->pane[i]) gui->pane[i]->canvas->timebar_position = position;
4474
4475 //printf("TrackCanvas::cursor_update %d %d %p %p\n", __LINE__, pane->number, pane, pane->timebar);
4476                                 gui->update_timebar(0);
4477 // Update cursor
4478                                 if(do_transitions(get_cursor_x(), get_cursor_y(),
4479                                                 0, new_cursor, update_cursor)) break;
4480                                 if(do_keyframes(get_cursor_x(), get_cursor_y(),
4481                                         0, 0, new_cursor, update_cursor, rerender)) break;
4482                                 if(do_transition_handles(get_cursor_x(), get_cursor_y(),
4483                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4484                                 if(do_edit_handles(get_cursor_x(), get_cursor_y(),
4485                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4486 // Plugin boundaries
4487                                 if(do_plugin_handles(get_cursor_x(), get_cursor_y(),
4488                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4489                                 if(do_edits(get_cursor_x(), get_cursor_y(),
4490                                         0, 0, update_overlay, rerender, new_cursor, update_cursor)) break;
4491                         }
4492                         break;
4493         }
4494
4495 //printf("TrackCanvas::cursor_update 1\n");
4496         if(update_cursor && new_cursor != get_cursor())
4497         {
4498                 set_cursor(new_cursor, 0, 1);
4499         }
4500
4501 //printf("TrackCanvas::cursor_update 1 %d\n", rerender);
4502         if(rerender && render_timer->get_difference() > 0.25 ) {
4503                 render_timer->update();
4504                 mwindow->restart_brender();
4505                 mwindow->sync_parameters(CHANGE_PARAMS);
4506                 mwindow->update_plugin_guis();
4507                 gui->unlock_window();
4508                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4509                 gui->lock_window("TrackCanvas::cursor_update 2");
4510         }
4511         if(rerender) {
4512 // Update faders
4513                 gui->update_patchbay();
4514         }
4515
4516
4517         if(update_clock) {
4518                 if(!mwindow->cwindow->playback_engine->is_playing_back)
4519                         gui->mainclock->update(position);
4520         }
4521
4522         if(update_zoom) {
4523                 gui->zoombar->update();
4524         }
4525
4526         if(update_scroll) {
4527                 if(!drag_scroll &&
4528                         (cursor_x >= get_w() || cursor_x < 0 || cursor_y >= get_h() || cursor_y < 0))
4529                         start_dragscroll();
4530                 else
4531                 if(drag_scroll &&
4532                         (cursor_x < get_w() && cursor_x >= 0 && cursor_y < get_h() && cursor_y >= 0))
4533                         stop_dragscroll();
4534         }
4535
4536         if(update_overlay) {
4537                 gui->draw_overlays(1);
4538         }
4539
4540 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
4541         return result;
4542 }
4543
4544 int TrackCanvas::cursor_motion_event()
4545 {
4546         return cursor_update(1);
4547 }
4548
4549 void TrackCanvas::start_dragscroll()
4550 {
4551         if(!drag_scroll) {
4552                 drag_scroll = 1;
4553                 set_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4554 //printf("TrackCanvas::start_dragscroll 1\n");
4555         }
4556 }
4557
4558 void TrackCanvas::stop_dragscroll()
4559 {
4560         if(drag_scroll) {
4561                 drag_scroll = 0;
4562                 unset_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4563 //printf("TrackCanvas::stop_dragscroll 1\n");
4564         }
4565 }
4566
4567 int TrackCanvas::repeat_event(int64_t duration)
4568 {
4569         if(!drag_scroll) return 0;
4570         if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
4571
4572         int sample_movement = 0;
4573         int track_movement = 0;
4574         int64_t x_distance = 0;
4575         int64_t y_distance = 0;
4576         double position = 0;
4577         int result = 0;
4578
4579         switch(mwindow->session->current_operation) {
4580                 case SELECT_REGION:
4581 //printf("TrackCanvas::repeat_event 1 %d\n", mwindow->edl->local_session->view_start);
4582                 if(get_cursor_x() > get_w()) {
4583                                 x_distance = get_cursor_x() - get_w();
4584                                 sample_movement = 1;
4585                 }
4586                 else if(get_cursor_x() < 0) {
4587                         x_distance = get_cursor_x();
4588                         sample_movement = 1;
4589                 }
4590
4591                 if(get_cursor_y() > get_h()) {
4592                         y_distance = get_cursor_y() - get_h();
4593                         track_movement = 1;
4594                 }
4595                 else if(get_cursor_y() < 0) {
4596                         y_distance = get_cursor_y();
4597                         track_movement = 1;
4598                 }
4599                 result = 1;
4600                 break;
4601         }
4602
4603         if(sample_movement) {
4604                 position = (double)(get_cursor_x() +
4605                         mwindow->edl->local_session->view_start[pane->number] +
4606                         x_distance) *
4607                         mwindow->edl->local_session->zoom_sample /
4608                         mwindow->edl->session->sample_rate;
4609                 position = mwindow->edl->align_to_frame(position, 0);
4610                 position = MAX(position, 0);
4611
4612 //printf("TrackCanvas::repeat_event 1 %f\n", position);
4613                 switch(mwindow->session->current_operation) {
4614                 case SELECT_REGION:
4615                         if(position < selection_midpoint) {
4616                                 mwindow->edl->local_session->set_selectionend(selection_midpoint);
4617                                 mwindow->edl->local_session->set_selectionstart(position);
4618 // Que the CWindow
4619                                 gui->unlock_window();
4620                                 mwindow->cwindow->update(1, 0, 0);
4621                                 gui->lock_window("TrackCanvas::repeat_event");
4622 // Update the faders
4623                                 mwindow->update_plugin_guis();
4624                                 gui->update_patchbay();
4625                         }
4626                         else {
4627                                 mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4628                                 mwindow->edl->local_session->set_selectionend(position);
4629 // Don't que the CWindow
4630                         }
4631                         break;
4632                 }
4633
4634                 mwindow->samplemovement(
4635                         mwindow->edl->local_session->view_start[pane->number] + x_distance,
4636                         pane->number);
4637         }
4638
4639         if(track_movement) {
4640                 mwindow->trackmovement(y_distance, pane->number);
4641         }
4642
4643         return result;
4644 }
4645
4646 int TrackCanvas::button_release_event()
4647 {
4648         int redraw = -1, update_overlay = 0;
4649         int result = 0, load_flags = 0;
4650
4651 // printf("TrackCanvas::button_release_event %d\n",
4652 // mwindow->session->current_operation);
4653         if(active) {
4654                 switch(mwindow->session->current_operation) {
4655                 case DRAG_EDITHANDLE2:
4656                         mwindow->session->current_operation = NO_OPERATION;
4657                         drag_scroll = 0;
4658                         result = 1;
4659
4660                         end_edithandle_selection();
4661                         break;
4662
4663                 case DRAG_EDITHANDLE1:
4664                         mwindow->session->current_operation = NO_OPERATION;
4665                         drag_scroll = 0;
4666                         result = 1;
4667                         break;
4668
4669                 case DRAG_PLUGINHANDLE2:
4670                         mwindow->session->current_operation = NO_OPERATION;
4671                         drag_scroll = 0;
4672                         result = 1;
4673
4674                         end_pluginhandle_selection();
4675                         break;
4676
4677                 case DRAG_PLUGINHANDLE1:
4678                         mwindow->session->current_operation = NO_OPERATION;
4679                         drag_scroll = 0;
4680                         result = 1;
4681                         break;
4682
4683                 case DRAG_TRANSNHANDLE2:
4684                         mwindow->session->current_operation = NO_OPERATION;
4685                         drag_scroll = 0;
4686                         result = 1;
4687
4688                         end_transnhandle_selection();
4689                         break;
4690
4691                 case DRAG_TRANSNHANDLE1:
4692                         mwindow->session->current_operation = NO_OPERATION;
4693                         drag_scroll = 0;
4694                         result = 1;
4695                         break;
4696
4697                 case DRAG_SPEED:
4698                         redraw = FORCE_REDRAW;
4699                         load_flags |= LOAD_EDITS | LOAD_TIMEBAR;
4700                 case DRAG_FADE:
4701 // delete the drag_auto_gang first and remove out of order keys
4702                         clear_ganged_autos();
4703                 case DRAG_CZOOM:
4704                 case DRAG_PZOOM:
4705                 case DRAG_PLAY:
4706                 case DRAG_MUTE:
4707                 case DRAG_MASK:
4708                 case DRAG_MODE:
4709                 case DRAG_PAN:
4710                 case DRAG_CAMERA_X:
4711                 case DRAG_CAMERA_Y:
4712                 case DRAG_CAMERA_Z:
4713                 case DRAG_PROJECTOR_X:
4714                 case DRAG_PROJECTOR_Y:
4715                 case DRAG_PROJECTOR_Z:
4716                 case DRAG_PLUGINKEY:
4717                         load_flags |= LOAD_AUTOMATION;
4718                         mwindow->session->current_operation = NO_OPERATION;
4719                         mwindow->session->drag_handle = 0;
4720 // Remove any out-of-order keyframe
4721                         if(mwindow->session->drag_auto) {
4722                                 mwindow->session->drag_auto->autos->remove_nonsequential(
4723                                                 mwindow->session->drag_auto);
4724 //                              mwindow->session->drag_auto->autos->optimize();
4725                                 update_overlay = 1;
4726                         }
4727
4728                         mwindow->undo->update_undo_after(_("keyframe"), load_flags);
4729                         result = 1;
4730                         break;
4731
4732                 case DRAG_EDIT:
4733                 case DRAG_GROUP:
4734                 case DRAG_AEFFECT_COPY:
4735                 case DRAG_VEFFECT_COPY:
4736 // Trap in drag stop
4737                         break;
4738
4739                 case DROP_TARGETING: {
4740                         int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
4741                         Track *track=0;  Edit *edit=0;  PluginSet *pluginset=0;  Plugin *plugin=0;
4742                         drag_cursor_motion(cursor_x, cursor_y,
4743                                 &track, &edit, &pluginset, &plugin);
4744                         double position =
4745                                 mwindow->edl->get_cursor_position(cursor_x, pane->number);
4746                         gui->edit_menu->activate_menu(track, edit, pluginset, plugin, position);
4747                         mwindow->session->current_operation = NO_OPERATION;
4748                         result = 1;
4749                         break; }
4750
4751                 case GROUP_TOGGLE: {
4752                         Edit *edit = mwindow->session->drag_edit;
4753                         if( edit ) {
4754                                 if( shift_down() && edit->is_selected ) {
4755                                         if( edit->group_id > 0 ) {
4756                                                 mwindow->edl->tracks->clear_selected_edits();
4757                                                 mwindow->edl->tracks->del_group(edit->group_id);
4758                                         }
4759                                         else {
4760                                                 int id = mwindow->session->group_number++;
4761                                                 mwindow->edl->tracks->new_group(id);
4762                                         }
4763                                         redraw = 0;
4764                                 }
4765                                 else {
4766                                         if( mwindow->preferences->ctrl_toggle && !ctrl_down() )
4767                                                 mwindow->edl->tracks->clear_selected_edits();
4768                                         edit->set_selected(-1);
4769                                 }
4770                         }
4771                         mwindow->session->current_operation = NO_OPERATION;
4772                         update_overlay = 1;
4773                         result = 1;
4774                         drag_scroll = 0;
4775                         break; }
4776
4777                 default:
4778                         if( mwindow->session->current_operation ) {
4779 //                              if(mwindow->session->current_operation == SELECT_REGION) {
4780 //                                      mwindow->undo->update_undo_after(_("select"), LOAD_SESSION, 0, 0);
4781 //                              }
4782
4783                                 mwindow->session->current_operation = NO_OPERATION;
4784                                 drag_scroll = 0;
4785                                 //result = 0;
4786                                 break;
4787                         }
4788                         break;
4789                 }
4790         }
4791
4792         if( mwindow->edl->local_session->zoombar_showautocolor >= 0 )
4793                 mwindow->gui->zoombar->update_autozoom(-1);
4794
4795         if (result)
4796                 cursor_update(0);
4797
4798         if(update_overlay) {
4799                 gui->draw_overlays(1);
4800         }
4801         if(redraw >= 0) {
4802                 gui->draw_canvas(redraw, 0);
4803                 gui->flash_canvas(1);
4804         }
4805         return result;
4806 }
4807
4808 int TrackCanvas::do_edit_handles(int cursor_x, int cursor_y, int button_press,
4809         int &rerender, int &update_overlay, int &new_cursor, int &update_cursor)
4810 {
4811         Edit *edit_result = 0;
4812         int handle_result = -1;
4813         int result = 0;
4814
4815         for( Track *track=mwindow->edl->tracks->first; track && !result; track=track->next) {
4816                 if( track->is_hidden() ) continue;
4817                 for( Edit *edit=track->edits->first; edit && !result; edit=edit->next ) {
4818                         int64_t edit_x, edit_y, edit_w, edit_h;
4819                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4820
4821                         if( cursor_x >= edit_x && cursor_x <= edit_x + edit_w &&
4822                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h ) {
4823                                 if( cursor_x < edit_x + HANDLE_W ) {
4824                                         edit_result = edit;
4825                                         handle_result = 0;
4826                                         if( cursor_y >= edit_y+edit_h - HANDLE_W &&
4827                                             track->show_assets() ) {
4828                                                 new_cursor = DOWNLEFT_RESIZE;
4829                                                 if( button_press == LEFT_BUTTON )
4830                                                         result = -1;
4831                                         }
4832                                         else
4833                                                 result = 1;
4834                                 }
4835                                 else if( cursor_x >= edit_x + edit_w - HANDLE_W ) {
4836                                         edit_result = edit;
4837                                         handle_result = 1;
4838                                         if( cursor_y >= edit_y+edit_h - HANDLE_W &&
4839                                             track->show_assets() ) {
4840                                                 new_cursor = DOWNRIGHT_RESIZE;
4841                                                 if( button_press == LEFT_BUTTON )
4842                                                         result = -1;
4843                                         }
4844                                         else
4845                                                 result = 1;
4846                                 }
4847                         }
4848                 }
4849         }
4850
4851         if( result > 0 ) {
4852                 int group_id = edit_result->group_id;
4853                 if( group_id > 0 ) {
4854                         Track *track = edit_result->track;
4855                         Edit *left = 0, *right = 0;
4856                         double start = DBL_MAX, end = DBL_MIN;
4857                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
4858                                 if( edit->group_id != group_id ) continue;
4859                                 double edit_start = edit->track->from_units(edit->startproject);
4860                                 if( edit_start < start ) { start = edit_start;  left = edit; }
4861                                 double edit_end = edit->track->from_units(edit->startproject+edit->length);
4862                                 if( edit_end > end ) { end = edit_end;  right = edit; }
4863                         }
4864                         Edit *edit = !handle_result ? left : right;
4865                         if( edit != edit_result )
4866                                 result = 0;
4867                 }
4868         }
4869
4870         update_cursor = 1;
4871         if( result > 0 ) {
4872                 double position = 0;
4873                 if( handle_result == 0 ) {
4874                         position = edit_result->track->from_units(edit_result->startproject);
4875                         new_cursor = LEFT_CURSOR;
4876                 }
4877                 else if( handle_result == 1 ) {
4878                         position = edit_result->track->from_units(edit_result->startproject + edit_result->length);
4879                         new_cursor = RIGHT_CURSOR;
4880                 }
4881
4882 // Reposition cursor
4883                 if( button_press ) {
4884                         mwindow->session->drag_edit = edit_result;
4885                         mwindow->session->drag_handle = handle_result;
4886                         mwindow->session->drag_button = get_buttonpress() - 1;
4887                         mwindow->session->drag_position = position;
4888                         mwindow->session->current_operation = DRAG_EDITHANDLE1;
4889                         mwindow->session->drag_origin_x = get_cursor_x();
4890                         mwindow->session->drag_origin_y = get_cursor_y();
4891                         mwindow->session->drag_start = position;
4892
4893                         rerender = start_selection(position);
4894                         update_overlay = 1;
4895                 }
4896         }
4897         else if( result < 0 && !edit_result->silence() ) {
4898                 mwindow->undo->update_undo_before();
4899                 if( !shift_down() ) {
4900                         if( handle_result == 0 )
4901                                 edit_result->hard_left = !edit_result->hard_left;
4902                         else if( handle_result == 1 )
4903                                 edit_result->hard_right = !edit_result->hard_right;
4904                 }
4905                 else {
4906                         int status = handle_result == 0 ? edit_result->hard_left :
4907                                      handle_result == 1 ? edit_result->hard_right : 0;
4908                         int new_status = !status;
4909                         int64_t edit_edge = edit_result->startproject;
4910                         if( handle_result == 1 ) edit_edge += edit_result->length;
4911                         double edge_position = edit_result->track->from_units(edit_edge);
4912                         for( Track *track=mwindow->edl->tracks->first; track!=0; track=track->next ) {
4913                                 if( track->is_hidden() ) continue;
4914                                 int64_t track_position = track->to_units(edge_position, 1);
4915                                 Edit *left_edit = track->edits->editof(track_position, PLAY_FORWARD, 0);
4916                                 if( left_edit ) {
4917                                         int64_t left_edge = left_edit->startproject;
4918                                         double left_position = track->from_units(left_edge);
4919                                         if( EQUIV(edge_position, left_position) ) {
4920                                                 left_edit->hard_left = new_status;
4921                                                 if( left_edit->previous )
4922                                                         left_edit->previous->hard_right = new_status;
4923                                         }
4924                                 }
4925                                 Edit *right_edit = track->edits->editof(track_position, PLAY_REVERSE, 0);
4926                                 if( right_edit ) {
4927                                         int64_t right_edge = right_edit->startproject + right_edit->length;
4928                                         double right_position = track->from_units(right_edge);
4929                                         if( EQUIV(edge_position, right_position) ) {
4930                                                 right_edit->hard_right = new_status;
4931                                                 if( right_edit->next )
4932                                                         right_edit->next->hard_left = new_status;
4933                                         }
4934                                 }
4935                         }
4936                 }
4937                 rerender = update_overlay = 1;
4938                 mwindow->undo->update_undo_after(_("hard_edge"), LOAD_EDITS);
4939                 result = 1;
4940         }
4941
4942         return result;
4943 }
4944
4945 int TrackCanvas::do_plugin_handles(int cursor_x,
4946         int cursor_y,
4947         int button_press,
4948         int &rerender,
4949         int &update_overlay,
4950         int &new_cursor,
4951         int &update_cursor)
4952 {
4953         Plugin *plugin_result = 0;
4954         int handle_result = 0;
4955         int result = 0;
4956
4957         for(Track *track = mwindow->edl->tracks->first;
4958                 track && !result;
4959                 track = track->next) {
4960                 if( track->is_hidden() ) continue;
4961                 for(int i = 0; i < track->plugin_set.total && !result; i++) {
4962                         PluginSet *plugin_set = track->plugin_set.values[i];
4963                         for(Plugin *plugin = (Plugin*)plugin_set->first;
4964                                 plugin && !result;
4965                                 plugin = (Plugin*)plugin->next) {
4966                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
4967                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
4968
4969                                 if(cursor_x >= plugin_x && cursor_x <= plugin_x + plugin_w &&
4970                                         cursor_y >= plugin_y && cursor_y < plugin_y + plugin_h) {
4971                                         if(cursor_x < plugin_x + HANDLE_W) {
4972                                                 plugin_result = plugin;
4973                                                 handle_result = 0;
4974                                                 result = 1;
4975                                         }
4976                                         else if(cursor_x >= plugin_x + plugin_w - HANDLE_W) {
4977                                                 plugin_result = plugin;
4978                                                 handle_result = 1;
4979                                                 result = 1;
4980                                         }
4981                                 }
4982                         }
4983
4984                         if(result && shift_down())
4985                                 mwindow->session->trim_edits = plugin_set;
4986                 }
4987         }
4988
4989         update_cursor = 1;
4990         if(result) {
4991                 double position = 0;
4992                 if(handle_result == 0) {
4993                         position = plugin_result->track->from_units(plugin_result->startproject);
4994                         new_cursor = LEFT_CURSOR;
4995                 }
4996                 else if(handle_result == 1) {
4997                         position = plugin_result->track->from_units(plugin_result->startproject + plugin_result->length);
4998                         new_cursor = RIGHT_CURSOR;
4999                 }
5000
5001                 if(button_press) {
5002                         mwindow->session->drag_plugin = plugin_result;
5003                         mwindow->session->drag_handle = handle_result;
5004                         mwindow->session->drag_button = get_buttonpress() - 1;
5005                         mwindow->session->drag_position = position;
5006                         mwindow->session->current_operation = DRAG_PLUGINHANDLE1;
5007                         mwindow->session->drag_origin_x = get_cursor_x();
5008                         mwindow->session->drag_origin_y = get_cursor_y();
5009                         mwindow->session->drag_start = position;
5010
5011                         rerender = start_selection(position);
5012                         update_overlay = 1;
5013                 }
5014         }
5015
5016         return result;
5017 }
5018
5019 int TrackCanvas::do_transition_handles(int cursor_x, int cursor_y, int button_press,
5020                 int &rerender, int &update_overlay, int &new_cursor, int &update_cursor)
5021 {
5022         Transition *trans_result = 0;
5023         int result = 0;
5024
5025         Track *track = mwindow->edl->tracks->first;
5026         for( ; track && !result; track=track->next) {
5027                 if( track->is_hidden() ) continue;
5028                 Edit *edit = track->edits->first;
5029                 for( ; edit && !result; edit=edit->next ) {
5030                         Transition *trans = edit->transition;
5031                         if( !trans ) continue;
5032                         int64_t x, y, w, h;
5033                         edit_dimensions(edit, x, y, w, h);
5034                         int strip_x = x, edit_y = y;
5035                         get_transition_coords(edit, x, y, w, h);
5036                         VFrame *strip = mwindow->theme->get_image("plugin_bg_data");
5037                         int strip_y = y - strip->get_h();
5038                         if( track->show_assets() && track->show_titles() )
5039                                 edit_y += mwindow->theme->get_image("title_bg_data")->get_h();
5040                         if( strip_y < edit_y ) strip_y = edit_y;
5041                         int strip_w = Units::round(edit->track->from_units(edit->transition->length) *
5042                                 mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample);
5043                         int x1 = strip_x + strip_w - HANDLE_W/2, x2 = x1 + HANDLE_W;
5044                         int y1 = strip_y + strip->get_h()/2 - HANDLE_H/2, y2 = y1 + HANDLE_W;
5045                         if( cursor_x >= x1 && cursor_x < x2 &&
5046                             cursor_y >= y1 && cursor_y < y2 ) {
5047                                 trans_result = trans;
5048                                 result = 1;
5049                         }
5050                 }
5051         }
5052
5053         if( result ) {
5054                 if( button_press ) {
5055                         mwindow->session->drag_transition = trans_result;
5056                         mwindow->session->drag_handle = 1;
5057                         mwindow->session->drag_button = get_buttonpress() - 1;
5058                         int64_t trans_end = trans_result->edit->startproject + trans_result->length;
5059                         double position = trans_result->edit->track->from_units(trans_end);
5060                         mwindow->session->drag_position = position;
5061                         mwindow->session->drag_start = position;
5062                         mwindow->session->current_operation = DRAG_TRANSNHANDLE1;
5063                         mwindow->session->drag_origin_x = get_cursor_x();
5064                         mwindow->session->drag_origin_y = get_cursor_y();
5065                         update_cursor = 1;
5066                 }
5067                 new_cursor = RIGHT_CURSOR;
5068                 update_overlay = 1;
5069         }
5070
5071         return result;
5072 }
5073
5074 int TrackCanvas::drag_transition_handle(double position)
5075 {
5076         Transition *transition = mwindow->session->drag_transition;
5077         if( !transition ) return 1;
5078         mwindow->session->drag_position = position;
5079         mwindow->edl->local_session->set_selectionstart(position);
5080         mwindow->edl->local_session->set_selectionend(position);
5081         char string[BCSTRLEN];
5082         int64_t length = transition->length;
5083         Track *track = transition->edit->track;
5084         int64_t start_pos = track->to_units(mwindow->session->drag_start, 0);
5085         int64_t end_pos = track->to_units(mwindow->session->drag_position, 0);
5086         length += end_pos - start_pos;
5087         if( length < 0 ) length = 0;
5088         double time = track->from_units(length);
5089         Units::totext(string, time,
5090                 mwindow->edl->session->time_format,
5091                 mwindow->edl->session->sample_rate,
5092                 mwindow->edl->session->frame_rate,
5093                 mwindow->edl->session->frames_per_foot);
5094         mwindow->gui->show_message(string);
5095         if( mwindow->gui->transition_menu->length_thread->running() ) {
5096                 TransitionLengthDialog *dialog = (TransitionLengthDialog *)
5097                          mwindow->gui->transition_menu->length_thread->get_gui();
5098                 if( dialog ) {
5099                         dialog->lock_window("TrackCanvas::drag_transition_handle");
5100                         dialog->update_text(time);
5101                         dialog->thread->new_length = time;
5102                         dialog->unlock_window();
5103                 }
5104         }
5105         return 0;
5106 }
5107
5108
5109 int TrackCanvas::do_tracks(int cursor_x, int cursor_y, int button_press)
5110 {
5111         int result = 0;
5112
5113         Track *track=0;  Edit *edit=0;  PluginSet *pluginset=0;  Plugin *plugin=0;
5114         drag_cursor_motion(cursor_x, cursor_y,
5115                 &track, &edit, &pluginset, &plugin);
5116
5117         if( button_press && track ) {
5118                 switch( get_buttonpress() ) {
5119                 case RIGHT_BUTTON: {
5120                         double position =
5121                                 mwindow->edl->get_cursor_position(cursor_x, pane->number);
5122                         gui->track_menu->activate_menu(track, edit, pluginset, plugin, position);
5123                         mwindow->session->current_operation = NO_OPERATION;
5124                         result = 1;
5125                         break; }
5126                 case MIDDLE_BUTTON:
5127                         set_cursor(GRABBED_CURSOR, 0, 1);
5128                         mwindow->session->current_operation = DROP_TARGETING;
5129                         result = 1;
5130                         break;
5131                 }
5132         }
5133
5134         return result;
5135 }
5136
5137 int TrackCanvas::arrow_mode()
5138 {
5139         return mwindow->edl->session->editing_mode == EDITING_ARROW ? 1 : 0;
5140 }
5141 int TrackCanvas::ibeam_mode()
5142 {
5143         return mwindow->edl->session->editing_mode == EDITING_IBEAM ? 1 : 0;
5144 }
5145
5146 int TrackCanvas::do_edits(int cursor_x, int cursor_y, int button_press, int drag_start,
5147         int &redraw, int &rerender, int &new_cursor, int &update_cursor)
5148 {
5149         int result = 0;
5150
5151         for(Track *track = mwindow->edl->tracks->first; track && !result; track = track->next) {
5152                 if( track->is_hidden() ) continue;
5153                 for(Edit *edit = track->edits->first; edit && !result; edit = edit->next) {
5154                         int64_t edit_x, edit_y, edit_w, edit_h;
5155                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
5156
5157 // Cursor inside a track
5158 // Cursor inside an edit
5159                         if( cursor_x >= edit_x && cursor_x < edit_x + edit_w &&
5160                             cursor_y >= edit_y && cursor_y < edit_y + edit_h ) {
5161                                 if( button_press && get_buttonpress() == LEFT_BUTTON ) {
5162                                         if( get_double_click() ) {
5163                                                 mwindow->edl->tracks->clear_selected_edits();
5164                                                 edit->select_affected_edits(1, -1);
5165                                                 double start = edit->track->from_units(edit->startproject);
5166                                                 start = mwindow->edl->align_to_frame(start, 0);
5167                                                 mwindow->edl->local_session->set_selectionstart(start);
5168                                                 double end = edit->track->from_units(edit->startproject+edit->length);
5169                                                 end = mwindow->edl->align_to_frame(end, 0);
5170                                                 mwindow->edl->local_session->set_selectionend(end);
5171                                                 result = 1; // Select edit duration or select_region
5172                                         }
5173                                         else if( arrow_mode() || (ibeam_mode() && ctrl_down()) ) {
5174                                                 mwindow->session->drag_edit = edit;
5175                                                 mwindow->session->current_operation = GROUP_TOGGLE;
5176                                                 result = 1;
5177                                         }
5178                                         if( result ) {
5179                                                 rerender = 1;
5180                                                 update_cursor = -1;
5181                                         }
5182                                 }
5183                                 else if( drag_start && track->is_armed() ) {
5184                                         mwindow->session->drag_edit = edit;
5185                                         mwindow->session->drag_origin_x = cursor_x;
5186                                         mwindow->session->drag_origin_y = cursor_y;
5187 // Where the drag started, so we know relative position inside the edit later
5188                                         mwindow->session->drag_position =
5189                                                 mwindow->edl->get_cursor_position(cursor_x, pane->number);
5190                                         drag_start = 0; // if unselected "fast" drag
5191                                         if( !edit->silence() && !edit->is_selected ) {
5192                                                 mwindow->edl->tracks->clear_selected_edits();
5193                                                 if( ibeam_mode() ) {
5194                                                         double start = edit->track->from_units(edit->startproject);
5195                                                         mwindow->edl->local_session->set_selectionstart(start);
5196                                                         mwindow->edl->local_session->set_selectionend(start);
5197                                                         edit->set_selected(1);
5198                                                 }
5199                                                 else
5200                                                         edit->select_affected_edits(1, -1);
5201                                                 drag_start = 1;
5202                                         }
5203 // Construct list of all affected edits
5204                                         if( drag_start || ibeam_mode() ) {
5205                                                 mwindow->edl->tracks->get_selected_edits(mwindow->session->drag_edits);
5206                                                 if( mwindow->session->drag_edits->size() > 0 ) {
5207                                                         mwindow->session->current_operation = DRAG_EDIT;
5208 // Need to create drag window
5209                                                         int cx, cy;  get_abs_cursor(cx, cy);
5210                                                         gui->drag_popup = new BC_DragWindow(gui,
5211                                                                 mwindow->theme->get_image("clip_icon"), cx, cy);
5212                                                         result = 1;
5213                                                 }
5214                                                 else {
5215                                                         rerender = start_selection(mwindow->session->drag_position);
5216                                                         mwindow->session->current_operation = SELECT_REGION;
5217                                                         update_cursor = 1;
5218                                                 }
5219                                         }
5220                                         else if( edit->is_selected && arrow_mode() ) {
5221                                                 if( mwindow->session->drag_group )
5222                                                         mwindow->session->drag_group->remove_user();
5223                                                 double start_position = 0;
5224                                                 mwindow->session->drag_group =
5225                                                         mwindow->edl->selected_edits_to_clip(0, &start_position,
5226                                                                 &mwindow->session->drag_group_first_track);
5227                                                 if( mwindow->session->drag_group ) {
5228                                                         mwindow->session->current_operation = DRAG_GROUP;
5229                                                         mwindow->session->drag_group_position = start_position;
5230                                                         mwindow->session->drag_group_edit = edit;
5231                                                         mwindow->session->drag_origin_y = edit_y;
5232                                                         result = 1;
5233                                                 }
5234                                         }
5235                                 }
5236                         }
5237                 }
5238         }
5239         return result;
5240 }
5241
5242 // returns -1=doesnt fit, 1=fits, 0=fits but overwrites
5243 int TrackCanvas::test_track_group(EDL *group, Track *first_track, double &pos)
5244 {
5245         int intersects = 0;
5246         Track *src = group->tracks->first;
5247         for( Track *track=first_track; track && src; track=track->next ) {
5248                 if( !track->is_armed() ) return -1;
5249                 if( src->data_type != track->data_type ) return -1;
5250                 for( Edit *src_edit=src->edits->first; src_edit; src_edit=src_edit->next ) {
5251                         if( src_edit->silence() ) continue;
5252                         if( !intersects && edit_intersects(track, src_edit, pos) )
5253                                 intersects = 1;
5254                 }
5255                 src = src->next;
5256         }
5257         return src ? -1 : !intersects ? 1 : 0;
5258 }
5259
5260 int TrackCanvas::edit_intersects(Track *track, Edit *src_edit, double &pos)
5261 {
5262         if( pos < 0 ) { pos = 0;  return 1; }
5263         int pane_no = pane->number;
5264         int cur_pix = track->edl->get_position_cursorx(pos, pane_no);
5265         int64_t src_start = src_edit->startproject;
5266         int64_t src_end = src_start + src_edit->length;
5267         double new_start = src_edit->track->from_units(src_start) + pos;
5268         double new_end = src_edit->track->from_units(src_end) + pos;
5269         int64_t trk_start = track->to_units(new_start, 1);
5270         int64_t trk_end = track->to_units(new_end, 1);
5271         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
5272                 if( edit->is_selected || edit->silence() ) continue;
5273                 int64_t edit_start = edit->startproject;
5274                 if( edit_start >= trk_end ) continue;
5275                 int64_t edit_end = edit_start + edit->length;
5276                 if( trk_start >= edit_end ) continue;
5277
5278                 int64_t lt_dist = labs(trk_end - edit_start);
5279                 int64_t rt_dist = labs(edit_end - trk_start);
5280                 int64_t position;
5281                 if( lt_dist < rt_dist ) {
5282                         position = edit_start;
5283                         lt_dist = abs(trk_start - edit_start);
5284                         rt_dist = abs(trk_end - edit_start);
5285                         if( lt_dist > rt_dist )
5286                                 position -= src_end;
5287                 }
5288                 else {
5289                         position = edit_end;
5290                         lt_dist = abs(trk_start - edit_end);
5291                         rt_dist = abs(trk_end - edit_end);
5292                         if( lt_dist > rt_dist )
5293                                 position -= src_end;
5294                 }
5295                 double new_pos = edit->track->from_units(position);
5296                 int new_pix = track->edl->get_position_cursorx(new_pos, pane_no);
5297                 if( abs(new_pix-cur_pix) < HANDLE_W )
5298                         pos = new_pos;
5299                 return 1;
5300         }
5301         return 0;
5302 }
5303
5304 int TrackCanvas::test_resources(int cursor_x, int cursor_y)
5305 {
5306         return 0;
5307 }
5308
5309 int TrackCanvas::do_plugins(int cursor_x, int cursor_y, int drag_start,
5310         int button_press, int &redraw, int &rerender)
5311 {
5312         Plugin *plugin = 0;
5313         int result = 0;
5314         int done = 0;
5315         int64_t x, y, w, h;
5316         Track *track = 0;
5317
5318         for(track = mwindow->edl->tracks->first; track && !done; track = track->next) {
5319                 if(!track->expand_view) continue;
5320
5321                 for(int i = 0; i < track->plugin_set.total && !done; i++) {
5322                         // first check if plugins are visible at all
5323                         if (!track->expand_view)
5324                                 continue;
5325                         PluginSet *plugin_set = track->plugin_set.values[i];
5326                         for(plugin = (Plugin*)plugin_set->first;
5327                                 plugin && !done;
5328                                 plugin = (Plugin*)plugin->next) {
5329                                 plugin_dimensions(plugin, x, y, w, h);
5330                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
5331                                         MWindowGUI::visible(y, y + h, 0, get_h())) {
5332                                         if(cursor_x >= x && cursor_x < x + w &&
5333                                                 cursor_y >= y && cursor_y < y + h) {
5334                                                 done = 1;
5335                                                 break;
5336                                         }
5337                                 }
5338                         }
5339                 }
5340         }
5341
5342         if(plugin) {
5343 // Start plugin popup
5344                 if(button_press) {
5345                         if(get_buttonpress() == RIGHT_BUTTON ) {
5346                                 gui->plugin_menu->update(plugin);
5347                                 gui->plugin_menu->activate_menu();
5348                                 result = 1;
5349                         }
5350                         else if (get_double_click() && !drag_start) {
5351 // Select range of plugin on doubleclick over plugin
5352                                 mwindow->edl->local_session->set_selectionstart(plugin->track->from_units(plugin->startproject));
5353                                 mwindow->edl->local_session->set_selectionend(plugin->track->from_units(plugin->startproject) +
5354                                         plugin->track->from_units(plugin->length));
5355                                 if(mwindow->edl->session->cursor_on_frames) {
5356                                         mwindow->edl->local_session->set_selectionstart(
5357                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
5358                                         mwindow->edl->local_session->set_selectionend(
5359                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
5360                                 }
5361                                 rerender = 1;
5362                                 redraw = 1;
5363                                 result = 1;
5364                         }
5365                 }
5366                 else
5367 // Move plugin
5368                 if( drag_start && plugin->track->is_armed() && !plugin->silence() ) {
5369                         if( mwindow->edl->session->editing_mode == EDITING_ARROW ) {
5370                                 if( plugin->track->data_type == TRACK_AUDIO )
5371                                         mwindow->session->current_operation = DRAG_AEFFECT_COPY;
5372                                 else if( plugin->track->data_type == TRACK_VIDEO )
5373                                         mwindow->session->current_operation = DRAG_VEFFECT_COPY;
5374
5375                                 mwindow->session->drag_plugin = plugin;
5376                                 mwindow->session->drag_origin_x = cursor_x;
5377                                 mwindow->session->drag_origin_y = cursor_y;
5378                                 // Where the drag started, so we know relative position inside the edit later
5379                                 mwindow->session->drag_position =
5380                                         (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
5381                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
5382 // Create picon
5383                                 switch(plugin->plugin_type) {
5384                                 case PLUGIN_STANDALONE: {
5385                                         PluginServer *server =
5386                                                 mwindow->scan_plugindb(plugin->title, plugin->track->data_type);
5387                                         if( !server ) break;
5388                                         VFrame *frame = server->picon;
5389                                         if(!frame) {
5390                                                 if(plugin->track->data_type == TRACK_AUDIO) {
5391                                                         frame = mwindow->theme->get_image("aeffect_icon");
5392                                                 }
5393                                                 else {
5394                                                         frame = mwindow->theme->get_image("veffect_icon");
5395                                                 }
5396                                         }
5397                                         int cx, cy;
5398                                         get_abs_cursor(cx, cy);
5399                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
5400                                                 break; }
5401
5402                                 case PLUGIN_SHAREDPLUGIN:
5403                                 case PLUGIN_SHAREDMODULE: {
5404                                         VFrame *frame = mwindow->theme->get_image("clip_icon");
5405                                         int cx, cy;
5406                                         get_abs_cursor(cx, cy);
5407                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
5408                                         break; }
5409                                 }
5410
5411                                 result = 1;
5412                         }
5413                 }
5414         }
5415
5416         return result;
5417 }
5418
5419 int TrackCanvas::do_transitions(int cursor_x, int cursor_y,
5420         int button_press, int &new_cursor, int &update_cursor)
5421 {
5422         Transition *transition = 0;
5423         int result = 0;
5424         int64_t x, y, w, h;
5425
5426
5427         for( Track *track = mwindow->edl->tracks->first; track && !result; track = track->next ) {
5428                 if( track->is_hidden() ) continue;
5429                 if( !track->show_transitions() ) continue;
5430
5431                 for( Edit *edit = track->edits->first; edit; edit = edit->next ) {
5432                         if( edit->transition ) {
5433                                 edit_dimensions(edit, x, y, w, h);
5434                                 get_transition_coords(edit, x, y, w, h);
5435
5436                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
5437                                         MWindowGUI::visible(y, y + h, 0, get_h()) ) {
5438                                         if( cursor_x >= x && cursor_x < x + w &&
5439                                                 cursor_y >= y && cursor_y < y + h ) {
5440                                                 transition = edit->transition;
5441                                                 result = 1;
5442                                                 break;
5443                                         }
5444                                 }
5445                         }
5446                 }
5447         }
5448
5449         update_cursor = 1;
5450         if(transition) {
5451                 if(!button_press) {
5452                         new_cursor = UPRIGHT_ARROW_CURSOR;
5453                 }
5454                 else if(get_buttonpress() == RIGHT_BUTTON ) {
5455                         gui->transition_menu->update(transition);
5456                         gui->transition_menu->activate_menu();
5457                 }
5458         }
5459
5460         return result;
5461 }
5462
5463 int TrackCanvas::button_press_event()
5464 {
5465         int result = 0;
5466         int cursor_x, cursor_y;
5467         int new_cursor;
5468
5469         cursor_x = get_cursor_x();
5470         cursor_y = get_cursor_y();
5471         mwindow->session->trim_edits = 0;
5472
5473         if(is_event_win() && cursor_inside()) {
5474 //              double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
5475
5476                 result = 1;
5477                 if(!active) {
5478                         activate();
5479                 }
5480
5481                 if( get_buttonpress() == LEFT_BUTTON ) {
5482                         gui->stop_transport("TrackCanvas::button_press_event");
5483                 }
5484
5485                 int update_overlay = 0, update_cursor = 0, rerender = 0, update_message = 0;
5486
5487                 if(get_buttonpress() == WHEEL_UP) {
5488                         if(shift_down())
5489                                 mwindow->expand_sample();
5490                         else if(ctrl_down())
5491                                 mwindow->move_left(get_w()/ 10);
5492                         else
5493                                 mwindow->move_up(get_h() / 10);
5494                 }
5495                 else if(get_buttonpress() == WHEEL_DOWN) {
5496                         if(shift_down())
5497                                 mwindow->zoom_in_sample();
5498                         else if(ctrl_down())
5499                                 mwindow->move_right(get_w() / 10);
5500                         else
5501                                 mwindow->move_down(get_h() / 10);
5502                 }
5503                 else if(get_buttonpress() == 6) {
5504                         if(ctrl_down())
5505                                 mwindow->move_left(get_w());
5506                         else if(alt_down())
5507                                 mwindow->move_left(get_w() / 20);
5508                         else
5509                                 mwindow->move_left(get_w() / 5);
5510                 }
5511                 else if(get_buttonpress() == 7) {
5512                         if(ctrl_down())
5513                                 mwindow->move_right(get_w());
5514                         else if(alt_down())
5515                                 mwindow->move_right(get_w() / 20);
5516                         else
5517                                 mwindow->move_right(get_w() / 5);
5518                 }
5519                 else {
5520                         if( arrow_mode() ) do {
5521 // Test handles and resource boundaries and highlight a track
5522                                 if( do_transitions(cursor_x, cursor_y,
5523                                                 1, new_cursor, update_cursor) ) break;
5524
5525                                 if( do_keyframes(cursor_x, cursor_y,
5526                                         0, get_buttonpress(), new_cursor,
5527                                         update_cursor, rerender) ) break;
5528                                 update_message = 1;
5529
5530                                 if( do_transition_handles(cursor_x, cursor_y,
5531                                         1, rerender, update_overlay, new_cursor,
5532                                         update_cursor) ) break;
5533 // Test edit boundaries
5534                                 if( do_edit_handles(cursor_x, cursor_y,
5535                                         1, rerender, update_overlay, new_cursor,
5536                                         update_cursor) ) break;
5537 // Test plugin boundaries
5538                                 if( do_plugin_handles(cursor_x, cursor_y,
5539                                         1, rerender, update_overlay, new_cursor,
5540                                         update_cursor) ) break;
5541
5542                                 if( do_edits(cursor_x, cursor_y, 1, 0,
5543                                         update_overlay, rerender, new_cursor,
5544                                         update_cursor) ) break;
5545
5546                                 if( do_plugins(cursor_x, cursor_y, 0, 1,
5547                                         update_cursor, rerender) ) break;
5548
5549                                 if( test_resources(cursor_x, cursor_y) ) break;
5550
5551                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
5552
5553                                 result = 0;
5554                         } while(0);
5555                         else if( ibeam_mode() ) do {
5556 // Test handles only and select a region
5557                                 double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
5558 //printf("TrackCanvas::button_press_event %d\n", position);
5559
5560                                 if( do_transitions(cursor_x, cursor_y,
5561                                                 1, new_cursor, update_cursor)) break;
5562                                 if(do_keyframes(cursor_x, cursor_y,
5563                                         0, get_buttonpress(), new_cursor,
5564                                         update_cursor, rerender)) {
5565                                         update_overlay = 1;
5566                                         break;
5567                                 }
5568                                 update_message = 1;
5569                                 if( do_transition_handles(cursor_x, cursor_y,
5570                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
5571 // Test edit boundaries
5572                                 if( do_edit_handles(cursor_x, cursor_y,
5573                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
5574 // Test plugin boundaries
5575                                 if( do_plugin_handles(cursor_x, cursor_y,
5576                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
5577
5578                                 if( do_edits(cursor_x, cursor_y,
5579                                         1, 0, update_cursor, rerender, new_cursor, update_cursor) ) break;
5580
5581                                 if( do_plugins(cursor_x, cursor_y, 0, 1, update_cursor, rerender) ) break;
5582
5583                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
5584 // Highlight selection
5585                                 if( get_buttonpress() != LEFT_BUTTON ) break;
5586                                 rerender = start_selection(position);
5587                                 mwindow->session->current_operation = SELECT_REGION;
5588                                 update_cursor = 1;
5589                         } while(0);
5590                 }
5591
5592                 if( rerender ) {
5593                         gui->unlock_window();
5594                         mwindow->cwindow->update(1, 0, 0, 0, 1);
5595                         gui->lock_window("TrackCanvas::button_press_event 2");
5596 // Update faders
5597                         mwindow->update_plugin_guis();
5598                         gui->update_patchbay();
5599                 }
5600
5601                 if( update_message )
5602                         gui->default_message();
5603
5604                 if( update_overlay )
5605                         gui->draw_overlays(1);
5606
5607                 if( update_cursor < 0 ) {
5608 // double_click edit
5609                         gui->swindow->update_selection();
5610                         gui->draw_canvas(0, 0);
5611                 }
5612                 if( update_cursor ) {
5613                         gui->update_timebar(0);
5614                         gui->hide_cursor(0);
5615                         gui->show_cursor(1);
5616                         gui->zoombar->update();
5617                         gui->flash_canvas(1);
5618                 }
5619         }
5620
5621         return result;
5622 }
5623
5624 int TrackCanvas::start_selection(double position)
5625 {
5626         int rerender = 0;
5627         position = mwindow->edl->align_to_frame(position, 1);
5628
5629
5630 // Extend a border
5631         if(shift_down())
5632         {
5633                 double midpoint = (mwindow->edl->local_session->get_selectionstart(1) +
5634                         mwindow->edl->local_session->get_selectionend(1)) / 2;
5635
5636                 if(position < midpoint)
5637                 {
5638                         mwindow->edl->local_session->set_selectionstart(position);
5639                         selection_midpoint = mwindow->edl->local_session->get_selectionend(1);
5640 // Que the CWindow
5641                         rerender = 1;
5642                 }
5643                 else
5644                 {
5645                         mwindow->edl->local_session->set_selectionend(position);
5646                         selection_midpoint = mwindow->edl->local_session->get_selectionstart(1);
5647 // Don't que the CWindow for the end
5648                 }
5649         }
5650         else
5651 // Start a new selection
5652         {
5653 //printf("TrackCanvas::start_selection %f\n", position);
5654                 mwindow->edl->local_session->set_selectionstart(position);
5655                 mwindow->edl->local_session->set_selectionend(position);
5656                 selection_midpoint = position;
5657 // Que the CWindow
5658                 rerender = 1;
5659         }
5660
5661         return rerender;
5662 }
5663
5664 void TrackCanvas::end_edithandle_selection()
5665 {
5666         mwindow->modify_edithandles();
5667         mwindow->session->drag_edit = 0;
5668 }
5669
5670 void TrackCanvas::end_pluginhandle_selection()
5671 {
5672         mwindow->modify_pluginhandles();
5673         mwindow->session->drag_plugin = 0;
5674 }
5675
5676 void TrackCanvas::end_transnhandle_selection()
5677 {
5678         mwindow->modify_transnhandles();
5679         mwindow->session->drag_transition = 0;
5680 }
5681
5682
5683 double TrackCanvas::time_visible()
5684 {
5685         return (double)get_w() *
5686                 mwindow->edl->local_session->zoom_sample /
5687                 mwindow->edl->session->sample_rate;
5688 }
5689
5690
5691 void TrackCanvas::show_message(Auto *current, int box_color, const char *fmt, ...)
5692 {
5693         char string[BCTEXTLEN];
5694         char *cp = string, *ep = cp + sizeof(string)-1;
5695         if( box_color >= 0 ) {
5696                 cp += snprintf(string, ep-cp, "%-8s ",
5697                         FloatAuto::curve_name(((FloatAuto*)current)->curve_mode));
5698         }
5699         char string2[BCTEXTLEN];
5700         Units::totext(string2,
5701                 current->autos->track->from_units(current->position),
5702                 mwindow->edl->session->time_format,
5703                 mwindow->edl->session->sample_rate,
5704                 mwindow->edl->session->frame_rate,
5705                 mwindow->edl->session->frames_per_foot);
5706         cp += snprintf(cp, ep-cp, "%s", string2);
5707         va_list ap;
5708         va_start(ap, fmt);
5709         vsnprintf(cp, ep-cp, fmt, ap);
5710         va_end(ap);
5711         gui->show_message(string, -1, box_color);
5712 }
5713
5714 // Patchbay* TrackCanvas::get_patchbay()
5715 // {
5716 //      if(pane->patchbay) return pane->patchbay;
5717 //      if(gui->total_panes() == 2 &&
5718 //              gui->pane[TOP_LEFT_PANE] &&
5719 //              gui->pane[TOP_RIGHT_PANE])
5720 //              return gui->pane[TOP_LEFT_PANE]->patchbay;
5721 //      if(gui->total_panes() == 4)
5722 //      {
5723 //              if(pane->number == TOP_RIGHT_PANE)
5724 //                      return gui->pane[TOP_LEFT_PANE]->patchbay;
5725 //              else
5726 //                      return gui->pane[BOTTOM_LEFT_PANE]->patchbay;
5727 //      }
5728 //
5729 //      return 0;
5730 // }
5731
5732