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