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