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