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