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