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;
3043                         ax2 = get_w();
3044                         ay2 = ay + slope * (get_w() - ax);
3045                 }
3046
3047                 if(ax < 0) {
3048                         ay = ay + slope * (0 - ax);
3049                         ax = 0;
3050                 }
3051
3052 // Draw or test handle
3053                 if( current && !result && current != autos->default_auto ) {
3054                         if( !draw && track->record ) {
3055                                 result = test_floatauto((FloatAuto*)current, (int)ax2, (int)ay2,
3056                                         (int)in_x2, (int)in_y2, (int)out_x2, (int)out_y2,
3057                                         (int)center_pixel, (int)yscale, cursor_x, cursor_y,
3058                                         buttonpress, autogrouptype);
3059                                 if( result )
3060                                         auto_instance = current;
3061                         }
3062                         if( draw && draw_auto ) {
3063                                 draw_floatauto((FloatAuto*)current, (int)ax2 + x_offset, (int)ay2,
3064                                         (int)in_x2 + x_offset, (int)in_y2,
3065                                         (int)out_x2 + x_offset, (int)out_y2,
3066                                         (int)center_pixel + y_offset, (int)yscale, color);
3067                         }
3068                 }
3069
3070 // Draw or test joining line
3071                 if( !draw && !result && track->record /* && buttonpress != 3 */ ) {
3072                         result = test_floatline(center_pixel,
3073                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3074 // Exclude auto coverage from the end of the line.  The auto overlaps
3075                                 (int)ax, (int)ax2 - HANDLE_W / 2, cursor_x, cursor_y,
3076                                 buttonpress, autogrouptype);
3077                 }
3078                 if( draw )
3079                         draw_floatline(center_pixel, (FloatAuto*)previous, (FloatAuto*)current,
3080                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3081                                 (int)ax, (int)ay, (int)ax2, (int)ay2,
3082                                 color, autogrouptype);
3083
3084                 if( current ) {
3085                         previous = current;
3086                         current = NEXT;
3087                 }
3088
3089                 ax = ax2;  ay = ay2;
3090         } while( current && current->position <= unit_end && !result );
3091
3092         if( ax < get_w() && !result ) {
3093                 ax2 = get_w();  ay2 = ay;
3094                 if(!draw && track->record /* && buttonpress != 3 */ ) {
3095                         result = test_floatline(center_pixel,
3096                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3097                                 (int)ax, (int)ax2, cursor_x, cursor_y,
3098                                 buttonpress, autogrouptype);
3099                 }
3100                 if( draw )
3101                         draw_floatline(center_pixel,
3102                                 (FloatAuto*)previous, (FloatAuto*)current,
3103                                 (FloatAutos*)autos, unit_start, zoom_units, yscale,
3104                                 (int)ax, (int)ay, (int)ax2, (int)ay2,
3105                                 color, autogrouptype);
3106         }
3107
3108         return result;
3109 }
3110
3111
3112 int TrackCanvas::do_int_autos(Track *track,
3113                 Autos *autos,
3114                 int cursor_x,
3115                 int cursor_y,
3116                 int draw,
3117                 int buttonpress,
3118                 int x_offset,
3119                 int y_offset,
3120                 int color,
3121                 Auto *&auto_instance)
3122 {
3123         int result = 0;
3124         double view_start;
3125         double unit_start;
3126         double view_end;
3127         double unit_end;
3128         double yscale;
3129         int center_pixel;
3130         double zoom_sample;
3131         double zoom_units;
3132         double ax, ay, ax2, ay2;
3133
3134         auto_instance = 0;
3135
3136         if(draw) set_color(color);
3137
3138         calculate_viewport(track,
3139                 view_start,
3140                 unit_start,
3141                 view_end,
3142                 unit_end,
3143                 yscale,
3144                 center_pixel,
3145                 zoom_sample,
3146                 zoom_units);
3147
3148
3149         double high = -yscale * 0.8 / 2;
3150         double low = yscale * 0.8 / 2;
3151
3152 // Get first auto before start
3153         Auto *current;
3154         for(current = autos->last; current && current->position >= unit_start; current = PREVIOUS)
3155                 ;
3156
3157         if(current)
3158         {
3159                 ax = 0;
3160                 ay = ((IntAuto*)current)->value > 0 ? high : low;
3161                 current = NEXT;
3162         }
3163         else
3164         {
3165                 current = autos->first ? autos->first : autos->default_auto;
3166                 if(current)
3167                 {
3168                         ax = 0;
3169                         ay = ((IntAuto*)current)->value > 0 ? high : low;
3170                 }
3171                 else
3172                 {
3173                         ax = 0;
3174                         ay = yscale;
3175                 }
3176         }
3177
3178         do
3179         {
3180                 if(current)
3181                 {
3182                         ax2 = (double)(current->position - unit_start) / zoom_units;
3183                         ay2 = ((IntAuto*)current)->value > 0 ? high : low;
3184                 }
3185                 else
3186                 {
3187                         ax2 = get_w();
3188                         ay2 = ay;
3189                 }
3190
3191                 if(ax2 > get_w()) ax2 = get_w();
3192
3193                 if(current && !result)
3194                 {
3195                         if(current != autos->default_auto)
3196                         {
3197                                 if(!draw)
3198                                 {
3199                                         if(track->record)
3200                                         {
3201                                                 result = test_auto(current,
3202                                                         (int)ax2,
3203                                                         (int)ay2,
3204                                                         (int)center_pixel,
3205                                                         (int)yscale,
3206                                                         cursor_x,
3207                                                         cursor_y,
3208                                                         buttonpress);
3209                                                 if (result)
3210                                                         auto_instance = current;
3211                                         }
3212                                 }
3213                                 else
3214                                         draw_auto(current,
3215                                                 (int)ax2 + x_offset,
3216                                                 (int)ay2 + y_offset,
3217                                                 (int)center_pixel,
3218                                                 (int)yscale);
3219                         }
3220
3221                         current = NEXT;
3222                 }
3223
3224                 if(!draw)
3225                 {
3226                         if(!result)
3227                         {
3228                                 if(track->record /* && buttonpress != 3 */)
3229                                 {
3230                                         result = test_toggleline(autos,
3231                                                 center_pixel,
3232                                                 (int)ax,
3233                                                 (int)ay,
3234                                                 (int)ax2,
3235                                                 (int)ay2,
3236                                                 cursor_x,
3237                                                 cursor_y,
3238                                                 buttonpress);
3239                                 }
3240                         }
3241                 }
3242                 else
3243                         draw_toggleline(center_pixel + y_offset,
3244                                 (int)ax,
3245                                 (int)ay,
3246                                 (int)ax2,
3247                                 (int)ay2);
3248
3249                 ax = ax2;
3250                 ay = ay2;
3251         }while(current && current->position <= unit_end && !result);
3252
3253         if(ax < get_w() && !result)
3254         {
3255                 ax2 = get_w();
3256                 ay2 = ay;
3257                 if(!draw)
3258                 {
3259                         if(track->record /* && buttonpress != 3 */)
3260                         {
3261                                 result = test_toggleline(autos,
3262                                         center_pixel,
3263                                         (int)ax,
3264                                         (int)ay,
3265                                         (int)ax2,
3266                                         (int)ay2,
3267                                         cursor_x,
3268                                         cursor_y,
3269                                         buttonpress);
3270                         }
3271                 }
3272                 else
3273                         draw_toggleline(center_pixel + y_offset,
3274                                 (int)ax,
3275                                 (int)ay,
3276                                 (int)ax2,
3277                                 (int)ay2);
3278         }
3279         return result;
3280 }
3281
3282 int TrackCanvas::do_autos(Track *track,
3283                 Autos *autos,
3284                 int cursor_x,
3285                 int cursor_y,
3286                 int draw,
3287                 int buttonpress,
3288                 BC_Pixmap *pixmap,
3289                 Auto * &auto_instance,
3290                 int &rerender)
3291 {
3292         int result = 0;
3293
3294         double view_start;
3295         double unit_start;
3296         double view_end;
3297         double unit_end;
3298         double yscale;
3299         int center_pixel;
3300         double zoom_sample;
3301         double zoom_units;
3302
3303         calculate_viewport(track,
3304                 view_start,
3305                 unit_start,
3306                 view_end,
3307                 unit_end,
3308                 yscale,
3309                 center_pixel,
3310                 zoom_sample,
3311                 zoom_units);
3312
3313         Auto *current;
3314         auto_instance = 0;
3315
3316         for(current = autos->first; current && !result; current = NEXT)
3317         {
3318                 if(current->position >= unit_start && current->position < unit_end)
3319                 {
3320                         int64_t x, y;
3321                         x = (int64_t)((double)(current->position - unit_start) /
3322                                 zoom_units - (pixmap->get_w() / 2.0 + 0.5));
3323                         y = center_pixel - pixmap->get_h() / 2;
3324
3325                         if(!draw)
3326                         {
3327                                 if(cursor_x >= x && cursor_y >= y &&
3328                                         cursor_x < x + pixmap->get_w() &&
3329                                         cursor_y < y + pixmap->get_h())
3330                                 {
3331                                         result = 1;
3332                                         auto_instance = current;
3333
3334                                         if(buttonpress && (buttonpress != 3))
3335                                         {
3336                                                 mwindow->session->drag_auto = current;
3337                                                 mwindow->session->drag_start_position = current->position;
3338                                                 mwindow->session->drag_origin_x = cursor_x;
3339                                                 mwindow->session->drag_origin_y = cursor_y;
3340
3341                                                 double position = autos->track->from_units(current->position);
3342                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3343                                                         mwindow->edl->local_session->get_selectionend(1)) /
3344                                                         2;
3345
3346                                                 if(!shift_down())
3347                                                 {
3348                                                         mwindow->edl->local_session->set_selectionstart(position);
3349                                                         mwindow->edl->local_session->set_selectionend(position);
3350                                                 }
3351                                                 else
3352                                                 if(position < center)
3353                                                 {
3354                                                         mwindow->edl->local_session->set_selectionstart(position);
3355                                                 }
3356                                                 else
3357                                                         mwindow->edl->local_session->set_selectionend(position);
3358
3359                                                 rerender = 1;
3360                                         }
3361                                 }
3362                         }
3363                         else
3364                                 draw_pixmap(pixmap, x, y);
3365                 }
3366         }
3367
3368
3369         return result;
3370 }
3371
3372 // so this means it is always >0 when keyframe is found
3373 int TrackCanvas::do_plugin_autos(Track *track, int cursor_x, int cursor_y,
3374                 int draw, int buttonpress, Plugin* &keyframe_plugin,
3375                 KeyFrame* &keyframe_instance)
3376 {
3377         int result = 0;
3378
3379         double view_start;
3380         double unit_start;
3381         double view_end;
3382         double unit_end;
3383         double yscale;
3384         int center_pixel;
3385         double zoom_sample;
3386         double zoom_units;
3387
3388         if(!track->expand_view) return 0;
3389
3390         calculate_viewport(track,
3391                 view_start,
3392                 unit_start,
3393                 view_end,
3394                 unit_end,
3395                 yscale,
3396                 center_pixel,
3397                 zoom_sample,
3398                 zoom_units);
3399
3400
3401
3402         for(int i = 0; i < track->plugin_set.total && !result; i++)
3403         {
3404                 PluginSet *plugin_set = track->plugin_set.values[i];
3405                 int center_pixel = track->y_pixel -
3406                         mwindow->edl->local_session->track_start[pane->number];
3407                 if( track->show_titles() )
3408                         center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
3409                 if( track->show_assets() )
3410                         center_pixel += resource_h();
3411                 center_pixel += (i + 0.5) * mwindow->theme->get_image("plugin_bg_data")->get_h();
3412
3413                 for(Plugin *plugin = (Plugin*)plugin_set->first;
3414                         plugin && !result;
3415                         plugin = (Plugin*)plugin->next)
3416                 {
3417                         for(KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
3418                                 keyframe && !result;
3419                                 keyframe = (KeyFrame*)keyframe->next)
3420                         {
3421 //printf("TrackCanvas::draw_plugin_autos 3 %d\n", keyframe->position);
3422                                 if(keyframe->position >= unit_start && keyframe->position < unit_end)
3423                                 {
3424                                         int64_t x = (int64_t)((keyframe->position - unit_start) / zoom_units);
3425                                         int y = center_pixel - keyframe_pixmap->get_h() / 2;
3426
3427 //printf("TrackCanvas::draw_plugin_autos 4 %d %d\n", x, center_pixel);
3428                                         if(!draw)
3429                                         {
3430                                                 if(cursor_x >= x && cursor_y >= y &&
3431                                                         cursor_x < x + keyframe_pixmap->get_w() &&
3432                                                         cursor_y < y + keyframe_pixmap->get_h())
3433                                                 {
3434                                                         result = 1;
3435                                                         keyframe_plugin = plugin;
3436                                                         keyframe_instance = keyframe;
3437
3438                                                         if(buttonpress)
3439                                                         {
3440                                                                 mwindow->session->drag_auto = keyframe;
3441                                                                 mwindow->session->drag_start_position = keyframe->position;
3442                                                                 mwindow->session->drag_origin_x = cursor_x;
3443                                                                 mwindow->session->drag_origin_y = cursor_y;
3444
3445                                                                 double position = track->from_units(keyframe->position);
3446                                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3447                                                                         mwindow->edl->local_session->get_selectionend(1)) /
3448                                                                         2;
3449
3450                                                                 if(!shift_down())
3451                                                                 {
3452                                                                         mwindow->edl->local_session->set_selectionstart(position);
3453                                                                         mwindow->edl->local_session->set_selectionend(position);
3454                                                                 }
3455                                                                 else
3456                                                                 if(position < center)
3457                                                                 {
3458                                                                         mwindow->edl->local_session->set_selectionstart(position);
3459                                                                 }
3460                                                                 else
3461                                                                         mwindow->edl->local_session->set_selectionend(position);
3462                                                         }
3463                                                 }
3464                                         }
3465                                         else
3466                                                 draw_pixmap(keyframe_pixmap,
3467                                                         x,
3468                                                         y);
3469                                 }
3470                         }
3471                 }
3472         }
3473
3474
3475
3476 //      if(buttonpress && buttonpress != 3 && result)
3477 //      {
3478 //              mwindow->undo->update_undo_before();
3479 //      }
3480
3481         return result;
3482 }
3483
3484 int TrackCanvas::draw_hairline(Auto *auto_keyframe, int color, int show)
3485 {
3486         Track *track = auto_keyframe->autos->track;
3487         int autogrouptype = auto_keyframe->autos->autogrouptype;
3488
3489         int center_pixel;
3490         double view_start, unit_start;
3491         double view_end, unit_end, yscale;
3492         double zoom_sample, zoom_units;
3493
3494         calculate_viewport(track, view_start, unit_start, view_end, unit_end,
3495                         yscale, center_pixel, zoom_sample, zoom_units);
3496
3497         double ax = 0, ay = 0;
3498         calculate_auto_position(&ax, &ay, 0, 0, 0, 0,
3499                 auto_keyframe, unit_start, zoom_units, yscale, autogrouptype);
3500
3501         set_color(color);
3502         draw_line(ax, 0, ax, get_h());
3503
3504         if( show ) {
3505                 char text[BCSTRLEN];
3506                 if( auto_keyframe->is_floatauto() ) {
3507                         FloatAuto *float_auto = (FloatAuto *)auto_keyframe;
3508                         sprintf(text, "%0.2f", float_auto->get_value());
3509                 }
3510                 else {
3511                         IntAuto *int_auto = (IntAuto *)auto_keyframe;
3512                         sprintf(text, "%d", int_auto->value);
3513                 }
3514                 int font = MEDIUMFONT;
3515                 int tw = get_text_width(font, text)  + TOOLTIP_MARGIN * 2;
3516                 int th = get_text_height(font, text) + TOOLTIP_MARGIN * 2;
3517                 set_color(get_resources()->tooltip_bg_color);
3518                 ax += HANDLE_W/2;
3519                 ay += center_pixel + HANDLE_W/2;
3520                 draw_box(ax, ay, tw, th);
3521                 set_color(BLACK);
3522                 draw_rectangle(ax, ay, tw, th);
3523                 set_font(font);
3524                 ax += TOOLTIP_MARGIN;
3525                 ay += TOOLTIP_MARGIN + get_text_ascent(font);
3526                 draw_text(ax, ay, text);
3527         }
3528         return 0;
3529 }
3530
3531 void TrackCanvas::draw_overlays()
3532 {
3533         int new_cursor, update_cursor, rerender;
3534
3535 // Move background pixmap to foreground pixmap
3536         draw_pixmap(background_pixmap,
3537                 0,
3538                 0,
3539                 get_w(),
3540                 get_h(),
3541                 0,
3542                 0);
3543
3544 // In/Out points
3545         draw_inout_points();
3546
3547 // Transitions
3548         draw_transitions();
3549
3550 // Plugins
3551         draw_plugins();
3552         draw_hard_edges();
3553
3554 // Loop points
3555         draw_loop_points();
3556         draw_brender_range();
3557
3558 // Highlighted areas
3559         draw_highlighting();
3560
3561 // Automation
3562         do_keyframes(0, 0, 1, 0, new_cursor, update_cursor, rerender);
3563
3564 // Selection cursor
3565         if(pane->cursor) pane->cursor->restore(1);
3566
3567 // Handle dragging
3568         draw_drag_handle();
3569
3570 // Playback cursor
3571         draw_playback_cursor();
3572
3573         draw_keyframe_reticle();
3574
3575         show_window(0);
3576 }
3577
3578 int TrackCanvas::activate()
3579 {
3580         if(!active)
3581         {
3582 //printf("TrackCanvas::activate %d %d\n", __LINE__, pane->number);
3583 //BC_Signals::dump_stack();
3584                 get_top_level()->deactivate();
3585                 active = 1;
3586                 set_active_subwindow(this);
3587                 pane->cursor->activate();
3588                 gui->focused_pane = pane->number;
3589         }
3590         return 0;
3591 }
3592
3593 int TrackCanvas::deactivate()
3594 {
3595         if(active)
3596         {
3597                 active = 0;
3598                 pane->cursor->deactivate();
3599         }
3600         return 0;
3601 }
3602
3603
3604 void TrackCanvas::update_drag_handle()
3605 {
3606         double new_position;
3607         int cursor_x = get_cursor_x();
3608
3609         new_position =
3610                 (double)(cursor_x +
3611                 mwindow->edl->local_session->view_start[pane->number]) *
3612                 mwindow->edl->local_session->zoom_sample /
3613                 mwindow->edl->session->sample_rate;
3614
3615         new_position =
3616                 mwindow->edl->align_to_frame(new_position, 0);
3617
3618         if( ctrl_down() && alt_down() ) {
3619 #define snapper(v) do { \
3620         double pos = (v); \
3621         if( pos < 0 ) break; \
3622         double dist = fabs(new_position - pos); \
3623         if( dist >= snap_min ) break; \
3624         snap_position = pos;  snap_min = dist; \
3625 } while(0)
3626                 double snap_position = new_position;
3627                 double snap_min = DBL_MAX;
3628                 if( mwindow->edl->local_session->inpoint_valid() )
3629                         snapper(mwindow->edl->local_session->get_inpoint());
3630                 if( mwindow->edl->local_session->outpoint_valid() )
3631                         snapper(mwindow->edl->local_session->get_outpoint());
3632                 snapper(mwindow->edl->prev_edit(new_position));
3633                 snapper(mwindow->edl->next_edit(new_position));
3634                 Label *prev_label = mwindow->edl->labels->prev_label(new_position);
3635                 if( prev_label ) snapper(prev_label->position);
3636                 Label *next_label = mwindow->edl->labels->next_label(new_position);
3637                 if( next_label ) snapper(next_label->position);
3638                 int snap_x = snap_position * mwindow->edl->session->sample_rate /
3639                         mwindow->edl->local_session->zoom_sample -
3640                         mwindow->edl->local_session->view_start[pane->number];
3641                 if( abs(snap_x - cursor_x) < HANDLE_W ) {
3642                         snapped = 1;
3643                         new_position = snap_position;
3644                 }
3645 #undef snapper
3646         }
3647
3648         if(new_position != mwindow->session->drag_position)
3649         {
3650                 mwindow->session->drag_position = new_position;
3651                 gui->mainclock->update(new_position);
3652                 timebar_position = new_position;
3653                 gui->update_timebar(0);
3654
3655                 EDL *edl = new EDL;
3656                 edl->create_objects();
3657                 edl->copy_all(mwindow->edl);
3658                 MainSession *session = mwindow->session;
3659                 int edit_mode = mwindow->edl->session->edit_handle_mode[session->drag_button];
3660                 edl->modify_edithandles(session->drag_start,
3661                         session->drag_position,
3662                         session->drag_handle,
3663                         edit_mode,
3664                         edl->session->labels_follow_edits,
3665                         edl->session->plugins_follow_edits,
3666                         edl->session->autos_follow_edits);
3667                 double position = edit_mode != MOVE_NO_EDITS &&
3668                         ( session->drag_handle || edit_mode == MOVE_ONE_EDIT ) ?
3669                                 session->drag_position : session->drag_start;
3670                 Track *track = session->drag_handle_track();
3671                 int64_t pos = track->to_units(position, 0);
3672                 render_handle_frame(edl, pos, shift_down() ? 0 :
3673                         session->drag_handle ? 1 : 2);
3674                 edl->remove_user();
3675         }
3676 }
3677
3678 int TrackCanvas::render_handle_frame(EDL *edl, int64_t pos, int mode)
3679 {
3680         int result = 0;
3681         int64_t left = pos-1;
3682         if( left < 0 ) left = 0;
3683         switch( mode ) {
3684         case 0: {
3685                 VFrame vlt(edl->get_w(), edl->get_h(), edl->session->color_model);
3686                 VFrame vrt(edl->get_w(), edl->get_h(), edl->session->color_model);
3687                 TransportCommand command;
3688                 command.command = CURRENT_FRAME;
3689                 command.get_edl()->copy_all((EDL *)edl);
3690                 command.change_type = CHANGE_ALL;
3691                 command.realtime = 0;
3692                 Preferences *preferences = mwindow->preferences;
3693                 RenderEngine *render_engine = new RenderEngine(0, preferences, 0, 0);
3694                 CICache *video_cache = new CICache(preferences);
3695                 render_engine->set_vcache(video_cache);
3696                 render_engine->arm_command(&command);
3697                 int64_t left = pos-1;
3698                 if( left < 0 ) left = 0;
3699                 VRender *vrender = render_engine->vrender;
3700                 result = vrender &&
3701                         !vrender->process_buffer(&vlt, left, 0) &&
3702                         !vrender->process_buffer(&vrt, pos , 0) ? 0 : 1;
3703                 delete render_engine;
3704                 delete video_cache;
3705                 mwindow->cwindow->gui->lock_window("TrackCanvas::render_handle_frame 0");
3706                 Canvas *canvas = mwindow->cwindow->gui->canvas;
3707                 canvas->lock_canvas("TrackCanvas::render_handle_frame 1");
3708                 int w = canvas->w, h = canvas->h, w2 = w/2, h2 = h/2;
3709                 int lx = 0, ly = h2/2, rx = w2, ry = h2/2;
3710                 BC_WindowBase *window = canvas->get_canvas();
3711                 window->set_color(BLACK);
3712                 window->clear_box(0,0, window->get_w(),window->get_h());
3713                 window->draw_vframe(&vlt, lx,ly, w2,h2, 0,0,vlt.get_w(),vlt.get_h());
3714                 window->draw_vframe(&vrt, rx,ry, w2,h2, 0,0,vrt.get_w(),vrt.get_h());
3715                 window->flash(1);
3716                 canvas->unlock_canvas();
3717                 mwindow->cwindow->gui->unlock_window();
3718                 break; }
3719         case 1:
3720         case 2: {
3721                 Track *track = mwindow->session->drag_handle_track();
3722                 double position = track->from_units(mode == 1 ? left : pos);
3723                 if( position < 0 ) position = 0;
3724                 edl->local_session->set_selectionstart(position);
3725                 edl->local_session->set_selectionend(position);
3726                 PlaybackEngine *playback_engine = mwindow->cwindow->playback_engine;
3727                 if( playback_engine->is_playing_back )
3728                         playback_engine->stop_playback(1);
3729                 mwindow->cwindow->playback_engine->refresh_frame(CHANGE_EDL, edl, 0);
3730                 break; }
3731         }
3732         return result;
3733 }
3734
3735 int TrackCanvas::update_drag_edit()
3736 {
3737         int result = 0;
3738
3739
3740
3741         return result;
3742 }
3743
3744 int TrackCanvas::get_drag_values(float *percentage,
3745         int64_t *position,
3746         int do_clamp,
3747         int cursor_x,
3748         int cursor_y,
3749         Auto *current)
3750 {
3751         //int x = cursor_x - mwindow->session->drag_origin_x;
3752         //int y = cursor_y - mwindow->session->drag_origin_y;
3753         *percentage = 0;
3754         *position = 0;
3755
3756         if(!current->autos->track->record) return 1;
3757         double view_start;
3758         double unit_start;
3759         double view_end;
3760         double unit_end;
3761         double yscale;
3762         int center_pixel;
3763         double zoom_sample;
3764         double zoom_units;
3765
3766         calculate_viewport(current->autos->track,
3767                 view_start,
3768                 unit_start,
3769                 view_end,
3770                 unit_end,
3771                 yscale,
3772                 center_pixel,
3773                 zoom_sample,
3774                 zoom_units);
3775
3776         *percentage = (float)(mwindow->session->drag_origin_y - cursor_y) /
3777                 yscale +
3778                 mwindow->session->drag_start_percentage;
3779         if(do_clamp) CLAMP(*percentage, 0, 1);
3780
3781         *position = Units::to_int64(zoom_units *
3782                 (cursor_x - mwindow->session->drag_origin_x) +
3783                 mwindow->session->drag_start_position + 0.5);
3784
3785         if((do_clamp) && *position < 0) *position = 0;
3786         return 0;
3787 }
3788
3789
3790 #define UPDATE_DRAG_HEAD(do_clamp) \
3791         int result = 0, center_pixel; \
3792         if(!current->autos->track->record) return 0; \
3793         double view_start, unit_start, view_end, unit_end; \
3794         double yscale, zoom_sample, zoom_units; \
3795  \
3796         calculate_viewport(current->autos->track,  \
3797                 view_start, unit_start, view_end, unit_end, \
3798                 yscale, center_pixel, zoom_sample, zoom_units); \
3799  \
3800         float percentage = (float)(mwindow->session->drag_origin_y - cursor_y) / \
3801                 yscale +  mwindow->session->drag_start_percentage; \
3802         if(do_clamp) CLAMP(percentage, 0, 1); \
3803  \
3804         int64_t position = Units::to_int64(zoom_units * \
3805                 (cursor_x - mwindow->session->drag_origin_x) + \
3806                 mwindow->session->drag_start_position); \
3807         if((do_clamp) && position < 0) position = 0;
3808
3809
3810 int TrackCanvas::update_drag_floatauto(int cursor_x, int cursor_y)
3811 {
3812         FloatAuto *current = (FloatAuto*)mwindow->session->drag_auto;
3813
3814         UPDATE_DRAG_HEAD(mwindow->session->drag_handle == 0);
3815         int x = cursor_x - mwindow->session->drag_origin_x;
3816         int y = cursor_y - mwindow->session->drag_origin_y;
3817         float value, old_value;
3818
3819         if( mwindow->session->drag_handle == 0 && (ctrl_down() && !shift_down()) ) {
3820 // not really editing the node, rather start editing the curve
3821 // tangent is editable and drag movement is significant
3822                 if( (FloatAuto::FREE == current->curve_mode ||
3823                      FloatAuto::TFREE==current->curve_mode) &&
3824                     (fabs(x) > HANDLE_W / 2 || fabs(y) > HANDLE_W / 2))
3825                         mwindow->session->drag_handle = x < 0 ? 1 : 2;
3826         }
3827
3828         switch(mwindow->session->drag_handle) {
3829         case 0: // Center
3830 // Snap to nearby values
3831                 old_value = current->get_value();
3832                 if(shift_down()) {
3833                         double value1, value2, distance1, distance2;
3834
3835                         if(current->previous) {
3836                                 int autogrouptype = current->previous->autos->autogrouptype;
3837                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3838                                 value1 = ((FloatAuto*)current->previous)->get_value();
3839                                 distance1 = fabs(value - value1);
3840                                 current->set_value(value1);
3841                         }
3842
3843                         if(current->next) {
3844                                 int autogrouptype = current->next->autos->autogrouptype;
3845                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3846                                 value2 = ((FloatAuto*)current->next)->get_value();
3847                                 distance2 = fabs(value - value2);
3848                                 if(!current->previous || distance2 < distance1) {
3849                                         current->set_value(value2);
3850                                 }
3851                         }
3852
3853                         if(!current->previous && !current->next) {
3854                                 current->set_value( ((FloatAutos*)current->autos)->default_);
3855                         }
3856                         value = current->get_value();
3857                 }
3858                 else {
3859                         int autogrouptype = current->autos->autogrouptype;
3860                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
3861                 }
3862
3863                 if(alt_down() && !shift_down())
3864 // ALT constrains movement: fixed position, only changing the value
3865                         position = mwindow->session->drag_start_position;
3866
3867                 if(value != old_value || position != current->position) {
3868                         result = 1;
3869                         float change = value - old_value;
3870                         current->adjust_to_new_coordinates(position, value);
3871                         update_ganged_autos(change, current->autos->track, current);
3872                         show_message(current, 1,", %.2f", current->get_value());
3873                 }
3874                 break;
3875
3876 // In control
3877         case 1: {
3878                 int autogrouptype = current->autos->autogrouptype;
3879                 value = percentage_to_value(percentage, 0, current, autogrouptype);
3880                 if(value != current->get_control_in_value())
3881                 {
3882                         result = 1;
3883                         // note: (position,value) need not be at the location of the ctrl point,
3884                         // but could be somewhere in between on the curve (or even outward or
3885                         // on the opposit side). We set the new control point such as
3886                         // to point the curve through (position,value)
3887                         current->set_control_in_value(
3888                                 value * levered_position(position - current->position,
3889                                                          current->get_control_in_position()));
3890                         update_ganged_autos(0, current->autos->track, current);
3891                         show_message(current, 1,", %.2f", current->get_control_in_value());
3892                 }
3893                 break; }
3894
3895 // Out control
3896         case 2: {
3897                 int autogrouptype = current->autos->autogrouptype;
3898                 value = percentage_to_value(percentage, 0, current, autogrouptype);
3899                 if(value != current->get_control_out_value()) {
3900                         result = 1;
3901                         current->set_control_out_value(
3902                                 value * levered_position(position - current->position,
3903                                                          current->get_control_out_position()));
3904                         update_ganged_autos(0, current->autos->track, current);
3905                         show_message(current, 1,", %.2f", current->get_control_out_value());
3906                 }
3907                 break; }
3908         }
3909
3910         return result;
3911 }
3912
3913 int TrackCanvas::update_drag_toggleauto(int cursor_x, int cursor_y)
3914 {
3915         IntAuto *current = (IntAuto*)mwindow->session->drag_auto;
3916
3917         UPDATE_DRAG_HEAD(1);
3918         int value = (int)percentage_to_value(percentage, 1, 0, AUTOGROUPTYPE_INT255);
3919
3920         if(value != current->value || position != current->position)
3921         {
3922                 result = 1;
3923                 current->value = value;
3924                 current->position = position;
3925                 show_message(current, 0,", %d", current->value);
3926         }
3927
3928         return result;
3929 }
3930
3931 // Autos which can't change value through dragging.
3932
3933 int TrackCanvas::update_drag_auto(int cursor_x, int cursor_y)
3934 {
3935         Auto *current = (Auto*)mwindow->session->drag_auto;
3936
3937         UPDATE_DRAG_HEAD(1)
3938         if(position != current->position)
3939         {
3940                 result = 1;
3941                 current->position = position;
3942                 show_message(current, 0,"");
3943
3944                 double position_f = current->autos->track->from_units(current->position);
3945                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3946                         mwindow->edl->local_session->get_selectionend(1)) /
3947                         2;
3948                 if(!shift_down())
3949                 {
3950                         mwindow->edl->local_session->set_selectionstart(position_f);
3951                         mwindow->edl->local_session->set_selectionend(position_f);
3952                 }
3953                 else
3954                 if(position_f < center_f)
3955                 {
3956                         mwindow->edl->local_session->set_selectionstart(position_f);
3957                 }
3958                 else
3959                         mwindow->edl->local_session->set_selectionend(position_f);
3960         }
3961
3962
3963         return result;
3964 }
3965
3966 int TrackCanvas::update_drag_pluginauto(int cursor_x, int cursor_y)
3967 {
3968         KeyFrame *current = (KeyFrame*)mwindow->session->drag_auto;
3969
3970         UPDATE_DRAG_HEAD(1)
3971         if(position != current->position)
3972         {
3973 //      printf("uida: autos: %p, track: %p ta: %p\n", current->autos, current->autos->track, current->autos->track->automation);
3974                 Track *track = current->autos->track;
3975                 //PluginAutos *pluginautos = (PluginAutos *)current->autos;
3976                 PluginSet *pluginset;
3977                 Plugin *plugin = 0;
3978 // figure out the correct pluginset & correct plugin
3979                 int found = 0;
3980                 for(int i = 0; i < track->plugin_set.total; i++)
3981                 {
3982                         pluginset = track->plugin_set.values[i];
3983                         for(plugin = (Plugin *)pluginset->first; plugin; plugin = (Plugin *)plugin->next)
3984                         {
3985                                 KeyFrames *keyframes = plugin->keyframes;
3986                                 for(KeyFrame *currentkeyframe = (KeyFrame *)keyframes->first;
3987                                         currentkeyframe;
3988                                         currentkeyframe = (KeyFrame *) currentkeyframe->next)
3989                                 {
3990                                         if (currentkeyframe == current)
3991                                         {
3992                                                 found = 1;
3993                                                 break;
3994                                         }
3995
3996                                 }
3997                                 if (found) break;
3998                         }
3999                         if (found) break;
4000                 }
4001
4002                 mwindow->session->plugin_highlighted = plugin;
4003                 mwindow->session->track_highlighted = track;
4004                 result = 1;
4005                 current->position = position;
4006                 show_message(current, 0,"");
4007
4008                 double position_f = current->autos->track->from_units(current->position);
4009                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
4010                         mwindow->edl->local_session->get_selectionend(1)) /
4011                         2;
4012                 if(!shift_down())
4013                 {
4014                         mwindow->edl->local_session->set_selectionstart(position_f);
4015                         mwindow->edl->local_session->set_selectionend(position_f);
4016                 }
4017                 else
4018                 if(position_f < center_f)
4019                 {
4020                         mwindow->edl->local_session->set_selectionstart(position_f);
4021                 }
4022                 else
4023                         mwindow->edl->local_session->set_selectionend(position_f);
4024         }
4025
4026
4027         return result;
4028 }
4029
4030 void TrackCanvas::update_drag_caption()
4031 {
4032         switch(mwindow->session->current_operation)
4033         {
4034                 case DRAG_FADE:
4035
4036                         break;
4037         }
4038 }
4039
4040
4041
4042 int TrackCanvas::cursor_update(int in_motion)
4043 {
4044         int result = 0;
4045         int cursor_x = 0;
4046         int cursor_y = 0;
4047         int update_clock = 0;
4048         int update_zoom = 0;
4049         int update_scroll = 0;
4050         int update_overlay = 0;
4051         int update_cursor = 0;
4052         int new_cursor = 0;
4053         int rerender = 0;
4054         double position = 0.;
4055 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
4056
4057 // Default cursor
4058         switch(mwindow->edl->session->editing_mode)
4059         {
4060                 case EDITING_ARROW: new_cursor = ARROW_CURSOR; break;
4061                 case EDITING_IBEAM: new_cursor = IBEAM_CURSOR; break;
4062         }
4063
4064         switch(mwindow->session->current_operation)
4065         {
4066                 case DRAG_EDITHANDLE1:
4067 // Outside threshold.  Upgrade status
4068                         if(active)
4069                         {
4070                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
4071                                 {
4072                                         mwindow->session->current_operation = DRAG_EDITHANDLE2;
4073                                         update_overlay = 1;
4074                                 }
4075                         }
4076                         break;
4077
4078                 case DRAG_EDITHANDLE2:
4079                         if(active)
4080                         {
4081                                 update_drag_handle();
4082                                 update_overlay = 1;
4083                         }
4084                         break;
4085
4086                 case DRAG_PLUGINHANDLE1:
4087                         if(active)
4088                         {
4089                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
4090                                 {
4091                                         mwindow->session->current_operation = DRAG_PLUGINHANDLE2;
4092                                         update_overlay = 1;
4093                                 }
4094                         }
4095                         break;
4096
4097                 case DRAG_PLUGINHANDLE2:
4098                         if(active)
4099                         {
4100                                 update_drag_handle();
4101                                 update_overlay = 1;
4102                         }
4103                         break;
4104
4105 // Rubber band curves
4106                 case DRAG_FADE:
4107                 case DRAG_SPEED:
4108                 case DRAG_CZOOM:
4109                 case DRAG_PZOOM:
4110                 case DRAG_CAMERA_X:
4111                 case DRAG_CAMERA_Y:
4112                 case DRAG_CAMERA_Z:
4113                 case DRAG_PROJECTOR_X:
4114                 case DRAG_PROJECTOR_Y:
4115                 case DRAG_PROJECTOR_Z:
4116                         if(active) rerender = update_overlay =
4117                                 update_drag_floatauto(get_cursor_x(), get_cursor_y());
4118                         if( rerender && mwindow->session->current_operation == DRAG_SPEED )
4119                                 mwindow->speed_after(!in_motion ? 1 : 0);
4120                         break;
4121
4122                 case DRAG_PLAY:
4123                         if(active) rerender = update_overlay =
4124                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4125                         break;
4126
4127                 case DRAG_MUTE:
4128                         if(active) rerender = update_overlay =
4129                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4130                         break;
4131
4132 // Keyframe icons are sticky
4133                 case DRAG_PAN_PRE:
4134                 case DRAG_MASK_PRE:
4135                 case DRAG_MODE_PRE:
4136                 case DRAG_PLUGINKEY_PRE:
4137                         if(active) {
4138                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W) {
4139                                         mwindow->session->current_operation++;
4140                                         update_overlay = 1;
4141
4142                                         mwindow->undo->update_undo_before();
4143                                 }
4144                         }
4145                         break;
4146
4147                 case DRAG_PAN:
4148                 case DRAG_MASK:
4149                 case DRAG_MODE:
4150                         if(active) rerender = update_overlay =
4151                                 update_drag_auto(get_cursor_x(), get_cursor_y());
4152                         break;
4153
4154                 case DRAG_PLUGINKEY:
4155                         if(active) rerender = update_overlay =
4156                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4157                         break;
4158
4159                 case SELECT_REGION:
4160                         if(active) {
4161                                 cursor_x = get_cursor_x();
4162                                 cursor_y = get_cursor_y();
4163                                 position = (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
4164                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
4165
4166                                 position = mwindow->edl->align_to_frame(position, 0);
4167                                 position = MAX(position, 0);
4168
4169                                 double start = mwindow->edl->local_session->get_selectionstart(1);
4170                                 double end = mwindow->edl->local_session->get_selectionend(1);
4171                                 if(position < selection_midpoint) {
4172                                         mwindow->edl->local_session->set_selectionend(selection_midpoint);
4173                                         mwindow->edl->local_session->set_selectionstart(position);
4174                                 }
4175                                 else {
4176                                         mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4177                                         mwindow->edl->local_session->set_selectionend(position);
4178                                 }
4179         // Que the CWindow
4180                                 gui->unlock_window();
4181                                 int dir =
4182                                         start != mwindow->edl->local_session->get_selectionstart(1) ? 1 :
4183                                         end != mwindow->edl->local_session->get_selectionend(1) ? -1 : 0;
4184                                 mwindow->cwindow->update(dir, 0, 0, 0, 1);
4185                                 gui->lock_window("TrackCanvas::cursor_update 1");
4186         // Update the faders
4187                                 mwindow->update_plugin_guis();
4188                                 gui->update_patchbay();
4189
4190                                 timebar_position = mwindow->edl->local_session->get_selectionend(1);
4191
4192                                 gui->hide_cursor(0);
4193                                 gui->draw_cursor(1);
4194                                 gui->update_timebar(0);
4195                                 gui->flash_canvas(1);
4196                                 result = 1;
4197                                 update_clock = 1;
4198                                 update_zoom = 1;
4199                                 update_scroll = 1;
4200                         }
4201                         break;
4202
4203                 case DROP_TARGETING:
4204                         new_cursor = GRABBED_CURSOR;
4205                         break;
4206
4207                 default:
4208                         if(is_event_win() && cursor_inside()) {
4209 // Update clocks
4210                                 cursor_x = get_cursor_x();
4211                                 position = (double)cursor_x *
4212                                         (double)mwindow->edl->local_session->zoom_sample /
4213                                         (double)mwindow->edl->session->sample_rate +
4214                                         (double)mwindow->edl->local_session->view_start[pane->number] *
4215                                         (double)mwindow->edl->local_session->zoom_sample /
4216                                         (double)mwindow->edl->session->sample_rate;
4217                                 position = mwindow->edl->align_to_frame(position, 0);
4218                                 update_clock = 1;
4219
4220 // set all timebars
4221                                 for(int i = 0; i < TOTAL_PANES; i++)
4222                                         if(gui->pane[i]) gui->pane[i]->canvas->timebar_position = position;
4223
4224 //printf("TrackCanvas::cursor_update %d %d %p %p\n", __LINE__, pane->number, pane, pane->timebar);
4225                                 gui->update_timebar(0);
4226 // Update cursor
4227                                 if(do_transitions(get_cursor_x(), get_cursor_y(),
4228                                                 0, new_cursor, update_cursor)) break;
4229                                 if(do_keyframes(get_cursor_x(), get_cursor_y(),
4230                                         0, 0, new_cursor, update_cursor, rerender)) break;
4231                                 if(do_edit_handles(get_cursor_x(), get_cursor_y(),
4232                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4233 // Plugin boundaries
4234                                 if(do_plugin_handles(get_cursor_x(), get_cursor_y(),
4235                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4236                                 if(do_edits(get_cursor_x(), get_cursor_y(),
4237                                         0, 0, update_overlay, rerender, new_cursor, update_cursor)) break;
4238                         }
4239                         break;
4240         }
4241
4242 //printf("TrackCanvas::cursor_update 1\n");
4243         if(update_cursor && new_cursor != get_cursor())
4244         {
4245                 set_cursor(new_cursor, 0, 1);
4246         }
4247
4248 //printf("TrackCanvas::cursor_update 1 %d\n", rerender);
4249         if(rerender && render_timer->get_difference() > 0.25 ) {
4250                 render_timer->update();
4251                 mwindow->restart_brender();
4252                 mwindow->sync_parameters(CHANGE_PARAMS);
4253                 mwindow->update_plugin_guis();
4254                 gui->unlock_window();
4255                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4256                 gui->lock_window("TrackCanvas::cursor_update 2");
4257         }
4258         if(rerender) {
4259 // Update faders
4260                 gui->update_patchbay();
4261         }
4262
4263
4264         if(update_clock) {
4265                 if(!mwindow->cwindow->playback_engine->is_playing_back)
4266                         gui->mainclock->update(position);
4267         }
4268
4269         if(update_zoom) {
4270                 gui->zoombar->update();
4271         }
4272
4273         if(update_scroll) {
4274                 if(!drag_scroll &&
4275                         (cursor_x >= get_w() || cursor_x < 0 || cursor_y >= get_h() || cursor_y < 0))
4276                         start_dragscroll();
4277                 else
4278                 if(drag_scroll &&
4279                         (cursor_x < get_w() && cursor_x >= 0 && cursor_y < get_h() && cursor_y >= 0))
4280                         stop_dragscroll();
4281         }
4282
4283         if(update_overlay) {
4284                 gui->draw_overlays(1);
4285         }
4286
4287 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
4288         return result;
4289 }
4290
4291 int TrackCanvas::cursor_motion_event()
4292 {
4293         return cursor_update(1);
4294 }
4295
4296 void TrackCanvas::start_dragscroll()
4297 {
4298         if(!drag_scroll) {
4299                 drag_scroll = 1;
4300                 set_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4301 //printf("TrackCanvas::start_dragscroll 1\n");
4302         }
4303 }
4304
4305 void TrackCanvas::stop_dragscroll()
4306 {
4307         if(drag_scroll) {
4308                 drag_scroll = 0;
4309                 unset_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4310 //printf("TrackCanvas::stop_dragscroll 1\n");
4311         }
4312 }
4313
4314 int TrackCanvas::repeat_event(int64_t duration)
4315 {
4316         if(!drag_scroll) return 0;
4317         if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
4318
4319         int sample_movement = 0;
4320         int track_movement = 0;
4321         int64_t x_distance = 0;
4322         int64_t y_distance = 0;
4323         double position = 0;
4324         int result = 0;
4325
4326         switch(mwindow->session->current_operation) {
4327                 case SELECT_REGION:
4328 //printf("TrackCanvas::repeat_event 1 %d\n", mwindow->edl->local_session->view_start);
4329                 if(get_cursor_x() > get_w()) {
4330                                 x_distance = get_cursor_x() - get_w();
4331                                 sample_movement = 1;
4332                 }
4333                 else if(get_cursor_x() < 0) {
4334                         x_distance = get_cursor_x();
4335                         sample_movement = 1;
4336                 }
4337
4338                 if(get_cursor_y() > get_h()) {
4339                         y_distance = get_cursor_y() - get_h();
4340                         track_movement = 1;
4341                 }
4342                 else if(get_cursor_y() < 0) {
4343                         y_distance = get_cursor_y();
4344                         track_movement = 1;
4345                 }
4346                 result = 1;
4347                 break;
4348         }
4349
4350         if(sample_movement) {
4351                 position = (double)(get_cursor_x() +
4352                         mwindow->edl->local_session->view_start[pane->number] +
4353                         x_distance) *
4354                         mwindow->edl->local_session->zoom_sample /
4355                         mwindow->edl->session->sample_rate;
4356                 position = mwindow->edl->align_to_frame(position, 0);
4357                 position = MAX(position, 0);
4358
4359 //printf("TrackCanvas::repeat_event 1 %f\n", position);
4360                 switch(mwindow->session->current_operation) {
4361                 case SELECT_REGION:
4362                         if(position < selection_midpoint) {
4363                                 mwindow->edl->local_session->set_selectionend(selection_midpoint);
4364                                 mwindow->edl->local_session->set_selectionstart(position);
4365 // Que the CWindow
4366                                 gui->unlock_window();
4367                                 mwindow->cwindow->update(1, 0, 0);
4368                                 gui->lock_window("TrackCanvas::repeat_event");
4369 // Update the faders
4370                                 mwindow->update_plugin_guis();
4371                                 gui->update_patchbay();
4372                         }
4373                         else {
4374                                 mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4375                                 mwindow->edl->local_session->set_selectionend(position);
4376 // Don't que the CWindow
4377                         }
4378                         break;
4379                 }
4380
4381                 mwindow->samplemovement(
4382                         mwindow->edl->local_session->view_start[pane->number] + x_distance,
4383                         pane->number);
4384         }
4385
4386         if(track_movement) {
4387                 mwindow->trackmovement(y_distance, pane->number);
4388         }
4389
4390         return result;
4391 }
4392
4393 int TrackCanvas::button_release_event()
4394 {
4395         int redraw = -1, update_overlay = 0;
4396         int result = 0, load_flags = 0;
4397
4398 // printf("TrackCanvas::button_release_event %d\n",
4399 // mwindow->session->current_operation);
4400         if(active) {
4401                 switch(mwindow->session->current_operation) {
4402                 case DRAG_EDITHANDLE2:
4403                         mwindow->session->current_operation = NO_OPERATION;
4404                         drag_scroll = 0;
4405                         result = 1;
4406
4407                         end_edithandle_selection();
4408                         break;
4409
4410                 case DRAG_EDITHANDLE1:
4411                         mwindow->session->current_operation = NO_OPERATION;
4412                         drag_scroll = 0;
4413                         result = 1;
4414                         break;
4415
4416                 case DRAG_PLUGINHANDLE2:
4417                         mwindow->session->current_operation = NO_OPERATION;
4418                         drag_scroll = 0;
4419                         result = 1;
4420
4421                         end_pluginhandle_selection();
4422                         break;
4423
4424                 case DRAG_PLUGINHANDLE1:
4425                         mwindow->session->current_operation = NO_OPERATION;
4426                         drag_scroll = 0;
4427                         result = 1;
4428                         break;
4429
4430                 case DRAG_SPEED:
4431                         redraw = FORCE_REDRAW;
4432                         load_flags |= LOAD_EDITS;
4433                 case DRAG_FADE:
4434 // delete the drag_auto_gang first and remove out of order keys
4435                         clear_ganged_autos();
4436                 case DRAG_CZOOM:
4437                 case DRAG_PZOOM:
4438                 case DRAG_PLAY:
4439                 case DRAG_MUTE:
4440                 case DRAG_MASK:
4441                 case DRAG_MODE:
4442                 case DRAG_PAN:
4443                 case DRAG_CAMERA_X:
4444                 case DRAG_CAMERA_Y:
4445                 case DRAG_CAMERA_Z:
4446                 case DRAG_PROJECTOR_X:
4447                 case DRAG_PROJECTOR_Y:
4448                 case DRAG_PROJECTOR_Z:
4449                 case DRAG_PLUGINKEY:
4450                         load_flags |= LOAD_AUTOMATION;
4451                         mwindow->session->current_operation = NO_OPERATION;
4452                         mwindow->session->drag_handle = 0;
4453 // Remove any out-of-order keyframe
4454                         if(mwindow->session->drag_auto) {
4455                                 mwindow->session->drag_auto->autos->remove_nonsequential(
4456                                                 mwindow->session->drag_auto);
4457 //                              mwindow->session->drag_auto->autos->optimize();
4458                                 update_overlay = 1;
4459                         }
4460
4461                         mwindow->undo->update_undo_after(_("keyframe"), load_flags);
4462                         result = 1;
4463                         break;
4464
4465                 case DRAG_EDIT:
4466                 case DRAG_AEFFECT_COPY:
4467                 case DRAG_VEFFECT_COPY:
4468 // Trap in drag stop
4469                         break;
4470
4471                 case DROP_TARGETING: {
4472                         int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
4473                         Track *track=0;  Edit *edit=0;  PluginSet *pluginset=0;  Plugin *plugin=0;
4474                         drag_cursor_motion(cursor_x, cursor_y,
4475                                 &track, &edit, &pluginset, &plugin);
4476                         double position =
4477                                 mwindow->edl->get_cursor_position(cursor_x, pane->number);
4478                         gui->edit_menu->activate_menu(track, edit, pluginset, plugin, position);
4479                         mwindow->session->current_operation = NO_OPERATION;
4480                         result = 1;
4481                         break; }
4482
4483                 default:
4484                         if(mwindow->session->current_operation) {
4485 //                              if(mwindow->session->current_operation == SELECT_REGION) {
4486 //                                      mwindow->undo->update_undo_after(_("select"), LOAD_SESSION, 0, 0);
4487 //                              }
4488
4489                                 mwindow->session->current_operation = NO_OPERATION;
4490                                 drag_scroll = 0;
4491 // Traps button release events
4492 //                              result = 1;
4493                         }
4494                         break;
4495                 }
4496         }
4497
4498         if (result)
4499                 cursor_update(0);
4500
4501         if(update_overlay) {
4502                 gui->draw_overlays(1);
4503         }
4504         if(redraw >= 0) {
4505                 gui->draw_canvas(redraw, 0);
4506                 gui->flash_canvas(1);
4507         }
4508         return result;
4509 }
4510
4511 int TrackCanvas::do_edit_handles(int cursor_x, int cursor_y, int button_press,
4512         int &rerender, int &update_overlay, int &new_cursor, int &update_cursor)
4513 {
4514         Edit *edit_result = 0;
4515         int handle_result = -1;
4516         int result = 0;
4517
4518         for( Track *track=mwindow->edl->tracks->first; track && !result; track=track->next) {
4519                 for( Edit *edit=track->edits->first; edit && !result; edit=edit->next ) {
4520                         int64_t edit_x, edit_y, edit_w, edit_h;
4521                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4522
4523                         if( cursor_x >= edit_x && cursor_x <= edit_x + edit_w &&
4524                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h ) {
4525                                 if( cursor_x < edit_x + HANDLE_W ) {
4526                                         edit_result = edit;
4527                                         handle_result = 0;
4528                                         if( cursor_y >= edit_y+edit_h - HANDLE_W &&
4529                                             track->show_assets() ) {
4530                                                 new_cursor = DOWNLEFT_RESIZE;
4531                                                 if( button_press == LEFT_BUTTON )
4532                                                         result = -1;
4533                                         }
4534                                         else
4535                                                 result = 1;
4536                                 }
4537                                 else if( cursor_x >= edit_x + edit_w - HANDLE_W ) {
4538                                         edit_result = edit;
4539                                         handle_result = 1;
4540                                         if( cursor_y >= edit_y+edit_h - HANDLE_W &&
4541                                             track->show_assets() ) {
4542                                                 new_cursor = DOWNRIGHT_RESIZE;
4543                                                 if( button_press == LEFT_BUTTON )
4544                                                         result = -1;
4545                                         }
4546                                         else
4547                                                 result = 1;
4548                                 }
4549                         }
4550                 }
4551         }
4552
4553         update_cursor = 1;
4554         if( result > 0 ) {
4555                 double position = 0;
4556                 if( handle_result == 0 ) {
4557                         position = edit_result->track->from_units(edit_result->startproject);
4558                         new_cursor = LEFT_CURSOR;
4559                 }
4560                 else if( handle_result == 1 ) {
4561                         position = edit_result->track->from_units(edit_result->startproject + edit_result->length);
4562                         new_cursor = RIGHT_CURSOR;
4563                 }
4564
4565 // Reposition cursor
4566                 if( button_press ) {
4567                         mwindow->session->drag_edit = edit_result;
4568                         mwindow->session->drag_handle = handle_result;
4569                         mwindow->session->drag_button = get_buttonpress() - 1;
4570                         mwindow->session->drag_position = position;
4571                         mwindow->session->current_operation = DRAG_EDITHANDLE1;
4572                         mwindow->session->drag_origin_x = get_cursor_x();
4573                         mwindow->session->drag_origin_y = get_cursor_y();
4574                         mwindow->session->drag_start = position;
4575
4576                         rerender = start_selection(position);
4577                         update_overlay = 1;
4578                 }
4579         }
4580         else if( result < 0 ) {
4581                 mwindow->undo->update_undo_before();
4582                 if( !shift_down() ) {
4583                         if( handle_result == 0 )
4584                                 edit_result->hard_left = !edit_result->hard_left;
4585                         else if( handle_result == 1 )
4586                                 edit_result->hard_right = !edit_result->hard_right;
4587                 }
4588                 else {
4589                         int status = handle_result == 0 ? edit_result->hard_left :
4590                                      handle_result == 1 ? edit_result->hard_right : 0;
4591                         int new_status = !status;
4592                         int64_t edit_edge = edit_result->startproject;
4593                         if( handle_result == 1 ) edit_edge += edit_result->length;
4594                         double edge_position = edit_result->track->from_units(edit_edge);
4595                         for( Track *track=mwindow->edl->tracks->first; track!=0; track=track->next ) {
4596                                 int64_t track_position = track->to_units(edge_position, 1);
4597                                 Edit *left_edit = track->edits->editof(track_position, PLAY_FORWARD, 0);
4598                                 if( left_edit ) {
4599                                         int64_t left_edge = left_edit->startproject;
4600                                         double left_position = track->from_units(left_edge);
4601                                         if( EQUIV(edge_position, left_position) ) {
4602                                                 left_edit->hard_left = new_status;
4603                                                 if( left_edit->previous )
4604                                                         left_edit->previous->hard_right = new_status;
4605                                         }
4606                                 }
4607                                 Edit *right_edit = track->edits->editof(track_position, PLAY_REVERSE, 0);
4608                                 if( right_edit ) {
4609                                         int64_t right_edge = right_edit->startproject + right_edit->length;
4610                                         double right_position = track->from_units(right_edge);
4611                                         if( EQUIV(edge_position, right_position) ) {
4612                                                 right_edit->hard_right = new_status;
4613                                                 if( right_edit->next )
4614                                                         right_edit->next->hard_left = new_status;
4615                                         }
4616                                 }
4617                         }
4618                 }
4619                 rerender = update_overlay = 1;
4620                 mwindow->undo->update_undo_after(_("hard_edge"), LOAD_EDITS);
4621                 result = 1;
4622         }
4623
4624         return result;
4625 }
4626
4627 int TrackCanvas::do_plugin_handles(int cursor_x,
4628         int cursor_y,
4629         int button_press,
4630         int &rerender,
4631         int &update_overlay,
4632         int &new_cursor,
4633         int &update_cursor)
4634 {
4635         Plugin *plugin_result = 0;
4636         int handle_result = 0;
4637         int result = 0;
4638
4639         for(Track *track = mwindow->edl->tracks->first;
4640                 track && !result;
4641                 track = track->next) {
4642                 for(int i = 0; i < track->plugin_set.total && !result; i++) {
4643                         PluginSet *plugin_set = track->plugin_set.values[i];
4644                         for(Plugin *plugin = (Plugin*)plugin_set->first;
4645                                 plugin && !result;
4646                                 plugin = (Plugin*)plugin->next) {
4647                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
4648                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
4649
4650                                 if(cursor_x >= plugin_x && cursor_x <= plugin_x + plugin_w &&
4651                                         cursor_y >= plugin_y && cursor_y < plugin_y + plugin_h) {
4652                                         if(cursor_x < plugin_x + HANDLE_W) {
4653                                                 plugin_result = plugin;
4654                                                 handle_result = 0;
4655                                                 result = 1;
4656                                         }
4657                                         else if(cursor_x >= plugin_x + plugin_w - HANDLE_W) {
4658                                                 plugin_result = plugin;
4659                                                 handle_result = 1;
4660                                                 result = 1;
4661                                         }
4662                                 }
4663                         }
4664
4665                         if(result && shift_down())
4666                                 mwindow->session->trim_edits = plugin_set;
4667                 }
4668         }
4669
4670         update_cursor = 1;
4671         if(result) {
4672                 double position = 0;
4673                 if(handle_result == 0) {
4674                         position = plugin_result->track->from_units(plugin_result->startproject);
4675                         new_cursor = LEFT_CURSOR;
4676                 }
4677                 else if(handle_result == 1) {
4678                         position = plugin_result->track->from_units(plugin_result->startproject + plugin_result->length);
4679                         new_cursor = RIGHT_CURSOR;
4680                 }
4681
4682                 if(button_press) {
4683                         mwindow->session->drag_plugin = plugin_result;
4684                         mwindow->session->drag_handle = handle_result;
4685                         mwindow->session->drag_button = get_buttonpress() - 1;
4686                         mwindow->session->drag_position = position;
4687                         mwindow->session->current_operation = DRAG_PLUGINHANDLE1;
4688                         mwindow->session->drag_origin_x = get_cursor_x();
4689                         mwindow->session->drag_origin_y = get_cursor_y();
4690                         mwindow->session->drag_start = position;
4691
4692                         rerender = start_selection(position);
4693                         update_overlay = 1;
4694                 }
4695         }
4696
4697         return result;
4698 }
4699
4700
4701 int TrackCanvas::do_tracks(int cursor_x, int cursor_y, int button_press)
4702 {
4703         int result = 0;
4704
4705         Track *track=0;  Edit *edit=0;  PluginSet *pluginset=0;  Plugin *plugin=0;
4706         drag_cursor_motion(cursor_x, cursor_y,
4707                 &track, &edit, &pluginset, &plugin);
4708
4709         if( button_press && track ) {
4710                 switch( get_buttonpress() ) {
4711                 case RIGHT_BUTTON: {
4712                         double position =
4713                                 mwindow->edl->get_cursor_position(cursor_x, pane->number);
4714                         gui->track_menu->activate_menu(track, edit, pluginset, plugin, position);
4715                         mwindow->session->current_operation = NO_OPERATION;
4716                         result = 1;
4717                         break; }
4718                 case MIDDLE_BUTTON:
4719                         set_cursor(GRABBED_CURSOR, 0, 1);
4720                         mwindow->session->current_operation = DROP_TARGETING;
4721                         result = 1;
4722                         break;
4723                 }
4724         }
4725
4726         return result;
4727 }
4728
4729 int TrackCanvas::do_edits(int cursor_x, int cursor_y, int button_press, int drag_start,
4730         int &redraw, int &rerender, int &new_cursor, int &update_cursor)
4731 {
4732         int result = 0;
4733
4734         for(Track *track = mwindow->edl->tracks->first; track && !result; track = track->next) {
4735                 for(Edit *edit = track->edits->first; edit && !result; edit = edit->next) {
4736                         int64_t edit_x, edit_y, edit_w, edit_h;
4737                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4738
4739 // Cursor inside a track
4740 // Cursor inside an edit
4741                         if(cursor_x >= edit_x && cursor_x < edit_x + edit_w &&
4742                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h) {
4743 // Select duration of edit
4744                                 if(button_press) {
4745                                         if( !drag_start && get_double_click() ) {
4746                                                 mwindow->edl->local_session->set_selectionstart(edit->track->from_units(edit->startproject));
4747                                                 mwindow->edl->local_session->set_selectionend(edit->track->from_units(edit->startproject) +
4748                                                         edit->track->from_units(edit->length));
4749                                                 if(mwindow->edl->session->cursor_on_frames)
4750                                                 {
4751                                                         mwindow->edl->local_session->set_selectionstart(
4752                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4753                                                         mwindow->edl->local_session->set_selectionend(
4754                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4755                                                 }
4756                                                 result = 1;
4757                                         }
4758                                         if( ctrl_down() && get_buttonpress() == 1 &&
4759                                             mwindow->edl->session->editing_mode == EDITING_ARROW ) {
4760                                                 edit->is_selected = !edit->is_selected ? 1 : 0;
4761                                                 result = 1;
4762                                         }
4763                                         if( result ) {
4764                                                 redraw = 1;
4765                                                 rerender = 1;
4766                                                 update_cursor = -1;
4767                                         }
4768                                 }
4769                                 else if( drag_start && track->record ) {
4770                                         if( mwindow->edl->session->editing_mode == EDITING_ARROW ) {
4771 // Need to create drag window
4772                                                 mwindow->session->current_operation = DRAG_EDIT;
4773                                                 mwindow->session->drag_edit = edit;
4774 // Construct list of all affected edits
4775                                                 mwindow->edl->tracks->clear_selected_edits();
4776                                                 if( ctrl_down() )
4777                                                         edit->is_selected = 1;
4778                                                 else
4779                                                         mwindow->edl->tracks->select_affected_edits(
4780                                                                 edit->track->from_units(edit->startproject),
4781                                                                 edit->track);
4782                                                 mwindow->edl->tracks->get_selected_edits(mwindow->session->drag_edits);
4783                                                 mwindow->session->drag_origin_x = cursor_x;
4784                                                 mwindow->session->drag_origin_y = cursor_y;
4785                                                 // Where the drag started, so we know relative position inside the edit later
4786                                                 mwindow->session->drag_position = (double)cursor_x *
4787                                                         mwindow->edl->local_session->zoom_sample /
4788                                                         mwindow->edl->session->sample_rate +
4789                                                         (double)mwindow->edl->local_session->view_start[pane->number] *
4790                                                         mwindow->edl->local_session->zoom_sample /
4791                                                         mwindow->edl->session->sample_rate;
4792
4793                                                 int cx, cy;
4794                                                 get_abs_cursor(cx, cy);
4795                                                 gui->drag_popup = new BC_DragWindow(gui,
4796                                                         mwindow->theme->get_image("clip_icon"), cx, cy);
4797
4798                                                 result = 1;
4799                                         }
4800                                 }
4801                         }
4802                 }
4803         }
4804         return result;
4805 }
4806
4807
4808 int TrackCanvas::test_resources(int cursor_x, int cursor_y)
4809 {
4810         return 0;
4811 }
4812
4813 int TrackCanvas::do_plugins(int cursor_x, int cursor_y, int drag_start,
4814         int button_press, int &redraw, int &rerender)
4815 {
4816         Plugin *plugin = 0;
4817         int result = 0;
4818         int done = 0;
4819         int64_t x, y, w, h;
4820         Track *track = 0;
4821
4822         for(track = mwindow->edl->tracks->first; track && !done; track = track->next) {
4823                 if(!track->expand_view) continue;
4824
4825                 for(int i = 0; i < track->plugin_set.total && !done; i++) {
4826                         // first check if plugins are visible at all
4827                         if (!track->expand_view)
4828                                 continue;
4829                         PluginSet *plugin_set = track->plugin_set.values[i];
4830                         for(plugin = (Plugin*)plugin_set->first;
4831                                 plugin && !done;
4832                                 plugin = (Plugin*)plugin->next) {
4833                                 plugin_dimensions(plugin, x, y, w, h);
4834                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
4835                                         MWindowGUI::visible(y, y + h, 0, get_h())) {
4836                                         if(cursor_x >= x && cursor_x < x + w &&
4837                                                 cursor_y >= y && cursor_y < y + h) {
4838                                                 done = 1;
4839                                                 break;
4840                                         }
4841                                 }
4842                         }
4843                 }
4844         }
4845
4846         if(plugin) {
4847 // Start plugin popup
4848                 if(button_press) {
4849                         if(get_buttonpress() == 3) {
4850                                 gui->plugin_menu->update(plugin);
4851                                 gui->plugin_menu->activate_menu();
4852                                 result = 1;
4853                         }
4854                         else if (get_double_click() && !drag_start) {
4855 // Select range of plugin on doubleclick over plugin
4856                                 mwindow->edl->local_session->set_selectionstart(plugin->track->from_units(plugin->startproject));
4857                                 mwindow->edl->local_session->set_selectionend(plugin->track->from_units(plugin->startproject) +
4858                                         plugin->track->from_units(plugin->length));
4859                                 if(mwindow->edl->session->cursor_on_frames) {
4860                                         mwindow->edl->local_session->set_selectionstart(
4861                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4862                                         mwindow->edl->local_session->set_selectionend(
4863                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4864                                 }
4865                                 rerender = 1;
4866                                 redraw = 1;
4867                                 result = 1;
4868                         }
4869                 }
4870                 else
4871 // Move plugin
4872                 if(drag_start && plugin->track->record) {
4873                         if(mwindow->edl->session->editing_mode == EDITING_ARROW) {
4874                                 if(plugin->track->data_type == TRACK_AUDIO)
4875                                         mwindow->session->current_operation = DRAG_AEFFECT_COPY;
4876                                 else if(plugin->track->data_type == TRACK_VIDEO)
4877                                         mwindow->session->current_operation = DRAG_VEFFECT_COPY;
4878
4879                                 mwindow->session->drag_plugin = plugin;
4880                                 mwindow->session->drag_origin_x = cursor_x;
4881                                 mwindow->session->drag_origin_y = cursor_y;
4882                                 // Where the drag started, so we know relative position inside the edit later
4883                                 mwindow->session->drag_position =
4884                                         (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
4885                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
4886 // Create picon
4887                                 switch(plugin->plugin_type) {
4888                                 case PLUGIN_STANDALONE: {
4889                                         PluginServer *server =
4890                                                 mwindow->scan_plugindb(plugin->title, plugin->track->data_type);
4891                                         if( !server ) break;
4892                                         VFrame *frame = server->picon;
4893                                         if(!frame) {
4894                                                 if(plugin->track->data_type == TRACK_AUDIO) {
4895                                                         frame = mwindow->theme->get_image("aeffect_icon");
4896                                                 }
4897                                                 else {
4898                                                         frame = mwindow->theme->get_image("veffect_icon");
4899                                                 }
4900                                         }
4901                                         int cx, cy;
4902                                         get_abs_cursor(cx, cy);
4903                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
4904                                                 break; }
4905
4906                                 case PLUGIN_SHAREDPLUGIN:
4907                                 case PLUGIN_SHAREDMODULE: {
4908                                         VFrame *frame = mwindow->theme->get_image("clip_icon");
4909                                         int cx, cy;
4910                                         get_abs_cursor(cx, cy);
4911                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
4912                                         break; }
4913                                 }
4914
4915                                 result = 1;
4916                         }
4917                 }
4918         }
4919
4920         return result;
4921 }
4922
4923 int TrackCanvas::do_transitions(int cursor_x, int cursor_y,
4924         int button_press, int &new_cursor, int &update_cursor)
4925 {
4926         Transition *transition = 0;
4927         int result = 0;
4928         int64_t x, y, w, h;
4929
4930
4931         for( Track *track = mwindow->edl->tracks->first; track && !result; track = track->next ) {
4932                 if( !track->show_transitions() ) continue;
4933
4934                 for( Edit *edit = track->edits->first; edit; edit = edit->next ) {
4935                         if( edit->transition ) {
4936                                 edit_dimensions(edit, x, y, w, h);
4937                                 get_transition_coords(edit, x, y, w, h);
4938
4939                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
4940                                         MWindowGUI::visible(y, y + h, 0, get_h()) ) {
4941                                         if( cursor_x >= x && cursor_x < x + w &&
4942                                                 cursor_y >= y && cursor_y < y + h ) {
4943                                                 transition = edit->transition;
4944                                                 result = 1;
4945                                                 break;
4946                                         }
4947                                 }
4948                         }
4949                 }
4950         }
4951
4952         update_cursor = 1;
4953         if(transition) {
4954                 if(!button_press) {
4955                         new_cursor = UPRIGHT_ARROW_CURSOR;
4956                 }
4957                 else if(get_buttonpress() == 3) {
4958                         gui->transition_menu->update(transition);
4959                         gui->transition_menu->activate_menu();
4960                 }
4961         }
4962
4963         return result;
4964 }
4965
4966 int TrackCanvas::button_press_event()
4967 {
4968         int result = 0;
4969         int cursor_x, cursor_y;
4970         int new_cursor;
4971
4972         cursor_x = get_cursor_x();
4973         cursor_y = get_cursor_y();
4974         mwindow->session->trim_edits = 0;
4975
4976         if(is_event_win() && cursor_inside()) {
4977 //              double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
4978
4979                 result = 1;
4980                 if(!active) {
4981                         activate();
4982                 }
4983
4984                 if( get_buttonpress() == LEFT_BUTTON ) {
4985                         gui->stop_transport("TrackCanvas::button_press_event");
4986                 }
4987
4988                 int update_overlay = 0, update_cursor = 0, rerender = 0;
4989
4990                 if(get_buttonpress() == WHEEL_UP) {
4991                         if(shift_down())
4992                                 mwindow->expand_sample();
4993                         else if(ctrl_down())
4994                                 mwindow->move_left(get_w()/ 10);
4995                         else
4996                                 mwindow->move_up(get_h() / 10);
4997                 }
4998                 else if(get_buttonpress() == WHEEL_DOWN) {
4999                         if(shift_down())
5000                                 mwindow->zoom_in_sample();
5001                         else if(ctrl_down())
5002                                 mwindow->move_right(get_w() / 10);
5003                         else
5004                                 mwindow->move_down(get_h() / 10);
5005                 }
5006                 else if(get_buttonpress() == 6) {
5007                         if(ctrl_down())
5008                                 mwindow->move_left(get_w());
5009                         else if(alt_down())
5010                                 mwindow->move_left(get_w() / 20);
5011                         else
5012                                 mwindow->move_left(get_w() / 5);
5013                 }
5014                 else if(get_buttonpress() == 7) {
5015                         if(ctrl_down())
5016                                 mwindow->move_right(get_w());
5017                         else if(alt_down())
5018                                 mwindow->move_right(get_w() / 20);
5019                         else
5020                                 mwindow->move_right(get_w() / 5);
5021                 }
5022                 else {
5023                         switch(mwindow->edl->session->editing_mode) {
5024 // Test handles and resource boundaries and highlight a track
5025                         case EDITING_ARROW: {
5026                                 if( do_transitions(cursor_x, cursor_y,
5027                                                 1, new_cursor, update_cursor) ) break;
5028
5029                                 if( do_keyframes(cursor_x, cursor_y,
5030                                         0, get_buttonpress(), new_cursor,
5031                                         update_cursor, rerender) ) break;
5032 // Test edit boundaries
5033                                 if( do_edit_handles(cursor_x, cursor_y,
5034                                         1, rerender, update_overlay, new_cursor,
5035                                         update_cursor) ) break;
5036 // Test plugin boundaries
5037                                 if( do_plugin_handles(cursor_x, cursor_y,
5038                                         1, rerender, update_overlay, new_cursor,
5039                                         update_cursor) ) break;
5040
5041                                 if( do_edits(cursor_x, cursor_y, 1, 0,
5042                                         update_overlay, rerender, new_cursor,
5043                                         update_cursor) ) break;
5044
5045                                 if( do_plugins(cursor_x, cursor_y, 0, 1,
5046                                         update_cursor, rerender) ) break;
5047
5048                                 if( test_resources(cursor_x, cursor_y) ) break;
5049
5050                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
5051
5052                                 result = 0;
5053                                 break; }
5054
5055 // Test handles only and select a region
5056                         case EDITING_IBEAM: {
5057                                 double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
5058 //printf("TrackCanvas::button_press_event %d\n", position);
5059
5060                                 if( do_transitions(cursor_x, cursor_y,
5061                                                 1, new_cursor, update_cursor)) break;
5062                                 if(do_keyframes(cursor_x, cursor_y,
5063                                         0, get_buttonpress(), new_cursor,
5064                                         update_cursor, rerender)) {
5065                                         update_overlay = 1;
5066                                         break;
5067                                 }
5068 // Test edit boundaries
5069                                 if( do_edit_handles(cursor_x, cursor_y,
5070                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
5071 // Test plugin boundaries
5072                                 if( do_plugin_handles(cursor_x, cursor_y,
5073                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
5074
5075                                 if( do_edits(cursor_x, cursor_y,
5076                                         1, 0, update_cursor, rerender, new_cursor, update_cursor) ) break;
5077
5078                                 if( do_plugins(cursor_x, cursor_y, 0, 1, update_cursor, rerender) ) break;
5079
5080                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
5081 // Highlight selection
5082                                 if( get_buttonpress() != LEFT_BUTTON ) break;
5083                                 rerender = start_selection(position);
5084                                 mwindow->session->current_operation = SELECT_REGION;
5085                                 update_cursor = 1;
5086                                 break; }
5087                         }
5088                 }
5089
5090                 if( rerender ) {
5091                         gui->unlock_window();
5092                         mwindow->cwindow->update(1, 0, 0, 0, 1);
5093                         gui->lock_window("TrackCanvas::button_press_event 2");
5094 // Update faders
5095                         mwindow->update_plugin_guis();
5096                         gui->update_patchbay();
5097                 }
5098
5099                 if( update_overlay ) {
5100                         gui->draw_overlays(1);
5101                 }
5102                 if( update_cursor < 0 ) {
5103 // double_click edit
5104                         gui->swindow->update_selection();
5105                 }
5106                 if( update_cursor ) {
5107                         gui->update_timebar(0);
5108                         gui->hide_cursor(0);
5109                         gui->show_cursor(1);
5110                         gui->zoombar->update();
5111                         gui->flash_canvas(1);
5112                 }
5113         }
5114
5115         return result;
5116 }
5117
5118 int TrackCanvas::start_selection(double position)
5119 {
5120         int rerender = 0;
5121         position = mwindow->edl->align_to_frame(position, 1);
5122
5123
5124 // Extend a border
5125         if(shift_down())
5126         {
5127                 double midpoint = (mwindow->edl->local_session->get_selectionstart(1) +
5128                         mwindow->edl->local_session->get_selectionend(1)) / 2;
5129
5130                 if(position < midpoint)
5131                 {
5132                         mwindow->edl->local_session->set_selectionstart(position);
5133                         selection_midpoint = mwindow->edl->local_session->get_selectionend(1);
5134 // Que the CWindow
5135                         rerender = 1;
5136                 }
5137                 else
5138                 {
5139                         mwindow->edl->local_session->set_selectionend(position);
5140                         selection_midpoint = mwindow->edl->local_session->get_selectionstart(1);
5141 // Don't que the CWindow for the end
5142                 }
5143         }
5144         else
5145 // Start a new selection
5146         {
5147 //printf("TrackCanvas::start_selection %f\n", position);
5148                 mwindow->edl->local_session->set_selectionstart(position);
5149                 mwindow->edl->local_session->set_selectionend(position);
5150                 selection_midpoint = position;
5151 // Que the CWindow
5152                 rerender = 1;
5153         }
5154
5155         return rerender;
5156 }
5157
5158 void TrackCanvas::end_edithandle_selection()
5159 {
5160         mwindow->modify_edithandles();
5161 }
5162
5163 void TrackCanvas::end_pluginhandle_selection()
5164 {
5165         mwindow->modify_pluginhandles();
5166 }
5167
5168
5169 double TrackCanvas::time_visible()
5170 {
5171         return (double)get_w() *
5172                 mwindow->edl->local_session->zoom_sample /
5173                 mwindow->edl->session->sample_rate;
5174 }
5175
5176
5177 void TrackCanvas::show_message(Auto *current, int show_curve_type, const char *fmt, ...)
5178 {
5179         char string[BCTEXTLEN];
5180         char *cp = string, *ep = cp + sizeof(string)-1;
5181         if( show_curve_type ) {
5182                 cp += snprintf(string, ep-cp, "%-8s ",
5183                         FloatAuto::curve_name(((FloatAuto*)current)->curve_mode));
5184         }
5185         char string2[BCTEXTLEN];
5186         Units::totext(string2,
5187                 current->autos->track->from_units(current->position),
5188                 mwindow->edl->session->time_format,
5189                 mwindow->edl->session->sample_rate,
5190                 mwindow->edl->session->frame_rate,
5191                 mwindow->edl->session->frames_per_foot);
5192         cp += snprintf(cp, ep-cp, "%s", string2);
5193         va_list ap;
5194         va_start(ap, fmt);
5195         vsnprintf(cp, ep-cp, fmt, ap);
5196         va_end(ap);
5197         gui->show_message(string);
5198 }
5199
5200 // Patchbay* TrackCanvas::get_patchbay()
5201 // {
5202 //      if(pane->patchbay) return pane->patchbay;
5203 //      if(gui->total_panes() == 2 &&
5204 //              gui->pane[TOP_LEFT_PANE] &&
5205 //              gui->pane[TOP_RIGHT_PANE])
5206 //              return gui->pane[TOP_LEFT_PANE]->patchbay;
5207 //      if(gui->total_panes() == 4)
5208 //      {
5209 //              if(pane->number == TOP_RIGHT_PANE)
5210 //                      return gui->pane[TOP_LEFT_PANE]->patchbay;
5211 //              else
5212 //                      return gui->pane[BOTTOM_LEFT_PANE]->patchbay;
5213 //      }
5214 //
5215 //      return 0;
5216 // }
5217
5218