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