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