add crop plugin, add timeline bars, render setup err chks, minor tweaks
[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                         int64_t track_x, track_y, track_w, track_h;
214                         track_dimensions(track, track_x, track_y, track_w, track_h);
215
216                         if(cursor_y >= track_y &&
217                                 cursor_y < track_y + track_h)
218                         {
219                                 *over_track = track;
220                                 for(Edit *edit = track->edits->first; edit; edit = edit->next)
221                                 {
222                                         int64_t edit_x, edit_y, edit_w, edit_h;
223                                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
224                                         if( cursor_y < edit_y || cursor_y >= edit_y + edit_h ) continue;
225                                         if( cursor_x >= edit_x && cursor_x < edit_x + edit_w ) {
226                                                 *over_edit = edit;
227                                                 break;
228                                         }
229                                         if( edit != track->edits->last ) continue;
230                                         if( mwindow->session->current_operation != DRAG_ATRANSITION &&
231                                             mwindow->session->current_operation != DRAG_VTRANSITION ) continue;
232                                         if( !edit->silence() ) {
233                                                 // add silence to allow drag transition past last edit
234                                                 //  will be deleted by Edits::optimize if not used
235                                                 double length = mwindow->edl->session->default_transition_length;
236                                                 int64_t start = edit->startproject+edit->length;
237                                                 int64_t units = track->to_units(length, 1);
238                                                 track->edits->create_silence(start, start+units);
239                                                 continue;
240                                         }
241                                         if( cursor_x >= edit_x ) { *over_edit = edit; break; }
242                                 }
243
244                                 for(int i = 0; i < track->plugin_set.total; i++)
245                                 {
246                                         PluginSet *pluginset = track->plugin_set.values[i];
247                                         for(Plugin *plugin = (Plugin*)pluginset->first;
248                                                 plugin;
249                                                 plugin = (Plugin*)plugin->next)
250                                         {
251                                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
252                                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
253
254                                                 if(cursor_y >= plugin_y &&
255                                                         cursor_y < plugin_y + plugin_h)
256                                                 {
257                                                         *over_pluginset = plugin->plugin_set;
258
259                                                         if(cursor_x >= plugin_x &&
260                                                                 cursor_x < plugin_x + plugin_w)
261                                                         {
262                                                                 *over_plugin = plugin;
263                                                                 break;
264                                                         }
265                                                 }
266                                         }
267                                 }
268                                 break;
269                         }
270                 }
271         }
272
273         return 0;
274 }
275
276
277 int TrackCanvas::drag_stop_event()
278 {
279         int result = gui->drag_stop();
280         if( !result && mwindow->session->current_operation ) {
281                 mwindow->session->current_operation = NO_OPERATION;
282                 drag_scroll = 0;
283         }
284         return result;
285 }
286
287 int TrackCanvas::drag_stop(int *redraw)
288 {
289         int result = 0;
290         int insertion  = 0;
291         int over_window = 0;
292         int cursor_x = -1, cursor_y = -1;
293
294         if( get_cursor_over_window() ) {
295                 if( (cursor_x = get_relative_cursor_x()) >= 0 && cursor_x < get_w() &&
296                     (cursor_y = get_relative_cursor_y()) >= 0 && cursor_y < get_h() )
297                         over_window = 1;
298                 else {
299                         Track *track = pane->over_patchbay();
300                         if( track && mwindow->session->track_highlighted == track )
301                                 over_window = 1;
302                 }
303         }
304
305         if( over_window ) {
306                 switch(mwindow->session->current_operation) {
307                 case DRAG_VTRANSITION:
308                 case DRAG_ATRANSITION:
309                         if(mwindow->session->edit_highlighted) {
310                                 if( (mwindow->session->current_operation == DRAG_ATRANSITION &&
311                                      mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
312                                     (mwindow->session->current_operation == DRAG_VTRANSITION &&
313                                      mwindow->session->track_highlighted->data_type == TRACK_VIDEO) ) {
314                                         mwindow->session->current_operation = NO_OPERATION;
315                                         mwindow->paste_transition();
316                                         result = 1;
317                                 }
318                         }
319                         *redraw = 1;
320                         break;
321
322 // Behavior for dragged plugins is limited by the fact that a shared plugin
323 // can only refer to a standalone plugin that exists in the same position in
324 // time.  Dragging a plugin from one point in time to another can't produce
325 // a shared plugin to the original plugin.  In this case we relocate the
326 // plugin instead of sharing it.
327                 case DRAG_AEFFECT_COPY:
328                 case DRAG_VEFFECT_COPY:
329                         if( mwindow->session->track_highlighted &&
330                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
331                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
332                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
333                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) ) {
334                                 mwindow->session->current_operation = NO_OPERATION;
335
336                                 int64_t drop_position = -1;
337                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
338                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
339                                 Track *hi_track = mwindow->session->track_highlighted;
340
341 // Insert shared plugin in source
342 // Move source to different location
343                                 if( hi_plugin_set && hi_plugin_set->track == drag_plugin->track ) {
344 //printf("TrackCanvas::drag_stop 6\n");
345                                         drop_position = drop_plugin_position(hi_plugin_set, drag_plugin);
346                                         if( drop_position < 0 ) {
347                                                 result = 1;
348                                                 break;          // Do not do anything
349                                         }
350
351                                         Track *track = mwindow->session->track_highlighted;
352                                         drop_position = track->frame_align(drop_position, 0);
353                                         mwindow->move_effect(drag_plugin, hi_plugin_set, drop_position);
354                                         result = 1;
355                                 }
356                                 else if( hi_track ) {
357 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
358                                         Edit *hi_edit = mwindow->session->edit_highlighted;
359                                         drop_position = hi_edit ? hi_edit->startproject : 0;
360                                         if( drop_position < 0 ) {
361                                                 result = 1;
362                                                 break;          // Do not do anything
363                                         }
364
365                                         Track *track = mwindow->session->track_highlighted;
366                                         drop_position = track->frame_align(drop_position, 0);
367                                         mwindow->move_effect(drag_plugin, hi_track, drop_position);
368                                         result = 1;
369                                 }
370                         }
371                         break;
372
373                 case DRAG_AEFFECT:
374                 case DRAG_VEFFECT:
375                         if( mwindow->session->track_highlighted &&
376                             ((mwindow->session->current_operation == DRAG_AEFFECT &&
377                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
378                              (mwindow->session->current_operation == DRAG_VEFFECT &&
379                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) ) {
380 // Drop all the effects
381                                 PluginSet *plugin_set = mwindow->session->pluginset_highlighted;
382                                 Track *track = mwindow->session->track_highlighted;
383                                 double start = 0;
384                                 double length = track->get_length();
385
386                                 if(mwindow->session->plugin_highlighted) {
387                                         start = track->from_units(mwindow->session->plugin_highlighted->startproject);
388                                         length = track->from_units(mwindow->session->plugin_highlighted->length);
389                                         if(length <= 0) length = track->get_length();
390                                 }
391                                 else if(mwindow->session->pluginset_highlighted) {
392                                                 start = track->from_units(plugin_set->length());
393                                         length = track->get_length() - start;
394                                         if(length <= 0) length = track->get_length();
395                                 }
396                                 else if(mwindow->edl->local_session->get_selectionend() >
397                                         mwindow->edl->local_session->get_selectionstart()) {
398                                         start = mwindow->edl->local_session->get_selectionstart();
399                                         length = mwindow->edl->local_session->get_selectionend() -
400                                                 mwindow->edl->local_session->get_selectionstart();
401                                 }
402 // Move to a point between two edits
403                                 else if(mwindow->session->edit_highlighted) {
404                                         start = mwindow->session->track_highlighted->from_units(
405                                                 mwindow->session->edit_highlighted->startproject);
406                                         length = mwindow->session->track_highlighted->from_units(
407                                                 mwindow->session->edit_highlighted->length);
408                                 }
409                                 start = mwindow->edl->align_to_frame(start, 0);
410                                 mwindow->insert_effects_canvas(start, length);
411                                 *redraw = 1;
412                         }
413                         if( mwindow->session->track_highlighted )
414                                 result = 1;  // we have to cleanup
415                         break;
416
417                 case DRAG_ASSET:
418                         if(mwindow->session->track_highlighted) {
419                                 double asset_duration = 0;
420                                 int64_t asset_length_units = 0;
421
422                                 if(mwindow->session->current_operation == DRAG_ASSET &&
423                                         mwindow->session->drag_assets->total) {
424                                         Indexable *indexable = mwindow->session->drag_assets->values[0];
425                                         // we use video if we are over video and audio if we are over audio
426                                         if( indexable->have_video() &&
427                                             mwindow->session->track_highlighted->data_type == TRACK_VIDEO ) {
428                                                 // Images have length -1
429                                                 int64_t video_length = indexable->get_video_frames();
430                                                 if (video_length < 0) {
431                                                         if(mwindow->edl->session->si_useduration)
432                                                                 video_length = mwindow->edl->session->si_duration;
433                                                         else
434                                                                 video_length = 1.0 / mwindow->edl->session->frame_rate ;
435                                                 }
436                                                 asset_duration = video_length / indexable->get_frame_rate();
437                                         }
438                                         else if( indexable->have_audio() &&
439                                                  mwindow->session->track_highlighted->data_type == TRACK_AUDIO ) {
440                                                 int64_t audio_length = indexable->get_audio_samples();
441                                                 asset_duration = (double)audio_length / indexable->get_sample_rate();
442                                         }
443                                         else {
444                                                 result = 1;
445                                                 break;  // Do not do anything
446                                         }
447                                 }
448                                 else if( mwindow->session->current_operation == DRAG_ASSET &&
449                                          mwindow->session->drag_clips->total ) {
450                                         EDL *clip = mwindow->session->drag_clips->values[0];
451                                         asset_duration = clip->tracks->total_length();
452                                 }
453                                 else {
454                                         printf("DRAG_ASSET error: Asset dropped, but both drag_clips and drag_assets total is zero\n");
455                                 }
456
457                                 asset_length_units = mwindow->session->track_highlighted->to_units(asset_duration, 1);
458                                 int64_t drop_position = drop_edit_position (&insertion, NULL, asset_length_units);
459                                 if( drop_position < 0 ) {
460                                         result = 1;
461                                         break;          // Do not do anything
462                                 }
463
464                                 Track *track = mwindow->session->track_highlighted;
465                                 double track_position = track->from_units(drop_position);
466                                 track_position = mwindow->edl->align_to_frame(track_position, 0);
467
468 //                              if (!insertion) {
469 //                                      // FIXME, we should create an mwindow/EDL method that overwrites, without clearing the keyframes and autos
470 //                                      // Unfortunately, this is _a lot_ of work to do right
471 //                                      printf("Problematic insertion\n");
472 //                                      mwindow->edl->tracks->clear(track_position,
473 //                                              track_position + asset_duration, 0);
474 //                              }
475                                 mwindow->paste_assets(track_position, track, !insertion);
476                                 result = 1;    // need to be one no matter what, since we have track highlited so we have to cleanup....
477                         }
478                         break;
479
480                 case DRAG_EDIT:
481                         mwindow->session->current_operation = NO_OPERATION;
482                         if(mwindow->session->track_highlighted) {
483                                 if( mwindow->session->track_highlighted->data_type ==
484                                         mwindow->session->drag_edit->track->data_type) {
485                                         int64_t drop_position = drop_edit_position(&insertion,
486                                                         mwindow->session->drag_edit,
487                                                         mwindow->session->drag_edit->length);
488                                         if( drop_position < 0 ) {
489                                                 result = 1;
490                                                 break;          // Do not do anything
491                                         }
492                                         Track *track = mwindow->session->track_highlighted;
493                                         double track_position = track->from_units(drop_position);
494                                         track_position = mwindow->edl->align_to_frame(track_position, 0);
495                                         mwindow->move_edits(mwindow->session->drag_edits,
496                                                         track, track_position, insertion);
497                                 }
498
499                                 result = 1;
500                         }
501                         break;
502                 case DRAG_GROUP:
503                         mwindow->session->current_operation = NO_OPERATION;
504                         EDL *drag_group = mwindow->session->drag_group;
505                         if( drag_group ) {
506                                 Track *drop_track = mwindow->session->track_highlighted;
507                                 Track *drag_track = mwindow->session->drag_group_first_track;
508                                 Edit *drag_edit = mwindow->session->drag_group_edit;
509                                 Track *edit_track = drag_edit ? drag_edit->track : 0;
510                                 while( drop_track && edit_track ) {
511                                         if( edit_track == drag_track ) break;
512                                         edit_track = edit_track->previous;
513                                         drop_track = drop_track->previous;
514                                 }
515                                 if( drop_track ) {
516                                         double cur_pos = mwindow->session->drag_group_position;
517                                         double new_pos = mwindow->edl->get_cursor_position(cursor_x, pane->number);
518                                         new_pos -= mwindow->session->drag_position - cur_pos;
519                                         new_pos = mwindow->edl->align_to_frame(new_pos, 0);
520                                         double drop_position = new_pos;
521                                         int ret = test_track_group(drag_group, drop_track, new_pos);
522                                         if( !ret && new_pos != drop_position ) {
523                                                 drop_position = new_pos;
524                                                 ret = test_track_group(drag_group, drop_track, new_pos);
525                                         }
526                                         if( ret >= 0 )
527                                                 mwindow->move_group(drag_group, drop_track, drop_position,
528                                                         ret > 0 ? !shift_down() : shift_down());
529                                         drag_group->remove_user();
530                                         mwindow->session->drag_group = 0;
531                                 }
532                         }
533                         result = 1;
534                         break;
535                 }
536         }
537
538         return result;
539 }
540
541 int TrackCanvas::drag_start_event()
542 {
543         int result = 0;
544         int redraw = 0;
545         int rerender = 0;
546         int new_cursor, update_cursor;
547
548         if( mwindow->session->current_operation == GROUP_TOGGLE )
549                 mwindow->session->current_operation = NO_OPERATION;
550         else if( mwindow->session->current_operation != NO_OPERATION )
551                 return 0;
552
553         if(is_event_win()) {
554                 if(do_plugins(get_drag_x(), get_drag_y(),
555                                 1, 0, redraw, rerender)) {
556                         result = 1;
557                 }
558                 else if(do_edits(get_drag_x(), get_drag_y(),
559                                 0, 1, redraw, rerender, new_cursor, update_cursor)) {
560                         result = 1;
561                 }
562         }
563
564         return result;
565 }
566
567 int64_t TrackCanvas::drop_edit_position(int *is_insertion, Edit *moved_edit, int64_t moved_edit_length)
568 {
569         // get the canvas/track position
570         Track *track = mwindow->session->track_highlighted;
571         int cursor_x = get_relative_cursor_x();
572         double zoom_scale = (double)mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample;
573         double cur_pos = (cursor_x + mwindow->edl->local_session->view_start[pane->number]) / zoom_scale;
574         double position = mwindow->edl->align_to_frame(cur_pos, 1);
575         if( position <= 0 ) {
576                 *is_insertion = 1;
577                 return 0;
578         }
579         double cursor_position = position;
580         int64_t drop_position = track->to_units(cursor_position, 1);
581         if( moved_edit ) { // relative cursor position depends upon drop point
582                 double moved_edit_start = moved_edit->track->from_units(moved_edit->startproject);
583                 position -= mwindow->session->drag_position - moved_edit_start;
584         }
585         int64_t edit_position = track->to_units(position, 1);
586         int64_t grab_position = edit_position;
587         if( !moved_edit )  // for clips and assets acts as they were grabbed in the middle
588                 grab_position -= moved_edit_length / 2;
589         Edit *last_edit = track->edits->last;
590         if( !last_edit || edit_position >= (last_edit->startproject+last_edit->length) ) {
591                 *is_insertion = 0;
592                 return edit_position;
593         }
594
595         int64_t drop_start = 0, drop_end = 0;  // drop zone boundries
596         Edit *next_edit = track->edits->first;
597         while( next_edit ) {
598                 Edit *edit = next_edit;  next_edit = (Edit *)edit->next;
599                 int64_t edit_start = edit->startproject;
600                 int64_t edit_end = edit_start + edit->length;
601                 double edit_start_pos = edit->track->from_units(edit_start);
602                 if( (fabs(edit_start_pos-cursor_position)*zoom_scale) < HANDLE_W ) {
603                         *is_insertion = 1; // cursor is close to the beginning of an edit -> insertion
604                         return edit_start;
605                 }
606                 double edit_end_pos = edit->track->from_units(edit_end);
607                 if( (fabs(edit_end_pos-cursor_position)*zoom_scale) < HANDLE_W ) {
608                         *is_insertion = 1; // cursor is close to the end of an edit -> insertion
609                         return edit_end;
610                 }
611                 if( edit != moved_edit && !edit->silence() )
612                         drop_start = edit_end; // reset drop zone
613                 if( next_edit ) {
614                         if( next_edit == moved_edit || next_edit->silence() ) continue;
615                         drop_end = edit_end;
616                 }
617                 else
618                         drop_end = INT64_MAX;
619                 if( edit_position >= drop_start &&
620                     edit_position+moved_edit_length < drop_end ) {
621                         *is_insertion = 0; // fits in the zone
622                         return edit_position;
623                 }
624                 if( drop_position < drop_end ) { // drop in the zone
625                         if( (drop_end - drop_start) >= moved_edit_length ) {
626                                 *is_insertion = 0; // fits in the zone, but over the edge
627                                 int64_t dx0 = llabs(drop_position - drop_start);
628                                 int64_t dx1 = llabs(drop_position - drop_end);
629                                 return dx0 < dx1 ? drop_start : drop_end - moved_edit_length;
630                         }
631                         *is_insertion = 1;
632                         int64_t edit_center = (edit_start + edit_end) / 2;
633                         return position < edit_center ? drop_start : drop_end;
634                 }
635         }
636
637         *is_insertion = 0;
638         return -1;
639 }
640
641 int64_t TrackCanvas::drop_plugin_position(PluginSet *plugin_set, Plugin *moved_plugin)
642 {
643         // get the canvas/track position
644         Track *track = plugin_set->track;
645         double moved_plugin_length = moved_plugin->track->from_units(moved_plugin->length);
646         int64_t track_plugin_length = track->to_units(moved_plugin_length, 0);
647         int cursor_x = get_relative_cursor_x();
648         double zoom_scale = (double)mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample;
649         double cur_pos = (cursor_x + mwindow->edl->local_session->view_start[pane->number]) / zoom_scale;
650         double position = mwindow->edl->align_to_frame(cur_pos, 1);
651         if( position <= 0 ) return 0;
652         int64_t drop_position = track->to_units(position, 1);
653         Plugin *last_plugin = (Plugin *)plugin_set->last;
654         if( !last_plugin ) return drop_position;
655         double plugin_set_end = last_plugin->track->from_units(last_plugin->startproject+last_plugin->length);
656         if( position >= plugin_set_end ) return drop_position;
657         double moved_plugin_start = moved_plugin->track->from_units(moved_plugin->startproject);
658         position -= mwindow->session->drag_position - moved_plugin_start;
659         int64_t plugin_position = track->to_units(position, 1);
660
661         int64_t drop_start = 0, drop_end = 0;  // drop zone boundries
662         Plugin *next_plugin = (Plugin *)plugin_set->first;
663         while( next_plugin ) {
664                 Plugin *plugin = next_plugin;  next_plugin = (Plugin *)plugin->next;
665                 int64_t plugin_start = plugin->startproject;
666                 int64_t plugin_end = plugin_start + plugin->length;
667                 double plugin_end_pos = plugin->track->from_units(plugin_end);
668                 int64_t track_plugin_end = track->to_units(plugin_end_pos, 0);
669                 if( plugin != moved_plugin && !plugin->silence() )
670                         drop_start = track_plugin_end;
671                 if( next_plugin ) {
672                         if( next_plugin == moved_plugin || next_plugin->silence() ) continue;
673                         drop_end = track_plugin_end;
674                 }
675                 else
676                         drop_end = INT64_MAX;
677                 if( plugin_position >= drop_start && // fits in the zone
678                     plugin_position+track_plugin_length < drop_end ) {
679                         return plugin_position;
680                 }
681                 if( drop_position < drop_end ) { // drop in the zone
682                         if( (drop_end - drop_start) >= track_plugin_length ) {
683                                 int64_t dx0 = llabs(drop_position - drop_start);
684                                 int64_t dx1 = llabs(drop_position - drop_end);
685                                 return dx0 < dx1 ? drop_start : drop_end - track_plugin_length;
686                         }
687                 }
688         }
689
690         return -1;
691 }
692
693 void TrackCanvas::draw(int mode, int hide_cursor)
694 {
695         const int debug = 0;
696
697 // Swap pixmap layers
698         if(get_w() != background_pixmap->get_w() ||
699                 get_h() != background_pixmap->get_h())
700         {
701                 delete background_pixmap;
702                 background_pixmap = new BC_Pixmap(this, get_w(), get_h());
703         }
704
705 // Cursor disappears after resize when this is called.
706 // Cursor doesn't redraw after editing when this isn't called.
707         if(pane->cursor && hide_cursor) pane->cursor->hide();
708         draw_top_background(get_parent(), 0, 0, get_w(), get_h(), background_pixmap);
709
710         if(debug) PRINT_TRACE
711         draw_resources(mode);
712
713         if(debug) PRINT_TRACE
714         draw_overlays();
715         if(debug) PRINT_TRACE
716 }
717
718 void TrackCanvas::update_cursor(int flush)
719 {
720         if( arrow_mode() )
721                 set_cursor(ARROW_CURSOR, 0, flush);
722         else if( ibeam_mode() )
723                 set_cursor(IBEAM_CURSOR, 0, flush);
724 }
725
726
727 void TrackCanvas::test_timer()
728 {
729         if( resource_timer->get_difference() > 1000 && !hourglass_enabled ) {
730                 start_hourglass();
731                 hourglass_enabled = 1;
732         }
733 }
734
735
736 void TrackCanvas::draw_indexes(Indexable *indexable)
737 {
738         IndexState *index_state = indexable->index_state;
739 // Don't redraw raw samples
740         if(index_state->index_zoom > mwindow->edl->local_session->zoom_sample)
741                 return;
742         draw_resources(NORMAL_DRAW, 1, indexable);
743         draw_overlays();
744         flash(0);
745 }
746
747 void TrackCanvas::draw_resources(int mode,
748         int indexes_only,
749         Indexable *indexable)
750 {
751         const int debug = 0;
752
753         if(debug) PRINT_TRACE
754
755 // can't stop thread here, because this is called for every pane
756 //      if(mode != IGNORE_THREAD && !indexes_only)
757 //              gui->resource_thread->stop_draw(!indexes_only);
758
759         if(mode != IGNORE_THREAD &&
760                 !indexes_only &&
761                 !gui->resource_thread->interrupted)
762         {
763                 printf("TrackCanvas::draw_resources %d: called without stopping ResourceThread\n",
764                         __LINE__);
765
766                 BC_Signals::dump_stack();
767         }
768
769         if(debug) PRINT_TRACE
770
771         resource_timer->update();
772
773 // Age resource pixmaps for deletion
774         if(!indexes_only)
775                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
776                         gui->resource_pixmaps.values[i]->visible--;
777
778         if(mode == FORCE_REDRAW)
779                 gui->resource_pixmaps.remove_all_objects();
780
781         if(debug) PRINT_TRACE
782
783 // Search every edit
784         for(Track *current = mwindow->edl->tracks->first;
785                 current;
786                 current = NEXT)
787         {
788                 if(debug) PRINT_TRACE
789                 for(Edit *edit = current->edits->first; edit; edit = edit->next)
790                 {
791                         if(debug) PRINT_TRACE
792                         if( current->data_type != TRACK_SUBTITLE )
793                                 if(!edit->asset && !edit->nested_edl) continue;
794                         if(indexes_only)
795                         {
796                                 if(edit->track->data_type != TRACK_AUDIO) continue;
797
798                                 if(edit->nested_edl &&
799                                         strcmp(indexable->path, edit->nested_edl->path)) continue;
800
801                                 if(edit->asset &&
802                                         strcmp(indexable->path, edit->asset->path)) continue;
803                         }
804
805                         if(debug) PRINT_TRACE
806
807                         int64_t edit_x, edit_y, edit_w, edit_h;
808                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
809
810 // Edit is visible
811                         if(MWindowGUI::visible(edit_x, edit_x + edit_w, 0, get_w()) &&
812                                 MWindowGUI::visible(edit_y, edit_y + edit_h, 0, get_h()))
813                         {
814                                 int64_t pixmap_x, pixmap_w, pixmap_h;
815                                 if(debug) PRINT_TRACE
816
817 // Search for existing pixmap containing edit
818                                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
819                                 {
820                                         ResourcePixmap* pixmap = gui->resource_pixmaps.values[i];
821 // Same pointer can be different edit if editing took place
822                                         if(pixmap->edit_id == edit->id &&
823                                                 pixmap->pane_number == pane->number)
824                                         {
825                                                 pixmap->visible = 1;
826                                                 break;
827                                         }
828                                 }
829                                 if(debug) PRINT_TRACE
830
831 // Get new size, offset of pixmap needed
832                                 get_pixmap_size(edit,
833                                         edit_x,
834                                         edit_w,
835                                         pixmap_x,
836                                         pixmap_w,
837                                         pixmap_h);
838                                 if(debug) PRINT_TRACE
839
840 // Draw new data
841                                 if(pixmap_w && pixmap_h)
842                                 {
843 // Create pixmap if it doesn't exist
844                                         ResourcePixmap* pixmap = create_pixmap(edit, edit_x,
845                                                 pixmap_x, pixmap_w, pixmap_h);
846 // Resize it if it's bigger
847                                         if( pixmap_w > pixmap->pixmap_w ||
848                                             pixmap_h > pixmap->pixmap_h)
849                                                 pixmap->resize(pixmap_w, pixmap_h);
850                                         pixmap->update_settings(edit, edit_x, edit_w,
851                                                 pixmap_x, pixmap_w, pixmap_h);
852 // Draw data
853                                         if( current->show_assets() )
854                                                 pixmap->draw_data(this,
855                                                         edit, edit_x, edit_w,
856                                                         pixmap_x, pixmap_w, pixmap_h,
857                                                         mode, indexes_only);
858                                         else {
859                                                 set_bg_color(BLACK);
860                                                 clear_box(0,0, pixmap_w,pixmap_h, pixmap);
861                                         }
862 // Draw title
863                                         if( current->show_titles() )
864                                                 pixmap->draw_title(this,
865                                                         edit, edit_x, edit_w,
866                                                         pixmap_x, pixmap_w);
867 // Resize it if it's smaller
868                                         if(pixmap_w < pixmap->pixmap_w ||
869                                                 pixmap_h < pixmap->pixmap_h)
870                                                 pixmap->resize(pixmap_w, pixmap_h);
871
872 // Copy pixmap to background canvas
873                                         background_pixmap->draw_pixmap(pixmap,
874                                                 pixmap->pixmap_x,
875                                                 current->y_pixel - mwindow->edl->local_session->track_start[pane->number],
876                                                 pixmap->pixmap_w,
877                                                 edit_h);
878                                 }
879                                 if(debug) PRINT_TRACE
880
881                         }
882                 }
883                 int64_t track_x, track_y, track_w, track_h;
884                 track_dimensions(current,
885                         track_x, track_y, track_w, track_h);
886                 set_color((~get_resources()->get_bg_color()) & 0xffffff);
887                 set_opaque();
888                 int x1 = track_x, x2 = x1+track_w;
889                 int y1 = track_y+track_h-1;
890                 draw_line(x1,y1, x2,y1, background_pixmap);
891         }
892
893
894 // Delete unused pixmaps
895         if(debug) PRINT_TRACE
896         if(!indexes_only)
897                 for(int i = gui->resource_pixmaps.total - 1; i >= 0; i--)
898                         if(gui->resource_pixmaps.values[i]->visible < PIXMAP_AGE)
899                         {
900 //printf("TrackCanvas::draw_resources %d\n", __LINE__);
901                                 delete gui->resource_pixmaps.values[i];
902                                 gui->resource_pixmaps.remove(gui->resource_pixmaps.values[i]);
903                         }
904         if(debug) PRINT_TRACE
905
906         if(hourglass_enabled)
907         {
908                 stop_hourglass();
909                 hourglass_enabled = 0;
910         }
911         if(debug) PRINT_TRACE
912
913 // can't stop thread here, because this is called for every pane
914 //      if(mode != IGNORE_THREAD && !indexes_only)
915 //              gui->resource_thread->start_draw();
916         if(debug) PRINT_TRACE
917
918
919 }
920
921 ResourcePixmap* TrackCanvas::create_pixmap(Edit *edit,
922         int64_t edit_x,
923         int64_t pixmap_x,
924         int64_t pixmap_w,
925         int64_t pixmap_h)
926 {
927         ResourcePixmap *result = 0;
928
929         for(int i = 0; i < gui->resource_pixmaps.total; i++)
930         {
931 //printf("TrackCanvas::create_pixmap 1 %d %d\n", edit->id, resource_pixmaps.values[i]->edit->id);
932                 if(gui->resource_pixmaps.values[i]->edit_id == edit->id &&
933                         gui->resource_pixmaps.values[i]->pane_number == pane->number)
934                 {
935                         result = gui->resource_pixmaps.values[i];
936                         break;
937                 }
938         }
939
940         if(!result)
941         {
942 //SET_TRACE
943                 result = new ResourcePixmap(mwindow,
944                         gui,
945                         edit,
946                         pane->number,
947                         pixmap_w,
948                         pixmap_h);
949 //SET_TRACE
950                 gui->resource_pixmaps.append(result);
951         }
952
953 //      result->resize(pixmap_w, pixmap_h);
954         return result;
955 }
956
957 void TrackCanvas::get_pixmap_size(Edit *edit,
958         int64_t edit_x,
959         int64_t edit_w,
960         int64_t &pixmap_x,
961         int64_t &pixmap_w,
962         int64_t &pixmap_h)
963 {
964
965 // Align x on frame boundaries
966
967
968 //      switch(edit->edits->track->data_type)
969 //      {
970 //              case TRACK_AUDIO:
971
972                         pixmap_x = edit_x;
973                         pixmap_w = edit_w;
974                         if(pixmap_x < 0)
975                         {
976                                 pixmap_w -= -edit_x;
977                                 pixmap_x = 0;
978                         }
979
980                         if(pixmap_x + pixmap_w > get_w())
981                         {
982                                 pixmap_w = get_w() - pixmap_x;
983                         }
984
985 //                      break;
986 //
987 //              case TRACK_VIDEO:
988 //              {
989 //                      int64_t picon_w = (int64_t)(edit->picon_w() + 0.5);
990 //                      int64_t frame_w = (int64_t)(edit->frame_w() + 0.5);
991 //                      int64_t pixel_increment = MAX(picon_w, frame_w);
992 //                      int64_t pixmap_x1 = edit_x;
993 //                      int64_t pixmap_x2 = edit_x + edit_w;
994 //
995 //                      if(pixmap_x1 < 0)
996 //                      {
997 //                              pixmap_x1 = (int64_t)((double)-edit_x / pixel_increment) *
998 //                                      pixel_increment +
999 //                                      edit_x;
1000 //                      }
1001 //
1002 //                      if(pixmap_x2 > get_w())
1003 //                      {
1004 //                              pixmap_x2 = (int64_t)((double)(get_w() - edit_x) / pixel_increment + 1) *
1005 //                                      pixel_increment +
1006 //                                      edit_x;
1007 //                      }
1008 //                      pixmap_x = pixmap_x1;
1009 //                      pixmap_w = pixmap_x2 - pixmap_x1;
1010 //                      break;
1011 //              }
1012 //      }
1013
1014         pixmap_h = mwindow->edl->local_session->zoom_track;
1015         Track *track = edit->edits->track;
1016         if( track->show_titles() )
1017                 pixmap_h += mwindow->theme->get_image("title_bg_data")->get_h();
1018 //printf("get_pixmap_size %d %d %d %d\n", edit_x, edit_w, pixmap_x, pixmap_w);
1019 }
1020
1021 void TrackCanvas::edit_dimensions(Edit *edit,
1022         int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1023 {
1024         x = Units::round(edit->track->from_units(edit->startproject) *
1025                         mwindow->edl->session->sample_rate /
1026                         mwindow->edl->local_session->zoom_sample -
1027                         mwindow->edl->local_session->view_start[pane->number]);
1028
1029         y = edit->edits->track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1030
1031 // Method for calculating w so when edits are together we never get off by one error due to rounding
1032         int64_t x_next = Units::round(edit->track->from_units(edit->startproject + edit->length) *
1033                         mwindow->edl->session->sample_rate /
1034                         mwindow->edl->local_session->zoom_sample -
1035                         mwindow->edl->local_session->view_start[pane->number]);
1036         w = x_next - x;
1037
1038         int edit_h = 0;
1039         if( edit->track->show_titles() )
1040                 edit_h += mwindow->theme->get_image("title_bg_data")->get_h();
1041         if( edit->track->show_assets() )
1042                 edit_h += resource_h();
1043         h = edit_h;
1044 }
1045
1046 void TrackCanvas::track_dimensions(Track *track, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1047 {
1048         x = 0;
1049         w = get_w();
1050         y = track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1051         h = track->vertical_span(mwindow->theme);
1052 }
1053
1054
1055 void TrackCanvas::draw_paste_destination()
1056 {
1057         //int cursor_x = get_relative_cursor_x();
1058         //int cursor_y = get_relative_cursor_y();
1059         int current_atrack = 0;
1060         int current_vtrack = 0;
1061         int current_aedit = 0;
1062         int current_vedit = 0;
1063         int64_t w = 0;
1064         int64_t x;
1065         double position;
1066         int insertion  = 0;
1067
1068 //if(pane->number == BOTTOM_RIGHT_PANE)
1069 //printf("TrackCanvas::draw_paste_destination %d %p\n", __LINE__, mwindow->session->track_highlighted);
1070
1071         if((mwindow->session->current_operation == DRAG_ASSET &&
1072                         (mwindow->session->drag_assets->total ||
1073                         mwindow->session->drag_clips->total)) ||
1074                 (mwindow->session->current_operation == DRAG_EDIT &&
1075                         mwindow->session->drag_edits->total)) {
1076                 Indexable *indexable = 0;
1077                 EDL *clip = 0;
1078                 //int draw_box = 0;
1079
1080                 if(mwindow->session->current_operation == DRAG_ASSET &&
1081                         mwindow->session->drag_assets->size())
1082                         indexable = mwindow->session->drag_assets->get(0);
1083
1084                 if(mwindow->session->current_operation == DRAG_ASSET &&
1085                         mwindow->session->drag_clips->size())
1086                         clip = mwindow->session->drag_clips->get(0);
1087
1088                 int has_audio = 0, has_video = 0;
1089                 double paste_audio_length = 0, paste_video_length = 0;
1090                 double paste_audio_position = -1, paste_video_position = -1;
1091
1092                 if( indexable ) {
1093                         has_audio = indexable->have_audio();
1094                         paste_audio_length = !has_audio ? 0 :
1095                                  (double)indexable->get_audio_samples() / indexable->get_sample_rate();
1096                         has_video = indexable->have_video();
1097                         Asset *asset = indexable->is_asset ? (Asset *)indexable : 0;
1098                         // Images have length -1
1099                         if( asset && asset->video_data && asset->video_length < 0 ) {
1100                                 paste_video_length = mwindow->edl->session->si_useduration ?
1101                                         mwindow->edl->session->si_duration / asset->frame_rate :
1102                                         1.0 / asset->frame_rate ;  // 1 frame
1103                         }
1104                         else
1105                                 paste_video_length = !has_video ? 0 :
1106                                         (double)indexable->get_video_frames() / indexable->get_frame_rate();
1107                 }
1108
1109                 if( clip ) {
1110                         has_audio = clip->tracks->total_audio_tracks() > 0 ? 1 : 0;
1111                         paste_audio_length = !has_audio ? 0 : clip->tracks->total_audio_length();
1112                         has_video = clip->tracks->total_video_tracks() > 0 ? 1 : 0;
1113                         paste_video_length = !has_video ? 0 : clip->tracks->total_video_length();
1114                 }
1115
1116 // 'Align cursor on frame' lengths calculations
1117                 if( mwindow->edl->session->cursor_on_frames ) {
1118                         double fps = mwindow->edl->session->frame_rate;
1119                         double aud = floor(paste_audio_length * fps) / fps;
1120                         double vid = floor(paste_video_length * fps) / fps;
1121                         double length = has_video && has_audio ?
1122                                         aud < vid ? aud : vid :
1123                                 has_video ? vid :
1124                                 has_audio ? aud : 0;
1125                         paste_video_length = paste_audio_length = length;
1126                 }
1127
1128                 if( has_audio ) { // we use video if we are over video and audio if we are over audio
1129                         int64_t length = 0;
1130                         switch( mwindow->session->track_highlighted->data_type ) {
1131                         case TRACK_VIDEO: length = paste_video_length;  break;
1132                         case TRACK_AUDIO: length = paste_audio_length;  break;
1133                         }
1134                         int64_t asset_length = mwindow->session->track_highlighted->
1135                                 to_units(length, 1);
1136                         paste_audio_position = mwindow->session->track_highlighted->
1137                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1138                 }
1139                 if( has_video ) {
1140                         int64_t asset_length = mwindow->session->track_highlighted->
1141                                 to_units(paste_video_length, 1);
1142                         paste_video_position = mwindow->session->track_highlighted->
1143                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1144                 }
1145
1146 // Get destination track
1147                 for(Track *dest = mwindow->session->track_highlighted;
1148                         dest;
1149                         dest = dest->next) {
1150                         if(dest->record) {
1151 // Get source width in pixels
1152                                 w = -1;
1153 // Use start of highlighted edit
1154                                 if(mwindow->session->edit_highlighted) {
1155                                         position = mwindow->session->track_highlighted->from_units(
1156                                                 mwindow->session->edit_highlighted->startproject);
1157                                 }
1158                                 else {
1159 // Use end of highlighted track, disregarding effects
1160                                         position = mwindow->session->track_highlighted->from_units(
1161                                                 mwindow->session->track_highlighted->edits->length());
1162                                 }
1163
1164 // Get the x coordinate
1165                                 x = Units::to_int64(position *
1166                                         mwindow->edl->session->sample_rate /
1167                                         mwindow->edl->local_session->zoom_sample) -
1168                                         mwindow->edl->local_session->view_start[pane->number];
1169
1170                                 double paste_position = -1.;
1171                                 if(dest->data_type == TRACK_AUDIO) {
1172                                         if(indexable && current_atrack < indexable->get_audio_channels()) {
1173 //printf("TrackCanvas::draw_paste_destination %d %d\n", __LINE__, current_atrack);
1174                                                 w = Units::to_int64((double)indexable->get_audio_samples() /
1175                                                         indexable->get_sample_rate() *
1176                                                         mwindow->edl->session->sample_rate /
1177                                                         mwindow->edl->local_session->zoom_sample);
1178                                                 paste_position = paste_audio_position;
1179                                         }
1180                                         else if(clip && current_atrack < clip->tracks->total_audio_tracks()) {
1181                                                 w = Units::to_int64((double)clip->tracks->total_length() *
1182                                                         mwindow->edl->session->sample_rate /
1183                                                         mwindow->edl->local_session->zoom_sample);
1184                                                 paste_position = paste_audio_position;
1185 //printf("draw_paste_destination %d\n", x);
1186                                         }
1187                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1188                                                 current_aedit < mwindow->session->drag_edits->total) {
1189                                                 Edit *edit;
1190                                                 while(current_aedit < mwindow->session->drag_edits->total &&
1191                                                         mwindow->session->drag_edits->values[current_aedit]->track->data_type != TRACK_AUDIO)
1192                                                         current_aedit++;
1193
1194                                                 if(current_aedit < mwindow->session->drag_edits->total) {
1195                                                         edit = mwindow->session->drag_edits->values[current_aedit];
1196                                                         w = Units::to_int64(edit->length / mwindow->edl->local_session->zoom_sample);
1197                                                         paste_position = mwindow->session->track_highlighted->
1198                                                                 from_units(drop_edit_position(&insertion,
1199                                                                         mwindow->session->drag_edit,
1200                                                                         mwindow->session->drag_edit->length));
1201                                                         current_aedit++;
1202                                                 }
1203                                         }
1204                                         if( paste_position >= 0 ) {
1205                                                 position = paste_position;
1206                                                 current_atrack++;
1207                                                 //draw_box = 1;
1208                                         }
1209                                         else
1210                                                 w = -1;
1211                                 }
1212                                 else if(dest->data_type == TRACK_VIDEO) {
1213 //printf("draw_paste_destination 1\n");
1214                                         if(indexable && current_vtrack < indexable->get_video_layers())
1215                                         {
1216                                                 w = Units::to_int64((double)indexable->get_video_frames() /
1217                                                         indexable->get_frame_rate() *
1218                                                         mwindow->edl->session->sample_rate /
1219                                                         mwindow->edl->local_session->zoom_sample);
1220                                                 paste_position = paste_video_position;
1221                                         }
1222                                         else if(clip && current_vtrack < clip->tracks->total_video_tracks()) {
1223                                                 w = Units::to_int64(clip->tracks->total_length() *
1224                                                         mwindow->edl->session->sample_rate /
1225                                                         mwindow->edl->local_session->zoom_sample);
1226                                                 paste_position = paste_video_position;
1227                                         }
1228                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1229                                                 current_vedit < mwindow->session->drag_edits->total) {
1230                                                 Edit *edit;
1231                                                 while(current_vedit < mwindow->session->drag_edits->total &&
1232                                                         mwindow->session->drag_edits->values[current_vedit]->track->data_type != TRACK_VIDEO)
1233                                                         current_vedit++;
1234
1235                                                 if(current_vedit < mwindow->session->drag_edits->total) {
1236                                                         edit = mwindow->session->drag_edits->values[current_vedit];
1237                                                         w = Units::to_int64(edit->track->from_units(edit->length) *
1238                                                                 mwindow->edl->session->sample_rate /
1239                                                                 mwindow->edl->local_session->zoom_sample);
1240                                                         paste_position = mwindow->session->track_highlighted->
1241                                                                 from_units(drop_edit_position(&insertion,
1242                                                                         mwindow->session->drag_edit,
1243                                                                         mwindow->session->drag_edit->length));
1244                                                         current_vedit++;
1245                                                 }
1246                                         }
1247                                         if( paste_position >= 0 ) {
1248                                                 position = paste_position;
1249                                                 current_vtrack++;
1250                                                 //draw_box = 1;
1251                                         }
1252                                         else
1253                                                 w = -1;
1254                                 }
1255
1256                                 if(w >= 0) {
1257 // Get the x coordinate
1258                                         x = Units::to_int64(position *
1259                                                 mwindow->edl->session->sample_rate /
1260                                                 mwindow->edl->local_session->zoom_sample) -
1261                                                 mwindow->edl->local_session->view_start[pane->number];
1262                                         int y = dest->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1263                                         int h = dest->vertical_span(mwindow->theme);
1264
1265 //printf("TrackCanvas::draw_paste_destination 2 %d %d %d %d\n", x, y, w, h);
1266                                         if(x < -BC_INFINITY) {
1267                                                 w -= -BC_INFINITY - x;
1268                                                 x += -BC_INFINITY - x;
1269                                         }
1270                                         w = MIN(65535, w);
1271 // if(pane->number == TOP_RIGHT_PANE)
1272 // printf("TrackCanvas::draw_paste_destination %d %d %d %d %d\n",
1273 // __LINE__, x, y, w, h);
1274                                         if (insertion)
1275                                                 draw_highlight_insertion(x, y, w, h);
1276                                         else
1277                                                 draw_highlight_rectangle(x, y, w, h);
1278                                 }
1279                         }
1280                 }
1281         }
1282 }
1283
1284 void TrackCanvas::plugin_dimensions(Plugin *plugin, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1285 {
1286         x = Units::round(plugin->track->from_units(plugin->startproject) *
1287                 mwindow->edl->session->sample_rate /
1288                 mwindow->edl->local_session->zoom_sample -
1289                 mwindow->edl->local_session->view_start[pane->number]);
1290         w = Units::round(plugin->track->from_units(plugin->length) *
1291                 mwindow->edl->session->sample_rate /
1292                 mwindow->edl->local_session->zoom_sample);
1293         y = plugin->track->y_pixel
1294                 - mwindow->edl->local_session->track_start[pane->number];
1295         if( plugin->track->show_titles() )
1296                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1297         if( plugin->track->show_assets() )
1298                 y += resource_h();
1299         y += plugin->plugin_set->get_number() *
1300                         mwindow->theme->get_image("plugin_bg_data")->get_h();
1301         h = mwindow->theme->get_image("plugin_bg_data")->get_h();
1302 }
1303
1304 int TrackCanvas::resource_h()
1305 {
1306         return mwindow->edl->local_session->zoom_track;
1307 }
1308
1309 void TrackCanvas::draw_highlight_rectangle(int x, int y, int w, int h)
1310 {
1311
1312 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1313 // just draw arrows, so user has indication that something is there
1314 // FIXME: get better colors
1315
1316         if (x + w <= 0)
1317         {
1318                 draw_triangle_left(0, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1319                 return;
1320         } else
1321         if (x >= get_w())
1322         {
1323                 draw_triangle_right(get_w() - h * 2/3, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1324                 return;
1325         }
1326
1327 // Fix bug in heroines & cvs version as of 22.8.2005:
1328 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1329         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1330         if(x < -10)
1331         {
1332                 w += x - -10;
1333                 x = -10;
1334         }
1335
1336         if(y < -10)
1337         {
1338                 h += y - -10;
1339                 y = -10;
1340         }
1341
1342         w = MIN(w, get_w() + 20);
1343         h = MIN(h, get_h() + 20);
1344         if(w > 0 && h > 0)
1345         {
1346                 set_color(mwindow->preferences->highlight_inverse);
1347                 set_inverse();
1348                 //draw_rectangle(x, y, w, h);
1349                 draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1350                 draw_rectangle(x + 2, y + 2, w - 4, h - 4);
1351                 set_opaque();
1352                 draw_rectangle(x, y, w, h);
1353         }
1354 //if(pane->number == TOP_RIGHT_PANE)
1355 //printf("TrackCanvas::draw_highlight_rectangle %d %d %d %d %d\n", __LINE__, x, y, w, h);
1356 }
1357
1358 void TrackCanvas::draw_highlight_insertion(int x, int y, int w, int h)
1359 {
1360
1361 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1362 // just draw arrows, so user has indication that something is there
1363 // FIXME: get better colors
1364
1365
1366
1367         int h1 = h / 8;
1368         int h2 = h / 4;
1369
1370         set_inverse();
1371
1372 /* these don't look so good
1373
1374         draw_line(x, y, x, y+h);
1375         draw_line(x - h2 * 2, y + h1*2,   x - h2, y+h1*2);
1376         draw_line(x - h2 * 2, y + h1*2+1, x - h2, y+h1*2+1);
1377         draw_line(x - h2 * 2, y + h1*6,   x - h2, y+h1*6);
1378         draw_line(x - h2 * 2, y + h1*6+1, x - h2, y+h1*6+1);
1379 */
1380         draw_triangle_right(x - h2, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1381         draw_triangle_right(x - h2, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1382
1383 /*      draw_line(x + h2 * 2, y + h1*2,   x + h2, y+h1*2);
1384         draw_line(x + h2 * 2, y + h1*2+1, x + h2, y+h1*2+1);
1385         draw_line(x + h2 * 2, y + h1*6,   x + h2, y+h1*6);
1386         draw_line(x - h2 * 2, y + h1*6+1, x + h2, y+h1*6+1);
1387 */
1388         draw_triangle_left(x, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1389         draw_triangle_left(x, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1390
1391 // draw the box centred around x
1392         x -= w / 2;
1393 // Fix bug in heroines & cvs version as of 22.8.2005:
1394 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1395         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1396         if(x < -10)
1397         {
1398                 w += x - -10;
1399                 x = -10;
1400         }
1401         if(y < -10)
1402         {
1403                 h += y - -10;
1404                 y = -10;
1405         }
1406         w = MIN(w, get_w() + 20);
1407         h = MIN(h, get_h() + 20);
1408         set_color(mwindow->preferences->highlight_inverse);
1409         set_inverse();
1410         draw_rectangle(x, y, w, h);
1411         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1412         set_opaque();
1413 //printf("TrackCanvas::draw_highlight_insertion %d %d %d %d\n", x, y, w, h);
1414 }
1415
1416 void TrackCanvas::draw_playback_cursor()
1417 {
1418 // Called before playback_cursor exists
1419 //      if(mwindow->playback_cursor && mwindow->playback_cursor->visible)
1420 //      {
1421 //              mwindow->playback_cursor->visible = 0;
1422 //              mwindow->playback_cursor->draw();
1423 //      }
1424 }
1425
1426 void TrackCanvas::get_handle_coords(Edit *edit, int64_t &x, int64_t &y, int64_t &w, int64_t &h, int side)
1427 {
1428         int handle_w = mwindow->theme->edithandlein_data[0]->get_w();
1429         int handle_h = mwindow->theme->edithandlein_data[0]->get_h();
1430
1431         edit_dimensions(edit, x, y, w, h);
1432
1433         if( edit->track->show_titles() )
1434                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1435
1436         if(side == EDIT_OUT)
1437                 x += w - handle_w;
1438
1439         h = handle_h;
1440         w = handle_w;
1441 }
1442
1443 void TrackCanvas::get_transition_coords(Edit *edit,
1444                 int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1445 {
1446         int transition_w = 30, transition_h = 30;
1447         int has_titles = edit->track->show_titles();
1448         int has_assets = edit->track->show_assets();
1449         double title_bg_h = mwindow->theme->get_image("title_bg_data")->get_h();
1450         double asset_h = resource_h();
1451         double ys = has_assets ? asset_h : has_titles ? title_bg_h : 0;
1452         double dy = has_titles ?
1453                 ( has_assets ? title_bg_h + asset_h/2 : title_bg_h/2 ) :
1454                 ( has_assets ? asset_h/2 : 0) ;
1455         double title_h = mwindow->theme->title_h;
1456         if( dy < title_h / 2 ) { ys = title_h;  dy = ys / 2; }
1457         y += dy;
1458
1459         x -= transition_w / 2;
1460         y -= transition_h / 2;
1461         w = transition_w;
1462         h = transition_h;
1463 }
1464
1465 void TrackCanvas::draw_highlighting()
1466 {
1467         int64_t x, y, w, h;
1468         int draw_box = 0;
1469
1470         switch(mwindow->session->current_operation)
1471         {
1472                 case DRAG_ATRANSITION:
1473                 case DRAG_VTRANSITION:
1474 //printf("TrackCanvas::draw_highlighting 1 %p %p\n",
1475 //      mwindow->session->track_highlighted, mwindow->session->edit_highlighted);
1476                         if(mwindow->session->edit_highlighted) {
1477                                 if((mwindow->session->current_operation == DRAG_ATRANSITION &&
1478                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1479                                         (mwindow->session->current_operation == DRAG_VTRANSITION &&
1480                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) {
1481                                         edit_dimensions(mwindow->session->edit_highlighted,
1482                                                 x, y, w, h);
1483                                         if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1484                                                 MWindowGUI::visible(y, y + h, 0, get_h())) {
1485                                                 draw_box = 1;
1486                                                 get_transition_coords(mwindow->session->edit_highlighted,
1487                                                         x, y, w, h);
1488                                         }
1489                                 }
1490                         }
1491                         break;
1492
1493 // Dragging a new effect from the Resource window
1494                 case DRAG_AEFFECT:
1495                 case DRAG_VEFFECT:
1496                         if(mwindow->session->track_highlighted &&
1497                                 ((mwindow->session->current_operation == DRAG_AEFFECT &&
1498                                   mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1499                                  (mwindow->session->current_operation == DRAG_VEFFECT &&
1500                                   mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1501 // Put it before another plugin
1502                                 if(mwindow->session->plugin_highlighted) {
1503                                         plugin_dimensions(mwindow->session->plugin_highlighted,
1504                                                 x, y, w, h);
1505 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1506                                 }
1507 // Put it after a plugin set
1508                                 else if( mwindow->session->pluginset_highlighted &&
1509                                          mwindow->session->pluginset_highlighted->last ) {
1510                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last,
1511                                                 x, y, w, h);
1512 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1513                                         int64_t track_x, track_y, track_w, track_h;
1514                                         track_dimensions(mwindow->session->track_highlighted,
1515                                                 track_x, track_y, track_w, track_h);
1516
1517                                         x += w;
1518                                         w = Units::round(
1519                                                         mwindow->session->track_highlighted->get_length() *
1520                                                         mwindow->edl->session->sample_rate /
1521                                                         mwindow->edl->local_session->zoom_sample -
1522                                                         mwindow->edl->local_session->view_start[pane->number]) -
1523                                                 x;
1524 //printf("TrackCanvas::draw_highlighting 2 %d\n", w);
1525                                         if(w <= 0) w = track_w;
1526                                 }
1527                                 else {
1528                                         track_dimensions(mwindow->session->track_highlighted,
1529                                                 x, y, w, h);
1530
1531 //printf("TrackCanvas::draw_highlighting 1 %d %d %d %d\n", x, y, w, h);
1532 // Put it in a new plugin set determined by the selected range
1533                                         if(mwindow->edl->local_session->get_selectionend() >
1534                                                 mwindow->edl->local_session->get_selectionstart()) {
1535                                                 x = Units::to_int64(mwindow->edl->local_session->get_selectionstart() *
1536                                                         mwindow->edl->session->sample_rate /
1537                                                         mwindow->edl->local_session->zoom_sample -
1538                                                         mwindow->edl->local_session->view_start[pane->number]);
1539                                                 w = Units::to_int64((mwindow->edl->local_session->get_selectionend() -
1540                                                         mwindow->edl->local_session->get_selectionstart()) *
1541                                                         mwindow->edl->session->sample_rate /
1542                                                         mwindow->edl->local_session->zoom_sample);
1543                                         }
1544 // Put it in a new plugin set determined by an edit boundary
1545                                         else if(mwindow->session->edit_highlighted) {
1546                                                 int64_t temp_y, temp_h;
1547                                                 edit_dimensions(mwindow->session->edit_highlighted,
1548                                                         x, temp_y, w, temp_h);
1549                                         }
1550 // Put it at the beginning of the track in a new plugin set
1551                                 }
1552
1553                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1554                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1555 //printf("TrackCanvas::draw_highlighting 1\n");
1556                                         draw_box = 1;
1557                                 }
1558                         }
1559                         break;
1560
1561                 case DRAG_ASSET:
1562                         if(mwindow->session->track_highlighted) {
1563 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1564
1565 //                              if(MWindowGUI::visible(y, y + h, 0, get_h()))
1566 //                              {
1567                                         draw_paste_destination();
1568 //                              }
1569                         }
1570                         break;
1571
1572                 case DRAG_EDIT:
1573                         if(mwindow->session->track_highlighted) {
1574 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1575 //
1576 //                              if(MWindowGUI::visible(y, y + h, 0, get_h())) {
1577                                         draw_paste_destination();
1578 //                              }
1579                         }
1580                         break;
1581                 case DRAG_GROUP:
1582                         if( mwindow->session->track_highlighted && mwindow->session->drag_group &&
1583                             mwindow->session->drag_group_edit && mwindow->session->drag_group_first_track ) {
1584                                 Track *track = mwindow->session->track_highlighted;
1585                                 EDL *drag_group = mwindow->session->drag_group;
1586                                 Track *edit_track = mwindow->session->drag_group_edit->track;
1587                                 Track *drag_track = mwindow->session->drag_group_first_track;
1588                                 while( track && edit_track && edit_track != drag_track ) {
1589                                         edit_track = edit_track->previous;
1590                                         track = track->previous;
1591                                 }
1592                                 int cx = get_cursor_x();
1593                                 double cur_pos = mwindow->session->drag_group_position;
1594                                 double new_pos = mwindow->edl->get_cursor_position(cx, pane->number);
1595                                 new_pos -= mwindow->session->drag_position - cur_pos;
1596                                 new_pos = mwindow->edl->align_to_frame(new_pos, 0);
1597                                 double drop_position = new_pos;
1598                                 int color = GREEN;
1599                                 int ret = test_track_group(drag_group, track, new_pos);
1600                                 if( ret < 0 )
1601                                         color = ORANGE;
1602                                 else if( !ret ) {
1603                                         if( new_pos != drop_position ) {
1604                                                 double pos = new_pos;
1605                                                 pos += mwindow->session->drag_position - cur_pos;
1606                                                 cx = mwindow->edl->get_position_cursorx(pos, pane->number);
1607                                                 ret = test_track_group(drag_group, track, new_pos);
1608                                         }
1609                                         color = ret > 0 ? YELLOW : shift_down() ? RED : BLUE;
1610                                 }
1611                                 track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1612                                 int dx = cx - mwindow->session->drag_origin_x;
1613                                 int dy = y - mwindow->session->drag_origin_y;
1614                                 draw_selected_edits(mwindow->edl, dx, dy, color, -1);
1615                         }
1616                         break;
1617 // Dragging an effect from the timeline
1618                 case DRAG_AEFFECT_COPY:
1619                 case DRAG_VEFFECT_COPY:
1620                         if( (mwindow->session->plugin_highlighted || mwindow->session->track_highlighted) &&
1621                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
1622                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1623                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
1624                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1625
1626                                 int64_t plugin_position = -1;
1627                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
1628                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
1629                                 Track *hi_track = mwindow->session->track_highlighted;
1630                                 Edit *hi_edit = mwindow->session->edit_highlighted;
1631 // Put it into a silence zone
1632                                 if( hi_plugin_set && hi_plugin_set->last &&
1633                                     hi_plugin_set->track == drag_plugin->track ) {
1634                                         plugin_dimensions((Plugin *)hi_plugin_set->last, x, y, w, h);
1635                                         plugin_position = drop_plugin_position(hi_plugin_set, drag_plugin);
1636                                         hi_track = drag_plugin->track;
1637                                 }
1638                                 else if( hi_track ) {
1639 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
1640                                         track_dimensions(hi_track, x, y, w, h);
1641                                         plugin_position = hi_edit ? hi_edit->startproject : 0;
1642                                 }
1643
1644                                 if( plugin_position < 0 ) break;
1645
1646 // Calculate length of plugin based on data type of track and units
1647                                 double zoom_scale = (double)mwindow->edl->session->sample_rate /
1648                                                 mwindow->edl->local_session->zoom_sample;
1649                                 x = Units::round(hi_track->from_units(plugin_position) * zoom_scale) -
1650                                                 mwindow->edl->local_session->view_start[pane->number];
1651                                 w = Units::round(hi_track->from_units(drag_plugin->length) * zoom_scale);
1652
1653                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1654                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1655                                         draw_box = 1;
1656                                 }
1657                         }
1658                         break;
1659
1660                 case DRAG_PLUGINKEY:
1661                         if(mwindow->session->plugin_highlighted &&
1662                            mwindow->session->current_operation == DRAG_PLUGINKEY)
1663                         {
1664 // Just highlight the plugin
1665                                 plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1666
1667                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1668                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1669                                 {
1670                                         draw_box = 1;
1671                                 }
1672                         }
1673                         break;
1674
1675         }
1676
1677         if( draw_box )
1678                 draw_highlight_rectangle(x, y, w, h);
1679
1680         draw_selected_edits(mwindow->edl, 0, 0, GREEN+BLUE, RED);
1681 }
1682
1683 // x does not reliably draw a really big rectangle
1684 void TrackCanvas::draw_selected(int x, int y, int w, int h)
1685 {
1686         int x1 = bmax(x, 0), x2 = bmin(x+w, get_w());
1687         if( x1 > x2 ) return;
1688         int y1 = bmax(y, 0), y2 = bmin(y+h, get_h());
1689         if( y1 > y2 ) return;
1690         if( x   >= 0 && x   < get_w() ) draw_line(x,y1,   x,y2);
1691         if( x+w >= 0 && x+w < get_w() ) draw_line(x+w,y1, x+w,y2);
1692         if( y   >= 0 && y   < get_h() ) draw_line(x1,y,   x2,y);
1693         if( y+h >= 0 && y+h < get_h() ) draw_line(x1,y+h, x2,y+h);
1694 }
1695
1696 void TrackCanvas::draw_selected_edits(EDL *edl, int dx, int dy, int color0, int color1)
1697 {
1698         for( Track *track=edl->tracks->first; track; track=track->next ) {
1699                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1700                         if( !edit->is_selected ) continue;
1701                         int64_t x, y, w, h;
1702                         edit_dimensions(edit, x, y, w, h);
1703                         x += dx;  y += dy;
1704                         if( !MWindowGUI::visible(x, x + w, 0, get_w()) ) continue;
1705                         if( !MWindowGUI::visible(y, y + h, 0, get_h()) ) continue;
1706                         set_opaque();
1707                         int inner = color1 < 0 ? color0 : !edit->group_id ? color0 :
1708                                 mwindow->get_group_color(edit->group_id);
1709                         set_color(inner);
1710                         draw_selected(x, y, w, h);
1711                         int outer = color1 < 0 ? color0 : !edit->group_id ? color1 : inner;
1712                         set_color(outer);
1713                         draw_selected(x-1, y-1, w+2, h+2);
1714                         draw_selected(x-2, y-2, w+1, h+1);
1715                 }
1716         }
1717 }
1718
1719 void TrackCanvas::draw_plugins()
1720 {
1721         char string[BCTEXTLEN];
1722         int current_on = 0;
1723         int current_show = 0;
1724         int current_preset = 0;
1725
1726         for(int i = 0; i < plugin_on_toggles.total; i++)
1727                 plugin_on_toggles.values[i]->in_use = 0;
1728         for(int i = 0; i < plugin_show_toggles.total; i++)
1729                 plugin_show_toggles.values[i]->in_use = 0;
1730         for(int i = 0; i < preset_edit_buttons.total; i++)
1731                 plugin_show_toggles.values[i]->in_use = 0;
1732
1733
1734         for(Track *track = mwindow->edl->tracks->first;
1735                 track;
1736                 track = track->next)
1737         {
1738                 if(track->expand_view)
1739                 {
1740                         for(int i = 0; i < track->plugin_set.total; i++)
1741                         {
1742                                 PluginSet *pluginset = track->plugin_set.values[i];
1743
1744                                 for(Plugin *plugin = (Plugin*)pluginset->first; plugin; plugin = (Plugin*)plugin->next)
1745                                 {
1746                                         int64_t total_x, y, total_w, h;
1747                                         plugin_dimensions(plugin, total_x, y, total_w, h);
1748
1749                                         if(MWindowGUI::visible(total_x, total_x + total_w, 0, get_w()) &&
1750                                                 MWindowGUI::visible(y, y + h, 0, get_h()) &&
1751                                                 plugin->plugin_type != PLUGIN_NONE)
1752                                         {
1753                                                 int x = total_x, w = total_w, left_margin = 5;
1754                                                 int right_margin = 5;
1755                                                 if(x < 0)
1756                                                 {
1757                                                         w -= -x;
1758                                                         x = 0;
1759                                                 }
1760                                                 if(w + x > get_w()) w -= (w + x) - get_w();
1761
1762                                                 draw_3segmenth(x,
1763                                                         y,
1764                                                         w,
1765                                                         total_x,
1766                                                         total_w,
1767                                                         mwindow->theme->get_image("plugin_bg_data"),
1768                                                         0);
1769                                                 set_color(mwindow->theme->title_color);
1770                                                 set_font(mwindow->theme->title_font);
1771                                                 plugin->calculate_title(string, 0);
1772
1773 // Truncate string to int64_test visible in background
1774                                                 int len = strlen(string), j;
1775                                                 for(j = len; j >= 0; j--)
1776                                                 {
1777                                                         if(left_margin + get_text_width(mwindow->theme->title_font, string) > w)
1778                                                         {
1779                                                                 string[j] = 0;
1780                                                         }
1781                                                         else
1782                                                                 break;
1783                                                 }
1784
1785 // Justify the text on the left boundary of the edit if it is visible.
1786 // Otherwise justify it on the left side of the screen.
1787                                                 int64_t text_x = total_x + left_margin;
1788                                                 int64_t text_w = get_text_width(mwindow->theme->title_font, string, strlen(string));
1789                                                 text_x = MAX(left_margin, text_x);
1790                                                 draw_text(text_x,
1791                                                         y + get_text_ascent(mwindow->theme->title_font) + 2,
1792                                                         string,
1793                                                         strlen(string),
1794                                                         0);
1795                                                 int64_t min_x = total_x + text_w;
1796
1797
1798 // Update plugin toggles
1799                                                 int toggle_x = total_x + total_w;
1800                                                 int toggle_y = y;
1801                                                 toggle_x = MIN(get_w() - right_margin, toggle_x);
1802
1803 // On toggle
1804                                                 toggle_x -= PluginOn::calculate_w(mwindow) + 10;
1805                                                 if(toggle_x > min_x)
1806                                                 {
1807                                                         if(current_on >= plugin_on_toggles.total)
1808                                                         {
1809                                                                 PluginOn *plugin_on = new PluginOn(mwindow, toggle_x, toggle_y, plugin);
1810                                                                 add_subwindow(plugin_on);
1811                                                                 plugin_on_toggles.append(plugin_on);
1812                                                         }
1813                                                         else
1814                                                         {
1815                                                                 plugin_on_toggles.values[current_on]->update(toggle_x, toggle_y, plugin);
1816                                                         }
1817                                                         current_on++;
1818                                                 }
1819
1820 // Toggles for standalone plugins only
1821                                                 if(plugin->plugin_type == PLUGIN_STANDALONE)
1822                                                 {
1823 // Show
1824                                                         toggle_x -= PluginShow::calculate_w(mwindow) + 10;
1825                                                         if(toggle_x > min_x)
1826                                                         {
1827                                                                 if(current_show >= plugin_show_toggles.total)
1828                                                                 {
1829                                                                         PluginShow *plugin_show = new PluginShow(mwindow, toggle_x, toggle_y, plugin);
1830                                                                         add_subwindow(plugin_show);
1831                                                                         plugin_show_toggles.append(plugin_show);
1832                                                                 }
1833                                                                 else
1834                                                                 {
1835                                                                         plugin_show_toggles.values[current_show]->update(toggle_x, toggle_y, plugin);
1836                                                                 }
1837                                                                 current_show++;
1838                                                         }
1839                                                         toggle_x -= PluginPresetEdit::calculate_w(mwindow) + 10;
1840                                                         if(toggle_x > min_x)
1841                                                         {
1842                                                                 if(current_preset >= preset_edit_buttons.total)
1843                                                                 {
1844                                                                         PluginPresetEdit *preset_edit = new PluginPresetEdit(mwindow, toggle_x, toggle_y, plugin);
1845                                                                         add_subwindow(preset_edit);
1846                                                                         preset_edit_buttons.append(preset_edit);
1847                                                                 }
1848                                                                 else
1849                                                                 {
1850                                                                         preset_edit_buttons.values[current_preset]->update(toggle_x, toggle_y, plugin);
1851                                                                 }
1852                                                                 current_preset++;
1853                                                         }
1854                                                 }
1855                                         }
1856                                 }
1857                         }
1858                 }
1859         }
1860
1861 // Remove unused toggles
1862
1863         while(current_preset < preset_edit_buttons.total)
1864         {
1865                 preset_edit_buttons.remove_object_number(current_preset);
1866         }
1867         while(current_show < plugin_show_toggles.total)
1868         {
1869                 plugin_show_toggles.remove_object_number(current_show);
1870         }
1871
1872         while(current_on < plugin_on_toggles.total)
1873         {
1874                 plugin_on_toggles.remove_object_number(current_on);
1875         }
1876 }
1877
1878 void TrackCanvas::refresh_plugintoggles()
1879 {
1880         for(int i = 0; i < plugin_on_toggles.total; i++)
1881         {
1882                 PluginOn *on = plugin_on_toggles.values[i];
1883                 on->reposition_window(on->get_x(), on->get_y());
1884         }
1885         for(int i = 0; i < plugin_show_toggles.total; i++)
1886         {
1887                 PluginShow *show = plugin_show_toggles.values[i];
1888                 show->reposition_window(show->get_x(), show->get_y());
1889         }
1890         for(int i = 0; i < preset_edit_buttons.total; i++)
1891         {
1892                 PluginPresetEdit *preset_edit = preset_edit_buttons.values[i];
1893                 preset_edit->reposition_window(preset_edit->get_x(), preset_edit->get_y());
1894         }
1895 }
1896
1897 void TrackCanvas::draw_hard_edges()
1898 {
1899         if( !mwindow->edl->session->auto_conf->hard_edges )
1900                 return;
1901         int64_t x, y, w, h;
1902
1903         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1904                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1905                         if( !edit->hard_left && !edit->hard_right ) continue;
1906                         edit_dimensions(edit, x, y, w, h);
1907                         set_color(GREEN);
1908                         set_opaque();
1909                         int y1 = y;
1910                         if( track->show_titles() )
1911                                 y1 += mwindow->theme->get_image("title_bg_data")->get_h();
1912                         if( track->show_assets() )
1913                                 y1 += resource_h();
1914                         if( y1 == y )
1915                                 y1 += mwindow->theme->title_h;
1916                         if( edit->hard_left ) {
1917                                 ArrayList<int> xpt, ypt;
1918                                 xpt.append(x);              ypt.append(y1);
1919                                 xpt.append(x+HANDLE_W);     ypt.append(y1);
1920                                 xpt.append(x);              ypt.append(y1-HANDLE_H);
1921                                 fill_polygon(&xpt, &ypt);
1922                         }
1923                         if( edit->hard_right ) {
1924                                 ArrayList<int> xpt, ypt;   int x1 = x+w-1;
1925                                 xpt.append(x1);            ypt.append(y1);
1926                                 xpt.append(x1-HANDLE_W);   ypt.append(y1);
1927                                 xpt.append(x1);            ypt.append(y1-HANDLE_H);
1928                                 fill_polygon(&xpt, &ypt);
1929                         }
1930                 }
1931         }
1932 }
1933
1934 void TrackCanvas::draw_inout_points()
1935 {
1936 }
1937
1938
1939 void TrackCanvas::draw_drag_handle()
1940 {
1941         if( mwindow->session->current_operation != DRAG_EDITHANDLE2 &&
1942             mwindow->session->current_operation != DRAG_PLUGINHANDLE2 ) return;
1943         int64_t pixel1 = Units::round(mwindow->session->drag_position *
1944                 mwindow->edl->session->sample_rate /
1945                 mwindow->edl->local_session->zoom_sample -
1946                 mwindow->edl->local_session->view_start[pane->number]);
1947         set_color(!snapped ? GREEN : (snapped=0, YELLOW));
1948         set_inverse();
1949         draw_line(pixel1, 0, pixel1, get_h());
1950         set_opaque();
1951
1952         if( mwindow->session->current_operation != DRAG_EDITHANDLE2 ) return;
1953         if( !mwindow->session->drag_edit ) return;
1954         int group_id = mwindow->session->drag_edit->group_id;
1955         if( !group_id ) return;
1956         int64_t dx, dy, dw, dh;
1957         edit_dimensions(mwindow->session->drag_edit, dx, dy, dw, dh);
1958         int drag_handle = mwindow->session->drag_handle;
1959         int64_t pixel0 = !drag_handle ? dx : dx + dw;
1960         int delta = pixel1 - pixel0;
1961         set_color(LTPURPLE);
1962         set_line_width(3);
1963
1964         for( Track *track=mwindow->edl->tracks->first; track; track=track->next ) {
1965                 Edit *left = 0, *right = 0;
1966                 double start = DBL_MAX, end = DBL_MIN;
1967                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1968                         if( edit->group_id != group_id ) continue;
1969                         double edit_start = edit->track->from_units(edit->startproject);
1970                         if( edit_start < start ) { start = edit_start;  left = edit; }
1971                         double edit_end = edit->track->from_units(edit->startproject+edit->length);
1972                         if( edit_end > end ) { end = edit_end;  right = edit; }
1973                 }
1974                 Edit *edit = !drag_handle ? left : right;
1975                 if( !edit ) continue;
1976                 Indexable *idxbl = edit->asset;
1977                 if( !idxbl ) idxbl = edit->nested_edl;
1978                 int can_drag = idxbl ? 1 : 0;
1979                 if( drag_handle ) {
1980                         int64_t source_len = !idxbl ? -1 :
1981                                 edit->track->data_type == TRACK_AUDIO ?
1982                                         idxbl->get_audio_samples() :
1983                                 edit->track->data_type == TRACK_VIDEO ?
1984                                         idxbl->get_video_frames() : -1;
1985                         if( edit->startsource + edit->length >= source_len )
1986                                 can_drag = 0;
1987                 }
1988                 else if( !edit->startsource )
1989                         can_drag = 0;
1990                 int64_t x, y, w, h;
1991                 edit_dimensions(edit, x, y, w, h);
1992                 if( y+h < 0 || y >= get_h() ) continue;
1993                 int edge_x = !drag_handle ? x : x + w;
1994                 int edge_y = y + h/2, k = 10;
1995                 if( edge_x >= 0 && edge_x < get_w() ) {
1996                         if( !can_drag ) {
1997                                 draw_line(edge_x-k,edge_y-k, edge_x+k,edge_y+k);
1998                                 draw_line(edge_x-k,edge_y+k, edge_x+k,edge_y-k);
1999                         }
2000                         else if( !drag_handle ) {
2001                                 draw_line(edge_x+k,edge_y-k, edge_x,edge_y);
2002                                 draw_line(edge_x+k,edge_y+k, edge_x,edge_y);
2003                         }
2004                         else {
2005                                 draw_line(edge_x,edge_y, edge_x-k,edge_y-k);
2006                                 draw_line(edge_x,edge_y, edge_x-k,edge_y+k);
2007                         }
2008                 }
2009                 edge_x += delta;
2010                 if( edge_x >= 0 && edge_x < get_w() ) {
2011                         draw_line(edge_x, y, edge_x, y+h);
2012                 }
2013         }
2014         set_line_width(1);
2015 }
2016
2017
2018 void TrackCanvas::draw_transitions()
2019 {
2020         int64_t x, y, w, h;
2021
2022         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
2023                 if( !track->show_transitions() ) continue;
2024
2025                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
2026                         if(!edit->transition) continue;
2027                         edit_dimensions(edit, x, y, w, h);
2028                         int strip_x = x, edit_y = y;
2029                         get_transition_coords(edit, x, y, w, h);
2030                         int strip_y = y - mwindow->theme->get_image("plugin_bg_data")->get_h();
2031                         if( track->show_assets() && track->show_titles() )
2032                                 edit_y += mwindow->theme->get_image("title_bg_data")->get_h();
2033                         if( strip_y < edit_y ) strip_y = edit_y;
2034
2035                         int strip_w = Units::round(edit->track->from_units(edit->transition->length) *
2036                                 mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample);
2037                         if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
2038                             MWindowGUI::visible(y, y + h, 0, get_h()) ) {
2039                                 PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
2040                                                 track->data_type);
2041                                 if( !server ) continue;
2042                                 VFrame *picon = server->get_picon();
2043                                 if( !picon ) continue;
2044                                 int picon_w = picon->get_w(), picon_h = picon->get_h();
2045                                 int track_h = edit->track->vertical_span(mwindow->theme);
2046                                 if( picon_h > track_h ) picon_h = track_h;
2047                                 draw_vframe(picon, x, y, w, h, 0, 0, picon_w, picon_h);
2048                         }
2049                         if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
2050                                 MWindowGUI::visible(strip_y, strip_y + h, 0, get_h())) {
2051                                 int x = strip_x, w = strip_w;
2052                                 if( x < 0 ) {  w -= -x;  x = 0; }
2053                                 if( w + x > get_w() ) w -= (w + x) - get_w();
2054
2055                                 draw_3segmenth( x, strip_y, w, strip_x, strip_w,
2056                                         mwindow->theme->get_image("plugin_bg_data"), 0);
2057                         }
2058                 }
2059         }
2060 }
2061
2062 void TrackCanvas::draw_loop_points()
2063 {
2064 //printf("TrackCanvas::draw_loop_points 1\n");
2065         if(mwindow->edl->local_session->loop_playback)
2066         {
2067 //printf("TrackCanvas::draw_loop_points 2\n");
2068                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
2069                         mwindow->edl->session->sample_rate /
2070                         mwindow->edl->local_session->zoom_sample -
2071                         mwindow->edl->local_session->view_start[pane->number]);
2072 //printf("TrackCanvas::draw_loop_points 3\n");
2073
2074                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
2075                 {
2076                         set_color(GREEN);
2077                         draw_line(x, 0, x, get_h());
2078                 }
2079 //printf("TrackCanvas::draw_loop_points 4\n");
2080
2081                 x = Units::round(mwindow->edl->local_session->loop_end *
2082                         mwindow->edl->session->sample_rate /
2083                         mwindow->edl->local_session->zoom_sample -
2084                         mwindow->edl->local_session->view_start[pane->number]);
2085 //printf("TrackCanvas::draw_loop_points 5\n");
2086
2087                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
2088                 {
2089                         set_color(GREEN);
2090                         draw_line(x, 0, x, get_h());
2091                 }
2092 //printf("TrackCanvas::draw_loop_points 6\n");
2093         }
2094 //printf("TrackCanvas::draw_loop_points 7\n");
2095 }
2096
2097 void TrackCanvas::draw_brender_range()
2098 {
2099         if( !mwindow->preferences->use_brender || !mwindow->brender_active ) return;
2100         if( mwindow->edl->session->brender_start >= mwindow->edl->session->brender_end ) return;
2101         if( mwindow->edl->session->brender_end > 0 )
2102         {
2103                 int64_t x1 = Units::round(mwindow->edl->session->brender_start *
2104                         mwindow->edl->session->sample_rate /
2105                         mwindow->edl->local_session->zoom_sample -
2106                         mwindow->edl->local_session->view_start[pane->number]);
2107                 if(MWindowGUI::visible(x1, x1 + 1, 0, get_w()))
2108                 {
2109                         set_color(RED);
2110                         draw_line(x1, 0, x1, get_h());
2111                 }
2112                 int64_t x2 = Units::round(mwindow->edl->session->brender_end *
2113                         mwindow->edl->session->sample_rate /
2114                         mwindow->edl->local_session->zoom_sample -
2115                         mwindow->edl->local_session->view_start[pane->number]);
2116
2117                 if(MWindowGUI::visible(x2, x2 + 1, 0, get_w()))
2118                 {
2119                         set_color(RED);
2120                         draw_line(x2, 0, x2, get_h());
2121                 }
2122         }
2123 }
2124
2125 // The operations which correspond to each automation type
2126 int TrackCanvas::auto_operations[AUTOMATION_TOTAL] =
2127 {
2128         DRAG_MUTE,
2129         DRAG_CAMERA_X,
2130         DRAG_CAMERA_Y,
2131         DRAG_CAMERA_Z,
2132         DRAG_PROJECTOR_X,
2133         DRAG_PROJECTOR_Y,
2134         DRAG_PROJECTOR_Z,
2135         DRAG_FADE,
2136         DRAG_PAN,
2137         DRAG_MODE,
2138         DRAG_MASK,
2139         DRAG_SPEED
2140 };
2141
2142 // The buttonpress operations, so nothing changes unless the mouse moves
2143 // a certain amount.  This allows the keyframe to be used to position the
2144 // insertion point without moving itself.
2145 static int pre_auto_operations[AUTOMATION_TOTAL] =
2146 {
2147         DRAG_MUTE,
2148         DRAG_CAMERA_X,
2149         DRAG_CAMERA_Y,
2150         DRAG_CAMERA_Z,
2151         DRAG_PROJECTOR_X,
2152         DRAG_PROJECTOR_Y,
2153         DRAG_PROJECTOR_Z,
2154         DRAG_FADE,
2155         DRAG_PAN_PRE,
2156         DRAG_MODE_PRE,
2157         DRAG_MASK_PRE,
2158         DRAG_SPEED
2159 };
2160
2161
2162 int TrackCanvas::do_keyframes(int cursor_x,
2163         int cursor_y,
2164         int draw,
2165         int buttonpress,
2166         int &new_cursor,
2167         int &update_cursor,
2168         int &rerender)
2169 {
2170 // Note: button 3 (right mouse button) is not eaten to allow
2171 // track context menu to appear
2172         int result = 0;
2173         EDLSession *session = mwindow->edl->session;
2174
2175
2176
2177         static BC_Pixmap *auto_pixmaps[AUTOMATION_TOTAL] =
2178         {
2179                 0, 0, 0, 0, 0, 0, 0, 0,
2180                 pankeyframe_pixmap,
2181                 modekeyframe_pixmap,
2182                 maskkeyframe_pixmap,
2183                 0,
2184         };
2185
2186
2187
2188         for(Track *track = mwindow->edl->tracks->first;
2189                 track && !result;
2190                 track = track->next) {
2191                 Auto *auto_keyframe = 0;
2192                 Automation *automation = track->automation;
2193
2194
2195 // Handle keyframes in reverse drawing order if a button press
2196                 int start = 0;
2197                 int end = AUTOMATION_TOTAL;
2198                 int step = 1;
2199                 if(buttonpress)
2200                 {
2201                         start = AUTOMATION_TOTAL - 1;
2202                         end = -1;
2203                         step = -1;
2204                 }
2205                 for(int i = start; i != end && !result; i += step)
2206                 {
2207 // Event not trapped and automation visible
2208                         Autos *autos = automation->autos[i];
2209                         if(!result && session->auto_conf->autos[i] && autos) {
2210                                 switch(i) {
2211                                 case AUTOMATION_MODE:
2212                                 case AUTOMATION_PAN:
2213                                 case AUTOMATION_MASK:
2214                                         result = do_autos(track, automation->autos[i],
2215                                                 cursor_x, cursor_y, draw,
2216                                                 buttonpress, auto_pixmaps[i],
2217                                                 auto_keyframe, rerender);
2218                                                 break;
2219
2220                                 default: {
2221                                         switch(autos->get_type()) {
2222                                         case Autos::AUTOMATION_TYPE_FLOAT: {
2223                                                 Automation automation(0, track);
2224                                                 int grouptype = automation.autogrouptype(i, track);
2225                                                 if( buttonpress && i == AUTOMATION_SPEED ) {
2226                                                         mwindow->speed_before();
2227                                                 }
2228
2229                                                 if(draw) // Do dropshadow
2230                                                         result = do_float_autos(track, autos,
2231                                                                 cursor_x, cursor_y, draw,
2232                                                                 buttonpress, 1, 1, MDGREY,
2233                                                                 auto_keyframe, grouptype);
2234                                                 result = do_float_autos(track, autos,
2235                                                         cursor_x, cursor_y, draw, buttonpress,
2236                                                         0, 0, GWindowGUI::auto_colors[i],
2237                                                         auto_keyframe, grouptype);
2238
2239                                                 if( !result && buttonpress && i == AUTOMATION_SPEED )
2240                                                         mwindow->speed_after(-1);
2241                                                 if( result && buttonpress ) {
2242                                                         int color = GWindowGUI::auto_colors[i];
2243                                                         mwindow->gui->zoombar->update_autozoom(grouptype, color);
2244                                                 }
2245                                                 break; }
2246
2247                                         case Autos::AUTOMATION_TYPE_INT: {
2248                                                 if(draw) // Do dropshadow
2249                                                         result = do_int_autos(track, autos,
2250                                                                 cursor_x, cursor_y, draw,
2251                                                                 buttonpress, 1, 1, MDGREY,
2252                                                                 auto_keyframe);
2253                                                 result = do_int_autos(track, autos,
2254                                                         cursor_x, cursor_y, draw, buttonpress,
2255                                                         0, 0, GWindowGUI::auto_colors[i],
2256                                                         auto_keyframe);
2257                                                 break; }
2258                                         }
2259                                         break; }
2260                                 }
2261
2262                                 if(result)
2263                                 {
2264                                         if(mwindow->session->current_operation == auto_operations[i])
2265                                                 rerender = 1;
2266
2267 // printf("TrackCanvas::do_keyframes %d %d %d\n",
2268 // __LINE__,
2269 // mwindow->session->current_operation,
2270 // auto_operations[i]);
2271                                         if(buttonpress)
2272                                         {
2273                                                 if (buttonpress == 2 && auto_keyframe )
2274                                                 {
2275                                                         double position = track->from_units(auto_keyframe->position);
2276                                                         mwindow->edl->local_session->set_selectionstart(position);
2277                                                         mwindow->edl->local_session->set_selectionend(position);
2278                                                 }
2279                                                 if (buttonpress != 3)
2280                                                 {
2281                                                         if(i == AUTOMATION_FADE || i == AUTOMATION_SPEED)
2282                                                                 fill_ganged_autos(get_double_click(), 0, track,
2283                                                                         (FloatAuto*)mwindow->session->drag_auto);
2284                                                         mwindow->session->current_operation = pre_auto_operations[i];
2285                                                         update_drag_caption();
2286                                                         rerender = 1;
2287                                                 }
2288                                                 else if( auto_keyframe )
2289                                                 {
2290                                                         gui->keyframe_menu->update(automation,
2291                                                                 autos,
2292                                                                 auto_keyframe);
2293                                                         gui->keyframe_menu->activate_menu();
2294                                                         rerender = 1; // the position changes
2295                                                 }
2296                                                 else if( autos )
2297                                                 {
2298                                                         gui->keyframe_hide->update(autos);
2299                                                         gui->keyframe_hide->activate_menu();
2300                                                         rerender = 1; // the position changes
2301                                                 }
2302                                                 if(buttonpress == 1 && ctrl_down() &&
2303                                                    AUTOMATION_TYPE_FLOAT == autos->get_type())
2304                                                         rerender = 1; // special case: curve mode changed
2305                                         }
2306                                 }
2307                         }
2308                 }
2309
2310                 if(!result && session->auto_conf->plugins) {
2311                         Plugin *plugin;
2312                         KeyFrame *keyframe;
2313                         result = do_plugin_autos(track, cursor_x, cursor_y,
2314                                 draw, buttonpress, plugin, keyframe);
2315                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY) {
2316                                 rerender = 1;
2317                         }
2318                         if(result && (buttonpress == 1)) {
2319                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2320                                 update_drag_caption();
2321                                 rerender = 1;
2322                         }
2323                         else if (result && (buttonpress == 3)) {
2324                                 gui->keyframe_menu->update(plugin, keyframe);
2325                                 gui->keyframe_menu->activate_menu();
2326                                 rerender = 1; // the position changes
2327                         }
2328                 }
2329         }
2330
2331 // Final pass to trap event
2332         for(int i = 0; i < AUTOMATION_TOTAL; i++) {
2333                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2334                         mwindow->session->current_operation == auto_operations[i]) {
2335                         result = 1;
2336                         break;
2337                 }
2338         }
2339
2340         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2341                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE) {
2342                 result = 1;
2343         }
2344
2345         update_cursor = 1;
2346         if(result) {
2347                 new_cursor = UPRIGHT_ARROW_CURSOR;
2348         }
2349
2350         return result;
2351 }
2352
2353 void TrackCanvas::draw_keyframe_reticle()
2354 {
2355         int keyframe_hairline = mwindow->preferences->keyframe_reticle;
2356         if( keyframe_hairline == HAIRLINE_NEVER ) return;
2357
2358         int current_op = mwindow->session->current_operation, dragging = 0;
2359         for( int i=0; !dragging && i<AUTOMATION_TOTAL; ++i )
2360                 if( current_op == auto_operations[i] ) dragging = 1;
2361         if( dragging && !mwindow->session->drag_auto ) dragging = 0;
2362
2363         int autoidx = dragging && keyframe_hairline != HAIRLINE_ALWAYS ?
2364                 mwindow->session->drag_auto->autos->autoidx : -1;
2365
2366         if( get_buttonpress() == LEFT_BUTTON && dragging &&
2367             keyframe_hairline == HAIRLINE_DRAGGING ) {
2368                 draw_hairline(mwindow->session->drag_auto, RED, 1);
2369                 return;
2370         }
2371
2372         if( keyframe_hairline == HAIRLINE_ALWAYS || ( get_buttonpress() == MIDDLE_BUTTON &&
2373             keyframe_hairline == HAIRLINE_DRAGGING && dragging ) ) {
2374                 int show = dragging || keyframe_hairline == HAIRLINE_ALWAYS ? 1 : 0;
2375                 for( Track *track = mwindow->edl->tracks->first; track; track=track->next ) {
2376                         Automation *automation = track->automation;
2377                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2378                                 if( !mwindow->edl->session->auto_conf->autos[i] ) continue;
2379                                 // automation visible
2380                                 Autos *autos = automation->autos[i];
2381                                 if( !autos ) continue;
2382                                 if( autoidx >= 0 && autos->autoidx != autoidx ) continue;
2383                                 for( Auto *auto_keyframe=autos->first; auto_keyframe;
2384                                      auto_keyframe = auto_keyframe->next ) {
2385                                         draw_hairline(auto_keyframe, BLUE, show);
2386                                 }
2387                         }
2388                 }
2389
2390                 if( dragging )
2391                         draw_hairline(mwindow->session->drag_auto, RED, 1);
2392         }
2393 }
2394
2395 void TrackCanvas::draw_auto(Auto *current,
2396         int x,
2397         int y,
2398         int center_pixel,
2399         int zoom_track)
2400 {
2401         int x1, y1, x2, y2;
2402
2403         x1 = x - HANDLE_W / 2;
2404         x2 = x + HANDLE_W / 2;
2405         y1 = center_pixel + y - HANDLE_W / 2;
2406         y2 = center_pixel + y + HANDLE_W / 2;
2407
2408         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2409         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2410
2411         draw_box(x1, y1, x2 - x1, y2 - y1);
2412 }
2413
2414
2415 // This draws lines for bezier in & out controls
2416 void TrackCanvas::draw_cropped_line(int x1,
2417         int y1,
2418         int x2,
2419         int y2,
2420         int min_y,
2421         int max_y)
2422 {
2423
2424
2425 // Don't care about x since it is clipped by the window.
2426 // Put y coords in ascending order
2427         if(y2 < y1) {
2428                 y2 ^= y1; y1 ^= y2; y2 ^= y1;
2429                 x2 ^= x1; x1 ^= x2; x2 ^= x1;
2430         }
2431
2432
2433
2434         double slope = (double)(x2 - x1) / (y2 - y1);
2435 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2436         if(y1 < min_y) {
2437                 x1 = (int)(x1 + (min_y - y1) * slope);
2438                 y1 = min_y;
2439         }
2440         else if(y1 >= max_y) {
2441                 x1 = (int)(x1 + (max_y - 1 - y1) * slope);
2442                 y1 = max_y - 1;
2443         }
2444
2445         if(y2 >= max_y) {
2446                 x2 = (int)(x2 + (max_y - 1 - y2) * slope);
2447                 y2 = max_y - 1;
2448         }
2449         else if(y2 < min_y) {
2450                 x2 = (int)(x2 + (min_y - y2) * slope);
2451                 y1 = min_y;
2452         }
2453
2454
2455 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2456         if( y1 >= min_y && y1 < max_y &&
2457             y2 >= min_y && y2 < max_y )
2458                 draw_line(x1, y1, x2, y2);
2459 }
2460
2461
2462 void TrackCanvas::draw_floatauto(FloatAuto *current,
2463         int x,
2464         int y,
2465         int in_x,
2466         int in_y,
2467         int out_x,
2468         int out_y,
2469         int center_pixel,
2470         int zoom_track,
2471         int color)
2472 {
2473         int x1 = x - HANDLE_W / 2; // Center
2474         int x2 = x + HANDLE_W / 2;
2475         int y1 = center_pixel + y - HANDLE_H / 2;
2476         int y2 = center_pixel + y + HANDLE_H / 2;
2477         int ymin = center_pixel - zoom_track / 2;
2478         int ymax = center_pixel + zoom_track / 2;
2479         CLAMP(y1, ymin, ymax);
2480         CLAMP(y2, ymin, ymax);
2481
2482         if( y2-1 > y1 ) {
2483                 if( current->curve_mode == FloatAuto::LINEAR ) {
2484                         draw_box(x1, y1, x2 - x1, y2 - y1);
2485                 }
2486                 else {
2487                         ArrayList<int> polygon_x;
2488                         ArrayList<int> polygon_y;
2489                         polygon_x.append((x1 + x2) / 2 + 1);
2490                         polygon_y.append(y1 + 1);
2491                         polygon_x.append(x2 + 1);
2492                         polygon_y.append((y1 + y2) / 2 + 1);
2493                         polygon_x.append((x1 + x2) / 2 + 1);
2494                         polygon_y.append(y2 + 1);
2495                         polygon_x.append(x1 + 1);
2496                         polygon_y.append((y1 + y2) / 2 + 1);
2497                         fill_polygon(&polygon_x, &polygon_y);
2498                 }
2499         }
2500
2501 // show bezier control points (only) if this
2502 // floatauto doesn't adjust it's tangents automatically
2503         if(current->curve_mode != FloatAuto::FREE &&
2504            current->curve_mode != FloatAuto::TFREE)
2505                 return;
2506
2507         if(in_x != x)
2508                 draw_floatauto_ctrlpoint(x, y, in_x, in_y, center_pixel, zoom_track, color);
2509         if(out_x != x)
2510                 draw_floatauto_ctrlpoint(x, y, out_x, out_y, center_pixel, zoom_track, color);
2511 }
2512
2513 inline int quantize(float f) { return (int)floor(f + 0.5); }
2514
2515 inline void TrackCanvas::draw_floatauto_ctrlpoint(
2516         int x, int y, int cp_x, int cp_y, int center_pixel,
2517         int zoom_track, int color)
2518 // draw the tangent and a handle for given b├ęzier ctrl point
2519 {
2520         bool handle_visible = (abs(cp_y) <= zoom_track / 2);
2521
2522         float slope = (float)(cp_y - y)/(cp_x - x);
2523         CLAMP(cp_y, -zoom_track / 2, zoom_track / 2);
2524         if(slope != 0)
2525                 cp_x = x + quantize((cp_y - y) / slope);
2526
2527         y    += center_pixel;
2528         cp_y += center_pixel;
2529
2530         // drawing the tangent as a dashed line...
2531         int const dash = HANDLE_W;
2532         int const gap  = HANDLE_W / 2;
2533         float sx = 3 * (cp_x - x) / 4.;
2534         float ex = 0;
2535
2536         // q is the x displacement for a unit line of slope
2537         float q = (sx > 0 ? 1 : -1) / sqrt(1 + slope * slope);
2538
2539         float dist = 1/q * sx;
2540         if( dist > dash )
2541                 ex = sx - q * dash;
2542
2543         set_color(color);
2544         do {
2545                 float sy = slope * sx, ey = slope * ex;
2546                 draw_line(quantize(sx + x), quantize(sy + y), quantize(ex + x), quantize(ey + y));
2547                 sx = ex - q * gap;
2548                 ex = sx - q * dash;
2549         } while(q*ex > 0);
2550
2551         if(handle_visible)
2552         {
2553                 int r = HANDLE_W / 2;
2554                 int cp_x1 = cp_x - r;
2555                 int cp_y1 = cp_y - r;
2556                 set_color(BLACK);
2557                 draw_disc(cp_x1, cp_y1, 2 * r, 2 * r);
2558                 set_color(color);
2559                 draw_circle(cp_x1, cp_y1, 2 * r, 2 * r);
2560         }
2561 }
2562
2563
2564 int TrackCanvas::test_auto(Auto *current,
2565         int x, int y, int center_pixel, int zoom_track,
2566         int cursor_x, int cursor_y, int buttonpress)
2567 {
2568         int x1, y1, x2, y2;
2569         int result = 0;
2570
2571         x1 = x - HANDLE_W / 2;
2572         x2 = x + HANDLE_W / 2;
2573         y1 = center_pixel + y - HANDLE_H / 2;
2574         y2 = center_pixel + y + HANDLE_H / 2;
2575         int ymin = center_pixel - zoom_track / 2;
2576         int ymax = center_pixel + zoom_track / 2;
2577         CLAMP(y1, ymin, ymax);
2578         CLAMP(y2, ymin, ymax);
2579
2580         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2581         {
2582                 if(buttonpress && buttonpress != 3)
2583                 {
2584                         mwindow->session->drag_auto = current;
2585                         mwindow->session->drag_start_percentage = (float)((IntAuto*)current)->value;
2586                         // Toggle Autos don't respond to vertical zoom, they always show up
2587                         // with "on" == 100% == line on top
2588                         mwindow->session->drag_start_position = current->position;
2589                         mwindow->session->drag_origin_x = cursor_x;
2590                         mwindow->session->drag_origin_y = cursor_y;
2591                 }
2592                 result = 1;
2593         }
2594
2595         if(buttonpress && buttonpress != 3 && result)
2596         {
2597 //printf("TrackCanvas::test_auto %d\n", __LINE__);
2598                 mwindow->undo->update_undo_before();
2599         }
2600
2601         return result;
2602 }
2603
2604 // some Helpers for test_floatauto(..)
2605 // and for dragging the tangents/ctrl points
2606 inline float test_curve_line( int x0, int y0, int ctrl_x, int ctrl_y,
2607         float cursor_x, float cursor_y)
2608 {
2609 // Control point switched off?
2610         if( x0 == ctrl_x ) return 0.0;
2611         double x1 = ctrl_x-x0, y1 = ctrl_y-y0;
2612         double x = cursor_x-x0, y = cursor_y-y0;
2613 // wrong side of ctrl handle
2614         if( x*x1 < 0 ) return 0.0;
2615 // outside handle radius
2616         if( (x*x + y*y) > (x1*x1 + y1*y1) ) return 0;
2617         double xx1 = x1*x1, yy1 = y1*y1;
2618         double xx = x*x, yy = y*y;
2619 // distance squared from cursor to cursor projected to line from ctrl to handle
2620 //   along a line perpendicular to line from ctrl to handle (closest approach)
2621 // (x**2*y1**2 - 2*x*x1*y*y1 + x1**2*y**2)/(x1**2 + y1**2)
2622         double dist2 = (xx*yy1 - 2*x*x1*y*y1 + xx1*yy)/(xx1 + yy1);
2623         return dist2 < (HANDLE_W*HANDLE_W)/4. ? x/x1 : 0.;
2624 }
2625
2626
2627 inline
2628