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