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