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