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