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