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