reload plugin btn in prefs, speed gang fader tweaks
[goodguy/history.git] / cinelerra-5.1 / cinelerra / trackcanvas.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "apatchgui.inc"
23 #include "asset.h"
24 #include "autoconf.h"
25 #include "automation.h"
26 #include "bcsignals.h"
27 #include "bctimer.h"
28 #include "clip.h"
29 #include "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                                                         current_vedit++;
1197                                                 }
1198                                         }
1199                                         if( paste_position >= 0 ) {
1200                                                 position = paste_position;
1201                                                 current_vedit++;
1202                                                 //draw_box = 1;
1203                                         }
1204                                         else
1205                                                 w = -1;
1206                                 }
1207
1208                                 if(w >= 0) {
1209 // Get the x coordinate
1210                                         x = Units::to_int64(position *
1211                                                 mwindow->edl->session->sample_rate /
1212                                                 mwindow->edl->local_session->zoom_sample) -
1213                                                 mwindow->edl->local_session->view_start[pane->number];
1214                                         int y = dest->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1215                                         int h = dest->vertical_span(mwindow->theme);
1216
1217 //printf("TrackCanvas::draw_paste_destination 2 %d %d %d %d\n", x, y, w, h);
1218                                         if(x < -BC_INFINITY) {
1219                                                 w -= -BC_INFINITY - x;
1220                                                 x += -BC_INFINITY - x;
1221                                         }
1222                                         w = MIN(65535, w);
1223 // if(pane->number == TOP_RIGHT_PANE)
1224 // printf("TrackCanvas::draw_paste_destination %d %d %d %d %d\n",
1225 // __LINE__, x, y, w, h);
1226                                         if (insertion)
1227                                                 draw_highlight_insertion(x, y, w, h);
1228                                         else
1229                                                 draw_highlight_rectangle(x, y, w, h);
1230                                 }
1231                         }
1232                 }
1233         }
1234 }
1235
1236 void TrackCanvas::plugin_dimensions(Plugin *plugin, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1237 {
1238         x = Units::round(plugin->track->from_units(plugin->startproject) *
1239                 mwindow->edl->session->sample_rate /
1240                 mwindow->edl->local_session->zoom_sample -
1241                 mwindow->edl->local_session->view_start[pane->number]);
1242         w = Units::round(plugin->track->from_units(plugin->length) *
1243                 mwindow->edl->session->sample_rate /
1244                 mwindow->edl->local_session->zoom_sample);
1245         y = plugin->track->y_pixel
1246                 - mwindow->edl->local_session->track_start[pane->number];
1247         if( plugin->track->show_titles() )
1248                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1249         if( plugin->track->show_assets() )
1250                 y += resource_h();
1251         y += plugin->plugin_set->get_number() *
1252                         mwindow->theme->get_image("plugin_bg_data")->get_h();
1253         h = mwindow->theme->get_image("plugin_bg_data")->get_h();
1254 }
1255
1256 int TrackCanvas::resource_h()
1257 {
1258         return mwindow->edl->local_session->zoom_track;
1259 }
1260
1261 void TrackCanvas::draw_highlight_rectangle(int x, int y, int w, int h)
1262 {
1263
1264 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1265 // just draw arrows, so user has indication that something is there
1266 // FIXME: get better colors
1267
1268         if (x + w <= 0)
1269         {
1270                 draw_triangle_left(0, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1271                 return;
1272         } else
1273         if (x >= get_w())
1274         {
1275                 draw_triangle_right(get_w() - h * 2/3, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1276                 return;
1277         }
1278
1279 // Fix bug in heroines & cvs version as of 22.8.2005:
1280 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1281         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1282         if(x < -10)
1283         {
1284                 w += x - -10;
1285                 x = -10;
1286         }
1287
1288         if(y < -10)
1289         {
1290                 h += y - -10;
1291                 y = -10;
1292         }
1293
1294         w = MIN(w, get_w() + 20);
1295         h = MIN(h, get_h() + 20);
1296         if(w > 0 && h > 0)
1297         {
1298                 set_color(mwindow->preferences->highlight_inverse);
1299                 set_inverse();
1300                 //draw_rectangle(x, y, w, h);
1301                 draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1302                 draw_rectangle(x + 2, y + 2, w - 4, h - 4);
1303                 set_opaque();
1304                 draw_rectangle(x, y, w, h);
1305         }
1306 //if(pane->number == TOP_RIGHT_PANE)
1307 //printf("TrackCanvas::draw_highlight_rectangle %d %d %d %d %d\n", __LINE__, x, y, w, h);
1308 }
1309
1310 void TrackCanvas::draw_highlight_insertion(int x, int y, int w, int h)
1311 {
1312
1313 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1314 // just draw arrows, so user has indication that something is there
1315 // FIXME: get better colors
1316
1317
1318
1319         int h1 = h / 8;
1320         int h2 = h / 4;
1321
1322         set_inverse();
1323
1324 /* these don't look so good
1325
1326         draw_line(x, y, x, y+h);
1327         draw_line(x - h2 * 2, y + h1*2,   x - h2, y+h1*2);
1328         draw_line(x - h2 * 2, y + h1*2+1, x - h2, y+h1*2+1);
1329         draw_line(x - h2 * 2, y + h1*6,   x - h2, y+h1*6);
1330         draw_line(x - h2 * 2, y + h1*6+1, x - h2, y+h1*6+1);
1331 */
1332         draw_triangle_right(x - h2, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1333         draw_triangle_right(x - h2, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1334
1335 /*      draw_line(x + h2 * 2, y + h1*2,   x + h2, y+h1*2);
1336         draw_line(x + h2 * 2, y + h1*2+1, x + h2, y+h1*2+1);
1337         draw_line(x + h2 * 2, y + h1*6,   x + h2, y+h1*6);
1338         draw_line(x - h2 * 2, y + h1*6+1, x + h2, y+h1*6+1);
1339 */
1340         draw_triangle_left(x, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1341         draw_triangle_left(x, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1342
1343 // draw the box centred around x
1344         x -= w / 2;
1345 // Fix bug in heroines & cvs version as of 22.8.2005:
1346 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1347         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1348         if(x < -10)
1349         {
1350                 w += x - -10;
1351                 x = -10;
1352         }
1353         if(y < -10)
1354         {
1355                 h += y - -10;
1356                 y = -10;
1357         }
1358         w = MIN(w, get_w() + 20);
1359         h = MIN(h, get_h() + 20);
1360         set_color(mwindow->preferences->highlight_inverse);
1361         set_inverse();
1362         draw_rectangle(x, y, w, h);
1363         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1364         set_opaque();
1365 //printf("TrackCanvas::draw_highlight_insertion %d %d %d %d\n", x, y, w, h);
1366 }
1367
1368 void TrackCanvas::draw_playback_cursor()
1369 {
1370 // Called before playback_cursor exists
1371 //      if(mwindow->playback_cursor && mwindow->playback_cursor->visible)
1372 //      {
1373 //              mwindow->playback_cursor->visible = 0;
1374 //              mwindow->playback_cursor->draw();
1375 //      }
1376 }
1377
1378 void TrackCanvas::get_handle_coords(Edit *edit, int64_t &x, int64_t &y, int64_t &w, int64_t &h, int side)
1379 {
1380         int handle_w = mwindow->theme->edithandlein_data[0]->get_w();
1381         int handle_h = mwindow->theme->edithandlein_data[0]->get_h();
1382
1383         edit_dimensions(edit, x, y, w, h);
1384
1385         if( edit->track->show_titles() )
1386                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1387
1388         if(side == EDIT_OUT)
1389                 x += w - handle_w;
1390
1391         h = handle_h;
1392         w = handle_w;
1393 }
1394
1395 void TrackCanvas::get_transition_coords(Edit *edit,
1396                 int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1397 {
1398         int transition_w = 30, transition_h = 30;
1399         int has_titles = edit->track->show_titles();
1400         int has_assets = edit->track->show_assets();
1401         double title_bg_h = mwindow->theme->get_image("title_bg_data")->get_h();
1402         double asset_h = resource_h();
1403         double ys = has_assets ? asset_h : has_titles ? title_bg_h : 0;
1404         double dy = has_titles ?
1405                 ( has_assets ? title_bg_h + asset_h/2 : title_bg_h/2 ) :
1406                 ( has_assets ? asset_h/2 : 0) ;
1407         double title_h = mwindow->theme->title_h;
1408         if( dy < title_h / 2 ) { ys = title_h;  dy = ys / 2; }
1409         y += dy;
1410
1411         x -= transition_w / 2;
1412         y -= transition_h / 2;
1413         w = transition_w;
1414         h = transition_h;
1415 }
1416
1417 void TrackCanvas::draw_highlighting()
1418 {
1419         int64_t x, y, w, h;
1420         int draw_box = 0;
1421
1422         switch(mwindow->session->current_operation)
1423         {
1424                 case DRAG_ATRANSITION:
1425                 case DRAG_VTRANSITION:
1426 //printf("TrackCanvas::draw_highlighting 1 %p %p\n",
1427 //      mwindow->session->track_highlighted, mwindow->session->edit_highlighted);
1428                         if(mwindow->session->edit_highlighted) {
1429                                 if((mwindow->session->current_operation == DRAG_ATRANSITION &&
1430                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1431                                         (mwindow->session->current_operation == DRAG_VTRANSITION &&
1432                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) {
1433                                         edit_dimensions(mwindow->session->edit_highlighted,
1434                                                 x, y, w, h);
1435                                         if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1436                                                 MWindowGUI::visible(y, y + h, 0, get_h())) {
1437                                                 draw_box = 1;
1438                                                 get_transition_coords(mwindow->session->edit_highlighted,
1439                                                         x, y, w, h);
1440                                         }
1441                                 }
1442                         }
1443                         break;
1444
1445 // Dragging a new effect from the Resource window
1446                 case DRAG_AEFFECT:
1447                 case DRAG_VEFFECT:
1448                         if(mwindow->session->track_highlighted &&
1449                                 ((mwindow->session->current_operation == DRAG_AEFFECT &&
1450                                   mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1451                                  (mwindow->session->current_operation == DRAG_VEFFECT &&
1452                                   mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1453 // Put it before another plugin
1454                                 if(mwindow->session->plugin_highlighted) {
1455                                         plugin_dimensions(mwindow->session->plugin_highlighted,
1456                                                 x, y, w, h);
1457 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1458                                 }
1459 // Put it after a plugin set
1460                                 else if( mwindow->session->pluginset_highlighted &&
1461                                          mwindow->session->pluginset_highlighted->last ) {
1462                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last,
1463                                                 x, y, w, h);
1464 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1465                                         int64_t track_x, track_y, track_w, track_h;
1466                                         track_dimensions(mwindow->session->track_highlighted,
1467                                                 track_x, track_y, track_w, track_h);
1468
1469                                         x += w;
1470                                         w = Units::round(
1471                                                         mwindow->session->track_highlighted->get_length() *
1472                                                         mwindow->edl->session->sample_rate /
1473                                                         mwindow->edl->local_session->zoom_sample -
1474                                                         mwindow->edl->local_session->view_start[pane->number]) -
1475                                                 x;
1476 //printf("TrackCanvas::draw_highlighting 2 %d\n", w);
1477                                         if(w <= 0) w = track_w;
1478                                 }
1479                                 else {
1480                                         track_dimensions(mwindow->session->track_highlighted,
1481                                                 x, y, w, h);
1482
1483 //printf("TrackCanvas::draw_highlighting 1 %d %d %d %d\n", x, y, w, h);
1484 // Put it in a new plugin set determined by the selected range
1485                                         if(mwindow->edl->local_session->get_selectionend() >
1486                                                 mwindow->edl->local_session->get_selectionstart()) {
1487                                                 x = Units::to_int64(mwindow->edl->local_session->get_selectionstart() *
1488                                                         mwindow->edl->session->sample_rate /
1489                                                         mwindow->edl->local_session->zoom_sample -
1490                                                         mwindow->edl->local_session->view_start[pane->number]);
1491                                                 w = Units::to_int64((mwindow->edl->local_session->get_selectionend() -
1492                                                         mwindow->edl->local_session->get_selectionstart()) *
1493                                                         mwindow->edl->session->sample_rate /
1494                                                         mwindow->edl->local_session->zoom_sample);
1495                                         }
1496 // Put it in a new plugin set determined by an edit boundary
1497                                         else if(mwindow->session->edit_highlighted) {
1498                                                 int64_t temp_y, temp_h;
1499                                                 edit_dimensions(mwindow->session->edit_highlighted,
1500                                                         x, temp_y, w, temp_h);
1501                                         }
1502 // Put it at the beginning of the track in a new plugin set
1503                                 }
1504
1505                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1506                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1507 //printf("TrackCanvas::draw_highlighting 1\n");
1508                                         draw_box = 1;
1509                                 }
1510                         }
1511                         break;
1512
1513                 case DRAG_ASSET:
1514                         if(mwindow->session->track_highlighted) {
1515 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1516
1517 //                              if(MWindowGUI::visible(y, y + h, 0, get_h()))
1518 //                              {
1519                                         draw_paste_destination();
1520 //                              }
1521                         }
1522                         break;
1523
1524                 case DRAG_EDIT:
1525                         if(mwindow->session->track_highlighted) {
1526 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1527 //
1528 //                              if(MWindowGUI::visible(y, y + h, 0, get_h())) {
1529                                         draw_paste_destination();
1530 //                              }
1531                         }
1532                         break;
1533
1534 // Dragging an effect from the timeline
1535                 case DRAG_AEFFECT_COPY:
1536                 case DRAG_VEFFECT_COPY:
1537                         if( (mwindow->session->plugin_highlighted || mwindow->session->track_highlighted) &&
1538                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
1539                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1540                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
1541                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1542
1543                                 int64_t plugin_position = -1;
1544                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
1545                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
1546                                 Track *hi_track = mwindow->session->track_highlighted;
1547                                 Edit *hi_edit = mwindow->session->edit_highlighted;
1548 // Put it into a silence zone
1549                                 if( hi_plugin_set && hi_plugin_set->last &&
1550                                     hi_plugin_set->track == drag_plugin->track ) {
1551                                         plugin_dimensions((Plugin *)hi_plugin_set->last, x, y, w, h);
1552                                         plugin_position = drop_plugin_position(hi_plugin_set, drag_plugin);
1553                                         hi_track = drag_plugin->track;
1554                                 }
1555                                 else if( hi_track ) {
1556 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
1557                                         track_dimensions(hi_track, x, y, w, h);
1558                                         plugin_position = hi_edit ? hi_edit->startproject : 0;
1559                                 }
1560
1561                                 if( plugin_position < 0 ) break;
1562
1563 // Calculate length of plugin based on data type of track and units
1564                                 double zoom_scale = (double)mwindow->edl->session->sample_rate /
1565                                                 mwindow->edl->local_session->zoom_sample;
1566                                 x = Units::round(hi_track->from_units(plugin_position) * zoom_scale) -
1567                                                 mwindow->edl->local_session->view_start[pane->number];
1568                                 w = Units::round(hi_track->from_units(drag_plugin->length) * zoom_scale);
1569
1570                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1571                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1572                                         draw_box = 1;
1573                                 }
1574                         }
1575                         break;
1576
1577                 case DRAG_PLUGINKEY:
1578                         if(mwindow->session->plugin_highlighted &&
1579                            mwindow->session->current_operation == DRAG_PLUGINKEY)
1580                         {
1581 // Just highlight the plugin
1582                                 plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1583
1584                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1585                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1586                                 {
1587                                         draw_box = 1;
1588                                 }
1589                         }
1590                         break;
1591
1592         }
1593
1594
1595         if(draw_box)
1596         {
1597                 draw_highlight_rectangle(x, y, w, h);
1598         }
1599 }
1600
1601 void TrackCanvas::draw_plugins()
1602 {
1603         char string[BCTEXTLEN];
1604         int current_on = 0;
1605         int current_show = 0;
1606         int current_preset = 0;
1607
1608         for(int i = 0; i < plugin_on_toggles.total; i++)
1609                 plugin_on_toggles.values[i]->in_use = 0;
1610         for(int i = 0; i < plugin_show_toggles.total; i++)
1611                 plugin_show_toggles.values[i]->in_use = 0;
1612         for(int i = 0; i < preset_edit_buttons.total; i++)
1613                 plugin_show_toggles.values[i]->in_use = 0;
1614
1615
1616         for(Track *track = mwindow->edl->tracks->first;
1617                 track;
1618                 track = track->next)
1619         {
1620                 if(track->expand_view)
1621                 {
1622                         for(int i = 0; i < track->plugin_set.total; i++)
1623                         {
1624                                 PluginSet *pluginset = track->plugin_set.values[i];
1625
1626                                 for(Plugin *plugin = (Plugin*)pluginset->first; plugin; plugin = (Plugin*)plugin->next)
1627                                 {
1628                                         int64_t total_x, y, total_w, h;
1629                                         plugin_dimensions(plugin, total_x, y, total_w, h);
1630
1631                                         if(MWindowGUI::visible(total_x, total_x + total_w, 0, get_w()) &&
1632                                                 MWindowGUI::visible(y, y + h, 0, get_h()) &&
1633                                                 plugin->plugin_type != PLUGIN_NONE)
1634                                         {
1635                                                 int x = total_x, w = total_w, left_margin = 5;
1636                                                 int right_margin = 5;
1637                                                 if(x < 0)
1638                                                 {
1639                                                         w -= -x;
1640                                                         x = 0;
1641                                                 }
1642                                                 if(w + x > get_w()) w -= (w + x) - get_w();
1643
1644                                                 draw_3segmenth(x,
1645                                                         y,
1646                                                         w,
1647                                                         total_x,
1648                                                         total_w,
1649                                                         mwindow->theme->get_image("plugin_bg_data"),
1650                                                         0);
1651                                                 set_color(mwindow->theme->title_color);
1652                                                 set_font(mwindow->theme->title_font);
1653                                                 plugin->calculate_title(string, 0);
1654
1655 // Truncate string to int64_test visible in background
1656                                                 int len = strlen(string), j;
1657                                                 for(j = len; j >= 0; j--)
1658                                                 {
1659                                                         if(left_margin + get_text_width(mwindow->theme->title_font, string) > w)
1660                                                         {
1661                                                                 string[j] = 0;
1662                                                         }
1663                                                         else
1664                                                                 break;
1665                                                 }
1666
1667 // Justify the text on the left boundary of the edit if it is visible.
1668 // Otherwise justify it on the left side of the screen.
1669                                                 int64_t text_x = total_x + left_margin;
1670                                                 int64_t text_w = get_text_width(mwindow->theme->title_font, string, strlen(string));
1671                                                 text_x = MAX(left_margin, text_x);
1672                                                 draw_text(text_x,
1673                                                         y + get_text_ascent(mwindow->theme->title_font) + 2,
1674                                                         string,
1675                                                         strlen(string),
1676                                                         0);
1677                                                 int64_t min_x = total_x + text_w;
1678
1679
1680 // Update plugin toggles
1681                                                 int toggle_x = total_x + total_w;
1682                                                 int toggle_y = y;
1683                                                 toggle_x = MIN(get_w() - right_margin, toggle_x);
1684
1685 // On toggle
1686                                                 toggle_x -= PluginOn::calculate_w(mwindow) + 10;
1687                                                 if(toggle_x > min_x)
1688                                                 {
1689                                                         if(current_on >= plugin_on_toggles.total)
1690                                                         {
1691                                                                 PluginOn *plugin_on = new PluginOn(mwindow, toggle_x, toggle_y, plugin);
1692                                                                 add_subwindow(plugin_on);
1693                                                                 plugin_on_toggles.append(plugin_on);
1694                                                         }
1695                                                         else
1696                                                         {
1697                                                                 plugin_on_toggles.values[current_on]->update(toggle_x, toggle_y, plugin);
1698                                                         }
1699                                                         current_on++;
1700                                                 }
1701
1702 // Toggles for standalone plugins only
1703                                                 if(plugin->plugin_type == PLUGIN_STANDALONE)
1704                                                 {
1705 // Show
1706                                                         toggle_x -= PluginShow::calculate_w(mwindow) + 10;
1707                                                         if(toggle_x > min_x)
1708                                                         {
1709                                                                 if(current_show >= plugin_show_toggles.total)
1710                                                                 {
1711                                                                         PluginShow *plugin_show = new PluginShow(mwindow, toggle_x, toggle_y, plugin);
1712                                                                         add_subwindow(plugin_show);
1713                                                                         plugin_show_toggles.append(plugin_show);
1714                                                                 }
1715                                                                 else
1716                                                                 {
1717                                                                         plugin_show_toggles.values[current_show]->update(toggle_x, toggle_y, plugin);
1718                                                                 }
1719                                                                 current_show++;
1720                                                         }
1721                                                         toggle_x -= PluginPresetEdit::calculate_w(mwindow) + 10;
1722                                                         if(toggle_x > min_x)
1723                                                         {
1724                                                                 if(current_preset >= preset_edit_buttons.total)
1725                                                                 {
1726                                                                         PluginPresetEdit *preset_edit = new PluginPresetEdit(mwindow, toggle_x, toggle_y, plugin);
1727                                                                         add_subwindow(preset_edit);
1728                                                                         preset_edit_buttons.append(preset_edit);
1729                                                                 }
1730                                                                 else
1731                                                                 {
1732                                                                         preset_edit_buttons.values[current_preset]->update(toggle_x, toggle_y, plugin);
1733                                                                 }
1734                                                                 current_preset++;
1735                                                         }
1736                                                 }
1737                                         }
1738                                 }
1739                         }
1740                 }
1741         }
1742
1743 // Remove unused toggles
1744
1745         while(current_preset < preset_edit_buttons.total)
1746         {
1747                 preset_edit_buttons.remove_object_number(current_preset);
1748         }
1749         while(current_show < plugin_show_toggles.total)
1750         {
1751                 plugin_show_toggles.remove_object_number(current_show);
1752         }
1753
1754         while(current_on < plugin_on_toggles.total)
1755         {
1756                 plugin_on_toggles.remove_object_number(current_on);
1757         }
1758 }
1759
1760 void TrackCanvas::refresh_plugintoggles()
1761 {
1762         for(int i = 0; i < plugin_on_toggles.total; i++)
1763         {
1764                 PluginOn *on = plugin_on_toggles.values[i];
1765                 on->reposition_window(on->get_x(), on->get_y());
1766         }
1767         for(int i = 0; i < plugin_show_toggles.total; i++)
1768         {
1769                 PluginShow *show = plugin_show_toggles.values[i];
1770                 show->reposition_window(show->get_x(), show->get_y());
1771         }
1772         for(int i = 0; i < preset_edit_buttons.total; i++)
1773         {
1774                 PluginPresetEdit *preset_edit = preset_edit_buttons.values[i];
1775                 preset_edit->reposition_window(preset_edit->get_x(), preset_edit->get_y());
1776         }
1777 }
1778
1779 void TrackCanvas::draw_hard_edges()
1780 {
1781         int64_t x, y, w, h;
1782
1783         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1784                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1785                         if( !edit->hard_left && !edit->hard_right ) continue;
1786                         edit_dimensions(edit, x, y, w, h);
1787                         set_color(GREEN);
1788                         set_opaque();
1789                         int y1 = y;
1790                         if( track->show_titles() )
1791                                 y1 += mwindow->theme->get_image("title_bg_data")->get_h();
1792                         if( track->show_assets() )
1793                                 y1 += resource_h();
1794                         if( y1 == y )
1795                                 y1 += mwindow->theme->title_h;
1796                         if( edit->hard_left ) {
1797                                 ArrayList<int> xpt, ypt;
1798                                 xpt.append(x);              ypt.append(y1);
1799                                 xpt.append(x+HANDLE_W);     ypt.append(y1);
1800                                 xpt.append(x);              ypt.append(y1-HANDLE_H);
1801                                 fill_polygon(&xpt, &ypt);
1802                         }
1803                         if( edit->hard_right ) {
1804                                 ArrayList<int> xpt, ypt;   int x1 = x+w-1;
1805                                 xpt.append(x1);            ypt.append(y1);
1806                                 xpt.append(x1-HANDLE_W);   ypt.append(y1);
1807                                 xpt.append(x1);            ypt.append(y1-HANDLE_H);
1808                                 fill_polygon(&xpt, &ypt);
1809                         }
1810                 }
1811         }
1812 }
1813
1814 void TrackCanvas::draw_inout_points()
1815 {
1816 }
1817
1818
1819 void TrackCanvas::draw_drag_handle()
1820 {
1821         if(mwindow->session->current_operation == DRAG_EDITHANDLE2 ||
1822                 mwindow->session->current_operation == DRAG_PLUGINHANDLE2)
1823         {
1824 //printf("TrackCanvas::draw_drag_handle 1 %ld %ld\n", mwindow->session->drag_sample, mwindow->edl->local_session->view_start);
1825                 int64_t pixel1 = Units::round(mwindow->session->drag_position *
1826                         mwindow->edl->session->sample_rate /
1827                         mwindow->edl->local_session->zoom_sample -
1828                         mwindow->edl->local_session->view_start[pane->number]);
1829 //printf("TrackCanvas::draw_drag_handle 2 %d %jd\n", pane->number, pixel1);
1830                 set_color(!snapped ? GREEN : (snapped=0, YELLOW));
1831                 set_inverse();
1832 //printf("TrackCanvas::draw_drag_handle 3\n");
1833                 draw_line(pixel1, 0, pixel1, get_h());
1834                 set_opaque();
1835 //printf("TrackCanvas::draw_drag_handle 4\n");
1836         }
1837 }
1838
1839
1840 void TrackCanvas::draw_transitions()
1841 {
1842         int64_t x, y, w, h;
1843
1844         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1845                 if( !track->show_transitions() ) continue;
1846
1847                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1848                         if(!edit->transition) continue;
1849                         edit_dimensions(edit, x, y, w, h);
1850                         int strip_x = x, edit_y = y;
1851                         get_transition_coords(edit, x, y, w, h);
1852                         int strip_y = y - mwindow->theme->get_image("plugin_bg_data")->get_h();
1853                         if( track->show_assets() && track->show_titles() )
1854                                 edit_y += mwindow->theme->get_image("title_bg_data")->get_h();
1855                         if( strip_y < edit_y ) strip_y = edit_y;
1856
1857                         int strip_w = Units::round(edit->track->from_units(edit->transition->length) *
1858                                 mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample);
1859                         if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1860                             MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1861                                 PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
1862                                                 track->data_type);
1863                                 if( !server ) continue;
1864                                 VFrame *picon = server->get_picon();
1865                                 if( !picon ) continue;
1866                                 int picon_w = picon->get_w(), picon_h = picon->get_h();
1867                                 int track_h = edit->track->vertical_span(mwindow->theme);
1868                                 if( picon_h > track_h ) picon_h = track_h;
1869                                 draw_vframe(picon, x, y, w, h, 0, 0, picon_w, picon_h);
1870                         }
1871                         if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
1872                                 MWindowGUI::visible(strip_y, strip_y + h, 0, get_h())) {
1873                                 int x = strip_x, w = strip_w;
1874                                 if( x < 0 ) {  w -= -x;  x = 0; }
1875                                 if( w + x > get_w() ) w -= (w + x) - get_w();
1876
1877                                 draw_3segmenth( x, strip_y, w, strip_x, strip_w,
1878                                         mwindow->theme->get_image("plugin_bg_data"), 0);
1879                         }
1880                 }
1881         }
1882 }
1883
1884 void TrackCanvas::draw_loop_points()
1885 {
1886 //printf("TrackCanvas::draw_loop_points 1\n");
1887         if(mwindow->edl->local_session->loop_playback)
1888         {
1889 //printf("TrackCanvas::draw_loop_points 2\n");
1890                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
1891                         mwindow->edl->session->sample_rate /
1892                         mwindow->edl->local_session->zoom_sample -
1893                         mwindow->edl->local_session->view_start[pane->number]);
1894 //printf("TrackCanvas::draw_loop_points 3\n");
1895
1896                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1897                 {
1898                         set_color(GREEN);
1899                         draw_line(x, 0, x, get_h());
1900                 }
1901 //printf("TrackCanvas::draw_loop_points 4\n");
1902
1903                 x = Units::round(mwindow->edl->local_session->loop_end *
1904                         mwindow->edl->session->sample_rate /
1905                         mwindow->edl->local_session->zoom_sample -
1906                         mwindow->edl->local_session->view_start[pane->number]);
1907 //printf("TrackCanvas::draw_loop_points 5\n");
1908
1909                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1910                 {
1911                         set_color(GREEN);
1912                         draw_line(x, 0, x, get_h());
1913                 }
1914 //printf("TrackCanvas::draw_loop_points 6\n");
1915         }
1916 //printf("TrackCanvas::draw_loop_points 7\n");
1917 }
1918
1919 void TrackCanvas::draw_brender_range()
1920 {
1921         if( !mwindow->preferences->use_brender || !mwindow->brender_active ) return;
1922         if( mwindow->edl->session->brender_start >= mwindow->edl->session->brender_end ) return;
1923         if( mwindow->edl->session->brender_end > 0 )
1924         {
1925                 int64_t x1 = Units::round(mwindow->edl->session->brender_start *
1926                         mwindow->edl->session->sample_rate /
1927                         mwindow->edl->local_session->zoom_sample -
1928                         mwindow->edl->local_session->view_start[pane->number]);
1929                 if(MWindowGUI::visible(x1, x1 + 1, 0, get_w()))
1930                 {
1931                         set_color(RED);
1932                         draw_line(x1, 0, x1, get_h());
1933                 }
1934                 int64_t x2 = Units::round(mwindow->edl->session->brender_end *
1935                         mwindow->edl->session->sample_rate /
1936                         mwindow->edl->local_session->zoom_sample -
1937                         mwindow->edl->local_session->view_start[pane->number]);
1938
1939                 if(MWindowGUI::visible(x2, x2 + 1, 0, get_w()))
1940                 {
1941                         set_color(RED);
1942                         draw_line(x2, 0, x2, get_h());
1943                 }
1944         }
1945 }
1946
1947 // The operations which correspond to each automation type
1948 int TrackCanvas::auto_operations[AUTOMATION_TOTAL] =
1949 {
1950         DRAG_MUTE,
1951         DRAG_CAMERA_X,
1952         DRAG_CAMERA_Y,
1953         DRAG_CAMERA_Z,
1954         DRAG_PROJECTOR_X,
1955         DRAG_PROJECTOR_Y,
1956         DRAG_PROJECTOR_Z,
1957         DRAG_FADE,
1958         DRAG_PAN,
1959         DRAG_MODE,
1960         DRAG_MASK,
1961         DRAG_SPEED
1962 };
1963
1964 // The buttonpress operations, so nothing changes unless the mouse moves
1965 // a certain amount.  This allows the keyframe to be used to position the
1966 // insertion point without moving itself.
1967 static int pre_auto_operations[AUTOMATION_TOTAL] =
1968 {
1969         DRAG_MUTE,
1970         DRAG_CAMERA_X,
1971         DRAG_CAMERA_Y,
1972         DRAG_CAMERA_Z,
1973         DRAG_PROJECTOR_X,
1974         DRAG_PROJECTOR_Y,
1975         DRAG_PROJECTOR_Z,
1976         DRAG_FADE,
1977         DRAG_PAN_PRE,
1978         DRAG_MODE_PRE,
1979         DRAG_MASK_PRE,
1980         DRAG_SPEED
1981 };
1982
1983
1984 int TrackCanvas::do_keyframes(int cursor_x,
1985         int cursor_y,
1986         int draw,
1987         int buttonpress,
1988         int &new_cursor,
1989         int &update_cursor,
1990         int &rerender)
1991 {
1992 // Note: button 3 (right mouse button) is not eaten to allow
1993 // track context menu to appear
1994         int result = 0;
1995         EDLSession *session = mwindow->edl->session;
1996
1997
1998
1999         static BC_Pixmap *auto_pixmaps[AUTOMATION_TOTAL] =
2000         {
2001                 0, 0, 0, 0, 0, 0, 0, 0,
2002                 pankeyframe_pixmap,
2003                 modekeyframe_pixmap,
2004                 maskkeyframe_pixmap,
2005         };
2006
2007
2008
2009         for(Track *track = mwindow->edl->tracks->first;
2010                 track && !result;
2011                 track = track->next) {
2012                 Auto *auto_keyframe = 0;
2013                 Automation *automation = track->automation;
2014
2015
2016 // Handle keyframes in reverse drawing order if a button press
2017                 int start = 0;
2018                 int end = AUTOMATION_TOTAL;
2019                 int step = 1;
2020                 if(buttonpress)
2021                 {
2022                         start = AUTOMATION_TOTAL - 1;
2023                         end = -1;
2024                         step = -1;
2025                 }
2026                 for(int i = start; i != end && !result; i += step)
2027                 {
2028 // Event not trapped and automation visible
2029                         Autos *autos = automation->autos[i];
2030                         if(!result && session->auto_conf->autos[i] && autos) {
2031                                 switch(i) {
2032                                 case AUTOMATION_MODE:
2033                                 case AUTOMATION_PAN:
2034                                 case AUTOMATION_MASK:
2035                                         result = do_autos(track, automation->autos[i],
2036                                                 cursor_x, cursor_y, draw,
2037                                                 buttonpress, auto_pixmaps[i],
2038                                                 auto_keyframe, rerender);
2039                                                 break;
2040
2041                                 default: {
2042                                         switch(autos->get_type()) {
2043                                         case Autos::AUTOMATION_TYPE_FLOAT: {
2044                                                 Automation automation(0, track);
2045                                                 int grouptype = automation.autogrouptype(i, track);
2046                                                 if( buttonpress && i == AUTOMATION_SPEED ) {
2047                                                         mwindow->speed_before();
2048                                                 }
2049
2050                                                 if(draw) // Do dropshadow
2051                                                         result = do_float_autos(track, autos,
2052                                                                 cursor_x, cursor_y, draw,
2053                                                                 buttonpress, 1, 1, MDGREY,
2054                                                                 auto_keyframe, grouptype);
2055                                                 result = do_float_autos(track, autos,
2056                                                         cursor_x, cursor_y, draw, buttonpress,
2057                                                         0, 0, GWindowGUI::auto_colors[i],
2058                                                         auto_keyframe, grouptype);
2059
2060                                                 if( !result && buttonpress && i == AUTOMATION_SPEED )
2061                                                         mwindow->speed_after(-1);
2062                                                 int current_grouptype = mwindow->edl->local_session->zoombar_showautotype;
2063                                                 if( result && buttonpress && grouptype != current_grouptype ) {
2064                                                         mwindow->edl->local_session->zoombar_showautotype = grouptype;
2065                                                         mwindow->gui->zoombar->update_autozoom();
2066                                                 }
2067                                                 break; }
2068
2069                                         case Autos::AUTOMATION_TYPE_INT: {
2070                                                 if(draw) // Do dropshadow
2071                                                         result = do_int_autos(track, autos,
2072                                                                 cursor_x, cursor_y, draw,
2073                                                                 buttonpress, 1, 1, MDGREY,
2074                                                                 auto_keyframe);
2075                                                 result = do_int_autos(track, autos,
2076                                                         cursor_x, cursor_y, draw, buttonpress,
2077                                                         0, 0, GWindowGUI::auto_colors[i],
2078                                                         auto_keyframe);
2079                                                 break; }
2080                                         }
2081                                         break; }
2082                                 }
2083
2084                                 if(result)
2085                                 {
2086                                         if(mwindow->session->current_operation == auto_operations[i])
2087                                                 rerender = 1;
2088
2089 // printf("TrackCanvas::do_keyframes %d %d %d\n",
2090 // __LINE__,
2091 // mwindow->session->current_operation,
2092 // auto_operations[i]);
2093                                         if(buttonpress)
2094                                         {
2095                                                 if (buttonpress != 3)
2096                                                 {
2097                                                         if(i == AUTOMATION_FADE || i == AUTOMATION_SPEED)
2098                                                                 fill_ganged_autos(get_double_click(), 0, track,
2099                                                                         (FloatAuto*)mwindow->session->drag_auto);
2100                                                         mwindow->session->current_operation = pre_auto_operations[i];
2101                                                         update_drag_caption();
2102                                                         rerender = 1;
2103                                                 }
2104                                                 else if( auto_keyframe )
2105                                                 {
2106                                                         gui->keyframe_menu->update(automation,
2107                                                                 autos,
2108                                                                 auto_keyframe);
2109                                                         gui->keyframe_menu->activate_menu();
2110                                                         rerender = 1; // the position changes
2111                                                 }
2112                                                 else if( autos )
2113                                                 {
2114                                                         gui->keyframe_hide->update(autos);
2115                                                         gui->keyframe_hide->activate_menu();
2116                                                         rerender = 1; // the position changes
2117                                                 }
2118                                                 if(buttonpress == 1 && ctrl_down() &&
2119                                                    AUTOMATION_TYPE_FLOAT == autos->get_type())
2120                                                         rerender = 1; // special case: curve mode changed
2121                                         }
2122                                 }
2123                         }
2124                 }
2125
2126                 if(!result && session->auto_conf->plugins) {
2127                         Plugin *plugin;
2128                         KeyFrame *keyframe;
2129                         result = do_plugin_autos(track, cursor_x, cursor_y,
2130                                 draw, buttonpress, plugin, keyframe);
2131                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY) {
2132                                 rerender = 1;
2133                         }
2134                         if(result && (buttonpress == 1)) {
2135                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2136                                 update_drag_caption();
2137                                 rerender = 1;
2138                         }
2139                         else if (result && (buttonpress == 3)) {
2140                                 gui->keyframe_menu->update(plugin, keyframe);
2141                                 gui->keyframe_menu->activate_menu();
2142                                 rerender = 1; // the position changes
2143                         }
2144                 }
2145         }
2146
2147 // Final pass to trap event
2148         for(int i = 0; i < AUTOMATION_TOTAL; i++) {
2149                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2150                         mwindow->session->current_operation == auto_operations[i]) {
2151                         result = 1;
2152                         break;
2153                 }
2154         }
2155
2156         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2157                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE) {
2158                 result = 1;
2159         }
2160
2161         update_cursor = 1;
2162         if(result) {
2163                 new_cursor = UPRIGHT_ARROW_CURSOR;
2164         }
2165
2166
2167         return result;
2168 }
2169
2170 void TrackCanvas::draw_keyframe_reticle()
2171 {
2172         int keyframe_hairline = mwindow->preferences->keyframe_reticle;
2173         if( keyframe_hairline == HAIRLINE_NEVER ) return;
2174
2175         int current_op = mwindow->session->current_operation, dragging = 0;
2176         for( int i=0; !dragging && i<AUTOMATION_TOTAL; ++i )
2177                 if( current_op == auto_operations[i] ) dragging = 1;
2178         if( dragging && !mwindow->session->drag_auto ) dragging = 0;
2179
2180         int autoidx = dragging && keyframe_hairline != HAIRLINE_ALWAYS ?
2181                 mwindow->session->drag_auto->autos->autoidx : -1;
2182
2183         if( get_buttonpress() == 1 && dragging &&
2184             keyframe_hairline == HAIRLINE_DRAGGING ) {
2185                 draw_hairline(mwindow->session->drag_auto, RED, 1);
2186                 return;
2187         }
2188
2189         if( keyframe_hairline == HAIRLINE_ALWAYS || ( get_buttonpress() == 2 &&
2190             keyframe_hairline == HAIRLINE_DRAGGING && dragging ) ) {
2191                 int show = dragging || keyframe_hairline == HAIRLINE_ALWAYS ? 1 : 0;
2192                 for( Track *track = mwindow->edl->tracks->first; track; track=track->next ) {
2193                         Automation *automation = track->automation;
2194                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2195                                 if( !mwindow->edl->session->auto_conf->autos[i] ) continue;
2196                                 // automation visible
2197                                 Autos *autos = automation->autos[i];
2198                                 if( !autos ) continue;
2199                                 if( autoidx >= 0 && autos->autoidx != autoidx ) continue;
2200                                 for( Auto *auto_keyframe=autos->first; auto_keyframe;
2201                                      auto_keyframe = auto_keyframe->next ) {
2202                                         draw_hairline(auto_keyframe, BLUE, show);
2203                                 }
2204                         }
2205                 }
2206
2207                 if( dragging )
2208                         draw_hairline(mwindow->session->drag_auto, RED, 1);
2209         }
2210 }
2211
2212 void TrackCanvas::draw_auto(Auto *current,
2213         int x,
2214         int y,
2215         int center_pixel,
2216         int zoom_track)
2217 {
2218         int x1, y1, x2, y2;
2219
2220         x1 = x - HANDLE_W / 2;
2221         x2 = x + HANDLE_W / 2;
2222         y1 = center_pixel + y - HANDLE_W / 2;
2223         y2 = center_pixel + y + HANDLE_W / 2;
2224
2225         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2226         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2227
2228         draw_box(x1, y1, x2 - x1, y2 - y1);
2229 }
2230
2231
2232 // This draws lines for bezier in & out controls
2233 void TrackCanvas::draw_cropped_line(int x1,
2234         int y1,
2235         int x2,
2236         int y2,
2237         int min_y,
2238         int max_y)
2239 {
2240
2241
2242 // Don't care about x since it is clipped by the window.
2243 // Put y coords in ascending order
2244         if(y2 < y1) {
2245                 y2 ^= y1; y1 ^= y2; y2 ^= y1;
2246                 x2 ^= x1; x1 ^= x2; x2 ^= x1;
2247         }
2248
2249
2250
2251         double slope = (double)(x2 - x1) / (y2 - y1);
2252 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2253         if(y1 < min_y) {
2254                 x1 = (int)(x1 + (min_y - y1) * slope);
2255                 y1 = min_y;
2256         }
2257         else if(y1 >= max_y) {
2258                 x1 = (int)(x1 + (max_y - 1 - y1) * slope);
2259                 y1 = max_y - 1;
2260         }
2261
2262         if(y2 >= max_y) {
2263                 x2 = (int)(x2 + (max_y - 1 - y2) * slope);
2264                 y2 = max_y - 1;
2265         }
2266         else if(y2 < min_y) {
2267                 x2 = (int)(x2 + (min_y - y2) * slope);
2268                 y1 = min_y;
2269         }
2270
2271
2272 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2273         if( y1 >= min_y && y1 < max_y &&
2274             y2 >= min_y && y2 < max_y )
2275                 draw_line(x1, y1, x2, y2);
2276 }
2277
2278
2279 void TrackCanvas::draw_floatauto(FloatAuto *current,
2280         int x,
2281         int y,
2282         int in_x,
2283         int in_y,
2284         int out_x,
2285         int out_y,
2286         int center_pixel,
2287         int zoom_track,
2288         int color)
2289 {
2290         int x1 = x - HANDLE_W / 2; // Center
2291         int x2 = x + HANDLE_W / 2;
2292         int y1 = center_pixel + y - HANDLE_H / 2;
2293         int y2 = center_pixel + y + HANDLE_H / 2;
2294         int ymin = center_pixel - zoom_track / 2;
2295         int ymax = center_pixel + zoom_track / 2;
2296         CLAMP(y1, ymin, ymax);
2297         CLAMP(y2, ymin, ymax);
2298
2299         if(y2 - 1 > y1)
2300         {
2301                 set_color(BLACK);
2302                 draw_box(x1 + 1, y1 + 1, x2 - x1, y2 - y1);
2303                 set_color(color);
2304                 draw_box(x1, y1, x2 - x1, y2 - y1);
2305         }
2306
2307 // show bezier control points (only) if this
2308 // floatauto doesn't adjust it's tangents automatically
2309         if(current->curve_mode != FloatAuto::FREE &&
2310            current->curve_mode != FloatAuto::TFREE)
2311                 return;
2312
2313         if(in_x != x)
2314                 draw_floatauto_ctrlpoint(x, y, in_x, in_y, center_pixel, zoom_track, color);
2315         if(out_x != x)
2316                 draw_floatauto_ctrlpoint(x, y, out_x, out_y, center_pixel, zoom_track, color);
2317 }
2318
2319 inline int quantize(float f) { return (int)floor(f + 0.5); }
2320
2321 inline void TrackCanvas::draw_floatauto_ctrlpoint(
2322         int x, int y, int cp_x, int cp_y, int center_pixel,
2323         int zoom_track, int color)
2324 // draw the tangent and a handle for given bézier ctrl point
2325 {
2326         bool handle_visible = (abs(cp_y) <= zoom_track / 2);
2327
2328         float slope = (float)(cp_y - y)/(cp_x - x);
2329         CLAMP(cp_y, -zoom_track / 2, zoom_track / 2);
2330         if(slope != 0)
2331                 cp_x = x + quantize((cp_y - y) / slope);
2332
2333         y    += center_pixel;
2334         cp_y += center_pixel;
2335
2336         // drawing the tangent as a dashed line...
2337         int const dash = HANDLE_W;
2338         int const gap  = HANDLE_W / 2;
2339         float sx = 3 * (cp_x - x) / 4.;
2340         float ex = 0;
2341
2342         // q is the x displacement for a unit line of slope
2343         float q = (sx > 0 ? 1 : -1) / sqrt(1 + slope * slope);
2344
2345         float dist = 1/q * sx;
2346         if( dist > dash )
2347                 ex = sx - q * dash;
2348
2349         set_color(color);
2350         do {
2351                 float sy = slope * sx, ey = slope * ex;
2352                 draw_line(quantize(sx + x), quantize(sy + y), quantize(ex + x), quantize(ey + y));
2353                 sx = ex - q * gap;
2354                 ex = sx - q * dash;
2355         } while(q*ex > 0);
2356
2357         if(handle_visible)
2358         {
2359                 int r = HANDLE_W / 2;
2360                 int cp_x1 = cp_x - r;
2361                 int cp_y1 = cp_y - r;
2362                 set_color(BLACK);
2363                 draw_disc(cp_x1, cp_y1, 2 * r, 2 * r);
2364                 set_color(color);
2365                 draw_circle(cp_x1, cp_y1, 2 * r, 2 * r);
2366         }
2367 }
2368
2369
2370 int TrackCanvas::test_auto(Auto *current,
2371         int x, int y, int center_pixel, int zoom_track,
2372         int cursor_x, int cursor_y, int buttonpress)
2373 {
2374         int x1, y1, x2, y2;
2375         int result = 0;
2376
2377         x1 = x - HANDLE_W / 2;
2378         x2 = x + HANDLE_W / 2;
2379         y1 = center_pixel + y - HANDLE_H / 2;
2380         y2 = center_pixel + y + HANDLE_H / 2;
2381         int ymin = center_pixel - zoom_track / 2;
2382         int ymax = center_pixel + zoom_track / 2;
2383         CLAMP(y1, ymin, ymax);
2384         CLAMP(y2, ymin, ymax);
2385
2386         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2387         {
2388                 if(buttonpress && buttonpress != 3)
2389                 {
2390                         mwindow->session->drag_auto = current;
2391                         mwindow->session->drag_start_percentage = (float)((IntAuto*)current)->value;
2392                         // Toggle Autos don't respond to vertical zoom, they always show up
2393                         // with "on" == 100% == line on top
2394                         mwindow->session->drag_start_position = current->position;
2395                         mwindow->session->drag_origin_x = cursor_x;
2396                         mwindow->session->drag_origin_y = cursor_y;
2397                 }
2398                 result = 1;
2399         }
2400
2401         if(buttonpress && buttonpress != 3 && result)
2402         {
2403 //printf("TrackCanvas::test_auto %d\n", __LINE__);
2404                 mwindow->undo->update_undo_before();
2405         }
2406
2407         return result;
2408 }
2409
2410 // some Helpers for test_floatauto(..)
2411 // and for dragging the tangents/ctrl points
2412 inline float test_curve_line( int x0, int y0, int ctrl_x, int ctrl_y,
2413         float cursor_x, float cursor_y)
2414 {
2415 // Control point switched off?
2416         if( x0 == ctrl_x ) return 0.0;
2417         double x1 = ctrl_x-x0, y1 = ctrl_y-y0;
2418         double x = cursor_x-x0, y = cursor_y-y0;
2419 // wrong side of ctrl handle
2420         if( x*x1 < 0 ) return 0.0;
2421 // outside handle radius
2422         if( (x*x + y*y) > (x1*x1 + y1*y1) ) return 0;
2423         double xx1 = x1*x1, yy1 = y1*y1;
2424         double xx = x*x, yy = y*y;
2425 // distance squared from cursor to cursor projected to line from ctrl to handle
2426 //   along a line perpendicular to line from ctrl to handle (closest approach)
2427 // (x**2*y1**2 - 2*x*x1*y*y1 + x1**2*y**2)/(x1**2 + y1**2)
2428         double dist2 = (xx*yy1 - 2*x*x1*y*y1 + xx1*yy)/(xx1 + yy1);
2429         return dist2 < (HANDLE_W*HANDLE_W)/4. ? x/x1 : 0.;
2430 }
2431
2432
2433 inline
2434 float levered_position(float position, float ref_pos)
2435 {
2436         if( 1e-6 > fabs(ref_pos) || isnan(ref_pos))
2437                 return 0.0;
2438         return ref_pos / position;
2439 }
2440
2441
2442 float TrackCanvas::value_to_percentage(float auto_value, int autogrouptype)
2443 // transforms automation value into current display coords,
2444 // dependant on current automation display range for the given kind of automation
2445 {
2446         if(!mwindow || !mwindow->edl) return 0;
2447         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2448         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2449         float automation_range = automation_max - automation_min;
2450         if( 0 >= automation_range || isnan(auto_value) || isinf(auto_value) )
2451                 return 0;
2452         return (auto_value - automation_min) / automation_range;
2453 }
2454
2455
2456
2457 int TrackCanvas::test_floatauto(FloatAuto *current, int x, int y, int in_x,
2458         int in_y, int out_x, int out_y, int center_pixel, int zoom_track,
2459         int cursor_x, int cursor_y, int buttonpress, int autogrouptype)
2460 {
2461         int result = 0;
2462
2463         int x1 = x - HANDLE_W / 2;
2464         int x2 = x + HANDLE_W / 2;
2465         int y1 = center_pixel + y - HANDLE_W / 2;
2466         int y2 = center_pixel + y + HANDLE_W / 2;
2467         int ymin = center_pixel - zoom_track / 2;
2468         int ymax = center_pixel + zoom_track / 2;
2469         CLAMP(y1, ymin, ymax);
2470         CLAMP(y2, ymin, ymax);
2471
2472         int in_x1 = in_x - HANDLE_W / 2;
2473         int in_x2 = in_x + HANDLE_W / 2;
2474         int in_y1 = center_pixel + in_y - HANDLE_W / 2;
2475         int in_y2 = center_pixel + in_y + HANDLE_W / 2;
2476         CLAMP(in_y1, ymin, ymax);
2477         CLAMP(in_y2, ymin, ymax);
2478
2479         int out_x1 = out_x - HANDLE_W / 2;
2480         int out_x2 = out_x + HANDLE_W / 2;
2481         int out_y1 = center_pixel + out_y - HANDLE_W / 2;
2482         int out_y2 = center_pixel + out_y + HANDLE_W / 2;
2483         CLAMP(out_y1, ymin, ymax);
2484         CLAMP(out_y2, ymin, ymax);
2485
2486
2487 //printf("TrackCanvas::test_floatauto %d %d %d %d %d %d\n", cursor_x, cursor_y, x1, x2, y1, y2);
2488 // buttonpress could be the start of a drag operation
2489 #define INIT_DRAG(POS,VAL) \
2490                 mwindow->session->drag_auto = current;      \
2491                 mwindow->session->drag_origin_x = cursor_x;  \
2492                 mwindow->session->drag_origin_y = cursor_y;   \
2493                 mwindow->session->drag_start_position = (POS); \
2494                 mwindow->session->drag_start_percentage = (VAL);
2495
2496 #define WITHIN(X1,X2,Y1,Y2) (cursor_x >=(X1) && cursor_x <(X2) && cursor_y >=(Y1) && cursor_y <(Y2) )
2497
2498
2499 // without modifier we are manipulating the automation node
2500 // with ALT it's about dragging only the value of the node
2501 // with SHIFT the value snaps to the value of neighbouring nodes
2502 // CTRL indicates we are rather manipulating the tangent(s) of the node
2503
2504         if(!ctrl_down())
2505         {
2506                 if( WITHIN(x1,x2,y1,y2))
2507                 {       // cursor hits node
2508                         result = 1;
2509
2510                         if(buttonpress && (buttonpress != 3))
2511                         {
2512                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2513                                 mwindow->session->drag_handle = 0;
2514                 }       }
2515         }
2516         else // ctrl_down()
2517         {
2518                 if( WITHIN(x1,x2,y1,y2))
2519                 {
2520                         result = 1;
2521                         if(buttonpress && (buttonpress != 3))
2522                         {
2523                                 // could be ctrl-click or ctrl-drag
2524                                 // click would cycle through tangent modes
2525                                 ((FloatAuto*)current)->toggle_curve_mode();
2526
2527                                 // drag will start dragging the tangent, if applicable
2528                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2529                                 mwindow->session->drag_handle = 0;
2530                         }
2531                 }
2532
2533                 float lever = 0.0; // we use the tangent as a draggable lever. 1.0 is at the ctrl point
2534
2535 // Test in control
2536                 if( in_x != x && current->position > 0 &&
2537                         (FloatAuto::FREE == current->curve_mode ||
2538                          FloatAuto::TFREE == current->curve_mode))
2539 // act on in control handle only if
2540 // tangent is significant and is editable (not automatically choosen)
2541                 {
2542                         lever = test_curve_line(x, y, in_x, in_y, cursor_x, cursor_y-center_pixel);
2543  // either cursor in ctrl-point handle or cursor on tangent line
2544                         if( WITHIN(in_x1,in_x2,in_y1,in_y2) || lever > 0.0 ) {
2545                                 result = 1;
2546                                 if(buttonpress && (buttonpress != 3)) {
2547                                         if(lever == 0.0) lever=1.0; // we entered by dragging the handle...
2548 //                                      lever = 1.0;
2549                                         mwindow->session->drag_handle = 1;
2550                                         float new_invalue = current->get_value() + lever * current->get_control_in_value();
2551                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_in_position()),
2552                                                   value_to_percentage(new_invalue, autogrouptype))
2553                                 }
2554                         }
2555                 }
2556
2557 // Test out control
2558                 if(out_x != x &&
2559                         (FloatAuto::FREE == current->curve_mode ||
2560                          FloatAuto::TFREE == current->curve_mode))
2561 // act on out control only if tangent is significant and is editable
2562                 {
2563                         lever = test_curve_line(x, y, out_x, out_y, cursor_x, cursor_y-center_pixel);
2564                         if(WITHIN(out_x1,out_x2,out_y1,out_y2) || lever > 0.0 ) {
2565                                 result = 1;
2566                                 if(buttonpress && (buttonpress != 3)) {
2567                                         if(lever == 0.0) lever=1.0;
2568 //                                      lever = 1.0;
2569                                         mwindow->session->drag_handle = 2;
2570                                         float new_outvalue = current->get_value() + lever * current->get_control_out_value();
2571                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_out_position()),
2572                                                   value_to_percentage(new_outvalue, autogrouptype))
2573                                 }
2574                         }
2575                 }
2576         } // end ctrl_down()
2577
2578 #undef WITHIN
2579 #undef INIT_DRAG
2580
2581 // if(buttonpress)
2582 // 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",
2583 // mwindow->session->drag_handle,
2584 // ctrl_down(),
2585 // cursor_x,
2586 // cursor_y,
2587 // x1, x2, y1, y2);
2588         if(buttonpress && (buttonpress != 3) && result)
2589         {
2590                 mwindow->undo->update_undo_before();
2591         }
2592
2593         return result;
2594 }
2595
2596
2597 // Get the float value & y for position x on the canvas
2598 #define X_TO_FLOATLINE(x) \
2599         int64_t position1 = (int64_t)(unit_start + x * zoom_units); \
2600         int64_t position2 = (int64_t)(unit_start + x * zoom_units) + 1; \
2601 /* Call by reference fails for some reason here */ \
2602         float value1 = autos->get_value(position1, PLAY_FORWARD, previous1, next1); \
2603         float value2 = autos->get_value(position2, PLAY_FORWARD, previous1, next1); \
2604         double position = unit_start + x * zoom_units; \
2605         double value = 0; \
2606         if(position2 > position1) \
2607         { \
2608                 value = value1 + \
2609                         (value2 - value1) * \
2610                         (position - position1) / \
2611                         (position2 - position1); \
2612         } \
2613         else \
2614         { \
2615                 value = value1; \
2616         } \
2617         AUTOMATIONCLAMPS(value, autogrouptype); \
2618         int y = center_pixel + \
2619                 (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2620
2621
2622 void TrackCanvas::draw_floatline(int center_pixel,
2623         FloatAuto *previous, FloatAuto *next, FloatAutos *autos,
2624         double unit_start, double zoom_units, double yscale,
2625         int x1, int y1, int x2, int y2,
2626         int color, int autogrouptype)
2627 {
2628 // Solve bezier equation for either every pixel or a certain large number of
2629 // points.
2630
2631 // Not using slope intercept
2632         x1 = MAX(0, x1);
2633         int prev_y = y1 + center_pixel;
2634
2635
2636 // Call by reference fails for some reason here
2637         FloatAuto *previous1 = previous, *next1 = next;
2638         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2639         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2640         float automation_range = automation_max - automation_min;
2641
2642         for(int x = x1; x < x2; x++)
2643         {
2644 // Interpolate value between frames
2645                 X_TO_FLOATLINE(x)
2646
2647                 if(/* x > x1 && */
2648                         y >= center_pixel - yscale / 2 &&
2649                         y < center_pixel + yscale / 2 - 1)
2650                 {
2651 // printf("TrackCanvas::draw_floatline y=%d min=%d max=%d\n",
2652 // y,
2653 // (int)(center_pixel - yscale / 2),
2654 // (int)(center_pixel + yscale / 2 - 1));
2655
2656 //printf("draw_line(%d,%d,  %d,%d)\n", x - 1, prev_y  , x, y);
2657                         draw_line(x - 1, prev_y  , x, y   );
2658                 }
2659                 prev_y = y;
2660         }
2661 }
2662
2663
2664
2665
2666
2667 int TrackCanvas::test_floatline(int center_pixel,
2668                 FloatAutos *autos,
2669                 double unit_start,
2670                 double zoom_units,
2671                 double yscale,
2672                 int x1,
2673                 int x2,
2674                 int cursor_x,
2675                 int cursor_y,
2676                 int buttonpress,
2677                 int autogrouptype)
2678 {
2679         int result = 0;
2680
2681
2682         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2683         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2684         float automation_range = automation_max - automation_min;
2685         FloatAuto *previous1 = 0, *next1 = 0;
2686         X_TO_FLOATLINE(cursor_x);
2687
2688         if(cursor_x >= x1 &&
2689                 cursor_x < x2 &&
2690                 cursor_y >= y - HANDLE_W / 2 &&
2691                 cursor_y < y + HANDLE_W / 2 &&
2692                 !ctrl_down())
2693         {
2694                 result = 1;
2695
2696 // Menu
2697                 if(buttonpress == 3)
2698                 {
2699                 }
2700                 else
2701 // Create keyframe
2702                 if(buttonpress)
2703                 {
2704                         Auto *current;
2705                         mwindow->undo->update_undo_before();
2706                         double position = autos->track->from_units(position1);
2707                         position = mwindow->edl->align_to_frame(position, 0);
2708                         int64_t new_position = autos->track->to_units(position,0);
2709                         current = mwindow->session->drag_auto = autos->insert_auto(new_position);
2710                         ((FloatAuto*)current)->set_value(value);
2711                         mwindow->session->drag_start_percentage = value_to_percentage(value, autogrouptype);
2712                         mwindow->session->drag_start_position = current->position;
2713                         mwindow->session->drag_origin_x = cursor_x;
2714                         mwindow->session->drag_origin_y = cursor_y;
2715                         mwindow->session->drag_handle = 0;
2716                 }
2717         }
2718
2719
2720         return result;
2721 }
2722
2723
2724 void TrackCanvas::fill_ganged_autos(int all, float change, Track *skip, FloatAuto *fauto)
2725 {
2726         if( !skip->gang ) return;
2727 // Handles the special case of modifying a fadeauto
2728 // when there are ganged faders on several tracks
2729         double position = skip->from_units(fauto->position);
2730         int autoidx = fauto->autos->autoidx;
2731
2732         for(Track *current = mwindow->edl->tracks->first; current; current = NEXT) {
2733                 if( (all || current->data_type == skip->data_type) &&
2734                     current->gang && current->record && current != skip ) {
2735                         FloatAutos *fade_autos = (FloatAutos*)current->automation->autos[autoidx];
2736                         float auto_min = mwindow->edl->local_session->automation_mins[fade_autos->autogrouptype];
2737                         float auto_max = mwindow->edl->local_session->automation_maxs[fade_autos->autogrouptype];
2738                         int64_t current_position = current->to_units(position, 1);
2739                         FloatAuto *keyframe = (FloatAuto*)fade_autos->get_auto_at_position(position);
2740                         if( keyframe ) {
2741 // keyframe exists, just change it
2742                                 float value = keyframe->get_value();
2743                                 float new_value = value + change;
2744                                 CLAMP(new_value, auto_min, auto_max);
2745                                 keyframe->adjust_to_new_coordinates(current_position, new_value);
2746                         }
2747                         else if( mwindow->edl->session->auto_keyframes ) {
2748 // create keyframe on neighbouring track at the point in time given by fauto
2749                                 FloatAuto *previous = 0, *next = 0;
2750                                 float value = fade_autos->get_value(current_position, PLAY_FORWARD, previous, next);
2751                                 float new_value = value + change;
2752                                 CLAMP(new_value, auto_min, auto_max);
2753                                 keyframe = (FloatAuto*)fade_autos->insert_auto(current_position);
2754                                 keyframe->set_value(new_value);
2755                         }
2756                         else
2757                                 continue;
2758                         mwindow->session->drag_auto_gang->append((Auto *)keyframe);
2759                 }
2760         }
2761 }
2762
2763 void TrackCanvas::update_ganged_autos(float change, Track *skip, FloatAuto *fauto)
2764 {
2765         double position = skip->from_units(fauto->position);
2766 // Move the gangs
2767         for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
2768                 FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2769                 int64_t keyframe_position = keyframe->autos->track->to_units(position, 1);
2770                 float new_value = keyframe->get_value() + change;
2771                 CLAMP(new_value,
2772                       mwindow->edl->local_session->automation_mins[keyframe->autos->autogrouptype],
2773                       mwindow->edl->local_session->automation_maxs[keyframe->autos->autogrouptype]);
2774                 keyframe->adjust_to_new_coordinates(keyframe_position, new_value);
2775         }
2776 }
2777
2778 void TrackCanvas::clear_ganged_autos()
2779 {
2780 // remove the gangs
2781         for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
2782                 FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2783                 keyframe->autos->remove_nonsequential(keyframe);
2784         }
2785         mwindow->session->drag_auto_gang->remove_all();
2786 }
2787
2788
2789 void TrackCanvas::draw_toggleline(int center_pixel,
2790         int x1,
2791         int y1,
2792         int x2,
2793         int y2)
2794 {
2795         draw_line(x1, center_pixel + y1, x2, center_pixel + y1);
2796
2797         if(y2 != y1)
2798         {
2799                 draw_line(x2, center_pixel + y1, x2, center_pixel + y2);
2800         }
2801 }
2802
2803 int TrackCanvas::test_toggleline(Autos *autos,
2804         int center_pixel,
2805         int x1,
2806         int y1,
2807         int x2,
2808         int y2,
2809         int cursor_x,
2810         int cursor_y,
2811         int buttonpress)
2812 {
2813         int result = 0;
2814         if(cursor_x >= x1 && cursor_x < x2)
2815         {
2816                 int miny = center_pixel + y1 - HANDLE_W / 2;
2817                 int maxy = center_pixel + y1 + HANDLE_W / 2;
2818                 if(cursor_y >= miny && cursor_y < maxy)
2819                 {
2820                         result = 1;
2821
2822 // Menu
2823                         if(buttonpress == 3)
2824                         {
2825                         }
2826                         else
2827 // Insert keyframe
2828                         if(buttonpress)
2829                         {
2830                                 Auto *current;
2831                                 double position = (double)(cursor_x +
2832                                                 mwindow->edl->local_session->view_start[pane->number]) *
2833                                         mwindow->edl->local_session->zoom_sample /
2834                                         mwindow->edl->session->sample_rate;
2835                                 int64_t unit_position = autos->track->to_units(position, 0);
2836                                 int new_value = (int)((IntAutos*)autos)->get_automation_constant(unit_position, unit_position);
2837
2838                                 mwindow->undo->update_undo_before();
2839
2840                                 current = mwindow->session->drag_auto = autos->insert_auto(unit_position);
2841                                 ((IntAuto*)current)->value = new_value;
2842                                 // Toggle Autos don't respond to vertical zoom, they always show up
2843                                 // with "on" == 100% == line on top
2844                                 mwindow->session->drag_start_percentage = (float)new_value;
2845                                 mwindow->session->drag_start_position = current->position;
2846                                 mwindow->session->drag_origin_x = cursor_x;
2847                                 mwindow->session->drag_origin_y = cursor_y;
2848                         }
2849                 }
2850         };
2851
2852         return result;
2853 }
2854
2855 void TrackCanvas::calculate_viewport(Track *track,
2856         double &view_start,   // Seconds
2857         double &unit_start,
2858         double &view_end,     // Seconds
2859         double &unit_end,
2860         double &yscale,
2861         int &center_pixel,
2862         double &zoom_sample,
2863         double &zoom_units)
2864 {
2865
2866         view_start = (double)mwindow->edl->local_session->view_start[pane->number] *
2867                 mwindow->edl->local_session->zoom_sample /
2868                 mwindow->edl->session->sample_rate;
2869         unit_start = track->to_doubleunits(view_start);
2870         view_end = (double)(mwindow->edl->local_session->view_start[pane->number] +
2871                 get_w()) *
2872                 mwindow->edl->local_session->zoom_sample /
2873                 mwindow->edl->session->sample_rate;
2874         unit_end = track->to_doubleunits(view_end);
2875
2876         int y = track->y_pixel
2877                         - mwindow->edl->local_session->track_start[pane->number];
2878         int has_titles = track->show_titles();
2879         int has_assets = track->show_assets();
2880         double title_bg_h = mwindow->theme->get_image("title_bg_data")->get_h();
2881         double asset_h = resource_h();
2882         double title_h = mwindow->theme->title_h;
2883         double ys = has_assets ? asset_h : has_titles ? title_bg_h : 0;
2884         double dy = has_titles ?
2885                 ( has_assets ? title_bg_h + asset_h/2 : title_bg_h/2) :
2886                 ( has_assets ? asset_h/2 : 0) ;
2887         if( dy < title_h/2 ) { ys = title_h;  dy = ys / 2; }
2888         yscale = ys;
2889         center_pixel = y + dy;
2890
2891         zoom_sample = mwindow->edl->local_session->zoom_sample;
2892         zoom_units = track->to_doubleunits(zoom_sample / mwindow->edl->session->sample_rate);
2893 }
2894
2895 float TrackCanvas::percentage_to_value(float percentage,
2896         int is_toggle,
2897         Auto *reference,
2898         int autogrouptype)
2899 {
2900         float result;
2901         if(is_toggle)
2902         {
2903                 if(percentage > 0.5)
2904                         result = 1;
2905                 else
2906                         result = 0;
2907         }
2908         else
2909         {
2910                 float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2911                 float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2912                 float automation_range = automation_max - automation_min;
2913
2914                 result = percentage * automation_range + automation_min;
2915                 if(reference)
2916                 {
2917                         FloatAuto *ptr = (FloatAuto*)reference;
2918                         result -= ptr->get_value();
2919                 }
2920 //printf("TrackCanvas::percentage_to_value %d %f\n", __LINE__, result);
2921         }
2922         return result;
2923 }
2924
2925
2926 void TrackCanvas::calculate_auto_position(double *x, double *y,
2927         double *in_x, double *in_y, double *out_x, double *out_y,
2928         Auto *current, double unit_start, double zoom_units, double yscale,
2929         int autogrouptype)
2930 {
2931         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2932         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2933         float automation_range = automation_max - automation_min;
2934         FloatAuto *ptr = (FloatAuto*)current;
2935         *x = (double)(ptr->position - unit_start) / zoom_units;
2936         *y = ((ptr->get_value() - automation_min) / automation_range - 0.5) * -yscale;
2937
2938         if(in_x) {
2939 //              *in_x = EQUIV(ptr->control_in_value, 0.0) ? *x : *x - mwindow->theme->control_pixels;
2940                 *in_x = (double)(ptr->position + ptr->get_control_in_position() - unit_start) / zoom_units;
2941         }
2942
2943         if(in_y) {
2944                 *in_y = (((ptr->get_value() + ptr->get_control_in_value()) -
2945                                 automation_min) / automation_range - 0.5) * -yscale;
2946         }
2947
2948         if(out_x) {
2949 //              *out_x = EQUIV(ptr->control_out_value, 0.0) ? *x : *x + mwindow->theme->control_pixels;
2950                 *out_x = (double)(ptr->position + ptr->get_control_out_position() - unit_start) / zoom_units;
2951         }
2952
2953         if(out_y) {
2954                 *out_y = (((ptr->get_value() + ptr->get_control_out_value()) -
2955                                  automation_min) / automation_range - 0.5) * -yscale;
2956         }
2957 }
2958
2959
2960 int TrackCanvas::do_float_autos(Track *track, Autos *autos, int cursor_x, int cursor_y,
2961                 int draw, int buttonpress, int x_offset, int y_offset, int color,
2962                 Auto* &auto_instance, int autogrouptype)
2963 {
2964         int result = 0;
2965         int center_pixel, draw_auto;
2966         double view_start, unit_start;
2967         double view_end, unit_end, yscale;
2968         double zoom_sample, zoom_units;
2969         double in_x2, in_y2, out_x2, out_y2;
2970         double slope;
2971         //int skip = 0;
2972
2973         auto_instance = 0;
2974
2975         if(draw) set_color(color);
2976
2977         calculate_viewport(track, view_start, unit_start, view_end, unit_end,
2978                         yscale, center_pixel, zoom_sample, zoom_units);
2979
2980 // Get first auto before start
2981         Auto *current = 0, *previous = 0;
2982
2983         for( current = autos->last;
2984                 current && current->position >= unit_start;
2985                 current = PREVIOUS ) ;
2986
2987         Auto *first_auto = current ? current :
2988                  autos->first ? autos->first : autos->default_auto;
2989
2990         double ax = 0, ay = 0, ax2 = 0, ay2 = 0;
2991         if( first_auto ) {
2992                 calculate_auto_position(&ax, &ay, 0, 0, 0, 0,
2993                         first_auto, unit_start, zoom_units, yscale, autogrouptype);
2994         }
2995         if( current )
2996                 current = NEXT;
2997         else {
2998                 current = autos->first;
2999                 ax = 0;
3000         }
3001
3002         do {
3003                 //skip = 0;
3004                 draw_auto = 1;
3005
3006                 if(current) {
3007                         calculate_auto_position(&ax2, &ay2, &in_x2, &in_y2, &out_x2, &out_y2,
3008                                 current, unit_start, zoom_units, yscale, autogrouptype);
3009                 }
3010                 else {
3011                         ax2 = get_w();
3012                         ay2 = ay;
3013                         //skip = 1;
3014                 }
3015
3016                 slope = ax2 > ax ? (ay2 - ay) / (ax2 - ax) : 0;
3017
3018                 if(ax2 > get_w()) {
3019                         draw_auto = 0;
3020                         ax2 = get_w();
3021                         ay2 = ay + slope * (get_w() - ax);
3022                 }
3023
3024                 if(ax < 0) {
3025                         ay = ay + slope * (0 - ax);
3026                         ax = 0;
3027                 }
3028
3029 // Draw or test handle
3030                 if( current && !result && current != autos->default_auto ) {
3031                         if( !draw && track->record ) {
3032                                 result = test_floatauto((FloatAuto*)current, (int)ax2, (int)ay2,
3033                                         (int)in_x2, (int)in_y2, (int)out_x2, (int)out_y2,
3034                                         (int)center_pixel, (int)yscale, cursor_x, cursor_y,
3035                                         buttonpress, autogrouptype);
3036                                 if( result )
3037                                         auto_instance = current;
3038                         }
3039                         if( draw && draw_auto ) {
3040                                 draw_floatauto((FloatAuto*)current, (int)ax2 + x_offset, (int)ay2,
3041                                         (int)in_x2 + x_offset, (int)in_y2,
3042                                         (int)out_x2 + x_offset, (int)out_y2,
3043                                         (int)center_pixel + y_offset, (int)yscale, color);
3044                         }
3045                 }
3046
3047 // Draw or test joining line
3048                 if( !draw && !result && track->record /* && buttonpress != 3 */ ) {
3049                         result = test_floatline(center_pixel,
3050                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3051 // Exclude auto coverage from the end of the line.  The auto overlaps
3052                                 (int)ax, (int)ax2 - HANDLE_W / 2, cursor_x, cursor_y,
3053                                 buttonpress, autogrouptype);
3054                 }
3055                 if( draw )
3056                         draw_floatline(center_pixel, (FloatAuto*)previous, (FloatAuto*)current,
3057                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3058                                 (int)ax, (int)ay, (int)ax2, (int)ay2,
3059                                 color, autogrouptype);
3060
3061                 if( current ) {
3062                         previous = current;
3063                         current = NEXT;
3064                 }
3065
3066                 ax = ax2;  ay = ay2;
3067         } while( current && current->position <= unit_end && !result );
3068
3069         if( ax < get_w() && !result ) {
3070                 ax2 = get_w();  ay2 = ay;
3071                 if(!draw && track->record /* && buttonpress != 3 */ ) {
3072                         result = test_floatline(center_pixel,
3073                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3074                                 (int)ax, (int)ax2, cursor_x, cursor_y,
3075                                 buttonpress, autogrouptype);
3076                 }
3077                 if( draw )
3078                         draw_floatline(center_pixel,
3079                                 (FloatAuto*)previous, (FloatAuto*)current,
3080                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3081                                 (int)ax, (int)ay, (int)ax2, (int)ay2,
3082                                 color, autogrouptype);
3083         }
3084
3085         return result;
3086 }
3087
3088
3089 int TrackCanvas::do_int_autos(Track *track,
3090                 Autos *autos,
3091                 int cursor_x,
3092                 int cursor_y,
3093                 int draw,
3094                 int buttonpress,
3095                 int x_offset,
3096                 int y_offset,
3097                 int color,
3098                 Auto *&auto_instance)
3099 {
3100         int result = 0;
3101         double view_start;
3102         double unit_start;
3103         double view_end;
3104         double unit_end;
3105         double yscale;
3106         int center_pixel;
3107         double zoom_sample;
3108         double zoom_units;
3109         double ax, ay, ax2, ay2;
3110
3111         auto_instance = 0;
3112
3113         if(draw) set_color(color);
3114
3115         calculate_viewport(track,
3116                 view_start,
3117                 unit_start,
3118                 view_end,
3119                 unit_end,
3120                 yscale,
3121                 center_pixel,
3122                 zoom_sample,
3123                 zoom_units);
3124
3125
3126         double high = -yscale * 0.8 / 2;
3127         double low = yscale * 0.8 / 2;
3128
3129 // Get first auto before start
3130         Auto *current;
3131         for(current = autos->last; current && current->position >= unit_start; current = PREVIOUS)
3132                 ;
3133
3134         if(current)
3135         {
3136                 ax = 0;
3137                 ay = ((IntAuto*)current)->value > 0 ? high : low;
3138                 current = NEXT;
3139         }
3140         else
3141         {
3142                 current = autos->first ? autos->first : autos->default_auto;
3143                 if(current)
3144                 {
3145                         ax = 0;
3146                         ay = ((IntAuto*)current)->value > 0 ? high : low;
3147                 }
3148                 else
3149                 {
3150                         ax = 0;
3151                         ay = yscale;
3152                 }
3153         }
3154
3155         do
3156         {
3157                 if(current)
3158                 {
3159                         ax2 = (double)(current->position - unit_start) / zoom_units;
3160                         ay2 = ((IntAuto*)current)->value > 0 ? high : low;
3161                 }
3162                 else
3163                 {
3164                         ax2 = get_w();
3165                         ay2 = ay;
3166                 }
3167
3168                 if(ax2 > get_w()) ax2 = get_w();
3169
3170                 if(current && !result)
3171                 {
3172                         if(current != autos->default_auto)
3173                         {
3174                                 if(!draw)
3175                                 {
3176                                         if(track->record)
3177                                         {
3178                                                 result = test_auto(current,
3179                                                         (int)ax2,
3180                                                         (int)ay2,
3181                                                         (int)center_pixel,
3182                                                         (int)yscale,
3183                                                         cursor_x,
3184                                                         cursor_y,
3185                                                         buttonpress);
3186                                                 if (result)
3187                                                         auto_instance = current;
3188                                         }
3189                                 }
3190                                 else
3191                                         draw_auto(current,
3192                                                 (int)ax2 + x_offset,
3193                                                 (int)ay2 + y_offset,
3194                                                 (int)center_pixel,
3195                                                 (int)yscale);
3196                         }
3197
3198                         current = NEXT;
3199                 }
3200
3201                 if(!draw)
3202                 {
3203                         if(!result)
3204                         {
3205                                 if(track->record /* && buttonpress != 3 */)
3206                                 {
3207                                         result = test_toggleline(autos,
3208                                                 center_pixel,
3209                                                 (int)ax,
3210                                                 (int)ay,
3211                                                 (int)ax2,
3212                                                 (int)ay2,
3213                                                 cursor_x,
3214                                                 cursor_y,
3215                                                 buttonpress);
3216                                 }
3217                         }
3218                 }
3219                 else
3220                         draw_toggleline(center_pixel + y_offset,
3221                                 (int)ax,
3222                                 (int)ay,
3223                                 (int)ax2,
3224                                 (int)ay2);
3225
3226                 ax = ax2;
3227                 ay = ay2;
3228         }while(current && current->position <= unit_end && !result);
3229
3230         if(ax < get_w() && !result)
3231         {
3232                 ax2 = get_w();
3233                 ay2 = ay;
3234                 if(!draw)
3235                 {
3236                         if(track->record /* && buttonpress != 3 */)
3237                         {
3238                                 result = test_toggleline(autos,
3239                                         center_pixel,
3240                                         (int)ax,
3241                                         (int)ay,
3242                                         (int)ax2,
3243                                         (int)ay2,
3244                                         cursor_x,
3245                                         cursor_y,
3246                                         buttonpress);
3247                         }
3248                 }
3249                 else
3250                         draw_toggleline(center_pixel + y_offset,
3251                                 (int)ax,
3252                                 (int)ay,
3253                                 (int)ax2,
3254                                 (int)ay2);
3255         }
3256         return result;
3257 }
3258
3259 int TrackCanvas::do_autos(Track *track,
3260                 Autos *autos,
3261                 int cursor_x,
3262                 int cursor_y,
3263                 int draw,
3264                 int buttonpress,
3265                 BC_Pixmap *pixmap,
3266                 Auto * &auto_instance,
3267                 int &rerender)
3268 {
3269         int result = 0;
3270
3271         double view_start;
3272         double unit_start;
3273         double view_end;
3274         double unit_end;
3275         double yscale;
3276         int center_pixel;
3277         double zoom_sample;
3278         double zoom_units;
3279
3280         calculate_viewport(track,
3281                 view_start,
3282                 unit_start,
3283                 view_end,
3284                 unit_end,
3285                 yscale,
3286                 center_pixel,
3287                 zoom_sample,
3288                 zoom_units);
3289
3290         Auto *current;
3291         auto_instance = 0;
3292
3293         for(current = autos->first; current && !result; current = NEXT)
3294         {
3295                 if(current->position >= unit_start && current->position < unit_end)
3296                 {
3297                         int64_t x, y;
3298                         x = (int64_t)((double)(current->position - unit_start) /
3299                                 zoom_units - (pixmap->get_w() / 2.0 + 0.5));
3300                         y = center_pixel - pixmap->get_h() / 2;
3301
3302                         if(!draw)
3303                         {
3304                                 if(cursor_x >= x && cursor_y >= y &&
3305                                         cursor_x < x + pixmap->get_w() &&
3306                                         cursor_y < y + pixmap->get_h())
3307                                 {
3308                                         result = 1;
3309                                         auto_instance = current;
3310
3311                                         if(buttonpress && (buttonpress != 3))
3312                                         {
3313                                                 mwindow->session->drag_auto = current;
3314                                                 mwindow->session->drag_start_position = current->position;
3315                                                 mwindow->session->drag_origin_x = cursor_x;
3316                                                 mwindow->session->drag_origin_y = cursor_y;
3317
3318                                                 double position = autos->track->from_units(current->position);
3319                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3320                                                         mwindow->edl->local_session->get_selectionend(1)) /
3321                                                         2;
3322
3323                                                 if(!shift_down())
3324                                                 {
3325                                                         mwindow->edl->local_session->set_selectionstart(position);
3326                                                         mwindow->edl->local_session->set_selectionend(position);
3327                                                 }
3328                                                 else
3329                                                 if(position < center)
3330                                                 {
3331                                                         mwindow->edl->local_session->set_selectionstart(position);
3332                                                 }
3333                                                 else
3334                                                         mwindow->edl->local_session->set_selectionend(position);
3335
3336                                                 rerender = 1;
3337                                         }
3338                                 }
3339                         }
3340                         else
3341                                 draw_pixmap(pixmap, x, y);
3342                 }
3343         }
3344
3345
3346         return result;
3347 }
3348
3349 // so this means it is always >0 when keyframe is found
3350 int TrackCanvas::do_plugin_autos(Track *track, int cursor_x, int cursor_y,
3351                 int draw, int buttonpress, Plugin* &keyframe_plugin,
3352                 KeyFrame* &keyframe_instance)
3353 {
3354         int result = 0;
3355
3356         double view_start;
3357         double unit_start;
3358         double view_end;
3359         double unit_end;
3360         double yscale;
3361         int center_pixel;
3362         double zoom_sample;
3363         double zoom_units;
3364
3365         if(!track->expand_view) return 0;
3366
3367         calculate_viewport(track,
3368                 view_start,
3369                 unit_start,
3370                 view_end,
3371                 unit_end,
3372                 yscale,
3373                 center_pixel,
3374                 zoom_sample,
3375                 zoom_units);
3376
3377
3378
3379         for(int i = 0; i < track->plugin_set.total && !result; i++)
3380         {
3381                 PluginSet *plugin_set = track->plugin_set.values[i];
3382                 int center_pixel = track->y_pixel -
3383                         mwindow->edl->local_session->track_start[pane->number];
3384                 if( track->show_titles() )
3385                         center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
3386                 if( track->show_assets() )
3387                         center_pixel += resource_h();
3388                 center_pixel += (i + 0.5) * mwindow->theme->get_image("plugin_bg_data")->get_h();
3389
3390                 for(Plugin *plugin = (Plugin*)plugin_set->first;
3391                         plugin && !result;
3392                         plugin = (Plugin*)plugin->next)
3393                 {
3394                         for(KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
3395                                 keyframe && !result;
3396                                 keyframe = (KeyFrame*)keyframe->next)
3397                         {
3398 //printf("TrackCanvas::draw_plugin_autos 3 %d\n", keyframe->position);
3399                                 if(keyframe->position >= unit_start && keyframe->position < unit_end)
3400                                 {
3401                                         int64_t x = (int64_t)((keyframe->position - unit_start) / zoom_units);
3402                                         int y = center_pixel - keyframe_pixmap->get_h() / 2;
3403
3404 //printf("TrackCanvas::draw_plugin_autos 4 %d %d\n", x, center_pixel);
3405                                         if(!draw)
3406                                         {
3407                                                 if(cursor_x >= x && cursor_y >= y &&
3408                                                         cursor_x < x + keyframe_pixmap->get_w() &&
3409                                                         cursor_y < y + keyframe_pixmap->get_h())
3410                                                 {
3411                                                         result = 1;
3412                                                         keyframe_plugin = plugin;
3413                                                         keyframe_instance = keyframe;
3414
3415                                                         if(buttonpress)
3416                                                         {
3417                                                                 mwindow->session->drag_auto = keyframe;
3418                                                                 mwindow->session->drag_start_position = keyframe->position;
3419                                                                 mwindow->session->drag_origin_x = cursor_x;
3420                                                                 mwindow->session->drag_origin_y = cursor_y;
3421
3422                                                                 double position = track->from_units(keyframe->position);
3423                                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3424                                                                         mwindow->edl->local_session->get_selectionend(1)) /
3425                                                                         2;
3426
3427                                                                 if(!shift_down())
3428                                                                 {
3429                                                                         mwindow->edl->local_session->set_selectionstart(position);
3430                                                                         mwindow->edl->local_session->set_selectionend(position);
3431                                                                 }
3432                                                                 else
3433                                                                 if(position < center)
3434                                                                 {
3435                                                                         mwindow->edl->local_session->set_selectionstart(position);
3436                                                                 }
3437                                                                 else
3438                                                                         mwindow->edl->local_session->set_selectionend(position);
3439                                                         }
3440                                                 }
3441                                         }
3442                                         else
3443                                                 draw_pixmap(keyframe_pixmap,
3444                                                         x,
3445                                                         y);
3446                                 }
3447                         }
3448                 }
3449         }
3450
3451
3452
3453 //      if(buttonpress && buttonpress != 3 && result)
3454 //      {
3455 //              mwindow->undo->update_undo_before();
3456 //      }
3457
3458         return result;
3459 }
3460
3461 int TrackCanvas::draw_hairline(Auto *auto_keyframe, int color, int show)
3462 {
3463         Track *track = auto_keyframe->autos->track;
3464         int autogrouptype = auto_keyframe->autos->autogrouptype;
3465
3466         int center_pixel;
3467         double view_start, unit_start;
3468         double view_end, unit_end, yscale;
3469         double zoom_sample, zoom_units;
3470
3471         calculate_viewport(track, view_start, unit_start, view_end, unit_end,
3472                         yscale, center_pixel, zoom_sample, zoom_units);
3473
3474         double ax = 0, ay = 0;
3475         calculate_auto_position(&ax, &ay, 0, 0, 0, 0,
3476                 auto_keyframe, unit_start, zoom_units, yscale, autogrouptype);
3477
3478         set_color(color);
3479         draw_line(ax, 0, ax, get_h());
3480
3481         if( show ) {
3482                 char text[BCSTRLEN];
3483                 if( auto_keyframe->is_floatauto() ) {
3484                         FloatAuto *float_auto = (FloatAuto *)auto_keyframe;
3485                         sprintf(text, "%0.2f", float_auto->get_value());
3486                 }
3487                 else {
3488                         IntAuto *int_auto = (IntAuto *)auto_keyframe;
3489                         sprintf(text, "%d", int_auto->value);
3490                 }
3491                 int font = MEDIUMFONT;
3492                 int tw = get_text_width(font, text)  + TOOLTIP_MARGIN * 2;
3493                 int th = get_text_height(font, text) + TOOLTIP_MARGIN * 2;
3494                 set_color(get_resources()->tooltip_bg_color);
3495                 ax += HANDLE_W/2;
3496                 ay += center_pixel + HANDLE_W/2;
3497                 draw_box(ax, ay, tw, th);
3498                 set_color(BLACK);
3499                 draw_rectangle(ax, ay, tw, th);
3500                 set_font(font);
3501                 ax += TOOLTIP_MARGIN;
3502                 ay += TOOLTIP_MARGIN + get_text_ascent(font);
3503                 draw_text(ax, ay, text);
3504         }
3505         return 0;
3506 }
3507
3508 void TrackCanvas::draw_overlays()
3509 {
3510         int new_cursor, update_cursor, rerender;
3511
3512 // Move background pixmap to foreground pixmap
3513         draw_pixmap(background_pixmap,
3514                 0,
3515                 0,
3516                 get_w(),
3517                 get_h(),
3518                 0,
3519                 0);
3520
3521 // In/Out points
3522         draw_inout_points();
3523
3524 // Transitions
3525         draw_transitions();
3526
3527 // Plugins
3528         draw_plugins();
3529         draw_hard_edges();
3530
3531 // Loop points
3532         draw_loop_points();
3533         draw_brender_range();
3534
3535 // Highlighted areas
3536         draw_highlighting();
3537
3538 // Automation
3539         do_keyframes(0, 0, 1, 0, new_cursor, update_cursor, rerender);
3540
3541 // Selection cursor
3542         if(pane->cursor) pane->cursor->restore(1);
3543
3544 // Handle dragging
3545         draw_drag_handle();
3546
3547 // Playback cursor
3548         draw_playback_cursor();
3549
3550         draw_keyframe_reticle();
3551
3552         show_window(0);
3553 }
3554
3555 int TrackCanvas::activate()
3556 {
3557         if(!active)
3558         {
3559 //printf("TrackCanvas::activate %d %d\n", __LINE__, pane->number);
3560 //BC_Signals::dump_stack();
3561                 get_top_level()->deactivate();
3562                 active = 1;
3563                 set_active_subwindow(this);
3564                 pane->cursor->activate();
3565                 gui->focused_pane = pane->number;
3566         }
3567         return 0;
3568 }
3569
3570 int TrackCanvas::deactivate()
3571 {
3572         if(active)
3573         {
3574                 active = 0;
3575                 pane->cursor->deactivate();
3576         }
3577         return 0;
3578 }
3579
3580
3581 void TrackCanvas::update_drag_handle()
3582 {
3583         double new_position;
3584         int cursor_x = get_cursor_x();
3585
3586         new_position =
3587                 (double)(cursor_x +
3588                 mwindow->edl->local_session->view_start[pane->number]) *
3589                 mwindow->edl->local_session->zoom_sample /
3590                 mwindow->edl->session->sample_rate;
3591
3592         new_position =
3593                 mwindow->edl->align_to_frame(new_position, 0);
3594
3595         if( ctrl_down() && alt_down() ) {
3596 #define snapper(v) do { \
3597         double pos = (v); \
3598         if( pos < 0 ) break; \
3599         double dist = fabs(new_position - pos); \
3600         if( dist >= snap_min ) break; \
3601         snap_position = pos;  snap_min = dist; \
3602 } while(0)
3603                 double snap_position = new_position;
3604                 double snap_min = DBL_MAX;
3605                 if( mwindow->edl->local_session->inpoint_valid() )
3606                         snapper(mwindow->edl->local_session->get_inpoint());
3607                 if( mwindow->edl->local_session->outpoint_valid() )
3608                         snapper(mwindow->edl->local_session->get_outpoint());
3609                 snapper(mwindow->edl->prev_edit(new_position));
3610                 snapper(mwindow->edl->next_edit(new_position));
3611                 Label *prev_label = mwindow->edl->labels->prev_label(new_position);
3612                 if( prev_label ) snapper(prev_label->position);
3613                 Label *next_label = mwindow->edl->labels->next_label(new_position);
3614                 if( next_label ) snapper(next_label->position);
3615                 int snap_x = snap_position * mwindow->edl->session->sample_rate /
3616                         mwindow->edl->local_session->zoom_sample -
3617                         mwindow->edl->local_session->view_start[pane->number];
3618                 if( abs(snap_x - cursor_x) < HANDLE_W ) {
3619                         snapped = 1;
3620                         new_position = snap_position;
3621                 }
3622 #undef snapper
3623         }
3624
3625         if(new_position != mwindow->session->drag_position)
3626         {
3627                 mwindow->session->drag_position = new_position;
3628                 gui->mainclock->update(new_position);
3629                 timebar_position = new_position;
3630                 gui->update_timebar(0);
3631
3632                 EDL *edl = new EDL;
3633                 edl->create_objects();
3634                 edl->copy_all(mwindow->edl);
3635                 MainSession *session = mwindow->session;
3636                 int edit_mode = mwindow->edl->session->edit_handle_mode[session->drag_button];
3637                 edl->modify_edithandles(session->drag_start,
3638                         session->drag_position,
3639                         session->drag_handle,
3640                         edit_mode,
3641                         edl->session->labels_follow_edits,
3642                         edl->session->plugins_follow_edits,
3643                         edl->session->autos_follow_edits);
3644                 double position = edit_mode != MOVE_NO_EDITS &&
3645                         ( session->drag_handle || edit_mode == MOVE_ONE_EDIT ) ?
3646                                 session->drag_position : session->drag_start;
3647                 Track *track = session->drag_handle_track();
3648                 int64_t pos = track->to_units(position, 0);
3649                 render_handle_frame(edl, pos, shift_down() ? 0 :
3650                         session->drag_handle ? 1 : 2);
3651                 edl->remove_user();
3652         }
3653 }
3654
3655 int TrackCanvas::render_handle_frame(EDL *edl, int64_t pos, int mode)
3656 {
3657         int result = 0;
3658         int64_t left = pos-1;
3659         if( left < 0 ) left = 0;
3660         switch( mode ) {
3661         case 0: {
3662                 VFrame vlt(edl->get_w(), edl->get_h(), edl->session->color_model);
3663                 VFrame vrt(edl->get_w(), edl->get_h(), edl->session->color_model);
3664                 TransportCommand command;
3665                 command.command = CURRENT_FRAME;
3666                 command.get_edl()->copy_all((EDL *)edl);
3667                 command.change_type = CHANGE_ALL;
3668                 command.realtime = 0;
3669                 Preferences *preferences = mwindow->preferences;
3670                 RenderEngine *render_engine = new RenderEngine(0, preferences, 0, 0);
3671                 CICache *video_cache = new CICache(preferences);
3672                 render_engine->set_vcache(video_cache);
3673                 render_engine->arm_command(&command);
3674                 int64_t left = pos-1;
3675                 if( left < 0 ) left = 0;
3676                 VRender *vrender = render_engine->vrender;
3677                 result = vrender &&
3678                         !vrender->process_buffer(&vlt, left, 0) &&
3679                         !vrender->process_buffer(&vrt, pos , 0) ? 0 : 1;
3680                 delete render_engine;
3681                 delete video_cache;
3682                 mwindow->cwindow->gui->lock_window("TrackCanvas::render_handle_frame 0");
3683                 Canvas *canvas = mwindow->cwindow->gui->canvas;
3684                 canvas->lock_canvas("TrackCanvas::render_handle_frame 1");
3685                 int w = canvas->w, h = canvas->h, w2 = w/2, h2 = h/2;
3686                 int lx = 0, ly = h2/2, rx = w2, ry = h2/2;
3687                 BC_WindowBase *window = canvas->get_canvas();
3688                 window->set_color(BLACK);
3689                 window->clear_box(0,0, window->get_w(),window->get_h());
3690                 window->draw_vframe(&vlt, lx,ly, w2,h2, 0,0,vlt.get_w(),vlt.get_h());
3691                 window->draw_vframe(&vrt, rx,ry, w2,h2, 0,0,vrt.get_w(),vrt.get_h());
3692                 window->flash(1);
3693                 canvas->unlock_canvas();
3694                 mwindow->cwindow->gui->unlock_window();
3695                 break; }
3696         case 1:
3697         case 2: {
3698                 Track *track = mwindow->session->drag_handle_track();
3699                 double position = track->from_units(mode == 1 ? left : pos);
3700                 if( position < 0 ) position = 0;
3701                 edl->local_session->set_selectionstart(position);
3702                 edl->local_session->set_selectionend(position);
3703                 PlaybackEngine *playback_engine = mwindow->cwindow->playback_engine;
3704                 if( playback_engine->is_playing_back )
3705                         playback_engine->stop_playback(1);
3706                 mwindow->cwindow->playback_engine->refresh_frame(CHANGE_EDL, edl, 0);
3707                 break; }
3708         }
3709         return result;
3710 }
3711
3712 int TrackCanvas::update_drag_edit()
3713 {
3714         int result = 0;
3715
3716
3717
3718         return result;
3719 }
3720
3721 int TrackCanvas::get_drag_values(float *percentage,
3722         int64_t *position,
3723         int do_clamp,
3724         int cursor_x,
3725         int cursor_y,
3726         Auto *current)
3727 {
3728         //int x = cursor_x - mwindow->session->drag_origin_x;
3729         //int y = cursor_y - mwindow->session->drag_origin_y;
3730         *percentage = 0;
3731         *position = 0;
3732
3733         if(!current->autos->track->record) return 1;
3734         double view_start;
3735         double unit_start;
3736         double view_end;
3737         double unit_end;
3738         double yscale;
3739         int center_pixel;
3740         double zoom_sample;
3741         double zoom_units;
3742
3743         calculate_viewport(current->autos->track,
3744                 view_start,
3745                 unit_start,
3746                 view_end,
3747                 unit_end,
3748                 yscale,
3749                 center_pixel,
3750                 zoom_sample,
3751                 zoom_units);
3752
3753         *percentage = (float)(mwindow->session->drag_origin_y - cursor_y) /
3754                 yscale +
3755                 mwindow->session->drag_start_percentage;
3756         if(do_clamp) CLAMP(*percentage, 0, 1);
3757
3758         *position = Units::to_int64(zoom_units *
3759                 (cursor_x - mwindow->session->drag_origin_x) +
3760                 mwindow->session->drag_start_position + 0.5);
3761
3762         if((do_clamp) && *position < 0) *position = 0;
3763         return 0;
3764 }
3765
3766
3767 #define UPDATE_DRAG_HEAD(do_clamp) \
3768         int result = 0, center_pixel; \
3769         if(!current->autos->track->record) return 0; \
3770         double view_start, unit_start, view_end, unit_end; \
3771         double yscale, zoom_sample, zoom_units; \
3772  \
3773         calculate_viewport(current->autos->track,  \
3774                 view_start, unit_start, view_end, unit_end, \
3775                 yscale, center_pixel, zoom_sample, zoom_units); \
3776  \
3777         float percentage = (float)(mwindow->session->drag_origin_y - cursor_y) / \
3778                 yscale +  mwindow->session->drag_start_percentage; \
3779         if(do_clamp) CLAMP(percentage, 0, 1); \
3780  \
3781         int64_t position = Units::to_int64(zoom_units * \
3782                 (cursor_x - mwindow->session->drag_origin_x) + \
3783                 mwindow->session->drag_start_position); \
3784         if((do_clamp) && position < 0) position = 0;
3785
3786
3787 int TrackCanvas::update_drag_floatauto(int cursor_x, int cursor_y)
3788 {
3789         FloatAuto *current = (FloatAuto*)mwindow->session->drag_auto;
3790
3791         UPDATE_DRAG_HEAD(mwindow->session->drag_handle == 0);
3792         int x = cursor_x - mwindow->session->drag_origin_x;
3793         int y = cursor_y - mwindow->session->drag_origin_y;
3794         float value, old_value;
3795
3796         if( mwindow->session->drag_handle == 0 && (ctrl_down() && !shift_down()) ) {
3797 // not really editing the node, rather start editing the curve
3798 // tangent is editable and drag movement is significant
3799                 if( (FloatAuto::FREE == current->curve_mode ||
3800                      FloatAuto::TFREE==current->curve_mode) &&
3801                     (fabs(x) > HANDLE_W / 2 || fabs(y) > HANDLE_W / 2))
3802                         mwindow->session->drag_handle = x < 0 ? 1 : 2;
3803         }
3804
3805         switch(mwindow->session->drag_handle) {
3806         case 0: // Center
3807 // Snap to nearby values
3808                 old_value = current->get_value();
3809                 if(shift_down()) {
3810                         double value1, value2, distance1, distance2;
3811
3812                         if(current->previous) {
3813                                 int autogrouptype = current->previous->autos->autogrouptype;
3814                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3815                                 value1 = ((FloatAuto*)current->previous)->get_value();
3816                                 distance1 = fabs(value - value1);
3817                                 current->set_value(value1);
3818                         }
3819
3820                         if(current->next) {
3821                                 int autogrouptype = current->next->autos->autogrouptype;
3822                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3823                                 value2 = ((FloatAuto*)current->next)->get_value();
3824                                 distance2 = fabs(value - value2);
3825                                 if(!current->previous || distance2 < distance1) {
3826                                         current->set_value(value2);
3827                                 }
3828                         }
3829
3830                         if(!current->previous && !current->next) {
3831                                 current->set_value( ((FloatAutos*)current->autos)->default_);
3832                         }
3833                         value = current->get_value();
3834                 }
3835                 else {
3836                         int autogrouptype = current->autos->autogrouptype;
3837                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
3838                 }
3839
3840                 if(alt_down() && !shift_down())
3841 // ALT constrains movement: fixed position, only changing the value
3842                         position = mwindow->session->drag_start_position;
3843
3844                 if(value != old_value || position != current->position) {
3845                         result = 1;
3846                         float change = value - old_value;
3847                         current->adjust_to_new_coordinates(position, value);
3848                         update_ganged_autos(change, current->autos->track, current);
3849                         show_message(current, 1,", %.2f", current->get_value());
3850                 }
3851                 break;
3852
3853 // In control
3854         case 1: {
3855                 int autogrouptype = current->autos->autogrouptype;
3856                 value = percentage_to_value(percentage, 0, current, autogrouptype);
3857                 if(value != current->get_control_in_value())
3858                 {
3859                         result = 1;
3860                         // note: (position,value) need not be at the location of the ctrl point,
3861                         // but could be somewhere in between on the curve (or even outward or
3862                         // on the opposit side). We set the new control point such as
3863                         // to point the curve through (position,value)
3864                         current->set_control_in_value(
3865                                 value * levered_position(position - current->position,
3866                                                          current->get_control_in_position()));
3867                         update_ganged_autos(0, current->autos->track, current);
3868                         show_message(current, 1,", %.2f", current->get_control_in_value());
3869                 }
3870                 break; }
3871
3872 // Out control
3873         case 2: {
3874                 int autogrouptype = current->autos->autogrouptype;
3875                 value = percentage_to_value(percentage, 0, current, autogrouptype);
3876                 if(value != current->get_control_out_value()) {
3877                         result = 1;
3878                         current->set_control_out_value(
3879                                 value * levered_position(position - current->position,
3880                                                          current->get_control_out_position()));
3881                         update_ganged_autos(0, current->autos->track, current);
3882                         show_message(current, 1,", %.2f", current->get_control_out_value());
3883                 }
3884                 break; }
3885         }
3886
3887         return result;
3888 }
3889
3890 int TrackCanvas::update_drag_toggleauto(int cursor_x, int cursor_y)
3891 {
3892         IntAuto *current = (IntAuto*)mwindow->session->drag_auto;
3893
3894         UPDATE_DRAG_HEAD(1);
3895         int value = (int)percentage_to_value(percentage, 1, 0, AUTOGROUPTYPE_INT255);
3896
3897         if(value != current->value || position != current->position)
3898         {
3899                 result = 1;
3900                 current->value = value;
3901                 current->position = position;
3902                 show_message(current, 0,", %d", current->value);
3903         }
3904
3905         return result;
3906 }
3907
3908 // Autos which can't change value through dragging.
3909
3910 int TrackCanvas::update_drag_auto(int cursor_x, int cursor_y)
3911 {
3912         Auto *current = (Auto*)mwindow->session->drag_auto;
3913
3914         UPDATE_DRAG_HEAD(1)
3915         if(position != current->position)
3916         {
3917                 result = 1;
3918                 current->position = position;
3919                 show_message(current, 0,"");
3920
3921                 double position_f = current->autos->track->from_units(current->position);
3922                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3923                         mwindow->edl->local_session->get_selectionend(1)) /
3924                         2;
3925                 if(!shift_down())
3926                 {
3927                         mwindow->edl->local_session->set_selectionstart(position_f);
3928                         mwindow->edl->local_session->set_selectionend(position_f);
3929                 }
3930                 else
3931                 if(position_f < center_f)
3932                 {
3933                         mwindow->edl->local_session->set_selectionstart(position_f);
3934                 }
3935                 else
3936                         mwindow->edl->local_session->set_selectionend(position_f);
3937         }
3938
3939
3940         return result;
3941 }
3942
3943 int TrackCanvas::update_drag_pluginauto(int cursor_x, int cursor_y)
3944 {
3945         KeyFrame *current = (KeyFrame*)mwindow->session->drag_auto;
3946
3947         UPDATE_DRAG_HEAD(1)
3948         if(position != current->position)
3949         {
3950 //      printf("uida: autos: %p, track: %p ta: %p\n", current->autos, current->autos->track, current->autos->track->automation);
3951                 Track *track = current->autos->track;
3952                 //PluginAutos *pluginautos = (PluginAutos *)current->autos;
3953                 PluginSet *pluginset;
3954                 Plugin *plugin = 0;
3955 // figure out the correct pluginset & correct plugin
3956                 int found = 0;
3957                 for(int i = 0; i < track->plugin_set.total; i++)
3958                 {
3959                         pluginset = track->plugin_set.values[i];
3960                         for(plugin = (Plugin *)pluginset->first; plugin; plugin = (Plugin *)plugin->next)
3961                         {
3962                                 KeyFrames *keyframes = plugin->keyframes;
3963                                 for(KeyFrame *currentkeyframe = (KeyFrame *)keyframes->first;
3964                                         currentkeyframe;
3965                                         currentkeyframe = (KeyFrame *) currentkeyframe->next)
3966                                 {
3967                                         if (currentkeyframe == current)
3968                                         {
3969                                                 found = 1;
3970                                                 break;
3971                                         }
3972
3973                                 }
3974                                 if (found) break;
3975                         }
3976                         if (found) break;
3977                 }
3978
3979                 mwindow->session->plugin_highlighted = plugin;
3980                 mwindow->session->track_highlighted = track;
3981                 result = 1;
3982                 current->position = position;
3983                 show_message(current, 0,"");
3984
3985                 double position_f = current->autos->track->from_units(current->position);
3986                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3987                         mwindow->edl->local_session->get_selectionend(1)) /
3988                         2;
3989                 if(!shift_down())
3990                 {
3991                         mwindow->edl->local_session->set_selectionstart(position_f);
3992                         mwindow->edl->local_session->set_selectionend(position_f);
3993                 }
3994                 else
3995                 if(position_f < center_f)
3996                 {
3997                         mwindow->edl->local_session->set_selectionstart(position_f);
3998                 }
3999                 else
4000                         mwindow->edl->local_session->set_selectionend(position_f);
4001         }
4002
4003
4004         return result;
4005 }
4006
4007 void TrackCanvas::update_drag_caption()
4008 {
4009         switch(mwindow->session->current_operation)
4010         {
4011                 case DRAG_FADE:
4012
4013                         break;
4014         }
4015 }
4016
4017
4018
4019 int TrackCanvas::cursor_update(int in_motion)
4020 {
4021         int result = 0;
4022         int cursor_x = 0;
4023         int cursor_y = 0;
4024         int update_clock = 0;
4025         int update_zoom = 0;
4026         int update_scroll = 0;
4027         int update_overlay = 0;
4028         int update_cursor = 0;
4029         int new_cursor = 0;
4030         int rerender = 0;
4031         double position = 0.;
4032 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
4033
4034 // Default cursor
4035         switch(mwindow->edl->session->editing_mode)
4036         {
4037                 case EDITING_ARROW: new_cursor = ARROW_CURSOR; break;
4038                 case EDITING_IBEAM: new_cursor = IBEAM_CURSOR; break;
4039         }
4040
4041         switch(mwindow->session->current_operation)
4042         {
4043                 case DRAG_EDITHANDLE1:
4044 // Outside threshold.  Upgrade status
4045                         if(active)
4046                         {
4047                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
4048                                 {
4049                                         mwindow->session->current_operation = DRAG_EDITHANDLE2;
4050                                         update_overlay = 1;
4051                                 }
4052                         }
4053                         break;
4054
4055                 case DRAG_EDITHANDLE2:
4056                         if(active)
4057                         {
4058                                 update_drag_handle();
4059                                 update_overlay = 1;
4060                         }
4061                         break;
4062
4063                 case DRAG_PLUGINHANDLE1:
4064                         if(active)
4065                         {
4066                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
4067                                 {
4068                                         mwindow->session->current_operation = DRAG_PLUGINHANDLE2;
4069                                         update_overlay = 1;
4070                                 }
4071                         }
4072                         break;
4073
4074                 case DRAG_PLUGINHANDLE2:
4075                         if(active)
4076                         {
4077                                 update_drag_handle();
4078                                 update_overlay = 1;
4079                         }
4080                         break;
4081
4082 // Rubber band curves
4083                 case DRAG_FADE:
4084                 case DRAG_SPEED:
4085                 case DRAG_CZOOM:
4086                 case DRAG_PZOOM:
4087                 case DRAG_CAMERA_X:
4088                 case DRAG_CAMERA_Y:
4089                 case DRAG_CAMERA_Z:
4090                 case DRAG_PROJECTOR_X:
4091                 case DRAG_PROJECTOR_Y:
4092                 case DRAG_PROJECTOR_Z:
4093                         if(active) rerender = update_overlay =
4094                                 update_drag_floatauto(get_cursor_x(), get_cursor_y());
4095                         if( rerender && mwindow->session->current_operation == DRAG_SPEED )
4096                                 mwindow->speed_after(!in_motion ? 1 : 0);
4097                         break;
4098
4099                 case DRAG_PLAY:
4100                         if(active) rerender = update_overlay =
4101                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4102                         break;
4103
4104                 case DRAG_MUTE:
4105                         if(active) rerender = update_overlay =
4106                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4107                         break;
4108
4109 // Keyframe icons are sticky
4110                 case DRAG_PAN_PRE:
4111                 case DRAG_MASK_PRE:
4112                 case DRAG_MODE_PRE:
4113                 case DRAG_PLUGINKEY_PRE:
4114                         if(active) {
4115                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W) {
4116                                         mwindow->session->current_operation++;
4117                                         update_overlay = 1;
4118
4119                                         mwindow->undo->update_undo_before();
4120                                 }
4121                         }
4122                         break;
4123
4124                 case DRAG_PAN:
4125                 case DRAG_MASK:
4126                 case DRAG_MODE:
4127                         if(active) rerender = update_overlay =
4128                                 update_drag_auto(get_cursor_x(), get_cursor_y());
4129                         break;
4130
4131                 case DRAG_PLUGINKEY:
4132                         if(active) rerender = update_overlay =
4133                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4134                         break;
4135
4136                 case SELECT_REGION:
4137                         if(active) {
4138                                 cursor_x = get_cursor_x();
4139                                 cursor_y = get_cursor_y();
4140                                 position = (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
4141                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
4142
4143                                 position = mwindow->edl->align_to_frame(position, 0);
4144                                 position = MAX(position, 0);
4145
4146                                 double start = mwindow->edl->local_session->get_selectionstart(1);
4147                                 double end = mwindow->edl->local_session->get_selectionend(1);
4148                                 if(position < selection_midpoint) {
4149                                         mwindow->edl->local_session->set_selectionend(selection_midpoint);
4150                                         mwindow->edl->local_session->set_selectionstart(position);
4151                                 }
4152                                 else {
4153                                         mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4154                                         mwindow->edl->local_session->set_selectionend(position);
4155                                 }
4156         // Que the CWindow
4157                                 gui->unlock_window();
4158                                 int dir =
4159                                         start != mwindow->edl->local_session->get_selectionstart(1) ? 1 :
4160                                         end != mwindow->edl->local_session->get_selectionend(1) ? -1 : 0;
4161                                 mwindow->cwindow->update(dir, 0, 0, 0, 1);
4162                                 gui->lock_window("TrackCanvas::cursor_update 1");
4163         // Update the faders
4164                                 mwindow->update_plugin_guis();
4165                                 gui->update_patchbay();
4166
4167                                 timebar_position = mwindow->edl->local_session->get_selectionend(1);
4168
4169                                 gui->hide_cursor(0);
4170                                 gui->draw_cursor(1);
4171                                 gui->update_timebar(0);
4172                                 gui->flash_canvas(1);
4173                                 result = 1;
4174                                 update_clock = 1;
4175                                 update_zoom = 1;
4176                                 update_scroll = 1;
4177                         }
4178                         break;
4179
4180                 default:
4181                         if(is_event_win() && cursor_inside()) {
4182 // Update clocks
4183                                 cursor_x = get_cursor_x();
4184                                 position = (double)cursor_x *
4185                                         (double)mwindow->edl->local_session->zoom_sample /
4186                                         (double)mwindow->edl->session->sample_rate +
4187                                         (double)mwindow->edl->local_session->view_start[pane->number] *
4188                                         (double)mwindow->edl->local_session->zoom_sample /
4189                                         (double)mwindow->edl->session->sample_rate;
4190                                 position = mwindow->edl->align_to_frame(position, 0);
4191                                 update_clock = 1;
4192
4193 // set all timebars
4194                                 for(int i = 0; i < TOTAL_PANES; i++)
4195                                         if(gui->pane[i]) gui->pane[i]->canvas->timebar_position = position;
4196
4197 //printf("TrackCanvas::cursor_update %d %d %p %p\n", __LINE__, pane->number, pane, pane->timebar);
4198                                 gui->update_timebar(0);
4199 // Update cursor
4200                                 if(do_transitions(get_cursor_x(), get_cursor_y(),
4201                                                 0, new_cursor, update_cursor)) break;
4202                                 if(do_keyframes(get_cursor_x(), get_cursor_y(),
4203                                         0, 0, new_cursor, update_cursor, rerender)) break;
4204                                 if(do_edit_handles(get_cursor_x(), get_cursor_y(),
4205                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4206 // Plugin boundaries
4207                                 if(do_plugin_handles(get_cursor_x(), get_cursor_y(),
4208                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4209                                 if(do_edits(get_cursor_x(), get_cursor_y(),
4210                                         0, 0, update_overlay, rerender, new_cursor, update_cursor)) break;
4211                         }
4212                         break;
4213         }
4214
4215 //printf("TrackCanvas::cursor_update 1\n");
4216         if(update_cursor && new_cursor != get_cursor())
4217         {
4218                 set_cursor(new_cursor, 0, 1);
4219         }
4220
4221 //printf("TrackCanvas::cursor_update 1 %d\n", rerender);
4222         if(rerender && render_timer->get_difference() > 0.25 ) {
4223                 render_timer->update();
4224                 mwindow->restart_brender();
4225                 mwindow->sync_parameters(CHANGE_PARAMS);
4226                 mwindow->update_plugin_guis();
4227                 gui->unlock_window();
4228                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4229                 gui->lock_window("TrackCanvas::cursor_update 2");
4230         }
4231         if(rerender) {
4232 // Update faders
4233                 gui->update_patchbay();
4234         }
4235
4236
4237         if(update_clock) {
4238                 if(!mwindow->cwindow->playback_engine->is_playing_back)
4239                         gui->mainclock->update(position);
4240         }
4241
4242         if(update_zoom) {
4243                 gui->zoombar->update();
4244         }
4245
4246         if(update_scroll) {
4247                 if(!drag_scroll &&
4248                         (cursor_x >= get_w() || cursor_x < 0 || cursor_y >= get_h() || cursor_y < 0))
4249                         start_dragscroll();
4250                 else
4251                 if(drag_scroll &&
4252                         (cursor_x < get_w() && cursor_x >= 0 && cursor_y < get_h() && cursor_y >= 0))
4253                         stop_dragscroll();
4254         }
4255
4256         if(update_overlay) {
4257                 gui->draw_overlays(1);
4258         }
4259
4260 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
4261         return result;
4262 }
4263
4264 int TrackCanvas::cursor_motion_event()
4265 {
4266         return cursor_update(1);
4267 }
4268
4269 void TrackCanvas::start_dragscroll()
4270 {
4271         if(!drag_scroll) {
4272                 drag_scroll = 1;
4273                 set_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4274 //printf("TrackCanvas::start_dragscroll 1\n");
4275         }
4276 }
4277
4278 void TrackCanvas::stop_dragscroll()
4279 {
4280         if(drag_scroll) {
4281                 drag_scroll = 0;
4282                 unset_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4283 //printf("TrackCanvas::stop_dragscroll 1\n");
4284         }
4285 }
4286
4287 int TrackCanvas::repeat_event(int64_t duration)
4288 {
4289         if(!drag_scroll) return 0;
4290         if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
4291
4292         int sample_movement = 0;
4293         int track_movement = 0;
4294         int64_t x_distance = 0;
4295         int64_t y_distance = 0;
4296         double position = 0;
4297         int result = 0;
4298
4299         switch(mwindow->session->current_operation) {
4300                 case SELECT_REGION:
4301 //printf("TrackCanvas::repeat_event 1 %d\n", mwindow->edl->local_session->view_start);
4302                 if(get_cursor_x() > get_w()) {
4303                                 x_distance = get_cursor_x() - get_w();
4304                                 sample_movement = 1;
4305                 }
4306                 else if(get_cursor_x() < 0) {
4307                         x_distance = get_cursor_x();
4308                         sample_movement = 1;
4309                 }
4310
4311                 if(get_cursor_y() > get_h()) {
4312                         y_distance = get_cursor_y() - get_h();
4313                         track_movement = 1;
4314                 }
4315                 else if(get_cursor_y() < 0) {
4316                         y_distance = get_cursor_y();
4317                         track_movement = 1;
4318                 }
4319                 result = 1;
4320                 break;
4321         }
4322
4323         if(sample_movement) {
4324                 position = (double)(get_cursor_x() +
4325                         mwindow->edl->local_session->view_start[pane->number] +
4326                         x_distance) *
4327                         mwindow->edl->local_session->zoom_sample /
4328                         mwindow->edl->session->sample_rate;
4329                 position = mwindow->edl->align_to_frame(position, 0);
4330                 position = MAX(position, 0);
4331
4332 //printf("TrackCanvas::repeat_event 1 %f\n", position);
4333                 switch(mwindow->session->current_operation) {
4334                 case SELECT_REGION:
4335                         if(position < selection_midpoint) {
4336                                 mwindow->edl->local_session->set_selectionend(selection_midpoint);
4337                                 mwindow->edl->local_session->set_selectionstart(position);
4338 // Que the CWindow
4339                                 gui->unlock_window();
4340                                 mwindow->cwindow->update(1, 0, 0);
4341                                 gui->lock_window("TrackCanvas::repeat_event");
4342 // Update the faders
4343                                 mwindow->update_plugin_guis();
4344                                 gui->update_patchbay();
4345                         }
4346                         else {
4347                                 mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4348                                 mwindow->edl->local_session->set_selectionend(position);
4349 // Don't que the CWindow
4350                         }
4351                         break;
4352                 }
4353
4354                 mwindow->samplemovement(
4355                         mwindow->edl->local_session->view_start[pane->number] + x_distance,
4356                         pane->number);
4357         }
4358
4359         if(track_movement) {
4360                 mwindow->trackmovement(y_distance, pane->number);
4361         }
4362
4363         return result;
4364 }
4365
4366 int TrackCanvas::button_release_event()
4367 {
4368         int redraw = -1, update_overlay = 0;
4369         int result = 0, load_flags = 0;
4370
4371 // printf("TrackCanvas::button_release_event %d\n",
4372 // mwindow->session->current_operation);
4373         if(active) {
4374                 switch(mwindow->session->current_operation) {
4375                 case DRAG_EDITHANDLE2:
4376                         mwindow->session->current_operation = NO_OPERATION;
4377                         drag_scroll = 0;
4378                         result = 1;
4379
4380                         end_edithandle_selection();
4381                         break;
4382
4383                 case DRAG_EDITHANDLE1:
4384                         mwindow->session->current_operation = NO_OPERATION;
4385                         drag_scroll = 0;
4386                         result = 1;
4387                         break;
4388
4389                 case DRAG_PLUGINHANDLE2:
4390                         mwindow->session->current_operation = NO_OPERATION;
4391                         drag_scroll = 0;
4392                         result = 1;
4393
4394                         end_pluginhandle_selection();
4395                         break;
4396
4397                 case DRAG_PLUGINHANDLE1:
4398                         mwindow->session->current_operation = NO_OPERATION;
4399                         drag_scroll = 0;
4400                         result = 1;
4401                         break;
4402
4403                 case DRAG_SPEED:
4404                         redraw = FORCE_REDRAW;
4405                         load_flags |= LOAD_EDITS;
4406                 case DRAG_FADE:
4407 // delete the drag_auto_gang first and remove out of order keys
4408                         clear_ganged_autos();
4409                 case DRAG_CZOOM:
4410                 case DRAG_PZOOM:
4411                 case DRAG_PLAY:
4412                 case DRAG_MUTE:
4413                 case DRAG_MASK:
4414                 case DRAG_MODE:
4415                 case DRAG_PAN:
4416                 case DRAG_CAMERA_X:
4417                 case DRAG_CAMERA_Y:
4418                 case DRAG_CAMERA_Z:
4419                 case DRAG_PROJECTOR_X:
4420                 case DRAG_PROJECTOR_Y:
4421                 case DRAG_PROJECTOR_Z:
4422                 case DRAG_PLUGINKEY:
4423                         load_flags |= LOAD_AUTOMATION;
4424                         mwindow->session->current_operation = NO_OPERATION;
4425                         mwindow->session->drag_handle = 0;
4426 // Remove any out-of-order keyframe
4427                         if(mwindow->session->drag_auto) {
4428                                 mwindow->session->drag_auto->autos->remove_nonsequential(
4429                                                 mwindow->session->drag_auto);
4430 //                              mwindow->session->drag_auto->autos->optimize();
4431                                 update_overlay = 1;
4432                         }
4433
4434                         mwindow->undo->update_undo_after(_("keyframe"), load_flags);
4435                         result = 1;
4436                         break;
4437
4438                 case DRAG_EDIT:
4439                 case DRAG_AEFFECT_COPY:
4440                 case DRAG_VEFFECT_COPY:
4441 // Trap in drag stop
4442                         break;
4443
4444
4445                 default:
4446                         if(mwindow->session->current_operation) {
4447 //                              if(mwindow->session->current_operation == SELECT_REGION) {
4448 //                                      mwindow->undo->update_undo_after(_("select"), LOAD_SESSION, 0, 0);
4449 //                              }
4450
4451                                 mwindow->session->current_operation = NO_OPERATION;
4452                                 drag_scroll = 0;
4453 // Traps button release events
4454 //                              result = 1;
4455                         }
4456                         break;
4457                 }
4458         }
4459
4460         if (result)
4461                 cursor_update(0);
4462
4463         if(update_overlay) {
4464                 gui->draw_overlays(1);
4465         }
4466         if(redraw >= 0) {
4467                 gui->draw_canvas(redraw, 0);
4468                 gui->flash_canvas(1);
4469         }
4470         return result;
4471 }
4472
4473 int TrackCanvas::do_edit_handles(int cursor_x, int cursor_y, int button_press,
4474         int &rerender, int &update_overlay, int &new_cursor, int &update_cursor)
4475 {
4476         Edit *edit_result = 0;
4477         int handle_result = -1;
4478         int result = 0;
4479
4480         for( Track *track=mwindow->edl->tracks->first; track && !result; track=track->next) {
4481                 for( Edit *edit=track->edits->first; edit && !result; edit=edit->next ) {
4482                         int64_t edit_x, edit_y, edit_w, edit_h;
4483                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4484
4485                         if( cursor_x >= edit_x && cursor_x <= edit_x + edit_w &&
4486                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h ) {
4487                                 if( cursor_x < edit_x + HANDLE_W ) {
4488                                         edit_result = edit;
4489                                         handle_result = 0;
4490                                         if( cursor_y >= edit_y+edit_h - HANDLE_W &&
4491                                             track->show_assets() ) {
4492                                                 new_cursor = DOWNLEFT_RESIZE;
4493                                                 if( button_press == LEFT_BUTTON )
4494                                                         result = -1;
4495                                         }
4496                                         else
4497                                                 result = 1;
4498                                 }
4499                                 else if( cursor_x >= edit_x + edit_w - HANDLE_W ) {
4500                                         edit_result = edit;
4501                                         handle_result = 1;
4502                                         if( cursor_y >= edit_y+edit_h - HANDLE_W &&
4503                                             track->show_assets() ) {
4504                                                 new_cursor = DOWNRIGHT_RESIZE;
4505                                                 if( button_press == LEFT_BUTTON )
4506                                                         result = -1;
4507                                         }
4508                                         else
4509                                                 result = 1;
4510                                 }
4511                         }
4512                 }
4513         }
4514
4515         update_cursor = 1;
4516         if( result > 0 ) {
4517                 double position = 0;
4518                 if( handle_result == 0 ) {
4519                         position = edit_result->track->from_units(edit_result->startproject);
4520                         new_cursor = LEFT_CURSOR;
4521                 }
4522                 else if( handle_result == 1 ) {
4523                         position = edit_result->track->from_units(edit_result->startproject + edit_result->length);
4524                         new_cursor = RIGHT_CURSOR;
4525                 }
4526
4527 // Reposition cursor
4528                 if( button_press ) {
4529                         mwindow->session->drag_edit = edit_result;
4530                         mwindow->session->drag_handle = handle_result;
4531                         mwindow->session->drag_button = get_buttonpress() - 1;
4532                         mwindow->session->drag_position = position;
4533                         mwindow->session->current_operation = DRAG_EDITHANDLE1;
4534                         mwindow->session->drag_origin_x = get_cursor_x();
4535                         mwindow->session->drag_origin_y = get_cursor_y();
4536                         mwindow->session->drag_start = position;
4537
4538                         rerender = start_selection(position);
4539                         update_overlay = 1;
4540                 }
4541         }
4542         else if( result < 0 ) {
4543                 mwindow->undo->update_undo_before();
4544                 if( !shift_down() ) {
4545                         if( handle_result == 0 )
4546                                 edit_result->hard_left = !edit_result->hard_left;
4547                         else if( handle_result == 1 )
4548                                 edit_result->hard_right = !edit_result->hard_right;
4549                 }
4550                 else {
4551                         int status = handle_result == 0 ? edit_result->hard_left :
4552                                      handle_result == 1 ? edit_result->hard_right : 0;
4553                         int new_status = !status;
4554                         int64_t edit_edge = edit_result->startproject;
4555                         if( handle_result == 1 ) edit_edge += edit_result->length;
4556                         double edge_position = edit_result->track->from_units(edit_edge);
4557                         for( Track *track=mwindow->edl->tracks->first; track!=0; track=track->next ) {
4558                                 int64_t track_position = track->to_units(edge_position, 1);
4559                                 Edit *left_edit = track->edits->editof(track_position, PLAY_FORWARD, 0);
4560                                 if( left_edit ) {
4561                                         int64_t left_edge = left_edit->startproject;
4562                                         double left_position = track->from_units(left_edge);
4563                                         if( EQUIV(edge_position, left_position) ) {
4564                                                 left_edit->hard_left = new_status;
4565                                                 if( left_edit->previous )
4566                                                         left_edit->previous->hard_right = new_status;
4567                                         }
4568                                 }
4569                                 Edit *right_edit = track->edits->editof(track_position, PLAY_REVERSE, 0);
4570                                 if( right_edit ) {
4571                                         int64_t right_edge = right_edit->startproject + right_edit->length;
4572                                         double right_position = track->from_units(right_edge);
4573                                         if( EQUIV(edge_position, right_position) ) {
4574                                                 right_edit->hard_right = new_status;
4575                                                 if( right_edit->next )
4576                                                         right_edit->next->hard_left = new_status;
4577                                         }
4578                                 }
4579                         }
4580                 }
4581                 rerender = update_overlay = 1;
4582                 mwindow->undo->update_undo_after(_("hard_edge"), LOAD_EDITS);
4583                 result = 1;
4584         }
4585
4586         return result;
4587 }
4588
4589 int TrackCanvas::do_plugin_handles(int cursor_x,
4590         int cursor_y,
4591         int button_press,
4592         int &rerender,
4593         int &update_overlay,
4594         int &new_cursor,
4595         int &update_cursor)
4596 {
4597         Plugin *plugin_result = 0;
4598         int handle_result = 0;
4599         int result = 0;
4600
4601         for(Track *track = mwindow->edl->tracks->first;
4602                 track && !result;
4603                 track = track->next) {
4604                 for(int i = 0; i < track->plugin_set.total && !result; i++) {
4605                         PluginSet *plugin_set = track->plugin_set.values[i];
4606                         for(Plugin *plugin = (Plugin*)plugin_set->first;
4607                                 plugin && !result;
4608                                 plugin = (Plugin*)plugin->next) {
4609                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
4610                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
4611
4612                                 if(cursor_x >= plugin_x && cursor_x <= plugin_x + plugin_w &&
4613                                         cursor_y >= plugin_y && cursor_y < plugin_y + plugin_h) {
4614                                         if(cursor_x < plugin_x + HANDLE_W) {
4615                                                 plugin_result = plugin;
4616                                                 handle_result = 0;
4617                                                 result = 1;
4618                                         }
4619                                         else if(cursor_x >= plugin_x + plugin_w - HANDLE_W) {
4620                                                 plugin_result = plugin;
4621                                                 handle_result = 1;
4622                                                 result = 1;
4623                                         }
4624                                 }
4625                         }
4626
4627                         if(result && shift_down())
4628                                 mwindow->session->trim_edits = plugin_set;
4629                 }
4630         }
4631
4632         update_cursor = 1;
4633         if(result) {
4634                 double position = 0;
4635                 if(handle_result == 0) {
4636                         position = plugin_result->track->from_units(plugin_result->startproject);
4637                         new_cursor = LEFT_CURSOR;
4638                 }
4639                 else if(handle_result == 1) {
4640                         position = plugin_result->track->from_units(plugin_result->startproject + plugin_result->length);
4641                         new_cursor = RIGHT_CURSOR;
4642                 }
4643
4644                 if(button_press) {
4645                         mwindow->session->drag_plugin = plugin_result;
4646                         mwindow->session->drag_handle = handle_result;
4647                         mwindow->session->drag_button = get_buttonpress() - 1;
4648                         mwindow->session->drag_position = position;
4649                         mwindow->session->current_operation = DRAG_PLUGINHANDLE1;
4650                         mwindow->session->drag_origin_x = get_cursor_x();
4651                         mwindow->session->drag_origin_y = get_cursor_y();
4652                         mwindow->session->drag_start = position;
4653
4654                         rerender = start_selection(position);
4655                         update_overlay = 1;
4656                 }
4657         }
4658
4659         return result;
4660 }
4661
4662
4663 int TrackCanvas::do_tracks(int cursor_x, int cursor_y, int button_press)
4664 {
4665         int result = 0;
4666
4667         for(Track *track = mwindow->edl->tracks->first;
4668                 track && !result;
4669                 track = track->next) {
4670                 int64_t track_x, track_y, track_w, track_h;
4671                 track_dimensions(track, track_x, track_y, track_w, track_h);
4672
4673                 if( button_press && get_buttonpress() == RIGHT_BUTTON &&
4674                     cursor_y >= track_y && cursor_y < track_y + track_h) {
4675                         double pos = mwindow->edl->get_cursor_position(cursor_x, pane->number);
4676                         int64_t position = track->to_units(pos, 0);
4677                         gui->edit_menu->update(track, track->edits->editof(position, PLAY_FORWARD, 0));
4678                         gui->edit_menu->activate_menu();
4679                         result = 1;
4680                 }
4681         }
4682
4683         return result;
4684 }
4685
4686 int TrackCanvas::do_edits(int cursor_x, int cursor_y, int button_press, int drag_start,
4687         int &redraw, int &rerender, int &new_cursor, int &update_cursor)
4688 {
4689         int result = 0;
4690
4691         for(Track *track = mwindow->edl->tracks->first; track && !result; track = track->next) {
4692                 for(Edit *edit = track->edits->first; edit && !result; edit = edit->next) {
4693                         int64_t edit_x, edit_y, edit_w, edit_h;
4694                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4695
4696 // Cursor inside a track
4697 // Cursor inside an edit
4698                         if(cursor_x >= edit_x && cursor_x < edit_x + edit_w &&
4699                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h) {
4700 // Select duration of edit
4701                                 if(button_press) {
4702                                         if(get_double_click() && !drag_start) {
4703                                                 mwindow->edl->local_session->set_selectionstart(edit->track->from_units(edit->startproject));
4704                                                 mwindow->edl->local_session->set_selectionend(edit->track->from_units(edit->startproject) +
4705                                                         edit->track->from_units(edit->length));
4706                                                 if(mwindow->edl->session->cursor_on_frames)
4707                                                 {
4708                                                         mwindow->edl->local_session->set_selectionstart(
4709                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4710                                                         mwindow->edl->local_session->set_selectionend(
4711                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4712                                                 }
4713                                                 redraw = 1;
4714                                                 rerender = 1;
4715                                                 update_cursor = -1;
4716                                                 result = 1;
4717                                         }
4718                                 }
4719                                 else if(drag_start && track->record) {
4720                                         if(mwindow->edl->session->editing_mode == EDITING_ARROW) {
4721 // Need to create drag window
4722                                                 mwindow->session->current_operation = DRAG_EDIT;
4723                                                 mwindow->session->drag_edit = edit;
4724 //printf("TrackCanvas::do_edits 2\n");
4725
4726 // Drag only one edit if ctrl is initially down
4727                                                 if(ctrl_down()) {
4728                                                         mwindow->session->drag_edits->remove_all();
4729                                                         mwindow->session->drag_edits->append(edit);
4730                                                 }
4731                                                 else {
4732 // Construct list of all affected edits
4733                                                         mwindow->edl->tracks->get_affected_edits(
4734                                                                 mwindow->session->drag_edits,
4735                                                                 edit->track->from_units(edit->startproject),
4736                                                                 edit->track);
4737                                                 }
4738                                                 mwindow->session->drag_origin_x = cursor_x;
4739                                                 mwindow->session->drag_origin_y = cursor_y;
4740                                                 // Where the drag started, so we know relative position inside the edit later
4741                                                 mwindow->session->drag_position = (double)cursor_x *
4742                                                         mwindow->edl->local_session->zoom_sample /
4743                                                         mwindow->edl->session->sample_rate +
4744                                                         (double)mwindow->edl->local_session->view_start[pane->number] *
4745                                                         mwindow->edl->local_session->zoom_sample /
4746                                                         mwindow->edl->session->sample_rate;
4747
4748                                                 int cx, cy;
4749                                                 get_abs_cursor(cx, cy);
4750                                                 gui->drag_popup = new BC_DragWindow(gui,
4751                                                         mwindow->theme->get_image("clip_icon"), cx, cy);
4752
4753                                                 result = 1;
4754                                         }
4755                                 }
4756                         }
4757                 }
4758         }
4759         return result;
4760 }
4761
4762
4763 int TrackCanvas::test_resources(int cursor_x, int cursor_y)
4764 {
4765         return 0;
4766 }
4767
4768 int TrackCanvas::do_plugins(int cursor_x, int cursor_y, int drag_start,
4769         int button_press, int &redraw, int &rerender)
4770 {
4771         Plugin *plugin = 0;
4772         int result = 0;
4773         int done = 0;
4774         int64_t x, y, w, h;
4775         Track *track = 0;
4776
4777         for(track = mwindow->edl->tracks->first; track && !done; track = track->next) {
4778                 if(!track->expand_view) continue;
4779
4780                 for(int i = 0; i < track->plugin_set.total && !done; i++) {
4781                         // first check if plugins are visible at all
4782                         if (!track->expand_view)
4783                                 continue;
4784                         PluginSet *plugin_set = track->plugin_set.values[i];
4785                         for(plugin = (Plugin*)plugin_set->first;
4786                                 plugin && !done;
4787                                 plugin = (Plugin*)plugin->next) {
4788                                 plugin_dimensions(plugin, x, y, w, h);
4789                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
4790                                         MWindowGUI::visible(y, y + h, 0, get_h())) {
4791                                         if(cursor_x >= x && cursor_x < x + w &&
4792                                                 cursor_y >= y && cursor_y < y + h) {
4793                                                 done = 1;
4794                                                 break;
4795                                         }
4796                                 }
4797                         }
4798                 }
4799         }
4800
4801         if(plugin) {
4802 // Start plugin popup
4803                 if(button_press) {
4804                         if(get_buttonpress() == 3) {
4805                                 gui->plugin_menu->update(plugin);
4806                                 gui->plugin_menu->activate_menu();
4807                                 result = 1;
4808                         }
4809                         else if (get_double_click() && !drag_start) {
4810 // Select range of plugin on doubleclick over plugin
4811                                 mwindow->edl->local_session->set_selectionstart(plugin->track->from_units(plugin->startproject));
4812                                 mwindow->edl->local_session->set_selectionend(plugin->track->from_units(plugin->startproject) +
4813                                         plugin->track->from_units(plugin->length));
4814                                 if(mwindow->edl->session->cursor_on_frames) {
4815                                         mwindow->edl->local_session->set_selectionstart(
4816                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4817                                         mwindow->edl->local_session->set_selectionend(
4818                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4819                                 }
4820                                 rerender = 1;
4821                                 redraw = 1;
4822                                 result = 1;
4823                         }
4824                 }
4825                 else
4826 // Move plugin
4827                 if(drag_start && plugin->track->record) {
4828                         if(mwindow->edl->session->editing_mode == EDITING_ARROW) {
4829                                 if(plugin->track->data_type == TRACK_AUDIO)
4830                                         mwindow->session->current_operation = DRAG_AEFFECT_COPY;
4831                                 else if(plugin->track->data_type == TRACK_VIDEO)
4832                                         mwindow->session->current_operation = DRAG_VEFFECT_COPY;
4833
4834                                 mwindow->session->drag_plugin = plugin;
4835                                 mwindow->session->drag_origin_x = cursor_x;
4836                                 mwindow->session->drag_origin_y = cursor_y;
4837                                 // Where the drag started, so we know relative position inside the edit later
4838                                 mwindow->session->drag_position =
4839                                         (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
4840                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
4841 // Create picon
4842                                 switch(plugin->plugin_type) {
4843                                 case PLUGIN_STANDALONE: {
4844                                         PluginServer *server =
4845                                                 mwindow->scan_plugindb(plugin->title, plugin->track->data_type);
4846                                         if( !server ) break;
4847                                         VFrame *frame = server->picon;
4848                                         if(!frame) {
4849                                                 if(plugin->track->data_type == TRACK_AUDIO) {
4850                                                         frame = mwindow->theme->get_image("aeffect_icon");
4851                                                 }
4852                                                 else {
4853                                                         frame = mwindow->theme->get_image("veffect_icon");
4854                                                 }
4855                                         }
4856                                         int cx, cy;
4857                                         get_abs_cursor(cx, cy);
4858                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
4859                                                 break; }
4860
4861                                 case PLUGIN_SHAREDPLUGIN:
4862                                 case PLUGIN_SHAREDMODULE: {
4863                                         VFrame *frame = mwindow->theme->get_image("clip_icon");
4864                                         int cx, cy;
4865                                         get_abs_cursor(cx, cy);
4866                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
4867                                         break; }
4868                                 }
4869
4870                                 result = 1;
4871                         }
4872                 }
4873         }
4874
4875         return result;
4876 }
4877
4878 int TrackCanvas::do_transitions(int cursor_x, int cursor_y,
4879         int button_press, int &new_cursor, int &update_cursor)
4880 {
4881         Transition *transition = 0;
4882         int result = 0;
4883         int64_t x, y, w, h;
4884
4885
4886         for( Track *track = mwindow->edl->tracks->first; track && !result; track = track->next ) {
4887                 if( !track->show_transitions() ) continue;
4888
4889                 for( Edit *edit = track->edits->first; edit; edit = edit->next ) {
4890                         if( edit->transition ) {
4891                                 edit_dimensions(edit, x, y, w, h);
4892                                 get_transition_coords(edit, x, y, w, h);
4893
4894                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
4895                                         MWindowGUI::visible(y, y + h, 0, get_h()) ) {
4896                                         if( cursor_x >= x && cursor_x < x + w &&
4897                                                 cursor_y >= y && cursor_y < y + h ) {
4898                                                 transition = edit->transition;
4899                                                 result = 1;
4900                                                 break;
4901                                         }
4902                                 }
4903                         }
4904                 }
4905         }
4906
4907         update_cursor = 1;
4908         if(transition) {
4909                 if(!button_press) {
4910                         new_cursor = UPRIGHT_ARROW_CURSOR;
4911                 }
4912                 else if(get_buttonpress() == 3) {
4913                         gui->transition_menu->update(transition);
4914                         gui->transition_menu->activate_menu();
4915                 }
4916         }
4917
4918         return result;
4919 }
4920
4921 int TrackCanvas::button_press_event()
4922 {
4923         int result = 0;
4924         int cursor_x, cursor_y;
4925         int new_cursor;
4926
4927         cursor_x = get_cursor_x();
4928         cursor_y = get_cursor_y();
4929         mwindow->session->trim_edits = 0;
4930
4931         if(is_event_win() && cursor_inside()) {
4932 //              double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
4933
4934                 result = 1;
4935                 if(!active) {
4936                         activate();
4937                 }
4938
4939                 if( get_buttonpress() == LEFT_BUTTON ) {
4940                         gui->stop_transport("TrackCanvas::button_press_event");
4941                 }
4942
4943                 int update_overlay = 0, update_cursor = 0, rerender = 0;
4944
4945                 if(get_buttonpress() == WHEEL_UP) {
4946                         if(shift_down())
4947                                 mwindow->expand_sample();
4948                         else if(ctrl_down())
4949                                 mwindow->move_left(get_w()/ 10);
4950                         else
4951                                 mwindow->move_up(get_h() / 10);
4952                 }
4953                 else if(get_buttonpress() == WHEEL_DOWN) {
4954                         if(shift_down())
4955                                 mwindow->zoom_in_sample();
4956                         else if(ctrl_down())
4957                                 mwindow->move_right(get_w() / 10);
4958                         else
4959                                 mwindow->move_down(get_h() / 10);
4960                 }
4961                 else if(get_buttonpress() == 6) {
4962                         if(ctrl_down())
4963                                 mwindow->move_left(get_w());
4964                         else if(alt_down())
4965                                 mwindow->move_left(get_w() / 20);
4966                         else
4967                                 mwindow->move_left(get_w() / 5);
4968                 }
4969                 else if(get_buttonpress() == 7) {
4970                         if(ctrl_down())
4971                                 mwindow->move_right(get_w());
4972                         else if(alt_down())
4973                                 mwindow->move_right(get_w() / 20);
4974                         else
4975                                 mwindow->move_right(get_w() / 5);
4976                 }
4977                 else {
4978                         switch(mwindow->edl->session->editing_mode) {
4979 // Test handles and resource boundaries and highlight a track
4980                         case EDITING_ARROW: {
4981                                 if( do_transitions(cursor_x, cursor_y,
4982                                                 1, new_cursor, update_cursor) ) break;
4983
4984                                 if( do_keyframes(cursor_x, cursor_y,
4985                                         0, get_buttonpress(), new_cursor,
4986                                         update_cursor, rerender) ) break;
4987 // Test edit boundaries
4988                                 if( do_edit_handles(cursor_x, cursor_y,
4989                                         1, rerender, update_overlay, new_cursor,
4990                                         update_cursor) ) break;
4991 // Test plugin boundaries
4992                                 if( do_plugin_handles(cursor_x, cursor_y,
4993                                         1, rerender, update_overlay, new_cursor,
4994                                         update_cursor) ) break;
4995
4996                                 if( do_edits(cursor_x, cursor_y, 1, 0,
4997                                         update_cursor, rerender, new_cursor,
4998                                         update_cursor) ) break;
4999
5000                                 if( do_plugins(cursor_x, cursor_y, 0, 1,
5001                                         update_cursor, rerender) ) break;
5002
5003                                 if( test_resources(cursor_x, cursor_y) ) break;
5004
5005                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
5006
5007                                 result = 0;
5008                                 break; }
5009
5010 // Test handles only and select a region
5011                         case EDITING_IBEAM: {
5012                                 double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
5013 //printf("TrackCanvas::button_press_event %d\n", position);
5014
5015                                 if( do_transitions(cursor_x, cursor_y,
5016                                                 1, new_cursor, update_cursor)) break;
5017                                 if(do_keyframes(cursor_x, cursor_y,
5018                                         0, get_buttonpress(), new_cursor,
5019                                         update_cursor, rerender)) {
5020                                         update_overlay = 1;
5021                                         break;
5022                                 }
5023 // Test edit boundaries
5024                                 if( do_edit_handles(cursor_x, cursor_y,
5025                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
5026 // Test plugin boundaries
5027                                 if( do_plugin_handles(cursor_x, cursor_y,
5028                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
5029
5030                                 if( do_edits(cursor_x, cursor_y,
5031                                         1, 0, update_cursor, rerender, new_cursor, update_cursor) ) break;
5032
5033                                 if( do_plugins(cursor_x, cursor_y, 0, 1, update_cursor, rerender) ) break;
5034
5035                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
5036 // Highlight selection
5037                                 if( get_buttonpress() != LEFT_BUTTON ) break;
5038                                 rerender = start_selection(position);
5039                                 mwindow->session->current_operation = SELECT_REGION;
5040                                 update_cursor = 1;
5041                                 break; }
5042                         }
5043                 }
5044
5045                 if( rerender ) {
5046                         gui->unlock_window();
5047                         mwindow->cwindow->update(1, 0, 0, 0, 1);
5048                         gui->lock_window("TrackCanvas::button_press_event 2");
5049 // Update faders
5050                         mwindow->update_plugin_guis();
5051                         gui->update_patchbay();
5052                 }
5053
5054                 if( update_overlay ) {
5055                         gui->draw_overlays(1);
5056                 }
5057                 if( update_cursor < 0 ) {
5058 // double_click edit
5059                         gui->swindow->update_selection();
5060                 }
5061                 if( update_cursor ) {
5062                         gui->update_timebar(0);
5063                         gui->hide_cursor(0);
5064                         gui->show_cursor(1);
5065                         gui->zoombar->update();
5066                         gui->flash_canvas(1);
5067                 }
5068         }
5069
5070         return result;
5071 }
5072
5073 int TrackCanvas::start_selection(double position)
5074 {
5075         int rerender = 0;
5076         position = mwindow->edl->align_to_frame(position, 1);
5077
5078
5079 // Extend a border
5080         if(shift_down())
5081         {
5082                 double midpoint = (mwindow->edl->local_session->get_selectionstart(1) +
5083                         mwindow->edl->local_session->get_selectionend(1)) / 2;
5084
5085                 if(position < midpoint)
5086                 {
5087                         mwindow->edl->local_session->set_selectionstart(position);
5088                         selection_midpoint = mwindow->edl->local_session->get_selectionend(1);
5089 // Que the CWindow
5090                         rerender = 1;
5091                 }
5092                 else
5093                 {
5094                         mwindow->edl->local_session->set_selectionend(position);
5095                         selection_midpoint = mwindow->edl->local_session->get_selectionstart(1);
5096 // Don't que the CWindow for the end
5097                 }
5098         }
5099         else
5100 // Start a new selection
5101         {
5102 //printf("TrackCanvas::start_selection %f\n", position);
5103                 mwindow->edl->local_session->set_selectionstart(position);
5104                 mwindow->edl->local_session->set_selectionend(position);
5105                 selection_midpoint = position;
5106 // Que the CWindow
5107                 rerender = 1;
5108         }
5109
5110         return rerender;
5111 }
5112
5113 void TrackCanvas::end_edithandle_selection()
5114 {
5115         mwindow->modify_edithandles();
5116 }
5117
5118 void TrackCanvas::end_pluginhandle_selection()
5119 {
5120         mwindow->modify_pluginhandles();
5121 }
5122
5123
5124 double TrackCanvas::time_visible()
5125 {
5126         return (double)get_w() *
5127                 mwindow->edl->local_session->zoom_sample /
5128                 mwindow->edl->session->sample_rate;
5129 }
5130
5131
5132 void TrackCanvas::show_message(Auto *current, int show_curve_type, const char *fmt, ...)
5133 {
5134         char string[BCTEXTLEN];
5135         char *cp = string, *ep = cp + sizeof(string)-1;
5136         if( show_curve_type ) {
5137                 cp += snprintf(string, ep-cp, "%-8s ",
5138                         FloatAuto::curve_name(((FloatAuto*)current)->curve_mode));
5139         }
5140         char string2[BCTEXTLEN];
5141         Units::totext(string2,
5142                 current->autos->track->from_units(current->position),
5143                 mwindow->edl->session->time_format,
5144                 mwindow->edl->session->sample_rate,
5145                 mwindow->edl->session->frame_rate,
5146                 mwindow->edl->session->frames_per_foot);
5147         cp += snprintf(cp, ep-cp, "%s", string2);
5148         va_list ap;
5149         va_start(ap, fmt);
5150         vsnprintf(cp, ep-cp, fmt, ap);
5151         va_end(ap);
5152         gui->show_message(string);
5153 }
5154
5155 // Patchbay* TrackCanvas::get_patchbay()
5156 // {
5157 //      if(pane->patchbay) return pane->patchbay;
5158 //      if(gui->total_panes() == 2 &&
5159 //              gui->pane[TOP_LEFT_PANE] &&
5160 //              gui->pane[TOP_RIGHT_PANE])
5161 //              return gui->pane[TOP_LEFT_PANE]->patchbay;
5162 //      if(gui->total_panes() == 4)
5163 //      {
5164 //              if(pane->number == TOP_RIGHT_PANE)
5165 //                      return gui->pane[TOP_LEFT_PANE]->patchbay;
5166 //              else
5167 //                      return gui->pane[BOTTOM_LEFT_PANE]->patchbay;
5168 //      }
5169 //
5170 //      return 0;
5171 // }
5172
5173