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