f37b1cd63c4d307873a64fdda3c71b2cd5cfc3c5
[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 ) return;
1971         int64_t pixel1 = Units::round(mwindow->session->drag_position *
1972                 mwindow->edl->session->sample_rate /
1973                 mwindow->edl->local_session->zoom_sample -
1974                 mwindow->edl->local_session->view_start[pane->number]);
1975         set_color(!snapped ? GREEN : (snapped=0, YELLOW));
1976         set_inverse();
1977         draw_line(pixel1, 0, pixel1, get_h());
1978         set_opaque();
1979
1980         if( mwindow->session->current_operation != DRAG_EDITHANDLE2 ) return;
1981         if( !mwindow->session->drag_edit ) return;
1982         int group_id = mwindow->session->drag_edit->group_id;
1983         if( !group_id ) return;
1984         int64_t dx, dy, dw, dh;
1985         edit_dimensions(mwindow->session->drag_edit, dx, dy, dw, dh);
1986         int drag_handle = mwindow->session->drag_handle;
1987         int64_t pixel0 = !drag_handle ? dx : dx + dw;
1988         int delta = pixel1 - pixel0;
1989         set_color(LTPURPLE);
1990         set_line_width(3);
1991
1992         for( Track *track=mwindow->edl->tracks->first; track; track=track->next ) {
1993                 if( track->is_hidden() ) continue;
1994                 Edit *left = 0, *right = 0;
1995                 double start = DBL_MAX, end = DBL_MIN;
1996                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1997                         if( edit->group_id != group_id ) continue;
1998                         double edit_start = edit->track->from_units(edit->startproject);
1999                         if( edit_start < start ) { start = edit_start;  left = edit; }
2000                         double edit_end = edit->track->from_units(edit->startproject+edit->length);
2001                         if( edit_end > end ) { end = edit_end;  right = edit; }
2002                 }
2003                 Edit *edit = !drag_handle ? left : right;
2004                 if( !edit ) continue;
2005                 Indexable *idxbl = edit->asset;
2006                 if( !idxbl ) idxbl = edit->nested_edl;
2007                 int can_drag = idxbl ? 1 : 0;
2008                 if( drag_handle ) {
2009                         int64_t source_len = !idxbl ? -1 :
2010                                 edit->track->data_type == TRACK_AUDIO ?
2011                                         idxbl->get_audio_samples() :
2012                                 edit->track->data_type == TRACK_VIDEO ?
2013                                         idxbl->get_video_frames() : -1;
2014                         int64_t speed_start = edit->startproject;
2015                         int64_t speed_end = speed_start + edit->length;
2016                         int64_t speed_length = track->speed_length(speed_start, speed_end);
2017                         if( edit->startsource + speed_length >= source_len )
2018                                 can_drag = 0;
2019                 }
2020                 else if( !edit->startsource )
2021                         can_drag = 0;
2022                 int64_t x, y, w, h;
2023                 edit_dimensions(edit, x, y, w, h);
2024                 if( y+h < 0 || y >= get_h() ) continue;
2025                 int edge_x = !drag_handle ? x : x + w;
2026                 int edge_y = y + h/2, xs10 = xS(10), ys10 = yS(10);
2027                 if( edge_x >= 0 && edge_x < get_w() ) {
2028                         if( !can_drag ) {
2029                                 draw_line(edge_x-xs10,edge_y-ys10, edge_x+xs10,edge_y+ys10);
2030                                 draw_line(edge_x-xs10,edge_y+ys10, edge_x+xs10,edge_y-ys10);
2031                         }
2032                         else if( !drag_handle ) {
2033                                 draw_line(edge_x+xs10,edge_y-ys10, edge_x,edge_y);
2034                                 draw_line(edge_x+xs10,edge_y+ys10, edge_x,edge_y);
2035                         }
2036                         else {
2037                                 draw_line(edge_x,edge_y, edge_x-xs10,edge_y-ys10);
2038                                 draw_line(edge_x,edge_y, edge_x-xs10,edge_y+ys10);
2039                         }
2040                 }
2041                 edge_x += delta;
2042                 if( edge_x >= 0 && edge_x < get_w() ) {
2043                         draw_line(edge_x, y, edge_x, y+h);
2044                 }
2045         }
2046         set_line_width(1);
2047 }
2048
2049
2050 void TrackCanvas::draw_transitions()
2051 {
2052         int64_t x, y, w, h;
2053
2054         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
2055                 if( track->is_hidden() ) continue;
2056                 if( !track->show_transitions() ) continue;
2057
2058                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
2059                         if(!edit->transition) continue;
2060                         edit_dimensions(edit, x, y, w, h);
2061                         int strip_x = x, edit_y = y;
2062                         get_transition_coords(edit, x, y, w, h);
2063                         int strip_y = y - mwindow->theme->get_image("plugin_bg_data")->get_h();
2064                         if( track->show_assets() && track->show_titles() )
2065                                 edit_y += mwindow->theme->get_image("title_bg_data")->get_h();
2066                         if( strip_y < edit_y ) strip_y = edit_y;
2067
2068                         int strip_w = Units::round(edit->track->from_units(edit->transition->length) *
2069                                 mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample);
2070                         if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
2071                             MWindowGUI::visible(y, y + h, 0, get_h()) ) {
2072                                 PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
2073                                                 track->data_type);
2074                                 if( !server ) continue;
2075                                 VFrame *picon = server->get_picon();
2076                                 if( !picon ) continue;
2077                                 int picon_w = picon->get_w(), picon_h = picon->get_h();
2078                                 int track_h = edit->track->vertical_span(mwindow->theme);
2079                                 if( picon_h > track_h ) picon_h = track_h;
2080                                 draw_vframe(picon, x, y, w, h, 0, 0, picon_w, picon_h);
2081                         }
2082                         if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
2083                                 MWindowGUI::visible(strip_y, strip_y + h, 0, get_h())) {
2084                                 int x = strip_x, w = strip_w;
2085                                 if( x < 0 ) {  w -= -x;  x = 0; }
2086                                 if( w + x > get_w() ) w -= (w + x) - get_w();
2087
2088                                 draw_3segmenth( x, strip_y, w, strip_x, strip_w,
2089                                         mwindow->theme->get_image("plugin_bg_data"), 0);
2090                         }
2091                 }
2092         }
2093 }
2094
2095 void TrackCanvas::draw_loop_points()
2096 {
2097 //printf("TrackCanvas::draw_loop_points 1\n");
2098         if(mwindow->edl->local_session->loop_playback)
2099         {
2100 //printf("TrackCanvas::draw_loop_points 2\n");
2101                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
2102                         mwindow->edl->session->sample_rate /
2103                         mwindow->edl->local_session->zoom_sample -
2104                         mwindow->edl->local_session->view_start[pane->number]);
2105 //printf("TrackCanvas::draw_loop_points 3\n");
2106
2107                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
2108                 {
2109                         set_color(GREEN);
2110                         draw_line(x, 0, x, get_h());
2111                 }
2112 //printf("TrackCanvas::draw_loop_points 4\n");
2113
2114                 x = Units::round(mwindow->edl->local_session->loop_end *
2115                         mwindow->edl->session->sample_rate /
2116                         mwindow->edl->local_session->zoom_sample -
2117                         mwindow->edl->local_session->view_start[pane->number]);
2118 //printf("TrackCanvas::draw_loop_points 5\n");
2119
2120                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
2121                 {
2122                         set_color(GREEN);
2123                         draw_line(x, 0, x, get_h());
2124                 }
2125 //printf("TrackCanvas::draw_loop_points 6\n");
2126         }
2127 //printf("TrackCanvas::draw_loop_points 7\n");
2128 }
2129
2130 void TrackCanvas::draw_brender_range()
2131 {
2132         if( !mwindow->preferences->use_brender || !mwindow->brender_active ) return;
2133         if( mwindow->edl->session->brender_start >= mwindow->edl->session->brender_end ) return;
2134         if( mwindow->edl->session->brender_end > 0 )
2135         {
2136                 int64_t x1 = Units::round(mwindow->edl->session->brender_start *
2137                         mwindow->edl->session->sample_rate /
2138                         mwindow->edl->local_session->zoom_sample -
2139                         mwindow->edl->local_session->view_start[pane->number]);
2140                 if(MWindowGUI::visible(x1, x1 + 1, 0, get_w()))
2141                 {
2142                         set_color(RED);
2143                         draw_line(x1, 0, x1, get_h());
2144                 }
2145                 int64_t x2 = Units::round(mwindow->edl->session->brender_end *
2146                         mwindow->edl->session->sample_rate /
2147                         mwindow->edl->local_session->zoom_sample -
2148                         mwindow->edl->local_session->view_start[pane->number]);
2149
2150                 if(MWindowGUI::visible(x2, x2 + 1, 0, get_w()))
2151                 {
2152                         set_color(RED);
2153                         draw_line(x2, 0, x2, get_h());
2154                 }
2155         }
2156 }
2157
2158 // The operations which correspond to each automation type
2159 int TrackCanvas::auto_operations[AUTOMATION_TOTAL] =
2160 {
2161         DRAG_MUTE,
2162         DRAG_CAMERA_X,
2163         DRAG_CAMERA_Y,
2164         DRAG_CAMERA_Z,
2165         DRAG_PROJECTOR_X,
2166         DRAG_PROJECTOR_Y,
2167         DRAG_PROJECTOR_Z,
2168         DRAG_FADE,
2169         DRAG_PAN,
2170         DRAG_MODE,
2171         DRAG_MASK,
2172         DRAG_SPEED
2173 };
2174
2175 // The buttonpress operations, so nothing changes unless the mouse moves
2176 // a certain amount.  This allows the keyframe to be used to position the
2177 // insertion point without moving itself.
2178 static int pre_auto_operations[AUTOMATION_TOTAL] =
2179 {
2180         DRAG_MUTE,
2181         DRAG_CAMERA_X,
2182         DRAG_CAMERA_Y,
2183         DRAG_CAMERA_Z,
2184         DRAG_PROJECTOR_X,
2185         DRAG_PROJECTOR_Y,
2186         DRAG_PROJECTOR_Z,
2187         DRAG_FADE,
2188         DRAG_PAN_PRE,
2189         DRAG_MODE_PRE,
2190         DRAG_MASK_PRE,
2191         DRAG_SPEED
2192 };
2193
2194
2195 int TrackCanvas::do_keyframes(int cursor_x,
2196         int cursor_y,
2197         int draw,
2198         int buttonpress,
2199         int &new_cursor,
2200         int &update_cursor,
2201         int &rerender)
2202 {
2203 // Note: button 3 (right mouse button) is not eaten to allow
2204 // track context menu to appear
2205         int result = 0;
2206         EDLSession *session = mwindow->edl->session;
2207         int gang = session->gang_tracks != GANG_NONE || get_double_click() ? 1 : 0;
2208
2209         static BC_Pixmap *auto_pixmaps[AUTOMATION_TOTAL] =
2210         {
2211                 0, 0, 0, 0, 0, 0, 0, 0,
2212                 pankeyframe_pixmap,
2213                 modekeyframe_pixmap,
2214                 maskkeyframe_pixmap,
2215                 0,
2216         };
2217
2218
2219
2220         for(Track *track = mwindow->edl->tracks->first;
2221                 track && !result;
2222                 track = track->next) {
2223                 if( track->is_hidden() ) continue;
2224                 Auto *auto_keyframe = 0;
2225                 Automation *automation = track->automation;
2226
2227
2228 // Handle keyframes in reverse drawing order if a button press
2229                 int start = 0;
2230                 int end = AUTOMATION_TOTAL;
2231                 int step = 1;
2232                 if(buttonpress)
2233                 {
2234                         start = AUTOMATION_TOTAL - 1;
2235                         end = -1;
2236                         step = -1;
2237                 }
2238                 for(int i = start; i != end && !result; i += step)
2239                 {
2240 // Event not trapped and automation visible
2241                         Autos *autos = automation->autos[i];
2242                         if(!result && session->auto_conf->autos[i] && autos) {
2243                                 switch(i) {
2244                                 case AUTOMATION_MODE:
2245                                 case AUTOMATION_PAN:
2246                                 case AUTOMATION_MASK:
2247                                         result = do_autos(track, automation->autos[i],
2248                                                 cursor_x, cursor_y, draw,
2249                                                 buttonpress, auto_pixmaps[i],
2250                                                 auto_keyframe, rerender);
2251                                                 break;
2252
2253                                 default: {
2254                                         switch(autos->get_type()) {
2255                                         case Autos::AUTOMATION_TYPE_FLOAT: {
2256                                                 Automation automation(0, track);
2257                                                 int grouptype = automation.autogrouptype(i, track);
2258                                                 if( buttonpress && i == AUTOMATION_SPEED ) {
2259                                                         mwindow->speed_before();
2260                                                 }
2261
2262                                                 if(draw) // Do dropshadow
2263                                                         result = do_float_autos(track, autos,
2264                                                                 cursor_x, cursor_y, draw,
2265                                                                 buttonpress, 1, 1, MDGREY,
2266                                                                 auto_keyframe, grouptype);
2267                                                 result = do_float_autos(track, autos,
2268                                                         cursor_x, cursor_y, draw, buttonpress,
2269                                                         0, 0, GWindowGUI::auto_colors[i],
2270                                                         auto_keyframe, grouptype);
2271
2272                                                 if( !result && buttonpress && i == AUTOMATION_SPEED )
2273                                                         mwindow->speed_after(-1, 0);
2274                                                 if( result && buttonpress ) {
2275                                                         int color = GWindowGUI::auto_colors[i];
2276                                                         mwindow->gui->zoombar->update_autozoom(grouptype, color);
2277                                                 }
2278                                                 break; }
2279
2280                                         case Autos::AUTOMATION_TYPE_INT: {
2281                                                 if(draw) // Do dropshadow
2282                                                         result = do_int_autos(track, autos,
2283                                                                 cursor_x, cursor_y, draw,
2284                                                                 buttonpress, 1, 1, MDGREY,
2285                                                                 auto_keyframe);
2286                                                 result = do_int_autos(track, autos,
2287                                                         cursor_x, cursor_y, draw, buttonpress,
2288                                                         0, 0, GWindowGUI::auto_colors[i],
2289                                                         auto_keyframe);
2290                                                 break; }
2291                                         }
2292                                         break; }
2293                                 }
2294
2295                                 if(result)
2296                                 {
2297                                         if(mwindow->session->current_operation == auto_operations[i])
2298                                                 rerender = 1;
2299
2300 // printf("TrackCanvas::do_keyframes %d %d %d\n",
2301 // __LINE__,
2302 // mwindow->session->current_operation,
2303 // auto_operations[i]);
2304                                         if(buttonpress)
2305                                         {
2306                                                 if (buttonpress == 2 && auto_keyframe )
2307                                                 {
2308                                                         double position = track->from_units(auto_keyframe->position);
2309                                                         mwindow->edl->local_session->set_selectionstart(position);
2310                                                         mwindow->edl->local_session->set_selectionend(position);
2311                                                 }
2312                                                 if (buttonpress != 3)
2313                                                 {
2314                                                         if(i == AUTOMATION_FADE || i == AUTOMATION_SPEED)
2315                                                                 fill_ganged_autos(gang, 0, track,
2316                                                                         (FloatAuto*)mwindow->session->drag_auto);
2317                                                         mwindow->session->current_operation = pre_auto_operations[i];
2318                                                         update_drag_caption();
2319                                                         rerender = 1;
2320                                                 }
2321                                                 else if( auto_keyframe )
2322                                                 {
2323                                                         gui->keyframe_menu->update(automation,
2324                                                                 autos,
2325                                                                 auto_keyframe);
2326                                                         gui->keyframe_menu->activate_menu();
2327                                                         rerender = 1; // the position changes
2328                                                 }
2329                                                 else if( autos )
2330                                                 {
2331                                                         gui->keyframe_hide->update(autos);
2332                                                         gui->keyframe_hide->activate_menu();
2333                                                         rerender = 1; // the position changes
2334                                                 }
2335                                                 if(buttonpress == 1 && ctrl_down() &&
2336                                                    AUTOMATION_TYPE_FLOAT == autos->get_type())
2337                                                         rerender = 1; // special case: curve mode changed
2338                                         }
2339                                 }
2340                         }
2341                 }
2342
2343                 if(!result && session->auto_conf->plugins) {
2344                         Plugin *plugin;
2345                         KeyFrame *keyframe;
2346                         result = do_plugin_autos(track, cursor_x, cursor_y,
2347                                 draw, buttonpress, plugin, keyframe);
2348                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY) {
2349                                 rerender = 1;
2350                         }
2351                         if(result && (buttonpress == 1)) {
2352                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2353                                 update_drag_caption();
2354                                 rerender = 1;
2355                         }
2356                         else if (result && (buttonpress == 3)) {
2357                                 gui->keyframe_menu->update(plugin, keyframe);
2358                                 gui->keyframe_menu->activate_menu();
2359                                 rerender = 1; // the position changes
2360                         }
2361                 }
2362         }
2363
2364 // Final pass to trap event
2365         for(int i = 0; i < AUTOMATION_TOTAL; i++) {
2366                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2367                         mwindow->session->current_operation == auto_operations[i]) {
2368                         result = 1;
2369                         break;
2370                 }
2371         }
2372
2373         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2374                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE) {
2375                 result = 1;
2376         }
2377
2378         update_cursor = 1;
2379         if(result) {
2380                 new_cursor = UPRIGHT_ARROW_CURSOR;
2381         }
2382
2383         return result;
2384 }
2385
2386 void TrackCanvas::draw_keyframe_reticle()
2387 {
2388         int keyframe_hairline = mwindow->preferences->keyframe_reticle;
2389         if( keyframe_hairline == HAIRLINE_NEVER ) return;
2390
2391         int current_op = mwindow->session->current_operation, dragging = 0;
2392         for( int i=0; !dragging && i<AUTOMATION_TOTAL; ++i )
2393                 if( current_op == auto_operations[i] ) dragging = 1;
2394         if( dragging && !mwindow->session->drag_auto ) dragging = 0;
2395
2396         int autoidx = dragging && keyframe_hairline != HAIRLINE_ALWAYS ?
2397                 mwindow->session->drag_auto->autos->autoidx : -1;
2398
2399         if( get_buttonpress() == LEFT_BUTTON && dragging &&
2400             keyframe_hairline == HAIRLINE_DRAGGING ) {
2401                 draw_hairline(mwindow->session->drag_auto, RED, 1);
2402                 return;
2403         }
2404
2405         if( keyframe_hairline == HAIRLINE_ALWAYS || ( get_buttonpress() == MIDDLE_BUTTON &&
2406             keyframe_hairline == HAIRLINE_DRAGGING && dragging ) ) {
2407                 int show = dragging || keyframe_hairline == HAIRLINE_ALWAYS ? 1 : 0;
2408                 for( Track *track = mwindow->edl->tracks->first; track; track=track->next ) {
2409                         if( track->is_hidden() ) continue;
2410                         Automation *automation = track->automation;
2411                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2412                                 if( !mwindow->edl->session->auto_conf->autos[i] ) continue;
2413                                 // automation visible
2414                                 Autos *autos = automation->autos[i];
2415                                 if( !autos ) continue;
2416                                 if( autoidx >= 0 && autos->autoidx != autoidx ) continue;
2417                                 for( Auto *auto_keyframe=autos->first; auto_keyframe;
2418                                      auto_keyframe = auto_keyframe->next ) {
2419                                         draw_hairline(auto_keyframe, BLUE, show);
2420                                 }
2421                         }
2422                 }
2423
2424                 if( dragging )
2425                         draw_hairline(mwindow->session->drag_auto, RED, 1);
2426         }
2427 }
2428
2429 void TrackCanvas::draw_auto(Auto *current, int x, int y,
2430                 int center_pixel, int data_h)
2431 {
2432         int x1, y1, x2, y2;
2433
2434         x1 = x - HANDLE_W / 2;
2435         x2 = x + HANDLE_W / 2;
2436         y1 = center_pixel + y - HANDLE_W / 2;
2437         y2 = center_pixel + y + HANDLE_W / 2;
2438
2439         if( y1 < center_pixel - data_h/2 ) y1 = center_pixel - data_h/2;
2440         if( y2 > center_pixel + data_h/2 ) y2 = center_pixel + data_h/2;
2441
2442         draw_box(x1, y1, x2 - x1, y2 - y1);
2443 }
2444
2445
2446 // This draws lines for bezier in & out controls
2447 void TrackCanvas::draw_cropped_line(int x1,
2448         int y1,
2449         int x2,
2450         int y2,
2451         int min_y,
2452         int max_y)
2453 {
2454
2455
2456 // Don't care about x since it is clipped by the window.
2457 // Put y coords in ascending order
2458         if(y2 < y1) {
2459                 y2 ^= y1; y1 ^= y2; y2 ^= y1;
2460                 x2 ^= x1; x1 ^= x2; x2 ^= x1;
2461         }
2462
2463
2464
2465         double slope = (double)(x2 - x1) / (y2 - y1);
2466 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2467         if(y1 < min_y) {
2468                 x1 = (int)(x1 + (min_y - y1) * slope);
2469                 y1 = min_y;
2470         }
2471         else if(y1 >= max_y) {
2472                 x1 = (int)(x1 + (max_y - 1 - y1) * slope);
2473                 y1 = max_y - 1;
2474         }
2475
2476         if(y2 >= max_y) {
2477                 x2 = (int)(x2 + (max_y - 1 - y2) * slope);
2478                 y2 = max_y - 1;
2479         }
2480         else if(y2 < min_y) {
2481                 x2 = (int)(x2 + (min_y - y2) * slope);
2482                 y1 = min_y;
2483         }
2484
2485
2486 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2487         if( y1 >= min_y && y1 < max_y &&
2488             y2 >= min_y && y2 < max_y )
2489                 draw_line(x1, y1, x2, y2);
2490 }
2491
2492
2493 void TrackCanvas::draw_floatauto(FloatAuto *current,
2494                 int x_offset, int center_pixel, int data_h, int color,
2495                 double unit_start, double zoom_units, double yscale,
2496                 int autogrouptype)
2497 {
2498         double xx, yy;
2499         calculate_auto_position(1, &xx, &yy, 0, 0, 0, 0,
2500                 current, unit_start, zoom_units, yscale, autogrouptype);
2501         int x = (int)xx + x_offset;
2502         int x1 = x - HANDLE_W / 2; // Center
2503         int x2 = x + HANDLE_W / 2;
2504         int y = (int)yy + center_pixel;
2505         int y1 = y - HANDLE_H / 2;
2506         int y2 = y + HANDLE_H / 2;
2507         int ymin = center_pixel - data_h / 2;
2508         int ymax = center_pixel + data_h / 2;
2509         CLAMP(y1, ymin, ymax);
2510         CLAMP(y2, ymin, ymax);
2511
2512         if( y2-1 > y1 ) {
2513                 if( current->curve_mode == FloatAuto::LINEAR ) {
2514                         draw_box(x1, y1, x2 - x1, y2 - y1);
2515                 }
2516                 else {
2517                         ArrayList<int> polygon_x;
2518                         ArrayList<int> polygon_y;
2519                         polygon_x.append((x1 + x2) / 2 + 1);
2520                         polygon_y.append(y1 + 1);
2521                         polygon_x.append(x2 + 1);
2522                         polygon_y.append((y1 + y2) / 2 + 1);
2523                         polygon_x.append((x1 + x2) / 2 + 1);
2524                         polygon_y.append(y2 + 1);
2525                         polygon_x.append(x1 + 1);
2526                         polygon_y.append((y1 + y2) / 2 + 1);
2527                         fill_polygon(&polygon_x, &polygon_y);
2528                 }
2529         }
2530
2531 // show bezier control points (only) if this
2532 // floatauto doesn't adjust it's tangents automatically
2533         if(current->curve_mode != FloatAuto::FREE &&
2534            current->curve_mode != FloatAuto::TFREE &&
2535            current->curve_mode != FloatAuto::BUMP)
2536                 return;
2537         double in_xx, in_yy, out_xx, out_yy;
2538         calculate_auto_position(1, &xx, &yy, &in_xx, &in_yy, 0, 0,
2539                 current, unit_start, zoom_units, yscale, autogrouptype);
2540         int ix = xx, iy = yy, in_x = in_xx, in_y = in_yy;
2541         if( in_x != ix )
2542                 draw_floatauto_ctrlpoint(ix, iy, in_x, in_y, center_pixel, data_h, color);
2543         calculate_auto_position(0, &xx, &yy, 0, 0, &out_xx, &out_yy,
2544                 current, unit_start, zoom_units, yscale, autogrouptype);
2545         int ox = xx, oy = yy, out_x = out_xx, out_y = out_yy;
2546         if( out_x != ox )
2547                 draw_floatauto_ctrlpoint(ox, oy, out_x, out_y, center_pixel, data_h, color);
2548         if( current->curve_mode == FloatAuto::BUMP && iy != oy )
2549                 draw_bline(ix, iy+center_pixel, ox, oy+center_pixel);
2550 }
2551
2552 inline int quantize(float f) { return (int)floor(f + 0.5); }
2553
2554 inline void TrackCanvas::draw_floatauto_ctrlpoint(
2555                 int x, int y, int cp_x, int cp_y, int center_pixel,
2556                 int data_h, int color)
2557 // draw the tangent and a handle for given b├ęzier ctrl point
2558 {
2559         bool handle_visible = (abs(cp_y) <= data_h/2);
2560
2561         float slope = (float)(cp_y - y)/(cp_x - x);
2562         CLAMP(cp_y, -data_h/2, data_h/2);
2563         if(slope != 0)
2564                 cp_x = x + quantize((cp_y - y) / slope);
2565
2566         y    += center_pixel;
2567         cp_y += center_pixel;
2568
2569         // drawing the tangent as a dashed line...
2570         int const dash = HANDLE_W;
2571         int const gap  = HANDLE_W / 2;
2572         float sx = 3 * (cp_x - x) / 4.;
2573         float ex = 0;
2574
2575         // q is the x displacement for a unit line of slope
2576         float q = (sx > 0 ? 1 : -1) / sqrt(1 + slope * slope);
2577
2578         float dist = 1/q * sx;
2579         if( dist > dash )
2580                 ex = sx - q * dash;
2581
2582         set_color(color);
2583         do {
2584                 float sy = slope * sx, ey = slope * ex;
2585                 draw_line(quantize(sx + x), quantize(sy + y), quantize(ex + x), quantize(ey + y));
2586                 sx = ex - q * gap;
2587                 ex = sx - q * dash;
2588         } while(q*ex > 0);
2589
2590         if(handle_visible)
2591         {
2592                 int r = HANDLE_W / 2;
2593                 int cp_x1 = cp_x - r;
2594                 int cp_y1 = cp_y - r;
2595                 set_color(BLACK);
2596                 draw_disc(cp_x1, cp_y1, 2 * r, 2 * r);
2597                 set_color(color);
2598                 draw_circle(cp_x1, cp_y1, 2 * r, 2 * r);
2599         }
2600 }
2601
2602
2603 int TrackCanvas::test_auto(Auto *current,
2604         int x, int y, int center_pixel, int data_h,
2605         int cursor_x, int cursor_y, int buttonpress)
2606 {
2607         int x1, y1, x2, y2;
2608         int result = 0;
2609
2610         x1 = x - HANDLE_W / 2;
2611         x2 = x + HANDLE_W / 2;
2612         y1 = center_pixel + y - HANDLE_H / 2;
2613         y2 = center_pixel + y + HANDLE_H / 2;
2614         int ymin = center_pixel - data_h/2;
2615         int ymax = center_pixel + data_h/2;
2616         CLAMP(y1, ymin, ymax);
2617         CLAMP(y2, ymin, ymax);
2618
2619         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2620         {
2621                 if(buttonpress && buttonpress != 3)
2622                 {
2623                         mwindow->session->drag_auto = current;
2624                         mwindow->session->drag_start_percentage = (float)((IntAuto*)current)->value;
2625                         // Toggle Autos don't respond to vertical zoom, they always show up
2626                         // with "on" == 100% == line on top
2627                         mwindow->session->drag_start_position = current->position;
2628                         mwindow->session->drag_origin_x = cursor_x;
2629                         mwindow->session->drag_origin_y = cursor_y;
2630                 }
2631                 result = 1;
2632         }
2633
2634         if(buttonpress && buttonpress != 3 && result)
2635         {
2636 //printf("TrackCanvas::test_auto %d\n", __LINE__);
2637                 mwindow->undo->update_undo_before();
2638         }
2639
2640         return result;
2641 }
2642
2643 // some Helpers for test_floatauto(..)
2644 // and for dragging the tangents/ctrl points
2645 inline float test_curve_line( int x0, int y0, int ctrl_x, int ctrl_y,
2646         float cursor_x, float cursor_y)
2647 {
2648 // Control point switched off?
2649         if( x0 == ctrl_x ) return 0.0;
2650         double x1 = ctrl_x-x0, y1 = ctrl_y-y0;
2651         double x = cursor_x-x0, y = cursor_y-y0;
2652 // wrong side of ctrl handle
2653         if( x*x1 < 0 ) return 0.0;
2654 // outside handle radius
2655         if( (x*x + y*y) > (x1*x1 + y1*y1) ) return 0;
2656         double xx1 = x1*x1, yy1 = y1*y1;
2657         double xx = x*x, yy = y*y;
2658 // distance squared from cursor to cursor projected to line from ctrl to handle
2659 //   along a line perpendicular to line from ctrl to handle (closest approach)
2660 // (x**2*y1**2 - 2*x*x1*y*y1 + x1**2*y**2)/(x1**2 + y1**2)
2661         double dist2 = (xx*yy1 - 2*x*x1*y*y1 + xx1*yy)/(xx1 + yy1);
2662         return dist2 < (HANDLE_W*HANDLE_W)/4. ? x/x1 : 0.;
2663 }
2664
2665
2666 inline
2667 float levered_position(float position, float ref_pos)
2668 {
2669         if( 1e-6 > fabs(ref_pos) || isnan(ref_pos))
2670                 return 0.0;
2671         return ref_pos / position;
2672 }
2673
2674
2675 float TrackCanvas::value_to_percentage(float auto_value, int autogrouptype)
2676 // transforms automation value into current display coords,
2677 // dependant on current automation display range for the given kind of automation
2678 {
2679         if(!mwindow || !mwindow->edl) return 0;
2680         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2681         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2682         float automation_range = automation_max - automation_min;
2683         if( 0 >= automation_range || isnan(auto_value) || isinf(auto_value) )
2684                 return 0;
2685         return (auto_value - automation_min) / automation_range;
2686 }
2687
2688
2689
2690 int TrackCanvas::test_floatauto(FloatAuto *current, int buttonpress,
2691                 int center_pixel, int data_h, int cursor_x, int cursor_y,
2692                 double unit_start, double zoom_units, double yscale,
2693                 int autogrouptype)
2694 {
2695         int result = 0;
2696         double xx, yy, in_xx, in_yy, out_xx, out_yy;
2697         calculate_auto_position(1, &xx, &yy, &in_xx, &in_yy, 0, 0,
2698                 current, unit_start, zoom_units, yscale, autogrouptype);
2699         int ix = xx, iy = yy, in_x = in_xx, in_y = in_yy;
2700
2701         int x1 = ix - HANDLE_W / 2;
2702         int x2 = ix + HANDLE_W / 2;
2703         int y1 = center_pixel + iy - HANDLE_W / 2;
2704         int y2 = center_pixel + iy + HANDLE_W / 2;
2705         int ymin = center_pixel - data_h/2;
2706         int ymax = center_pixel + data_h/2;
2707         CLAMP(y1, ymin, ymax);
2708         CLAMP(y2, ymin, ymax);
2709
2710         int in_x1 = in_x - HANDLE_W / 2;
2711         int in_x2 = in_x + HANDLE_W / 2;
2712         int in_y1 = center_pixel + in_y - HANDLE_W / 2;
2713         int in_y2 = center_pixel + in_y + HANDLE_W / 2;
2714         CLAMP(in_y1, ymin, ymax);
2715         CLAMP(in_y2, ymin, ymax);
2716
2717         calculate_auto_position(0, &xx, &yy, 0, 0, &out_xx, &out_yy,
2718                 current, unit_start, zoom_units, yscale, autogrouptype);
2719         int ox = xx, oy = yy, out_x = out_xx, out_y = out_yy;
2720         int out_x1 = out_x - HANDLE_W / 2;
2721         int out_x2 = out_x + HANDLE_W / 2;
2722         int out_y1 = center_pixel + out_y - HANDLE_W / 2;
2723         int out_y2 = center_pixel + out_y + HANDLE_W / 2;
2724         CLAMP(out_y1, ymin, ymax);
2725         CLAMP(out_y2, ymin, ymax);
2726
2727
2728 //printf("TrackCanvas::test_floatauto %d %d %d %d %d %d\n", cursor_x, cursor_y, x1, x2, y1, y2);
2729 // buttonpress could be the start of a drag operation
2730 #define INIT_DRAG(POS,VAL) \
2731                 mwindow->session->drag_auto = current;      \
2732                 mwindow->session->drag_origin_x = cursor_x;  \
2733                 mwindow->session->drag_origin_y = cursor_y;   \
2734                 mwindow->session->drag_start_position = (POS); \
2735                 mwindow->session->drag_start_percentage = (VAL);
2736
2737 #define WITHIN(X1,X2,Y1,Y2) (cursor_x >=(X1) && cursor_x <(X2) && cursor_y >=(Y1) && cursor_y <(Y2) )
2738
2739
2740 // without modifier we are manipulating the automation node
2741 // with ALT it's about dragging only the value of the node
2742 // with SHIFT the value snaps to the value of neighbouring nodes
2743 // CTRL indicates we are rather manipulating the tangent(s) of the node
2744
2745         if(!ctrl_down())
2746         {
2747                 if( WITHIN(x1,x2,y1,y2))
2748                 {       // cursor hits node
2749                         result = 1;
2750
2751                         if(buttonpress && (buttonpress != 3))
2752                         {
2753                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2754                                 mwindow->session->drag_handle = 0;
2755                 }       }
2756         }
2757         else // ctrl_down()
2758         {
2759                 if( WITHIN(x1,x2,y1,y2))
2760                 {
2761                         result = 1;
2762                         if(buttonpress && (buttonpress != 3))
2763                         {
2764                                 // could be ctrl-click or ctrl-drag
2765                                 // click would cycle through tangent modes
2766                                 ((FloatAuto*)current)->toggle_curve_mode();
2767
2768                                 // drag will start dragging the tangent, if applicable
2769                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2770                                 mwindow->session->drag_handle = 0;
2771                         }
2772                 }
2773
2774                 float lever = 0.0; // we use the tangent as a draggable lever. 1.0 is at the ctrl point
2775
2776 // Test in control
2777                 if( in_x != ix && current->position > 0 &&
2778                         (FloatAuto::FREE == current->curve_mode ||
2779                          FloatAuto::TFREE == current->curve_mode ||
2780                          FloatAuto::BUMP == current->curve_mode))
2781 // act on in control handle only if
2782 // tangent is significant and is editable (not automatically choosen)
2783                 {
2784                         lever = test_curve_line(ix, iy, in_x, in_y, cursor_x, cursor_y-center_pixel);
2785  // either cursor in ctrl-point handle or cursor on tangent line
2786                         if( WITHIN(in_x1,in_x2,in_y1,in_y2) || lever > 0.0 ) {
2787                                 result = 1;
2788                                 if(buttonpress && (buttonpress != 3)) {
2789                                         if(lever == 0.0) lever=1.0; // we entered by dragging the handle...
2790 //                                      lever = 1.0;
2791                                         mwindow->session->drag_handle = 1;
2792                                         float new_invalue = current->get_value() + lever * current->get_control_in_value();
2793                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_in_position()),
2794                                                   value_to_percentage(new_invalue, autogrouptype))
2795                                 }
2796                         }
2797                 }
2798
2799 // Test out control
2800                 if(out_x != ox &&
2801                         (FloatAuto::FREE == current->curve_mode ||
2802                          FloatAuto::TFREE == current->curve_mode ||
2803                          FloatAuto::BUMP == current->curve_mode))
2804 // act on out control only if tangent is significant and is editable
2805                 {
2806                         lever = test_curve_line(ox, oy, out_x, out_y, cursor_x, cursor_y-center_pixel);
2807                         if(WITHIN(out_x1,out_x2,out_y1,out_y2) || lever > 0.0 ) {
2808                                 result = 1;
2809                                 if(buttonpress && (buttonpress != 3)) {
2810                                         if(lever == 0.0) lever=1.0;
2811 //                                      lever = 1.0;
2812                                         mwindow->session->drag_handle = 2;
2813                                         float new_outvalue = current->get_value() + lever * current->get_control_out_value();
2814                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_out_position()),
2815                                                   value_to_percentage(new_outvalue, autogrouptype))
2816                                 }
2817                         }
2818                 }
2819         } // end ctrl_down()
2820
2821 #undef WITHIN
2822 #undef INIT_DRAG
2823
2824 // if(buttonpress)
2825 // 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",
2826 // mwindow->session->drag_handle,
2827 // ctrl_down(),
2828 // cursor_x,
2829 // cursor_y,
2830 // x1, x2, y1, y2);
2831         if(buttonpress && (buttonpress != 3) && result)
2832         {
2833                 mwindow->undo->update_undo_before();
2834                 double position = current->autos->track->from_units(current->position);
2835                 mwindow->edl->local_session->set_selectionstart(position);
2836                 mwindow->edl->local_session->set_selectionend(position);
2837         }
2838
2839         return result;
2840 }
2841
2842 static int is_linear(FloatAuto *prev, FloatAuto *next)
2843 {
2844         if( !prev || !next ) return 0;
2845         if( prev->curve_mode == FloatAuto::LINEAR ) return 1;
2846         int64_t ipos = prev->get_control_in_position();
2847         int64_t opos = prev->get_control_out_position();
2848         if( !ipos && !opos ) return 1;
2849         if( !ipos || !opos ) return 0;
2850         float ival = next->get_control_in_value();
2851         float oval = prev->get_control_out_value();
2852         float cval = prev->get_value(0), nval = next->get_value(1);
2853         if( !ival && !oval && EQUIV(cval, nval) ) return 1;
2854         float ig = ival / ipos, og = oval / opos;
2855         int64_t cpos = prev->position, npos = next->position;
2856         float g = (nval - cval) / (npos - cpos);
2857         if( !EQUIV(g, 0) && EQUIV(ig, g) && EQUIV(og, g) ) return 1;
2858         return 0;
2859 }
2860
2861 // Get the float value & y for position x on the canvas
2862 #define X_TO_FLOATLINE(x) \
2863         double position = unit_start + x * zoom_units; \
2864         int64_t position1 = (int64_t)position, position2 = position1 + 1; \
2865 /* Call by reference fails for some reason here */ \
2866         float value1 = autos->get_value(position1, PLAY_FORWARD, previous1, next1); \
2867         float value2 = autos->get_value(position2, PLAY_FORWARD, previous1, next1); \
2868         double value = value1 + (value2 - value1) * (position - position1); \
2869         AUTOMATIONCLAMPS(value, autogrouptype); \
2870         int y = center_pixel + \
2871                 (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2872
2873
2874 void TrackCanvas::draw_floatline(int center_pixel,
2875         FloatAuto *previous, FloatAuto *next, FloatAutos *autos,
2876         double unit_start, double zoom_units, double yscale,
2877         int x1, int y1, int x2, int y2,
2878         int color, int autogrouptype)
2879 {
2880         int ytop = center_pixel - yscale / 2;
2881         int ybot = center_pixel + yscale / 2 - 1;
2882         y1 += center_pixel;  y2 += center_pixel;
2883         if( (y1 < ytop && y1 >= ybot) && (y2 < ytop || y2 >= ybot) ) return;
2884 // check for line draw
2885         if( is_linear(previous, next) ) {
2886                 if( previous && previous->curve_mode == FloatAuto::BUMP ) {
2887                         double ax, ay;
2888                         calculate_auto_position(0, &ax, &ay, 0, 0, 0, 0, previous,
2889                                 unit_start, zoom_units, yscale, autogrouptype);
2890                         x1 = ax;  y1 = ay;  y1 += center_pixel;
2891                 }
2892                 draw_line(x1, y1, x2, y2);
2893                 return;
2894         }
2895
2896 // Solve bezier equation for either every pixel or a certain large number of
2897 // points.
2898
2899 // Not using slope intercept
2900         x1 = MAX(0, x1);
2901         draw_pixel(x1, y1);
2902         int prev_y = y1;
2903
2904 // Call by reference fails for some reason here
2905         FloatAuto *previous1 = previous, *next1 = next;
2906         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2907         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2908         float automation_range = automation_max - automation_min;
2909         if( autogrouptype == AUTOGROUPTYPE_SPEED && automation_range < SPEED_MIN )
2910                 automation_range = SPEED_MIN;
2911         for( int x=x1; x<x2; ++x ) {
2912 // Interpolate value between frames
2913                 X_TO_FLOATLINE(x)
2914
2915                 if( /* x > x1 && */ y >= ytop && y < ybot ) {
2916                         int x1 = x-1, y1 = prev_y, x2 = x, y2 = y;
2917                         if( abs(y2-y1) < 2 )
2918                                 draw_pixel(x2, y2);
2919                         else
2920                                 draw_bline(x1, y1, x2, y2);
2921                 }
2922                 prev_y = y;
2923         }
2924 }
2925
2926
2927 int TrackCanvas::test_floatline(int center_pixel,
2928                 FloatAutos *autos,
2929                 double unit_start,
2930                 double zoom_units,
2931                 double yscale,
2932                 int x1,
2933                 int x2,
2934                 int cursor_x,
2935                 int cursor_y,
2936                 int buttonpress,
2937                 int autogrouptype)
2938 {
2939         int result = 0;
2940
2941
2942         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2943         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2944         float automation_range = automation_max - automation_min;
2945         if( autogrouptype == AUTOGROUPTYPE_SPEED && automation_range < SPEED_MIN )
2946                 automation_range = SPEED_MIN;
2947         FloatAuto *previous1 = 0, *next1 = 0;
2948         X_TO_FLOATLINE(cursor_x);
2949
2950         if(cursor_x >= x1 &&
2951                 cursor_x < x2 &&
2952                 cursor_y >= y - HANDLE_W / 2 &&
2953                 cursor_y < y + HANDLE_W / 2 &&
2954                 !ctrl_down())
2955         {
2956                 result = 1;
2957
2958 // Menu
2959                 if(buttonpress == 3)
2960                 {
2961                 }
2962                 else
2963 // Create keyframe
2964                 if(buttonpress)
2965                 {
2966                         Auto *current;
2967                         mwindow->undo->update_undo_before();
2968                         double position = autos->track->from_units(position1);
2969                         position = mwindow->edl->align_to_frame(position, 0);
2970                         int64_t new_position = autos->track->to_units(position,0);
2971                         current = mwindow->session->drag_auto = autos->insert_auto(new_position);
2972                         ((FloatAuto*)current)->set_value(value);
2973                         mwindow->session->drag_start_percentage = value_to_percentage(value, autogrouptype);
2974                         mwindow->session->drag_start_position = current->position;
2975                         mwindow->session->drag_origin_x = cursor_x;
2976                         mwindow->session->drag_origin_y = cursor_y;
2977                         mwindow->session->drag_handle = 0;
2978                 }
2979         }
2980
2981
2982         return result;
2983 }
2984
2985 // gang=-1 for keyframepopup update, all tracks where fautos exist
2986 // gang=0 for trackcanvas drag update, all gang matching tracks, create new fautos if needed