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