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