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