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