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