add 2 asset list fmts, user title rework, added show edit, bt tweak
[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         if(!mwindow->edl->session->show_assets) return;
712
713
714 // can't stop thread here, because this is called for every pane
715 //      if(mode != IGNORE_THREAD && !indexes_only)
716 //              gui->resource_thread->stop_draw(!indexes_only);
717
718         if(mode != IGNORE_THREAD &&
719                 !indexes_only &&
720                 !gui->resource_thread->interrupted)
721         {
722                 printf("TrackCanvas::draw_resources %d: called without stopping ResourceThread\n",
723                         __LINE__);
724
725                 BC_Signals::dump_stack();
726         }
727
728         if(debug) PRINT_TRACE
729
730         resource_timer->update();
731
732 // Age resource pixmaps for deletion
733         if(!indexes_only)
734                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
735                         gui->resource_pixmaps.values[i]->visible--;
736
737         if(mode == FORCE_REDRAW)
738                 gui->resource_pixmaps.remove_all_objects();
739
740         if(debug) PRINT_TRACE
741
742 // Search every edit
743         for(Track *current = mwindow->edl->tracks->first;
744                 current;
745                 current = NEXT)
746         {
747                 if(debug) PRINT_TRACE
748                 for(Edit *edit = current->edits->first; edit; edit = edit->next)
749                 {
750                         if(debug) PRINT_TRACE
751                         if( current->data_type != TRACK_SUBTITLE )
752                                 if(!edit->asset && !edit->nested_edl) continue;
753                         if(indexes_only)
754                         {
755                                 if(edit->track->data_type != TRACK_AUDIO) continue;
756
757                                 if(edit->nested_edl &&
758                                         strcmp(indexable->path, edit->nested_edl->path)) continue;
759
760                                 if(edit->asset &&
761                                         strcmp(indexable->path, edit->asset->path)) continue;
762                         }
763
764                         if(debug) PRINT_TRACE
765
766                         int64_t edit_x, edit_y, edit_w, edit_h;
767                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
768
769 // Edit is visible
770                         if(MWindowGUI::visible(edit_x, edit_x + edit_w, 0, get_w()) &&
771                                 MWindowGUI::visible(edit_y, edit_y + edit_h, 0, get_h()))
772                         {
773                                 int64_t pixmap_x, pixmap_w, pixmap_h;
774                                 if(debug) PRINT_TRACE
775
776 // Search for existing pixmap containing edit
777                                 for(int i = 0; i < gui->resource_pixmaps.total; i++)
778                                 {
779                                         ResourcePixmap* pixmap = gui->resource_pixmaps.values[i];
780 // Same pointer can be different edit if editing took place
781                                         if(pixmap->edit_id == edit->id &&
782                                                 pixmap->pane_number == pane->number)
783                                         {
784                                                 pixmap->visible = 1;
785                                                 break;
786                                         }
787                                 }
788                                 if(debug) PRINT_TRACE
789
790 // Get new size, offset of pixmap needed
791                                 get_pixmap_size(edit,
792                                         edit_x,
793                                         edit_w,
794                                         pixmap_x,
795                                         pixmap_w,
796                                         pixmap_h);
797                                 if(debug) PRINT_TRACE
798
799 // Draw new data
800                                 if(pixmap_w && pixmap_h)
801                                 {
802 // Create pixmap if it doesn't exist
803                                         ResourcePixmap* pixmap = create_pixmap(edit,
804                                                 edit_x,
805                                                 pixmap_x,
806                                                 pixmap_w,
807                                                 pixmap_h);
808 // Resize it if it's bigger
809                                         if(pixmap_w > pixmap->pixmap_w ||
810                                                 pixmap_h > pixmap->pixmap_h)
811                                                 pixmap->resize(pixmap_w, pixmap_h);
812                                         pixmap->draw_data(this,
813                                                 edit,
814                                                 edit_x,
815                                                 edit_w,
816                                                 pixmap_x,
817                                                 pixmap_w,
818                                                 pixmap_h,
819                                                 mode,
820                                                 indexes_only);
821 // Resize it if it's smaller
822                                         if(pixmap_w < pixmap->pixmap_w ||
823                                                 pixmap_h < pixmap->pixmap_h)
824                                                 pixmap->resize(pixmap_w, pixmap_h);
825
826 // Copy pixmap to background canvas
827                                         background_pixmap->draw_pixmap(pixmap,
828                                                 pixmap->pixmap_x,
829                                                 current->y_pixel - mwindow->edl->local_session->track_start[pane->number],
830                                                 pixmap->pixmap_w,
831                                                 edit_h);
832                                 }
833                                 if(debug) PRINT_TRACE
834
835                         }
836                 }
837         }
838
839
840 // Delete unused pixmaps
841         if(debug) PRINT_TRACE
842         if(!indexes_only)
843                 for(int i = gui->resource_pixmaps.total - 1; i >= 0; i--)
844                         if(gui->resource_pixmaps.values[i]->visible < PIXMAP_AGE)
845                         {
846 //printf("TrackCanvas::draw_resources %d\n", __LINE__);
847                                 delete gui->resource_pixmaps.values[i];
848                                 gui->resource_pixmaps.remove(gui->resource_pixmaps.values[i]);
849                         }
850         if(debug) PRINT_TRACE
851
852         if(hourglass_enabled)
853         {
854                 stop_hourglass();
855                 hourglass_enabled = 0;
856         }
857         if(debug) PRINT_TRACE
858
859 // can't stop thread here, because this is called for every pane
860 //      if(mode != IGNORE_THREAD && !indexes_only)
861 //              gui->resource_thread->start_draw();
862         if(debug) PRINT_TRACE
863
864
865 }
866
867 ResourcePixmap* TrackCanvas::create_pixmap(Edit *edit,
868         int64_t edit_x,
869         int64_t pixmap_x,
870         int64_t pixmap_w,
871         int64_t pixmap_h)
872 {
873         ResourcePixmap *result = 0;
874
875         for(int i = 0; i < gui->resource_pixmaps.total; i++)
876         {
877 //printf("TrackCanvas::create_pixmap 1 %d %d\n", edit->id, resource_pixmaps.values[i]->edit->id);
878                 if(gui->resource_pixmaps.values[i]->edit_id == edit->id &&
879                         gui->resource_pixmaps.values[i]->pane_number == pane->number)
880                 {
881                         result = gui->resource_pixmaps.values[i];
882                         break;
883                 }
884         }
885
886         if(!result)
887         {
888 //SET_TRACE
889                 result = new ResourcePixmap(mwindow,
890                         gui,
891                         edit,
892                         pane->number,
893                         pixmap_w,
894                         pixmap_h);
895 //SET_TRACE
896                 gui->resource_pixmaps.append(result);
897         }
898
899 //      result->resize(pixmap_w, pixmap_h);
900         return result;
901 }
902
903 void TrackCanvas::get_pixmap_size(Edit *edit,
904         int64_t edit_x,
905         int64_t edit_w,
906         int64_t &pixmap_x,
907         int64_t &pixmap_w,
908         int64_t &pixmap_h)
909 {
910
911 // Align x on frame boundaries
912
913
914 //      switch(edit->edits->track->data_type)
915 //      {
916 //              case TRACK_AUDIO:
917
918                         pixmap_x = edit_x;
919                         pixmap_w = edit_w;
920                         if(pixmap_x < 0)
921                         {
922                                 pixmap_w -= -edit_x;
923                                 pixmap_x = 0;
924                         }
925
926                         if(pixmap_x + pixmap_w > get_w())
927                         {
928                                 pixmap_w = get_w() - pixmap_x;
929                         }
930
931 //                      break;
932 //
933 //              case TRACK_VIDEO:
934 //              {
935 //                      int64_t picon_w = (int64_t)(edit->picon_w() + 0.5);
936 //                      int64_t frame_w = (int64_t)(edit->frame_w() + 0.5);
937 //                      int64_t pixel_increment = MAX(picon_w, frame_w);
938 //                      int64_t pixmap_x1 = edit_x;
939 //                      int64_t pixmap_x2 = edit_x + edit_w;
940 //
941 //                      if(pixmap_x1 < 0)
942 //                      {
943 //                              pixmap_x1 = (int64_t)((double)-edit_x / pixel_increment) *
944 //                                      pixel_increment +
945 //                                      edit_x;
946 //                      }
947 //
948 //                      if(pixmap_x2 > get_w())
949 //                      {
950 //                              pixmap_x2 = (int64_t)((double)(get_w() - edit_x) / pixel_increment + 1) *
951 //                                      pixel_increment +
952 //                                      edit_x;
953 //                      }
954 //                      pixmap_x = pixmap_x1;
955 //                      pixmap_w = pixmap_x2 - pixmap_x1;
956 //                      break;
957 //              }
958 //      }
959
960         pixmap_h = mwindow->edl->local_session->zoom_track;
961         if(mwindow->edl->session->show_titles) pixmap_h += mwindow->theme->get_image("title_bg_data")->get_h();
962 //printf("get_pixmap_size %d %d %d %d\n", edit_x, edit_w, pixmap_x, pixmap_w);
963 }
964
965 void TrackCanvas::edit_dimensions(Edit *edit,
966         int64_t &x,
967         int64_t &y,
968         int64_t &w,
969         int64_t &h)
970 {
971 //      w = Units::round(edit->track->from_units(edit->length) *
972 //              mwindow->edl->session->sample_rate /
973 //              mwindow->edl->local_session->zoom_sample);
974
975         h = resource_h();
976
977         x = Units::round(edit->track->from_units(edit->startproject) *
978                         mwindow->edl->session->sample_rate /
979                         mwindow->edl->local_session->zoom_sample -
980                         mwindow->edl->local_session->view_start[pane->number]);
981
982 // Method for calculating w so when edits are together we never get off by one error due to rounding
983         int64_t x_next = Units::round(edit->track->from_units(edit->startproject + edit->length) *
984                         mwindow->edl->session->sample_rate /
985                         mwindow->edl->local_session->zoom_sample -
986                         mwindow->edl->local_session->view_start[pane->number]);
987         w = x_next - x;
988
989         y = edit->edits->track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
990
991         if(mwindow->edl->session->show_titles)
992                 h += mwindow->theme->get_image("title_bg_data")->get_h();
993 }
994
995 void TrackCanvas::track_dimensions(Track *track, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
996 {
997         x = 0;
998         w = get_w();
999         y = track->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1000         h = track->vertical_span(mwindow->theme);
1001 }
1002
1003
1004 void TrackCanvas::draw_paste_destination()
1005 {
1006         //int cursor_x = get_relative_cursor_x();
1007         //int cursor_y = get_relative_cursor_y();
1008         int current_atrack = 0;
1009         int current_vtrack = 0;
1010         int current_aedit = 0;
1011         int current_vedit = 0;
1012         int64_t w = 0;
1013         int64_t x;
1014         double position;
1015         int insertion  = 0;
1016
1017 //if(pane->number == BOTTOM_RIGHT_PANE)
1018 //printf("TrackCanvas::draw_paste_destination %d %p\n", __LINE__, mwindow->session->track_highlighted);
1019
1020         if((mwindow->session->current_operation == DRAG_ASSET &&
1021                         (mwindow->session->drag_assets->total ||
1022                         mwindow->session->drag_clips->total)) ||
1023                 (mwindow->session->current_operation == DRAG_EDIT &&
1024                         mwindow->session->drag_edits->total)) {
1025                 Indexable *indexable = 0;
1026                 EDL *clip = 0;
1027                 //int draw_box = 0;
1028
1029                 if(mwindow->session->current_operation == DRAG_ASSET &&
1030                         mwindow->session->drag_assets->size())
1031                         indexable = mwindow->session->drag_assets->get(0);
1032
1033                 if(mwindow->session->current_operation == DRAG_ASSET &&
1034                         mwindow->session->drag_clips->size())
1035                         clip = mwindow->session->drag_clips->get(0);
1036
1037                 int has_audio = 0, has_video = 0;
1038                 double paste_audio_length = 0, paste_video_length = 0;
1039                 double paste_audio_position = -1, paste_video_position = -1;
1040
1041                 if( indexable ) {
1042                         has_audio = indexable->have_audio();
1043                         paste_audio_length = !has_audio ? 0 :
1044                                  (double)indexable->get_audio_samples() / indexable->get_sample_rate();
1045                         has_video = indexable->have_video();
1046                         Asset *asset = indexable->is_asset ? (Asset *)indexable : 0;
1047                         // Images have length -1
1048                         if( asset && asset->video_data && asset->video_length < 0 ) {
1049                                 paste_video_length = mwindow->edl->session->si_useduration ?
1050                                         mwindow->edl->session->si_duration / asset->frame_rate :
1051                                         1.0 / asset->frame_rate ;  // 1 frame
1052                         }
1053                         else
1054                                 paste_video_length = !has_video ? 0 :
1055                                         (double)indexable->get_video_frames() / indexable->get_frame_rate();
1056                 }
1057
1058                 if( clip ) {
1059                         has_audio = clip->tracks->total_audio_tracks() > 0 ? 1 : 0;
1060                         paste_audio_length = !has_audio ? 0 : clip->tracks->total_audio_length();
1061                         has_video = clip->tracks->total_video_tracks() > 0 ? 1 : 0;
1062                         paste_video_length = !has_video ? 0 : clip->tracks->total_video_length();
1063                 }
1064
1065 // 'Align cursor on frame' lengths calculations
1066                 if( mwindow->edl->session->cursor_on_frames ) {
1067                         double fps = mwindow->edl->session->frame_rate;
1068                         double aud = floor(paste_audio_length * fps) / fps;
1069                         double vid = floor(paste_video_length * fps) / fps;
1070                         double length = has_video && has_audio ?
1071                                         aud < vid ? aud : vid :
1072                                 has_video ? vid :
1073                                 has_audio ? aud : 0;
1074                         paste_video_length = paste_audio_length = length;
1075                 }
1076
1077                 if( has_audio ) { // we use video if we are over video and audio if we are over audio
1078                         int64_t length = 0;
1079                         switch( mwindow->session->track_highlighted->data_type ) {
1080                         case TRACK_VIDEO: length = paste_video_length;  break;
1081                         case TRACK_AUDIO: length = paste_audio_length;  break;
1082                         }
1083                         int64_t asset_length = mwindow->session->track_highlighted->
1084                                 to_units(length, 1);
1085                         paste_audio_position = mwindow->session->track_highlighted->
1086                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1087                 }
1088                 if( has_video ) {
1089                         int64_t asset_length = mwindow->session->track_highlighted->
1090                                 to_units(paste_video_length, 1);
1091                         paste_video_position = mwindow->session->track_highlighted->
1092                                 from_units(drop_edit_position(&insertion, NULL, asset_length));
1093                 }
1094
1095 // Get destination track
1096                 for(Track *dest = mwindow->session->track_highlighted;
1097                         dest;
1098                         dest = dest->next) {
1099                         if(dest->record) {
1100 // Get source width in pixels
1101                                 w = -1;
1102 // Use start of highlighted edit
1103                                 if(mwindow->session->edit_highlighted) {
1104                                         position = mwindow->session->track_highlighted->from_units(
1105                                                 mwindow->session->edit_highlighted->startproject);
1106                                 }
1107                                 else {
1108 // Use end of highlighted track, disregarding effects
1109                                         position = mwindow->session->track_highlighted->from_units(
1110                                                 mwindow->session->track_highlighted->edits->length());
1111                                 }
1112
1113 // Get the x coordinate
1114                                 x = Units::to_int64(position *
1115                                         mwindow->edl->session->sample_rate /
1116                                         mwindow->edl->local_session->zoom_sample) -
1117                                         mwindow->edl->local_session->view_start[pane->number];
1118
1119                                 double paste_position = -1.;
1120                                 if(dest->data_type == TRACK_AUDIO) {
1121                                         if(indexable && current_atrack < indexable->get_audio_channels()) {
1122 //printf("TrackCanvas::draw_paste_destination %d %d\n", __LINE__, current_atrack);
1123                                                 w = Units::to_int64((double)indexable->get_audio_samples() /
1124                                                         indexable->get_sample_rate() *
1125                                                         mwindow->edl->session->sample_rate /
1126                                                         mwindow->edl->local_session->zoom_sample);
1127                                                 paste_position = paste_audio_position;
1128                                         }
1129                                         else if(clip && current_atrack < clip->tracks->total_audio_tracks()) {
1130                                                 w = Units::to_int64((double)clip->tracks->total_length() *
1131                                                         mwindow->edl->session->sample_rate /
1132                                                         mwindow->edl->local_session->zoom_sample);
1133                                                 paste_position = paste_audio_position;
1134 //printf("draw_paste_destination %d\n", x);
1135                                         }
1136                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1137                                                 current_aedit < mwindow->session->drag_edits->total) {
1138                                                 Edit *edit;
1139                                                 while(current_aedit < mwindow->session->drag_edits->total &&
1140                                                         mwindow->session->drag_edits->values[current_aedit]->track->data_type != TRACK_AUDIO)
1141                                                         current_aedit++;
1142
1143                                                 if(current_aedit < mwindow->session->drag_edits->total) {
1144                                                         edit = mwindow->session->drag_edits->values[current_aedit];
1145                                                         w = Units::to_int64(edit->length / mwindow->edl->local_session->zoom_sample);
1146                                                         paste_position = mwindow->session->track_highlighted->
1147                                                                 from_units(drop_edit_position(&insertion,
1148                                                                         mwindow->session->drag_edit,
1149                                                                         mwindow->session->drag_edit->length));
1150                                                         current_aedit++;
1151                                                 }
1152                                         }
1153                                         if( paste_position >= 0 ) {
1154                                                 position = paste_position;
1155                                                 current_atrack++;
1156                                                 //draw_box = 1;
1157                                         }
1158                                         else
1159                                                 w = -1;
1160                                 }
1161                                 else if(dest->data_type == TRACK_VIDEO) {
1162 //printf("draw_paste_destination 1\n");
1163                                         if(indexable && current_vtrack < indexable->get_video_layers())
1164                                         {
1165                                                 w = Units::to_int64((double)indexable->get_video_frames() /
1166                                                         indexable->get_frame_rate() *
1167                                                         mwindow->edl->session->sample_rate /
1168                                                         mwindow->edl->local_session->zoom_sample);
1169                                                 paste_position = paste_video_position;
1170                                         }
1171                                         else if(clip && current_vtrack < clip->tracks->total_video_tracks()) {
1172                                                 w = Units::to_int64(clip->tracks->total_length() *
1173                                                         mwindow->edl->session->sample_rate /
1174                                                         mwindow->edl->local_session->zoom_sample);
1175                                                 paste_position = paste_video_position;
1176                                         }
1177                                         else if(mwindow->session->current_operation == DRAG_EDIT &&
1178                                                 current_vedit < mwindow->session->drag_edits->total) {
1179                                                 Edit *edit;
1180                                                 while(current_vedit < mwindow->session->drag_edits->total &&
1181                                                         mwindow->session->drag_edits->values[current_vedit]->track->data_type != TRACK_VIDEO)
1182                                                         current_vedit++;
1183
1184                                                 if(current_vedit < mwindow->session->drag_edits->total) {
1185                                                         edit = mwindow->session->drag_edits->values[current_vedit];
1186                                                         w = Units::to_int64(edit->track->from_units(edit->length) *
1187                                                                 mwindow->edl->session->sample_rate /
1188                                                                 mwindow->edl->local_session->zoom_sample);
1189                                                         paste_position = mwindow->session->track_highlighted->
1190                                                                 from_units(drop_edit_position(&insertion,
1191                                                                         mwindow->session->drag_edit,
1192                                                                         mwindow->session->drag_edit->length));
1193                                                         current_vedit++;
1194                                                 }
1195                                         }
1196                                         if( paste_position >= 0 ) {
1197                                                 position = paste_position;
1198                                                 current_vedit++;
1199                                                 //draw_box = 1;
1200                                         }
1201                                         else
1202                                                 w = -1;
1203                                 }
1204
1205                                 if(w >= 0) {
1206 // Get the x coordinate
1207                                         x = Units::to_int64(position *
1208                                                 mwindow->edl->session->sample_rate /
1209                                                 mwindow->edl->local_session->zoom_sample) -
1210                                                 mwindow->edl->local_session->view_start[pane->number];
1211                                         int y = dest->y_pixel - mwindow->edl->local_session->track_start[pane->number];
1212                                         int h = dest->vertical_span(mwindow->theme);
1213
1214 //printf("TrackCanvas::draw_paste_destination 2 %d %d %d %d\n", x, y, w, h);
1215                                         if(x < -BC_INFINITY) {
1216                                                 w -= -BC_INFINITY - x;
1217                                                 x += -BC_INFINITY - x;
1218                                         }
1219                                         w = MIN(65535, w);
1220 // if(pane->number == TOP_RIGHT_PANE)
1221 // printf("TrackCanvas::draw_paste_destination %d %d %d %d %d\n",
1222 // __LINE__, x, y, w, h);
1223                                         if (insertion)
1224                                                 draw_highlight_insertion(x, y, w, h);
1225                                         else
1226                                                 draw_highlight_rectangle(x, y, w, h);
1227                                 }
1228                         }
1229                 }
1230         }
1231 }
1232
1233 void TrackCanvas::plugin_dimensions(Plugin *plugin, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1234 {
1235         x = Units::round(plugin->track->from_units(plugin->startproject) *
1236                 mwindow->edl->session->sample_rate /
1237                 mwindow->edl->local_session->zoom_sample -
1238                 mwindow->edl->local_session->view_start[pane->number]);
1239         w = Units::round(plugin->track->from_units(plugin->length) *
1240                 mwindow->edl->session->sample_rate /
1241                 mwindow->edl->local_session->zoom_sample);
1242         y = plugin->track->y_pixel -
1243                 mwindow->edl->local_session->track_start[pane->number] +
1244                 mwindow->edl->local_session->zoom_track +
1245                 plugin->plugin_set->get_number() *
1246                 mwindow->theme->get_image("plugin_bg_data")->get_h();
1247         if(mwindow->edl->session->show_titles)
1248                 y += mwindow->theme->get_image("title_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(mwindow->edl->session->show_titles)
1382         {
1383                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1384         }
1385         else
1386         {
1387                 y = 0;
1388         }
1389
1390         if(side == EDIT_OUT)
1391         {
1392                 x += w - handle_w;
1393         }
1394
1395         h = handle_h;
1396         w = handle_w;
1397 }
1398
1399 void TrackCanvas::get_transition_coords(int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1400 {
1401 //printf("TrackCanvas::get_transition_coords 1\n");
1402 //      int transition_w = mwindow->theme->transitionhandle_data[0]->get_w();
1403 //      int transition_h = mwindow->theme->transitionhandle_data[0]->get_h();
1404         int transition_w = 30;
1405         int transition_h = 30;
1406 //printf("TrackCanvas::get_transition_coords 1\n");
1407
1408         if(mwindow->edl->session->show_titles)
1409                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1410 //printf("TrackCanvas::get_transition_coords 2\n");
1411
1412         y += (h - mwindow->theme->get_image("title_bg_data")->get_h()) / 2 - transition_h / 2;
1413         x -= transition_w / 2;
1414
1415         h = transition_h;
1416         w = transition_w;
1417 }
1418
1419 void TrackCanvas::draw_highlighting()
1420 {
1421         int64_t x, y, w, h;
1422         int draw_box = 0;
1423
1424         switch(mwindow->session->current_operation)
1425         {
1426                 case DRAG_ATRANSITION:
1427                 case DRAG_VTRANSITION:
1428 //printf("TrackCanvas::draw_highlighting 1 %p %p\n",
1429 //      mwindow->session->track_highlighted, mwindow->session->edit_highlighted);
1430                         if(mwindow->session->edit_highlighted) {
1431 //printf("TrackCanvas::draw_highlighting 2\n");
1432                                 if((mwindow->session->current_operation == DRAG_ATRANSITION &&
1433                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1434                                         (mwindow->session->current_operation == DRAG_VTRANSITION &&
1435                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO)) {
1436 //printf("TrackCanvas::draw_highlighting 2\n");
1437                                         edit_dimensions(mwindow->session->edit_highlighted, x, y, w, h);
1438 //printf("TrackCanvas::draw_highlighting 2\n");
1439
1440                                         if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1441                                                 MWindowGUI::visible(y, y + h, 0, get_h())) {
1442                                                 draw_box = 1;
1443                                                 get_transition_coords(x, y, w, h);
1444                                         }
1445 //printf("TrackCanvas::draw_highlighting 3\n");
1446                                 }
1447                         }
1448                         break;
1449
1450 // Dragging a new effect from the Resource window
1451                 case DRAG_AEFFECT:
1452                 case DRAG_VEFFECT:
1453                         if(mwindow->session->track_highlighted &&
1454                                 ((mwindow->session->current_operation == DRAG_AEFFECT &&
1455                                   mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1456                                  (mwindow->session->current_operation == DRAG_VEFFECT &&
1457                                   mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1458 // Put it before another plugin
1459                                 if(mwindow->session->plugin_highlighted) {
1460                                         plugin_dimensions(mwindow->session->plugin_highlighted,
1461                                                 x, y, w, h);
1462 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1463                                 }
1464 // Put it after a plugin set
1465                                 else if( mwindow->session->pluginset_highlighted &&
1466                                          mwindow->session->pluginset_highlighted->last ) {
1467                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last,
1468                                                 x, y, w, h);
1469 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1470                                         int64_t track_x, track_y, track_w, track_h;
1471                                         track_dimensions(mwindow->session->track_highlighted,
1472                                                 track_x, track_y, track_w, track_h);
1473
1474                                         x += w;
1475                                         w = Units::round(
1476                                                         mwindow->session->track_highlighted->get_length() *
1477                                                         mwindow->edl->session->sample_rate /
1478                                                         mwindow->edl->local_session->zoom_sample -
1479                                                         mwindow->edl->local_session->view_start[pane->number]) -
1480                                                 x;
1481 //printf("TrackCanvas::draw_highlighting 2 %d\n", w);
1482                                         if(w <= 0) w = track_w;
1483                                 }
1484                                 else {
1485                                         track_dimensions(mwindow->session->track_highlighted,
1486                                                 x, y, w, h);
1487
1488 //printf("TrackCanvas::draw_highlighting 1 %d %d %d %d\n", x, y, w, h);
1489 // Put it in a new plugin set determined by the selected range
1490                                         if(mwindow->edl->local_session->get_selectionend() >
1491                                                 mwindow->edl->local_session->get_selectionstart()) {
1492                                                 x = Units::to_int64(mwindow->edl->local_session->get_selectionstart() *
1493                                                         mwindow->edl->session->sample_rate /
1494                                                         mwindow->edl->local_session->zoom_sample -
1495                                                         mwindow->edl->local_session->view_start[pane->number]);
1496                                                 w = Units::to_int64((mwindow->edl->local_session->get_selectionend() -
1497                                                         mwindow->edl->local_session->get_selectionstart()) *
1498                                                         mwindow->edl->session->sample_rate /
1499                                                         mwindow->edl->local_session->zoom_sample);
1500                                         }
1501 // Put it in a new plugin set determined by an edit boundary
1502                                         else if(mwindow->session->edit_highlighted) {
1503                                                 int64_t temp_y, temp_h;
1504                                                 edit_dimensions(mwindow->session->edit_highlighted,
1505                                                         x, temp_y, w, temp_h);
1506                                         }
1507 // Put it at the beginning of the track in a new plugin set
1508                                 }
1509
1510                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1511                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1512 //printf("TrackCanvas::draw_highlighting 1\n");
1513                                         draw_box = 1;
1514                                 }
1515                         }
1516                         break;
1517
1518                 case DRAG_ASSET:
1519                         if(mwindow->session->track_highlighted) {
1520 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1521
1522 //                              if(MWindowGUI::visible(y, y + h, 0, get_h()))
1523 //                              {
1524                                         draw_paste_destination();
1525 //                              }
1526                         }
1527                         break;
1528
1529                 case DRAG_EDIT:
1530                         if(mwindow->session->track_highlighted) {
1531 //                              track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1532 //
1533 //                              if(MWindowGUI::visible(y, y + h, 0, get_h())) {
1534                                         draw_paste_destination();
1535 //                              }
1536                         }
1537                         break;
1538
1539 // Dragging an effect from the timeline
1540                 case DRAG_AEFFECT_COPY:
1541                 case DRAG_VEFFECT_COPY:
1542                         if( (mwindow->session->plugin_highlighted || mwindow->session->track_highlighted) &&
1543                             ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
1544                               mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1545                              (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
1546                               mwindow->session->track_highlighted->data_type == TRACK_VIDEO))) {
1547
1548                                 int64_t plugin_position = -1;
1549                                 Plugin *drag_plugin = mwindow->session->drag_plugin;
1550                                 PluginSet *hi_plugin_set = mwindow->session->pluginset_highlighted;
1551                                 Track *hi_track = mwindow->session->track_highlighted;
1552                                 Edit *hi_edit = mwindow->session->edit_highlighted;
1553 // Put it into a silence zone
1554                                 if( hi_plugin_set && hi_plugin_set->last &&
1555                                     hi_plugin_set->track == drag_plugin->track ) {
1556                                         plugin_dimensions((Plugin *)hi_plugin_set->last, x, y, w, h);
1557                                         plugin_position = drop_plugin_position(hi_plugin_set, drag_plugin);
1558                                         hi_track = drag_plugin->track;
1559                                 }
1560                                 else if( hi_track ) {
1561 // Put it in a new plugin set determined by an edit boundary, or at the start of the track
1562                                         track_dimensions(hi_track, x, y, w, h);
1563                                         plugin_position = hi_edit ? hi_edit->startproject : 0;
1564                                 }
1565
1566                                 if( plugin_position < 0 ) break;
1567
1568 // Calculate length of plugin based on data type of track and units
1569                                 double zoom_scale = (double)mwindow->edl->session->sample_rate /
1570                                                 mwindow->edl->local_session->zoom_sample;
1571                                 x = Units::round(hi_track->from_units(plugin_position) * zoom_scale) -
1572                                                 mwindow->edl->local_session->view_start[pane->number];
1573                                 w = Units::round(hi_track->from_units(drag_plugin->length) * zoom_scale);
1574
1575                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1576                                     MWindowGUI::visible(y, y + h, 0, get_h()) ) {
1577                                         draw_box = 1;
1578                                 }
1579                         }
1580                         break;
1581
1582                 case DRAG_PLUGINKEY:
1583                         if(mwindow->session->plugin_highlighted &&
1584                            mwindow->session->current_operation == DRAG_PLUGINKEY)
1585                         {
1586 // Just highlight the plugin
1587                                 plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1588
1589                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1590                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1591                                 {
1592                                         draw_box = 1;
1593                                 }
1594                         }
1595                         break;
1596
1597         }
1598
1599
1600         if(draw_box)
1601         {
1602                 draw_highlight_rectangle(x, y, w, h);
1603         }
1604 }
1605
1606 void TrackCanvas::draw_plugins()
1607 {
1608         char string[BCTEXTLEN];
1609         int current_on = 0;
1610         int current_show = 0;
1611         int current_preset = 0;
1612
1613 //      if(!mwindow->edl->session->show_assets) goto done;
1614
1615         for(int i = 0; i < plugin_on_toggles.total; i++)
1616                 plugin_on_toggles.values[i]->in_use = 0;
1617         for(int i = 0; i < plugin_show_toggles.total; i++)
1618                 plugin_show_toggles.values[i]->in_use = 0;
1619         for(int i = 0; i < preset_edit_buttons.total; i++)
1620                 plugin_show_toggles.values[i]->in_use = 0;
1621
1622
1623         for(Track *track = mwindow->edl->tracks->first;
1624                 track;
1625                 track = track->next)
1626         {
1627                 if(track->expand_view)
1628                 {
1629                         for(int i = 0; i < track->plugin_set.total; i++)
1630                         {
1631                                 PluginSet *pluginset = track->plugin_set.values[i];
1632
1633                                 for(Plugin *plugin = (Plugin*)pluginset->first; plugin; plugin = (Plugin*)plugin->next)
1634                                 {
1635                                         int64_t total_x, y, total_w, h;
1636                                         plugin_dimensions(plugin, total_x, y, total_w, h);
1637
1638                                         if(MWindowGUI::visible(total_x, total_x + total_w, 0, get_w()) &&
1639                                                 MWindowGUI::visible(y, y + h, 0, get_h()) &&
1640                                                 plugin->plugin_type != PLUGIN_NONE)
1641                                         {
1642                                                 int x = total_x, w = total_w, left_margin = 5;
1643                                                 int right_margin = 5;
1644                                                 if(x < 0)
1645                                                 {
1646                                                         w -= -x;
1647                                                         x = 0;
1648                                                 }
1649                                                 if(w + x > get_w()) w -= (w + x) - get_w();
1650
1651                                                 draw_3segmenth(x,
1652                                                         y,
1653                                                         w,
1654                                                         total_x,
1655                                                         total_w,
1656                                                         mwindow->theme->get_image("plugin_bg_data"),
1657                                                         0);
1658                                                 set_color(mwindow->theme->title_color);
1659                                                 set_font(mwindow->theme->title_font);
1660                                                 plugin->calculate_title(string, 0);
1661
1662 // Truncate string to int64_test visible in background
1663                                                 int len = strlen(string), j;
1664                                                 for(j = len; j >= 0; j--)
1665                                                 {
1666                                                         if(left_margin + get_text_width(mwindow->theme->title_font, string) > w)
1667                                                         {
1668                                                                 string[j] = 0;
1669                                                         }
1670                                                         else
1671                                                                 break;
1672                                                 }
1673
1674 // Justify the text on the left boundary of the edit if it is visible.
1675 // Otherwise justify it on the left side of the screen.
1676                                                 int64_t text_x = total_x + left_margin;
1677                                                 int64_t text_w = get_text_width(mwindow->theme->title_font, string, strlen(string));
1678                                                 text_x = MAX(left_margin, text_x);
1679                                                 draw_text(text_x,
1680                                                         y + get_text_ascent(mwindow->theme->title_font) + 2,
1681                                                         string,
1682                                                         strlen(string),
1683                                                         0);
1684                                                 int64_t min_x = total_x + text_w;
1685
1686
1687 // Update plugin toggles
1688                                                 int toggle_x = total_x + total_w;
1689                                                 int toggle_y = y;
1690                                                 toggle_x = MIN(get_w() - right_margin, toggle_x);
1691
1692 // On toggle
1693                                                 toggle_x -= PluginOn::calculate_w(mwindow) + 10;
1694                                                 if(toggle_x > min_x)
1695                                                 {
1696                                                         if(current_on >= plugin_on_toggles.total)
1697                                                         {
1698                                                                 PluginOn *plugin_on = new PluginOn(mwindow, toggle_x, toggle_y, plugin);
1699                                                                 add_subwindow(plugin_on);
1700                                                                 plugin_on_toggles.append(plugin_on);
1701                                                         }
1702                                                         else
1703                                                         {
1704                                                                 plugin_on_toggles.values[current_on]->update(toggle_x, toggle_y, plugin);
1705                                                         }
1706                                                         current_on++;
1707                                                 }
1708
1709 // Toggles for standalone plugins only
1710                                                 if(plugin->plugin_type == PLUGIN_STANDALONE)
1711                                                 {
1712 // Show
1713                                                         toggle_x -= PluginShow::calculate_w(mwindow) + 10;
1714                                                         if(toggle_x > min_x)
1715                                                         {
1716                                                                 if(current_show >= plugin_show_toggles.total)
1717                                                                 {
1718                                                                         PluginShow *plugin_show = new PluginShow(mwindow, toggle_x, toggle_y, plugin);
1719                                                                         add_subwindow(plugin_show);
1720                                                                         plugin_show_toggles.append(plugin_show);
1721                                                                 }
1722                                                                 else
1723                                                                 {
1724                                                                         plugin_show_toggles.values[current_show]->update(toggle_x, toggle_y, plugin);
1725                                                                 }
1726                                                                 current_show++;
1727                                                         }
1728                                                         toggle_x -= PluginPresetEdit::calculate_w(mwindow) + 10;
1729                                                         if(toggle_x > min_x)
1730                                                         {
1731                                                                 if(current_preset >= preset_edit_buttons.total)
1732                                                                 {
1733                                                                         PluginPresetEdit *preset_edit = new PluginPresetEdit(mwindow, toggle_x, toggle_y, plugin);
1734                                                                         add_subwindow(preset_edit);
1735                                                                         preset_edit_buttons.append(preset_edit);
1736                                                                 }
1737                                                                 else
1738                                                                 {
1739                                                                         preset_edit_buttons.values[current_preset]->update(toggle_x, toggle_y, plugin);
1740                                                                 }
1741                                                                 current_preset++;
1742                                                         }
1743                                                 }
1744                                         }
1745                                 }
1746                         }
1747                 }
1748         }
1749
1750 // Remove unused toggles
1751
1752         while(current_preset < preset_edit_buttons.total)
1753         {
1754                 preset_edit_buttons.remove_object_number(current_preset);
1755         }
1756         while(current_show < plugin_show_toggles.total)
1757         {
1758                 plugin_show_toggles.remove_object_number(current_show);
1759         }
1760
1761         while(current_on < plugin_on_toggles.total)
1762         {
1763                 plugin_on_toggles.remove_object_number(current_on);
1764         }
1765 }
1766
1767 void TrackCanvas::refresh_plugintoggles()
1768 {
1769         for(int i = 0; i < plugin_on_toggles.total; i++)
1770         {
1771                 PluginOn *on = plugin_on_toggles.values[i];
1772                 on->reposition_window(on->get_x(), on->get_y());
1773         }
1774         for(int i = 0; i < plugin_show_toggles.total; i++)
1775         {
1776                 PluginShow *show = plugin_show_toggles.values[i];
1777                 show->reposition_window(show->get_x(), show->get_y());
1778         }
1779         for(int i = 0; i < preset_edit_buttons.total; i++)
1780         {
1781                 PluginPresetEdit *preset_edit = preset_edit_buttons.values[i];
1782                 preset_edit->reposition_window(preset_edit->get_x(), preset_edit->get_y());
1783         }
1784 }
1785
1786 void TrackCanvas::draw_hard_edges()
1787 {
1788         int64_t x, y, w, h;
1789
1790 //      if(!mwindow->edl->session->show_assets) return;
1791
1792         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1793                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1794                         if( !edit->hard_left && !edit->hard_right ) continue;
1795                         edit_dimensions(edit, x, y, w, h);
1796                         set_color(GREEN);
1797                         set_opaque();    int y1 = y+h-1;
1798                         if( edit->hard_left ) {
1799                                 ArrayList<int> xpt, ypt;
1800                                 xpt.append(x);              ypt.append(y1);
1801                                 xpt.append(x+HANDLE_W);     ypt.append(y1);
1802                                 xpt.append(x);              ypt.append(y1-HANDLE_H);
1803                                 fill_polygon(&xpt, &ypt);
1804                         }
1805                         if( edit->hard_right ) {
1806                                 ArrayList<int> xpt, ypt;   int x1 = x+w-1;
1807                                 xpt.append(x1);            ypt.append(y1);
1808                                 xpt.append(x1-HANDLE_W);   ypt.append(y1);
1809                                 xpt.append(x1);            ypt.append(y1-HANDLE_H);
1810                                 fill_polygon(&xpt, &ypt);
1811                         }
1812                 }
1813         }
1814 }
1815
1816 void TrackCanvas::draw_inout_points()
1817 {
1818 }
1819
1820
1821 void TrackCanvas::draw_drag_handle()
1822 {
1823         if(mwindow->session->current_operation == DRAG_EDITHANDLE2 ||
1824                 mwindow->session->current_operation == DRAG_PLUGINHANDLE2)
1825         {
1826 //printf("TrackCanvas::draw_drag_handle 1 %ld %ld\n", mwindow->session->drag_sample, mwindow->edl->local_session->view_start);
1827                 int64_t pixel1 = Units::round(mwindow->session->drag_position *
1828                         mwindow->edl->session->sample_rate /
1829                         mwindow->edl->local_session->zoom_sample -
1830                         mwindow->edl->local_session->view_start[pane->number]);
1831 //printf("TrackCanvas::draw_drag_handle 2 %d %jd\n", pane->number, pixel1);
1832                 set_color(!snapped ? GREEN : (snapped=0, YELLOW));
1833                 set_inverse();
1834 //printf("TrackCanvas::draw_drag_handle 3\n");
1835                 draw_line(pixel1, 0, pixel1, get_h());
1836                 set_opaque();
1837 //printf("TrackCanvas::draw_drag_handle 4\n");
1838         }
1839 }
1840
1841
1842 void TrackCanvas::draw_transitions()
1843 {
1844         int64_t x, y, w, h;
1845
1846 //      if(!mwindow->edl->session->show_assets) return;
1847
1848         for(Track *track = mwindow->edl->tracks->first; track; track = track->next) {
1849                 for(Edit *edit = track->edits->first; edit; edit = edit->next) {
1850                         if(!edit->transition) continue;
1851                         edit_dimensions(edit, x, y, w, h);
1852                         int strip_x = x, strip_y = y;
1853                         if(mwindow->edl->session->show_titles)
1854                                 strip_y += mwindow->theme->get_image("title_bg_data")->get_h();
1855                         get_transition_coords(x, y, w, h);
1856                         int strip_w = Units::round(edit->track->from_units(edit->transition->length) *
1857                                 mwindow->edl->session->sample_rate / mwindow->edl->local_session->zoom_sample);
1858                         if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
1859                                 MWindowGUI::visible(y, y + h, 0, get_h()) )
1860                         {
1861                                 PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
1862                                                 track->data_type);
1863                                 if( !server ) continue;
1864                                 VFrame *picon = server->get_picon();
1865                                 if( !picon ) continue;
1866                                 draw_vframe(picon, x, y, w, h, 0, 0, picon->get_w(), picon->get_h());
1867                         }
1868                         if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
1869                                 MWindowGUI::visible(strip_y, strip_y + h, 0, get_h()))
1870                         {
1871                                 int x = strip_x, w = strip_w;
1872                                 if(x < 0)
1873                                 {
1874                                         w -= -x;
1875                                         x = 0;
1876                                 }
1877                                 if(w + x > get_w()) w -= (w + x) - get_w();
1878
1879                                 draw_3segmenth( x, strip_y, w, strip_x, strip_w,
1880                                         mwindow->theme->get_image("plugin_bg_data"), 0);
1881                         }
1882                 }
1883         }
1884 }
1885
1886 void TrackCanvas::draw_loop_points()
1887 {
1888 //printf("TrackCanvas::draw_loop_points 1\n");
1889         if(mwindow->edl->local_session->loop_playback)
1890         {
1891 //printf("TrackCanvas::draw_loop_points 2\n");
1892                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
1893                         mwindow->edl->session->sample_rate /
1894                         mwindow->edl->local_session->zoom_sample -
1895                         mwindow->edl->local_session->view_start[pane->number]);
1896 //printf("TrackCanvas::draw_loop_points 3\n");
1897
1898                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1899                 {
1900                         set_color(GREEN);
1901                         draw_line(x, 0, x, get_h());
1902                 }
1903 //printf("TrackCanvas::draw_loop_points 4\n");
1904
1905                 x = Units::round(mwindow->edl->local_session->loop_end *
1906                         mwindow->edl->session->sample_rate /
1907                         mwindow->edl->local_session->zoom_sample -
1908                         mwindow->edl->local_session->view_start[pane->number]);
1909 //printf("TrackCanvas::draw_loop_points 5\n");
1910
1911                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1912                 {
1913                         set_color(GREEN);
1914                         draw_line(x, 0, x, get_h());
1915                 }
1916 //printf("TrackCanvas::draw_loop_points 6\n");
1917         }
1918 //printf("TrackCanvas::draw_loop_points 7\n");
1919 }
1920
1921 void TrackCanvas::draw_brender_range()
1922 {
1923         if( !mwindow->preferences->use_brender || !mwindow->brender_active ) return;
1924         if( mwindow->edl->session->brender_start >= mwindow->edl->session->brender_end ) return;
1925         if( mwindow->edl->session->brender_end > 0 )
1926         {
1927                 int64_t x1 = Units::round(mwindow->edl->session->brender_start *
1928                         mwindow->edl->session->sample_rate /
1929                         mwindow->edl->local_session->zoom_sample -
1930                         mwindow->edl->local_session->view_start[pane->number]);
1931                 if(MWindowGUI::visible(x1, x1 + 1, 0, get_w()))
1932                 {
1933                         set_color(RED);
1934                         draw_line(x1, 0, x1, get_h());
1935                 }
1936                 int64_t x2 = Units::round(mwindow->edl->session->brender_end *
1937                         mwindow->edl->session->sample_rate /
1938                         mwindow->edl->local_session->zoom_sample -
1939                         mwindow->edl->local_session->view_start[pane->number]);
1940
1941                 if(MWindowGUI::visible(x2, x2 + 1, 0, get_w()))
1942                 {
1943                         set_color(RED);
1944                         draw_line(x2, 0, x2, get_h());
1945                 }
1946         }
1947 }
1948
1949 // The operations which correspond to each automation type
1950 int TrackCanvas::auto_operations[AUTOMATION_TOTAL] =
1951 {
1952         DRAG_MUTE,
1953         DRAG_CAMERA_X,
1954         DRAG_CAMERA_Y,
1955         DRAG_CAMERA_Z,
1956         DRAG_PROJECTOR_X,
1957         DRAG_PROJECTOR_Y,
1958         DRAG_PROJECTOR_Z,
1959         DRAG_FADE,
1960         DRAG_PAN,
1961         DRAG_MODE,
1962         DRAG_MASK,
1963         DRAG_SPEED
1964 };
1965
1966 // The buttonpress operations, so nothing changes unless the mouse moves
1967 // a certain amount.  This allows the keyframe to be used to position the
1968 // insertion point without moving itself.
1969 static int pre_auto_operations[AUTOMATION_TOTAL] =
1970 {
1971         DRAG_MUTE,
1972         DRAG_CAMERA_X,
1973         DRAG_CAMERA_Y,
1974         DRAG_CAMERA_Z,
1975         DRAG_PROJECTOR_X,
1976         DRAG_PROJECTOR_Y,
1977         DRAG_PROJECTOR_Z,
1978         DRAG_FADE,
1979         DRAG_PAN_PRE,
1980         DRAG_MODE_PRE,
1981         DRAG_MASK_PRE,
1982         DRAG_SPEED
1983 };
1984
1985
1986 int TrackCanvas::do_keyframes(int cursor_x,
1987         int cursor_y,
1988         int draw,
1989         int buttonpress,
1990         int &new_cursor,
1991         int &update_cursor,
1992         int &rerender)
1993 {
1994 // Note: button 3 (right mouse button) is not eaten to allow
1995 // track context menu to appear
1996         int result = 0;
1997         EDLSession *session = mwindow->edl->session;
1998
1999
2000
2001         static BC_Pixmap *auto_pixmaps[AUTOMATION_TOTAL] =
2002         {
2003                 0, 0, 0, 0, 0, 0, 0, 0,
2004                 pankeyframe_pixmap,
2005                 modekeyframe_pixmap,
2006                 maskkeyframe_pixmap,
2007         };
2008
2009
2010
2011         for(Track *track = mwindow->edl->tracks->first;
2012                 track && !result;
2013                 track = track->next) {
2014                 Auto *auto_keyframe = 0;
2015                 Automation *automation = track->automation;
2016
2017
2018 // Handle keyframes in reverse drawing order if a button press
2019                 int start = 0;
2020                 int end = AUTOMATION_TOTAL;
2021                 int step = 1;
2022                 if(buttonpress)
2023                 {
2024                         start = AUTOMATION_TOTAL - 1;
2025                         end = -1;
2026                         step = -1;
2027                 }
2028                 for(int i = start; i != end && !result; i += step)
2029                 {
2030 // Event not trapped and automation visible
2031                         Autos *autos = automation->autos[i];
2032                         if(!result && session->auto_conf->autos[i] && autos) {
2033                                 switch(i) {
2034                                 case AUTOMATION_MODE:
2035                                 case AUTOMATION_PAN:
2036                                 case AUTOMATION_MASK:
2037                                         result = do_autos(track, automation->autos[i],
2038                                                 cursor_x, cursor_y, draw,
2039                                                 buttonpress, auto_pixmaps[i],
2040                                                 auto_keyframe, rerender);
2041                                                 break;
2042
2043                                 default: {
2044                                         switch(autos->get_type()) {
2045                                         case Autos::AUTOMATION_TYPE_FLOAT: {
2046                                                 Automation automation(0, track);
2047                                                 int grouptype = automation.autogrouptype(i, track);
2048                                                 if( buttonpress && i == AUTOMATION_SPEED ) {
2049                                                         mwindow->speed_before();
2050                                                 }
2051
2052                                                 if(draw) // Do dropshadow
2053                                                         result = do_float_autos(track, autos,
2054                                                                 cursor_x, cursor_y, draw,
2055                                                                 buttonpress, 1, 1, MDGREY,
2056                                                                 auto_keyframe, grouptype);
2057                                                 result = do_float_autos(track, autos,
2058                                                         cursor_x, cursor_y, draw, buttonpress,
2059                                                         0, 0, GWindowGUI::auto_colors[i],
2060                                                         auto_keyframe, grouptype);
2061
2062                                                 if( !result && buttonpress && i == AUTOMATION_SPEED )
2063                                                         mwindow->speed_after(-1);
2064                                                 int current_grouptype = mwindow->edl->local_session->zoombar_showautotype;
2065                                                 if( result && buttonpress && grouptype != current_grouptype ) {
2066                                                         mwindow->edl->local_session->zoombar_showautotype = grouptype;
2067                                                         mwindow->gui->zoombar->update_autozoom();
2068                                                 }
2069                                                 break; }
2070
2071                                         case Autos::AUTOMATION_TYPE_INT: {
2072                                                 if(draw) // Do dropshadow
2073                                                         result = do_int_autos(track, autos,
2074                                                                 cursor_x, cursor_y, draw,
2075                                                                 buttonpress, 1, 1, MDGREY,
2076                                                                 auto_keyframe);
2077                                                 result = do_int_autos(track, autos,
2078                                                         cursor_x, cursor_y, draw, buttonpress,
2079                                                         0, 0, GWindowGUI::auto_colors[i],
2080                                                         auto_keyframe);
2081                                                 break; }
2082                                         }
2083                                         break; }
2084                                 }
2085
2086                                 if(result)
2087                                 {
2088                                         if(mwindow->session->current_operation == auto_operations[i])
2089                                                 rerender = 1;
2090
2091 // printf("TrackCanvas::do_keyframes %d %d %d\n",
2092 // __LINE__,
2093 // mwindow->session->current_operation,
2094 // auto_operations[i]);
2095                                         if(buttonpress)
2096                                         {
2097                                                 if (buttonpress != 3)
2098                                                 {
2099                                                         if(i == AUTOMATION_FADE || i == AUTOMATION_SPEED)
2100                                                                 synchronize_autos(0,
2101                                                                         track,
2102                                                                         (FloatAuto*)mwindow->session->drag_auto,
2103                                                                         1);
2104                                                         mwindow->session->current_operation = pre_auto_operations[i];
2105                                                         update_drag_caption();
2106                                                         rerender = 1;
2107                                                 }
2108                                                 else if( auto_keyframe )
2109                                                 {
2110                                                         gui->keyframe_menu->update(automation,
2111                                                                 autos,
2112                                                                 auto_keyframe);
2113                                                         gui->keyframe_menu->activate_menu();
2114                                                         rerender = 1; // the position changes
2115                                                 }
2116                                                 else if( autos )
2117                                                 {
2118                                                         gui->keyframe_hide->update(autos);
2119                                                         gui->keyframe_hide->activate_menu();
2120                                                         rerender = 1; // the position changes
2121                                                 }
2122                                                 if(buttonpress == 1 && ctrl_down() &&
2123                                                    AUTOMATION_TYPE_FLOAT == autos->get_type())
2124                                                         rerender = 1; // special case: curve mode changed
2125                                         }
2126                                 }
2127                         }
2128                 }
2129
2130                 if(!result &&
2131                         session->auto_conf->plugins /* &&
2132                         mwindow->edl->session->show_assets */) {
2133                         Plugin *plugin;
2134                         KeyFrame *keyframe;
2135                         result = do_plugin_autos(track, cursor_x, cursor_y,
2136                                 draw, buttonpress, plugin, keyframe);
2137                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY) {
2138                                 rerender = 1;
2139                         }
2140                         if(result && (buttonpress == 1)) {
2141                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2142                                 update_drag_caption();
2143                                 rerender = 1;
2144                         }
2145                         else if (result && (buttonpress == 3)) {
2146                                 gui->keyframe_menu->update(plugin, keyframe);
2147                                 gui->keyframe_menu->activate_menu();
2148                                 rerender = 1; // the position changes
2149                         }
2150                 }
2151         }
2152
2153 // Final pass to trap event
2154         for(int i = 0; i < AUTOMATION_TOTAL; i++) {
2155                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2156                         mwindow->session->current_operation == auto_operations[i]) {
2157                         result = 1;
2158                         break;
2159                 }
2160         }
2161
2162         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2163                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE) {
2164                 result = 1;
2165         }
2166
2167         update_cursor = 1;
2168         if(result) {
2169                 new_cursor = UPRIGHT_ARROW_CURSOR;
2170         }
2171
2172
2173         return result;
2174 }
2175
2176 void TrackCanvas::draw_keyframe_reticle()
2177 {
2178         int keyframe_hairline = mwindow->preferences->keyframe_reticle;
2179         if( keyframe_hairline == HAIRLINE_NEVER ) return;
2180
2181         int current_op = mwindow->session->current_operation, dragging = 0;
2182         for( int i=0; !dragging && i<AUTOMATION_TOTAL; ++i )
2183                 if( current_op == auto_operations[i] ) dragging = 1;
2184
2185         if( keyframe_hairline == HAIRLINE_DRAGGING && dragging ) {
2186                 if( mwindow->session->drag_auto && get_buttonpress() == 1 ) {
2187                         draw_hairline(mwindow->session->drag_auto, RED);
2188                         return;
2189                 }
2190         }
2191
2192         if( keyframe_hairline == HAIRLINE_ALWAYS || ( get_buttonpress() == 2 &&
2193             keyframe_hairline == HAIRLINE_DRAGGING && dragging ) ) {
2194                 for( Track *track = mwindow->edl->tracks->first; track;
2195                      track=track->next ) {
2196                         Automation *automation = track->automation;
2197                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
2198                                 if( !mwindow->edl->session->auto_conf->autos[i] ) continue;
2199                                 // automation visible
2200                                 Autos *autos = automation->autos[i];
2201                                 if( !autos ) continue;
2202                                 for( Auto *auto_keyframe=autos->first; auto_keyframe;
2203                                      auto_keyframe = auto_keyframe->next ) {
2204                                         draw_hairline(auto_keyframe, BLUE);
2205                                 }
2206                         }
2207
2208                         if( dragging && mwindow->session->drag_auto ) {
2209                                 draw_hairline(mwindow->session->drag_auto, RED);
2210                         }
2211                 }
2212         }
2213 }
2214
2215 void TrackCanvas::draw_auto(Auto *current,
2216         int x,
2217         int y,
2218         int center_pixel,
2219         int zoom_track)
2220 {
2221         int x1, y1, x2, y2;
2222
2223         x1 = x - HANDLE_W / 2;
2224         x2 = x + HANDLE_W / 2;
2225         y1 = center_pixel + y - HANDLE_W / 2;
2226         y2 = center_pixel + y + HANDLE_W / 2;
2227
2228         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2229         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2230
2231         draw_box(x1, y1, x2 - x1, y2 - y1);
2232 }
2233
2234
2235 // This draws lines for bezier in & out controls
2236 void TrackCanvas::draw_cropped_line(int x1,
2237         int y1,
2238         int x2,
2239         int y2,
2240         int min_y,
2241         int max_y)
2242 {
2243
2244
2245 // Don't care about x since it is clipped by the window.
2246 // Put y coords in ascending order
2247         if(y2 < y1) {
2248                 y2 ^= y1; y1 ^= y2; y2 ^= y1;
2249                 x2 ^= x1; x1 ^= x2; x2 ^= x1;
2250         }
2251
2252
2253
2254         double slope = (double)(x2 - x1) / (y2 - y1);
2255 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2256         if(y1 < min_y) {
2257                 x1 = (int)(x1 + (min_y - y1) * slope);
2258                 y1 = min_y;
2259         }
2260         else if(y1 >= max_y) {
2261                 x1 = (int)(x1 + (max_y - 1 - y1) * slope);
2262                 y1 = max_y - 1;
2263         }
2264
2265         if(y2 >= max_y) {
2266                 x2 = (int)(x2 + (max_y - 1 - y2) * slope);
2267                 y2 = max_y - 1;
2268         }
2269         else if(y2 < min_y) {
2270                 x2 = (int)(x2 + (min_y - y2) * slope);
2271                 y1 = min_y;
2272         }
2273
2274
2275 //printf("TrackCanvas::draw_cropped_line %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
2276         if( y1 >= min_y && y1 < max_y &&
2277             y2 >= min_y && y2 < max_y )
2278                 draw_line(x1, y1, x2, y2);
2279 }
2280
2281
2282 void TrackCanvas::draw_floatauto(FloatAuto *current,
2283         int x,
2284         int y,
2285         int in_x,
2286         int in_y,
2287         int out_x,
2288         int out_y,
2289         int center_pixel,
2290         int zoom_track,
2291         int color)
2292 {
2293         int x1 = x - HANDLE_W / 2; // Center
2294         int x2 = x + HANDLE_W / 2;
2295         int y1 = center_pixel + y - HANDLE_H / 2;
2296         int y2 = center_pixel + y + HANDLE_H / 2;
2297         int ymin = center_pixel - zoom_track / 2;
2298         int ymax = center_pixel + zoom_track / 2;
2299         CLAMP(y1, ymin, ymax);
2300         CLAMP(y2, ymin, ymax);
2301
2302         if(y2 - 1 > y1)
2303         {
2304                 set_color(BLACK);
2305                 draw_box(x1 + 1, y1 + 1, x2 - x1, y2 - y1);
2306                 set_color(color);
2307                 draw_box(x1, y1, x2 - x1, y2 - y1);
2308         }
2309
2310 // show bezier control points (only) if this
2311 // floatauto doesn't adjust it's tangents automatically
2312         if(current->curve_mode != FloatAuto::FREE &&
2313            current->curve_mode != FloatAuto::TFREE)
2314                 return;
2315
2316         if(in_x != x)
2317                 draw_floatauto_ctrlpoint(x, y, in_x, in_y, center_pixel, zoom_track, color);
2318         if(out_x != x)
2319                 draw_floatauto_ctrlpoint(x, y, out_x, out_y, center_pixel, zoom_track, color);
2320 }
2321
2322 inline int quantize(float f) { return (int)floor(f + 0.5); }
2323
2324 inline void TrackCanvas::draw_floatauto_ctrlpoint(
2325         int x, int y, int cp_x, int cp_y, int center_pixel,
2326         int zoom_track, int color)
2327 // draw the tangent and a handle for given bézier ctrl point
2328 {
2329         bool handle_visible = (abs(cp_y) <= zoom_track / 2);
2330
2331         float slope = (float)(cp_y - y)/(cp_x - x);
2332         CLAMP(cp_y, -zoom_track / 2, zoom_track / 2);
2333         if(slope != 0)
2334                 cp_x = x + quantize((cp_y - y) / slope);
2335
2336         y    += center_pixel;
2337         cp_y += center_pixel;
2338
2339         // drawing the tangent as a dashed line...
2340         int const dash = HANDLE_W;
2341         int const gap  = HANDLE_W / 2;
2342         float sx = 3 * (cp_x - x) / 4.;
2343         float ex = 0;
2344
2345         // q is the x displacement for a unit line of slope
2346         float q = (sx > 0 ? 1 : -1) / sqrt(1 + slope * slope);
2347
2348         float dist = 1/q * sx;
2349         if( dist > dash )
2350                 ex = sx - q * dash;
2351
2352         set_color(color);
2353         do {
2354                 float sy = slope * sx, ey = slope * ex;
2355                 draw_line(quantize(sx + x), quantize(sy + y), quantize(ex + x), quantize(ey + y));
2356                 sx = ex - q * gap;
2357                 ex = sx - q * dash;
2358         } while(q*ex > 0);
2359
2360         if(handle_visible)
2361         {
2362                 int r = HANDLE_W / 2;
2363                 int cp_x1 = cp_x - r;
2364                 int cp_y1 = cp_y - r;
2365                 set_color(BLACK);
2366                 draw_disc(cp_x1, cp_y1, 2 * r, 2 * r);
2367                 set_color(color);
2368                 draw_circle(cp_x1, cp_y1, 2 * r, 2 * r);
2369         }
2370 }
2371
2372
2373 int TrackCanvas::test_auto(Auto *current,
2374         int x, int y, int center_pixel, int zoom_track,
2375         int cursor_x, int cursor_y, int buttonpress)
2376 {
2377         int x1, y1, x2, y2;
2378         int result = 0;
2379
2380         x1 = x - HANDLE_W / 2;
2381         x2 = x + HANDLE_W / 2;
2382         y1 = center_pixel + y - HANDLE_H / 2;
2383         y2 = center_pixel + y + HANDLE_H / 2;
2384         int ymin = center_pixel - zoom_track / 2;
2385         int ymax = center_pixel + zoom_track / 2;
2386         CLAMP(y1, ymin, ymax);
2387         CLAMP(y2, ymin, ymax);
2388
2389         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2390         {
2391                 if(buttonpress && buttonpress != 3)
2392                 {
2393                         mwindow->session->drag_auto = current;
2394                         mwindow->session->drag_start_percentage = (float)((IntAuto*)current)->value;
2395                         // Toggle Autos don't respond to vertical zoom, they always show up
2396                         // with "on" == 100% == line on top
2397                         mwindow->session->drag_start_position = current->position;
2398                         mwindow->session->drag_origin_x = cursor_x;
2399                         mwindow->session->drag_origin_y = cursor_y;
2400                 }
2401                 result = 1;
2402         }
2403
2404         if(buttonpress && buttonpress != 3 && result)
2405         {
2406 //printf("TrackCanvas::test_auto %d\n", __LINE__);
2407                 mwindow->undo->update_undo_before();
2408         }
2409
2410         return result;
2411 }
2412
2413 // some Helpers for test_floatauto(..)
2414 // and for dragging the tangents/ctrl points
2415 inline float test_curve_line( int x0, int y0, int ctrl_x, int ctrl_y,
2416         float cursor_x, float cursor_y)
2417 {
2418 // Control point switched off?
2419         if( x0 == ctrl_x ) return 0.0;
2420         double x1 = ctrl_x-x0, y1 = ctrl_y-y0;
2421         double x = cursor_x-x0, y = cursor_y-y0;
2422 // wrong side of ctrl handle
2423         if( x*x1 < 0 ) return 0.0;
2424 // outside handle radius
2425         if( (x*x + y*y) > (x1*x1 + y1*y1) ) return 0;
2426         double xx1 = x1*x1, yy1 = y1*y1;
2427         double xx = x*x, yy = y*y;
2428 // distance squared from cursor to cursor projected to line from ctrl to handle
2429 //   along a line perpendicular to line from ctrl to handle (closest approach)
2430 // (x**2*y1**2 - 2*x*x1*y*y1 + x1**2*y**2)/(x1**2 + y1**2)
2431         double dist2 = (xx*yy1 - 2*x*x1*y*y1 + xx1*yy)/(xx1 + yy1);
2432         return dist2 < (HANDLE_W*HANDLE_W)/4. ? x/x1 : 0.;
2433 }
2434
2435
2436 inline
2437 float levered_position(float position, float ref_pos)
2438 {
2439         if( 1e-6 > fabs(ref_pos) || isnan(ref_pos))
2440                 return 0.0;
2441         return ref_pos / position;
2442 }
2443
2444
2445 float TrackCanvas::value_to_percentage(float auto_value, int autogrouptype)
2446 // transforms automation value into current display coords,
2447 // dependant on current automation display range for the given kind of automation
2448 {
2449         if(!mwindow || !mwindow->edl) return 0;
2450         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2451         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2452         float automation_range = automation_max - automation_min;
2453         if( 0 >= automation_range || isnan(auto_value) || isinf(auto_value) )
2454                 return 0;
2455         return (auto_value - automation_min) / automation_range;
2456 }
2457
2458
2459
2460 int TrackCanvas::test_floatauto(FloatAuto *current, int x, int y, int in_x,
2461         int in_y, int out_x, int out_y, int center_pixel, int zoom_track,
2462         int cursor_x, int cursor_y, int buttonpress, int autogrouptype)
2463 {
2464         int result = 0;
2465
2466         int x1 = x - HANDLE_W / 2;
2467         int x2 = x + HANDLE_W / 2;
2468         int y1 = center_pixel + y - HANDLE_W / 2;
2469         int y2 = center_pixel + y + HANDLE_W / 2;
2470         int ymin = center_pixel - zoom_track / 2;
2471         int ymax = center_pixel + zoom_track / 2;
2472         CLAMP(y1, ymin, ymax);
2473         CLAMP(y2, ymin, ymax);
2474
2475         int in_x1 = in_x - HANDLE_W / 2;
2476         int in_x2 = in_x + HANDLE_W / 2;
2477         int in_y1 = center_pixel + in_y - HANDLE_W / 2;
2478         int in_y2 = center_pixel + in_y + HANDLE_W / 2;
2479         CLAMP(in_y1, ymin, ymax);
2480         CLAMP(in_y2, ymin, ymax);
2481
2482         int out_x1 = out_x - HANDLE_W / 2;
2483         int out_x2 = out_x + HANDLE_W / 2;
2484         int out_y1 = center_pixel + out_y - HANDLE_W / 2;
2485         int out_y2 = center_pixel + out_y + HANDLE_W / 2;
2486         CLAMP(out_y1, ymin, ymax);
2487         CLAMP(out_y2, ymin, ymax);
2488
2489
2490 //printf("TrackCanvas::test_floatauto %d %d %d %d %d %d\n", cursor_x, cursor_y, x1, x2, y1, y2);
2491 // buttonpress could be the start of a drag operation
2492 #define INIT_DRAG(POS,VAL) \
2493                 mwindow->session->drag_auto = current;      \
2494                 mwindow->session->drag_origin_x = cursor_x;  \
2495                 mwindow->session->drag_origin_y = cursor_y;   \
2496                 mwindow->session->drag_start_position = (POS); \
2497                 mwindow->session->drag_start_percentage = (VAL);
2498
2499 #define WITHIN(X1,X2,Y1,Y2) (cursor_x >=(X1) && cursor_x <(X2) && cursor_y >=(Y1) && cursor_y <(Y2) )
2500
2501
2502 // without modifier we are manipulating the automation node
2503 // with ALT it's about dragging only the value of the node
2504 // with SHIFT the value snaps to the value of neighbouring nodes
2505 // CTRL indicates we are rather manipulating the tangent(s) of the node
2506
2507         if(!ctrl_down())
2508         {
2509                 if( WITHIN(x1,x2,y1,y2))
2510                 {       // cursor hits node
2511                         result = 1;
2512
2513                         if(buttonpress && (buttonpress != 3))
2514                         {
2515                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2516                                 mwindow->session->drag_handle = 0;
2517                 }       }
2518         }
2519         else // ctrl_down()
2520         {
2521                 if( WITHIN(x1,x2,y1,y2))
2522                 {
2523                         result = 1;
2524                         if(buttonpress && (buttonpress != 3))
2525                         {
2526                                 // could be ctrl-click or ctrl-drag
2527                                 // click would cycle through tangent modes
2528                                 ((FloatAuto*)current)->toggle_curve_mode();
2529
2530                                 // drag will start dragging the tangent, if applicable
2531                                 INIT_DRAG(current->position, value_to_percentage(current->get_value(), autogrouptype))
2532                                 mwindow->session->drag_handle = 0;
2533                         }
2534                 }
2535
2536                 float lever = 0.0; // we use the tangent as a draggable lever. 1.0 is at the ctrl point
2537
2538 // Test in control
2539                 if( in_x != x && current->position > 0 &&
2540                         (FloatAuto::FREE == current->curve_mode ||
2541                          FloatAuto::TFREE == current->curve_mode))
2542 // act on in control handle only if
2543 // tangent is significant and is editable (not automatically choosen)
2544                 {
2545                         lever = test_curve_line(x, y, in_x, in_y, cursor_x, cursor_y-center_pixel);
2546  // either cursor in ctrl-point handle or cursor on tangent line
2547                         if( WITHIN(in_x1,in_x2,in_y1,in_y2) || lever > 0.0 ) {
2548                                 result = 1;
2549                                 if(buttonpress && (buttonpress != 3)) {
2550                                         if(lever == 0.0) lever=1.0; // we entered by dragging the handle...
2551 //                                      lever = 1.0;
2552                                         mwindow->session->drag_handle = 1;
2553                                         float new_invalue = current->get_value() + lever * current->get_control_in_value();
2554                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_in_position()),
2555                                                   value_to_percentage(new_invalue, autogrouptype))
2556                                 }
2557                         }
2558                 }
2559
2560 // Test out control
2561                 if(out_x != x &&
2562                         (FloatAuto::FREE == current->curve_mode ||
2563                          FloatAuto::TFREE == current->curve_mode))
2564 // act on out control only if tangent is significant and is editable
2565                 {
2566                         lever = test_curve_line(x, y, out_x, out_y, cursor_x, cursor_y-center_pixel);
2567                         if(WITHIN(out_x1,out_x2,out_y1,out_y2) || lever > 0.0 ) {
2568                                 result = 1;
2569                                 if(buttonpress && (buttonpress != 3)) {
2570                                         if(lever == 0.0) lever=1.0;
2571 //                                      lever = 1.0;
2572                                         mwindow->session->drag_handle = 2;
2573                                         float new_outvalue = current->get_value() + lever * current->get_control_out_value();
2574                                         INIT_DRAG(current->position + (int64_t)(lever * current->get_control_out_position()),
2575                                                   value_to_percentage(new_outvalue, autogrouptype))
2576                                 }
2577                         }
2578                 }
2579         } // end ctrl_down()
2580
2581 #undef WITHIN
2582 #undef INIT_DRAG
2583
2584 // if(buttonpress)
2585 // 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",
2586 // mwindow->session->drag_handle,
2587 // ctrl_down(),
2588 // cursor_x,
2589 // cursor_y,
2590 // x1, x2, y1, y2);
2591         if(buttonpress && (buttonpress != 3) && result)
2592         {
2593                 mwindow->undo->update_undo_before();
2594         }
2595
2596         return result;
2597 }
2598
2599
2600 // Get the float value & y for position x on the canvas
2601 #define X_TO_FLOATLINE(x) \
2602         int64_t position1 = (int64_t)(unit_start + x * zoom_units); \
2603         int64_t position2 = (int64_t)(unit_start + x * zoom_units) + 1; \
2604 /* Call by reference fails for some reason here */ \
2605         float value1 = autos->get_value(position1, PLAY_FORWARD, previous1, next1); \
2606         float value2 = autos->get_value(position2, PLAY_FORWARD, previous1, next1); \
2607         double position = unit_start + x * zoom_units; \
2608         double value = 0; \
2609         if(position2 > position1) \
2610         { \
2611                 value = value1 + \
2612                         (value2 - value1) * \
2613                         (position - position1) / \
2614                         (position2 - position1); \
2615         } \
2616         else \
2617         { \
2618                 value = value1; \
2619         } \
2620         AUTOMATIONCLAMPS(value, autogrouptype); \
2621         int y = center_pixel + \
2622                 (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2623
2624
2625 void TrackCanvas::draw_floatline(int center_pixel,
2626         FloatAuto *previous, FloatAuto *next, FloatAutos *autos,
2627         double unit_start, double zoom_units, double yscale,
2628         int x1, int y1, int x2, int y2,
2629         int color, int autogrouptype)
2630 {
2631 // Solve bezier equation for either every pixel or a certain large number of
2632 // points.
2633
2634 // Not using slope intercept
2635         x1 = MAX(0, x1);
2636         int prev_y = y1 + center_pixel;
2637
2638
2639 // Call by reference fails for some reason here
2640         FloatAuto *previous1 = previous, *next1 = next;
2641         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2642         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2643         float automation_range = automation_max - automation_min;
2644
2645         for(int x = x1; x < x2; x++)
2646         {
2647 // Interpolate value between frames
2648                 X_TO_FLOATLINE(x)
2649
2650                 if(/* x > x1 && */
2651                         y >= center_pixel - yscale / 2 &&
2652                         y < center_pixel + yscale / 2 - 1)
2653                 {
2654 // printf("TrackCanvas::draw_floatline y=%d min=%d max=%d\n",
2655 // y,
2656 // (int)(center_pixel - yscale / 2),
2657 // (int)(center_pixel + yscale / 2 - 1));
2658
2659 //printf("draw_line(%d,%d,  %d,%d)\n", x - 1, prev_y  , x, y);
2660                         draw_line(x - 1, prev_y  , x, y   );
2661                 }
2662                 prev_y = y;
2663         }
2664 }
2665
2666
2667
2668
2669
2670 int TrackCanvas::test_floatline(int center_pixel,
2671                 FloatAutos *autos,
2672                 double unit_start,
2673                 double zoom_units,
2674                 double yscale,
2675                 int x1,
2676                 int x2,
2677                 int cursor_x,
2678                 int cursor_y,
2679                 int buttonpress,
2680                 int autogrouptype)
2681 {
2682         int result = 0;
2683
2684
2685         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2686         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2687         float automation_range = automation_max - automation_min;
2688         FloatAuto *previous1 = 0, *next1 = 0;
2689         X_TO_FLOATLINE(cursor_x);
2690
2691         if(cursor_x >= x1 &&
2692                 cursor_x < x2 &&
2693                 cursor_y >= y - HANDLE_W / 2 &&
2694                 cursor_y < y + HANDLE_W / 2 &&
2695                 !ctrl_down())
2696         {
2697                 result = 1;
2698
2699 // Menu
2700                 if(buttonpress == 3)
2701                 {
2702                 }
2703                 else
2704 // Create keyframe
2705                 if(buttonpress)
2706                 {
2707                         Auto *current;
2708                         mwindow->undo->update_undo_before();
2709                         double position = autos->track->from_units(position1);
2710                         position = mwindow->edl->align_to_frame(position, 0);
2711                         int64_t new_position = autos->track->to_units(position,0);
2712                         current = mwindow->session->drag_auto = autos->insert_auto(new_position);
2713                         ((FloatAuto*)current)->set_value(value);
2714                         mwindow->session->drag_start_percentage = value_to_percentage(value, autogrouptype);
2715                         mwindow->session->drag_start_position = current->position;
2716                         mwindow->session->drag_origin_x = cursor_x;
2717                         mwindow->session->drag_origin_y = cursor_y;
2718                         mwindow->session->drag_handle = 0;
2719                 }
2720         }
2721
2722
2723         return result;
2724 }
2725
2726
2727 void TrackCanvas::synchronize_autos(float change,
2728                 Track *skip, FloatAuto *fauto, int fill_gangs)
2729 {
2730 // Handles the special case of modifying a fadeauto
2731 // when there are ganged faders on several tracks
2732 // (skip and fauto may be NULL if fill_gangs==-1)
2733
2734         if( fill_gangs > 0 && skip->gang ) {
2735                 double position = skip->from_units(fauto->position);
2736                 int autoidx = fauto->autos->autoidx;
2737
2738                 for(Track *current = mwindow->edl->tracks->first; current; current = NEXT) {
2739                         if( (current->data_type == skip->data_type || get_double_click()) &&
2740                             current->gang && current->record && current != skip ) {
2741                                 int64_t current_position = current->to_units(position, 1);
2742                                 FloatAutos *fade_autos = (FloatAutos*)current->automation->autos[autoidx];
2743                                 float auto_min = mwindow->edl->local_session->automation_mins[fade_autos->autogrouptype];
2744                                 float auto_max = mwindow->edl->local_session->automation_maxs[fade_autos->autogrouptype];
2745                                 FloatAuto *previous = 0, *next = 0;
2746                                 FloatAuto *keyframe = (FloatAuto*)fade_autos->get_auto_at_position(current_position);
2747                                 if( !keyframe ) {
2748 // create keyframe on neighbouring track at the point in time given by fauto
2749                                         float init_value = fade_autos->get_value(current_position, PLAY_FORWARD, previous, next);
2750                                         float new_value = init_value + change;
2751                                         CLAMP(new_value, auto_min, auto_max);
2752                                         keyframe = (FloatAuto*)fade_autos->insert_auto(current_position);
2753                                         keyframe->set_value(new_value);
2754                                 }
2755                                 else {
2756 // keyframe exists, just change it
2757                                         float new_value = keyframe->get_value() + change;
2758                                         CLAMP(new_value, auto_min, auto_max);
2759                                         keyframe->adjust_to_new_coordinates(current_position, new_value);
2760 // need to (re)set the position, as the existing node could be on a "equivalent" position (within half a frame)
2761                                 }
2762
2763                                 mwindow->session->drag_auto_gang->append((Auto *)keyframe);
2764                         }
2765                 }
2766         }
2767         else if( !fill_gangs ) {
2768                 double position = skip->from_units(fauto->position);
2769 // Move the gangs
2770                 for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
2771                         FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2772                         int64_t keyframe_position = keyframe->autos->track->to_units(position, 1);
2773                         float new_value = keyframe->get_value() + change;
2774                         CLAMP(new_value,
2775                               mwindow->edl->local_session->automation_mins[keyframe->autos->autogrouptype],
2776                               mwindow->edl->local_session->automation_maxs[keyframe->autos->autogrouptype]);
2777                         keyframe->adjust_to_new_coordinates(keyframe_position, new_value);
2778                 }
2779
2780         }
2781         else {
2782 // remove the gangs
2783                 for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++) {
2784                         FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2785                         keyframe->autos->remove_nonsequential(
2786                                         keyframe);
2787                 }
2788                 mwindow->session->drag_auto_gang->remove_all();
2789         }
2790 }
2791
2792
2793 void TrackCanvas::draw_toggleline(int center_pixel,
2794         int x1,
2795         int y1,
2796         int x2,
2797         int y2)
2798 {
2799         draw_line(x1, center_pixel + y1, x2, center_pixel + y1);
2800
2801         if(y2 != y1)
2802         {
2803                 draw_line(x2, center_pixel + y1, x2, center_pixel + y2);
2804         }
2805 }
2806
2807 int TrackCanvas::test_toggleline(Autos *autos,
2808         int center_pixel,
2809         int x1,
2810         int y1,
2811         int x2,
2812         int y2,
2813         int cursor_x,
2814         int cursor_y,
2815         int buttonpress)
2816 {
2817         int result = 0;
2818         if(cursor_x >= x1 && cursor_x < x2)
2819         {
2820                 int miny = center_pixel + y1 - HANDLE_W / 2;
2821                 int maxy = center_pixel + y1 + HANDLE_W / 2;
2822                 if(cursor_y >= miny && cursor_y < maxy)
2823                 {
2824                         result = 1;
2825
2826 // Menu
2827                         if(buttonpress == 3)
2828                         {
2829                         }
2830                         else
2831 // Insert keyframe
2832                         if(buttonpress)
2833                         {
2834                                 Auto *current;
2835                                 double position = (double)(cursor_x +
2836                                                 mwindow->edl->local_session->view_start[pane->number]) *
2837                                         mwindow->edl->local_session->zoom_sample /
2838                                         mwindow->edl->session->sample_rate;
2839                                 int64_t unit_position = autos->track->to_units(position, 0);
2840                                 int new_value = (int)((IntAutos*)autos)->get_automation_constant(unit_position, unit_position);
2841
2842                                 mwindow->undo->update_undo_before();
2843
2844                                 current = mwindow->session->drag_auto = autos->insert_auto(unit_position);
2845                                 ((IntAuto*)current)->value = new_value;
2846                                 // Toggle Autos don't respond to vertical zoom, they always show up
2847                                 // with "on" == 100% == line on top
2848                                 mwindow->session->drag_start_percentage = (float)new_value;
2849                                 mwindow->session->drag_start_position = current->position;
2850                                 mwindow->session->drag_origin_x = cursor_x;
2851                                 mwindow->session->drag_origin_y = cursor_y;
2852                         }
2853                 }
2854         };
2855
2856         return result;
2857 }
2858
2859 void TrackCanvas::calculate_viewport(Track *track,
2860         double &view_start,   // Seconds
2861         double &unit_start,
2862         double &view_end,     // Seconds
2863         double &unit_end,
2864         double &yscale,
2865         int &center_pixel,
2866         double &zoom_sample,
2867         double &zoom_units)
2868 {
2869         view_start = (double)mwindow->edl->local_session->view_start[pane->number] *
2870                 mwindow->edl->local_session->zoom_sample /
2871                 mwindow->edl->session->sample_rate;
2872         unit_start = track->to_doubleunits(view_start);
2873         view_end = (double)(mwindow->edl->local_session->view_start[pane->number] +
2874                 get_w()) *
2875                 mwindow->edl->local_session->zoom_sample /
2876                 mwindow->edl->session->sample_rate;
2877         unit_end = track->to_doubleunits(view_end);
2878         yscale = mwindow->edl->local_session->zoom_track;
2879 //printf("TrackCanvas::calculate_viewport yscale=%.0f\n", yscale);
2880         center_pixel = (int)(track->y_pixel -
2881                         mwindow->edl->local_session->track_start[pane->number] +
2882                         yscale / 2) +
2883                 (mwindow->edl->session->show_titles ?
2884                         mwindow->theme->get_image("title_bg_data")->get_h() :
2885                         0);
2886         zoom_sample = mwindow->edl->local_session->zoom_sample;
2887
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 = (int)(track->y_pixel -
3379                         mwindow->edl->local_session->track_start[pane->number] +
3380                         mwindow->edl->local_session->zoom_track +
3381                         (i + 0.5) * mwindow->theme->get_image("plugin_bg_data")->get_h() +
3382                         (mwindow->edl->session->show_titles ? mwindow->theme->get_image("title_bg_data")->get_h() : 0));
3383
3384                 for(Plugin *plugin = (Plugin*)plugin_set->first;
3385                         plugin && !result;
3386                         plugin = (Plugin*)plugin->next)
3387                 {
3388                         for(KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
3389                                 keyframe && !result;
3390                                 keyframe = (KeyFrame*)keyframe->next)
3391                         {
3392 //printf("TrackCanvas::draw_plugin_autos 3 %d\n", keyframe->position);
3393                                 if(keyframe->position >= unit_start && keyframe->position < unit_end)
3394                                 {
3395                                         int64_t x = (int64_t)((keyframe->position - unit_start) / zoom_units);
3396                                         int y = center_pixel - keyframe_pixmap->get_h() / 2;
3397
3398 //printf("TrackCanvas::draw_plugin_autos 4 %d %d\n", x, center_pixel);
3399                                         if(!draw)
3400                                         {
3401                                                 if(cursor_x >= x && cursor_y >= y &&
3402                                                         cursor_x < x + keyframe_pixmap->get_w() &&
3403                                                         cursor_y < y + keyframe_pixmap->get_h())
3404                                                 {
3405                                                         result = 1;
3406                                                         keyframe_plugin = plugin;
3407                                                         keyframe_instance = keyframe;
3408
3409                                                         if(buttonpress)
3410                                                         {
3411                                                                 mwindow->session->drag_auto = keyframe;
3412                                                                 mwindow->session->drag_start_position = keyframe->position;
3413                                                                 mwindow->session->drag_origin_x = cursor_x;
3414                                                                 mwindow->session->drag_origin_y = cursor_y;
3415
3416                                                                 double position = track->from_units(keyframe->position);
3417                                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3418                                                                         mwindow->edl->local_session->get_selectionend(1)) /
3419                                                                         2;
3420
3421                                                                 if(!shift_down())
3422                                                                 {
3423                                                                         mwindow->edl->local_session->set_selectionstart(position);
3424                                                                         mwindow->edl->local_session->set_selectionend(position);
3425                                                                 }
3426                                                                 else
3427                                                                 if(position < center)
3428                                                                 {
3429                                                                         mwindow->edl->local_session->set_selectionstart(position);
3430                                                                 }
3431                                                                 else
3432                                                                         mwindow->edl->local_session->set_selectionend(position);
3433                                                         }
3434                                                 }
3435                                         }
3436                                         else
3437                                                 draw_pixmap(keyframe_pixmap,
3438                                                         x,
3439                                                         y);
3440                                 }
3441                         }
3442                 }
3443         }
3444
3445
3446
3447 //      if(buttonpress && buttonpress != 3 && result)
3448 //      {
3449 //              mwindow->undo->update_undo_before();
3450 //      }
3451
3452         return result;
3453 }
3454
3455 int TrackCanvas::draw_hairline(Auto *auto_keyframe, int color)
3456 {
3457         Track *track = auto_keyframe->autos->track;
3458         int autogrouptype = auto_keyframe->autos->get_type();
3459
3460         int center_pixel;
3461         double view_start, unit_start;
3462         double view_end, unit_end, yscale;
3463         double zoom_sample, zoom_units;
3464
3465         calculate_viewport(track, view_start, unit_start, view_end, unit_end,
3466                         yscale, center_pixel, zoom_sample, zoom_units);
3467
3468         double ax = 0, ay = 0;
3469         calculate_auto_position(&ax, &ay, 0, 0, 0, 0,
3470                 auto_keyframe, unit_start, zoom_units, yscale, autogrouptype);
3471
3472         set_color(color);
3473         draw_line(ax, 0, ax, get_h());
3474         return 0;
3475 }
3476
3477 void TrackCanvas::draw_overlays()
3478 {
3479         int new_cursor, update_cursor, rerender;
3480
3481 // Move background pixmap to foreground pixmap
3482         draw_pixmap(background_pixmap,
3483                 0,
3484                 0,
3485                 get_w(),
3486                 get_h(),
3487                 0,
3488                 0);
3489
3490 // In/Out points
3491         draw_inout_points();
3492
3493 // Transitions
3494         if(mwindow->edl->session->auto_conf->transitions) draw_transitions();
3495
3496 // Plugins
3497         draw_plugins();
3498         draw_hard_edges();
3499
3500 // Loop points
3501         draw_loop_points();
3502         draw_brender_range();
3503
3504 // Highlighted areas
3505         draw_highlighting();
3506
3507 // Automation
3508         do_keyframes(0, 0, 1, 0, new_cursor, update_cursor, rerender);
3509
3510 // Selection cursor
3511         if(pane->cursor) pane->cursor->restore(1);
3512
3513 // Handle dragging
3514         draw_drag_handle();
3515
3516 // Playback cursor
3517         draw_playback_cursor();
3518
3519         draw_keyframe_reticle();
3520
3521         show_window(0);
3522 }
3523
3524 int TrackCanvas::activate()
3525 {
3526         if(!active)
3527         {
3528 //printf("TrackCanvas::activate %d %d\n", __LINE__, pane->number);
3529 //BC_Signals::dump_stack();
3530                 get_top_level()->deactivate();
3531                 active = 1;
3532                 set_active_subwindow(this);
3533                 pane->cursor->activate();
3534                 gui->focused_pane = pane->number;
3535         }
3536         return 0;
3537 }
3538
3539 int TrackCanvas::deactivate()
3540 {
3541         if(active)
3542         {
3543                 active = 0;
3544                 pane->cursor->deactivate();
3545         }
3546         return 0;
3547 }
3548
3549
3550 void TrackCanvas::update_drag_handle()
3551 {
3552         double new_position;
3553         int cursor_x = get_cursor_x();
3554
3555         new_position =
3556                 (double)(cursor_x +
3557                 mwindow->edl->local_session->view_start[pane->number]) *
3558                 mwindow->edl->local_session->zoom_sample /
3559                 mwindow->edl->session->sample_rate;
3560
3561         new_position =
3562                 mwindow->edl->align_to_frame(new_position, 0);
3563
3564         if( ctrl_down() && alt_down() ) {
3565 #define snapper(v) do { \
3566         double pos = (v); \
3567         if( pos < 0 ) break; \
3568         double dist = fabs(new_position - pos); \
3569         if( dist >= snap_min ) break; \
3570         snap_position = pos;  snap_min = dist; \
3571 } while(0)
3572                 double snap_position = new_position;
3573                 double snap_min = DBL_MAX;
3574                 if( mwindow->edl->local_session->inpoint_valid() )
3575                         snapper(mwindow->edl->local_session->get_inpoint());
3576                 if( mwindow->edl->local_session->outpoint_valid() )
3577                         snapper(mwindow->edl->local_session->get_outpoint());
3578                 snapper(mwindow->edl->prev_edit(new_position));
3579                 snapper(mwindow->edl->next_edit(new_position));
3580                 Label *prev_label = mwindow->edl->labels->prev_label(new_position);
3581                 if( prev_label ) snapper(prev_label->position);
3582                 Label *next_label = mwindow->edl->labels->next_label(new_position);
3583                 if( next_label ) snapper(next_label->position);
3584                 int snap_x = snap_position * mwindow->edl->session->sample_rate /
3585                         mwindow->edl->local_session->zoom_sample -
3586                         mwindow->edl->local_session->view_start[pane->number];
3587                 if( abs(snap_x - cursor_x) < HANDLE_W ) {
3588                         snapped = 1;
3589                         new_position = snap_position;
3590                 }
3591 #undef snapper
3592         }
3593
3594         if(new_position != mwindow->session->drag_position)
3595         {
3596                 mwindow->session->drag_position = new_position;
3597                 gui->mainclock->update(new_position);
3598
3599
3600                 timebar_position = new_position;
3601                 gui->update_timebar(0);
3602 // Que the CWindow.  Doesn't do anything if selectionstart and selection end
3603 // aren't changed.
3604 //              mwindow->cwindow->update(1, 0, 0);
3605         }
3606 }
3607
3608 int TrackCanvas::update_drag_edit()
3609 {
3610         int result = 0;
3611
3612
3613
3614         return result;
3615 }
3616
3617 int TrackCanvas::get_drag_values(float *percentage,
3618         int64_t *position,
3619         int do_clamp,
3620         int cursor_x,
3621         int cursor_y,
3622         Auto *current)
3623 {
3624         //int x = cursor_x - mwindow->session->drag_origin_x;
3625         //int y = cursor_y - mwindow->session->drag_origin_y;
3626         *percentage = 0;
3627         *position = 0;
3628
3629         if(!current->autos->track->record) return 1;
3630         double view_start;
3631         double unit_start;
3632         double view_end;
3633         double unit_end;
3634         double yscale;
3635         int center_pixel;
3636         double zoom_sample;
3637         double zoom_units;
3638
3639         calculate_viewport(current->autos->track,
3640                 view_start,
3641                 unit_start,
3642                 view_end,
3643                 unit_end,
3644                 yscale,
3645                 center_pixel,
3646                 zoom_sample,
3647                 zoom_units);
3648
3649         *percentage = (float)(mwindow->session->drag_origin_y - cursor_y) /
3650                 yscale +
3651                 mwindow->session->drag_start_percentage;
3652         if(do_clamp) CLAMP(*percentage, 0, 1);
3653
3654         *position = Units::to_int64(zoom_units *
3655                 (cursor_x - mwindow->session->drag_origin_x) +
3656                 mwindow->session->drag_start_position + 0.5);
3657
3658         if((do_clamp) && *position < 0) *position = 0;
3659         return 0;
3660 }
3661
3662
3663 #define UPDATE_DRAG_HEAD(do_clamp) \
3664         int result = 0, center_pixel; \
3665         if(!current->autos->track->record) return 0; \
3666         double view_start, unit_start, view_end, unit_end; \
3667         double yscale, zoom_sample, zoom_units; \
3668  \
3669         calculate_viewport(current->autos->track,  \
3670                 view_start, unit_start, view_end, unit_end, \
3671                 yscale, center_pixel, zoom_sample, zoom_units); \
3672  \
3673         float percentage = (float)(mwindow->session->drag_origin_y - cursor_y) / \
3674                 yscale +  mwindow->session->drag_start_percentage; \
3675         if(do_clamp) CLAMP(percentage, 0, 1); \
3676  \
3677         int64_t position = Units::to_int64(zoom_units * \
3678                 (cursor_x - mwindow->session->drag_origin_x) + \
3679                 mwindow->session->drag_start_position); \
3680         if((do_clamp) && position < 0) position = 0;
3681
3682
3683 int TrackCanvas::update_drag_floatauto(int cursor_x, int cursor_y)
3684 {
3685         FloatAuto *current = (FloatAuto*)mwindow->session->drag_auto;
3686
3687         UPDATE_DRAG_HEAD(mwindow->session->drag_handle == 0);
3688         int x = cursor_x - mwindow->session->drag_origin_x;
3689         int y = cursor_y - mwindow->session->drag_origin_y;
3690         float value, old_value;
3691
3692         if( mwindow->session->drag_handle == 0 && (ctrl_down() && !shift_down()) ) {
3693 // not really editing the node, rather start editing the curve
3694 // tangent is editable and drag movement is significant
3695                 if( (FloatAuto::FREE == current->curve_mode ||
3696                      FloatAuto::TFREE==current->curve_mode) &&
3697                     (fabs(x) > HANDLE_W / 2 || fabs(y) > HANDLE_W / 2))
3698                         mwindow->session->drag_handle = x < 0 ? 1 : 2;
3699         }
3700
3701         switch(mwindow->session->drag_handle) {
3702         case 0: // Center
3703 // Snap to nearby values
3704                 old_value = current->get_value();
3705                 if(shift_down()) {
3706                         double value1, value2, distance1, distance2;
3707
3708                         if(current->previous) {
3709                                 int autogrouptype = current->previous->autos->autogrouptype;
3710                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3711                                 value1 = ((FloatAuto*)current->previous)->get_value();
3712                                 distance1 = fabs(value - value1);
3713                                 current->set_value(value1);
3714                         }
3715
3716                         if(current->next) {
3717                                 int autogrouptype = current->next->autos->autogrouptype;
3718                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3719                                 value2 = ((FloatAuto*)current->next)->get_value();
3720                                 distance2 = fabs(value - value2);
3721                                 if(!current->previous || distance2 < distance1) {
3722                                         current->set_value(value2);
3723                                 }
3724                         }
3725
3726                         if(!current->previous && !current->next) {
3727                                 current->set_value( ((FloatAutos*)current->autos)->default_);
3728                         }
3729                         value = current->get_value();
3730                 }
3731                 else {
3732                         int autogrouptype = current->autos->autogrouptype;
3733                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
3734                 }
3735
3736                 if(alt_down() && !shift_down())
3737 // ALT constrains movement: fixed position, only changing the value
3738                         position = mwindow->session->drag_start_position;
3739
3740                 if(value != old_value || position != current->position) {
3741                         result = 1;
3742                         float change = value - old_value;
3743                         current->adjust_to_new_coordinates(position, value);
3744                         synchronize_autos(change, current->autos->track, current, 0);
3745                         show_message(current, 1,", %.2f", current->get_value());
3746                 }
3747                 break;
3748
3749 // In control
3750         case 1: {
3751                 int autogrouptype = current->autos->autogrouptype;
3752                 value = percentage_to_value(percentage, 0, current, autogrouptype);
3753                 if(value != current->get_control_in_value())
3754                 {
3755                         result = 1;
3756                         // note: (position,value) need not be at the location of the ctrl point,
3757                         // but could be somewhere in between on the curve (or even outward or
3758                         // on the opposit side). We set the new control point such as
3759                         // to point the curve through (position,value)
3760                         current->set_control_in_value(
3761                                 value * levered_position(position - current->position,
3762                                                          current->get_control_in_position()));
3763                         synchronize_autos(0, current->autos->track, current, 0);
3764                         show_message(current, 1,", %.2f", current->get_control_in_value());
3765                 }
3766                 break; }
3767
3768 // Out control
3769         case 2: {
3770                 int autogrouptype = current->autos->autogrouptype;
3771                 value = percentage_to_value(percentage, 0, current, autogrouptype);
3772                 if(value != current->get_control_out_value()) {
3773                         result = 1;
3774                         current->set_control_out_value(
3775                                 value * levered_position(position - current->position,
3776                                                          current->get_control_out_position()));
3777                         synchronize_autos(0, current->autos->track, current, 0);
3778                         show_message(current, 1,", %.2f", current->get_control_out_value());
3779                 }
3780                 break; }
3781         }
3782
3783         return result;
3784 }
3785
3786 int TrackCanvas::update_drag_toggleauto(int cursor_x, int cursor_y)
3787 {
3788         IntAuto *current = (IntAuto*)mwindow->session->drag_auto;
3789
3790         UPDATE_DRAG_HEAD(1);
3791         int value = (int)percentage_to_value(percentage, 1, 0, AUTOGROUPTYPE_INT255);
3792
3793         if(value != current->value || position != current->position)
3794         {
3795                 result = 1;
3796                 current->value = value;
3797                 current->position = position;
3798                 show_message(current, 0,", %d", current->value);
3799         }
3800
3801         return result;
3802 }
3803
3804 // Autos which can't change value through dragging.
3805
3806 int TrackCanvas::update_drag_auto(int cursor_x, int cursor_y)
3807 {
3808         Auto *current = (Auto*)mwindow->session->drag_auto;
3809
3810         UPDATE_DRAG_HEAD(1)
3811         if(position != current->position)
3812         {
3813                 result = 1;
3814                 current->position = position;
3815                 show_message(current, 0,"");
3816
3817                 double position_f = current->autos->track->from_units(current->position);
3818                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3819                         mwindow->edl->local_session->get_selectionend(1)) /
3820                         2;
3821                 if(!shift_down())
3822                 {
3823                         mwindow->edl->local_session->set_selectionstart(position_f);
3824                         mwindow->edl->local_session->set_selectionend(position_f);
3825                 }
3826                 else
3827                 if(position_f < center_f)
3828                 {
3829                         mwindow->edl->local_session->set_selectionstart(position_f);
3830                 }
3831                 else
3832                         mwindow->edl->local_session->set_selectionend(position_f);
3833         }
3834
3835
3836         return result;
3837 }
3838
3839 int TrackCanvas::update_drag_pluginauto(int cursor_x, int cursor_y)
3840 {
3841         KeyFrame *current = (KeyFrame*)mwindow->session->drag_auto;
3842
3843         UPDATE_DRAG_HEAD(1)
3844         if(position != current->position)
3845         {
3846 //      printf("uida: autos: %p, track: %p ta: %p\n", current->autos, current->autos->track, current->autos->track->automation);
3847                 Track *track = current->autos->track;
3848                 //PluginAutos *pluginautos = (PluginAutos *)current->autos;
3849                 PluginSet *pluginset;
3850                 Plugin *plugin = 0;
3851 // figure out the correct pluginset & correct plugin
3852                 int found = 0;
3853                 for(int i = 0; i < track->plugin_set.total; i++)
3854                 {
3855                         pluginset = track->plugin_set.values[i];
3856                         for(plugin = (Plugin *)pluginset->first; plugin; plugin = (Plugin *)plugin->next)
3857                         {
3858                                 KeyFrames *keyframes = plugin->keyframes;
3859                                 for(KeyFrame *currentkeyframe = (KeyFrame *)keyframes->first;
3860                                         currentkeyframe;
3861                                         currentkeyframe = (KeyFrame *) currentkeyframe->next)
3862                                 {
3863                                         if (currentkeyframe == current)
3864                                         {
3865                                                 found = 1;
3866                                                 break;
3867                                         }
3868
3869                                 }
3870                                 if (found) break;
3871                         }
3872                         if (found) break;
3873                 }
3874
3875                 mwindow->session->plugin_highlighted = plugin;
3876                 mwindow->session->track_highlighted = track;
3877                 result = 1;
3878                 current->position = position;
3879                 show_message(current, 0,"");
3880
3881                 double position_f = current->autos->track->from_units(current->position);
3882                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3883                         mwindow->edl->local_session->get_selectionend(1)) /
3884                         2;
3885                 if(!shift_down())
3886                 {
3887                         mwindow->edl->local_session->set_selectionstart(position_f);
3888                         mwindow->edl->local_session->set_selectionend(position_f);
3889                 }
3890                 else
3891                 if(position_f < center_f)
3892                 {
3893                         mwindow->edl->local_session->set_selectionstart(position_f);
3894                 }
3895                 else
3896                         mwindow->edl->local_session->set_selectionend(position_f);
3897         }
3898
3899
3900         return result;
3901 }
3902
3903 void TrackCanvas::update_drag_caption()
3904 {
3905         switch(mwindow->session->current_operation)
3906         {
3907                 case DRAG_FADE:
3908
3909                         break;
3910         }
3911 }
3912
3913
3914
3915 int TrackCanvas::cursor_update(int in_motion)
3916 {
3917         int result = 0;
3918         int cursor_x = 0;
3919         int cursor_y = 0;
3920         int update_clock = 0;
3921         int update_zoom = 0;
3922         int update_scroll = 0;
3923         int update_overlay = 0;
3924         int update_cursor = 0;
3925         int new_cursor = 0;
3926         int rerender = 0;
3927         double position = 0.;
3928 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
3929
3930 // Default cursor
3931         switch(mwindow->edl->session->editing_mode)
3932         {
3933                 case EDITING_ARROW: new_cursor = ARROW_CURSOR; break;
3934                 case EDITING_IBEAM: new_cursor = IBEAM_CURSOR; break;
3935         }
3936
3937         switch(mwindow->session->current_operation)
3938         {
3939                 case DRAG_EDITHANDLE1:
3940 // Outside threshold.  Upgrade status
3941                         if(active)
3942                         {
3943                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
3944                                 {
3945                                         mwindow->session->current_operation = DRAG_EDITHANDLE2;
3946                                         update_overlay = 1;
3947                                 }
3948                         }
3949                         break;
3950
3951                 case DRAG_EDITHANDLE2:
3952                         if(active)
3953                         {
3954                                 update_drag_handle();
3955                                 update_overlay = 1;
3956                         }
3957                         break;
3958
3959                 case DRAG_PLUGINHANDLE1:
3960                         if(active)
3961                         {
3962                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
3963                                 {
3964                                         mwindow->session->current_operation = DRAG_PLUGINHANDLE2;
3965                                         update_overlay = 1;
3966                                 }
3967                         }
3968                         break;
3969
3970                 case DRAG_PLUGINHANDLE2:
3971                         if(active)
3972                         {
3973                                 update_drag_handle();
3974                                 update_overlay = 1;
3975                         }
3976                         break;
3977
3978 // Rubber band curves
3979                 case DRAG_FADE:
3980                 case DRAG_SPEED:
3981                 case DRAG_CZOOM:
3982                 case DRAG_PZOOM:
3983                 case DRAG_CAMERA_X:
3984                 case DRAG_CAMERA_Y:
3985                 case DRAG_CAMERA_Z:
3986                 case DRAG_PROJECTOR_X:
3987                 case DRAG_PROJECTOR_Y:
3988                 case DRAG_PROJECTOR_Z:
3989                         if(active) rerender = update_overlay =
3990                                 update_drag_floatauto(get_cursor_x(), get_cursor_y());
3991                         if( rerender && mwindow->session->current_operation == DRAG_SPEED )
3992                                 mwindow->speed_after(!in_motion ? 1 : 0);
3993                         break;
3994
3995                 case DRAG_PLAY:
3996                         if(active) rerender = update_overlay =
3997                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
3998                         break;
3999
4000                 case DRAG_MUTE:
4001                         if(active) rerender = update_overlay =
4002                                 update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4003                         break;
4004
4005 // Keyframe icons are sticky
4006                 case DRAG_PAN_PRE:
4007                 case DRAG_MASK_PRE:
4008                 case DRAG_MODE_PRE:
4009                 case DRAG_PLUGINKEY_PRE:
4010                         if(active) {
4011                                 if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W) {
4012                                         mwindow->session->current_operation++;
4013                                         update_overlay = 1;
4014
4015                                         mwindow->undo->update_undo_before();
4016                                 }
4017                         }
4018                         break;
4019
4020                 case DRAG_PAN:
4021                 case DRAG_MASK:
4022                 case DRAG_MODE:
4023                         if(active) rerender = update_overlay =
4024                                 update_drag_auto(get_cursor_x(), get_cursor_y());
4025                         break;
4026
4027                 case DRAG_PLUGINKEY:
4028                         if(active) rerender = update_overlay =
4029                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4030                         break;
4031
4032                 case SELECT_REGION:
4033                         if(active) {
4034                                 cursor_x = get_cursor_x();
4035                                 cursor_y = get_cursor_y();
4036                                 position = (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
4037                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
4038
4039                                 position = mwindow->edl->align_to_frame(position, 0);
4040                                 position = MAX(position, 0);
4041
4042                                 double start = mwindow->edl->local_session->get_selectionstart(1);
4043                                 double end = mwindow->edl->local_session->get_selectionend(1);
4044                                 if(position < selection_midpoint) {
4045                                         mwindow->edl->local_session->set_selectionend(selection_midpoint);
4046                                         mwindow->edl->local_session->set_selectionstart(position);
4047                                 }
4048                                 else {
4049                                         mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4050                                         mwindow->edl->local_session->set_selectionend(position);
4051                                 }
4052         // Que the CWindow
4053                                 gui->unlock_window();
4054                                 int dir =
4055                                         start != mwindow->edl->local_session->get_selectionstart(1) ? 1 :
4056                                         end != mwindow->edl->local_session->get_selectionend(1) ? -1 : 0;
4057                                 mwindow->cwindow->update(dir, 0, 0, 0, 1);
4058                                 gui->lock_window("TrackCanvas::cursor_update 1");
4059         // Update the faders
4060                                 mwindow->update_plugin_guis();
4061                                 gui->update_patchbay();
4062
4063                                 timebar_position = mwindow->edl->local_session->get_selectionend(1);
4064
4065                                 gui->hide_cursor(0);
4066                                 gui->draw_cursor(1);
4067                                 gui->update_timebar(0);
4068                                 gui->flash_canvas(1);
4069                                 result = 1;
4070                                 update_clock = 1;
4071                                 update_zoom = 1;
4072                                 update_scroll = 1;
4073                         }
4074                         break;
4075
4076                 default:
4077                         if(is_event_win() && cursor_inside()) {
4078 // Update clocks
4079                                 cursor_x = get_cursor_x();
4080                                 position = (double)cursor_x *
4081                                         (double)mwindow->edl->local_session->zoom_sample /
4082                                         (double)mwindow->edl->session->sample_rate +
4083                                         (double)mwindow->edl->local_session->view_start[pane->number] *
4084                                         (double)mwindow->edl->local_session->zoom_sample /
4085                                         (double)mwindow->edl->session->sample_rate;
4086                                 position = mwindow->edl->align_to_frame(position, 0);
4087                                 update_clock = 1;
4088
4089 // set all timebars
4090                                 for(int i = 0; i < TOTAL_PANES; i++)
4091                                         if(gui->pane[i]) gui->pane[i]->canvas->timebar_position = position;
4092
4093 //printf("TrackCanvas::cursor_update %d %d %p %p\n", __LINE__, pane->number, pane, pane->timebar);
4094                                 gui->update_timebar(0);
4095 // Update cursor
4096                                 if(do_transitions(get_cursor_x(), get_cursor_y(),
4097                                                 0, new_cursor, update_cursor)) break;
4098                                 if(do_keyframes(get_cursor_x(), get_cursor_y(),
4099                                         0, 0, new_cursor, update_cursor, rerender)) break;
4100                                 if(do_edit_handles(get_cursor_x(), get_cursor_y(),
4101                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4102 // Plugin boundaries
4103                                 if(do_plugin_handles(get_cursor_x(), get_cursor_y(),
4104                                         0, rerender, update_overlay, new_cursor, update_cursor)) break;
4105                                 if(do_edits(get_cursor_x(), get_cursor_y(),
4106                                         0, 0, update_overlay, rerender, new_cursor, update_cursor)) break;
4107                         }
4108                         break;
4109         }
4110
4111 //printf("TrackCanvas::cursor_update 1\n");
4112         if(update_cursor && new_cursor != get_cursor())
4113         {
4114                 set_cursor(new_cursor, 0, 1);
4115         }
4116
4117 //printf("TrackCanvas::cursor_update 1 %d\n", rerender);
4118         if(rerender && render_timer->get_difference() > 0.25 ) {
4119                 render_timer->update();
4120                 mwindow->restart_brender();
4121                 mwindow->sync_parameters(CHANGE_PARAMS);
4122                 mwindow->update_plugin_guis();
4123                 gui->unlock_window();
4124                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4125                 gui->lock_window("TrackCanvas::cursor_update 2");
4126         }
4127         if(rerender) {
4128 // Update faders
4129                 gui->update_patchbay();
4130         }
4131
4132
4133         if(update_clock) {
4134                 if(!mwindow->cwindow->playback_engine->is_playing_back)
4135                         gui->mainclock->update(position);
4136         }
4137
4138         if(update_zoom) {
4139                 gui->zoombar->update();
4140         }
4141
4142         if(update_scroll) {
4143                 if(!drag_scroll &&
4144                         (cursor_x >= get_w() || cursor_x < 0 || cursor_y >= get_h() || cursor_y < 0))
4145                         start_dragscroll();
4146                 else
4147                 if(drag_scroll &&
4148                         (cursor_x < get_w() && cursor_x >= 0 && cursor_y < get_h() && cursor_y >= 0))
4149                         stop_dragscroll();
4150         }
4151
4152         if(update_overlay) {
4153                 gui->draw_overlays(1);
4154         }
4155
4156 //printf("TrackCanvas::cursor_update %d\n", __LINE__);
4157         return result;
4158 }
4159
4160 int TrackCanvas::cursor_motion_event()
4161 {
4162         return cursor_update(1);
4163 }
4164
4165 void TrackCanvas::start_dragscroll()
4166 {
4167         if(!drag_scroll) {
4168                 drag_scroll = 1;
4169                 set_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4170 //printf("TrackCanvas::start_dragscroll 1\n");
4171         }
4172 }
4173
4174 void TrackCanvas::stop_dragscroll()
4175 {
4176         if(drag_scroll) {
4177                 drag_scroll = 0;
4178                 unset_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4179 //printf("TrackCanvas::stop_dragscroll 1\n");
4180         }
4181 }
4182
4183 int TrackCanvas::repeat_event(int64_t duration)
4184 {
4185         if(!drag_scroll) return 0;
4186         if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
4187
4188         int sample_movement = 0;
4189         int track_movement = 0;
4190         int64_t x_distance = 0;
4191         int64_t y_distance = 0;
4192         double position = 0;
4193         int result = 0;
4194
4195         switch(mwindow->session->current_operation) {
4196                 case SELECT_REGION:
4197 //printf("TrackCanvas::repeat_event 1 %d\n", mwindow->edl->local_session->view_start);
4198                 if(get_cursor_x() > get_w()) {
4199                                 x_distance = get_cursor_x() - get_w();
4200                                 sample_movement = 1;
4201                 }
4202                 else if(get_cursor_x() < 0) {
4203                         x_distance = get_cursor_x();
4204                         sample_movement = 1;
4205                 }
4206
4207                 if(get_cursor_y() > get_h()) {
4208                         y_distance = get_cursor_y() - get_h();
4209                         track_movement = 1;
4210                 }
4211                 else if(get_cursor_y() < 0) {
4212                         y_distance = get_cursor_y();
4213                         track_movement = 1;
4214                 }
4215                 result = 1;
4216                 break;
4217         }
4218
4219         if(sample_movement) {
4220                 position = (double)(get_cursor_x() +
4221                         mwindow->edl->local_session->view_start[pane->number] +
4222                         x_distance) *
4223                         mwindow->edl->local_session->zoom_sample /
4224                         mwindow->edl->session->sample_rate;
4225                 position = mwindow->edl->align_to_frame(position, 0);
4226                 position = MAX(position, 0);
4227
4228 //printf("TrackCanvas::repeat_event 1 %f\n", position);
4229                 switch(mwindow->session->current_operation) {
4230                 case SELECT_REGION:
4231                         if(position < selection_midpoint) {
4232                                 mwindow->edl->local_session->set_selectionend(selection_midpoint);
4233                                 mwindow->edl->local_session->set_selectionstart(position);
4234 // Que the CWindow
4235                                 gui->unlock_window();
4236                                 mwindow->cwindow->update(1, 0, 0);
4237                                 gui->lock_window("TrackCanvas::repeat_event");
4238 // Update the faders
4239                                 mwindow->update_plugin_guis();
4240                                 gui->update_patchbay();
4241                         }
4242                         else {
4243                                 mwindow->edl->local_session->set_selectionstart(selection_midpoint);
4244                                 mwindow->edl->local_session->set_selectionend(position);
4245 // Don't que the CWindow
4246                         }
4247                         break;
4248                 }
4249
4250                 mwindow->samplemovement(
4251                         mwindow->edl->local_session->view_start[pane->number] + x_distance,
4252                         pane->number);
4253         }
4254
4255         if(track_movement) {
4256                 mwindow->trackmovement(y_distance, pane->number);
4257         }
4258
4259         return result;
4260 }
4261
4262 int TrackCanvas::button_release_event()
4263 {
4264         int redraw = -1, update_overlay = 0;
4265         int result = 0, load_flags = 0;
4266
4267 // printf("TrackCanvas::button_release_event %d\n",
4268 // mwindow->session->current_operation);
4269         if(active) {
4270                 switch(mwindow->session->current_operation) {
4271                 case DRAG_EDITHANDLE2:
4272                         mwindow->session->current_operation = NO_OPERATION;
4273                         drag_scroll = 0;
4274                         result = 1;
4275
4276                         end_edithandle_selection();
4277                         break;
4278
4279                 case DRAG_EDITHANDLE1:
4280                         mwindow->session->current_operation = NO_OPERATION;
4281                         drag_scroll = 0;
4282                         result = 1;
4283                         break;
4284
4285                 case DRAG_PLUGINHANDLE2:
4286                         mwindow->session->current_operation = NO_OPERATION;
4287                         drag_scroll = 0;
4288                         result = 1;
4289
4290                         end_pluginhandle_selection();
4291                         break;
4292
4293                 case DRAG_PLUGINHANDLE1:
4294                         mwindow->session->current_operation = NO_OPERATION;
4295                         drag_scroll = 0;
4296                         result = 1;
4297                         break;
4298
4299                 case DRAG_SPEED:
4300                         redraw = FORCE_REDRAW;
4301                         load_flags |= LOAD_EDITS;
4302                 case DRAG_FADE:
4303 // delete the drag_auto_gang first and remove out of order keys
4304                         synchronize_autos(0, 0, 0, -1);
4305                 case DRAG_CZOOM:
4306                 case DRAG_PZOOM:
4307                 case DRAG_PLAY:
4308                 case DRAG_MUTE:
4309                 case DRAG_MASK:
4310                 case DRAG_MODE:
4311                 case DRAG_PAN:
4312                 case DRAG_CAMERA_X:
4313                 case DRAG_CAMERA_Y:
4314                 case DRAG_CAMERA_Z:
4315                 case DRAG_PROJECTOR_X:
4316                 case DRAG_PROJECTOR_Y:
4317                 case DRAG_PROJECTOR_Z:
4318                 case DRAG_PLUGINKEY:
4319                         load_flags |= LOAD_AUTOMATION;
4320                         mwindow->session->current_operation = NO_OPERATION;
4321                         mwindow->session->drag_handle = 0;
4322 // Remove any out-of-order keyframe
4323                         if(mwindow->session->drag_auto) {
4324                                 mwindow->session->drag_auto->autos->remove_nonsequential(
4325                                                 mwindow->session->drag_auto);
4326 //                              mwindow->session->drag_auto->autos->optimize();
4327                                 update_overlay = 1;
4328                         }
4329
4330                         mwindow->undo->update_undo_after(_("keyframe"), load_flags);
4331                         result = 1;
4332                         break;
4333
4334                 case DRAG_EDIT:
4335                 case DRAG_AEFFECT_COPY:
4336                 case DRAG_VEFFECT_COPY:
4337 // Trap in drag stop
4338                         break;
4339
4340
4341                 default:
4342                         if(mwindow->session->current_operation) {
4343 //                              if(mwindow->session->current_operation == SELECT_REGION) {
4344 //                                      mwindow->undo->update_undo_after(_("select"), LOAD_SESSION, 0, 0);
4345 //                              }
4346
4347                                 mwindow->session->current_operation = NO_OPERATION;
4348                                 drag_scroll = 0;
4349 // Traps button release events
4350 //                              result = 1;
4351                         }
4352                         break;
4353                 }
4354         }
4355
4356         if (result)
4357                 cursor_update(0);
4358
4359         if(update_overlay) {
4360                 gui->draw_overlays(1);
4361         }
4362         if(redraw >= 0) {
4363                 gui->draw_canvas(redraw, 0);
4364                 gui->flash_canvas(1);
4365         }
4366         return result;
4367 }
4368
4369 int TrackCanvas::do_edit_handles(int cursor_x, int cursor_y, int button_press,
4370         int &rerender, int &update_overlay, int &new_cursor, int &update_cursor)
4371 {
4372         Edit *edit_result = 0;
4373         int handle_result = -1;
4374         int result = 0;
4375
4376         if( !mwindow->edl->session->show_assets ) return 0;
4377
4378         for( Track *track=mwindow->edl->tracks->first; track && !result; track=track->next) {
4379                 for( Edit *edit=track->edits->first; edit && !result; edit=edit->next ) {
4380                         int64_t edit_x, edit_y, edit_w, edit_h;
4381                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4382
4383                         if( cursor_x >= edit_x && cursor_x <= edit_x + edit_w &&
4384                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h ) {
4385                                 if( cursor_x < edit_x + HANDLE_W ) {
4386                                         edit_result = edit;
4387                                         handle_result = 0;
4388                                         if( cursor_y >= edit_y+edit_h - HANDLE_W ) {
4389                                                 new_cursor = DOWNLEFT_RESIZE;
4390                                                 if( button_press == LEFT_BUTTON )
4391                                                         result = -1;
4392                                         }
4393                                         else
4394                                                 result = 1;
4395                                 }
4396                                 else if( cursor_x >= edit_x + edit_w - HANDLE_W ) {
4397                                         edit_result = edit;
4398                                         handle_result = 1;
4399                                         if( cursor_y >= edit_y+edit_h - HANDLE_W ) {
4400                                                 new_cursor = DOWNRIGHT_RESIZE;
4401                                                 if( button_press == LEFT_BUTTON )
4402                                                         result = -1;
4403                                         }
4404                                         else
4405                                                 result = 1;
4406                                 }
4407                         }
4408                 }
4409         }
4410
4411         update_cursor = 1;
4412         if( result > 0 ) {
4413                 double position = 0;
4414                 if( handle_result == 0 ) {
4415                         position = edit_result->track->from_units(edit_result->startproject);
4416                         new_cursor = LEFT_CURSOR;
4417                 }
4418                 else if( handle_result == 1 ) {
4419                         position = edit_result->track->from_units(edit_result->startproject + edit_result->length);
4420                         new_cursor = RIGHT_CURSOR;
4421                 }
4422
4423 // Reposition cursor
4424                 if( button_press ) {
4425                         mwindow->session->drag_edit = edit_result;
4426                         mwindow->session->drag_handle = handle_result;
4427                         mwindow->session->drag_button = get_buttonpress() - 1;
4428                         mwindow->session->drag_position = position;
4429                         mwindow->session->current_operation = DRAG_EDITHANDLE1;
4430                         mwindow->session->drag_origin_x = get_cursor_x();
4431                         mwindow->session->drag_origin_y = get_cursor_y();
4432                         mwindow->session->drag_start = position;
4433
4434                         rerender = start_selection(position);
4435                         update_overlay = 1;
4436                 }
4437         }
4438         else if( result <  0) {
4439                 mwindow->undo->update_undo_before();
4440                 if( !shift_down() ) {
4441                         if( handle_result == 0 )
4442                                 edit_result->hard_left = !edit_result->hard_left;
4443                         else if( handle_result == 1 )
4444                                 edit_result->hard_right = !edit_result->hard_right;
4445                 }
4446                 else {
4447                         int status = handle_result == 0 ? edit_result->hard_left :
4448                                      handle_result == 1 ? edit_result->hard_right : 0;
4449                         int new_status = !status;
4450                         int64_t edit_edge = edit_result->startproject;
4451                         if( handle_result == 1 ) edit_edge += edit_result->length;
4452                         double edge_position = edit_result->track->from_units(edit_edge);
4453                         for( Track *track=mwindow->edl->tracks->first; track!=0; track=track->next ) {
4454                                 int64_t track_position = track->to_units(edge_position, 1);
4455                                 Edit *left_edit = track->edits->editof(track_position, PLAY_FORWARD, 0);
4456                                 if( left_edit ) {
4457                                         int64_t left_edge = left_edit->startproject;
4458                                         double left_position = track->from_units(left_edge);
4459                                         if( EQUIV(edge_position, left_position) ) {
4460                                                 left_edit->hard_left = new_status;
4461                                                 if( left_edit->previous )
4462                                                         left_edit->previous->hard_right = new_status;
4463                                         }
4464                                 }
4465                                 Edit *right_edit = track->edits->editof(track_position, PLAY_REVERSE, 0);
4466                                 if( right_edit ) {
4467                                         int64_t right_edge = right_edit->startproject + right_edit->length;
4468                                         double right_position = track->from_units(right_edge);
4469                                         if( EQUIV(edge_position, right_position) ) {
4470                                                 right_edit->hard_right = new_status;
4471                                                 if( right_edit->next )
4472                                                         right_edit->next->hard_left = new_status;
4473                                         }
4474                                 }
4475                         }
4476                 }
4477                 rerender = update_overlay = 1;
4478                 mwindow->undo->update_undo_after(_("hard_edge"), LOAD_EDITS);
4479                 result = 1;
4480         }
4481
4482         return result;
4483 }
4484
4485 int TrackCanvas::do_plugin_handles(int cursor_x,
4486         int cursor_y,
4487         int button_press,
4488         int &rerender,
4489         int &update_overlay,
4490         int &new_cursor,
4491         int &update_cursor)
4492 {
4493         Plugin *plugin_result = 0;
4494         int handle_result = 0;
4495         int result = 0;
4496
4497 //      if(!mwindow->edl->session->show_assets) return 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 //      if(!mwindow->edl->session->show_assets) return 0;
4566
4567         for(Track *track = mwindow->edl->tracks->first;
4568                 track && !result;
4569                 track = track->next) {
4570                 int64_t track_x, track_y, track_w, track_h;
4571                 track_dimensions(track, track_x, track_y, track_w, track_h);
4572
4573                 if( button_press && get_buttonpress() == RIGHT_BUTTON &&
4574                     cursor_y >= track_y && cursor_y < track_y + track_h) {
4575                         double pos = mwindow->edl->get_cursor_position(cursor_x, pane->number);
4576                         int64_t position = track->to_units(pos, 0);
4577                         gui->edit_menu->update(track, track->edits->editof(position, PLAY_FORWARD, 0));
4578                         gui->edit_menu->activate_menu();
4579                         result = 1;
4580                 }
4581         }
4582
4583         return result;
4584 }
4585
4586 int TrackCanvas::do_edits(int cursor_x, int cursor_y, int button_press, int drag_start,
4587         int &redraw, int &rerender, int &new_cursor, int &update_cursor)
4588 {
4589         int result = 0;
4590
4591         if(!mwindow->edl->session->show_assets) return 0;
4592
4593         for(Track *track = mwindow->edl->tracks->first; track && !result; track = track->next) {
4594
4595                 for(Edit *edit = track->edits->first; edit && !result; edit = edit->next) {
4596                         int64_t edit_x, edit_y, edit_w, edit_h;
4597                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4598
4599 // Cursor inside a track
4600 // Cursor inside an edit
4601                         if(cursor_x >= edit_x && cursor_x < edit_x + edit_w &&
4602                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h) {
4603 // Select duration of edit
4604                                 if(button_press) {
4605                                         if(get_double_click() && !drag_start) {
4606                                                 mwindow->edl->local_session->set_selectionstart(edit->track->from_units(edit->startproject));
4607                                                 mwindow->edl->local_session->set_selectionend(edit->track->from_units(edit->startproject) +
4608                                                         edit->track->from_units(edit->length));
4609                                                 if(mwindow->edl->session->cursor_on_frames)
4610                                                 {
4611                                                         mwindow->edl->local_session->set_selectionstart(
4612                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4613                                                         mwindow->edl->local_session->set_selectionend(
4614                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4615                                                 }
4616                                                 redraw = 1;
4617                                                 rerender = 1;
4618                                                 update_cursor = -1;
4619                                                 result = 1;
4620                                         }
4621                                 }
4622                                 else if(drag_start && track->record) {
4623                                         if(mwindow->edl->session->editing_mode == EDITING_ARROW) {
4624 // Need to create drag window
4625                                                 mwindow->session->current_operation = DRAG_EDIT;
4626                                                 mwindow->session->drag_edit = edit;
4627 //printf("TrackCanvas::do_edits 2\n");
4628
4629 // Drag only one edit if ctrl is initially down
4630                                                 if(ctrl_down()) {
4631                                                         mwindow->session->drag_edits->remove_all();
4632                                                         mwindow->session->drag_edits->append(edit);
4633                                                 }
4634                                                 else {
4635 // Construct list of all affected edits
4636                                                         mwindow->edl->tracks->get_affected_edits(
4637                                                                 mwindow->session->drag_edits,
4638                                                                 edit->track->from_units(edit->startproject),
4639                                                                 edit->track);
4640                                                 }
4641                                                 mwindow->session->drag_origin_x = cursor_x;
4642                                                 mwindow->session->drag_origin_y = cursor_y;
4643                                                 // Where the drag started, so we know relative position inside the edit later
4644                                                 mwindow->session->drag_position = (double)cursor_x *
4645                                                         mwindow->edl->local_session->zoom_sample /
4646                                                         mwindow->edl->session->sample_rate +
4647                                                         (double)mwindow->edl->local_session->view_start[pane->number] *
4648                                                         mwindow->edl->local_session->zoom_sample /
4649                                                         mwindow->edl->session->sample_rate;
4650
4651                                                 int cx, cy;
4652                                                 get_abs_cursor(cx, cy);
4653                                                 gui->drag_popup = new BC_DragWindow(gui,
4654                                                         mwindow->theme->get_image("clip_icon"), cx, cy);
4655
4656                                                 result = 1;
4657                                         }
4658                                 }
4659                         }
4660                 }
4661         }
4662         return result;
4663 }
4664
4665
4666 int TrackCanvas::test_resources(int cursor_x, int cursor_y)
4667 {
4668         return 0;
4669 }
4670
4671 int TrackCanvas::do_plugins(int cursor_x, int cursor_y, int drag_start,
4672         int button_press, int &redraw, int &rerender)
4673 {
4674         Plugin *plugin = 0;
4675         int result = 0;
4676         int done = 0;
4677         int64_t x, y, w, h;
4678         Track *track = 0;
4679
4680
4681 //      if(!mwindow->edl->session->show_assets) return 0;
4682
4683
4684         for(track = mwindow->edl->tracks->first; track && !done; track = track->next) {
4685                 if(!track->expand_view) continue;
4686
4687                 for(int i = 0; i < track->plugin_set.total && !done; i++) {
4688                         // first check if plugins are visible at all
4689                         if (!track->expand_view)
4690                                 continue;
4691                         PluginSet *plugin_set = track->plugin_set.values[i];
4692                         for(plugin = (Plugin*)plugin_set->first;
4693                                 plugin && !done;
4694                                 plugin = (Plugin*)plugin->next) {
4695                                 plugin_dimensions(plugin, x, y, w, h);
4696                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
4697                                         MWindowGUI::visible(y, y + h, 0, get_h())) {
4698                                         if(cursor_x >= x && cursor_x < x + w &&
4699                                                 cursor_y >= y && cursor_y < y + h) {
4700                                                 done = 1;
4701                                                 break;
4702                                         }
4703                                 }
4704                         }
4705                 }
4706         }
4707
4708         if(plugin) {
4709 // Start plugin popup
4710                 if(button_press) {
4711                         if(get_buttonpress() == 3) {
4712                                 gui->plugin_menu->update(plugin);
4713                                 gui->plugin_menu->activate_menu();
4714                                 result = 1;
4715                         }
4716                         else if (get_double_click() && !drag_start) {
4717 // Select range of plugin on doubleclick over plugin
4718                                 mwindow->edl->local_session->set_selectionstart(plugin->track->from_units(plugin->startproject));
4719                                 mwindow->edl->local_session->set_selectionend(plugin->track->from_units(plugin->startproject) +
4720                                         plugin->track->from_units(plugin->length));
4721                                 if(mwindow->edl->session->cursor_on_frames) {
4722                                         mwindow->edl->local_session->set_selectionstart(
4723                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4724                                         mwindow->edl->local_session->set_selectionend(
4725                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4726                                 }
4727                                 rerender = 1;
4728                                 redraw = 1;
4729                                 result = 1;
4730                         }
4731                 }
4732                 else
4733 // Move plugin
4734                 if(drag_start && plugin->track->record) {
4735                         if(mwindow->edl->session->editing_mode == EDITING_ARROW) {
4736                                 if(plugin->track->data_type == TRACK_AUDIO)
4737                                         mwindow->session->current_operation = DRAG_AEFFECT_COPY;
4738                                 else if(plugin->track->data_type == TRACK_VIDEO)
4739                                         mwindow->session->current_operation = DRAG_VEFFECT_COPY;
4740
4741                                 mwindow->session->drag_plugin = plugin;
4742                                 mwindow->session->drag_origin_x = cursor_x;
4743                                 mwindow->session->drag_origin_y = cursor_y;
4744                                 // Where the drag started, so we know relative position inside the edit later
4745                                 mwindow->session->drag_position =
4746                                         (double)(cursor_x + mwindow->edl->local_session->view_start[pane->number]) *
4747                                         mwindow->edl->local_session->zoom_sample / mwindow->edl->session->sample_rate;
4748 // Create picon
4749                                 switch(plugin->plugin_type) {
4750                                 case PLUGIN_STANDALONE: {
4751                                         PluginServer *server =
4752                                                 mwindow->scan_plugindb(plugin->title, plugin->track->data_type);
4753                                         if( !server ) break;
4754                                         VFrame *frame = server->picon;
4755                                         if(!frame) {
4756                                                 if(plugin->track->data_type == TRACK_AUDIO) {
4757                                                         frame = mwindow->theme->get_image("aeffect_icon");
4758                                                 }
4759                                                 else {
4760                                                         frame = mwindow->theme->get_image("veffect_icon");
4761                                                 }
4762                                         }
4763                                         int cx, cy;
4764                                         get_abs_cursor(cx, cy);
4765                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
4766                                                 break; }
4767
4768                                 case PLUGIN_SHAREDPLUGIN:
4769                                 case PLUGIN_SHAREDMODULE: {
4770                                         VFrame *frame = mwindow->theme->get_image("clip_icon");
4771                                         int cx, cy;
4772                                         get_abs_cursor(cx, cy);
4773                                         gui->drag_popup = new BC_DragWindow(gui, frame, cx, cy);
4774                                         break; }
4775                                 }
4776
4777                                 result = 1;
4778                         }
4779                 }
4780         }
4781
4782         return result;
4783 }
4784
4785 int TrackCanvas::do_transitions(int cursor_x, int cursor_y,
4786         int button_press, int &new_cursor, int &update_cursor)
4787 {
4788         Transition *transition = 0;
4789         int result = 0;
4790         int64_t x, y, w, h;
4791
4792         if(/* !mwindow->edl->session->show_assets || */
4793                 !mwindow->edl->session->auto_conf->transitions) return 0;
4794
4795         for( Track *track = mwindow->edl->tracks->first; track && !result; track = track->next ) {
4796
4797                 for( Edit *edit = track->edits->first; edit; edit = edit->next ) {
4798                         if( edit->transition ) {
4799                                 edit_dimensions(edit, x, y, w, h);
4800                                 get_transition_coords(x, y, w, h);
4801
4802                                 if( MWindowGUI::visible(x, x + w, 0, get_w()) &&
4803                                         MWindowGUI::visible(y, y + h, 0, get_h()) ) {
4804                                         if( cursor_x >= x && cursor_x < x + w &&
4805                                                 cursor_y >= y && cursor_y < y + h ) {
4806                                                 transition = edit->transition;
4807                                                 result = 1;
4808                                                 break;
4809                                         }
4810                                 }
4811                         }
4812                 }
4813         }
4814
4815         update_cursor = 1;
4816         if(transition) {
4817                 if(!button_press) {
4818                         new_cursor = UPRIGHT_ARROW_CURSOR;
4819                 }
4820                 else if(get_buttonpress() == 3) {
4821                         gui->transition_menu->update(transition);
4822                         gui->transition_menu->activate_menu();
4823                 }
4824         }
4825
4826         return result;
4827 }
4828
4829 int TrackCanvas::button_press_event()
4830 {
4831         int result = 0;
4832         int cursor_x, cursor_y;
4833         int new_cursor;
4834
4835         cursor_x = get_cursor_x();
4836         cursor_y = get_cursor_y();
4837         mwindow->session->trim_edits = 0;
4838
4839         if(is_event_win() && cursor_inside()) {
4840 //              double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
4841
4842                 result = 1;
4843                 if(!active) {
4844                         activate();
4845                 }
4846
4847                 if( get_buttonpress() == LEFT_BUTTON ) {
4848                         gui->stop_transport("TrackCanvas::button_press_event");
4849                 }
4850
4851                 int update_overlay = 0, update_cursor = 0, rerender = 0;
4852
4853                 if(get_buttonpress() == WHEEL_UP) {
4854                         if(shift_down())
4855                                 mwindow->expand_sample();
4856                         else if(ctrl_down())
4857                                 mwindow->move_left(get_w()/ 10);
4858                         else
4859                                 mwindow->move_up(get_h() / 10);
4860                 }
4861                 else if(get_buttonpress() == WHEEL_DOWN) {
4862                         if(shift_down())
4863                                 mwindow->zoom_in_sample();
4864                         else if(ctrl_down())
4865                                 mwindow->move_right(get_w() / 10);
4866                         else
4867                                 mwindow->move_down(get_h() / 10);
4868                 }
4869                 else if(get_buttonpress() == 6) {
4870                         if(ctrl_down())
4871                                 mwindow->move_left(get_w());
4872                         else if(alt_down())
4873                                 mwindow->move_left(get_w() / 20);
4874                         else
4875                                 mwindow->move_left(get_w() / 5);
4876                 }
4877                 else if(get_buttonpress() == 7) {
4878                         if(ctrl_down())
4879                                 mwindow->move_right(get_w());
4880                         else if(alt_down())
4881                                 mwindow->move_right(get_w() / 20);
4882                         else
4883                                 mwindow->move_right(get_w() / 5);
4884                 }
4885                 else {
4886                         switch(mwindow->edl->session->editing_mode) {
4887 // Test handles and resource boundaries and highlight a track
4888                         case EDITING_ARROW: {
4889                                 if( do_transitions(cursor_x, cursor_y,
4890                                                 1, new_cursor, update_cursor) ) break;
4891
4892                                 if( do_keyframes(cursor_x, cursor_y,
4893                                         0, get_buttonpress(), new_cursor,
4894                                         update_cursor, rerender) ) break;
4895 // Test edit boundaries
4896                                 if( do_edit_handles(cursor_x, cursor_y,
4897                                         1, rerender, update_overlay, new_cursor,
4898                                         update_cursor) ) break;
4899 // Test plugin boundaries
4900                                 if( do_plugin_handles(cursor_x, cursor_y,
4901                                         1, rerender, update_overlay, new_cursor,
4902                                         update_cursor) ) break;
4903
4904                                 if( do_edits(cursor_x, cursor_y, 1, 0,
4905                                         update_cursor, rerender, new_cursor,
4906                                         update_cursor) ) break;
4907
4908                                 if( do_plugins(cursor_x, cursor_y, 0, 1,
4909                                         update_cursor, rerender) ) break;
4910
4911                                 if( test_resources(cursor_x, cursor_y) ) break;
4912
4913                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
4914
4915                                 result = 0;
4916                                 break; }
4917
4918 // Test handles only and select a region
4919                         case EDITING_IBEAM: {
4920                                 double position = mwindow->edl->get_cursor_position(cursor_x, pane->number);
4921 //printf("TrackCanvas::button_press_event %d\n", position);
4922
4923                                 if( do_transitions(cursor_x, cursor_y,
4924                                                 1, new_cursor, update_cursor)) break;
4925                                 if(do_keyframes(cursor_x, cursor_y,
4926                                         0, get_buttonpress(), new_cursor,
4927                                         update_cursor, rerender)) {
4928                                         update_overlay = 1;
4929                                         break;
4930                                 }
4931 // Test edit boundaries
4932                                 if( do_edit_handles(cursor_x, cursor_y,
4933                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
4934 // Test plugin boundaries
4935                                 if( do_plugin_handles(cursor_x, cursor_y,
4936                                         1, rerender, update_overlay, new_cursor, update_cursor) ) break;
4937
4938                                 if( do_edits(cursor_x, cursor_y,
4939                                         1, 0, update_cursor, rerender, new_cursor, update_cursor) ) break;
4940
4941                                 if( do_plugins(cursor_x, cursor_y, 0, 1, update_cursor, rerender) ) break;
4942
4943                                 if( do_tracks(cursor_x, cursor_y, 1) ) break;
4944 // Highlight selection
4945                                 if( get_buttonpress() != LEFT_BUTTON ) break;
4946                                 rerender = start_selection(position);
4947                                 mwindow->session->current_operation = SELECT_REGION;
4948                                 update_cursor = 1;
4949                                 break; }
4950                         }
4951                 }
4952
4953                 if( rerender ) {
4954                         gui->unlock_window();
4955                         mwindow->cwindow->update(1, 0, 0, 0, 1);
4956                         gui->lock_window("TrackCanvas::button_press_event 2");
4957 // Update faders
4958                         mwindow->update_plugin_guis();
4959                         gui->update_patchbay();
4960                 }
4961
4962                 if( update_overlay ) {
4963                         gui->draw_overlays(1);
4964                 }
4965                 if( update_cursor < 0 ) {
4966 // double_click edit
4967                         gui->swindow->update_selection();
4968                 }
4969                 if( update_cursor ) {
4970                         gui->update_timebar(0);
4971                         gui->hide_cursor(0);
4972                         gui->show_cursor(1);
4973                         gui->zoombar->update();
4974                         gui->flash_canvas(1);
4975                 }
4976         }
4977
4978         return result;
4979 }
4980
4981 int TrackCanvas::start_selection(double position)
4982 {
4983         int rerender = 0;
4984         position = mwindow->edl->align_to_frame(position, 1);
4985
4986
4987 // Extend a border
4988         if(shift_down())
4989         {
4990                 double midpoint = (mwindow->edl->local_session->get_selectionstart(1) +
4991                         mwindow->edl->local_session->get_selectionend(1)) / 2;
4992
4993                 if(position < midpoint)
4994                 {
4995                         mwindow->edl->local_session->set_selectionstart(position);
4996                         selection_midpoint = mwindow->edl->local_session->get_selectionend(1);
4997 // Que the CWindow
4998                         rerender = 1;
4999                 }
5000                 else
5001                 {
5002                         mwindow->edl->local_session->set_selectionend(position);
5003                         selection_midpoint = mwindow->edl->local_session->get_selectionstart(1);
5004 // Don't que the CWindow for the end
5005                 }
5006         }
5007         else
5008 // Start a new selection
5009         {
5010 //printf("TrackCanvas::start_selection %f\n", position);
5011                 mwindow->edl->local_session->set_selectionstart(position);
5012                 mwindow->edl->local_session->set_selectionend(position);
5013                 selection_midpoint = position;
5014 // Que the CWindow
5015                 rerender = 1;
5016         }
5017
5018         return rerender;
5019 }
5020
5021 void TrackCanvas::end_edithandle_selection()
5022 {
5023         mwindow->modify_edithandles();
5024 }
5025
5026 void TrackCanvas::end_pluginhandle_selection()
5027 {
5028         mwindow->modify_pluginhandles();
5029 }
5030
5031
5032 double TrackCanvas::time_visible()
5033 {
5034         return (double)get_w() *
5035                 mwindow->edl->local_session->zoom_sample /
5036                 mwindow->edl->session->sample_rate;
5037 }
5038
5039
5040 void TrackCanvas::show_message(Auto *current, int show_curve_type, const char *fmt, ...)
5041 {
5042         char string[BCTEXTLEN];
5043         char *cp = string, *ep = cp + sizeof(string)-1;
5044         if( show_curve_type ) {
5045                 cp += snprintf(string, ep-cp, "%-8s ",
5046                         FloatAuto::curve_name(((FloatAuto*)current)->curve_mode));
5047         }
5048         char string2[BCTEXTLEN];
5049         Units::totext(string2,
5050                 current->autos->track->from_units(current->position),
5051                 mwindow->edl->session->time_format,
5052                 mwindow->edl->session->sample_rate,
5053                 mwindow->edl->session->frame_rate,
5054                 mwindow->edl->session->frames_per_foot);
5055         cp += snprintf(cp, ep-cp, "%s", string2);
5056         va_list ap;
5057         va_start(ap, fmt);
5058         vsnprintf(cp, ep-cp, fmt, ap);
5059         va_end(ap);
5060         gui->show_message(string);
5061 }
5062
5063 // Patchbay* TrackCanvas::get_patchbay()
5064 // {
5065 //      if(pane->patchbay) return pane->patchbay;
5066 //      if(gui->total_panes() == 2 &&
5067 //              gui->pane[TOP_LEFT_PANE] &&
5068 //              gui->pane[TOP_RIGHT_PANE])
5069 //              return gui->pane[TOP_LEFT_PANE]->patchbay;
5070 //      if(gui->total_panes() == 4)
5071 //      {
5072 //              if(pane->number == TOP_RIGHT_PANE)
5073 //                      return gui->pane[TOP_LEFT_PANE]->patchbay;
5074 //              else
5075 //                      return gui->pane[BOTTOM_LEFT_PANE]->patchbay;
5076 //      }
5077 //
5078 //      return 0;
5079 // }
5080
5081