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