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