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