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