add grouping, default proxy vcodec h264.mp4, default titlebar alpha=1, green bar bug
[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 "trackpopup.h"
80 #include "tracks.h"
81 #include "transition.h"
82 #include "transitionhandles.h"
83 #include "transitionpopup.h"
84 #include "transportque.h"
85 #include "vframe.h"
86 #include "vpatchgui.inc"
87 #include "vrender.h"
88 #include "zoombar.h"
89
90 #include <string.h>
91
92 //#define PIXMAP_AGE -5
93 #define PIXMAP_AGE -32
94
95 TrackCanvas::TrackCanvas(MWindow *mwindow,
96         TimelinePane *pane,
97         int x,
98         int y,
99         int w,
100         int h)
101  : BC_SubWindow(x,
102         y,
103         w,
104         h)
105 {
106         this->mwindow = mwindow;
107         this->gui = mwindow->gui;
108         this->pane = pane;
109
110         selection_midpoint = 0;
111         drag_scroll = 0;
112         active = 0;
113         temp_picon = 0;
114         resource_timer = new Timer;
115         render_timer = new Timer;
116         hourglass_enabled = 0;
117         timebar_position = -1;
118         snapped = 0;
119 }
120
121 TrackCanvas::~TrackCanvas()
122 {
123 //      delete transition_handles;
124         delete edit_handles;
125         delete keyframe_pixmap;
126         delete camerakeyframe_pixmap;
127         delete modekeyframe_pixmap;
128         delete pankeyframe_pixmap;
129         delete projectorkeyframe_pixmap;
130         delete maskkeyframe_pixmap;
131         delete background_pixmap;
132         if(temp_picon) delete temp_picon;
133         delete render_timer;
134         delete resource_timer;
135 }
136
137 void TrackCanvas::create_objects()
138 {
139         background_pixmap = new BC_Pixmap(this, get_w(), get_h());
140 //      transition_handles = new TransitionHandles(mwindow, this);
141         edit_handles = new EditHandles(mwindow, this);
142         keyframe_pixmap = new BC_Pixmap(this, mwindow->theme->keyframe_data, PIXMAP_ALPHA);
143         camerakeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->camerakeyframe_data, PIXMAP_ALPHA);
144         modekeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->modekeyframe_data, PIXMAP_ALPHA);
145         pankeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->pankeyframe_data, PIXMAP_ALPHA);
146         projectorkeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->projectorkeyframe_data, PIXMAP_ALPHA);
147         maskkeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->maskkeyframe_data, PIXMAP_ALPHA);
148         draw(NORMAL_DRAW, 1);
149         update_cursor(0);
150         flash(0);
151 }
152
153 void TrackCanvas::resize_event()
154 {
155 //printf("TrackCanvas::resize_event 1\n");
156         draw(NORMAL_DRAW, 0);
157         flash(0);
158 //printf("TrackCanvas::resize_event 2\n");
159 }
160
161 int TrackCanvas::keypress_event()
162 {
163         return 0;
164 }
165
166 int TrackCanvas::cursor_leave_event()
167 {
168 // Because drag motion calls get_cursor_over_window we can be sure that
169 // all highlights get deleted now.
170 // This ended up blocking keyboard input from the drag operations.
171         if( timebar_position >= 0 )
172         {
173                 timebar_position = -1;
174                 if( pane->timebar )
175                         pane->timebar->update(1);
176         }
177
178         return 0;
179 //      return drag_motion();
180 }
181
182 int TrackCanvas::drag_motion_event()
183 {
184         return gui->drag_motion();
185 }
186
187 int TrackCanvas::drag_motion(
188                 Track **over_track, Edit **over_edit,
189                 PluginSet **over_pluginset, Plugin **over_plugin)
190 {
191         int cursor_x = get_relative_cursor_x();
192         int cursor_y = get_relative_cursor_y();
193         if( get_cursor_over_window() ) {
194                 drag_cursor_motion(cursor_x, cursor_y,
195                         over_track, over_edit, over_pluginset, over_plugin);
196         }
197         if( over_track && !*over_track )
198                 *over_track = pane->over_patchbay();
199         return 0;
200 }
201
202 int TrackCanvas::drag_cursor_motion(int cursor_x, int cursor_y,
203                 Track **over_track, Edit **over_edit,
204                 PluginSet **over_pluginset, Plugin **over_plugin)
205 {
206         if( cursor_x >= 0 && cursor_y >= 0 &&
207             cursor_x < get_w() && cursor_y < get_h() )
208         {
209 //printf("TrackCanvas::drag_motion %d %d\n", __LINE__, pane->number);
210 // Find the edit and track the cursor is over
211                 for(Track *track = mwindow->edl->tracks->first; track; track = track->next)
212                 {
213                         int64_t track_x, track_y, track_w, track_h;
214                         track_dimensions(track, track_x, track_y, track_w, track_h);
215
216                         if(cursor_y >= track_y &&
217                                 cursor_y < track_y + track_h)
218                         {
219                                 *over_track = track;
220                                 for(Edit *edit = track->edits->first; edit; edit = edit->next)
221                                 {
222                                         int64_t edit_x, edit_y, edit_w, edit_h;
223                                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
224                                         if( cursor_y < edit_y || cursor_y >= edit_y + edit_h ) continue;
225                                         if( cursor_x >= edit_x && cursor_x < edit_x + edit_w ) {
226                                                 *over_edit = edit;
227                                                 break;
228                                         }
229                                         if( edit != track->edits->last ) continue;
230                                         if( mwindow->session->current_operation != DRAG_ATRANSITION &&
231                                             mwindow->session->current_operation != DRAG_VTRANSITION ) continue;
232                                         if( !edit->silence() ) {
233                                                 // add silence to allow drag transition past last edit
234                                                 //  will be deleted by Edits::optimize if not used
235                                                 double length = mwindow->edl->session->default_transition_length;
236                                                 int64_t start = edit->startproject+edit->length;
237                                                 int64_t units = track->to_units(length, 1);
238                                                 track->edits->create_silence(start, start+units);
239                                                 continue;
240                                         }
241                                         if( cursor_x >= edit_x ) { *over_edit = edit; break; }
242                                 }
243
244                                 for(int i = 0; i < track->plugin_set.total; i++)
245                                 {
246                                         PluginSet *pluginset = track->plugin_set.values[i];
247                                         for(Plugin *plugin = (Plugin*)pluginset->first;
248                                                 plugin;
249                                                 plugin = (Plugin*)plugin->next)
250                                         {
251                                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
252                                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
253
254                                                 if(cursor_y >= plugin_y &&
255                                                         cursor_y < plugin_y + plugin_h)
256                                                 {
257                                                         *over_pluginset = plugin->plugin_set;
258
259                                                         if(cursor_x >= plugin_x &&
260                                                                 cursor_x < plugin_x + plugin_w)
261                                                         {
262                                                                 *over_plugin = plugin;
263                                                                 break;
264                                                         }
265                                                 }
266                                         }
267                                 }
268                                 break;
269                         }
270                 }
271         }
272
273         return 0;
274 }
275
276
277 int TrackCanvas::drag_stop_event()
278 {
279         int result = gui->drag_stop();
280         if( !result && mwindow->session->current_operation ) {
281                 mwindow->session->current_operation = NO_OPERATION;
282                 drag_scroll = 0;
283         }
284         return result;
285 }
286
287 int TrackCanvas::drag_stop(int *redraw)
288 {
289         int result = 0;
290         int insertion  = 0;
291         int over_window = 0;
292         int cursor_x = -1, cursor_y = -1;
293
294         if( get_cursor_over_window() ) {
295                 if( (cursor_x = get_relative_cursor_x()) >= 0 && cursor_x < get_w() &&
296                     (cursor_y = get_relative_cursor_y()) >= 0 && cursor_y < get_h() )
297                         over_window = 1;
298                 else {
299                         Track *track = pane->over_patchbay();
300                         if( track && mwindow->session->track_highlighted == track )
301                                 over_window = 1;
302                 }
303         }
304
305         if( over_window ) {
306                 switch(mwindow->session->current_operation) {
307                 case DRAG_VTRANSITION:
308                 case DRAG_ATRANSITION:
309                         if(mwindow->session->edit_highlighted) {
310                                 if( (mwindow->session->current_operation == DRAG_ATRANSITION &&
311                                      mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
312                                     (mwindow->session->current_operation == DRAG_VTRANSITION &&
313                                      mwindow->session->track_highlighted->data_type == TRACK_VIDEO) ) {
314                                         mwindow->session->current_operation = NO_OPERATION;
315                                         mwindow->paste_transition();
316                                         result = 1;
317                                 }
318                         }
319                         *redraw = 1;
320                         break;
321
322 // Behavior for dragged plugins is limited by the fact that a shared plugin
323 // can only refer to a standalone plugin that exists in the same position in
324 // time.  Dragging a plugin from one point in time to another can't produce
325 // a shared plugin to the original plugin.  In this case we relocate the
326 // plugin instead of sharing it.
327                 case DRAG_AEFFECT_COPY:
328                 case DRAG_VEFFECT_COPY:
329                         if( mwindow->session->track_highlighted &&
330                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
331                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
332                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
333                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) ) {
334                                 mwindow->session->current_operation = NO_OPERATION;
335
336                                 int64_t drop_position = -1;
337                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
338                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
339                                 Track *hi_track = mwindow->session->track_highlighted;
340
341 // Insert shared plugin in source
342 // Move source to different location
343                                 if( hi_plugin_set && hi_plugin_set->track == drag_plugin->track ) {
344 //printf("TrackCanvas::drag_stop 6\n");
345                                         drop_position = drop_plugin_position(hi_plugin_set, drag_plugin);
346                                         if( drop_position < 0 ) {
347                                                 result = 1;
348                                                 break;          // Do not do anything
349                                         }
350
351                                         Track *track = mwindow->session->track_highlighted;
352                                         drop_position = track->frame_align(drop_position, 0);
353                                         mwindow->move_effect(drag_plugin, hi_plugin_set, drop_position);
354                                         result = 1;
355                                 }
356                                 else if( hi_track ) {
357 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
358                                         Edit *hi_edit = mwindow->session->edit_highlighted;
359                                         drop_position = hi_edit ? hi_edit->startproject : 0;
360                                         if( drop_position < 0 ) {
361                                                 result = 1;
362                                                 break;          // Do not do anything
363                                         }
364
365                                         Track *track = mwindow->session->track_highlighted;
366                                         drop_position = track->frame_align(drop_position, 0);
367                                         mwindow->move_effect(drag_plugin, hi_track, drop_position);
368                                         result = 1;
369                                 }
370                         }
371                         break;
372
373                 case DRAG_AEFFECT:
374                 case DRAG_VEFFECT:
375                         if( mwindow->session->track_highlighted &&
376                             ((mwindow->session->current_operation == DRAG_AEFFECT &&
377                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
378                              (mwindow->session->current_operation == DRAG_VEFFECT &&
379                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) ) {
380 // Drop all the effects
381                                 PluginSet *plugin_set = mwindow->session->pluginset_highlighted;
382                                 Track *track = mwindow->session->track_highlighted;
383                                 double start = 0;
384                                 double length = track->get_length();
385
386                                 if(mwindow->session->plugin_highlighted) {
387                                         start = track->from_units(mwindow->session->plugin_highlighted->startproject);
388                                         length = track->from_units(mwindow->session->plugin_highlighted->length);
389                                         if(length <= 0) length = track->get_length();
390                                 }
391                                 else if(mwindow->session->pluginset_highlighted) {
392                                                 start = track->from_units(plugin_set->length());
393                                         length = track->get_length() - start;
394                                         if(length <= 0) length = track->get_length();
395                                 }
396                                 else if(mwindow->edl->local_session->get_selectionend() >
397                                         mwindow->edl->local_session->get_selectionstart()) {
398                                         start = mwindow->edl->local_session->get_selectionstart();
399                                         length = mwindow->edl->local_session->get_selectionend() -
400                                                 mwindow->edl->local_session->get_selectionstart();
401                                 }
402 // Move to a point between two edits
403                                 else if(mwindow->session->edit_highlighted) {
404                                         start = mwindow->session->track_highlighted->from_units(
405                                                 mwindow->session->edit_highlighted->startproject);
406                                         length = mwindow->session->track_highlighted->from_units(
407                                                 mwindow->session->edit_highlighted->length);
408                                 }
409                                 start = mwindow->edl->align_to_frame(start, 0);
410                                 mwindow->insert_effects_canvas(start, length);
411                                 *redraw = 1;
412                         }
413                         if( mwindow->session->track_highlighted )
414                                 result = 1;  // we have to cleanup
415                         break;
416
417                 case DRAG_ASSET:
418                         if(mwindow->session->track_highlighted) {
419                                 double asset_duration = 0;
420                                 int64_t asset_length_units = 0;
421
422                                 if(mwindow->session->current_operation == DRAG_ASSET &&
423                                         mwindow->session->drag_assets->total) {
424                                         Indexable *indexable = mwindow->session->drag_assets->values[0];
425                                         // we use video if we are over video and audio if we are over audio
426                                         if( indexable->have_video() &&
427                                             mwindow->session->track_highlighted->data_type == TRACK_VIDEO ) {
428                                                 // Images have length -1
429                                                 int64_t video_length = indexable->get_video_frames();
430                                                 if (video_length < 0) {
431                                                         if(mwindow->edl->session->si_useduration)
432                                                                 video_length = mwindow->edl->session->si_duration;
433                                                         else
434                                                                 video_length = 1.0 / mwindow->edl->session->frame_rate ;
435                                                 }
436                                                 asset_duration = video_length / indexable->get_frame_rate();
437                                         }
438                                         else if( indexable->have_audio() &&
439                                                  mwindow->session->track_highlighted->data_type == TRACK_AUDIO ) {
440                                                 int64_t audio_length = indexable->get_audio_samples();
441                                                 asset_duration = (double)audio_length / indexable->get_sample_rate();
442                                         }
443                                         else {
444                                                 result = 1;
445                                                 break;  // Do not do anything
446                                         }
447                                 }
448                                 else if( mwindow->session->current_operation == DRAG_ASSET &&
449                                          mwindow->session->drag_clips->total ) {
450                                         EDL *clip = mwindow->session->drag_clips->values[0];
451                                         asset_duration = clip->tracks->total_length();
452                                 }
453                                 else {
454                                         printf("DRAG_ASSET error: Asset dropped, but both drag_clips and drag_assets total is zero\n");
455                                 }
456
457                                 asset_length_units = mwindow->session->track_highlighted->to_units(asset_duration, 1);
458                                 int64_t drop_position = drop_edit_position (&insertion, NULL, asset_length_units);
459                                 if( drop_position < 0 ) {
460                                         result = 1;
461                                         break;          // Do not do anything
462                                 }
463
464                                 Track *track = mwindow->session->track_highlighted;
465                                 double track_position = track->from_units(drop_position);
466                                 track_position = mwindow->edl->align_to_frame(track_position, 0);
467
468 //                              if (!insertion) {
469 //                                      // FIXME, we should create an mwindow/EDL method that overwrites, without clearing the keyframes and autos
470 //                                      // Unfortunately, this is _a lot_ of work to do right
471 //                                      printf("Problematic insertion\n");
472 //                                      mwindow->edl->tracks->clear(track_position,
473 //                                              track_position + asset_duration, 0);
474 //                              }
475                                 mwindow->paste_assets(track_position, track, !insertion);
476                                 result = 1;    // need to be one no matter what, since we have track highlited so we have to cleanup....
477                         }
478                         break;
479
480                 case DRAG_EDIT:
481                         mwindow->session->current_operation = NO_OPERATION;
482                         if(mwindow->session->track_highlighted) {
483                                 if( mwindow->session->track_highlighted->data_type ==
484                                         mwindow->session->drag_edit->track->data_type) {
485                                         int64_t drop_position = drop_edit_position(&insertion,
486                                                         mwindow->session->drag_edit,
487                                                         mwindow->session->drag_edit->length);
488                                         if( drop_position < 0 ) {
489                                                 result = 1;
490                                                 break;          // Do not do anything
491                                         }
492                                         Track *track = mwindow->session->track_highlighted;
493                                         double track_position = track->from_units(drop_position);
494                                         track_position = mwindow->edl->align_to_frame(track_position, 0);
495                                         mwindow->move_edits(mwindow->session->drag_edits,
496                                                         track, track_position, !insertion);
497                                 }
498
499                                 result = 1;
500                         }
501                         break;
502                 case DRAG_GROUP:
503                         mwindow->session->current_operation = NO_OPERATION;
504                         EDL *drag_group = mwindow->session->drag_group;
505                         if( drag_group ) {
506                                 Track *drop_track = mwindow->session->track_highlighted;
507                                 Track *drag_track = mwindow->session->drag_group_first_track;
508                                 Edit *drag_edit = mwindow->session->drag_group_edit;
509                                 Track *edit_track = drag_edit ? drag_edit->track : 0;
510                                 while( drop_track && edit_track ) {
511                                         if( edit_track == drag_track ) break;
512                                         edit_track = edit_track->previous;
513                                         drop_track = drop_track->previous;
514                                 }
515                                 if( drop_track && test_track_group(drag_group, drop_track) ) {
516                                         double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
517                                         position = mwindow->edl->align_to_frame(position, 0);
518                                         position -= mwindow->session->drag_group_position;
519                                         mwindow->move_group(drag_group, drop_track, position);
520                                 }
521                                 drag_group->remove_user();
522                                 mwindow->session->drag_group = 0;
523                         }
524                         result = 1;
525                         break;
526                 }
527         }
528
529         return result;
530 }
531
532 int TrackCanvas::drag_start_event()
533 {
534         int result = 0;
535         int redraw = 0;
536         int rerender = 0;
537         int new_cursor, update_cursor;
538
539         if(mwindow->session->current_operation != NO_OPERATION) return 0;
540
541         if(is_event_win()) {
542                 if(do_plugins(get_drag_x(), get_drag_y(),
543                                 1, 0, redraw, rerender)) {
544                         result = 1;
545                 }
546                 else if(do_edits(get_drag_x(), get_drag_y(),
547                                 0, 1, redraw, rerender, new_cursor, update_cursor)) {
548                         result = 1;
549                 }
550         }
551
552         return result;
553 }
554
555 int64_t TrackCanvas::drop_edit_position(int *is_insertion, Edit *moved_edit, int64_t moved_edit_length)
556 {
557         // get the canvas/track position
558         Track *track = mwindow->session->track_highlighted;
559         int cursor_x = get_relative_cursor_x();
560         double zoom_scale = (double)mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample;
561         double cur_pos = (cursor_x + mwindow->edl->local_session->view_start[pane->number]) / zoom_scale;
562         double position = mwindow->edl->align_to_frame(cur_pos, 1);
563         if( position <= 0 ) {
564                 *is_insertion = 1;
565                 return 0;
566         }
567         double cursor_position = position;
568         int64_t drop_position = track->to_units(cursor_position, 1);
569         if( moved_edit ) { // relative cursor position depends upon drop point
570                 double moved_edit_start = moved_edit->track->from_units(moved_edit->startproject);
571                 position -= mwindow->session->drag_position - moved_edit_start;
572         }
573         int64_t edit_position = track->to_units(position, 1);
574         int64_t grab_position = edit_position;
575         if( !moved_edit )  // for clips and assets acts as they were grabbed in the middle
576                 grab_position -= moved_edit_length / 2;
577         Edit *last_edit = track->edits->last;
578         if( !last_edit || edit_position >= (last_edit->startproject+last_edit->length) ) {
579                 *is_insertion = 0;
580                 return edit_position;
581         }
582
583         int64_t drop_start = 0, drop_end = 0;  // drop zone boundries
584         Edit *next_edit = track->edits->first;
585         while( next_edit ) {
586                 Edit *edit = next_edit;  next_edit = (Edit *)edit->next;
587                 int64_t edit_start = edit->startproject;
588                 int64_t edit_end = edit_start + edit->length;
589                 double edit_start_pos = edit->track->from_units(edit_start);
590                 if( (fabs(edit_start_pos-cursor_position)*zoom_scale) < HANDLE_W ) {
591                         *is_insertion = 1; // cursor is close to the beginning of an edit -> insertion
592                         return edit_start;
593                 }
594                 double edit_end_pos = edit->track->from_units(edit_end);
595                 if( (fabs(edit_end_pos-cursor_position)*zoom_scale) < HANDLE_W ) {
596                         *is_insertion = 1; // cursor is close to the end of an edit -> insertion
597                         return edit_end;
598                 }
599                 if( edit != moved_edit && !edit->silence() )
600                         drop_start = edit_end; // reset drop zone
601                 if( next_edit ) {
602                         if( next_edit == moved_edit || next_edit->silence() ) continue;
603                         drop_end = edit_end;
604                 }
605                 else
606                         drop_end = INT64_MAX;
607                 if( edit_position >= drop_start &&
608                     edit_position+moved_edit_length < drop_end ) {
609                         *is_insertion = 0; // fits in the zone
610                         return edit_position;
611                 }
612                 if( drop_position < drop_end ) { // drop in the zone
613                         if( (drop_end - drop_start) >= moved_edit_length ) {
614                                 *is_insertion = 0; // fits in the zone, but over the edge
615                                 int64_t dx0 = llabs(drop_position - drop_start);
616                                 int64_t dx1 = llabs(drop_position - drop_end);
617                                 return dx0 < dx1 ? drop_start : drop_end - moved_edit_length;
618                         }
619                         *is_insertion = 1;
620                         int64_t edit_center = (edit_start + edit_end) / 2;
621                         return position < edit_center ? drop_start : drop_end;
622                 }
623         }
624
625         *is_insertion = 0;
626         return -1;
627 }
628
629 int64_t TrackCanvas::drop_plugin_position(PluginSet *plugin_set, Plugin *moved_plugin)
630 {
631         // get the canvas/track position
632         Track *track = plugin_set->track;
633         double moved_plugin_length = moved_plugin->track->from_units(moved_plugin->length);
634         int64_t track_plugin_length = track->to_units(moved_plugin_length, 0);
635         int cursor_x = get_relative_cursor_x();
636         double zoom_scale = (double)mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample;
637         double cur_pos = (cursor_x + mwindow->edl->local_session->view_start[pane->number]) / zoom_scale;
638         double position = mwindow->edl->align_to_frame(cur_pos, 1);
639         if( position <= 0 ) return 0;
640         int64_t drop_position = track->to_units(position, 1);
641         Plugin *last_plugin = (Plugin *)plugin_set->last;
642         if( !last_plugin ) return drop_position;
643         double plugin_set_end = last_plugin->track->from_units(last_plugin->startproject+last_plugin->length);
644         if( position >= plugin_set_end ) return drop_position;
645         double moved_plugin_start = moved_plugin->track->from_units(moved_plugin->startproject);
646         position -= mwindow->session->drag_position - moved_plugin_start;
647         int64_t plugin_position = track->to_units(position, 1);
648
649         int64_t drop_start = 0, drop_end = 0;  // drop zone boundries
650         Plugin *next_plugin = (Plugin *)plugin_set->first;
651         while( next_plugin ) {
652                 Plugin *plugin = next_plugin;  next_plugin = (Plugin *)plugin->next;
653                 int64_t plugin_start = plugin->startproject;
654                 int64_t plugin_end = plugin_start + plugin->length;
655                 double plugin_end_pos = plugin->track->from_units(plugin_end);
656                 int64_t track_plugin_end = track->to_units(plugin_end_pos, 0);
657                 if( plugin != moved_plugin && !plugin->silence() )
658                         drop_start = track_plugin_end;
659                 if( next_plugin ) {
660                         if( next_plugin == moved_plugin || next_plugin->silence() ) continue;
661                         drop_end = track_plugin_end;
662                 }
663                 else
664                         drop_end = INT64_MAX;
665                 if( plugin_position >= drop_start && // fits in the zone
666                     plugin_position+track_plugin_length < drop_end ) {
667                         return plugin_position;
668                 }
669                 if( drop_position < drop_end ) { // drop in the zone
670                         if( (drop_end - drop_start) >= track_plugin_length ) {
671                                 int64_t dx0 = llabs(drop_position - drop_start);
672                                 int64_t dx1 = llabs(drop_position - drop_end);
673                                 return dx0 < dx1 ? drop_start : drop_end - track_plugin_length;
674                         }
675                 }
676         }
677
678         return -1;
679 }
680
681 void TrackCanvas::draw(int mode, int hide_cursor)
682 {
683         const int debug = 0;
684
685 // Swap pixmap layers
686         if(get_w() != background_pixmap->get_w() ||
687                 get_h() != background_pixmap->get_h())
688         {
689                 delete background_pixmap;
690                 background_pixmap = new BC_Pixmap(this, get_w(), get_h());
691         }
692
693 // Cursor disappears after resize when this is called.
694 // Cursor doesn't redraw after editing when this isn't called.
695         if(pane->cursor && hide_cursor) pane->cursor->hide();
696         draw_top_background(get_parent(), 0, 0, get_w(), get_h(), background_pixmap);
697
698         if(debug) PRINT_TRACE
699         draw_resources(mode);
700
701         if(debug) PRINT_TRACE
702         draw_overlays();
703         if(debug) PRINT_TRACE
704 }
705
706 void TrackCanvas::update_cursor(int flush)
707 {
708         switch(mwindow->edl->session->editing_mode)
709         {
710                 case EDITING_ARROW: set_cursor(ARROW_CURSOR, 0, flush); break;
711                 case EDITING_IBEAM: set_cursor(IBEAM_CURSOR, 0, flush); break;
712         }
713 }
714
715
716 void TrackCanvas::test_timer()
717 {
718         if(resource_timer->get_difference() > 1000 &&
719                 !hourglass_enabled)
720         {
721                 start_hourglass();
722                 hourglass_enabled = 1;
723         }
724 }
725
726
727 void TrackCanvas::draw_indexes(Indexable *indexable)
728 {
729         IndexState *index_state = indexable->index_state;
730 // Don't redraw raw samples
731         if(index_state->index_zoom > mwindow->edl->local_session->zoom_sample)
732                 return;
733         draw_resources(NORMAL_DRAW, 1, indexable);
734         draw_overlays();
735         flash(0);
736 }
737
738 void TrackCanvas::draw_resources(int mode,
739         int indexes_only,
740         Indexable *indexable)
741 {
742         const int debug = 0;
743
744         if(debug) PRINT_TRACE
745
746 // can't stop thread here, because this is called for every pane
747 //      if(mode != IGNORE_THREAD && !indexes_only)
748 //              gui->resource_thread->stop_draw(!indexes_only);
749
750         if(mode != IGNORE_THREAD &&
751                 !indexes_only &&
752                 !gui->resource_thread->interrupted)
753         {
754                 printf("TrackCanvas::draw_resources %d: called without stopping ResourceThread\n",
755                         __LINE__);
756
757                 BC_Signals::dump_stack();
758         }
759
760         if(debug) PRINT_TRACE
761
762         resource_timer->update();
763
764 // Age resource pixmaps for deletion
765         if(!indexes_only)
766                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
767                         gui->resource_pixmaps.values[i]->visible--;
768
769         if(mode == FORCE_REDRAW)
770                 gui->resource_pixmaps.remove_all_objects();
771
772         if(debug) PRINT_TRACE
773
774 // Search every edit
775         for(Track *current = mwindow->edl->tracks->first;
776                 current;
777                 current = NEXT)
778         {
779                 if(debug) PRINT_TRACE
780                 for(Edit *edit = current->edits->first; edit; edit = edit->next)
781                 {
782                         if(debug) PRINT_TRACE
783                         if( current->data_type != TRACK_SUBTITLE )
784                                 if(!edit->asset && !edit->nested_edl) continue;
785                         if(indexes_only)
786                         {
787                                 if(edit->track->data_type != TRACK_AUDIO) continue;
788
789                                 if(edit->nested_edl &&
790                                         strcmp(indexable->path, edit->nested_edl->path)) continue;
791
792                                 if(edit->asset &&
793                                         strcmp(indexable->path, edit->asset->path)) continue;
794                         }
795
796                         if(debug) PRINT_TRACE
797
798                         int64_t edit_x, edit_y, edit_w, edit_h;
799                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
800
801 // Edit is visible
802                         if(MWindowGUI::visible(edit_x, edit_x + edit_w, 0, get_w()) &&
803                                 MWindowGUI::visible(edit_y, edit_y + edit_h, 0, get_h()))
804                         {
805                                 int64_t pixmap_x, pixmap_w, pixmap_h;
806                                 if(debug) PRINT_TRACE
807
808 // Search for existing pixmap containing edit
809                                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
810                                 {
811                                         ResourcePixmap* pixmap = gui->resource_pixmaps.values[i];
812 // Same pointer can be different edit if editing took place
813                                         if(pixmap->edit_id == edit->id &&
814                                                 pixmap->pane_number == pane->number)
815                                         {
816                                                 pixmap->visible = 1;
817                                                 break;
818                                         }
819                                 }
820                                 if(debug) PRINT_TRACE
821
822 // Get new size, offset of pixmap needed
823                                 get_pixmap_size(edit,
824                                         edit_x,
825                                         edit_w,
826                                         pixmap_x,
827                                         pixmap_w,
828                                         pixmap_h);
829                                 if(debug) PRINT_TRACE
830
831 // Draw new data
832                                 if(pixmap_w && pixmap_h)
833                                 {
834 // Create pixmap if it doesn't exist
835                                         ResourcePixmap* pixmap = create_pixmap(edit, edit_x,
836                                                 pixmap_x, pixmap_w, pixmap_h);
837 // Resize it if it's bigger
838                                         if( pixmap_w > pixmap->pixmap_w ||
839                                             pixmap_h > pixmap->pixmap_h)
840                                                 pixmap->resize(pixmap_w, pixmap_h);
841                                         pixmap->update_settings(edit, edit_x, edit_w,
842                                                 pixmap_x, pixmap_w, pixmap_h);
843 // Draw data
844                                         if( current->show_assets() )
845                                                 pixmap->draw_data(this,
846                                                         edit, edit_x, edit_w,
847                                                         pixmap_x, pixmap_w, pixmap_h,
848                                                         mode, indexes_only);
849                                         else {
850                                                 set_bg_color(BLACK);
851                                                 clear_box(0,0, pixmap_w,pixmap_h, pixmap);
852                                         }
853 // Draw title
854                                         if( current->show_titles() )
855                                                 pixmap->draw_title(this,
856                                                         edit, edit_x, edit_w,
857                                                         pixmap_x, pixmap_w);
858 // Resize it if it's smaller
859                                         if(pixmap_w < pixmap->pixmap_w ||
860                                                 pixmap_h < pixmap->pixmap_h)
861                                                 pixmap->resize(pixmap_w, pixmap_h);
862
863 // Copy pixmap to background canvas
864                                         background_pixmap->draw_pixmap(pixmap,
865                                                 pixmap->pixmap_x,
866                                                 current->y_pixel - mwindow->edl->local_session->track_start[pane->number],
867                                                 pixmap->pixmap_w,
868                                                 edit_h);
869                                 }
870                                 if(debug) PRINT_TRACE
871
872                         }
873                 }
874         }
875
876
877 // Delete unused pixmaps
878         if(debug) PRINT_TRACE
879         if(!indexes_only)
880                 for(int i = gui->resource_pixmaps.total - 1; i >= 0; i--)
881                         if(gui->resource_pixmaps.values[i]->visible < PIXMAP_AGE)
882                         {
883 //printf("TrackCanvas::draw_resources %d\n", __LINE__);
884                                 delete gui->resource_pixmaps.values[i];
885                                 gui->resource_pixmaps.remove(gui->resource_pixmaps.values[i]);
886                         }
887         if(debug) PRINT_TRACE
888
889         if(hourglass_enabled)
890         {
891                 stop_hourglass();
892                 hourglass_enabled = 0;
893         }
894         if(debug) PRINT_TRACE
895
896 // can't stop thread here, because this is called for every pane
897 //      if(mode != IGNORE_THREAD && !indexes_only)
898 //              gui->resource_thread->start_draw();
899         if(debug) PRINT_TRACE
900
901
902 }
903
904 ResourcePixmap* TrackCanvas::create_pixmap(Edit *edit,
905         int64_t edit_x,
906         int64_t pixmap_x,
907         int64_t pixmap_w,
908         int64_t pixmap_h)
909 {
910         ResourcePixmap *result = 0;
911
912         for(int i = 0; i < gui->resource_pixmaps.total; i++)
913         {
914 //printf("TrackCanvas::create_pixmap 1 %d %d\n", edit->id, resource_pixmaps.values[i]->edit->id);
915                 if(gui->resource_pixmaps.values[i]->edit_id == edit->id &&
916                         gui->resource_pixmaps.values[i]->pane_number == pane->number)
917                 {
918                         result = gui->resource_pixmaps.values[i];
919                         break;
920                 }
921         }
922
923         if(!result)
924         {
925 //SET_TRACE
926                 result = new ResourcePixmap(mwindow,
927                         gui,
928                         edit,
929                         pane->number,
930                         pixmap_w,
931                         pixmap_h);
932 //SET_TRACE
933                 gui->resource_pixmaps.append(result);
934         }
935
936 //      result->resize(pixmap_w, pixmap_h);
937         return result;
938 }
939
940 void TrackCanvas::get_pixmap_size(Edit *edit,
941         int64_t edit_x,
942         int64_t edit_w,
943         int64_t &pixmap_x,
944         int64_t &pixmap_w,
945         int64_t &pixmap_h)
946 {
947
948 // Align x on frame boundaries
949
950
951 //      switch(edit->edits->track->data_type)
952 //      {
953 //              case TRACK_AUDIO:
954
955                         pixmap_x = edit_x;
956                         pixmap_w = edit_w;
957                         if(pixmap_x < 0)
958                         {
959                                 pixmap_w -= -edit_x;
960                                 pixmap_x = 0;
961                         }
962
963                         if(pixmap_x + pixmap_w > get_w())
964                         {
965                                 pixmap_w = get_w() - pixmap_x;
966                         }
967
968 //                      break;
969 //
970 //              case TRACK_VIDEO:
971 //              {
972 //                      int64_t picon_w = (int64_t)(edit->picon_w() + 0.5);
973 //                      int64_t frame_w = (int64_t)(edit->frame_w() + 0.5);
974 //                      int64_t pixel_increment = MAX(picon_w, frame_w);
975 //                      int64_t pixmap_x1 = edit_x;
976 //                      int64_t pixmap_x2 = edit_x + edit_w;
977 //
978 //                      if(pixmap_x1 < 0)
979 //                      {
980 //                              pixmap_x1 = (int64_t)((double)-edit_x / pixel_increment) *
981 //                                      pixel_increment +
982 //                                      edit_x;
983 //                      }
984 //
985 //                      if(pixmap_x2 > get_w())
986 //                      {
987 //                              pixmap_x2 = (int64_t)((double)(get_w() - edit_x) / pixel_increment + 1) *
988 //                                      pixel_increment +
989 //                                      edit_x;
990 //                      }
991 //                      pixmap_x = pixmap_x1;
992 //                      pixmap_w = pixmap_x2 - pixmap_x1;
993 //                      break;
994 //              }
995 //      }
996
997         pixmap_h = mwindow->edl->local_session->zoom_track;
998         Track *track = edit->edits->track;
999         if( track->show_titles() )
1000                 pixmap_h += mwindow->theme->get_image("title_bg_data")->get_h();
1001 //printf("get_pixmap_size %d %d %d %d\n", edit_x, edit_w, pixmap_x, pixmap_w);
1002 }
1003
1004 void TrackCanvas::edit_dimensions(Edit *edit,
1005         int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1006 {
1007         x = Units::round(edit->track->from_units(edit->startproject) *
1008                         mwindow->edl->session->sample_rate /
1009                         mwindow->edl->local_session->zoom_sample -
1010                         mwindow->edl->local_session->view_start[pane->number]);
1011
1012         y = edit->edits->track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1013
1014 // Method for calculating w so when edits are together we never get off by one error due to rounding
1015         int64_t x_next = Units::round(edit->track->from_units(edit->startproject + edit->length) *
1016                         mwindow->edl->session->sample_rate /
1017                         mwindow->edl->local_session->zoom_sample -
1018                         mwindow->edl->local_session->view_start[pane->number]);
1019         w = x_next - x;
1020
1021         int edit_h = 0;
1022         if( edit->track->show_titles() )
1023                 edit_h += mwindow->theme->get_image("title_bg_data")->get_h();
1024         if( edit->track->show_assets() )
1025                 edit_h += resource_h();
1026         h = edit_h;
1027 }
1028
1029 void TrackCanvas::track_dimensions(Track *track, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1030 {
1031         x = 0;
1032         w = get_w();
1033         y = track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1034         h = track->vertical_span(mwindow->theme);
1035 }
1036
1037
1038 void TrackCanvas::draw_paste_destination()
1039 {
1040         //int cursor_x = get_relative_cursor_x();
1041         //int cursor_y = get_relative_cursor_y();
1042         int current_atrack = 0;
1043         int current_vtrack = 0;
1044         int current_aedit = 0;
1045         int current_vedit = 0;
1046         int64_t w = 0;
1047         int64_t x;
1048         double position;
1049         int insertion  = 0;
1050
1051 //if(pane->number == BOTTOM_RIGHT_PANE)
1052 //printf("TrackCanvas::draw_paste_destination %d %p\n", __LINE__, mwindow->session->track_highlighted);
1053
1054         if((mwindow->session->current_operation == DRAG_ASSET &&
1055                         (mwindow->session->drag_assets->total ||
1056                         mwindow->session->drag_clips->total)) ||
1057                 (mwindow->session->current_operation == DRAG_EDIT &&
1058                         mwindow->session->drag_edits->total)) {
1059                 Indexable *indexable = 0;
1060                 EDL *clip = 0;
1061                 //int draw_box = 0;
1062
1063                 if(mwindow->session->current_operation == DRAG_ASSET &&
1064                         mwindow->session->drag_assets->size())
1065                         indexable = mwindow->session->drag_assets->get(0);
1066
1067                 if(mwindow->session->current_operation == DRAG_ASSET &&
1068                         mwindow->session->drag_clips->size())
1069                         clip = mwindow->session->drag_clips->get(0);
1070
1071                 int has_audio = 0, has_video = 0;
1072                 double paste_audio_length = 0, paste_video_length = 0;
1073                 double paste_audio_position = -1, paste_video_position = -1;
1074
1075                 if( indexable ) {
1076                         has_audio = indexable->have_audio();
1077                         paste_audio_length = !has_audio ? 0 :
1078                                  (double)indexable->get_audio_samples() / indexable->get_sample_rate();
1079                         has_video = indexable->have_video();
1080                         Asset *asset = indexable->is_asset ? (Asset *)indexable : 0;
1081                         // Images have length -1
1082                         if( asset && asset->video_data && asset->video_length < 0 ) {
1083                                 paste_video_length = mwindow->edl->session->si_useduration ?
1084                                         mwindow->edl->session->si_duration / asset->frame_rate :
1085                                         1.0 / asset->frame_rate ;  // 1 frame
1086                         }
1087                         else
1088                                 paste_video_length = !has_video ? 0 :
1089                                         (double)indexable->get_video_frames() / indexable->get_frame_rate();
1090                 }
1091
1092                 if( clip ) {
1093                         has_audio = clip->tracks->total_audio_tracks() > 0 ? 1 : 0;
1094                         paste_audio_length = !has_audio ? 0 : clip->tracks->total_audio_length();
1095                         has_video = clip->tracks->total_video_tracks() > 0 ? 1 : 0;
1096                         paste_video_length = !has_video ? 0 : clip->tracks->total_video_length();
1097                 }
1098
1099 // 'Align cursor on frame' lengths calculations
1100                 if( mwindow->edl->session->cursor_on_frames ) {
1101                         double fps = mwindow->edl->session->frame_rate;
1102                         double aud = floor(paste_audio_length * fps) / fps;
1103                         double vid = floor(paste_video_length * fps) / fps;
1104                         double length = has_video && has_audio ?
1105                                         aud < vid ? aud : vid :
1106                                 has_video ? vid :
1107                                 has_audio ? aud : 0;
1108                         paste_video_length = paste_audio_length = length;
1109                 }
1110
1111                 if( has_audio ) { // we use video if we are over video and audio if we are over audio
1112                         int64_t length = 0;
1113                         switch( mwindow->session->track_highlighted->data_type ) {
1114                         case TRACK_VIDEO: length = paste_video_length;  break;
1115                         case TRACK_AUDIO: length = paste_audio_length;  break;
1116                         }
1117                         int64_t asset_length = mwindow->session->track_highlighted->
1118                                 to_units(length, 1);
1119                         paste_audio_position = mwindow->session->track_highlighted->
1120                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1121                 }
1122                 if( has_video ) {
1123                         int64_t asset_length = mwindow->session->track_highlighted->
1124                                 to_units(paste_video_length, 1);
1125                         paste_video_position = mwindow->session->track_highlighted->
1126                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1127                 }
1128
1129 // Get destination track
1130                 for(Track *dest = mwindow->session->track_highlighted;
1131                         dest;
1132                         dest = dest->next) {
1133                         if(dest->record) {
1134 // Get source width in pixels
1135                                 w = -1;
1136 // Use start of highlighted edit
1137                                 if(mwindow->session->edit_highlighted) {
1138                                         position = mwindow->session->track_highlighted->from_units(
1139                                                 mwindow->session->edit_highlighted->startproject);
1140                                 }
1141                                 else {
1142 // Use end of highlighted track, disregarding effects
1143                                         position = mwindow->session->track_highlighted->from_units(
1144                                                 mwindow->session->track_highlighted->edits->length());
1145                                 }
1146
1147 // Get the x coordinate
1148                                 x = Units::to_int64(position *
1149                                         mwindow->edl->session->sample_rate /
1150                                         mwindow->edl->local_session->zoom_sample) -
1151                                         mwindow->edl->local_session->view_start[pane->number];
1152
1153                                 double paste_position = -1.;
1154                                 if(dest->data_type == TRACK_AUDIO) {
1155                                         if(indexable && current_atrack < indexable->get_audio_channels()) {
1156 //printf("TrackCanvas::draw_paste_destination %d %d\n", __LINE__, current_atrack);
1157                                                 w = Units::to_int64((double)indexable->get_audio_samples() /
1158                                                         indexable->get_sample_rate() *
1159                                                         mwindow->edl->session->sample_rate /
1160                                                         mwindow->edl->local_session->zoom_sample);
1161                                                 paste_position = paste_audio_position;
1162                                         }
1163                                         else if(clip && current_atrack < clip->tracks->total_audio_tracks()) {
1164                                                 w = Units::to_int64((double)clip->tracks->total_length() *
1165                                                         mwindow->edl->session->sample_rate /
1166                                                         mwindow->edl->local_session->zoom_sample);
1167                                                 paste_position = paste_audio_position;
1168 //printf("draw_paste_destination %d\n", x);
1169                                         }
1170                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1171                                                 current_aedit < mwindow->session->drag_edits->total) {
1172                                                 Edit *edit;
1173                                                 while(current_aedit < mwindow->session->drag_edits->total &&
1174                                                         mwindow->session->drag_edits->values[current_aedit]->track->data_type != TRACK_AUDIO)
1175                                                         current_aedit++;
1176
1177                                                 if(current_aedit < mwindow->session->drag_edits->total) {
1178                                                         edit = mwindow->session->drag_edits->values[current_aedit];
1179                                                         w = Units::to_int64(edit->length / mwindow->edl->local_session->zoom_sample);
1180                                                         paste_position = mwindow->session->track_highlighted->
1181                                                                 from_units(drop_edit_position(&insertion,
1182                                                                         mwindow->session->drag_edit,
1183                                                                         mwindow->session->drag_edit->length));
1184                                                         current_aedit++;
1185                                                 }
1186                                         }
1187                                         if( paste_position >= 0 ) {
1188                                                 position = paste_position;
1189                                                 current_atrack++;
1190                                                 //draw_box = 1;
1191                                         }
1192                                         else
1193                                                 w = -1;
1194                                 }
1195                                 else if(dest->data_type == TRACK_VIDEO) {
1196 //printf("draw_paste_destination 1\n");
1197                                         if(indexable && current_vtrack < indexable->get_video_layers())
1198                                         {
1199                                                 w = Units::to_int64((double)indexable->get_video_frames() /
1200                                                         indexable->get_frame_rate() *
1201                                                         mwindow->edl->session->sample_rate /
1202                                                         mwindow->edl->local_session->zoom_sample);
1203                                                 paste_position = paste_video_position;
1204                                         }
1205                                         else if(clip && current_vtrack < clip->tracks->total_video_tracks()) {
1206                                                 w = Units::to_int64(clip->tracks->total_length() *
1207                                                         mwindow->edl->session->sample_rate /
1208                                                         mwindow->edl->local_session->zoom_sample);
1209                                                 paste_position = paste_video_position;
1210                                         }
1211                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1212                                                 current_vedit < mwindow->session->drag_edits->total) {
1213                                                 Edit *edit;
1214                                                 while(current_vedit < mwindow->session->drag_edits->total &&
1215                                                         mwindow->session->drag_edits->values[current_vedit]->track->data_type != TRACK_VIDEO)
1216                                                         current_vedit++;
1217
1218                                                 if(current_vedit < mwindow->session->drag_edits->total) {
1219                                                         edit = mwindow->session->drag_edits->values[current_vedit];
1220                                                         w = Units::to_int64(edit->track->from_units(edit->length) *
1221                                                                 mwindow->edl->session->sample_rate /
1222                                                                 mwindow->edl->local_session->zoom_sample);
1223                                                         paste_position = mwindow->session->track_highlighted->
1224                                                                 from_units(drop_edit_position(&insertion,
1225                                                                         mwindow->session->drag_edit,
1226                                                                         mwindow->session->drag_edit->length));
1227                                                         current_vedit++;
1228                                                 }
1229                                         }
1230                                         if( paste_position >= 0 ) {
1231                                                 position = paste_position;
1232                                                 current_vtrack++;
1233                                                 //draw_box = 1;
1234                                         }
1235                                         else
1236                                                 w = -1;
1237                                 }
1238
1239                                 if(w >= 0) {
1240 // Get the x coordinate
1241                                         x = Units::to_int64(position *
1242                                                 mwindow->edl->session->sample_rate /
1243                                                 mwindow->edl->local_session->zoom_sample) -
1244                                                 mwindow->edl->local_session->view_start[pane->number];
1245                                         int y = dest->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1246                                         int h = dest->vertical_span(mwindow->theme);
1247
1248 //printf("TrackCanvas::draw_paste_destination 2 %d %d %d %d\n", x, y, w, h);
1249                                         if(x < -BC_INFINITY) {
1250                                                 w -= -BC_INFINITY - x;
1251                                                 x += -BC_INFINITY - x;
1252                                         }
1253                                         w = MIN(65535, w);
1254 // if(pane->number == TOP_RIGHT_PANE)
1255 // printf("TrackCanvas::draw_paste_destination %d %d %d %d %d\n",
1256 // __LINE__, x, y, w, h);
1257                                         if (insertion)
1258                                                 draw_highlight_insertion(x, y, w, h);
1259                                         else
1260                                                 draw_highlight_rectangle(x, y, w, h);
1261                                 }
1262                         }
1263                 }
1264         }
1265 }
1266
1267 void TrackCanvas::plugin_dimensions(Plugin *plugin, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1268 {
1269         x = Units::round(plugin->track->from_units(plugin->startproject) *
1270                 mwindow->edl->session->sample_rate /
1271                 mwindow->edl->local_session->zoom_sample -
1272                 mwindow->edl->local_session->view_start[pane->number]);
1273         w = Units::round(plugin->track->from_units(plugin->length) *
1274                 mwindow->edl->session->sample_rate /
1275                 mwindow->edl->local_session->zoom_sample);
1276         y = plugin->track->y_pixel
1277                 - mwindow->edl->local_session->track_start[pane->number];
1278         if( plugin->track->show_titles() )
1279                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1280         if( plugin->track->show_assets() )
1281                 y += resource_h();
1282         y += plugin->plugin_set->get_number() *
1283                         mwindow->theme->get_image("plugin_bg_data")->get_h();
1284         h = mwindow->theme->get_image("plugin_bg_data")->get_h();
1285 }
1286
1287 int TrackCanvas::resource_h()
1288 {
1289         return mwindow->edl->local_session->zoom_track;
1290 }
1291
1292 void TrackCanvas::draw_highlight_rectangle(int x, int y, int w, int h)
1293 {
1294
1295 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1296 // just draw arrows, so user has indication that something is there
1297 // FIXME: get better colors
1298
1299         if (x + w <= 0)
1300         {
1301                 draw_triangle_left(0, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1302                 return;
1303         } else
1304         if (x >= get_w())
1305         {
1306                 draw_triangle_right(get_w() - h * 2/3, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1307                 return;
1308         }
1309
1310 // Fix bug in heroines & cvs version as of 22.8.2005:
1311 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1312         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1313         if(x < -10)
1314         {
1315                 w += x - -10;
1316                 x = -10;
1317         }
1318
1319         if(y < -10)
1320         {
1321                 h += y - -10;
1322                 y = -10;
1323         }
1324
1325         w = MIN(w, get_w() + 20);
1326         h = MIN(h, get_h() + 20);
1327         if(w > 0 && h > 0)
1328         {
1329                 set_color(mwindow->preferences->highlight_inverse);
1330                 set_inverse();
1331                 //draw_rectangle(x, y, w, h);
1332                 draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1333                 draw_rectangle(x + 2, y + 2, w - 4, h - 4);
1334                 set_opaque();
1335                 draw_rectangle(x, y, w, h);
1336         }
1337 //if(pane->number == TOP_RIGHT_PANE)
1338 //printf("TrackCanvas::draw_highlight_rectangle %d %d %d %d %d\n", __LINE__, x, y, w, h);
1339 }
1340
1341 void TrackCanvas::draw_highlight_insertion(int x, int y, int w, int h)
1342 {
1343
1344 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport,
1345 // just draw arrows, so user has indication that something is there
1346 // FIXME: get better colors
1347
1348
1349
1350         int h1 = h / 8;
1351         int h2 = h / 4;
1352
1353         set_inverse();
1354
1355 /* these don't look so good
1356
1357         draw_line(x, y, x, y+h);
1358         draw_line(x - h2 * 2, y + h1*2,   x - h2, y+h1*2);
1359         draw_line(x - h2 * 2, y + h1*2+1, x - h2, y+h1*2+1);
1360         draw_line(x - h2 * 2, y + h1*6,   x - h2, y+h1*6);
1361         draw_line(x - h2 * 2, y + h1*6+1, x - h2, y+h1*6+1);
1362 */
1363         draw_triangle_right(x - h2, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1364         draw_triangle_right(x - h2, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1365
1366 /*      draw_line(x + h2 * 2, y + h1*2,   x + h2, y+h1*2);
1367         draw_line(x + h2 * 2, y + h1*2+1, x + h2, y+h1*2+1);
1368         draw_line(x + h2 * 2, y + h1*6,   x + h2, y+h1*6);
1369         draw_line(x - h2 * 2, y + h1*6+1, x + h2, y+h1*6+1);
1370 */
1371         draw_triangle_left(x, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1372         draw_triangle_left(x, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1373
1374 // draw the box centred around x
1375         x -= w / 2;
1376 // Fix bug in heroines & cvs version as of 22.8.2005:
1377 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1378         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1379         if(x < -10)
1380         {
1381                 w += x - -10;
1382                 x = -10;
1383         }
1384         if(y < -10)
1385         {
1386                 h += y - -10;
1387                 y = -10;
1388         }
1389         w = MIN(w, get_w() + 20);
1390         h = MIN(h, get_h() + 20);
1391         set_color(mwindow->preferences->highlight_inverse);
1392         set_inverse();
1393         draw_rectangle(x, y, w, h);
1394         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1395         set_opaque();
1396 //printf("TrackCanvas::draw_highlight_insertion %d %d %d %d\n", x, y, w, h);
1397 }
1398
1399 void TrackCanvas::draw_playback_cursor()
1400 {
1401 // Called before playback_cursor exists
1402 //      if(mwindow->playback_cursor && mwindow->playback_cursor->visible)
1403 //      {
1404 //              mwindow->playback_cursor->visible = 0;
1405 //              mwindow->playback_cursor->draw();
1406 //      }
1407 }
1408
1409 void TrackCanvas::get_handle_coords(Edit *edit, int64_t &x, int64_t &y, int64_t &w, int64_t &h, int side)
1410 {
1411         int handle_w = mwindow->theme->edithandlein_data[0]->get_w();
1412         int handle_h = mwindow->theme->edithandlein_data[0]->get_h();
1413
1414         edit_dimensions(edit, x, y, w, h);
1415
1416         if( edit->track->show_titles() )
1417                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1418
1419         if(side == EDIT_OUT)
1420                 x += w - handle_w;
1421
1422         h = handle_h;
1423         w = handle_w;
1424 }
1425
1426 void TrackCanvas::get_transition_coords(Edit *edit,
1427                 int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1428 {
1429         int transition_w = 30, transition_h = 30;
1430         int has_titles = edit->track->show_titles();
1431         int has_assets = edit->track->show_assets();
1432         double title_bg_h = mwindow->theme->get_image("title_bg_data")->get_h();
1433         double asset_h = resource_h();
1434         double ys = has_assets ? asset_h : has_titles ? title_bg_h : 0;
1435         double dy = has_titles ?
1436                 ( has_assets ? title_bg_h + asset_h/2 : title_bg_h/2 ) :
1437                 ( has_assets ? asset_h/2 : 0) ;
1438         double title_h = mwindow->theme->title_h;
1439         if( dy < title_h / 2 ) { ys = title_h;  dy = ys / 2; }
1440         y += dy;
1441
1442         x -= transition_w / 2;
1443         y -= transition_h / 2;
1444         w = transition_w;
1445         h = transition_h;
1446 }
1447
1448 void TrackCanvas::draw_highlighting()
1449 {
1450         int64_t x, y, w, h;
1451         int draw_box = 0;
1452
1453         switch(mwindow->session->current_operation)
1454         {
1455                 case DRAG_ATRANSITION:
1456                 case DRAG_VTRANSITION:
1457 //printf("TrackCanvas::draw_highlighting 1 %p %p\n",
1458 //      mwindow->session->track_highlighted, mwindow->session->edit_highlighted);
1459                         if(mwindow->session->edit_highlighted) {
1460                                 if((mwindow->session->current_operation == DRAG_ATRANSITION &&
1461                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1462                                         (mwindow->session->current_operation == DRAG_VTRANSITION &&
1463                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) {
1464                                         edit_dimensions(mwindow->session->edit_highlighted,
1465                                                 x, y, w, h);
1466                                         if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1467                                                 MWindowGUI::visible(y, y + h, 0, get_h())) {
1468                                                 draw_box = 1;
1469                                                 get_transition_coords(mwindow->session->edit_highlighted,
1470                                                         x, y, w, h);
1471                                         }
1472                                 }
1473                         }
1474                         break;
1475
1476 // Dragging a new effect from the Resource window
1477                 case DRAG_AEFFECT:
1478                 case DRAG_VEFFECT:
1479                         if(mwindow->session->track_highlighted &&
1480                                 ((mwindow->session->current_operation == DRAG_AEFFECT &&
1481                                   mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1482                                  (mwindow->session->current_operation == DRAG_VEFFECT &&
1483                                   mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1484 // Put it before another plugin
1485                                 if(mwindow->session->plugin_highlighted) {
1486                                         plugin_dimensions(mwindow->session->plugin_highlighted,
1487                                                 x, y, w, h);
1488 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1489                                 }
1490 // Put it after a plugin set
1491                                 else if( mwindow->session->pluginset_highlighted &&
1492                                          mwindow->session->pluginset_highlighted->last ) {
1493                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last,
1494                                                 x, y, w, h);
1495 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1496                                         int64_t track_x, track_y, track_w, track_h;
1497                                         track_dimensions(mwindow->session->track_highlighted,
1498                                                 track_x, track_y, track_w, track_h);
1499
1500                                         x += w;
1501                                         w = Units::round(
1502                                                         mwindow->session->track_highlighted->get_length() *
1503                                                         mwindow->edl->session->sample_rate /
1504                                                         mwindow->edl->local_session->zoom_sample -
1505                                                         mwindow->edl->local_session->view_start[pane->number]) -
1506                                                 x;
1507 //printf("TrackCanvas::draw_highlighting 2 %d\n", w);
1508                                         if(w <= 0) w = track_w;
1509                                 }
1510                                 else {
1511                                         track_dimensions(mwindow->session->track_highlighted,
1512                                                 x, y, w, h);
1513
1514 //printf("TrackCanvas::draw_highlighting 1 %d %d %d %d\n", x, y, w, h);
1515 // Put it in a new plugin set determined by the selected range
1516                                         if(mwindow->edl->local_session->get_selectionend() >
1517                                                 mwindow->edl->local_session->get_selectionstart()) {
1518                                                 x = Units::to_int64(mwindow->edl->local_session->get_selectionstart() *
1519                                                         mwindow->edl->session->sample_rate /
1520                                                         mwindow->edl->local_session->zoom_sample -
1521                                                         mwindow->edl->local_session->view_start[pane->number]);
1522                                                 w = Units::to_int64((mwindow->edl->local_session->get_selectionend() -
1523                                                         mwindow->edl->local_session->get_selectionstart()) *
1524                                                         mwindow->edl->session->sample_rate /
1525                                                         mwindow->edl->local_session->zoom_sample);
1526                                         }
1527 // Put it in a new plugin set determined by an edit boundary
1528                                         else if(mwindow->session->edit_highlighted) {
1529                                                 int64_t temp_y, temp_h;
1530                                                 edit_dimensions(mwindow->session->edit_highlighted,
1531                                                         x, temp_y, w, temp_h);
1532                                         }
1533 // Put it at the beginning of the track in a new plugin set
1534                                 }
1535
1536                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1537                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1538 //printf("TrackCanvas::draw_highlighting 1\n");
1539                                         draw_box = 1;
1540                                 }
1541                         }
1542                         break;
1543
1544                 case DRAG_ASSET:
1545                         if(mwindow->session->track_highlighted) {
1546 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1547
1548 //                              if(MWindowGUI::visible(y, y + h, 0, get_h()))
1549 //                              {
1550                                         draw_paste_destination();
1551 //                              }
1552                         }
1553                         break;
1554
1555                 case DRAG_EDIT:
1556                         if(mwindow->session->track_highlighted) {
1557 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1558 //
1559 //                              if(MWindowGUI::visible(y, y + h, 0, get_h())) {
1560                                         draw_paste_destination();
1561 //                              }
1562                         }
1563                         break;
1564                 case DRAG_GROUP:
1565                         if( mwindow->session->track_highlighted && mwindow->session->drag_group &&
1566                             mwindow->session->drag_group_edit && mwindow->session->drag_group_first_track ) {
1567                                 Track *track = mwindow->session->track_highlighted;
1568                                 EDL *drag_group = mwindow->session->drag_group;
1569                                 Track *edit_track = mwindow->session->drag_group_edit->track;
1570                                 Track *drag_track = mwindow->session->drag_group_first_track;
1571                                 while( track && edit_track && edit_track != drag_track ) {
1572                                         edit_track = edit_track->previous;
1573                                         track = track->previous;
1574                                 }
1575                                 int color = track && test_track_group(drag_group, track) ? GREEN : RED;
1576                                 track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1577                                 int dx = get_cursor_x() - mwindow->session->drag_origin_x;
1578                                 int dy = y - mwindow->session->drag_origin_y;
1579                                 draw_selected_edits(mwindow->edl, dx, dy, color, -1);
1580                         }
1581                         break;
1582 // Dragging an effect from the timeline
1583                 case DRAG_AEFFECT_COPY:
1584                 case DRAG_VEFFECT_COPY:
1585                         if( (mwindow->session->plugin_highlighted || mwindow->session->track_highlighted) &&
1586                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
1587                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1588                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
1589                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1590
1591                                 int64_t plugin_position = -1;
1592                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
1593                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
1594                                 Track *hi_track = mwindow->session->track_highlighted;
1595                                 Edit *hi_edit = mwindow->session->edit_highlighted;
1596 // Put it into a silence zone
1597                                 if( hi_plugin_set && hi_plugin_set->last &&
1598                                     hi_plugin_set->track == drag_plugin->track ) {
1599                                         plugin_dimensions((Plugin *)hi_plugin_set->last, x, y, w, h);
1600                                         plugin_position = drop_plugin_position(hi_plugin_set, drag_plugin);
1601                                         hi_track = drag_plugin->track;
1602                                 }
1603                                 else if( hi_track ) {
1604 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
1605                                         track_dimensions(hi_track, x, y, w, h);
1606                                         plugin_position = hi_edit ? hi_edit->startproject : 0;
1607                                 }
1608
1609                                 if( plugin_position < 0 ) break;
1610
1611 // Calculate length of plugin based on data type of track and units
1612                                 double zoom_scale = (double)mwindow->edl->session->sample_rate /
1613                                                 mwindow->edl->local_session->zoom_sample;
1614                                 x = Units::round(hi_track->from_units(plugin_position) * zoom_scale) -
1615                                                 mwindow->edl->local_session->view_start[pane->number];
1616                                 w = Units::round(hi_track->from_units(drag_plugin->length) * zoom_scale);
1617
1618                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1619                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1620                                         draw_box = 1;
1621                                 }
1622                         }
1623                         break;
1624
1625                 case DRAG_PLUGINKEY:
1626                         if(mwindow->session->plugin_highlighted &&
1627                            mwindow->session->current_operation == DRAG_PLUGINKEY)
1628                         {
1629 // Just highlight the plugin
1630                                 plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1631
1632                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1633                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1634                                 {
1635                                         draw_box = 1;
1636                                 }
1637                         }
1638                         break;
1639
1640         }
1641
1642         if( draw_box )
1643                 draw_highlight_rectangle(x, y, w, h);
1644
1645         draw_selected_edits(mwindow->edl, 0, 0, GREEN+BLUE, RED);
1646 }
1647
1648 void TrackCanvas::draw_selected_edits(EDL *edl, int dx, int dy, int color0, int color1)
1649 {
1650         for( Track *track=edl->tracks->first; track; track=track->next ) {
1651                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
1652                         if( !edit->is_selected ) continue;
1653                         int64_t x, y, w, h;
1654                         edit_dimensions(edit, x, y, w, h);
1655                         x += dx;  y += dy;
1656                         if( !MWindowGUI::visible(x, x + w, 0, get_w()) ) continue;
1657                         if( !MWindowGUI::visible(y, y + h, 0, get_h()) ) continue;
1658                         set_opaque();
1659                         int inner = color1 < 0 ? color0 : !edit->group_id ? color0 :
1660                                 mwindow->get_group_color(edit->group_id);
1661                         set_color(inner);
1662                         draw_rectangle(x, y, w, h);
1663                         int outer = color1 < 0 ? color0 : !edit->group_id ? color1 : inner;
1664                         set_color(outer);
1665                         draw_rectangle(x-1, y-1, w+2, h+2);
1666                 }
1667         }
1668 }
1669
1670 void TrackCanvas::draw_plugins()
1671 {
1672         char string[BCTEXTLEN];
1673         int current_on = 0;
1674         int current_show = 0;
1675         int current_preset = 0;
1676
1677         for(int i = 0; i < plugin_on_toggles.total; i++)
1678                 plugin_on_toggles.values[i]->in_use = 0;
1679         for(int i = 0; i < plugin_show_toggles.total; i++)
1680                 plugin_show_toggles.values[i]->in_use = 0;
1681         for(int i = 0; i < preset_edit_buttons.total; i++)
1682                 plugin_show_toggles.values[i]->in_use = 0;
1683
1684
1685         for(Track *track = mwindow->edl->tracks->first;
1686                 track;
1687                 track = track->next)
1688         {
1689                 if(track->expand_view)
1690                 {
1691                         for(int i = 0; i < track->plugin_set.total; i++)
1692                         {
1693                                 PluginSet *pluginset = track->plugin_set.values[i];
1694
1695                                 for(Plugin *plugin = (Plugin*)pluginset->first; plugin; plugin = (Plugin*)plugin->next)
1696                                 {
1697                                         int64_t total_x, y, total_w, h;
1698                                         plugin_dimensions(plugin, total_x, y, total_w, h);
1699
1700                                         if(MWindowGUI::visible(total_x, total_x + total_w, 0, get_w()) &&
1701                                                 MWindowGUI::visible(y, y + h, 0, get_h()) &&
1702                                                 plugin->plugin_type != PLUGIN_NONE)
1703                                         {
1704                                                 int x = total_x, w = total_w, left_margin = 5;
1705                                                 int right_margin = 5;
1706                                                 if(x < 0)
1707                                                 {
1708                                                         w -= -x;
1709                                                         x = 0;
1710                                                 }
1711                                                 if(w + x > get_w()) w -= (w + x) - get_w();
1712
1713                                                 draw_3segmenth(x,
1714                                                         y,
1715                                                         w,
1716                                                         total_x,
1717                                                         total_w,
1718                                                         mwindow->theme->get_image("plugin_bg_data"),
1719                                                         0);
1720                                                 set_color(mwindow->theme->title_color);
1721                                                 set_font(mwindow->theme->title_font);
1722                                                 plugin->calculate_title(string, 0);
1723
1724 // Truncate string to int64_test visible in background
1725                                                 int len = strlen(string), j;
1726                                                 for(j = len; j >= 0; j--)
1727                                                 {
1728                                                         if(left_margin + get_text_width(mwindow->theme->title_font, string) > w)
1729                                                         {
1730                                                                 string[j] = 0;
1731                                                         }
1732                                                         else
1733                                                                 break;
1734                                                 }
1735
1736 // Justify the text on the left boundary of the edit if it is visible.
1737 // Otherwise justify it on the left side of the screen.
1738                                                 int64_t text_x = total_x + left_margin;
1739                                                 int64_t text_w = get_text_width(mwindow->theme->title_font, string, strlen(string));
1740                                                 text_x = MAX(left_margin, text_x);
1741                                                 draw_text(text_x,
1742                                                         y + get_text_ascent(mwindow->theme->title_font) + 2,
1743                                                         string,
1744                                                         strlen(string),
1745                                                         0);
1746                                                 int64_t min_x = total_x + text_w;
1747
1748
1749 // Update plugin toggles
1750                                                 int toggle_x = total_x + total_w;
1751                                                 int toggle_y = y;
1752                                                 toggle_x = MIN(get_w() - right_margin, toggle_x);
1753
1754 // On toggle
1755                                                 toggle_x -= PluginOn::calculate_w(mwindow) + 10;
1756                                                 if(toggle_x > min_x)
1757                                                 {
1758                                                         if(current_on >= plugin_on_toggles.total)
1759                                                         {
1760                                                                 PluginOn *plugin_on = new PluginOn(mwindow, toggle_x, toggle_y, plugin);
1761                                                                 add_subwindow(plugin_on);
1762                                                                 plugin_on_toggles.append(plugin_on);
1763                                                         }
1764                                                         else
1765                                                         {
1766                                                                 plugin_on_toggles.values[current_on]->update(toggle_x, toggle_y, plugin);
1767                                                         }
1768                                                         current_on++;
1769                                                 }
1770
1771 // Toggles for standalone plugins only
1772                                                 if(plugin->plugin_type == PLUGIN_STANDALONE)
1773                                                 {
1774 // Show
1775                                                         toggle_x -= PluginShow::calculate_w(mwindow) + 10;
1776                                                         if(toggle_x > min_x)
1777                                                         {
1778                                                                 if(current_show >= plugin_show_toggles.total)
1779                                                                 {
1780                                                                         PluginShow *plugin_show = new PluginShow(mwindow, toggle_x, toggle_y, plugin);
1781                                                                         add_subwindow(plugin_show);
1782                                                                         plugin_show_toggles.append(plugin_show);
1783                                                                 }
1784                                                                 else
1785                                                                 {
1786                                                                         plugin_show_toggles.values[current_show]->update(toggle_x, toggle_y, plugin);
1787                                                                 }
1788                                                                 current_show++;
1789                                                         }
1790                                                         toggle_x -= PluginPresetEdit::calculate_w(mwindow) + 10;
1791                                                         if(toggle_x > min_x)
1792                                                         {
1793                                                                 if(current_preset >= preset_edit_buttons.total)
1794                                                                 {
1795                                                                         PluginPresetEdit *preset_edit = new PluginPresetEdit(mwindow, toggle_x, toggle_y, plugin);
1796                                                                         add_subwindow(preset_edit);
1797                                                                         preset_edit_buttons.append(preset_edit);
1798                                                                 }
1799                                                                 else
1800                                                                 {
1801                                                                         preset_edit_buttons.values[current_preset]->update(toggle_x, toggle_y, plugin);
1802                                                                 }
1803                                                                 current_preset++;
1804                                                         }
1805                                                 }
1806                                         }
1807                                 }
1808                         }
1809                 }
1810         }
1811
1812 // Remove unused toggles
1813
1814         while(current_preset < preset_edit_buttons.total)
1815         {
1816                 preset_edit_buttons.remove_object_number(current_preset);
1817         }
1818         while(current_show < plugin_show_toggles.total)
1819         {
1820                 plugin_show_toggles.remove_object_number(current_show);
1821         }
1822
1823         while(current_on < plugin_on_toggles.total)
1824         {
1825                 plugin_on_toggles.remove_object_number(current_on);
1826         }
1827 }
1828
1829 void TrackCanvas::refresh_plugintoggles()
1830 {
1831         for(int i = 0; i < plugin_on_toggles.total; i++)
1832         {
1833                 PluginOn *on = plugin_on_toggles.values[i];
1834                 on->reposition_window(on->get_x(), on->get_y());
1835         }
1836         for(int i = 0; i < plugin_show_toggles.total; i++)
1837         {
1838                 PluginShow *show = plugin_show_toggles.values[i];
1839                 show->reposition_window(show->get_x(), show->get_y());
1840         }
1841         for(int i = 0; i < preset_edit_buttons.total; i++)
1842         {
1843                 PluginPresetEdit *preset_edit = preset_edit_buttons.values[i];
1844                 preset_edit->reposition_window(preset_edit->get_x(), preset_edit->get_y());
1845         }
1846 }
1847
1848 void TrackCanvas::draw_hard_edges()
1849 {
1850         int64_t x, y, w, h;
1851
1852         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1853                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1854                         if( !edit->hard_left && !edit->hard_right ) continue;
1855                         edit_dimensions(edit, x, y, w, h);
1856                         set_color(GREEN);
1857                         set_opaque();
1858                         int y1 = y;
1859                         if( track->show_titles() )
1860                                 y1 += mwindow->theme->get_image("title_bg_data")->get_h();
1861                         if( track->show_assets() )
1862                                 y1 += resource_h();
1863                         if( y1 == y )
1864                                 y1 += mwindow->theme->title_h;
1865                         if( edit->hard_left ) {
1866                                 ArrayList<int> xpt, ypt;
1867                                 xpt.append(x);              ypt.append(y1);
1868                                 xpt.append(x+HANDLE_W);     ypt.append(y1);
1869                                 xpt.append(x);              ypt.append(y1-HANDLE_H);
1870                                 fill_polygon(&xpt, &ypt);
1871                         }
1872                         if( edit->hard_right ) {
1873                                 ArrayList<int> xpt, ypt;   int x1 = x+w-1;
1874                                 xpt.append(x1);            ypt.append(y1);
1875                                 xpt.append(x1-HANDLE_W);   ypt.append(y1);
1876                                 xpt.append(x1);            ypt.append(y1-HANDLE_H);
1877                                 fill_polygon(&xpt, &ypt);
1878                         }
1879                 }
1880         }
1881 }
1882
1883 void TrackCanvas::draw_inout_points()
1884 {
1885 }
1886
1887
1888 void TrackCanvas::draw_drag_handle()
1889 {
1890         if(mwindow->session->current_operation == DRAG_EDITHANDLE2 ||
1891                 mwindow->session->current_operation == DRAG_PLUGINHANDLE2)
1892         {
1893 //printf("TrackCanvas::draw_drag_handle 1 %ld %ld\n", mwindow->session->drag_sample, mwindow->edl->local_session->view_start);
1894                 int64_t pixel1 = Units::round(mwindow->session->drag_position *
1895                         mwindow->edl->session->sample_rate /
1896                         mwindow->edl->local_session->zoom_sample -
1897                         mwindow->edl->local_session->view_start[pane->number]);
1898 //printf("TrackCanvas::draw_drag_handle 2 %d %jd\n", pane->number, pixel1);
1899                 set_color(!snapped ? GREEN : (snapped=0, YELLOW));
1900                 set_inverse();
1901 //printf("TrackCanvas::draw_drag_handle 3\n");
1902                 draw_line(pixel1, 0, pixel1, get_h());
1903                 set_opaque();
1904 //printf("TrackCanvas::draw_drag_handle 4\n");
1905         }
1906 }
1907
1908
1909 void TrackCanvas::draw_transitions()
1910 {
1911         int64_t x, y, w, h;
1912
1913         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1914                 if( !track->show_transitions() ) continue;
1915
1916                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1917                         if(!edit->transition) continue;
1918                         edit_dimensions(edit, x, y, w, h);
1919                         int strip_x = x, edit_y = y;
1920                         get_transition_coords(edit, x, y, w, h);
1921                         int strip_y = y - mwindow->theme->get_image("plugin_bg_data")->get_h();
1922                         if( track->show_assets() && track->show_titles() )
1923                                 edit_y += mwindow->theme->get_image("title_bg_data")->get_h();
1924                         if( strip_y < edit_y ) strip_y = edit_y;
1925
1926                         int strip_w = Units::round(edit->track->from_units(edit->transition->length) *
1927                                 mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample);
1928                         if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1929                             MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1930                                 PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
1931                                                 track->data_type);
1932                                 if( !server ) continue;
1933                                 VFrame *picon = server->get_picon();
1934                                 if( !picon ) continue;
1935                                 int picon_w = picon->get_w(), picon_h = picon->get_h();
1936                                 int track_h = edit->track->vertical_span(mwindow->theme);
1937                                 if( picon_h > track_h ) picon_h = track_h;
1938                                 draw_vframe(picon, x, y, w, h, 0, 0, picon_w, picon_h);
1939                         }
1940                         if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
1941                                 MWindowGUI::visible(strip_y, strip_y + h, 0, get_h())) {
1942                                 int x = strip_x, w = strip_w;
1943                                 if( x < 0 ) {  w -= -x;  x = 0; }
1944                                 if( w + x > get_w() ) w -= (w + x) - get_w();
1945
1946                                 draw_3segmenth( x, strip_y, w, strip_x, strip_w,
1947                                         mwindow->theme->get_image("plugin_bg_data"), 0);
1948                         }
1949                 }
1950         }
1951 }
1952
1953 void TrackCanvas::draw_loop_points()
1954 {
1955 //printf("TrackCanvas::draw_loop_points 1\n");
1956         if(mwindow->edl->local_session->loop_playback)
1957         {
1958 //printf("TrackCanvas::draw_loop_points 2\n");
1959                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
1960                         mwindow->edl->session->sample_rate /
1961                         mwindow->edl->local_session->zoom_sample -
1962                         mwindow->edl->local_session->view_start[pane->number]);
1963 //printf("TrackCanvas::draw_loop_points 3\n");
1964
1965                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1966                 {
1967                         set_color(GREEN);
1968                         draw_line(x, 0, x, get_h());
1969                 }
1970 //printf("TrackCanvas::draw_loop_points 4\n");
1971
1972                 x = Units::round(mwindow->edl->local_session->loop_end *
1973                         mwindow->edl->session->sample_rate /
1974                         mwindow->edl->local_session->zoom_sample -
1975                         mwindow->edl->local_session->view_start[pane->number]);
1976 //printf("TrackCanvas::draw_loop_points 5\n");
1977
1978                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1979                 {
1980                         set_color(GREEN);
1981                         draw_line(x, 0, x, get_h());
1982                 }
1983 //printf("TrackCanvas::draw_loop_points 6\n");
1984         }
1985 //printf("TrackCanvas::draw_loop_points 7\n");
1986 }
1987
1988 void TrackCanvas::draw_brender_range()
1989 {
1990         if( !mwindow->preferences->use_brender || !mwindow->brender_active ) return;
1991         if( mwindow->edl->session->brender_start >= mwindow->edl->session->brender_end ) return;
1992         if( mwindow->edl->session->brender_end > 0 )
1993         {
1994                 int64_t x1 = Units::round(mwindow->edl->session->brender_start *
1995                         mwindow->edl->session->sample_rate /
1996                         mwindow->edl->local_session->zoom_sample -
1997                         mwindow->edl->local_session->view_start[pane->number]);
1998                 if(MWindowGUI::visible(x1, x1 + 1, 0, get_w()))
1999                 {
2000                         set_color(RED);
2001                         draw_line(x1, 0, x1, get_h());
2002                 }
2003                 int64_t x2 = Units::round(mwindow->edl->session->brender_end *
2004                         mwindow->edl->session->sample_rate /
2005                         mwindow->edl->local_session->zoom_sample -
2006                         mwindow->edl->local_session->view_start[pane->number]);
2007
2008                 if(MWindowGUI::visible(x2, x2 + 1, 0, get_w()))
2009                 {
2010                         set_color(RED);
2011                         draw_line(x2, 0, x2, get_h());
2012                 }
2013         }
2014 }
2015
2016 // The operations which correspond to each automation type
2017 int TrackCanvas::auto_operations[AUTOMATION_TOTAL] =
2018 {
2019         DRAG_MUTE,
2020         DRAG_CAMERA_X,
2021         DRAG_CAMERA_Y,
2022         DRAG_CAMERA_Z,
2023         DRAG_PROJECTOR_X,
2024         DRAG_PROJECTOR_Y,
2025         DRAG_PROJECTOR_Z,
2026         DRAG_FADE,
2027         DRAG_PAN,
2028         DRAG_MODE,
2029         DRAG_MASK,
2030         DRAG_SPEED
2031 };
2032
2033 // The buttonpress operations, so nothing changes unless the mouse moves
2034 // a certain amount.  This allows the keyframe to be used to position the
2035 // insertion point without moving itself.
2036 static int pre_auto_operations[AUTOMATION_TOTAL] =
2037 {
2038         DRAG_MUTE,
2039         DRAG_CAMERA_X,
2040         DRAG_CAMERA_Y,
2041         DRAG_CAMERA_Z,
2042         DRAG_PROJECTOR_X,
2043         DRAG_PROJECTOR_Y,
2044         DRAG_PROJECTOR_Z,
2045         DRAG_FADE,
2046         DRAG_PAN_PRE,
2047         DRAG_MODE_PRE,
2048         DRAG_MASK_PRE,
2049         DRAG_SPEED
2050 };
2051
2052
2053 int TrackCanvas::do_keyframes(int cursor_x,
2054         int cursor_y,
2055         int draw,
2056         int buttonpress,
2057         int &new_cursor,
2058         int &update_cursor,
2059         int &rerender)
2060 {
2061 // Note: button 3 (right mouse button) is not eaten to allow
2062 // track context menu to appear
2063         int result = 0;
2064         EDLSession *session = mwindow->edl->session;
2065
2066
2067
2068         static BC_Pixmap *auto_pixmaps[AUTOMATION_TOTAL] =
2069         {
2070                 0, 0, 0, 0, 0, 0, 0, 0,
2071                 pankeyframe_pixmap,
2072                 modekeyframe_pixmap,
2073                 maskkeyframe_pixmap,
2074         };
2075
2076
2077
2078         for(Track *track = mwindow->edl->tracks->first;
2079                 track && !result;
2080                 track = track->next) {
2081                 Auto *auto_keyframe = 0;
2082                 Automation *automation = track->automation;
2083
2084
2085 // Handle keyframes in reverse drawing order if a button press
2086                 int start = 0;
2087                 int end = AUTOMATION_TOTAL;
2088                 int step = 1;
2089                 if(buttonpress)
2090                 {
2091                         start = AUTOMATION_TOTAL - 1;
2092                         end = -1;
2093                         step = -1;
2094                 }
2095                 for(int i = start; i != end && !result; i += step)
2096                 {
2097 // Event not trapped and automation visible
2098                         Autos *autos = automation->autos[i];
2099                         if(!result && session->auto_conf->autos[i] && autos) {
2100                                 switch(i) {
2101                                 case AUTOMATION_MODE:
2102                                 case AUTOMATION_PAN:
2103                                 case AUTOMATION_MASK:
2104                                         result = do_autos(track, automation->autos[i],
2105                                                 cursor_x, cursor_y, draw,
2106                                                 buttonpress, auto_pixmaps[i],
2107                                                 auto_keyframe, rerender);
2108                                                 break;
2109
2110                                 default: {
2111                                         switch(autos->get_type()) {
2112                                         case Autos::AUTOMATION_TYPE_FLOAT: {
2113                                                 Automation automation(0, track);
2114                                                 int grouptype = automation.autogrouptype(i, track);
2115                                                 if( buttonpress && i == AUTOMATION_SPEED ) {
2116                                                         mwindow->speed_before();
2117                                                 }
2118
2119                                                 if(draw) // Do dropshadow
2120                                                         result = do_float_autos(track, autos,
2121                                                                 cursor_x, cursor_y, draw,
2122                                                                 buttonpress, 1, 1, MDGREY,
2123                                                                 auto_keyframe, grouptype);
2124                                                 result = do_float_autos(track, autos,
2125                                                         cursor_x, cursor_y, draw, buttonpress,
2126                                                         0, 0, GWindowGUI::auto_colors[i],
2127                                                         auto_keyframe, grouptype);
2128
2129                                                 if( !result && buttonpress && i == AUTOMATION_SPEED )
2130                                                         mwindow->speed_after(-1);
2131                                                 int current_grouptype = mwindow->edl->local_session->zoombar_showautotype;
2132                                                 if( result && buttonpress && grouptype != current_grouptype ) {
2133                                                         mwindow->edl->local_session->zoombar_showautotype = grouptype;
2134                                                         mwindow->gui->zoombar->update_autozoom();
2135                                                 }
2136                                                 break; }
2137
2138                                         case Autos::AUTOMATION_TYPE_INT: {
2139                                                 if(draw) // Do dropshadow
2140                                                         result = do_int_autos(track, autos,
2141                                                                 cursor_x, cursor_y, draw,
2142                                                                 buttonpress, 1, 1, MDGREY,
2143                                                                 auto_keyframe);
2144                                                 result = do_int_autos(track, autos,
2145                                                         cursor_x, cursor_y, draw, buttonpress,
2146                                                         0, 0, GWindowGUI::auto_colors[i],
2147                                                         auto_keyframe);
2148                                                 break; }
2149                                         }
2150                                         break; }
2151                                 }
2152
2153                                 if(result)
2154                                 {
2155                                         if(mwindow->session->current_operation == auto_operations[i])
2156                                                 rerender = 1;
2157
2158 // printf("TrackCanvas::do_keyframes %d %d %d\n",
2159 // __LINE__,
2160 // mwindow->session->current_operation,
2161 // auto_operations[i]);
2162                                         if(buttonpress)
2163                                         {
2164                                                 if (buttonpress == 2 && auto_keyframe )
2165                                                 {
2166                                                         double position = track->from_units(auto_keyframe->position);
2167                                                         mwindow->edl->local_session->set_selectionstart(position);
2168                                                         mwindow->edl->local_session->set_selectionend(position);
2169                                                 }
2170                                                 if (buttonpress != 3)
2171                                                 {
2172                                                         if(i == AUTOMATION_FADE || i == AUTOMATION_SPEED)
2173                                                                 fill_ganged_autos(get_double_click(), 0, track,
2174                                                                         (FloatAuto*)mwindow->session->drag_auto);
2175                                                         mwindow->session->current_operation = pre_auto_operations[i];
2176                                                         update_drag_caption();
2177                                                         rerender = 1;
2178                                                 }
2179                                                 else if( auto_keyframe )
2180                                                 {
2181                                                         gui->keyframe_menu->update(automation,
2182                                                                 autos,
2183                                                                 auto_keyframe);
2184                                                         gui->keyframe_menu->activate_menu();
2185                                                         rerender = 1; // the position changes
2186                                                 }
2187                                                 else if( autos )
2188                                                 {
2189                                                         gui->keyframe_hide->update(autos);
2190                                                         gui->keyframe_hide->activate_menu();
2191                                                         rerender = 1; // the position changes
2192                                                 }
2193                                                 if(buttonpress == 1 && ctrl_down() &&
2194                                                    AUTOMATION_TYPE_FLOAT == autos->get_type())
2195                                                         rerender = 1; // special case: curve mode changed
2196                                         }
2197                                 }
2198                         }
2199                 }
2200
2201                 if(!result && session->auto_conf->plugins) {
2202                         Plugin *plugin;
2203                         KeyFrame *keyframe;
2204                         result = do_plugin_autos(track, cursor_x, cursor_y,
2205                                 draw, buttonpress, plugin, keyframe);
2206                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY) {
2207                                 rerender = 1;
2208                         }
2209                         if(result && (buttonpress == 1)) {
2210                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2211                                 update_drag_caption();
2212                                 rerender = 1;
2213                         }
2214                         else if (result && (buttonpress == 3)) {
2215                                 gui->keyframe_menu->update(plugin, keyframe);
2216                                 gui->keyframe_menu->activate_menu();
2217                                 rerender = 1; // the position changes
2218                         }
2219                 }
2220         }
2221
2222 // Final pass to trap event
2223         for(int i = 0; i < AUTOMATION_TOTAL; i++) {
2224                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2225                         mwindow->session->current_operation == auto_operations[i]) {
2226                         result = 1;
2227                         break;
2228                 }
2229         }
2230
2231         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2232                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE) {
2233                 result = 1;
2234         }
2235
2236         update_cursor = 1;
2237         if(result) {
2238                 new_cursor = UPRIGHT_ARROW_CURSOR;
2239         }
2240
2241
2242         return result;
2243 }
2244
2245 void TrackCanvas::draw_keyframe_reticle()
2246 {
2247         int keyframe_hairline = mwindow->preferences->keyframe_reticle;
2248         if( keyframe_hairline == HAIRLINE_NEVER ) return;
2249
2250         int current_op = mwindow->session->current_operation, dragging = 0;
2251         for( int i=0; !dragging && i<AUTOMATION_TOTAL; ++i )
2252                 if( current_op == auto_operations[i] ) dragging = 1;
2253         if( dragging && !mwindow->session->drag_auto ) dragging = 0;
2254
2255         int autoidx = dragging && keyframe_hairline != HAIRLINE_ALWAYS ?
2256                 mwindow->session->drag_auto->autos->autoidx : -1;
2257
2258         if( get_buttonpress() == 1 && dragging &&
2259             keyframe_hairline == HAIRLINE_DRAGGING ) {
2260                 draw_hairline(mwindow->session->drag_auto, RED, 1);
2261                 return;
2262         }
2263
2264         if( keyframe_hairline == HAIRLINE_ALWAYS || ( get_buttonpress() == 2 &&
2265             keyframe_hairline == HAIRLINE_DRAGGING && dragging ) ) {
2266                 int show = dragging || keyframe_hairline == HAIRLINE_ALWAYS ? 1 : 0;
2267                 for( Track *track = mwindow->edl->tracks->first; track; track=track->next ) {
2268                         Automation *automation = track->automation;
2269                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2270                                 if( !mwindow->edl->session->auto_conf->autos[i] ) continue;
2271                                 // automation visible
2272                                 Autos *autos = automation->autos[i];
2273                                 if( !autos ) continue;
2274                                 if( autoidx >= 0 && autos->autoidx != autoidx ) continue;
2275                                 for( Auto *auto_keyframe=autos->first; auto_keyframe;
2276                                      auto_keyframe = auto_keyframe->next ) {
2277                                         draw_hairline(auto_keyframe, BLUE, show);
2278                                 }
2279                         }
2280                 }
2281
2282                 if( dragging )
2283                         draw_hairline(mwindow->session->drag_auto, RED, 1);
2284         }
2285 }
2286
2287 void TrackCanvas::draw_auto(Auto *current,
2288         int x,
2289         int y,
2290         int center_pixel,
2291         int zoom_track)
2292 {
2293         int x1, y1, x2, y2;
2294
2295         x1 = x - HANDLE_W / 2;
2296         x2 = x + HANDLE_W / 2;
2297         y1 = center_pixel + y - HANDLE_W / 2;
2298         y2 = center_pixel + y + HANDLE_W / 2;
2299
2300         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2301         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2302
2303         draw_box(x1, y1, x2 - x1, y2 - y1);
2304 }
2305
2306
2307 // This draws lines for bezier in & out controls
2308 void TrackCanvas::draw_cropped_line(int x1,
2309         int y1,
2310         int x2,
2311         int y2,
2312         int min_y,
2313         int max_y)
2314 {
2315
2316
2317 // Don't care about x since it is clipped by the window.
2318 // Put y coords in ascending order
2319         if(y2 < y1) {
2320                 y2 ^= y1; y1 ^= y2; y2 ^= y1;
2321                 x2 ^= x1; x1 ^= x2; x2 ^= x1;
2322         }
2323
2324
2325
2326         double slope = (double)(x2 - x1) / (y2 - y1);
2327 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2328         if(y1 < min_y) {
2329                 x1 = (int)(x1 + (min_y - y1) * slope);
2330                 y1 = min_y;
2331         }
2332         else if(y1 >= max_y) {
2333                 x1 = (int)(x1 + (max_y - 1 - y1) * slope);
2334                 y1 = max_y - 1;
2335         }
2336
2337         if(y2 >= max_y) {
2338                 x2 = (int)(x2 + (max_y - 1 - y2) * slope);
2339                 y2 = max_y - 1;
2340         }
2341         else if(y2 < min_y) {
2342                 x2 = (int)(x2 + (min_y - y2) * slope);
2343                 y1 = min_y;
2344         }
2345
2346
2347 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2348         if( y1 >= min_y && y1 < max_y &&
2349             y2 >= min_y && y2 < max_y )
2350                 draw_line(x1, y1, x2, y2);
2351 }
2352
2353
2354 void TrackCanvas::draw_floatauto(FloatAuto *current,
2355         int x,
2356         int y,
2357         int in_x,
2358         int in_y,
2359         int out_x,
2360         int out_y,
2361         int center_pixel,
2362         int zoom_track,
2363         int color)
2364 {
2365         int x1 = x - HANDLE_W / 2; // Center
2366         int x2 = x + HANDLE_W / 2;
2367         int y1 = center_pixel + y - HANDLE_H / 2;
2368         int y2 = center_pixel + y + HANDLE_H / 2;
2369         int ymin = center_pixel - zoom_track / 2;
2370         int ymax = center_pixel + zoom_track / 2;
2371         CLAMP(y1, ymin, ymax);
2372         CLAMP(y2, ymin, ymax);
2373
2374         if(y2 - 1 > y1)
2375         {
2376                 set_color(BLACK);
2377                 draw_box(x1 + 1, y1 + 1, x2 - x1, y2 - y1);
2378                 set_color(color);
2379                 draw_box(x1, y1, x2 - x1, y2 - y1);
2380         }
2381
2382 // show bezier control points (only) if this
2383 // floatauto doesn't adjust it's tangents automatically
2384         if(current->curve_mode != FloatAuto::FREE &&
2385            current->curve_mode != FloatAuto::TFREE)
2386                 return;
2387
2388         if(in_x != x)
2389                 draw_floatauto_ctrlpoint(x, y, in_x, in_y, center_pixel, zoom_track, color);
2390         if(out_x != x)
2391                 draw_floatauto_ctrlpoint(x, y, out_x, out_y, center_pixel, zoom_track, color);
2392 }
2393
2394 inline int quantize(float f) { return (int)floor(f + 0.5); }
2395
2396 inline void TrackCanvas::draw_floatauto_ctrlpoint(
2397         int x, int y, int cp_x, int cp_y, int center_pixel,
2398         int zoom_track, int color)
2399 // draw the tangent and a handle for given b├ęzier ctrl point
2400 {
2401         bool handle_visible = (abs(cp_y) <= zoom_track / 2);
2402
2403         float slope = (float)(cp_y - y)/(cp_x - x);
2404         CLAMP(cp_y, -zoom_track / 2, zoom_track / 2);
2405         if(slope != 0)
2406                 cp_x = x + quantize((cp_y - y) / slope);
2407
2408         y    += center_pixel;
2409         cp_y += center_pixel;
2410
2411         // drawing the tangent as a dashed line...
2412         int const dash = HANDLE_W;
2413         int const gap  = HANDLE_W / 2;
2414         float sx = 3 * (cp_x - x) / 4.;
2415         float ex = 0;
2416
2417         // q is the x displacement for a unit line of slope
2418         float q = (sx > 0 ? 1 : -1) / sqrt(1 + slope * slope);
2419
2420         float dist = 1/q * sx;
2421         if( dist > dash )
2422                 ex = sx - q * dash;
2423
2424         set_color(color);
2425         do {
2426                 float sy = slope * sx, ey = slope * ex;
2427                 draw_line(quantize(sx + x), quantize(sy + y), quantize(ex + x), quantize(ey + y));
2428                 sx = ex - q * gap;
2429                 ex = sx - q * dash;
2430         } while(q*ex > 0);
2431
2432         if(handle_visible)
2433         {
2434                 int r = HANDLE_W / 2;
2435                 int cp_x1 = cp_x - r;
2436                 int cp_y1 = cp_y - r;
2437                 set_color(BLACK);
2438                 draw_disc(cp_x1, cp_y1, 2 * r, 2 * r);
2439                 set_color(color);
2440                 draw_circle(cp_x1, cp_y1, 2 * r, 2 * r);
2441         }
2442 }
2443
2444
2445 int TrackCanvas::test_auto(Auto *current,
2446         int x, int y, int center_pixel, int zoom_track,
2447         int cursor_x, int cursor_y, int buttonpress)
2448 {
2449         int x1, y1, x2, y2;
2450         int result = 0;
2451
2452         x1 = x - HANDLE_W / 2;
2453         x2 = x + HANDLE_W / 2;
2454         y1 = center_pixel + y - HANDLE_H / 2;
2455         y2 = center_pixel + y + HANDLE_H / 2;
2456         int ymin = center_pixel - zoom_track / 2;
2457         int ymax = center_pixel + zoom_track / 2;
2458         CLAMP(y1, ymin, ymax);
2459         CLAMP(y2, ymin, ymax);
2460
2461         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2462         {
2463                 if(buttonpress && buttonpress != 3)
2464                 {
2465                         mwindow->session->drag_auto = current;
2466                         mwindow->session->drag_start_percentage = (float)((IntAuto*)current)->value;
2467                         // Toggle Autos don't respond to vertical zoom, they always show up
2468                         // with "on" == 100% == line on top
2469                         mwindow->session->drag_start_position = current->position;
2470                         mwindow->session->drag_origin_x = cursor_x;
2471                         mwindow->session->drag_origin_y = cursor_y;
2472                 }
2473                 result = 1;
2474         }
2475
2476         if(buttonpress && buttonpress != 3 && result)
2477         {
2478 //printf("TrackCanvas::test_auto %d\n", __LINE__);
2479                 mwindow->undo->update_undo_before();
2480         }
2481
2482         return result;
2483 }
2484
2485 // some Helpers for test_floatauto(..)
2486 // and for dragging the tangents/ctrl points
2487 inline float test_curve_line( int x0, int y0, int ctrl_x, int ctrl_y,
2488         float cursor_x, float cursor_y)
2489 {
2490 // Control point switched off?
2491         if( x0 == ctrl_x ) return 0.0;
2492         double x1 = ctrl_x-x0, y1 = ctrl_y-y0;
2493         double x = cursor_x-x0, y = cursor_y-y0;
2494 // wrong side of ctrl handle
2495         if( x*x1 < 0 ) return 0.0;
2496 // outside handle radius
2497         if( (x*x + y*y) > (x1*x1 + y1*y1) ) return 0;
2498         double xx1 = x1*x1, yy1 = y1*y1;
2499         double xx = x*x, yy = y*y;
2500 // distance squared from cursor to cursor projected to line from ctrl to handle
2501 //   along a line perpendicular to line from ctrl to handle (closest approach)
2502 // (x**2*y1**2 - 2*x*x1*y*y1 + x1**2*y**2)/(x1**2 + y1**2)
2503         double dist2 = (xx*yy1 - 2*x*x1*y*y1 + xx1*yy)/(xx1 + yy1);
2504         return dist2 < (HANDLE_W*HANDLE_W)/4. ? x/x1 : 0.;
2505 }
2506
2507
2508 inline
2509 float levered_position(float position, float ref_pos)
2510 {
2511         if( 1e-6 > fabs(ref_pos) || isnan(ref_pos))
2512                 return 0.0;
2513         return ref_pos / position;
2514 }
2515
2516
2517 float TrackCanvas::value_to_percentage(float auto_value, int autogrouptype)
2518 // transforms automation value into current display coords,
2519 // dependant on current automation display range for the given kind of automation
2520 {
2521         if(!mwindow || !mwindow->edl) return 0;
2522         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2523         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2524         float automation_range = automation_max - automation_min;
2525         if( 0 >= automation_range || isnan(auto_value) || isinf(auto_value) )
2526                 return 0;
2527         return (auto_value - automation_min) / automation_range;
2528 }
2529
2530
2531
2532 int TrackCanvas::test_floatauto(FloatAuto *current, int x, int y, int in_x,
2533         int in_y, int out_x, int out_y, int center_pixel, int zoom_track,
2534         int cursor_x, int cursor_y, int buttonpress, int autogrouptype)
2535 {
2536         int result = 0;
2537
2538         int x1 = x - HANDLE_W / 2;
2539         int x2 = x + HANDLE_W / 2;
2540         int y1 = center_pixel + y - HANDLE_W / 2;
2541         int y2 = center_pixel + y + HANDLE_W / 2;
2542         int ymin = center_pixel - zoom_track / 2;
2543         int ymax = center_pixel + zoom_track / 2;
2544         CLAMP(y1, ymin, ymax);
2545         CLAMP(y2, ymin, ymax);
2546
2547         int in_x1 = in_x - HANDLE_W / 2;
2548         int in_x2 = in_x + HANDLE_W / 2;
2549         int in_y1 = center_pixel + in_y - HANDLE_W / 2;
2550         int in_y2 = center_pixel + in_y + HANDLE_W / 2;
2551         CLAMP(in_y1, ymin, ymax);
2552         CLAMP(in_y2, ymin, ymax);
2553
2554         int out_x1 = out_x - HANDLE_W / 2;
2555         int out_x2 = out_x + HANDLE_W / 2;
2556         int out_y1 = center_pixel + out_y - HANDLE_W / 2;
2557         int out_y2 = center_pixel + out_y + HANDLE_W / 2;
2558         CLAMP(out_y1, ymin, ymax);
2559         CLAMP(out_y2, ymin, ymax);
2560
2561
2562 //printf("TrackCanvas::test_floatauto %d %d %d %d %d %d\n", cursor_x, cursor_y, x1, x2, y1, y2);
2563 // buttonpress could be the start of a drag operation
2564 #define INIT_DRAG(POS,VAL) \
2565                 mwindow->session->drag_auto = current;      \
2566                 mwindow->session->drag_origin_x = cursor_x;  \
2567                 mwindow->session->drag_origin_y = cursor_y;   \
2568                 mwindow->session->drag_start_position = (POS); \
2569                 mwindow->session->drag_start_percentage = (VAL);
2570
2571 #define WITHIN(X1,X2,Y1,Y2) (cursor_x >=(X1) && cursor_x <(X2) && cursor_y >=(Y1) && cursor_y <(Y2) )
2572
2573
2574 // without modifier we are manipulating the automation node
2575 // with ALT it's about dragging only the value of the node
2576 // with SHIFT the value snaps to the value of neighbouring nodes
2577 // CTRL indicates we are rather manipulating the tangent(s) of the node
2578
2579         if(!ctrl_down())
2580         {
2581                 if( WITHIN(x1,x2,y1,y2))
2582                 {       // cursor hits node
2583                         result = 1;
2584
2585                         if(buttonpress && (buttonpress != 3))
2586                         {
2587                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2588                                 mwindow->session->drag_handle = 0;
2589                 }       }
2590         }
2591         else // ctrl_down()
2592         {
2593                 if( WITHIN(x1,x2,y1,y2))
2594                 {
2595                         result = 1;
2596                         if(buttonpress && (buttonpress != 3))
2597                         {
2598                                 // could be ctrl-click or ctrl-drag
2599                                 // click would cycle through tangent modes
2600                                 ((FloatAuto*)current)->toggle_curve_mode();
2601
2602                                 // drag will start dragging the tangent, if applicable
2603                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2604                                 mwindow->session->drag_handle = 0;
2605                         }
2606                 }
2607
2608                 float lever = 0.0; // we use the tangent as a draggable lever. 1.0 is at the ctrl point
2609
2610 // Test in control
2611                 if( in_x != x && current->position > 0 &&
2612                         (FloatAuto::FREE == current->curve_mode ||
2613                          FloatAuto::TFREE == current->curve_mode))
2614 // act on in control handle only if
2615 // tangent is significant and is editable (not automatically choosen)
2616                 {
2617                         lever = test_curve_line(x, y, in_x, in_y, cursor_x, cursor_y-center_pixel);
2618  // either cursor in ctrl-point handle or cursor on tangent line
2619                         if( WITHIN(in_x1,in_x2,in_y1,in_y2) || lever > 0.0 ) {
2620                                 result = 1;
2621                                 if(buttonpress && (buttonpress != 3)) {
2622                                         if(lever == 0.0) lever=1.0; // we entered by dragging the handle...
2623 //                                      lever = 1.0;
2624                                         mwindow->session->drag_handle = 1;
2625                                         float new_invalue = current->get_value() + lever * current->get_control_in_value();
2626                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_in_position()),
2627                                                   value_to_percentage(new_invalue, autogrouptype))
2628                                 }
2629                         }
2630                 }
2631
2632 // Test out control
2633                 if(out_x != x &&
2634                         (FloatAuto::FREE == current->curve_mode ||
2635                          FloatAuto::TFREE == current->curve_mode))
2636 // act on out control only if tangent is significant and is editable
2637                 {
2638                         lever = test_curve_line(x, y, out_x, out_y, cursor_x, cursor_y-center_pixel);
2639                         if(WITHIN(out_x1,out_x2,out_y1,out_y2) || lever > 0.0 ) {
2640                                 result = 1;
2641                                 if(buttonpress && (buttonpress != 3)) {
2642                                         if(lever == 0.0) lever=1.0;
2643 //                                      lever = 1.0;
2644                                         mwindow->session->drag_handle = 2;
2645                                         float new_outvalue = current->get_value() + lever * current->get_control_out_value();
2646                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_out_position()),
2647                                                   value_to_percentage(new_outvalue, autogrouptype))
2648                                 }
2649                         }
2650                 }
2651         } // end ctrl_down()
2652
2653 #undef WITHIN
2654 #undef INIT_DRAG
2655
2656 // if(buttonpress)
2657 // 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",
2658 // mwindow->session->drag_handle,
2659 // ctrl_down(),
2660 // cursor_x,
2661 // cursor_y,
2662 // x1, x2, y1, y2);
2663         if(buttonpress && (buttonpress != 3) && result)
2664         {
2665                 mwindow->undo->update_undo_before();
2666         }
2667
2668         return result;
2669 }
2670
2671
2672 // Get the float value & y for position x on the canvas
2673 #define X_TO_FLOATLINE(x) \
2674         int64_t position1 = (int64_t)(unit_start + x * zoom_units); \
2675         int64_t position2 = (int64_t)(unit_start + x * zoom_units) + 1; \
2676 /* Call by reference fails for some reason here */ \
2677         float value1 = autos->get_value(position1, PLAY_FORWARD, previous1, next1); \
2678         float value2 = autos->get_value(position2, PLAY_FORWARD, previous1, next1); \
2679         double position = unit_start + x * zoom_units; \
2680         double value = 0; \
2681         if(position2 > position1) \
2682         { \
2683                 value = value1 + \
2684                         (value2 - value1) * \
2685                         (position - position1) / \
2686                         (position2 - position1); \
2687         } \
2688         else \
2689         { \
2690                 value = value1; \
2691         } \
2692         AUTOMATIONCLAMPS(value, autogrouptype); \
2693         int y = center_pixel + \
2694                 (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2695
2696
2697 void TrackCanvas::draw_floatline(int center_pixel,
2698         FloatAuto *previous, FloatAuto *next, FloatAutos *autos,
2699         double unit_start, double zoom_units, double yscale,
2700         int x1, int y1, int x2, int y2,
2701         int color, int autogrouptype)
2702 {
2703 // Solve bezier equation for either every pixel or a certain large number of
2704 // points.
2705
2706 // Not using slope intercept
2707         x1 = MAX(0, x1);
2708         int prev_y = y1 + center_pixel;
2709
2710
2711 // Call by reference fails for some reason here
2712         FloatAuto *previous1 = previous, *next1 = next;
2713         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2714         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2715         float automation_range = automation_max - automation_min;
2716
2717         for(int x = x1; x < x2; x++)
2718         {
2719 // Interpolate value between frames
2720                 X_TO_FLOATLINE(x)
2721
2722                 if(/* x > x1 && */
2723                         y >= center_pixel - yscale / 2 &&
2724                         y < center_pixel + yscale / 2 - 1)
2725                 {
2726 // printf("TrackCanvas::draw_floatline y=%d min=%d max=%d\n",
2727 // y,
2728 // (int)(center_pixel - yscale / 2),
2729 // (int)(center_pixel + yscale / 2 - 1));
2730
2731 //printf("draw_line(%d,%d,  %d,%d)\n", x - 1, prev_y  , x, y);
2732                         draw_line(x - 1, prev_y  , x, y   );
2733                 }
2734                 prev_y = y;
2735         }
2736 }
2737
2738
2739
2740
2741
2742 int TrackCanvas::test_floatline(int center_pixel,
2743                 FloatAutos *autos,
2744                 double unit_start,
2745                 double zoom_units,
2746                 double yscale,
2747                 int x1,
2748                 int x2,
2749                 int cursor_x,
2750                 int cursor_y,
2751                 int buttonpress,
2752                 int autogrouptype)
2753 {
2754         int result = 0;
2755
2756
2757         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2758         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2759         float automation_range = automation_max - automation_min;
2760         FloatAuto *previous1 = 0, *next1 = 0;
2761         X_TO_FLOATLINE(cursor_x);
2762
2763         if(cursor_x >= x1 &&
2764                 cursor_x < x2 &&
2765                 cursor_y >= y - HANDLE_W / 2 &&
2766                 cursor_y < y + HANDLE_W / 2 &&
2767                 !ctrl_down())
2768         {
2769                 result = 1;
2770
2771 // Menu
2772                 if(buttonpress == 3)
2773                 {
2774                 }
2775                 else
2776 // Create keyframe
2777                 if(buttonpress)
2778                 {
2779                         Auto *current;
2780                         mwindow->undo->update_undo_before();
2781                         double position = autos->track->from_units(position1);
2782                         position = mwindow->edl->align_to_frame(position, 0);
2783                         int64_t new_position = autos->track->to_units(position,0);
2784                         current = mwindow->session->drag_auto = autos->insert_auto(new_position);
2785                         ((FloatAuto*)current)->set_value(value);
2786                         mwindow->session->drag_start_percentage = value_to_percentage(value, autogrouptype);
2787                         mwindow->session->drag_start_position = current->position;
2788                         mwindow->session->drag_origin_x = cursor_x;
2789                         mwindow->session->drag_origin_y = cursor_y;
2790                         mwindow->session->drag_handle = 0;
2791                 }
2792         }
2793
2794
2795         return result;
2796 }
2797
2798
2799 void TrackCanvas::fill_ganged_autos(int all, float change, Track *skip, FloatAuto *fauto)
2800 {
2801         if( !skip->gang ) return;
2802 // Handles the special case of modifying a fadeauto
2803 // when there are ganged faders on several tracks
2804         double position = skip->from_units(fauto->position);
2805         int autoidx = fauto->autos->autoidx;
2806
2807         for(Track *current = mwindow->edl->tracks->first; current; current = NEXT) {
2808                 if( (all || current->data_type == skip->data_type) &&
2809                     current->gang && current->record && current != skip ) {
2810                         FloatAutos *fade_autos = (FloatAutos*)current->automation->autos[autoidx];
2811                         float auto_min = mwindow->edl->local_session->automation_mins[fade_autos->autogrouptype];
2812                         float auto_max = mwindow->edl->local_session->automation_maxs[fade_autos->autogrouptype];
2813                         int64_t current_position = current->to_units(position, 1);
2814                         FloatAuto *keyframe = (FloatAuto*)fade_autos->get_auto_at_position(position);
2815                         if( keyframe ) {
2816 // keyframe exists, just change it
2817                                 float value = keyframe->get_value();
2818                                 float new_value = value + change;
2819                                 CLAMP(new_value, auto_min, auto_max);
2820                                 keyframe->adjust_to_new_coordinates(current_position, new_value);
2821                         }
2822                         else {
2823 // create keyframe on neighbouring track at the point in time given by fauto
2824                                 FloatAuto *previous = 0, *next = 0;
2825                                 float value = fade_autos->get_value(current_position, PLAY_FORWARD, previous, next);
2826                                 float new_value = value + change;
2827                                 CLAMP(new_value, auto_min, auto_max);
2828                                 keyframe = (FloatAuto*)fade_autos->insert_auto(current_position);
2829                                 keyframe->set_value(new_value);
2830                         }
2831                         mwindow->session->drag_auto_gang->append((Auto *)keyframe);
2832                 }
2833         }
2834 }
2835
2836 void TrackCanvas::update_ganged_autos(float change, Track *skip, FloatAuto *fauto)
2837 {
2838         double position = skip->from_units(fauto->position);
2839 // Move the gangs
2840         for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
2841                 FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2842                 int64_t keyframe_position = keyframe->autos->track->to_units(position, 1);
2843                 float new_value = keyframe->get_value() + change;
2844                 CLAMP(new_value,
2845                       mwindow->edl->local_session->automation_mins[keyframe->autos->autogrouptype],
2846                       mwindow->edl->local_session->automation_maxs[keyframe->autos->autogrouptype]);
2847                 keyframe->adjust_to_new_coordinates(keyframe_position, new_value);
2848         }
2849 }
2850
2851 void TrackCanvas::clear_ganged_autos()
2852 {
2853 // remove the gangs
2854         for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
2855                 FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2856                 keyframe->autos->remove_nonsequential(keyframe);
2857         }
2858         mwindow->session->drag_auto_gang->remove_all();
2859 }
2860
2861
2862 void TrackCanvas::draw_toggleline(int center_pixel,
2863         int x1,
2864         int y1,
2865         int x2,
2866         int y2)
2867 {
2868         draw_line(x1, center_pixel + y1, x2, center_pixel + y1);
2869
2870         if(y2 != y1)
2871         {
2872                 draw_line(x2, center_pixel + y1, x2, center_pixel + y2);
2873         }
2874 }
2875
2876 int TrackCanvas::test_toggleline(Autos *autos,
2877         int center_pixel,
2878         int x1,
2879         int y1,
2880         int x2,
2881         int y2,
2882         int cursor_x,
2883         int cursor_y,
2884         int buttonpress)
2885 {
2886         int result = 0;
2887         if(cursor_x >= x1 && cursor_x < x2)
2888         {
2889                 int miny = center_pixel + y1 - HANDLE_W / 2;
2890                 int maxy = center_pixel + y1 + HANDLE_W / 2;
2891                 if(cursor_y >= miny && cursor_y < maxy)
2892                 {
2893                         result = 1;
2894
2895 // Menu
2896                         if(buttonpress == 3)
2897                         {
2898                         }
2899                         else
2900 // Insert keyframe
2901                         if(buttonpress)
2902                         {
2903                                 Auto *current;
2904                                 double position = (double)(cursor_x +
2905                                                 mwindow->edl->local_session->view_start[pane->number]) *
2906                                         mwindow->edl->local_session->zoom_sample /
2907                                         mwindow->edl->session->sample_rate;
2908                                 int64_t unit_position = autos->track->to_units(position, 0);
2909                                 int new_value = (int)((IntAutos*)autos)->get_automation_constant(unit_position, unit_position);
2910
2911                                 mwindow->undo->update_undo_before();
2912
2913                                 current = mwindow->session->drag_auto = autos->insert_auto(unit_position);
2914                                 ((IntAuto*)current)->value = new_value;
2915                                 // Toggle Autos don't respond to vertical zoom, they always show up
2916                                 // with "on" == 100% == line on top
2917                                 mwindow->session->drag_start_percentage = (float)new_value;
2918                                 mwindow->session->drag_start_position = current->position;
2919                                 mwindow->session->drag_origin_x = cursor_x;
2920                                 mwindow->session->drag_origin_y = cursor_y;
2921                         }
2922                 }
2923         };
2924
2925         return result;
2926 }
2927
2928 void TrackCanvas::calculate_viewport(Track *track,
2929         double &view_start,   // Seconds
2930         double &unit_start,
2931         double &view_end,     // Seconds
2932         double &unit_end,
2933         double &yscale,
2934         int &center_pixel,
2935         double &zoom_sample,
2936         double &zoom_units)
2937 {
2938
2939         view_start = (double)mwindow->edl->local_session->view_start[pane->number] *
2940                 mwindow->edl->local_session->zoom_sample /
2941                 mwindow->edl->session->sample_rate;
2942         unit_start = track->to_doubleunits(view_start);
2943         view_end = (double)(mwindow->edl->local_session->view_start[pane->number] +
2944                 get_w()) *
2945                 mwindow->edl->local_session->zoom_sample /
2946                 mwindow->edl->session->sample_rate;
2947         unit_end = track->to_doubleunits(view_end);
2948
2949         int y = track->y_pixel
2950                         - mwindow->edl->local_session->track_start[pane->number];
2951         int has_titles = track->show_titles();
2952         int has_assets = track->show_assets();
2953         double title_bg_h = mwindow->theme->get_image("title_bg_data")->get_h();
2954         double asset_h = resource_h();
2955         double title_h = mwindow->theme->title_h;
2956         double ys = has_assets ? asset_h : has_titles ? title_bg_h : 0;
2957         double dy = has_titles ?
2958                 ( has_assets ? title_bg_h + asset_h/2 : title_bg_h/2) :
2959                 ( has_assets ? asset_h/2 : 0) ;
2960         if( dy < title_h/2 ) { ys = title_h;  dy = ys / 2; }
2961         yscale = ys;
2962         center_pixel = y + dy;
2963
2964         zoom_sample = mwindow->edl->local_session->zoom_sample;
2965         zoom_units = track->to_doubleunits(zoom_sample / mwindow->edl->session->sample_rate);
2966 }
2967
2968 float TrackCanvas::percentage_to_value(float percentage,
2969         int is_toggle,
2970         Auto *reference,
2971         int autogrouptype)
2972 {
2973         float result;
2974         if(is_toggle)
2975         {
2976                 if(percentage > 0.5)
2977                         result = 1;
2978                 else
2979                         result = 0;
2980         }
2981         else
2982         {
2983                 float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2984                 float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2985                 float automation_range = automation_max - automation_min;
2986
2987                 result = percentage * automation_range + automation_min;
2988                 if(reference)
2989                 {
2990                         FloatAuto *ptr = (FloatAuto*)reference;
2991                         result -= ptr->get_value();
2992                 }
2993 //printf("TrackCanvas::percentage_to_value %d %f\n", __LINE__, result);
2994         }
2995         return result;
2996 }
2997
2998
2999 void TrackCanvas::calculate_auto_position(double *x, double *y,
3000         double *in_x, double *in_y, double *out_x, double *out_y,
3001         Auto *current, double unit_start, double zoom_units, double yscale,
3002         int autogrouptype)
3003 {
3004         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
3005         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
3006         float automation_range = automation_max - automation_min;
3007         FloatAuto *ptr = (FloatAuto*)current;
3008         *x = (double)(ptr->position - unit_start) / zoom_units;
3009         *y = ((ptr->get_value() - automation_min) / automation_range - 0.5) * -yscale;
3010
3011         if(in_x) {
3012 //              *in_x = EQUIV(ptr->control_in_value, 0.0) ? *x : *x - mwindow->theme->control_pixels;
3013                 *in_x = (double)(ptr->position + ptr->get_control_in_position() - unit_start) / zoom_units;
3014         }
3015
3016         if(in_y) {
3017                 *in_y = (((ptr->get_value() + ptr->get_control_in_value()) -
3018                                 automation_min) / automation_range - 0.5) * -yscale;
3019         }