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