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