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