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