Make FloatAutos::get_values() always use a PLAY_FORWARD direction.
[cinelerra_cv/pmdumuid.git] / cinelerra / trackcanvas.C
blobb88d06d9900f3c4e2562a0ecaea9e98429b97796
1 #include "asset.h"
2 #include "autoconf.h"
3 #include "automation.h"
4 #include "bcsignals.h"
5 #include "bctimer.h"
6 #include "clip.h"
7 #include "colors.h"
8 #include "cplayback.h"
9 #include "cursors.h"
10 #include "cwindow.h"
11 #include "cwindowgui.h"
12 #include "edithandles.h"
13 #include "editpopup.h"
14 #include "edits.h"
15 #include "edl.h"
16 #include "edlsession.h"
17 #include "floatauto.h"
18 #include "floatautos.h"
19 #include "intauto.h"
20 #include "keyframe.h"
21 #include "keyframepopup.h"
22 #include "keyframes.h"
23 #include "keys.h"
24 #include "localsession.h"
25 #include "mainclock.h"
26 #include "maincursor.h"
27 #include "mainundo.h"
28 #include "maskautos.h"
29 #include "mbuttons.h"
30 #include "mtimebar.h"
31 #include "mwindow.h"
32 #include "mwindowgui.h"
33 #include "patchbay.h"
34 #include "tracking.h"
35 #include "panautos.h"
36 #include "resourcethread.h"
37 #include "playbackengine.h"
38 #include "playtransport.h"
39 #include "plugin.h"
40 #include "pluginpopup.h"
41 #include "pluginset.h"
42 #include "pluginserver.h"
43 #include "plugintoggles.h"
44 #include "preferences.h"
45 #include "resourcepixmap.h"
46 #include "mainsession.h"
47 #include "transitionhandles.h"
48 #include "transitionpopup.h"
49 #include "transportque.h"
50 #include "zoombar.h"
51 #include "theme.h"
52 #include "intautos.h"
53 #include "trackcanvas.h"
54 #include "tracks.h"
55 #include "transition.h"
56 #include "vframe.h"
57 #include "apatchgui.inc"
58 #include "vpatchgui.inc"
60 #include <string.h>
62 TrackCanvas::TrackCanvas(MWindow *mwindow, MWindowGUI *gui)
63  : BC_SubWindow(mwindow->theme->mcanvas_x,
64         mwindow->theme->mcanvas_y,
65         gui->view_w,
66         gui->view_h)
68         this->mwindow = mwindow;
69         this->gui = gui;
70         current_end = 0;
71         selection_midpoint1 = selection_midpoint2 = 0;
72         selection_type = 0;
73         region_selected = 0;
74         handle_selected = 0;
75         auto_selected = 0;
76         translate_selected = 0;
77         which_handle = 0;
78         handle_pixel = 0;
79         drag_scroll = 0;
80         drag_popup = 0;
81         active = 0;
82         temp_picon = 0;
83         resource_timer = new Timer;
84         hourglass_enabled = 0;
85         resource_thread = new ResourceThread(mwindow);
88 TrackCanvas::~TrackCanvas()
90         for(int i = 0; i < resource_pixmaps.total; i++)
91                 delete resource_pixmaps.values[i];
92 //      delete transition_handles;
93         delete edit_handles;
94         delete keyframe_pixmap;
95         delete camerakeyframe_pixmap;
96         delete modekeyframe_pixmap;
97         delete pankeyframe_pixmap;
98         delete projectorkeyframe_pixmap;
99         delete maskkeyframe_pixmap;
100         delete background_pixmap;
101         if(temp_picon) delete temp_picon;
102         delete resource_timer;
105 int TrackCanvas::create_objects()
107         background_pixmap = new BC_Pixmap(this, get_w(), get_h());
108 //      transition_handles = new TransitionHandles(mwindow, this);
109         edit_handles = new EditHandles(mwindow, this);
110         keyframe_pixmap = new BC_Pixmap(this, mwindow->theme->keyframe_data, PIXMAP_ALPHA);
111         camerakeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->camerakeyframe_data, PIXMAP_ALPHA);
112         modekeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->modekeyframe_data, PIXMAP_ALPHA);
113         pankeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->pankeyframe_data, PIXMAP_ALPHA);
114         projectorkeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->projectorkeyframe_data, PIXMAP_ALPHA);
115         maskkeyframe_pixmap = new BC_Pixmap(this, mwindow->theme->maskkeyframe_data, PIXMAP_ALPHA);
116         resource_thread->create_objects();
117         draw();
118         update_cursor();
119         flash();
120         return 0;
123 void TrackCanvas::resize_event()
125 //printf("TrackCanvas::resize_event 1\n");
126         draw(0, 0);
127         flash();
128 //printf("TrackCanvas::resize_event 2\n");
131 int TrackCanvas::keypress_event()
133         int result = 0;
136         return result;
139 int TrackCanvas::drag_motion()
141         int cursor_x = get_relative_cursor_x();
142         int cursor_y = get_relative_cursor_y();
143         Track *over_track = 0;
144         Edit *over_edit = 0;
145         PluginSet *over_pluginset = 0;
146         Plugin *over_plugin = 0;
147         int redraw = 0;
150         if(drag_popup)
151         {
152                 drag_popup->cursor_motion_event();
153         }
156 // there's no point in drawing highlights has until drag operation has been set
157         if (!mwindow->session->current_operation)
158                 return 0;
160         if(get_cursor_over_window() &&
161                 cursor_x >= 0 && 
162                 cursor_y >= 0 && 
163                 cursor_x < get_w() && 
164                 cursor_y < get_h())
165         {
166 // Find the edit and track the cursor is over
167                 for(Track *track = mwindow->edl->tracks->first; track; track = track->next)
168                 {
169                         int64_t track_x, track_y, track_w, track_h;
170                         track_dimensions(track, track_x, track_y, track_w, track_h);
172                         if(cursor_y >= track_y && 
173                                 cursor_y < track_y + track_h)
174                         {
175                                 over_track = track;
176                                 for(Edit *edit = track->edits->first; edit; edit = edit->next)
177                                 {
178                                         if (mwindow->session->current_operation != DRAG_ATRANSITION && 
179                                                 mwindow->session->current_operation != DRAG_VTRANSITION &&
180                                                 edit == track->edits->last) 
181                                                 break;  
182                                         int64_t edit_x, edit_y, edit_w, edit_h;
183                                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
185                                         if(cursor_x >= edit_x && 
186                                                 cursor_y >= edit_y && 
187                                                 cursor_x < edit_x + edit_w && 
188                                                 cursor_y < edit_y + edit_h)
189                                         {
190                                                 over_edit = edit;
191                                                 break;
192                                         }
193                                 }
195                                 for(int i = 0; i < track->plugin_set.total; i++)
196                                 {
197                                         PluginSet *pluginset = track->plugin_set.values[i];
198                                         
201                                         for(Plugin *plugin = (Plugin*)pluginset->first;
202                                                 plugin != pluginset->last;
203                                                 plugin = (Plugin*)plugin->next)
204                                         {
205                                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
206                                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
207                                                 
208                                                 if(cursor_y >= plugin_y &&
209                                                         cursor_y < plugin_y + plugin_h)
210                                                 {
211                                                         over_pluginset = plugin->plugin_set;
212                                                 
213                                                         if(cursor_x >= plugin_x &&
214                                                                 cursor_x < plugin_x + plugin_w)
215                                                         {
216                                                                 over_plugin = plugin;
217                                                                 break;
218                                                         }
219                                                 }
220                                         }
221                                 }
222                                 break;
223                         }
224                 }
225         }
227         if (!over_track)        // check for pastes from patchbay
228                 over_track = mwindow->gui->patchbay->is_over_track();
230         if(mwindow->session->track_highlighted != over_track) 
231         {
232                 mwindow->session->track_highlighted = over_track;
233                 redraw = 1;
234         }
236         if(mwindow->session->edit_highlighted != over_edit)
237         {
238                 mwindow->session->edit_highlighted = over_edit;
239                 redraw = 1;
240         }
242         if(mwindow->session->pluginset_highlighted != over_pluginset)
243         {
244                 mwindow->session->pluginset_highlighted = over_pluginset;
245                 redraw = 1;
246         }
248         if(mwindow->session->plugin_highlighted != over_plugin)
249         {
250                 mwindow->session->plugin_highlighted = over_plugin;
251                 redraw = 1;
252         }
254         if (mwindow->session->current_operation == DRAG_ASSET ||
255           mwindow->session->current_operation == DRAG_EDIT)
256         {
257                 redraw = 1;
258         }
260         if(redraw)
261         {
262                 lock_window("TrackCanvas::drag_motion");
263                 draw_overlays();
264                 flash();
265                 unlock_window();
266         }
268         return 0;
271 int TrackCanvas::drag_start_event()
273         int result = 0;
274         int redraw = 0;
275         int rerender = 0;
276         int new_cursor, update_cursor;
278         if(mwindow->session->current_operation != NO_OPERATION) return 0;
280         if(is_event_win())
281         {
282                 if(do_plugins(get_drag_x(), 
283                         get_drag_y(), 
284                         1,
285                         0,
286                         redraw,
287                         rerender))
288                 {
289                         result = 1;
290                 }
291                 else
292                 if(do_edits(get_drag_x(),
293                         get_drag_y(),
294                         0,
295                         1,
296                         redraw,
297                         rerender,
298                         new_cursor,
299                         update_cursor))
300                 {
301                         result = 1;
302                 }
303         }
305         return result;
308 int TrackCanvas::drag_motion_event()
310         return drag_motion();
313 int TrackCanvas::cursor_leave_event()
315 // Because drag motion calls get_cursor_over_window we can be sure that
316 // all highlights get deleted now.
317 // This ended up blocking keyboard input from the drag operations.
318         return 0;
319 //      return drag_motion();
323 int TrackCanvas::drag_stop_event()
325         int result = drag_stop();
327         if(drag_popup)
328         {
329                 delete drag_popup;
330                 drag_popup = 0;
331         }
332         return result;
336 int TrackCanvas::drag_stop()
338 // In most cases the editing routine redraws and not the drag_stop
339         int result = 0, redraw = 0;
341         int insertion = 0;           // used in drag and drop mode
342         switch(mwindow->session->current_operation)
343         {
344                 case DRAG_VTRANSITION:
345                 case DRAG_ATRANSITION:
346                         if(mwindow->session->edit_highlighted)
347                         {
348                                 if((mwindow->session->current_operation == DRAG_ATRANSITION &&
349                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
350                                         (mwindow->session->current_operation == DRAG_VTRANSITION &&
351                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO))
352                                 {
353                                         mwindow->session->current_operation = NO_OPERATION;
354                                         mwindow->paste_transition();
355                                         result = 1;
356                                 }
357                         }
358                         redraw = 1;
359                         break;
364 // Behavior for dragged plugins is limited by the fact that a shared plugin
365 // can only refer to a standalone plugin that exists in the same position in
366 // time.  Dragging a plugin from one point in time to another can't produce
367 // a shared plugin to the original plugin.  In this case we relocate the
368 // plugin instead of sharing it.
369                 case DRAG_AEFFECT_COPY:
370                 case DRAG_VEFFECT_COPY:
371                         if(mwindow->session->track_highlighted &&
372                                 ((mwindow->session->current_operation == DRAG_AEFFECT_COPY &&
373                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
374                                         (mwindow->session->current_operation == DRAG_VEFFECT_COPY &&
375                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO)))
376                         {
377                                 mwindow->session->current_operation = NO_OPERATION;
379 // Insert shared plugin in source
380                                 if(mwindow->session->track_highlighted != mwindow->session->drag_plugin->track &&
381                                         !mwindow->session->plugin_highlighted &&
382                                         !mwindow->session->pluginset_highlighted)
383                                 {
384 // Move plugin if different startproject
385                                         mwindow->move_effect(mwindow->session->drag_plugin,
386                                                 0,
387                                                 mwindow->session->track_highlighted,
388                                                 0);
389                                         result = 1;
390                                 }
391                                 else
392 // Move source to different location
393                                 if(mwindow->session->pluginset_highlighted)
394                                 {
395 //printf("TrackCanvas::drag_stop 6\n");
396                                         if(mwindow->session->plugin_highlighted)
397                                         {
398                                                 mwindow->move_effect(mwindow->session->drag_plugin,
399                                                         mwindow->session->plugin_highlighted->plugin_set,
400                                                         0,
401                                                         mwindow->session->plugin_highlighted->startproject);
402                                         }
403                                         else
404                                         {
405                                                 mwindow->move_effect(mwindow->session->drag_plugin,
406                                                         mwindow->session->pluginset_highlighted,
407                                                         0,
408                                                         mwindow->session->pluginset_highlighted->last->startproject);
409                                         }
410                                         result = 1;
411                                 }
412                                 else
413 // Move to a new plugin set between two edits
414                                 if(mwindow->session->edit_highlighted)
415                                 {
416                                         mwindow->move_effect(mwindow->session->drag_plugin,
417                                                 0,
418                                                 mwindow->session->track_highlighted,
419                                                 mwindow->session->edit_highlighted->startproject);
420                                         result = 1;
421                                 }
422                                 else
423 // Move to a new plugin set
424                                 if(mwindow->session->track_highlighted)
425                                 {
426                                         mwindow->move_effect(mwindow->session->drag_plugin,
427                                                 0,
428                                                 mwindow->session->track_highlighted,
429                                                 0);
430                                         result = 1;
431                                 }
432                         }
433                         break;
435                 case DRAG_AEFFECT:
436                 case DRAG_VEFFECT:
437                         if(mwindow->session->track_highlighted && 
438                                 ((mwindow->session->current_operation == DRAG_AEFFECT &&
439                                 mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
440                                 (mwindow->session->current_operation == DRAG_VEFFECT &&
441                                 mwindow->session->track_highlighted->data_type == TRACK_VIDEO)))
442                         {
443 // Drop all the effects
444                                 PluginSet *plugin_set = mwindow->session->pluginset_highlighted;
445                                 Track *track = mwindow->session->track_highlighted;
446                                 double start = 0;
447                                 double length = track->get_length();
449                                 if(mwindow->session->plugin_highlighted)
450                                 {
451                                         start = track->from_units(mwindow->session->plugin_highlighted->startproject);
452                                         length = track->from_units(mwindow->session->plugin_highlighted->length);
453                                         if(length <= 0) length = track->get_length();
454                                 }
455                                 else
456                                 if(mwindow->session->pluginset_highlighted)
457                                 {
458                                         start = track->from_units(plugin_set->last->startproject);
459                                         length = track->get_length() - start;
460                                         if(length <= 0) length = track->get_length();
461                                 }
462                                 else
463                                 if(mwindow->edl->local_session->get_selectionend() > 
464                                         mwindow->edl->local_session->get_selectionstart())
465                                 {
466                                         start = mwindow->edl->local_session->get_selectionstart();
467                                         length = mwindow->edl->local_session->get_selectionend() - 
468                                                 mwindow->edl->local_session->get_selectionstart();
469                                 }
470 // Move to a point between two edits
471                                 else
472                                 if(mwindow->session->edit_highlighted)
473                                 {
474                                         start = mwindow->session->track_highlighted->from_units(
475                                                 mwindow->session->edit_highlighted->startproject);
476                                         length = mwindow->session->track_highlighted->from_units(
477                                                 mwindow->session->edit_highlighted->length);
478                                 }
480                                 mwindow->insert_effects_canvas(start, length);
481                                 redraw = 1;
482                         }
483                         if (mwindow->session->track_highlighted)
484                                 result = 1;  // we have to cleanup
485                         break;
487                 case DRAG_ASSET:
488                         if(mwindow->session->track_highlighted)
489                         {
490                                 float asset_length_float;
491                                 int64_t asset_length_units;
492                                 int64_t position = 0;
493                                         
494                                 if(mwindow->session->current_operation == DRAG_ASSET &&
495                                         mwindow->session->drag_assets->total)
496                                 {
497                                         Asset *asset = mwindow->session->drag_assets->values[0];
498                                         // we use video if we are over video and audio if we are over audio
499                                         if (asset->video_data && mwindow->session->track_highlighted->data_type == TRACK_VIDEO)
500                                         {
501                                                 // Images have length -1
502                                                 double video_length = asset->video_length;
503                                                 if (video_length < 0)
504                                                 {
505                                                         if(mwindow->edl->session->si_useduration)
506                                                                 video_length = mwindow->edl->session->si_duration;
507                                                         else    
508                                                                 video_length = 1.0 / mwindow->edl->session->frame_rate ; 
509                                                 }
510                                                 asset_length_float = video_length / asset->frame_rate;
511                                         }
512                                         else if (asset->audio_data && mwindow->session->track_highlighted->data_type == TRACK_AUDIO)
513                                                 asset_length_float = asset->audio_length / asset->sample_rate;
514                                         else
515                                         {
516                                                 result = 1;
517                                                 break;  // Do not do anything
518                                         }
519                                 } else
520                                 if(mwindow->session->current_operation == DRAG_ASSET &&
521                                         mwindow->session->drag_clips->total)
522                                 {
523                                         EDL *clip = mwindow->session->drag_clips->values[0];
524                                         asset_length_float = clip->tracks->total_length();
525                                 } else
526                                 {
527                                         printf("DRAG_ASSET error: Asset dropped, but both drag_clips and drag_assets total is zero\n");
528                                 }
529                         
530                                 asset_length_units = mwindow->session->track_highlighted->to_units(asset_length_float, 0);
531                                 position = get_drop_position (&insertion, NULL, asset_length_units);
532                                 if (position == -1)
533                                 {
534                                         result = 1;
535                                         break;          // Do not do anything
536                                 }
537                                 
538                                 double position_f = mwindow->session->track_highlighted->from_units(position);
539                                 Track *track = mwindow->session->track_highlighted;
541                                 if (!insertion)
542                                 {
543                                         // FIXME, we should create an mwindow/EDL method that overwrites, without clearing the keyframes and autos
544                                         // Unfortunately, this is _a lot_ of work to do right
545                                         mwindow->edl->tracks->clear(position_f, 
546                                                 position_f + asset_length_float, 0);
547                                 }
548                                 mwindow->paste_assets(position_f, track);
549                                 result = 1;    // need to be one no matter what, since we have track highlited so we have to cleanup....
550                         }
551                         break;
553                 case DRAG_EDIT:
554                         mwindow->session->current_operation = NO_OPERATION;
555                         if(mwindow->session->track_highlighted)
556                         {
557                                 if(mwindow->session->track_highlighted->data_type == mwindow->session->drag_edit->track->data_type)
558                                 {
559                                         int64_t position = 0;
560                                 
561                                         position = get_drop_position (&insertion, mwindow->session->drag_edit, mwindow->session->drag_edit->length);
563                                         if (position == -1)
564                                         {
565                                                 result = 1;
566                                                 break;          // Do not do anything
567                                         }
568                                         
569                                         double position_f = mwindow->session->track_highlighted->from_units(position);
570                                         Track *track = mwindow->session->track_highlighted;
571                                         mwindow->move_edits(mwindow->session->drag_edits,
572                                                 track,
573                                                 position_f,
574                                                 !insertion);
575                                 }
577                                 result = 1;
578                         }
579                         break;
580         }
582 // since we don't have subwindows we have to terminate any drag operation
583         if(result)
584         {
585                 if (mwindow->session->track_highlighted
586                         || mwindow->session->edit_highlighted
587                         || mwindow->session->plugin_highlighted
588                         || mwindow->session->pluginset_highlighted) 
589                         redraw = 1;
590                 mwindow->session->track_highlighted = 0;
591                 mwindow->session->edit_highlighted = 0;
592                 mwindow->session->plugin_highlighted = 0;
593                 mwindow->session->pluginset_highlighted = 0;
594                 mwindow->session->current_operation = NO_OPERATION;
595         }
598 //printf("TrackCanvas::drag_stop %d %d\n", redraw, mwindow->session->current_operation);
599         if(redraw)
600         {
601                 mwindow->edl->tracks->update_y_pixels(mwindow->theme);
602                 gui->get_scrollbars();
603                 draw();
604                 gui->patchbay->update();
605                 gui->cursor->update();
606                 flash();
607                 flush();
608         }
610         return result;
614 int64_t TrackCanvas::get_drop_position (int *is_insertion, Edit *moved_edit, int64_t moved_edit_length)
616         *is_insertion = 0;
618         // get the canvas/track position
619         int cursor_x = get_relative_cursor_x();
620         double pos = (double)cursor_x * 
621                 mwindow->edl->local_session->zoom_sample / 
622                 mwindow->edl->session->sample_rate + 
623                 (double)mwindow->edl->local_session->view_start * 
624                 mwindow->edl->local_session->zoom_sample /
625                 mwindow->edl->session->sample_rate;
626         // convert to track's units to operate with them
627         Track *track = mwindow->session->track_highlighted;
628         // cursor relative position - depending on where we started the drag inside the edit
629         int64_t cursor_position;
630         if (moved_edit)  // relative cursor position depends upon grab point
631                 cursor_position = track->to_units (pos - (mwindow->session->drag_position - moved_edit->track->from_units(moved_edit->startproject)), 1);
632         else             // for clips and assets acts as they were grabbed in the middle
633                 cursor_position = track->to_units (pos , 1) - moved_edit_length / 2;
634            
635         // we use real cursor position for affinity calculations
636         int64_t real_cursor_position = track->to_units (pos, 0); 
637         if (cursor_position < 0) cursor_position = 0;
638         if (real_cursor_position < 0) real_cursor_position = 0;
639         int64_t position = -1;
640         int64_t span_start = 0;
641         int64_t span_length = 0;
642         int span_asset = 0;
643         int last_ignore = 0; // used to make sure we can ignore the last edit if that is what we are dragging
645         if (!track->edits->last)
646         {
647                 // No edits -> no problems!
648                 position = cursor_position;
649         }
650         else
651         {
652                 Edit *fake_edit = new Edit(mwindow->edl, track);
653                 int last2 = 0; // last2 is a hack that let us make virtual edits at the end so thing works for last edit also
654                                // we do this by appending two VERY long virtual edits at the end
655                 
656                 for (Edit *edit = track->edits->first; edit || last2 < 2; )
657                 {
658                 
659                         if (!edit && last_ignore)
660                         {
661                                 span_length += 100000000000000LL;
662                                 last_ignore = 0;
663                                 span_asset = 0;
664                         } else
665                         if (edit && 
666                             ((moved_edit && edit == moved_edit && edit->previous && !edit->previous->asset) ||
667                             (moved_edit && edit->previous == moved_edit  && !edit->asset)))
668                         {
669                                 span_length += edit->length;        // our fake edit spans over the edit we are moving
670                                 last_ignore = 1;
671                         } else
672                         { // This is a virtual edit
673                                 fake_edit->startproject = span_start;
674                                 fake_edit->length = span_length;
675                                 int64_t edit_x, edit_y, edit_w, edit_h;
676                                 edit_dimensions(fake_edit, edit_x, edit_y, edit_w, edit_h);
677                                 if (labs(edit_x - cursor_x) < HANDLE_W)                 // cursor is close to the beginning of an edit -> insertion
678                                 {
679                                         *is_insertion = 1;
680                                         position = span_start;
681                                 } else
682                                 if (labs(edit_x + edit_w - cursor_x) < HANDLE_W)        // cursor is close to the end of an edit -> insertion
683                                 {
684                                         *is_insertion = 1;
685                                         position = span_start + span_length;
687                                 }  else
688                                 if (!span_asset &&              // we have enough empty space to position the edit where user wants 
689                                         span_start <= cursor_position &&
690                                         span_start + span_length >= cursor_position + moved_edit_length)
691                                 {
692                                         position = cursor_position; 
693                                 } else
694                                 if (!span_asset &                               // we are inside an empty edit, but cannot push the edit as far as user wants, so 'resist moving it further'
695                                         real_cursor_position >= span_start && 
696                                         real_cursor_position < span_start + span_length && 
697                                         span_length >= moved_edit_length)
698                                 {
699                                         if (llabs(real_cursor_position - span_start) < llabs(real_cursor_position - span_start - span_length))
700                                                 position = span_start;
701                                         else
702                                                 position = span_start + span_length - moved_edit_length;
703                                 } else
704                                 if (cursor_x > edit_x && cursor_x <= edit_x + edit_w / 2) // we are inside an nonempty edit, - snap to left
705                                 {
706                                         *is_insertion = 1;
707                                         position = span_start;                          
708                                 } else
709                                 if (cursor_x > edit_x + edit_w / 2 && cursor_x <= edit_x + edit_w) // we are inside an nonempty edit, - snap to right
710                                 {
711                                         *is_insertion = 1;
712                                         position = span_start + span_length;                            
713                                 }                               
714                                 
716                                 if (position != -1) 
717                                         break;
718                                 
719                                 // This is the new edit
720                                 if (edit)
721                                 {
722                                                 span_length = edit->length;             
723                                                 span_start = edit->startproject;  
724                                                 last_ignore = 0;
725                                                 if (!edit->asset || (!moved_edit || moved_edit == edit)) 
726                                                 {
727                                                         if (moved_edit && moved_edit == edit)
728                                                                 last_ignore = 1;
729                 
730                                                         span_asset = 0;
731                                                 } else 
732                                                         span_asset = 1;
733                                 } else
734                                 {
735                                         span_start = span_length + span_start;
736                                         span_length = 100000000000000LL;
737                                         span_asset = 0;
738                                 };
739                                 
741                         }
742                         if (edit)
743                                 edit = edit->next;
744                         else
745                                 last2++;
746                         
747                 }
748                 delete fake_edit;
750         }
751         if (real_cursor_position == 0) 
752         {
753                 position = 0;
754                 *is_insertion = 1;
755         }
756 //      printf("rcp: %lli, position: %lli, insertion: %i\n", real_cursor_position, position, *is_insertion);
757         return position;
762 void TrackCanvas::draw(int mode, int hide_cursor)
764 // Swap pixmap layers
765         if(get_w() != background_pixmap->get_w() ||
766                 get_h() != background_pixmap->get_h())
767         {
768                 delete background_pixmap;
769                 background_pixmap = new BC_Pixmap(this, get_w(), get_h());
770         }
772 // Cursor disappears after resize when this is called.
773 // Cursor doesn't redraw after editing when this isn't called.
774         if(gui->cursor && hide_cursor) gui->cursor->hide();
775         draw_top_background(get_parent(), 0, 0, get_w(), get_h(), background_pixmap);
776         draw_resources(mode);
777         draw_overlays();
780 void TrackCanvas::update_cursor()
782         switch(mwindow->edl->session->editing_mode)
783         {
784                 case EDITING_ARROW: set_cursor(ARROW_CURSOR); break;
785                 case EDITING_IBEAM: set_cursor(IBEAM_CURSOR); break;
786         }
790 void TrackCanvas::test_timer()
792         if(resource_timer->get_difference() > 1000 && 
793                 !hourglass_enabled)
794         {
795                 start_hourglass();
796                 hourglass_enabled = 1;
797         }
801 void TrackCanvas::draw_indexes(Asset *asset)
803 // Don't redraw raw samples
804         if(asset->index_zoom > mwindow->edl->local_session->zoom_sample)
805                 return;
807         draw_resources(0, 1, asset);
809         draw_overlays();
810         draw_automation();
811         flash();
812         flush();
815 void TrackCanvas::draw_resources(int mode, 
816         int indexes_only, 
817         Asset *index_asset)
819         if(!mwindow->edl->session->show_assets) return;
821         if(mode != 3 && !indexes_only)
822                 resource_thread->stop_draw(!indexes_only);
824         resource_timer->update();
826 // Age resource pixmaps for deletion
827         if(!indexes_only)
828                 for(int i = 0; i < resource_pixmaps.total; i++)
829                         resource_pixmaps.values[i]->visible--;
831         if(mode == 2)
832                 resource_pixmaps.remove_all_objects();
835 // Search every edit
836         for(Track *current = mwindow->edl->tracks->first;
837                 current;
838                 current = NEXT)
839         {
840                 for(Edit *edit = current->edits->first; edit; edit = edit->next)
841                 {
842                         if(!edit->asset) continue;
843                         if(indexes_only)
844                         {
845                                 if(edit->track->data_type != TRACK_AUDIO) continue;
846                                 if(!edit->asset->test_path(index_asset->path)) continue;
847                         }
849                         int64_t edit_x, edit_y, edit_w, edit_h;
850                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
852 // Edit is visible
853                         if(MWindowGUI::visible(edit_x, edit_x + edit_w, 0, get_w()) &&
854                                 MWindowGUI::visible(edit_y, edit_y + edit_h, 0, get_h()))
855                         {
856                                 int64_t pixmap_x, pixmap_w, pixmap_h;
858 // Search for existing pixmap containing edit
859                                 for(int i = 0; i < resource_pixmaps.total; i++)
860                                 {
861                                         ResourcePixmap* pixmap = resource_pixmaps.values[i];
862 // Same pointer can be different edit if editing took place
863                                         if(pixmap->edit_id == edit->id)
864                                         {
865                                                 pixmap->visible = 1;
866                                                 break;
867                                         }
868                                 }
870 // Get new size, offset of pixmap needed
871                                 get_pixmap_size(edit, 
872                                         edit_x, 
873                                         edit_w, 
874                                         pixmap_x, 
875                                         pixmap_w, 
876                                         pixmap_h);
878 // Draw new data
879                                 if(pixmap_w && pixmap_h)
880                                 {
881 // Create pixmap if it doesn't exist
882                                         ResourcePixmap* pixmap = create_pixmap(edit, 
883                                                 edit_x, 
884                                                 pixmap_x, 
885                                                 pixmap_w, 
886                                                 pixmap_h);
887 // Resize it if it's bigger
888                                         if(pixmap_w > pixmap->pixmap_w ||
889                                                 pixmap_h > pixmap->pixmap_h)
890                                                 pixmap->resize(pixmap_w, pixmap_h);
891                                         pixmap->draw_data(edit,
892                                                 edit_x, 
893                                                 edit_w, 
894                                                 pixmap_x, 
895                                                 pixmap_w, 
896                                                 pixmap_h, 
897                                                 mode,
898                                                 indexes_only);
899 // Resize it if it's smaller
900                                         if(pixmap_w < pixmap->pixmap_w ||
901                                                 pixmap_h < pixmap->pixmap_h)
902                                                 pixmap->resize(pixmap_w, pixmap_h);
904 // Copy pixmap to background canvas
905                                         background_pixmap->draw_pixmap(pixmap, 
906                                                 pixmap->pixmap_x, 
907                                                 current->y_pixel,
908                                                 pixmap->pixmap_w,
909                                                 edit_h);
910                                 }
911                         }
912                 }
913         }
915 // Delete unused pixmaps
916         if(!indexes_only)
917                 for(int i = resource_pixmaps.total - 1; i >= 0; i--)
918                         if(resource_pixmaps.values[i]->visible < -5)
919                         {
920                                 delete resource_pixmaps.values[i];
921                                 resource_pixmaps.remove(resource_pixmaps.values[i]);
922                         }
924         if(hourglass_enabled) 
925         {
926                 stop_hourglass();
927                 hourglass_enabled = 0;
928         }
930         if(mode != 3 && !indexes_only)
931                 resource_thread->start_draw();
934 ResourcePixmap* TrackCanvas::create_pixmap(Edit *edit, 
935         int64_t edit_x, 
936         int64_t pixmap_x, 
937         int64_t pixmap_w, 
938         int64_t pixmap_h)
940         ResourcePixmap *result = 0;
942         for(int i = 0; i < resource_pixmaps.total; i++)
943         {
944 //printf("TrackCanvas::create_pixmap 1 %d %d\n", edit->id, resource_pixmaps.values[i]->edit->id);
945                 if(resource_pixmaps.values[i]->edit_id == edit->id) 
946                 {
947                         result = resource_pixmaps.values[i];
948                         break;
949                 }
950         }
952         if(!result)
953         {
954 //printf("TrackCanvas::create_pixmap 2\n");
955                 result = new ResourcePixmap(mwindow, 
956                         this, 
957                         edit, 
958                         pixmap_w, 
959                         pixmap_h);
960                 resource_pixmaps.append(result);
961         }
963 //      result->resize(pixmap_w, pixmap_h);
964         return result;
967 void TrackCanvas::get_pixmap_size(Edit *edit, 
968         int64_t edit_x, 
969         int64_t edit_w, 
970         int64_t &pixmap_x, 
971         int64_t &pixmap_w,
972         int64_t &pixmap_h)
975 // Align x on frame boundaries
978 //      switch(edit->edits->track->data_type)
979 //      {
980 //              case TRACK_AUDIO:
982                         pixmap_x = edit_x;
983                         pixmap_w = edit_w;
984                         if(pixmap_x < 0)
985                         {
986                                 pixmap_w -= -edit_x;
987                                 pixmap_x = 0;
988                         }
990                         if(pixmap_x + pixmap_w > get_w())
991                         {
992                                 pixmap_w = get_w() - pixmap_x;
993                         }
995 //                      break;
996 // 
997 //              case TRACK_VIDEO:
998 //              {
999 //                      int64_t picon_w = (int64_t)(edit->picon_w() + 0.5);
1000 //                      int64_t frame_w = (int64_t)(edit->frame_w() + 0.5);
1001 //                      int64_t pixel_increment = MAX(picon_w, frame_w);
1002 //                      int64_t pixmap_x1 = edit_x;
1003 //                      int64_t pixmap_x2 = edit_x + edit_w;
1004 // 
1005 //                      if(pixmap_x1 < 0)
1006 //                      {
1007 //                              pixmap_x1 = (int64_t)((double)-edit_x / pixel_increment) * 
1008 //                                      pixel_increment + 
1009 //                                      edit_x;
1010 //                      }
1011 // 
1012 //                      if(pixmap_x2 > get_w())
1013 //                      {
1014 //                              pixmap_x2 = (int64_t)((double)(get_w() - edit_x) / pixel_increment + 1) * 
1015 //                                      pixel_increment + 
1016 //                                      edit_x;
1017 //                      }
1018 //                      pixmap_x = pixmap_x1;
1019 //                      pixmap_w = pixmap_x2 - pixmap_x1;
1020 //                      break;
1021 //              }
1022 //      }
1024         pixmap_h = mwindow->edl->local_session->zoom_track;
1025         if(mwindow->edl->session->show_titles) pixmap_h += mwindow->theme->get_image("title_bg_data")->get_h();
1026 //printf("get_pixmap_size %d %d %d %d\n", edit_x, edit_w, pixmap_x, pixmap_w);
1029 void TrackCanvas::edit_dimensions(Edit *edit, 
1030         int64_t &x, 
1031         int64_t &y, 
1032         int64_t &w, 
1033         int64_t &h)
1035 //      w = Units::round(edit->track->from_units(edit->length) * 
1036 //              mwindow->edl->session->sample_rate / 
1037 //              mwindow->edl->local_session->zoom_sample);
1039         h = resource_h();
1041         x = Units::round(edit->track->from_units(edit->startproject) * 
1042                         mwindow->edl->session->sample_rate /
1043                         mwindow->edl->local_session->zoom_sample - 
1044                         mwindow->edl->local_session->view_start);
1046 // Method for calculating w so when edits are together we never get off by one error due to rounding
1047         int64_t x_next = Units::round(edit->track->from_units(edit->startproject + edit->length) * 
1048                         mwindow->edl->session->sample_rate /
1049                         mwindow->edl->local_session->zoom_sample - 
1050                         mwindow->edl->local_session->view_start);
1051         w = x_next - x;
1053         y = edit->edits->track->y_pixel;
1055         if(mwindow->edl->session->show_titles) 
1056                 h += mwindow->theme->get_image("title_bg_data")->get_h();
1059 void TrackCanvas::track_dimensions(Track *track, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1061         x = 0;
1062         w = get_w();
1063         y = track->y_pixel;
1064         h = track->vertical_span(mwindow->theme);
1068 void TrackCanvas::draw_paste_destination()
1070         int current_atrack = 0;
1071         int current_vtrack = 0;
1072         int current_aedit = 0;
1073         int current_vedit = 0;
1074         int64_t w = 0;
1075         int64_t x;
1076         double position;
1077         int insertion  = 0;
1080         if((mwindow->session->current_operation == DRAG_ASSET &&
1081                         (mwindow->session->drag_assets->total ||
1082                         mwindow->session->drag_clips->total)) ||
1083                 (mwindow->session->current_operation == DRAG_EDIT &&
1084                         mwindow->session->drag_edits->total))
1085         {
1087                 Asset *asset = 0;
1088                 EDL *clip = 0;
1089                 int draw_box = 0;
1091                 if(mwindow->session->current_operation == DRAG_ASSET &&
1092                         mwindow->session->drag_assets->total)
1093                         asset = mwindow->session->drag_assets->values[0];
1095                 if(mwindow->session->current_operation == DRAG_ASSET &&
1096                         mwindow->session->drag_clips->total)
1097                         clip = mwindow->session->drag_clips->values[0];
1099 // Get destination track
1100                 for(Track *dest = mwindow->session->track_highlighted; 
1101                         dest; 
1102                         dest = dest->next)
1103                 {
1104                         if(dest->record)
1105                         {
1106 // Get source width in pixels
1107                                 w = -1;
1109 // Use start of highlighted edit
1110                                 if(mwindow->session->edit_highlighted)
1111                                         position = mwindow->session->track_highlighted->from_units(
1112                                                 mwindow->session->edit_highlighted->startproject);
1113                                 else
1114 // Use end of highlighted track, disregarding effects
1115                                         position = mwindow->session->track_highlighted->from_units(
1116                                                 mwindow->session->track_highlighted->edits->last->startproject);
1119                                 if(dest->data_type == TRACK_AUDIO)
1120                                 {
1121                                         if(asset && current_atrack < asset->channels)
1122                                         {
1123                                                 w = Units::to_int64((double)asset->audio_length /
1124                                                         asset->sample_rate *
1125                                                         mwindow->edl->session->sample_rate / 
1126                                                         mwindow->edl->local_session->zoom_sample);
1128                                         // FIXME: more obvious, get_drop_position should be called only ONCE - for highlighted track
1129                                                 int64_t asset_length;
1130                                                 // we use video if we are over video and audio if we are over audio
1131                                                 if (asset->video_data && mwindow->session->track_highlighted->data_type == TRACK_VIDEO)
1132                                                 {
1133                                                         // Images have length -1
1134                                                         double video_length = asset->video_length;
1135                                                         if (video_length < 0)
1136                                                         {
1137                                                                 if(mwindow->edl->session->si_useduration)
1138                                                                         video_length = mwindow->edl->session->si_duration;
1139                                                                 else    
1140                                                                         video_length = 1.0 / mwindow->edl->session->frame_rate ; 
1141                                                         }
1142                                                         asset_length = mwindow->session->track_highlighted->to_units(video_length / asset->frame_rate, 0);
1143                                                 }
1144                                                 else
1145                                                         asset_length = mwindow->session->track_highlighted->to_units(asset->audio_length / asset->sample_rate, 0);
1147                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1148                                                 if (position < 0) 
1149                                                         w = -1;
1150                                                 else
1151                                                 {
1152                                                         current_atrack++;
1153                                                         draw_box = 1;
1154                                                 }
1155                                         }
1156                                         else
1157                                         if(clip && current_atrack < clip->tracks->total_audio_tracks())
1158                                         {
1159                                                 w = Units::to_int64((double)clip->tracks->total_length() *
1160                                                         mwindow->edl->session->sample_rate / 
1161                                                         mwindow->edl->local_session->zoom_sample);
1162 //printf("draw_paste_destination %d\n", x);
1163                                                 int64_t asset_length = mwindow->session->track_highlighted->to_units((double)clip->tracks->total_length(), 0);
1165                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1166                                                 if (position < 0) 
1167                                                         w = -1;
1168                                                 else
1169                                                 {
1170                                                         current_atrack++;
1171                                                         draw_box = 1;
1172                                                 }
1173                                         }
1174                                         else
1175                                         if(mwindow->session->current_operation == DRAG_EDIT &&
1176                                                 current_aedit < mwindow->session->drag_edits->total)
1177                                         {
1178                                                 Edit *edit;
1179                                                 while(current_aedit < mwindow->session->drag_edits->total &&
1180                                                         mwindow->session->drag_edits->values[current_aedit]->track->data_type != TRACK_AUDIO)
1181                                                         current_aedit++;
1183                                                 if(current_aedit < mwindow->session->drag_edits->total)
1184                                                 {
1185                                                         edit = mwindow->session->drag_edits->values[current_aedit];
1186                                                         w = Units::to_int64(edit->length / mwindow->edl->local_session->zoom_sample);
1188                                                         position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, mwindow->session->drag_edit, mwindow->session->drag_edit->length));
1189                                                         if (position < 0) 
1190                                                                 w = -1;
1191                                                         else
1192                                                         {
1193                                                                 current_aedit++;
1194                                                                 draw_box = 1;
1195                                                         }
1196                                                 }
1197                                         }
1198                                 }
1199                                 else
1200                                 if(dest->data_type == TRACK_VIDEO)
1201                                 {
1202 //printf("draw_paste_destination 1\n");
1203                                         if(asset && current_vtrack < asset->layers)
1204                                         {
1205                                                 // Images have length -1
1206                                                 double video_length = asset->video_length;
1207                                                 if (video_length < 0)
1208                                                 {
1209                                                         if(mwindow->edl->session->si_useduration)
1210                                                                 video_length = mwindow->edl->session->si_duration;
1211                                                         else    
1212                                                                 video_length = 1.0 / mwindow->edl->session->frame_rate ; 
1213                                                 }
1214                                                 w = Units::to_int64((double)video_length / 
1215                                                         asset->frame_rate *
1216                                                         mwindow->edl->session->sample_rate /
1217                                                         mwindow->edl->local_session->zoom_sample);
1218                                                 int64_t asset_length = mwindow->session->track_highlighted->to_units((double)video_length / 
1219                                                         asset->frame_rate, 0);
1221                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1222                                                 if (position < 0) 
1223                                                         w = -1;
1224                                                 else
1225                                                 {
1226                                                         current_vtrack++;
1227                                                         draw_box = 1;
1228                                                 }
1229                                         }
1230                                         else
1231                                         if(clip && current_vtrack < clip->tracks->total_video_tracks())
1232                                         {
1233                                                 w = Units::to_int64(clip->tracks->total_length() *
1234                                                         mwindow->edl->session->sample_rate / 
1235                                                         mwindow->edl->local_session->zoom_sample);
1236                                                 int64_t asset_length = mwindow->session->track_highlighted->to_units((double)clip->tracks->total_length(), 0);
1238                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1239                                                 if (position < 0) 
1240                                                         w = -1;
1241                                                 else
1242                                                 {
1243                                                         current_vtrack++;
1244                                                         draw_box = 1;
1245                                                 }
1246                                         }
1247                                         else
1248                                         if(mwindow->session->current_operation == DRAG_EDIT &&
1249                                                 current_vedit < mwindow->session->drag_edits->total)
1250                                         {
1251                                                 Edit *edit;
1252                                                 while(current_vedit < mwindow->session->drag_edits->total &&
1253                                                         mwindow->session->drag_edits->values[current_vedit]->track->data_type != TRACK_VIDEO)
1254                                                         current_vedit++;
1256                                                 if(current_vedit < mwindow->session->drag_edits->total)
1257                                                 {
1258                                                         edit = mwindow->session->drag_edits->values[current_vedit];
1259                                                         w = Units::to_int64(edit->track->from_units(edit->length) *
1260                                                                 mwindow->edl->session->sample_rate / 
1261                                                                 mwindow->edl->local_session->zoom_sample);
1263                                                         position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, mwindow->session->drag_edit, mwindow->session->drag_edit->length));
1264                                                         if (position < 0) 
1265                                                                 w = -1;
1266                                                         else
1267                                                         {
1268                                                                 current_vedit++;
1269                                                                 draw_box = 1;
1270                                                         }
1271                                                 }
1272                                         }
1273                                 }
1275                                 if(w >= 0)
1276                                 {
1277 // Get the x coordinate
1278                                         x = Units::to_int64(position * 
1279                                                 mwindow->edl->session->sample_rate /
1280                                                 mwindow->edl->local_session->zoom_sample) - 
1281                                                 mwindow->edl->local_session->view_start;
1282                                         int y = dest->y_pixel;
1283                                         int h = dest->vertical_span(mwindow->theme);
1286 //printf("TrackCanvas::draw_paste_destination 2 %d %d %d %d\n", x, y, w, h);
1287                                         if (insertion)
1288                                                 draw_highlight_insertion(x, y, w, h);
1289                                         else
1290                                                 draw_highlight_rectangle(x, y, w, h);
1291                                 }
1292                         }
1293                 }
1294         }
1297 void TrackCanvas::plugin_dimensions(Plugin *plugin, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1299         x = Units::round(plugin->track->from_units(plugin->startproject) *
1300                 mwindow->edl->session->sample_rate / 
1301                 mwindow->edl->local_session->zoom_sample - 
1302                 mwindow->edl->local_session->view_start);
1303         w = Units::round(plugin->track->from_units(plugin->length) *
1304                 mwindow->edl->session->sample_rate / 
1305                 mwindow->edl->local_session->zoom_sample);
1306         y = plugin->track->y_pixel + 
1307                         mwindow->edl->local_session->zoom_track +
1308                         plugin->plugin_set->get_number() * 
1309                         mwindow->theme->get_image("plugin_bg_data")->get_h();
1310         if(mwindow->edl->session->show_titles)
1311                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1312         h = mwindow->theme->get_image("plugin_bg_data")->get_h();
1315 int TrackCanvas::resource_h()
1317         return mwindow->edl->local_session->zoom_track;
1320 void TrackCanvas::draw_highlight_rectangle(int x, int y, int w, int h)
1323 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport, 
1324 // just draw arrows, so user has indication that something is there
1325 // FIXME: get better colors
1327         if (x + w <= 0)
1328         {
1329                 draw_triangle_left(0, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1330                 return;
1331         } else
1332         if (x >= get_w())
1333         {
1334                 draw_triangle_right(get_w() - h * 2/3, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1335                 return;
1336         }
1338 // Fix bug in heroines & cvs version as of 22.8.2005:
1339 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1340         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1341         if(x < -10)
1342         {
1343                 w += x - -10;
1344                 x = -10;
1345         }
1346         if(y < -10)
1347         {
1348                 h += y - -10;
1349                 y = -10;
1350         }
1351         w = MIN(w, get_w() + 20);
1352         h = MIN(h, get_h() + 20);
1353         set_color(WHITE);
1354         set_inverse();
1355         draw_rectangle(x, y, w, h);
1356         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1357         set_opaque();
1358 //printf("TrackCanvas::draw_highlight_rectangle %d %d %d %d\n", x, y, w, h);
1361 void TrackCanvas::draw_highlight_insertion(int x, int y, int w, int h)
1364 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport, 
1365 // just draw arrows, so user has indication that something is there
1366 // FIXME: get better colors
1369         
1370         int h1 = h / 8;
1371         int h2 = h / 4;
1372         
1373         set_inverse();
1375 /* these don't look so good
1377         draw_line(x, y, x, y+h);
1378         draw_line(x - h2 * 2, y + h1*2,   x - h2, y+h1*2);
1379         draw_line(x - h2 * 2, y + h1*2+1, x - h2, y+h1*2+1);
1380         draw_line(x - h2 * 2, y + h1*6,   x - h2, y+h1*6);
1381         draw_line(x - h2 * 2, y + h1*6+1, x - h2, y+h1*6+1);
1383         draw_triangle_right(x - h2, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1384         draw_triangle_right(x - h2, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1386 /*      draw_line(x + h2 * 2, y + h1*2,   x + h2, y+h1*2);
1387         draw_line(x + h2 * 2, y + h1*2+1, x + h2, y+h1*2+1);
1388         draw_line(x + h2 * 2, y + h1*6,   x + h2, y+h1*6);
1389         draw_line(x - h2 * 2, y + h1*6+1, x + h2, y+h1*6+1);
1391         draw_triangle_left(x, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1392         draw_triangle_left(x, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1393         
1394 // draw the box centred around x
1395         x -= w / 2;
1396 // Fix bug in heroines & cvs version as of 22.8.2005:
1397 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1398         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1399         if(x < -10)
1400         {
1401                 w += x - -10;
1402                 x = -10;
1403         }
1404         if(y < -10)
1405         {
1406                 h += y - -10;
1407                 y = -10;
1408         }
1409         w = MIN(w, get_w() + 20);
1410         h = MIN(h, get_h() + 20);
1411         set_color(WHITE);
1412         set_inverse();
1413         draw_rectangle(x, y, w, h);
1414         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1415         set_opaque();
1416 //printf("TrackCanvas::draw_highlight_insertion %d %d %d %d\n", x, y, w, h);
1419 void TrackCanvas::draw_playback_cursor()
1421 // Called before playback_cursor exists
1422 //      if(mwindow->playback_cursor && mwindow->playback_cursor->visible)
1423 //      {
1424 //              mwindow->playback_cursor->visible = 0;
1425 //              mwindow->playback_cursor->draw();
1426 //      }
1429 void TrackCanvas::get_handle_coords(Edit *edit, int64_t &x, int64_t &y, int64_t &w, int64_t &h, int side)
1431         int handle_w = mwindow->theme->edithandlein_data[0]->get_w();
1432         int handle_h = mwindow->theme->edithandlein_data[0]->get_h();
1434         edit_dimensions(edit, x, y, w, h);
1436         if(mwindow->edl->session->show_titles)
1437         {
1438                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1439         }
1440         else
1441         {
1442                 y = 0;
1443         }
1445         if(side == EDIT_OUT)
1446         {
1447                 x += w - handle_w;
1448         }
1450         h = handle_h;
1451         w = handle_w;
1454 void TrackCanvas::get_transition_coords(int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1456 //printf("TrackCanvas::get_transition_coords 1\n");
1457 //      int transition_w = mwindow->theme->transitionhandle_data[0]->get_w();
1458 //      int transition_h = mwindow->theme->transitionhandle_data[0]->get_h();
1459         int transition_w = 30;
1460         int transition_h = 30;
1461 //printf("TrackCanvas::get_transition_coords 1\n");
1463         if(mwindow->edl->session->show_titles)
1464                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1465 //printf("TrackCanvas::get_transition_coords 2\n");
1467         y += (h - mwindow->theme->get_image("title_bg_data")->get_h()) / 2 - transition_h / 2;
1468         x -= transition_w / 2;
1470         h = transition_h;
1471         w = transition_w;
1474 void TrackCanvas::draw_highlighting()
1476         int64_t x, y, w, h;
1477         int draw_box = 0;
1482         switch(mwindow->session->current_operation)
1483         {
1484                 case DRAG_ATRANSITION:
1485                 case DRAG_VTRANSITION:
1486 //printf("TrackCanvas::draw_highlighting 1 %p %p\n", 
1487 //      mwindow->session->track_highlighted, mwindow->session->edit_highlighted);
1488                         if(mwindow->session->edit_highlighted)
1489                         {
1490 //printf("TrackCanvas::draw_highlighting 2\n");
1491                                 if((mwindow->session->current_operation == DRAG_ATRANSITION && 
1492                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1493                                         (mwindow->session->current_operation == DRAG_VTRANSITION && 
1494                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO))
1495                                 {
1496 //printf("TrackCanvas::draw_highlighting 2\n");
1497                                         edit_dimensions(mwindow->session->edit_highlighted, x, y, w, h);
1498 //printf("TrackCanvas::draw_highlighting 2\n");
1500                                         if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1501                                                 MWindowGUI::visible(y, y + h, 0, get_h()))
1502                                         {
1503                                                 draw_box = 1;
1504                                                 get_transition_coords(x, y, w, h);
1505                                         }
1506 //printf("TrackCanvas::draw_highlighting 3\n");
1507                                 }
1508                         }
1509                         break;
1513 // Dragging a new effect from the Resource window
1514                 case DRAG_AEFFECT:
1515                 case DRAG_VEFFECT:
1516                         if(mwindow->session->track_highlighted &&
1517                                 ((mwindow->session->current_operation == DRAG_AEFFECT && mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1518                                         (mwindow->session->current_operation == DRAG_VEFFECT && mwindow->session->track_highlighted->data_type == TRACK_VIDEO)))
1519                         {
1520 // Put it before another plugin
1521                                 if(mwindow->session->plugin_highlighted)
1522                                 {
1523                                         plugin_dimensions(mwindow->session->plugin_highlighted, 
1524                                                 x, 
1525                                                 y, 
1526                                                 w, 
1527                                                 h);
1528 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1529                                 }
1530                                 else
1531 // Put it after a plugin set
1532                                 if(mwindow->session->pluginset_highlighted &&
1533                                         mwindow->session->pluginset_highlighted->last)
1534                                 {
1535                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last, 
1536                                                 x, 
1537                                                 y, 
1538                                                 w, 
1539                                                 h);
1540 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1541                                         int64_t track_x, track_y, track_w, track_h;
1542                                         track_dimensions(mwindow->session->track_highlighted, 
1543                                                 track_x, 
1544                                                 track_y, 
1545                                                 track_w, 
1546                                                 track_h);
1548                                         x += w;
1549                                         w = Units::round(
1550                                                         mwindow->session->track_highlighted->get_length() *
1551                                                         mwindow->edl->session->sample_rate / 
1552                                                         mwindow->edl->local_session->zoom_sample - 
1553                                                         mwindow->edl->local_session->view_start) -
1554                                                 x;
1555 //printf("TrackCanvas::draw_highlighting 2 %d\n", w);
1556                                         if(w <= 0) w = track_w;
1557                                 }
1558                                 else
1559                                 {
1560                                         track_dimensions(mwindow->session->track_highlighted, 
1561                                                 x, 
1562                                                 y, 
1563                                                 w, 
1564                                                 h);
1566 //printf("TrackCanvas::draw_highlighting 1 %d %d %d %d\n", x, y, w, h);
1567 // Put it in a new plugin set determined by the selected range
1568                                         if(mwindow->edl->local_session->get_selectionend() > 
1569                                                 mwindow->edl->local_session->get_selectionstart())
1570                                         {
1571                                                 x = Units::to_int64(mwindow->edl->local_session->get_selectionstart() *
1572                                                         mwindow->edl->session->sample_rate / 
1573                                                         mwindow->edl->local_session->zoom_sample -
1574                                                         mwindow->edl->local_session->view_start);
1575                                                 w = Units::to_int64((mwindow->edl->local_session->get_selectionend() - 
1576                                                         mwindow->edl->local_session->get_selectionstart()) *
1577                                                         mwindow->edl->session->sample_rate / 
1578                                                         mwindow->edl->local_session->zoom_sample);
1579                                         }
1580 // Put it in a new plugin set determined by an edit boundary
1581                                         else
1582                                         if(mwindow->session->edit_highlighted)
1583                                         {
1584                                                 int64_t temp_y, temp_h;
1585                                                 edit_dimensions(mwindow->session->edit_highlighted, 
1586                                                         x, 
1587                                                         temp_y, 
1588                                                         w, 
1589                                                         temp_h);
1590                                         }
1591 // Put it at the beginning of the track in a new plugin set
1592                                 }
1594                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1595                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1596                                 {
1597 //printf("TrackCanvas::draw_highlighting 1\n");
1598                                         draw_box = 1;
1599                                 }
1600                         }
1601                         break;
1602                 
1603                 case DRAG_ASSET:
1604                         if(mwindow->session->track_highlighted)
1605                         {
1606                                 track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1608                                 if(MWindowGUI::visible(y, y + h, 0, get_h()))
1609                                 {
1610                                         draw_paste_destination();
1611                                 }
1612                         }
1613                         break;
1615 // Dragging an effect from the timeline
1616                 case DRAG_AEFFECT_COPY:
1617                 case DRAG_VEFFECT_COPY:
1618                         if((mwindow->session->plugin_highlighted || mwindow->session->track_highlighted) &&
1619                                 ((mwindow->session->current_operation == DRAG_AEFFECT_COPY && mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1620                                 (mwindow->session->current_operation == DRAG_VEFFECT_COPY && mwindow->session->track_highlighted->data_type == TRACK_VIDEO)))
1621                         {
1622 // Put it before another plugin
1623                                 if(mwindow->session->plugin_highlighted)
1624                                         plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1625                                 else
1626 // Put it after a plugin set
1627                                 if(mwindow->session->pluginset_highlighted &&
1628                                         mwindow->session->pluginset_highlighted->last)
1629                                 {
1630                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last, x, y, w, h);
1631                                         x += w;
1632                                 }
1633                                 else
1634                                 if(mwindow->session->track_highlighted)
1635                                 {
1636                                         track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1638 // Put it in a new plugin set determined by an edit boundary
1639                                         if(mwindow->session->edit_highlighted)
1640                                         {
1641                                                 int64_t temp_y, temp_h;
1642                                                 edit_dimensions(mwindow->session->edit_highlighted, 
1643                                                         x, 
1644                                                         temp_y, 
1645                                                         w, 
1646                                                         temp_h);
1647                                         }
1648 // Put it in a new plugin set at the start of the track
1649                                 }
1651 // Calculate length of plugin based on data type of track and units
1652                                 if(mwindow->session->track_highlighted->data_type == TRACK_VIDEO)
1653                                 {
1654                                         w = (int64_t)((double)mwindow->session->drag_plugin->length / 
1655                                                 mwindow->edl->session->frame_rate *
1656                                                 mwindow->edl->session->sample_rate /
1657                                                 mwindow->edl->local_session->zoom_sample);
1658                                 }
1659                                 else
1660                                 {
1661                                         w = (int64_t)mwindow->session->drag_plugin->length /
1662                                                 mwindow->edl->local_session->zoom_sample;
1663                                 }
1665                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1666                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1667                                 {
1668                                         draw_box = 1;
1669                                 }
1670                         }
1671                         break;
1673                 case DRAG_PLUGINKEY:
1674                         if(mwindow->session->plugin_highlighted && 
1675                            mwindow->session->current_operation == DRAG_PLUGINKEY)
1676                         {
1677 // Just highlight the plugin
1678                                 plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1680                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1681                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1682                                 {
1683                                         draw_box = 1;
1684                                 }
1685                         }
1686                         break;
1688                 case DRAG_EDIT:
1689                         if(mwindow->session->track_highlighted)
1690                         {
1691                                 track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1693                                 if(MWindowGUI::visible(y, y + h, 0, get_h()))
1694                                 {
1695                                         draw_paste_destination();
1696                                 }
1697                         }
1698                         break;
1699         }
1702         if(draw_box)
1703         {
1704                 draw_highlight_rectangle(x, y, w, h);
1705         }
1708 void TrackCanvas::draw_plugins()
1710         char string[BCTEXTLEN];
1711         int current_toggle = 0;
1713         if(!mwindow->edl->session->show_assets) goto done;
1715         for(int i = 0; i < plugin_on_toggles.total; i++)
1716                 plugin_on_toggles.values[i]->in_use = 0;
1717         for(int i = 0; i < plugin_show_toggles.total; i++)
1718                 plugin_show_toggles.values[i]->in_use = 0;
1721         for(Track *track = mwindow->edl->tracks->first;
1722                 track;
1723                 track = track->next)
1724         {
1725                 if(track->expand_view)
1726                 {
1727                         for(int i = 0; i < track->plugin_set.total; i++)
1728                         {
1729                                 PluginSet *pluginset = track->plugin_set.values[i];
1731                                 for(Plugin *plugin = (Plugin*)pluginset->first; plugin; plugin = (Plugin*)plugin->next)
1732                                 {
1733                                         int64_t total_x, y, total_w, h;
1734                                         plugin_dimensions(plugin, total_x, y, total_w, h);
1735                                         
1736                                         if(MWindowGUI::visible(total_x, total_x + total_w, 0, get_w()) &&
1737                                                 MWindowGUI::visible(y, y + h, 0, get_h()) &&
1738                                                 plugin->plugin_type != PLUGIN_NONE)
1739                                         {
1740                                                 int x = total_x, w = total_w, left_margin = 5;
1741                                                 int right_margin = 5;
1742                                                 if(x < 0)
1743                                                 {
1744                                                         w -= -x;
1745                                                         x = 0;
1746                                                 }
1747                                                 if(w + x > get_w()) w -= (w + x) - get_w();
1749                                                 draw_3segmenth(x, 
1750                                                         y, 
1751                                                         w, 
1752                                                         total_x,
1753                                                         total_w,
1754                                                         mwindow->theme->get_image("plugin_bg_data"),
1755                                                         0);
1756                                                 set_color(get_resources()->default_text_color);
1757                                                 set_font(MEDIUMFONT_3D);
1758                                                 plugin->calculate_title(string, 0);
1760 // Truncate string to int64_test visible in background
1761                                                 int len = strlen(string), j;
1762                                                 for(j = len; j >= 0; j--)
1763                                                 {
1764                                                         if(left_margin + get_text_width(MEDIUMFONT_3D, string) > w)
1765                                                         {
1766                                                                 string[j] = 0;
1767                                                         }
1768                                                         else
1769                                                                 break;
1770                                                 }
1772 // Justify the text on the left boundary of the edit if it is visible.
1773 // Otherwise justify it on the left side of the screen.
1774                                                 int text_x = total_x + left_margin;
1775                                                 text_x = MAX(left_margin, text_x);
1776                                                 draw_text(text_x, 
1777                                                         y + get_text_ascent(MEDIUMFONT_3D) + 2, 
1778                                                         string,
1779                                                         strlen(string),
1780                                                         0);
1783 // Update plugin toggles
1784                                                 int toggle_x = total_x + total_w;
1785                                                 toggle_x = MIN(get_w() - right_margin, toggle_x);
1786                                                 toggle_x -= PluginOn::calculate_w(mwindow) + 10;
1787                                                 int toggle_y = y;
1788                                                 if(current_toggle >= plugin_on_toggles.total)
1789                                                 {
1790                                                         PluginOn *plugin_on = new PluginOn(mwindow, toggle_x, toggle_y, plugin);
1791                                                         add_subwindow(plugin_on);
1792                                                         plugin_on_toggles.append(plugin_on);
1793                                                 }
1794                                                 else
1795                                                 {
1796                                                         plugin_on_toggles.values[current_toggle]->update(toggle_x, toggle_y, plugin);
1797                                                 }
1799                                                 toggle_x -= PluginShow::calculate_w(mwindow) + 10;
1800                                                 if(current_toggle >= plugin_show_toggles.total)
1801                                                 {
1802                                                         PluginShow *plugin_off = new PluginShow(mwindow, toggle_x, toggle_y, plugin);
1803                                                         add_subwindow(plugin_off);
1804                                                         plugin_show_toggles.append(plugin_off);
1805                                                 }
1806                                                 else
1807                                                 {
1808                                                         plugin_show_toggles.values[current_toggle]->update(toggle_x, toggle_y, plugin);
1809                                                 }
1810                                                 current_toggle++;
1811                                         }
1812                                 }
1813                         }
1814                 }
1815         }
1818 done:
1819         int i = current_toggle;
1820         while(i < plugin_on_toggles.total &&
1821                 i < plugin_show_toggles.total)
1822         {
1823                 plugin_on_toggles.remove_object_number(current_toggle);
1824                 plugin_show_toggles.remove_object_number(current_toggle);
1825         }
1828 void TrackCanvas::refresh_plugintoggles()
1830         for(int i = 0; i < plugin_on_toggles.total; i++)
1831         {
1832                 PluginOn *on = plugin_on_toggles.values[i];
1833                 on->reposition_window(on->get_x(), on->get_y());
1834         }
1835         for(int i = 0; i < plugin_show_toggles.total; i++)
1836         {
1837                 PluginShow *show = plugin_show_toggles.values[i];
1838                 show->reposition_window(show->get_x(), show->get_y());
1839         }
1842 void TrackCanvas::draw_inout_points()
1847 void TrackCanvas::draw_drag_handle()
1849         if(mwindow->session->current_operation == DRAG_EDITHANDLE2 ||
1850                 mwindow->session->current_operation == DRAG_PLUGINHANDLE2)
1851         {
1852 //printf("TrackCanvas::draw_drag_handle 1 %ld %ld\n", mwindow->session->drag_sample, mwindow->edl->local_session->view_start);
1853                 int64_t pixel1 = Units::round(mwindow->session->drag_position * 
1854                         mwindow->edl->session->sample_rate /
1855                         mwindow->edl->local_session->zoom_sample - 
1856                         mwindow->edl->local_session->view_start);
1857 //printf("TrackCanvas::draw_drag_handle 2 %d\n", pixel1);
1858                 set_color(GREEN);
1859                 set_inverse();
1860 //printf("TrackCanvas::draw_drag_handle 3\n");
1861                 draw_line(pixel1, 0, pixel1, get_h());
1862                 set_opaque();
1863 //printf("TrackCanvas::draw_drag_handle 4\n");
1864         }
1868 void TrackCanvas::draw_transitions()
1870         int64_t x, y, w, h;
1872         if(!mwindow->edl->session->show_assets) return;
1874         for(Track *track = mwindow->edl->tracks->first;
1875                 track;
1876                 track = track->next)
1877         {
1878                 for(Edit *edit = track->edits->first;
1879                         edit;
1880                         edit = edit->next)
1881                 {
1882                         if(edit->transition)
1883                         {
1884                                 int64_t strip_w, strip_x, strip_y;
1885                                 edit_dimensions(edit, x, y, w, h);
1886                                 strip_x = x ;
1887                                 strip_y = y;
1888                                 if(mwindow->edl->session->show_titles)
1889                                         strip_y += mwindow->theme->get_image("title_bg_data")->get_h();
1891                                 get_transition_coords(x, y, w, h);
1892                                 strip_w = Units::round(edit->track->from_units(edit->transition->length) * 
1893                                         mwindow->edl->session->sample_rate / 
1894                                         mwindow->edl->local_session->zoom_sample);
1896                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1897                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1898                                 {
1899                                         PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
1900                                                 track->data_type);
1901                                         draw_vframe(server->picon, 
1902                                                 x, 
1903                                                 y, 
1904                                                 w, 
1905                                                 h, 
1906                                                 0, 
1907                                                 0, 
1908                                                 server->picon->get_w(), 
1909                                                 server->picon->get_h());
1910                                 }
1911                                 if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
1912                                         MWindowGUI::visible(strip_y, strip_y + h, 0, get_h()))
1913                                 {
1914                                         int x = strip_x, w = strip_w, left_margin = 5;
1915                                         if(x < 0)
1916                                         {
1917                                                 w -= -x;
1918                                                 x = 0;
1919                                         }
1920                                         if(w + x > get_w()) w -= (w + x) - get_w();
1921                                 
1922                                         draw_3segmenth(
1923                                                 x, 
1924                                                 strip_y, 
1925                                                 w, 
1926                                                 strip_x,
1927                                                 strip_w,
1928                                                 mwindow->theme->get_image("plugin_bg_data"),
1929                                                 0);
1931                                 }
1932                         }
1933                 }
1934         }
1937 void TrackCanvas::draw_loop_points()
1939 //printf("TrackCanvas::draw_loop_points 1\n");
1940         if(mwindow->edl->local_session->loop_playback)
1941         {
1942 //printf("TrackCanvas::draw_loop_points 2\n");
1943                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
1944                         mwindow->edl->session->sample_rate /
1945                         mwindow->edl->local_session->zoom_sample - 
1946                         mwindow->edl->local_session->view_start);
1947 //printf("TrackCanvas::draw_loop_points 3\n");
1949                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1950                 {
1951                         set_color(GREEN);
1952                         draw_line(x, 0, x, get_h());
1953                 }
1954 //printf("TrackCanvas::draw_loop_points 4\n");
1956                 x = Units::round(mwindow->edl->local_session->loop_end *
1957                         mwindow->edl->session->sample_rate /
1958                         mwindow->edl->local_session->zoom_sample - 
1959                         mwindow->edl->local_session->view_start);
1960 //printf("TrackCanvas::draw_loop_points 5\n");
1962                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1963                 {
1964                         set_color(GREEN);
1965                         draw_line(x, 0, x, get_h());
1966                 }
1967 //printf("TrackCanvas::draw_loop_points 6\n");
1968         }
1969 //printf("TrackCanvas::draw_loop_points 7\n");
1972 void TrackCanvas::draw_brender_start()
1974         if(mwindow->preferences->use_brender)
1975         {
1976                 int64_t x = Units::round(mwindow->edl->session->brender_start *
1977                         mwindow->edl->session->sample_rate /
1978                         mwindow->edl->local_session->zoom_sample - 
1979                         mwindow->edl->local_session->view_start);
1981                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1982                 {
1983                         set_color(RED);
1984                         draw_line(x, 0, x, get_h());
1985                 }
1986         }
1989 static int auto_colors[] = 
1991         BLUE,
1992         RED,
1993         GREEN,
1994         BLUE,
1995         RED,
1996         GREEN,
1997         BLUE,
1998         WHITE,
1999         0,
2000         0,
2001         0,
2002         0
2005 // The operations which correspond to each automation type
2006 static int auto_operations[] = 
2008         DRAG_MUTE,
2009         DRAG_CAMERA_X,
2010         DRAG_CAMERA_Y,
2011         DRAG_CAMERA_Z,
2012         DRAG_PROJECTOR_X,
2013         DRAG_PROJECTOR_Y,
2014         DRAG_PROJECTOR_Z,
2015         DRAG_FADE,
2016         DRAG_PAN,
2017         DRAG_MODE,
2018         DRAG_MASK,
2019         DRAG_NUDGE
2022 // The buttonpress operations, so nothing changes unless the mouse moves
2023 // a certain amount.  This allows the keyframe to be used to position the
2024 // insertion point without moving itself.
2025 static int pre_auto_operations[] =
2027         DRAG_MUTE,
2028         DRAG_CAMERA_X,
2029         DRAG_CAMERA_Y,
2030         DRAG_CAMERA_Z,
2031         DRAG_PROJECTOR_X,
2032         DRAG_PROJECTOR_Y,
2033         DRAG_PROJECTOR_Z,
2034         DRAG_FADE,
2035         DRAG_PAN_PRE,
2036         DRAG_MODE_PRE,
2037         DRAG_MASK_PRE,
2038         DRAG_NUDGE
2042 int TrackCanvas::do_keyframes(int cursor_x, 
2043         int cursor_y, 
2044         int draw, 
2045         int buttonpress, 
2046         int &new_cursor,
2047         int &update_cursor,
2048         int &rerender)
2050 // Note: button 3 (right mouse button) is not eaten to allow
2051 // track context menu to appear
2052         int current_tool = 0;
2053         int result = 0;
2054         EDLSession *session = mwindow->edl->session;
2058         BC_Pixmap *auto_pixmaps[] = 
2059         {
2060                 0,
2061                 0,
2062                 0,
2063                 0,
2064                 0,
2065                 0,
2066                 0,
2067                 0,
2068                 pankeyframe_pixmap,
2069                 modekeyframe_pixmap,
2070                 maskkeyframe_pixmap,
2071                 0,
2072         };
2076         for(Track *track = mwindow->edl->tracks->first;
2077                 track && !result;
2078                 track = track->next)
2079         {
2080         Auto *auto_keyframe;
2081                 Automation *automation = track->automation;
2084 // Handle float autos
2085                 for(int i = 0; i < AUTOMATION_TOTAL && !result; i++)
2086                 {
2087 // Event not trapped and automation visible
2088                         Autos *autos = automation->autos[i];
2089                         if(!result && session->auto_conf->autos[i] && autos)
2090                         {
2091                                 switch(i)
2092                                 {
2093                                         case AUTOMATION_MODE:
2094                                         case AUTOMATION_PAN:
2095                                         case AUTOMATION_MASK:
2096                                                 result = do_autos(track, 
2097                                                         automation->autos[i],
2098                                                         cursor_x, 
2099                                                         cursor_y, 
2100                                                         draw, 
2101                                                         buttonpress,
2102                                                         auto_pixmaps[i],
2103                             auto_keyframe);
2104                                                 break;
2106                                         default:
2107                                                 switch(autos->get_type())
2108                                                 {
2109                                                         case AUTOMATION_TYPE_FLOAT:
2110                                                         {
2111                                                                 Automation *dummy = new Automation(0,track);
2112                                                                 int autogrouptype = dummy->autogrouptype(i,track);
2113                                                                 result = do_float_autos(track, 
2114                                                                         autos,
2115                                                                         cursor_x, 
2116                                                                         cursor_y, 
2117                                                                         draw, 
2118                                                                         buttonpress, 
2119                                                                         auto_colors[i],
2120                                                                         auto_keyframe,
2121                                                                         autogrouptype);
2122                                                         }
2123                                                         break;
2125                                                         case AUTOMATION_TYPE_INT:
2126                                                                 result = do_toggle_autos(track, 
2127                                                                         autos,
2128                                                                         cursor_x, 
2129                                                                         cursor_y, 
2130                                                                         draw, 
2131                                                                         buttonpress,
2132                                                                         auto_colors[i],
2133                                                                         auto_keyframe);
2134                                                                 break;
2135                                                 }
2136                                                 break;
2137                                 }
2138                         
2141                                 if(result)
2142                                 {
2143                                         if(mwindow->session->current_operation == auto_operations[i])
2144                                                 rerender = 1;
2145                                         if(buttonpress)
2146                                         {
2147                         if (buttonpress != 3)
2148                         {
2149                                                         if(i == AUTOMATION_FADE) 
2150                                                                 synchronize_autos(0, 
2151                                                                         track, 
2152                                                                         (FloatAuto*)mwindow->session->drag_auto, 
2153                                                                         1);
2154                                                         mwindow->session->current_operation = pre_auto_operations[i];
2155                                                         update_drag_caption();
2156                                                         }
2157                                                         else
2158                                                         {
2159                                 gui->keyframe_menu->update(automation, autos, auto_keyframe);
2160                                 gui->keyframe_menu->activate_menu();
2161                                 rerender = 1; // the position changes
2162                                                         }
2163                                         }
2164                                 }
2165                         }
2166                 }
2171                 if(!result && 
2172                         session->auto_conf->plugins &&
2173                         mwindow->edl->session->show_assets)
2174                 {
2175                         Plugin *plugin;
2176                         KeyFrame *keyframe;
2177                         result = do_plugin_autos(track,
2178                                 cursor_x, 
2179                                 cursor_y, 
2180                                 draw, 
2181                                 buttonpress,
2182                                 plugin,
2183                                 keyframe);
2184                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY)
2185                         {
2186                                 rerender = 1;
2187                         }
2188                         if(result && (buttonpress == 1))
2189                         {
2190                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2191                                 update_drag_caption();
2192                                 rerender = 1;
2193                         } else
2194                         if (result && (buttonpress == 3))
2195                         {
2196                                 gui->keyframe_menu->update(plugin, keyframe);
2197                                 gui->keyframe_menu->activate_menu();
2198                                 rerender = 1; // the position changes
2199                         }
2200                 }
2201         }
2203 // Final pass to trap event
2204         for(int i = 0; i < AUTOMATION_TOTAL; i++)
2205         {
2206                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2207                         mwindow->session->current_operation == auto_operations[i])
2208                         result = 1;
2209         }
2211         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2212                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE)
2213         {
2214                 result = 1;
2215         }
2217         update_cursor = 1;
2218         if(result)
2219         {
2220                 new_cursor = UPRIGHT_ARROW_CURSOR;
2221         }
2223         return result;
2226 void TrackCanvas::draw_auto(Auto *current, 
2227         int x, 
2228         int y, 
2229         int center_pixel, 
2230         int zoom_track,
2231         int color)
2233         int x1, y1, x2, y2;
2234         char string[BCTEXTLEN];
2236         x1 = x - HANDLE_W / 2;
2237         x2 = x + HANDLE_W / 2;
2238         y1 = center_pixel + y - HANDLE_W / 2;
2239         y2 = center_pixel + y + HANDLE_W / 2;
2241         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2242         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2244         set_color(BLACK);
2245         draw_box(x1 + 1, y1 + 1, x2 - x1, y2 - y1);
2246         set_color(color);
2247         draw_box(x1, y1, x2 - x1, y2 - y1);
2250 void TrackCanvas::draw_floatauto(FloatAuto *current, 
2251         int x, 
2252         int y, 
2253         int in_x, 
2254         int in_y, 
2255         int out_x, 
2256         int out_y, 
2257         int center_pixel, 
2258         int zoom_track,
2259         int color)
2261         int x1, y1, x2, y2;
2262         char string[BCTEXTLEN];
2264 // Center
2265         x1 = x - HANDLE_W / 2;
2266         x2 = x + HANDLE_W / 2;
2267         y1 = center_pixel + y - HANDLE_W / 2;
2268         y2 = center_pixel + y + HANDLE_W / 2;
2270         CLAMP(y1, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2271         CLAMP(y2, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2273         if(y2 - 1 > y1)
2274         {
2275                 set_color(BLACK);
2276                 draw_box(x1 + 1, y1 + 1, x2 - x1, y2 - y1);
2277                 set_color(color);
2278                 draw_box(x1, y1, x2 - x1, y2 - y1);
2279         }
2281         if(in_x != x)
2282                 draw_floatauto_ctrlpoint(x, y, in_x, in_y, center_pixel, zoom_track, color);
2283         if(out_x != x)
2284                 draw_floatauto_ctrlpoint(x, y, out_x, out_y, center_pixel, zoom_track, color);
2288 inline void TrackCanvas::draw_floatauto_ctrlpoint(
2289         int x, 
2290         int y, 
2291         int cp_x, 
2292         int cp_y, 
2293         int center_pixel, 
2294         int zoom_track, 
2295         int color)
2296 // draw the tangent and a handle for given bézier ctrl point
2298         bool handle_visible = (abs(cp_y) <= zoom_track / 2); // abs(cp_y+HANDLE_W/2) would be more precise...
2300         float slope = (float)(cp_y - y)/(cp_x - x);
2301         CLAMP(cp_y, -zoom_track / 2, zoom_track / 2);
2302         if(slope != 0)
2303                 cp_x = x + (int)round((float)(cp_y - y) / slope);
2305         y    += center_pixel;
2306         cp_y += center_pixel;
2308         // drawing the tangent as a dashed line...
2309         int const dash = 2 * HANDLE_W;
2310         int const gap  = HANDLE_W / 2;
2311         int sy, ey;
2312         float sx = cp_x - x;
2313         float ex = 0;
2315         // q is the x displacement for a unit line of slope
2316         float q = 1 / sqrt(1 + slope * slope) * (sx > 0 ? 1 : -1);
2318         float dist = 1 / q * sx;
2319         if(dist > dash)
2320                 ex = sx - q * dash;
2322         set_color(color);
2323         do 
2324           {     sy = (int)round(slope * sx);
2325                 ey = (int)round(slope * ex);
2326                 draw_line((int)round(sx) + x, sy + y, (int)round(ex) + x, ey + y);
2327                 sx = ex - q * gap;
2328                 ex = sx - q * dash;
2329                 dist -= dash + gap;
2330         } while (dist > 2 * dash);
2332         if(handle_visible)
2333         {
2334                 int r = HANDLE_W / 2;
2335                 int cp_x1 = cp_x - r;
2336                 int cp_y1 = cp_y - r;
2337                 set_color(BLACK);
2338                 draw_disc  (cp_x1, cp_y1, 2 * r, 2 * r);
2339                 set_color(color);
2340                 draw_circle(cp_x1, cp_y1, 2 * r, 2 * r);
2341         }
2344 int TrackCanvas::test_auto(Auto *current, 
2345         int x, 
2346         int y, 
2347         int center_pixel, 
2348         int zoom_track, 
2349         int cursor_x, 
2350         int cursor_y, 
2351         int buttonpress)
2353         int x1, y1, x2, y2;
2354         char string[BCTEXTLEN];
2355         int result = 0;
2357         x1 = x - HANDLE_W / 2;
2358         x2 = x + HANDLE_W / 2;
2359         y1 = center_pixel + y - HANDLE_W / 2;
2360         y2 = center_pixel + y + HANDLE_W / 2;
2362         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2363         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2365         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2366         {
2367                 if(buttonpress && buttonpress != 3)
2368                 {
2369                         mwindow->session->drag_auto = current;
2370                         mwindow->session->drag_start_percentage = current->value_to_percentage();
2371                         mwindow->session->drag_start_position = current->position;
2372                         mwindow->session->drag_origin_x = cursor_x;
2373                         mwindow->session->drag_origin_y = cursor_y;
2374                 }
2375                 result = 1;
2376         }
2378         return result;
2381 int TrackCanvas::test_floatauto(Auto *current, 
2382         int x, 
2383         int y, 
2384         int in_x,
2385         int in_y,
2386         int out_x,
2387         int out_y,
2388         int center_pixel, 
2389         int zoom_track, 
2390         int cursor_x, 
2391         int cursor_y, 
2392         int buttonpress)
2394         int x1, y1, x2, y2;
2395         int in_x1, in_y1, in_x2, in_y2;
2396         int out_x1, out_y1, out_x2, out_y2;
2397         char string[BCTEXTLEN];
2398         int result = 0;
2400         x1 = x - HANDLE_W / 2;
2401         x2 = x + HANDLE_W / 2;
2402         y1 = center_pixel + y - HANDLE_W / 2;
2403         y2 = center_pixel + y + HANDLE_W / 2;
2405         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2406         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2408         in_x1 = in_x - HANDLE_W / 2;
2409         in_x2 = in_x + HANDLE_W / 2;
2410         in_y1 = center_pixel + in_y - HANDLE_W / 2;
2411         in_y2 = center_pixel + in_y + HANDLE_W / 2;
2413         if(in_y1 < center_pixel + -zoom_track / 2) in_y1 = center_pixel + -zoom_track / 2;
2414         if(in_y2 > center_pixel + zoom_track / 2) in_y2 = center_pixel + zoom_track / 2;
2416         out_x1 = out_x - HANDLE_W / 2;
2417         out_x2 = out_x + HANDLE_W / 2;
2418         out_y1 = center_pixel + out_y - HANDLE_W / 2;
2419         out_y2 = center_pixel + out_y + HANDLE_W / 2;
2421         if(out_y1 < center_pixel + -zoom_track / 2) out_y1 = center_pixel + -zoom_track / 2;
2422         if(out_y2 > center_pixel + zoom_track / 2) out_y2 = center_pixel + zoom_track / 2;
2426 //printf("TrackCanvas::test_floatauto %d %d %d %d %d %d\n", cursor_x, cursor_y, x1, x2, y1, y2);
2427 // Test value
2428         if(!ctrl_down() &&
2429                 cursor_x >= x1 && 
2430                 cursor_x < x2 && 
2431                 cursor_y >= y1 && 
2432                 cursor_y < y2)
2433         {
2434                 if(buttonpress && (buttonpress != 3))
2435                 {
2436                         mwindow->session->drag_auto = current;
2437                         mwindow->session->drag_start_percentage = current->value_to_percentage();
2438                         mwindow->session->drag_start_position = current->position;
2439                         mwindow->session->drag_origin_x = cursor_x;
2440                         mwindow->session->drag_origin_y = cursor_y;
2441                         mwindow->session->drag_handle = 0;
2442                 }
2443                 result = 1;
2444         }
2445         else
2446 // Test in control
2447         if(ctrl_down() &&
2448                 cursor_x >= in_x1 && 
2449                 cursor_x < in_x2 && 
2450                 cursor_y >= in_y1 && 
2451                 cursor_y < in_y2 &&
2452                 current->position > 0)
2453         {
2454                 if(buttonpress && (buttonpress != 3))
2455                 {
2456                         mwindow->session->drag_auto = current;
2457                         mwindow->session->drag_start_percentage = 
2458                                 current->invalue_to_percentage();
2459                         mwindow->session->drag_start_position = 
2460                                 ((FloatAuto*)current)->control_in_position;
2461                         mwindow->session->drag_origin_x = cursor_x;
2462                         mwindow->session->drag_origin_y = cursor_y;
2463                         mwindow->session->drag_handle = 1;
2464                 }
2465                 result = 1;
2466         }
2467         else
2468 // Test out control
2469         if(ctrl_down() &&
2470                 cursor_x >= out_x1 && 
2471                 cursor_x < out_x2 && 
2472                 cursor_y >= out_y1 && 
2473                 cursor_y < out_y2)
2474         {
2475                 if(buttonpress && (buttonpress != 3))
2476                 {
2477                         mwindow->session->drag_auto = current;
2478                         mwindow->session->drag_start_percentage = 
2479                                 current->outvalue_to_percentage();
2480                         mwindow->session->drag_start_position = 
2481                                 ((FloatAuto*)current)->control_out_position;
2482                         mwindow->session->drag_origin_x = cursor_x;
2483                         mwindow->session->drag_origin_y = cursor_y;
2484                         mwindow->session->drag_handle = 2;
2485                 }
2486                 result = 1;
2487         }
2489 // if(buttonpress) 
2490 // 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", 
2491 // mwindow->session->drag_handle,
2492 // ctrl_down(),
2493 // cursor_x,
2494 // cursor_y,
2495 // x1, x2, y1, y2);
2497         return result;
2500 void TrackCanvas::draw_floatline(int center_pixel, 
2501         FloatAuto *previous,
2502         FloatAuto *next,
2503         FloatAutos *autos,
2504         double unit_start,
2505         double zoom_units,
2506         double yscale,
2507         int x1,
2508         int y1,
2509         int x2,
2510         int y2,
2511         int color,
2512         int autogrouptype)
2514 // Solve bezier equation for either every pixel or a certain large number of
2515 // points.
2519 // Not using slope intercept
2520         x1 = MAX(0, x1);
2525         int prev_y;
2526 // Call by reference fails for some reason here
2527         FloatAuto *previous1 = previous, *next1 = next;
2528         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2529         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2530         float automation_range = automation_max - automation_min;
2532         for(int x = x1; x < x2; x++)
2533         {
2534                 int64_t position = (int64_t)(unit_start + x * zoom_units);
2535                 float value = autos->get_value(position, previous1, next1);
2536                 AUTOMATIONCLAMPS(value, autogrouptype);
2538                 int y = center_pixel + 
2539                         (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2541                 if(x > x1 && 
2542                         y >= center_pixel - yscale / 2 && 
2543                         y < center_pixel + yscale / 2 - 1)
2544                 {
2545                         set_color(BLACK);
2546                         draw_line(x - 1, prev_y, x, y);
2547                         set_color(color);
2548                         draw_line(x - 1, prev_y - 1, x, y - 1);
2549                 }
2550                 prev_y = y;
2551         }
2556 void TrackCanvas::synchronize_autos(float change, 
2557         Track *skip, 
2558         FloatAuto *fauto, 
2559         int fill_gangs)
2561 // fill mwindow->session->drag_auto_gang
2562         if (fill_gangs == 1 && skip->gang)
2563         {
2564                 for(Track *current = mwindow->edl->tracks->first;
2565                         current;
2566                         current = NEXT)
2567                 {
2568                         if(current->data_type == skip->data_type &&
2569                                 current->gang && 
2570                                 current->record && 
2571                                 current != skip)
2572                         {
2573                                 FloatAutos *fade_autos = (FloatAutos*)current->automation->autos[AUTOMATION_FADE];
2574                                 double position = skip->from_units(fauto->position);
2575                                 FloatAuto *previous = 0, *next = 0;
2577                                 float init_value = fade_autos->get_value(fauto->position, previous, next);
2578                                 FloatAuto *keyframe;
2579                                 keyframe = (FloatAuto*)fade_autos->get_auto_at_position(position);
2580                                 
2581                                 if (!keyframe)
2582                                 {
2583 // create keyframe at exactly this point in time
2584                                         keyframe = (FloatAuto*)fade_autos->insert_auto(fauto->position);
2585                                         keyframe->set_value(init_value);
2586                                 } 
2587                                 else
2588                                 { 
2589 // keyframe exists, just change it
2590                                         keyframe->addto_value(change);          
2591                                 } 
2592                                 
2593                                 keyframe->position = fauto->position;
2594                                 keyframe->control_out_position = fauto->control_out_position;
2595                                 keyframe->control_in_position = fauto->control_in_position;
2596                                 keyframe->control_out_value = fauto->control_out_value;
2597                                 keyframe->control_in_value = fauto->control_in_value;
2599                                 mwindow->session->drag_auto_gang->append((Auto *)keyframe);
2600                         }
2601                 }
2602         } else 
2603 // move the gangs
2604         if (fill_gangs == 0)      
2605         {
2606 // Move the gang!
2607                 for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++)
2608                 {
2609                         FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2610                         
2611                         float new_value = keyframe->get_value() + change;
2612                         CLAMP(new_value, 
2613                               mwindow->edl->local_session->automation_mins[keyframe->autos->autogrouptype],
2614                               mwindow->edl->local_session->automation_maxs[keyframe->autos->autogrouptype]);
2615                         keyframe->set_value(new_value);
2616                         keyframe->control_out_position = fauto->control_out_position;
2617                         keyframe->control_in_position = fauto->control_in_position;
2618                         keyframe->control_out_value = fauto->control_out_value;
2619                         keyframe->control_in_value = fauto->control_in_value;
2620                 } 
2622         } 
2623         else
2624 // remove the gangs
2625         if (fill_gangs == -1)      
2626         {
2627                 for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++)
2628                 {
2629                         FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2630                         keyframe->autos->remove_nonsequential(
2631                                         keyframe);
2632                 } 
2633                 mwindow->session->drag_auto_gang->remove_all();
2634         }
2638 int TrackCanvas::test_floatline(int center_pixel, 
2639                 FloatAutos *autos,
2640                 double unit_start,
2641                 double zoom_units,
2642                 double yscale,
2643                 int x1,
2644                 int x2,
2645                 int cursor_x, 
2646                 int cursor_y, 
2647                 int buttonpress,
2648                 int autogrouptype)
2650         int result = 0;
2653         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2654         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2655         float automation_range = automation_max - automation_min;
2656         int64_t position = (int64_t)(unit_start + cursor_x * zoom_units);
2657 // Call by reference fails for some reason here
2658         FloatAuto *previous = 0, *next = 0;
2659         float value = autos->get_value(position, previous, next);
2660         AUTOMATIONCLAMPS(value,autogrouptype);
2661         int y = center_pixel + 
2662                 (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2664         if(cursor_x >= x1 && 
2665                 cursor_x < x2 &&
2666                 cursor_y >= y - HANDLE_W / 2 && 
2667                 cursor_y < y + HANDLE_W / 2 &&
2668                 !ctrl_down())
2669         {
2670                 result = 1;
2673                 if(buttonpress)
2674                 {
2677                         Auto *current;
2678                         current = mwindow->session->drag_auto = autos->insert_auto(position);
2679                         ((FloatAuto*)current)->set_value(value);
2680                         mwindow->session->drag_start_percentage = current->value_to_percentage();
2681                         mwindow->session->drag_start_position = current->position;
2682                         mwindow->session->drag_origin_x = cursor_x;
2683                         mwindow->session->drag_origin_y = cursor_y;
2684                         mwindow->session->drag_handle = 0;
2686                 }
2687         }
2690         return result;
2693 void TrackCanvas::draw_toggleline(int center_pixel, 
2694         int x1,
2695         int y1,
2696         int x2,
2697         int y2,
2698         int color)
2700         set_color(BLACK);
2701         draw_line(x1, center_pixel + y1 + 1, x2, center_pixel + y1 + 1);
2702         set_color(color);
2703         draw_line(x1, center_pixel + y1, x2, center_pixel + y1);
2705         if(y2 != y1)
2706         {
2707                 set_color(BLACK);
2708                 draw_line(x2 + 1, center_pixel + y1, x2 + 1, center_pixel + y2);
2709                 set_color(color);
2710                 draw_line(x2, center_pixel + y1, x2, center_pixel + y2);
2711         }
2714 int TrackCanvas::test_toggleline(Autos *autos,
2715         int center_pixel, 
2716         int x1,
2717         int y1,
2718         int x2,
2719         int y2, 
2720         int cursor_x, 
2721         int cursor_y, 
2722         int buttonpress)
2724         int result = 0;
2725         if(cursor_x >= x1 && cursor_x < x2)
2726         {
2727                 int miny = center_pixel + y1 - HANDLE_W / 2;
2728                 int maxy = center_pixel + y1 + HANDLE_W / 2;
2729                 if(cursor_y >= miny && cursor_y < maxy) 
2730                 {
2731                         result = 1;
2733                         if(buttonpress)
2734                         {
2737                                 Auto *current;
2738                                 double position = (double)(cursor_x +
2739                                                 mwindow->edl->local_session->view_start) * 
2740                                         mwindow->edl->local_session->zoom_sample / 
2741                                         mwindow->edl->session->sample_rate;
2742                                 int64_t unit_position = autos->track->to_units(position, 0);
2743                                 int new_value = (int)((IntAutos*)autos)->get_automation_constant(unit_position, unit_position);
2745                                 current = mwindow->session->drag_auto = autos->insert_auto(unit_position);
2746                                 ((IntAuto*)current)->value = new_value;
2747                                 mwindow->session->drag_start_percentage = current->value_to_percentage();
2748                                 mwindow->session->drag_start_position = current->position;
2749                                 mwindow->session->drag_origin_x = cursor_x;
2750                                 mwindow->session->drag_origin_y = cursor_y;
2752                         }
2753                 }
2754         };
2755         return result;
2758 void TrackCanvas::calculate_viewport(Track *track, 
2759         double &view_start,   // Seconds
2760         double &unit_start,
2761         double &view_end,     // Seconds
2762         double &unit_end,
2763         double &yscale,
2764         int &center_pixel,
2765         double &zoom_sample,
2766         double &zoom_units)
2768         view_start = (double)mwindow->edl->local_session->view_start * 
2769                 mwindow->edl->local_session->zoom_sample /
2770                 mwindow->edl->session->sample_rate;
2771         unit_start = track->to_doubleunits(view_start);
2772         view_end = (double)(mwindow->edl->local_session->view_start + 
2773                 get_w()) * 
2774                 mwindow->edl->local_session->zoom_sample / 
2775                 mwindow->edl->session->sample_rate;
2776         unit_end = track->to_doubleunits(view_end);
2777         yscale = mwindow->edl->local_session->zoom_track;
2778         center_pixel = (int)(track->y_pixel + yscale / 2) + 
2779                 (mwindow->edl->session->show_titles ? 
2780                         mwindow->theme->get_image("title_bg_data")->get_h() : 
2781                         0);
2782         zoom_sample = mwindow->edl->local_session->zoom_sample;
2784         zoom_units = track->to_doubleunits(zoom_sample / mwindow->edl->session->sample_rate);
2787 float TrackCanvas::percentage_to_value(float percentage, 
2788         int is_toggle,
2789         Auto *reference,
2790         int autogrouptype)
2792         float result;
2793         if(is_toggle)
2794         {
2795                 if(percentage > 0.5) 
2796                         result = 1;
2797                 else
2798                         result = 0;
2799         }
2800         else
2801         {
2802                 float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2803                 float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2804                 float automation_range = automation_max - automation_min;
2806                 result = percentage * automation_range + automation_min;
2807                 if(reference)
2808                 {
2809                         FloatAuto *ptr = (FloatAuto*)reference;
2810                         result -= ptr->get_value();
2811                 }
2812         }
2813         return result;
2817 void TrackCanvas::calculate_auto_position(double *x, 
2818         double *y,
2819         double *in_x,
2820         double *in_y,
2821         double *out_x,
2822         double *out_y,
2823         Auto *current,
2824         double unit_start,
2825         double zoom_units,
2826         double yscale,
2827         int autogrouptype)
2829         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2830         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2831         float automation_range = automation_max - automation_min;
2832         FloatAuto *ptr = (FloatAuto*)current;
2833         *x = (double)(ptr->position - unit_start) / zoom_units;
2834         *y = ((ptr->get_value() - automation_min) /
2835                 automation_range - 0.5) * 
2836                 -yscale;
2837         if(in_x)
2838         {
2839                 *in_x = (double)(ptr->position + 
2840                         ptr->control_in_position - 
2841                         unit_start) /
2842                         zoom_units;
2843         }
2844         if(in_y)
2845         {
2846                 *in_y = (((ptr->get_value() + ptr->control_in_value) -
2847                         automation_min) /
2848                         automation_range - 0.5) *
2849                         -yscale;
2850         }
2851         if(out_x)
2852         {
2853                 *out_x = (double)(ptr->position + 
2854                         ptr->control_out_position - 
2855                         unit_start) /
2856                         zoom_units;
2857         }
2858         if(out_y)
2859         {
2860                 *out_y = (((ptr->get_value() + ptr->control_out_value) -
2861                         automation_min) /
2862                         automation_range - 0.5) *
2863                         -yscale;
2864         }
2871 int TrackCanvas::do_float_autos(Track *track, 
2872                 Autos *autos, 
2873                 int cursor_x, 
2874                 int cursor_y, 
2875                 int draw, 
2876                 int buttonpress,
2877                 int color,
2878                 Auto* &auto_instance,
2879                 int autogrouptype)
2881         int result = 0;
2883         double view_start;
2884         double unit_start;
2885         double view_end;
2886         double unit_end;
2887         double yscale;
2888         int center_pixel;
2889         double zoom_sample;
2890         double zoom_units;
2891         double ax, ay, ax2, ay2;
2892         double in_x2, in_y2, out_x2, out_y2;
2893         int draw_auto;
2894         double slope;
2895         int skip = 0;
2896         
2897         auto_instance = 0;
2899         calculate_viewport(track, 
2900                 view_start,
2901                 unit_start,
2902                 view_end,
2903                 unit_end,
2904                 yscale,
2905                 center_pixel,
2906                 zoom_sample,
2907                 zoom_units);
2911 // Get first auto before start
2912         Auto *current = 0;
2913         Auto *previous = 0;
2914         for(current = autos->last; 
2915                 current && current->position >= unit_start; 
2916                 current = PREVIOUS)
2917                 ;
2919         if(current)
2920         {
2921                 calculate_auto_position(&ax, 
2922                         &ay,
2923                         0,
2924                         0,
2925                         0,
2926                         0,
2927                         current,
2928                         unit_start,
2929                         zoom_units,
2930                         yscale,
2931                         autogrouptype);
2932                 current = NEXT;
2933         }
2934         else
2935         {
2936                 current = autos->first ? autos->first : autos->default_auto;
2937                 if(current)
2938                 {
2939                         calculate_auto_position(&ax, 
2940                                 &ay,
2941                                 0,
2942                                 0,
2943                                 0,
2944                                 0,
2945                                 current,
2946                                 unit_start,
2947                                 zoom_units,
2948                                 yscale,
2949                                 autogrouptype);
2950                         ax = 0;
2951                 }
2952                 else
2953                 {
2954                         ax = 0;
2955                         ay = 0;
2956                 }
2957         }
2963         do
2964         {
2965                 skip = 0;
2966                 draw_auto = 1;
2968                 if(current)
2969                 {
2970                         calculate_auto_position(&ax2, 
2971                                 &ay2,
2972                                 &in_x2,
2973                                 &in_y2,
2974                                 &out_x2,
2975                                 &out_y2,
2976                                 current,
2977                                 unit_start,
2978                                 zoom_units,
2979                                 yscale,
2980                                 autogrouptype);
2981                 }
2982                 else
2983                 {
2984                         ax2 = get_w();
2985                         ay2 = ay;
2986                         skip = 1;
2987                 }
2989                 slope = (ay2 - ay) / (ax2 - ax);
2991                 if(ax2 > get_w())
2992                 {
2993                         draw_auto = 0;
2994                         ax2 = get_w();
2995                         ay2 = ay + slope * (get_w() - ax);
2996                 }
2997                 
2998                 if(ax < 0)
2999                 {
3000                         ay = ay + slope * (0 - ax);
3001                         ax = 0;
3002                 }
3017 // Draw handle
3018                 if(current && !result)
3019                 {
3020                         if(current != autos->default_auto)
3021                         {
3022                                 if(!draw)
3023                                 {
3024                                         if(track->record)
3025                                                 result = test_floatauto(current, 
3026                                                         (int)ax2, 
3027                                                         (int)ay2, 
3028                                                         (int)in_x2,
3029                                                         (int)in_y2,
3030                                                         (int)out_x2,
3031                                                         (int)out_y2,
3032                                                         (int)center_pixel, 
3033                                                         (int)yscale, 
3034                                                         cursor_x, 
3035                                                         cursor_y, 
3036                                                         buttonpress);
3037                                         if (result) 
3038                                                 auto_instance = current;
3039                                 }
3040                                 else
3041                                 if(draw_auto)
3042                                         draw_floatauto((FloatAuto*)current, 
3043                                                 (int)ax2, 
3044                                                 (int)ay2, 
3045                                                 (int)in_x2,
3046                                                 (int)in_y2,
3047                                                 (int)out_x2,
3048                                                 (int)out_y2,
3049                                                 (int)center_pixel, 
3050                                                 (int)yscale,
3051                                                 color);
3052                         }
3053                 }
3059 // Draw joining line
3060                 if(!draw)
3061                 {
3062                         if(!result)
3063                         {
3064                                 if(track->record && buttonpress != 3)
3065                                 {
3066                                         result = test_floatline(center_pixel, 
3067                                                 (FloatAutos*)autos,
3068                                                 unit_start,
3069                                                 zoom_units,
3070                                                 yscale,
3071                                                 (int)ax,
3072 // Exclude auto coverage from the end of the line.  The auto overlaps
3073                                                 (int)ax2 - HANDLE_W / 2,
3074                                                 cursor_x, 
3075                                                 cursor_y, 
3076                                                 buttonpress,
3077                                                 autogrouptype);
3078                                 }
3079                         }
3080                 }
3081                 else
3082                         draw_floatline(center_pixel,
3083                                 (FloatAuto*)previous,
3084                                 (FloatAuto*)current,
3085                                 (FloatAutos*)autos,
3086                                 unit_start,
3087                                 zoom_units,
3088                                 yscale,
3089                                 (int)ax, 
3090                                 (int)ay, 
3091                                 (int)ax2, 
3092                                 (int)ay2,
3093                                 color,
3094                                 autogrouptype);
3102                 if(current)
3103                 {
3104                         previous = current;
3105                         current = NEXT;
3106                 }
3110                 ax = ax2;
3111                 ay = ay2;
3112         }while(current && 
3113                 current->position <= unit_end && 
3114                 !result);
3116 //printf("TrackCanvas::do_float_autos 100\n");
3125         if(ax < get_w() && !result)
3126         {
3127                 ax2 = get_w();
3128                 ay2 = ay;
3129                 if(!draw)
3130                 {
3131                         if(track->record && buttonpress != 3)
3132                         {
3133                                 result = test_floatline(center_pixel, 
3134                                         (FloatAutos*)autos,
3135                                         unit_start,
3136                                         zoom_units,
3137                                         yscale,
3138                                         (int)ax,
3139                                         (int)ax2,
3140                                         cursor_x, 
3141                                         cursor_y, 
3142                                         buttonpress,
3143                                         autogrouptype);
3144                         }
3145                 }
3146                 else
3147                         draw_floatline(center_pixel, 
3148                                 (FloatAuto*)previous,
3149                                 (FloatAuto*)current,
3150                                 (FloatAutos*)autos,
3151                                 unit_start,
3152                                 zoom_units,
3153                                 yscale,
3154                                 (int)ax, 
3155                                 (int)ay, 
3156                                 (int)ax2, 
3157                                 (int)ay2,
3158                                 color,
3159                                 autogrouptype);
3160         }
3169         return result;
3173 int TrackCanvas::do_toggle_autos(Track *track, 
3174                 Autos *autos, 
3175                 int cursor_x, 
3176                 int cursor_y, 
3177                 int draw, 
3178                 int buttonpress,
3179                 int color,
3180                 Auto * &auto_instance)
3182         int result = 0;
3183         double view_start;
3184         double unit_start;
3185         double view_end;
3186         double unit_end;
3187         double yscale;
3188         int center_pixel;
3189         double zoom_sample;
3190         double zoom_units;
3191         double ax, ay, ax2, ay2;
3192         
3193         auto_instance = 0;
3195         calculate_viewport(track, 
3196                 view_start,
3197                 unit_start,
3198                 view_end,
3199                 unit_end,
3200                 yscale,
3201                 center_pixel,
3202                 zoom_sample,
3203                 zoom_units);
3206         double high = -yscale * 0.8 / 2;
3207         double low = yscale * 0.8 / 2;
3209 // Get first auto before start
3210         Auto *current;
3211         for(current = autos->last; current && current->position >= unit_start; current = PREVIOUS)
3212                 ;
3214         if(current)
3215         {
3216                 ax = 0;
3217                 ay = ((IntAuto*)current)->value > 0 ? high : low;
3218                 current = NEXT;
3219         }
3220         else
3221         {
3222                 current = autos->first ? autos->first : autos->default_auto;
3223                 if(current)
3224                 {
3225                         ax = 0;
3226                         ay = ((IntAuto*)current)->value > 0 ? high : low;
3227                 }
3228                 else
3229                 {
3230                         ax = 0;
3231                         ay = yscale;
3232                 }
3233         }
3235         do
3236         {
3237                 if(current)
3238                 {
3239                         ax2 = (double)(current->position - unit_start) / zoom_units;
3240                         ay2 = ((IntAuto*)current)->value > 0 ? high : low;
3241                 }
3242                 else
3243                 {
3244                         ax2 = get_w();
3245                         ay2 = ay;
3246                 }
3248                 if(ax2 > get_w()) ax2 = get_w();
3250             if(current && !result) 
3251                 {
3252                         if(current != autos->default_auto)
3253                         {
3254                                 if(!draw)
3255                                 {
3256                                         if(track->record)
3257                                         {
3258                                                 result = test_auto(current, 
3259                                                         (int)ax2, 
3260                                                         (int)ay2, 
3261                                                         (int)center_pixel, 
3262                                                         (int)yscale, 
3263                                                         cursor_x, 
3264                                                         cursor_y, 
3265                                                         buttonpress);
3266                                                 if (result)
3267                                                         auto_instance = current;
3268                                         }
3269                                 }
3270                                 else
3271                                         draw_auto(current, 
3272                                                 (int)ax2, 
3273                                                 (int)ay2, 
3274                                                 (int)center_pixel, 
3275                                                 (int)yscale,
3276                                                 color);
3277                         }
3279                         current = NEXT;
3280                 }
3282                 if(!draw)
3283                 {
3284                         if(!result)
3285                         {
3286                                 if(track->record && buttonpress != 3)
3287                                 {
3288                                         result = test_toggleline(autos, 
3289                                                 center_pixel, 
3290                                                 (int)ax, 
3291                                                 (int)ay, 
3292                                                 (int)ax2, 
3293                                                 (int)ay2,
3294                                                 cursor_x, 
3295                                                 cursor_y, 
3296                                                 buttonpress);
3297                                 }
3298                         }
3299                 }
3300                 else
3301                         draw_toggleline(center_pixel, 
3302                                 (int)ax, 
3303                                 (int)ay, 
3304                                 (int)ax2, 
3305                                 (int)ay2,
3306                                 color);
3308                 ax = ax2;
3309                 ay = ay2;
3310         }while(current && current->position <= unit_end && !result);
3312         if(ax < get_w() && !result)
3313         {
3314                 ax2 = get_w();
3315                 ay2 = ay;
3316                 if(!draw)
3317                 {
3318                         if(track->record && buttonpress != 3)
3319                         {
3320                                 result = test_toggleline(autos,
3321                                         center_pixel, 
3322                                         (int)ax, 
3323                                         (int)ay, 
3324                                         (int)ax2, 
3325                                         (int)ay2,
3326                                         cursor_x, 
3327                                         cursor_y, 
3328                                         buttonpress);
3329                         }
3330                 }
3331                 else
3332                         draw_toggleline(center_pixel, 
3333                                 (int)ax, 
3334                                 (int)ay, 
3335                                 (int)ax2, 
3336                                 (int)ay2,
3337                                 color);
3338         }
3339         return result;
3342 int TrackCanvas::do_autos(Track *track, 
3343                 Autos *autos, 
3344                 int cursor_x, 
3345                 int cursor_y, 
3346                 int draw, 
3347                 int buttonpress,
3348                 BC_Pixmap *pixmap,
3349                 Auto * &auto_instance)
3351         int result = 0;
3353         double view_start;
3354         double unit_start;
3355         double view_end;
3356         double unit_end;
3357         double yscale;
3358         int center_pixel;
3359         double zoom_sample;
3360         double zoom_units;
3362         calculate_viewport(track, 
3363                 view_start,
3364                 unit_start,
3365                 view_end,
3366                 unit_end,
3367                 yscale,
3368                 center_pixel,
3369                 zoom_sample,
3370                 zoom_units);
3372         Auto *current;
3373         auto_instance = 0;
3375         for(current = autos->first; current && !result; current = NEXT)
3376         {
3377                 if(current->position >= unit_start && current->position < unit_end)
3378                 {
3379                         int64_t x, y;
3380                         x = (int64_t)((double)(current->position - unit_start) / 
3381                                 zoom_units - (pixmap->get_w() / 2 + 0.5));
3382                         y = center_pixel - pixmap->get_h() / 2;
3384                         if(!draw)
3385                         {
3386                                 if(cursor_x >= x && cursor_y >= y &&
3387                                         cursor_x < x + pixmap->get_w() &&
3388                                         cursor_y < y + pixmap->get_h())
3389                                 {
3390                                         result = 1;
3391                                         auto_instance = current;
3393                                         if(buttonpress && (buttonpress != 3))
3394                                         {
3395                                                 mwindow->session->drag_auto = current;
3396                                                 mwindow->session->drag_start_position = current->position;
3397                                                 mwindow->session->drag_origin_x = cursor_x;
3398                                                 mwindow->session->drag_origin_y = cursor_y;
3400                                                 double position = autos->track->from_units(current->position);
3401                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3402                                                         mwindow->edl->local_session->get_selectionend(1)) / 
3403                                                         2;
3405                                                 if(!shift_down())
3406                                                 {
3407                                                         mwindow->edl->local_session->set_selectionstart(position);
3408                                                         mwindow->edl->local_session->set_selectionend(position);
3409                                                 }
3410                                                 else
3411                                                 if(position < center)
3412                                                 {
3413                                                         mwindow->edl->local_session->set_selectionstart(position);
3414                                                 }
3415                                                 else
3416                                                         mwindow->edl->local_session->set_selectionend(position);
3417                                         }
3418                                 }
3419                         }
3420                         else
3421                                 draw_pixmap(pixmap, x, y);
3422                 }
3423         }
3424         return result;
3427 // so this means it is always >0 when keyframe is found 
3428 int TrackCanvas::do_plugin_autos(Track *track, 
3429                 int cursor_x, 
3430                 int cursor_y, 
3431                 int draw, 
3432                 int buttonpress,
3433                 Plugin* &keyframe_plugin,
3434                 KeyFrame* &keyframe_instance)
3436         int result = 0;
3438         double view_start;
3439         double unit_start;
3440         double view_end;
3441         double unit_end;
3442         double yscale;
3443         int center_pixel;
3444         double zoom_sample;
3445         double zoom_units;
3447         if(!track->expand_view) return 0;
3449         calculate_viewport(track, 
3450                 view_start,
3451                 unit_start,
3452                 view_end,
3453                 unit_end,
3454                 yscale,
3455                 center_pixel,
3456                 zoom_sample,
3457                 zoom_units);
3461         for(int i = 0; i < track->plugin_set.total && !result; i++)
3462         {
3463                 PluginSet *plugin_set = track->plugin_set.values[i];
3464                 int center_pixel = (int)(track->y_pixel + 
3465                         mwindow->edl->local_session->zoom_track +
3466                         (i + 0.5) * mwindow->theme->get_image("plugin_bg_data")->get_h() + 
3467                         (mwindow->edl->session->show_titles ? mwindow->theme->get_image("title_bg_data")->get_h() : 0));
3469                 for(Plugin *plugin = (Plugin*)plugin_set->first; 
3470                         plugin && !result; 
3471                         plugin = (Plugin*)plugin->next)
3472                 {
3473                         for(KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first; 
3474                                 keyframe && !result; 
3475                                 keyframe = (KeyFrame*)keyframe->next)
3476                         {
3477 //printf("TrackCanvas::draw_plugin_autos 3 %d\n", keyframe->position);
3478                                 if(keyframe->position >= unit_start && keyframe->position < unit_end)
3479                                 {
3480                                         int64_t x = (int64_t)((keyframe->position - unit_start) / zoom_units);
3481                                         int y = center_pixel - keyframe_pixmap->get_h() / 2;
3483 //printf("TrackCanvas::draw_plugin_autos 4 %d %d\n", x, center_pixel);
3484                                         if(!draw)
3485                                         {
3486                                                 if(cursor_x >= x && cursor_y >= y &&
3487                                                         cursor_x < x + keyframe_pixmap->get_w() &&
3488                                                         cursor_y < y + keyframe_pixmap->get_h())
3489                                                 {
3490                                                         result = 1;
3491                                                         keyframe_plugin = plugin;
3492                                                         keyframe_instance = keyframe;
3494                                                         if(buttonpress)
3495                                                         {
3496                                                                 mwindow->session->drag_auto = keyframe;
3497                                                                 mwindow->session->drag_start_position = keyframe->position;
3498                                                                 mwindow->session->drag_origin_x = cursor_x;
3499                                                                 mwindow->session->drag_origin_y = cursor_y;
3501                                                                 double position = track->from_units(keyframe->position);
3502                                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3503                                                                         mwindow->edl->local_session->get_selectionend(1)) / 
3504                                                                         2;
3506                                                                 if(!shift_down())
3507                                                                 {
3508                                                                         mwindow->edl->local_session->set_selectionstart(position);
3509                                                                         mwindow->edl->local_session->set_selectionend(position);
3510                                                                 }
3511                                                                 else
3512                                                                 if(position < center)
3513                                                                 {
3514                                                                         mwindow->edl->local_session->set_selectionstart(position);
3515                                                                 }
3516                                                                 else
3517                                                                         mwindow->edl->local_session->set_selectionend(position);
3518                                                         }
3519                                                 }
3520                                         }
3521                                         else
3522                                                 draw_pixmap(keyframe_pixmap, 
3523                                                         x, 
3524                                                         y);
3525                                 }
3526                         }
3527                 }
3528         }
3529 //printf("TrackCanvas::draw_plugin_autos 5\n");
3530         return result;
3533 void TrackCanvas::draw_overlays()
3535         int new_cursor, update_cursor, rerender;
3537 // Move background pixmap to foreground pixmap
3538         draw_pixmap(background_pixmap, 
3539                 0, 
3540                 0,
3541                 get_w(),
3542                 get_h(),
3543                 0,
3544                 0);
3546 // In/Out points
3547         draw_inout_points();
3549 // Transitions
3550         if(mwindow->edl->session->auto_conf->transitions) draw_transitions();
3552 // Plugins
3553         draw_plugins();
3555 // Loop points
3556         draw_loop_points();
3557         draw_brender_start();
3559 // Highlighted areas
3560         draw_highlighting();
3562 // Automation
3563         do_keyframes(0, 
3564                 0, 
3565                 1, 
3566                 0, 
3567                 new_cursor, 
3568                 update_cursor,
3569                 rerender);
3571 // Selection cursor
3572         if(gui->cursor) gui->cursor->restore(1);
3574 // Handle dragging
3575         draw_drag_handle();
3577 // Playback cursor
3578         draw_playback_cursor();
3582 int TrackCanvas::activate()
3584         if(!active)
3585         {
3586                 get_top_level()->deactivate();
3587                 active = 1;
3588                 set_active_subwindow(this);
3589                 gui->cursor->activate();
3590         }
3591         return 0;
3594 int TrackCanvas::deactivate()
3596         if(active)
3597         {
3598                 active = 0;
3599                 gui->cursor->deactivate();
3600         }
3601         return 0;
3605 void TrackCanvas::update_drag_handle()
3607         double new_position;
3609         new_position = 
3610                 (double)(get_cursor_x() + mwindow->edl->local_session->view_start) *
3611                 mwindow->edl->local_session->zoom_sample /
3612                 mwindow->edl->session->sample_rate;
3613         new_position = 
3614                 mwindow->edl->align_to_frame(new_position, 0);
3617         if(new_position != mwindow->session->drag_position)
3618         {
3619                 mwindow->session->drag_position = new_position;
3620                 gui->mainclock->update(new_position);
3621 // Que the CWindow.  Doesn't do anything if selectionstart and selection end 
3622 // aren't changed.
3623 //              mwindow->cwindow->update(1, 0, 0);
3624         }
3627 int TrackCanvas::update_drag_edit()
3629         int result = 0;
3630         
3631         
3632         
3633         return result;
3636 #define UPDATE_DRAG_HEAD(do_clamp) \
3637         int result = 0; \
3638         int x = cursor_x - mwindow->session->drag_origin_x; \
3639         int y = cursor_y - mwindow->session->drag_origin_y; \
3641         if(!current->autos->track->record) return 0; \
3642         double view_start; \
3643         double unit_start; \
3644         double view_end; \
3645         double unit_end; \
3646         double yscale; \
3647         int center_pixel; \
3648         double zoom_sample; \
3649         double zoom_units; \
3651         calculate_viewport(current->autos->track,  \
3652                 view_start, \
3653                 unit_start, \
3654                 view_end, \
3655                 unit_end, \
3656                 yscale, \
3657                 center_pixel, \
3658                 zoom_sample, \
3659                 zoom_units); \
3661         float percentage = (float)(mwindow->session->drag_origin_y - cursor_y) / \
3662                 yscale +  \
3663                 mwindow->session->drag_start_percentage; \
3664         if(do_clamp) CLAMP(percentage, 0, 1); \
3666         int64_t position = Units::to_int64(zoom_units * \
3667                 (cursor_x - mwindow->session->drag_origin_x) + \
3668                 mwindow->session->drag_start_position); \
3670         if((do_clamp) && position < 0) position = 0;
3680 int TrackCanvas::update_drag_floatauto(int cursor_x, int cursor_y)
3682         FloatAuto *current = (FloatAuto*)mwindow->session->drag_auto;
3684         UPDATE_DRAG_HEAD(mwindow->session->drag_handle == 0);
3686         float value;
3687         float old_value;
3689         switch(mwindow->session->drag_handle)
3690         {
3691 // Center
3692                 case 0:
3693 // Snap to nearby values
3694                         old_value = current->get_value();
3695                         if(shift_down())
3696                         {
3697                                 double value1;
3698                                 double distance1;
3699                                 double value2;
3700                                 double distance2;
3702                                 if(current->previous)
3703                                 {
3704                                         int autogrouptype = current->previous->autos->autogrouptype;
3705                                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
3706                                         value1 = ((FloatAuto*)current->previous)->get_value();
3707                                         distance1 = fabs(value - value1);
3708                                         current->set_value(value1);
3709                                 }
3711                                 if(current->next)
3712                                 {
3713                                         int autogrouptype = current->next->autos->autogrouptype;
3714                                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
3715                                         value2 = ((FloatAuto*)current->next)->get_value();
3716                                         distance2 = fabs(value - value2);
3717                                         if(!current->previous || distance2 < distance1)
3718                                         {
3719                                                 current->set_value(value2);
3720                                         }
3721                                 }
3723                                 if(!current->previous && !current->next)
3724                                 {
3725                                         current->set_value(((FloatAutos*)current->autos)->default_);
3726                                 }
3727                                 value = current->get_value();
3728                         }
3729                         else
3730                         {
3731                                 int autogrouptype = current->autos->autogrouptype;
3732                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3733                         }
3735                         if(value != old_value || position != current->position)
3736                         {
3737                                 result = 1;
3738                                 float change = value - old_value;               
3739                                 current->set_value(value);
3740                                 current->position = position;
3741                                 synchronize_autos(change, current->autos->track, current, 0);
3743                                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3744                                 Units::totext(string2, 
3745                                         current->autos->track->from_units(current->position),
3746                                         mwindow->edl->session->time_format,
3747                                         mwindow->edl->session->sample_rate,
3748                                         mwindow->edl->session->frame_rate,
3749                                         mwindow->edl->session->frames_per_foot);
3750                                 sprintf(string, "%s, %.2f", string2, current->get_value());
3751                                 gui->show_message(string);
3752                         }
3753                         break;
3755 // In control
3756                 case 1:
3757                 {
3758                         int autogrouptype = current->autos->autogrouptype;
3759                         value = percentage_to_value(percentage, 0, current, autogrouptype);
3760                         position = MIN(0, position);
3761                         if(value != current->control_in_value || 
3762                                 position != current->control_in_position)
3763                         {
3764                                 result = 1;
3765                                 current->control_in_value = value;
3766                                 current->control_in_position = position;
3767                                 synchronize_autos(0, current->autos->track, current, 0);
3769                                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3770                                 Units::totext(string2, 
3771                                         current->autos->track->from_units(current->control_in_position),
3772                                         mwindow->edl->session->time_format,
3773                                         mwindow->edl->session->sample_rate,
3774                                         mwindow->edl->session->frame_rate,
3775                                         mwindow->edl->session->frames_per_foot);
3776                                 sprintf(string, "%s, %.2f", string2, current->control_in_value);
3777                                 gui->show_message(string);
3778                         }
3779                 }
3780                         break;
3782 // Out control
3783                 case 2:
3784                 {
3785                         int autogrouptype = current->autos->autogrouptype;
3786                         value = percentage_to_value(percentage, 0, current, autogrouptype);
3787                         position = MAX(0, position);
3788                         if(value != current->control_out_value || 
3789                                 position != current->control_out_position)
3790                         {
3791                                 result = 1;
3792                                 current->control_out_value = value;
3793                                 current->control_out_position = position;
3794                                 synchronize_autos(0, current->autos->track, current, 0);
3796                                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3797                                 Units::totext(string2, 
3798                                         current->autos->track->from_units(
3799                                                 ((FloatAuto*)current)->control_out_position),
3800                                         mwindow->edl->session->time_format,
3801                                         mwindow->edl->session->sample_rate,
3802                                         mwindow->edl->session->frame_rate,
3803                                         mwindow->edl->session->frames_per_foot);
3804                                 sprintf(string, "%s, %.2f", 
3805                                         string2, 
3806                                         ((FloatAuto*)current)->control_out_value);
3807                                 gui->show_message(string);
3808                         }
3809                 }
3810                         break;
3811         }
3813         return result;
3816 int TrackCanvas::update_drag_toggleauto(int cursor_x, int cursor_y)
3818         IntAuto *current = (IntAuto*)mwindow->session->drag_auto;
3820         UPDATE_DRAG_HEAD(1);
3821         int value = (int)percentage_to_value(percentage, 1, 0, AUTOGROUPTYPE_INT255);
3823         if(value != current->value || position != current->position)
3824         {
3825                 result = 1;
3826                 current->value = value;
3827                 current->position = position;
3829                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3830                 Units::totext(string2, 
3831                         current->autos->track->from_units(current->position),
3832                         mwindow->edl->session->time_format,
3833                         mwindow->edl->session->sample_rate,
3834                         mwindow->edl->session->frame_rate,
3835                         mwindow->edl->session->frames_per_foot);
3836                 sprintf(string, "%s, %d", string2, current->value);
3837                 gui->show_message(string);
3838         }
3840         return result;
3843 // Autos which can't change value through dragging.
3845 int TrackCanvas::update_drag_auto(int cursor_x, int cursor_y)
3847         Auto *current = (Auto*)mwindow->session->drag_auto;
3849         UPDATE_DRAG_HEAD(1)
3850         if(position != current->position)
3851         {
3852                 result = 1;
3853                 current->position = position;
3855                 char string[BCTEXTLEN];
3856                 Units::totext(string, 
3857                         current->autos->track->from_units(current->position),
3858                         mwindow->edl->session->time_format,
3859                         mwindow->edl->session->sample_rate,
3860                         mwindow->edl->session->frame_rate,
3861                         mwindow->edl->session->frames_per_foot);
3862                 gui->show_message(string);
3864                 double position_f = current->autos->track->from_units(current->position);
3865                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3866                         mwindow->edl->local_session->get_selectionend(1)) / 
3867                         2;
3868                 if(!shift_down())
3869                 {
3870                         mwindow->edl->local_session->set_selectionstart(position_f);
3871                         mwindow->edl->local_session->set_selectionend(position_f);
3872                 }
3873                 else
3874                 if(position_f < center_f)
3875                 {
3876                         mwindow->edl->local_session->set_selectionstart(position_f);
3877                 }
3878                 else
3879                         mwindow->edl->local_session->set_selectionend(position_f);
3880         }
3883         return result;
3886 int TrackCanvas::update_drag_pluginauto(int cursor_x, int cursor_y)
3888         KeyFrame *current = (KeyFrame*)mwindow->session->drag_auto;
3890         UPDATE_DRAG_HEAD(1)
3891         if(position != current->position)
3892         {
3893 //      printf("uida: autos: %p, track: %p ta: %p\n", current->autos, current->autos->track, current->autos->track->automation);
3894                 Track *track = current->autos->track;
3895                 PluginAutos *pluginautos = (PluginAutos *)current->autos;
3896                 PluginSet *pluginset;
3897                 Plugin *plugin;
3898 // figure out the correct pluginset & correct plugin 
3899                 int found = 0;
3900                 for(int i = 0; i < track->plugin_set.total; i++)
3901                 {
3902                         pluginset = track->plugin_set.values[i];
3903                         for(plugin = (Plugin *)pluginset->first; plugin; plugin = (Plugin *)plugin->next)
3904                         {
3905                                 KeyFrames *keyframes = plugin->keyframes;
3906                                 for(KeyFrame *currentkeyframe = (KeyFrame *)keyframes->first; currentkeyframe; currentkeyframe = (KeyFrame *) currentkeyframe->next)
3907                                 {
3908                                         if (currentkeyframe == current) 
3909                                         {
3910                                                 found = 1;
3911                                                 break;
3912                                         }
3914                                 }
3915                                 if (found) break;                       
3916                         }
3917                         if (found) break;                       
3918                 }       
3919         
3920                 mwindow->session->plugin_highlighted = plugin;
3921                 mwindow->session->track_highlighted = track;
3922                 result = 1;
3923                 current->position = position;
3925                 char string[BCTEXTLEN];
3926                 Units::totext(string, 
3927                         current->autos->track->from_units(current->position),
3928                         mwindow->edl->session->time_format,
3929                         mwindow->edl->session->sample_rate,
3930                         mwindow->edl->session->frame_rate,
3931                         mwindow->edl->session->frames_per_foot);
3932                 gui->show_message(string);
3934                 double position_f = current->autos->track->from_units(current->position);
3935                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3936                         mwindow->edl->local_session->get_selectionend(1)) / 
3937                         2;
3938                 if(!shift_down())
3939                 {
3940                         mwindow->edl->local_session->set_selectionstart(position_f);
3941                         mwindow->edl->local_session->set_selectionend(position_f);
3942                 }
3943                 else
3944                 if(position_f < center_f)
3945                 {
3946                         mwindow->edl->local_session->set_selectionstart(position_f);
3947                 }
3948                 else
3949                         mwindow->edl->local_session->set_selectionend(position_f);
3950         }
3953         return result;
3956 void TrackCanvas::update_drag_caption()
3958         switch(mwindow->session->current_operation)
3959         {
3960                 case DRAG_FADE:
3961                         
3962                         break;
3963         }
3968 int TrackCanvas::cursor_motion_event()
3970         int result, cursor_x, cursor_y;
3971         int update_clock = 0;
3972         int update_zoom = 0;
3973         int update_scroll = 0;
3974         int update_overlay = 0;
3975         int update_cursor = 0;
3976         int new_cursor = 0;
3977         int rerender = 0;
3978         double position = 0;
3979 //printf("TrackCanvas::cursor_motion_event 1\n");
3980         result = 0;
3982 // Default cursor
3983         switch(mwindow->edl->session->editing_mode)
3984         {
3985                 case EDITING_ARROW: new_cursor = ARROW_CURSOR; break;
3986                 case EDITING_IBEAM: new_cursor = IBEAM_CURSOR; break;
3987         }
3989         switch(mwindow->session->current_operation)
3990         {
3991                 case DRAG_EDITHANDLE1:
3992 // Outside threshold.  Upgrade status
3993                         if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
3994                         {
3995                                 mwindow->session->current_operation = DRAG_EDITHANDLE2;
3996                                 update_overlay = 1;
3997                         }
3998                         break;
4000                 case DRAG_EDITHANDLE2:
4001                         update_drag_handle();
4002                         update_overlay = 1;
4003                         break;
4005                 case DRAG_PLUGINHANDLE1:
4006                         if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
4007                         {
4008                                 mwindow->session->current_operation = DRAG_PLUGINHANDLE2;
4009                                 update_overlay = 1;
4010                         }
4011                         break;
4013                 case DRAG_PLUGINHANDLE2:
4014                         update_drag_handle();
4015                         update_overlay = 1;
4016                         break;
4018 // Rubber band curves
4019                 case DRAG_FADE:
4020                 case DRAG_CZOOM:
4021                 case DRAG_PZOOM:
4022                 case DRAG_CAMERA_X:
4023                 case DRAG_CAMERA_Y:
4024                 case DRAG_CAMERA_Z:
4025                 case DRAG_PROJECTOR_X:
4026                 case DRAG_PROJECTOR_Y:
4027                 case DRAG_PROJECTOR_Z:
4028                         rerender = update_overlay = update_drag_floatauto(get_cursor_x(), get_cursor_y());
4029                         break;
4031                 case DRAG_PLAY:
4032                         rerender = update_overlay = update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4033                         break;
4035                 case DRAG_MUTE:
4036                         rerender = update_overlay = update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4037                         break;
4039 // Keyframe icons are sticky
4040                 case DRAG_PAN_PRE:
4041                 case DRAG_MASK_PRE:
4042                 case DRAG_MODE_PRE:
4043                 case DRAG_PLUGINKEY_PRE:
4044                         if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
4045                         {
4046                                 mwindow->session->current_operation++;
4047                                 update_overlay = 1;
4048                         }
4049                         break;
4051                 case DRAG_PAN:
4052                 case DRAG_MASK:
4053                 case DRAG_MODE:
4054                         rerender = update_overlay = 
4055                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4056                         break;
4058                 case DRAG_PLUGINKEY:
4059                         rerender = update_overlay = 
4060                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4061                         break;
4063                 case SELECT_REGION:
4064                 {
4065                         cursor_x = get_cursor_x();
4066                         cursor_y = get_cursor_y();
4067                         position = (double)(cursor_x + mwindow->edl->local_session->view_start) * 
4068                                 mwindow->edl->local_session->zoom_sample /
4069                                 mwindow->edl->session->sample_rate;
4071                         position = mwindow->edl->align_to_frame(position, 0);
4072                         position = MAX(position, 0);
4074                         if(position < selection_midpoint1)
4075                         {
4076                                 mwindow->edl->local_session->set_selectionend(selection_midpoint1);
4077                                 mwindow->edl->local_session->set_selectionstart(position);
4078 // Que the CWindow
4079                                 gui->unlock_window();
4080                                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4081                                 gui->lock_window("TrackCanvas::cursor_motion_event 1");
4082 // Update the faders
4083                                 mwindow->update_plugin_guis();
4084                                 gui->patchbay->update();
4085                         }
4086                         else
4087                         {
4088                                 mwindow->edl->local_session->set_selectionstart(selection_midpoint1);
4089                                 mwindow->edl->local_session->set_selectionend(position);
4090 // Don't que the CWindow
4091                         }
4093                         gui->cursor->hide(0);
4094                         gui->cursor->draw(1);
4095                         flash();
4096                         result = 1;
4097                         update_clock = 1;
4098                         update_zoom = 1;
4099                         update_scroll = 1;
4100                         break;
4101                 }
4103                 default:
4104                         if(is_event_win() && cursor_inside())
4105                         {
4106 // Update clocks
4107                                 cursor_x = get_cursor_x();
4108                                 position = (double)cursor_x * 
4109                                         (double)mwindow->edl->local_session->zoom_sample / 
4110                                         (double)mwindow->edl->session->sample_rate + 
4111                                         (double)mwindow->edl->local_session->view_start * 
4112                                         (double)mwindow->edl->local_session->zoom_sample / 
4113                                         (double)mwindow->edl->session->sample_rate;
4114                                 position = mwindow->edl->align_to_frame(position, 0);
4115                                 update_clock = 1;
4117 // Update cursor
4118                                 if(do_transitions(get_cursor_x(), 
4119                                                 get_cursor_y(), 
4120                                                 0, 
4121                                                 new_cursor, 
4122                                                 update_cursor))
4123                                 {
4124                                         break;
4125                                 }
4126                                 else
4127 // Update cursor
4128                                 if(do_keyframes(get_cursor_x(), 
4129                                         get_cursor_y(), 
4130                                         0, 
4131                                         0, 
4132                                         new_cursor,
4133                                         update_cursor,
4134                                         rerender))
4135                                 {
4136                                         break;
4137                                 }
4138                                 else
4139 // Edit boundaries
4140                                 if(do_edit_handles(get_cursor_x(), 
4141                                         get_cursor_y(), 
4142                                         0, 
4143                                         new_cursor,
4144                                         update_cursor))
4145                                 {
4146                                         break;
4147                                 }
4148                                 else
4149 // Plugin boundaries
4150                                 if(do_plugin_handles(get_cursor_x(), 
4151                                         get_cursor_y(), 
4152                                         0, 
4153                                         new_cursor,
4154                                         update_cursor))
4155                                 {
4156                                         break;
4157                                 }
4158                                 else
4159                                 if(do_edits(get_cursor_x(), 
4160                                         get_cursor_y(), 
4161                                         0, 
4162                                         0, 
4163                                         update_overlay, 
4164                                         rerender,
4165                                         new_cursor,
4166                                         update_cursor))
4167                                 {
4168                                         break;
4169                                 }
4170                         }
4171                         break;
4172         }
4174 //printf("TrackCanvas::cursor_motion_event 1\n");
4175         if(update_cursor && new_cursor != get_cursor())
4176         {
4177                 set_cursor(new_cursor);
4178         }
4180 //printf("TrackCanvas::cursor_motion_event 1 %d\n", rerender);
4181         if(rerender)
4182         {
4183                 mwindow->restart_brender();
4184                 mwindow->sync_parameters(CHANGE_PARAMS);
4185                 mwindow->update_plugin_guis();
4186                 gui->unlock_window();
4187                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4188                 gui->lock_window("TrackCanvas::cursor_motion_event 2");
4189 // Update faders
4190                 gui->patchbay->update();
4191         }
4194         if(update_clock)
4195         {
4196                 if(!mwindow->cwindow->playback_engine->is_playing_back)
4197                         gui->mainclock->update(position);
4198         }
4200         if(update_zoom)
4201         {
4202                 gui->zoombar->update();
4203         }
4205         if(update_scroll)
4206         {
4207                 if(!drag_scroll && 
4208                         (cursor_x >= get_w() || cursor_x < 0 || cursor_y >= get_h() || cursor_y < 0))
4209                         start_dragscroll();
4210                 else
4211                 if(drag_scroll &&
4212                         (cursor_x < get_w() && cursor_x >= 0 && cursor_y < get_h() && cursor_y >= 0))
4213                         stop_dragscroll();
4214         }
4216         if(update_overlay)
4217         {
4218                 draw_overlays();
4219                 flash();
4220         }
4223 //printf("TrackCanvas::cursor_motion_event 100\n");
4224         return result;
4227 void TrackCanvas::start_dragscroll()
4229         if(!drag_scroll)
4230         {
4231                 drag_scroll = 1;
4232                 set_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4233 //printf("TrackCanvas::start_dragscroll 1\n");
4234         }
4237 void TrackCanvas::stop_dragscroll()
4239         if(drag_scroll)
4240         {
4241                 drag_scroll = 0;
4242                 unset_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4243 //printf("TrackCanvas::stop_dragscroll 1\n");
4244         }
4247 int TrackCanvas::repeat_event(int64_t duration)
4249         if(!drag_scroll) return 0;
4250         if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
4252         int sample_movement = 0;
4253         int track_movement = 0;
4254         int64_t x_distance = 0;
4255         int64_t y_distance = 0;
4256         double position = 0;
4257         int result = 0;
4259         switch(mwindow->session->current_operation)
4260         {
4261                 case SELECT_REGION:
4262 //printf("TrackCanvas::repeat_event 1 %d\n", mwindow->edl->local_session->view_start);
4263                         if(get_cursor_x() > get_w())
4264                         {
4265                                 x_distance = get_cursor_x() - get_w();
4266                                 sample_movement = 1;
4267                         }
4268                         else
4269                         if(get_cursor_x() < 0)
4270                         {
4271                                 x_distance = get_cursor_x();
4272                                 sample_movement = 1;
4273                         }
4275                         if(get_cursor_y() > get_h())
4276                         {
4277                                 y_distance = get_cursor_y() - get_h();
4278                                 track_movement = 1;
4279                         }
4280                         else
4281                         if(get_cursor_y() < 0)
4282                         {
4283                                 y_distance = get_cursor_y();
4284                                 track_movement = 1;
4285                         }
4286                         result = 1;
4287                         break;
4288         }
4291         if(sample_movement)
4292         {
4293                 position = (double)(get_cursor_x() + 
4294                         mwindow->edl->local_session->view_start + 
4295                         x_distance) * 
4296                         mwindow->edl->local_session->zoom_sample /
4297                         mwindow->edl->session->sample_rate;
4298                 position = mwindow->edl->align_to_frame(position, 0);
4299                 position = MAX(position, 0);
4301 //printf("TrackCanvas::repeat_event 1 %f\n", position);
4302                 switch(mwindow->session->current_operation)
4303                 {
4304                         case SELECT_REGION:
4305                                 if(position < selection_midpoint1)
4306                                 {
4307                                         mwindow->edl->local_session->set_selectionend(selection_midpoint1);
4308                                         mwindow->edl->local_session->set_selectionstart(position);
4309 // Que the CWindow
4310                                         gui->unlock_window();
4311                                         mwindow->cwindow->update(1, 0, 0);
4312                                         gui->lock_window("TrackCanvas::repeat_event");
4313 // Update the faders
4314                                         mwindow->update_plugin_guis();
4315                                         gui->patchbay->update();
4316                                 }
4317                                 else
4318                                 {
4319                                         mwindow->edl->local_session->set_selectionstart(selection_midpoint1);
4320                                         mwindow->edl->local_session->set_selectionend(position);
4321 // Don't que the CWindow
4322                                 }
4323                                 break;
4324                 }
4326                 mwindow->samplemovement(mwindow->edl->local_session->view_start + 
4327                         x_distance);
4328         }
4330         if(track_movement)
4331         {
4332                 mwindow->trackmovement(mwindow->edl->local_session->track_start + 
4333                         y_distance);
4334         }
4336         return result;
4339 int TrackCanvas::button_release_event()
4341         int redraw = 0, update_overlay = 0, result = 0;
4343         switch(mwindow->session->current_operation)
4344         {
4345                 case DRAG_EDITHANDLE2:
4346                         mwindow->session->current_operation = NO_OPERATION;
4347                         drag_scroll = 0;
4348                         result = 1;
4349                         
4350                         end_edithandle_selection();
4351                         break;
4353                 case DRAG_EDITHANDLE1:
4354                         mwindow->session->current_operation = NO_OPERATION;
4355                         drag_scroll = 0;
4356                         result = 1;
4357                         break;
4359                 case DRAG_PLUGINHANDLE2:
4360                         mwindow->session->current_operation = NO_OPERATION;
4361                         drag_scroll = 0;
4362                         result = 1;
4363                         
4364                         end_pluginhandle_selection();
4365                         break;
4367                 case DRAG_PLUGINHANDLE1:
4368                         mwindow->session->current_operation = NO_OPERATION;
4369                         drag_scroll = 0;
4370                         result = 1;
4371                         break;
4373                 case DRAG_FADE:
4374 // delete the drag_auto_gang first and remove out of order keys
4375                         synchronize_autos(0, 0, 0, -1); 
4376                 case DRAG_CZOOM:
4377                 case DRAG_PZOOM:
4378                 case DRAG_PLAY:
4379                 case DRAG_MUTE:
4380                 case DRAG_MASK:
4381                 case DRAG_MODE:
4382                 case DRAG_PAN:
4383                 case DRAG_CAMERA_X:
4384                 case DRAG_CAMERA_Y:
4385                 case DRAG_CAMERA_Z:
4386                 case DRAG_PROJECTOR_X:
4387                 case DRAG_PROJECTOR_Y:
4388                 case DRAG_PROJECTOR_Z:
4389                 case DRAG_PLUGINKEY:
4390                         mwindow->session->current_operation = NO_OPERATION;
4391                         mwindow->session->drag_handle = 0;
4392 // Remove any out-of-order keyframe
4393                         if(mwindow->session->drag_auto)
4394                         {
4395                                 mwindow->session->drag_auto->autos->remove_nonsequential(
4396                                         mwindow->session->drag_auto);
4397 //                              mwindow->session->drag_auto->autos->optimize();
4398                                 update_overlay = 1;
4399                         }
4400                         mwindow->undo->update_undo(_("keyframe"), LOAD_AUTOMATION);
4401                         result = 1;
4402                         break;
4404                 case DRAG_EDIT:
4405                 case DRAG_AEFFECT_COPY:
4406                 case DRAG_VEFFECT_COPY:
4407 // Trap in drag stop
4409                         break;
4412                 default:
4413                         if(mwindow->session->current_operation)
4414                         {
4415                                 if(mwindow->session->current_operation == SELECT_REGION)
4416                                 {
4417                                         mwindow->undo->update_undo(_("select"), LOAD_SESSION, 0, 0);
4418                                 }
4420                                 mwindow->session->current_operation = NO_OPERATION;
4421                                 drag_scroll = 0;
4422 // Traps button release events
4423 //                              result = 1;
4424                         }
4425                         break;
4426         }
4427         if (result) 
4428                 cursor_motion_event();
4429         if(update_overlay)
4430         {
4431                 draw_overlays();
4432                 flash();
4433         }
4434         if(redraw)
4435         {
4436                 draw();
4437                 flash();
4438         }
4439         return result;
4442 int TrackCanvas::do_edit_handles(int cursor_x, 
4443         int cursor_y, 
4444         int button_press, 
4445         int &new_cursor,
4446         int &update_cursor)
4448         Edit *edit_result = 0;
4449         int handle_result = 0;
4450         int result = 0;
4452         if(!mwindow->edl->session->show_assets) return 0;
4454         for(Track *track = mwindow->edl->tracks->first;
4455                 track && !result;
4456                 track = track->next)
4457         {
4458                 for(Edit *edit = track->edits->first;
4459                         edit && !result;
4460                         edit = edit->next)
4461                 {
4462                         int64_t edit_x, edit_y, edit_w, edit_h;
4463                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4465                         if(cursor_x >= edit_x && cursor_x <= edit_x + edit_w &&
4466                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h)
4467                         {
4468                                 if(cursor_x < edit_x + HANDLE_W)
4469                                 {
4470                                         edit_result = edit;
4471                                         handle_result = 0;
4472                                         result = 1;
4473                                 }
4474                                 else
4475                                 if(cursor_x >= edit_x + edit_w - HANDLE_W)
4476                                 {
4477                                         edit_result = edit;
4478                                         handle_result = 1;
4479                                         result = 1;
4480                                 }
4481                                 else
4482                                 {
4483                                         result = 0;
4484                                 }
4485                         }
4486                 }
4487         }
4489         update_cursor = 1;
4490         if(result)
4491         {
4492                 double position;
4493                 if(handle_result == 0)
4494                 {
4495                         position = edit_result->track->from_units(edit_result->startproject);
4496                         new_cursor = LEFT_CURSOR;
4497                 }
4498                 else
4499                 if(handle_result == 1)
4500                 {
4501                         position = edit_result->track->from_units(edit_result->startproject + edit_result->length);
4502                         new_cursor = RIGHT_CURSOR;
4503                 }
4505 // Reposition cursor
4506                 if(button_press)
4507                 {
4508                         mwindow->session->drag_edit = edit_result;
4509                         mwindow->session->drag_handle = handle_result;
4510                         mwindow->session->drag_button = get_buttonpress() - 1;
4511                         mwindow->session->drag_position = position;
4512                         mwindow->session->current_operation = DRAG_EDITHANDLE1;
4513                         mwindow->session->drag_origin_x = get_cursor_x();
4514                         mwindow->session->drag_origin_y = get_cursor_y();
4515                         mwindow->session->drag_start = position;
4517                         int rerender = start_selection(position);
4518                         if(rerender)
4519                         {
4520                                 gui->unlock_window();
4521                                 mwindow->cwindow->update(1, 0, 0);
4522                                 gui->lock_window("TrackCanvas::do_edit_handles");
4523                         }
4524                         gui->timebar->update_highlights();
4525                         gui->zoombar->update();
4526                         gui->cursor->hide(0);
4527                         gui->cursor->draw(1);
4528                         draw_overlays();
4529                         flash();
4530                         flush();
4531                 }
4532         }
4534         return result;
4537 int TrackCanvas::do_plugin_handles(int cursor_x, 
4538         int cursor_y, 
4539         int button_press,
4540         int &new_cursor,
4541         int &update_cursor)
4543         Plugin *plugin_result = 0;
4544         int handle_result = 0;
4545         int result = 0;
4547         if(!mwindow->edl->session->show_assets) return 0;
4549         for(Track *track = mwindow->edl->tracks->first;
4550                 track && !result;
4551                 track = track->next)
4552         {
4553                 for(int i = 0; i < track->plugin_set.total && !result; i++)
4554                 {
4555                         PluginSet *plugin_set = track->plugin_set.values[i];
4556                         for(Plugin *plugin = (Plugin*)plugin_set->first; 
4557                                 plugin && !result; 
4558                                 plugin = (Plugin*)plugin->next)
4559                         {
4560                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
4561                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
4563                                 if(cursor_x >= plugin_x && cursor_x <= plugin_x + plugin_w &&
4564                                         cursor_y >= plugin_y && cursor_y < plugin_y + plugin_h)
4565                                 {
4566                                         if(cursor_x < plugin_x + HANDLE_W)
4567                                         {
4568                                                 plugin_result = plugin;
4569                                                 handle_result = 0;
4570                                                 result = 1;
4571                                         }
4572                                         else
4573                                         if(cursor_x >= plugin_x + plugin_w - HANDLE_W)
4574                                         {
4575                                                 plugin_result = plugin;
4576                                                 handle_result = 1;
4577                                                 result = 1;
4578                                         }
4579                                 }
4580                         }
4582                         if(result && shift_down())
4583                                 mwindow->session->trim_edits = plugin_set;
4584                 }
4585         }
4587         update_cursor = 1;
4588         if(result)
4589         {
4590                 double position;
4591                 if(handle_result == 0)
4592                 {
4593                         position = plugin_result->track->from_units(plugin_result->startproject);
4594                         new_cursor = LEFT_CURSOR;
4595                 }
4596                 else
4597                 if(handle_result == 1)
4598                 {
4599                         position = plugin_result->track->from_units(plugin_result->startproject + plugin_result->length);
4600                         new_cursor = RIGHT_CURSOR;
4601                 }
4602                 
4603                 if(button_press)
4604                 {
4605                         mwindow->session->drag_plugin = plugin_result;
4606                         mwindow->session->drag_handle = handle_result;
4607                         mwindow->session->drag_button = get_buttonpress() - 1;
4608                         mwindow->session->drag_position = position;
4609                         mwindow->session->current_operation = DRAG_PLUGINHANDLE1;
4610                         mwindow->session->drag_origin_x = get_cursor_x();
4611                         mwindow->session->drag_origin_y = get_cursor_y();
4612                         mwindow->session->drag_start = position;
4614                         int rerender = start_selection(position);
4615                         if(rerender) 
4616                         {
4617                                 gui->unlock_window();
4618                                 mwindow->cwindow->update(1, 0, 0);
4619                                 gui->lock_window("TrackCanvas::do_plugin_handles");
4620                         }
4621                         gui->timebar->update_highlights();
4622                         gui->zoombar->update();
4623                         gui->cursor->hide(0);
4624                         gui->cursor->draw(1);
4625                         draw_overlays();
4626                         flash();
4627                         flush();
4628                 }
4629         }
4630         
4631         return result;
4635 int TrackCanvas::do_tracks(int cursor_x, 
4636                 int cursor_y,
4637                 int button_press)
4639         int result = 0;
4642         if(!mwindow->edl->session->show_assets) return 0;
4645         for(Track *track = mwindow->edl->tracks->first;
4646                 track && !result;
4647                 track = track->next)
4648         {
4649                 int64_t track_x, track_y, track_w, track_h;
4650                 track_dimensions(track, track_x, track_y, track_w, track_h);
4652                 if(button_press && 
4653                         get_buttonpress() == 3 &&
4654                         cursor_y >= track_y && 
4655                         cursor_y < track_y + track_h)
4656                 {
4657                         gui->edit_menu->update(track, 0);
4658                         gui->edit_menu->activate_menu();
4659                         result = 1;
4660                 }
4661         }
4662         return result;
4665 int TrackCanvas::do_edits(int cursor_x, 
4666         int cursor_y, 
4667         int button_press,
4668         int drag_start,
4669         int &redraw,
4670         int &rerender,
4671         int &new_cursor,
4672         int &update_cursor)
4674         int result = 0;
4675         int over_edit_handle = 0;
4677         if(!mwindow->edl->session->show_assets) return 0;
4679         for(Track *track = mwindow->edl->tracks->first;
4680                 track && !result;
4681                 track = track->next)
4682         {
4683                 for(Edit *edit = track->edits->first;
4684                         edit != track->edits->last && !result;
4685                         edit = edit->next)
4686                 {
4687                         int64_t edit_x, edit_y, edit_w, edit_h;
4688                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4690 // Cursor inside a track
4691 // Cursor inside an edit
4692                         if(cursor_x >= edit_x && cursor_x < edit_x + edit_w &&
4693                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h)
4694                         {
4695 // Select duration of edit
4696                                 if(button_press)
4697                                 {
4698                                         if(get_double_click() && !drag_start)
4699                                         {
4700                                                 mwindow->edl->local_session->set_selectionstart(edit->track->from_units(edit->startproject));
4701                                                 mwindow->edl->local_session->set_selectionend(edit->track->from_units(edit->startproject) + 
4702                                                         edit->track->from_units(edit->length));
4703                                                 if(mwindow->edl->session->cursor_on_frames) 
4704                                                 {
4705                                                         mwindow->edl->local_session->set_selectionstart(
4706                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4707                                                         mwindow->edl->local_session->set_selectionend(
4708                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4709                                                 }
4710                                                 redraw = 1;
4711                                                 rerender = 1;
4712                                                 result = 1;
4713                                         }
4714                                 }
4715                                 else
4716                                 if(drag_start && track->record)
4717                                 {
4718                                         if(mwindow->edl->session->editing_mode == EDITING_ARROW)
4719                                         {
4720 // Need to create drag window
4721                                                 mwindow->session->current_operation = DRAG_EDIT;
4722                                                 mwindow->session->drag_edit = edit;
4723 //printf("TrackCanvas::do_edits 2\n");
4725 // Drag only one edit
4726                                                 if(ctrl_down())
4727                                                 {
4728                                                         mwindow->session->drag_edits->remove_all();
4729                                                         mwindow->session->drag_edits->append(edit);
4730                                                 }
4731                                                 else
4732 // Construct list of all affected edits
4733                                                 {
4734                                                         mwindow->edl->tracks->get_affected_edits(
4735                                                                 mwindow->session->drag_edits, 
4736                                                                 edit->track->from_units(edit->startproject),
4737                                                                 edit->track);
4738                                                 }
4739                                                 mwindow->session->drag_origin_x = cursor_x;
4740                                                 mwindow->session->drag_origin_y = cursor_y;
4741                                                 // Where the drag started, so we know relative position inside the edit later
4742                                                 mwindow->session->drag_position = (double)cursor_x * 
4743                                                         mwindow->edl->local_session->zoom_sample / 
4744                                                         mwindow->edl->session->sample_rate + 
4745                                                         (double)mwindow->edl->local_session->view_start * 
4746                                                         mwindow->edl->local_session->zoom_sample /
4747                                                         mwindow->edl->session->sample_rate;
4749                                                 drag_popup = new BC_DragWindow(gui, 
4750                                                         mwindow->theme->get_image("clip_icon"), 
4751                                                         get_abs_cursor_x(0) - mwindow->theme->get_image("clip_icon")->get_w() / 2,
4752                                                         get_abs_cursor_y(0) - mwindow->theme->get_image("clip_icon")->get_h() / 2);
4754                                                 result = 1;
4755                                         }
4756                                 }
4757                         }
4758                 }
4759         }
4760         return result;
4764 int TrackCanvas::test_resources(int cursor_x, int cursor_y)
4766         return 0;
4769 int TrackCanvas::do_plugins(int cursor_x, 
4770         int cursor_y, 
4771         int drag_start,
4772         int button_press,
4773         int &redraw,
4774         int &rerender)
4776         Plugin *plugin = 0;
4777         int result = 0;
4778         int done = 0;
4779         int64_t x, y, w, h;
4780         Track *track = 0;
4783         if(!mwindow->edl->session->show_assets) return 0;
4786         for(track = mwindow->edl->tracks->first;
4787                 track && !done;
4788                 track = track->next)
4789         {
4790                 if(!track->expand_view) continue;
4793                 for(int i = 0; i < track->plugin_set.total && !done; i++)
4794                 {
4795                         // first check if plugins are visible at all
4796                         if (!track->expand_view)
4797                                 continue;
4798                         PluginSet *plugin_set = track->plugin_set.values[i];
4799                         for(plugin = (Plugin*)plugin_set->first;
4800                                 plugin && !done;
4801                                 plugin = (Plugin*)plugin->next)
4802                         {
4803                                 plugin_dimensions(plugin, x, y, w, h);
4804                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
4805                                         MWindowGUI::visible(y, y + h, 0, get_h()))
4806                                 {
4807                                         if(cursor_x >= x && cursor_x < x + w &&
4808                                                 cursor_y >= y && cursor_y < y + h)
4809                                         {
4810                                                 done = 1;
4811                                                 break;
4812                                         }
4813                                 }
4814                         }
4815                 }
4816         }
4818         if(plugin)
4819         {
4820 // Start plugin popup
4821                 if(button_press)
4822                 {
4823                         if(get_buttonpress() == 3)
4824                         {
4825                                 gui->plugin_menu->update(plugin);
4826                                 gui->plugin_menu->activate_menu();
4827                                 result = 1;
4828                         } 
4829                         else
4830 // Select range of plugin on doubleclick over plugin
4831                         if (get_double_click() && !drag_start)
4832                         {
4833                                 mwindow->edl->local_session->set_selectionstart(plugin->track->from_units(plugin->startproject));
4834                                 mwindow->edl->local_session->set_selectionend(plugin->track->from_units(plugin->startproject) + 
4835                                         plugin->track->from_units(plugin->length));
4836                                 if(mwindow->edl->session->cursor_on_frames) 
4837                                 {
4838                                         mwindow->edl->local_session->set_selectionstart(
4839                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4840                                         mwindow->edl->local_session->set_selectionend(
4841                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4842                                 }
4843                                 rerender = 1;
4844                                 redraw = 1;
4845                                 result = 1;
4846                         }
4847                 }
4848                 else
4849 // Move plugin
4850                 if(drag_start && plugin->track->record)
4851                 {
4852                         if(mwindow->edl->session->editing_mode == EDITING_ARROW)
4853                         {
4854                                 if(plugin->track->data_type == TRACK_AUDIO)
4855                                         mwindow->session->current_operation = DRAG_AEFFECT_COPY;
4856                                 else
4857                                 if(plugin->track->data_type == TRACK_VIDEO)
4858                                         mwindow->session->current_operation = DRAG_VEFFECT_COPY;
4860                                 mwindow->session->drag_plugin = plugin;
4866 // Create picon
4867                                 switch(plugin->plugin_type)
4868                                 {
4869                                         case PLUGIN_STANDALONE:
4870                                         {
4871                                                 PluginServer *server = mwindow->scan_plugindb(
4872                                                         plugin->title,
4873                                                         plugin->track->data_type);
4874                                                 if (server) 
4875                                                 {
4876                                                         VFrame *frame = server->picon;
4878                                                         drag_popup = new BC_DragWindow(gui, 
4879                                                                 frame, 
4880                                                                 get_abs_cursor_x(0) - frame->get_w() / 2,
4881                                                                 get_abs_cursor_y(0) - frame->get_h() / 2);
4882                                                 }
4883                                                 break;
4884                                         }
4885                                         
4886                                         case PLUGIN_SHAREDPLUGIN:
4887                                         case PLUGIN_SHAREDMODULE:
4888                                                 drag_popup = new BC_DragWindow(gui, 
4889                                                         mwindow->theme->get_image("clip_icon"), 
4890                                                         get_abs_cursor_x(0) - mwindow->theme->get_image("clip_icon")->get_w() / 2,
4891                                                         get_abs_cursor_y(0) - mwindow->theme->get_image("clip_icon")->get_h() / 2);
4892                                                 break;
4893                                 }
4896                                 result = 1;
4897                         }
4898                 }
4899         }
4901         return result;
4904 int TrackCanvas::do_transitions(int cursor_x, 
4905         int cursor_y, 
4906         int button_press,
4907         int &new_cursor,
4908         int &update_cursor)
4910         Transition *transition = 0;
4911         int result = 0;
4912         int64_t x, y, w, h;
4916         if(!mwindow->edl->session->show_assets ||
4917                 !mwindow->edl->session->auto_conf->transitions) return 0;
4918                                         
4921         for(Track *track = mwindow->edl->tracks->first;
4922                 track && !result;
4923                 track = track->next)
4924         {
4925                 for(Edit *edit = track->edits->first;
4926                         edit;
4927                         edit = edit->next)
4928                 {
4929                         if(edit->transition)
4930                         {
4931                                 edit_dimensions(edit, x, y, w, h);
4932                                 get_transition_coords(x, y, w, h);
4934                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
4935                                         MWindowGUI::visible(y, y + h, 0, get_h()))
4936                                 {
4937                                         if(cursor_x >= x && cursor_x < x + w &&
4938                                                 cursor_y >= y && cursor_y < y + h)
4939                                         {
4940                                                 transition = edit->transition;
4941                                                 result = 1;
4942                                                 break;
4943                                         }
4944                                 }
4945                         }
4946                 }
4947         }
4948         
4949         update_cursor = 1;
4950         if(transition)
4951         {
4952                 if(!button_press)
4953                 {
4954                         new_cursor = UPRIGHT_ARROW_CURSOR;
4955                 }
4956                 else
4957                 if(get_buttonpress() == 3)
4958                 {
4959                         gui->transition_menu->update(transition);
4960                         gui->transition_menu->activate_menu();
4961                 }
4962         }
4964         return result;
4967 int TrackCanvas::button_press_event()
4969         int result = 0;
4970         int cursor_x, cursor_y;
4971         int new_cursor, update_cursor;
4973         cursor_x = get_cursor_x();
4974         cursor_y = get_cursor_y();
4975         mwindow->session->trim_edits = 0;
4977         if(is_event_win() && cursor_inside())
4978         {
4979                 double position = (double)cursor_x * 
4980                         mwindow->edl->local_session->zoom_sample /
4981                         mwindow->edl->session->sample_rate + 
4982                         (double)mwindow->edl->local_session->view_start * 
4983                         mwindow->edl->local_session->zoom_sample /
4984                         mwindow->edl->session->sample_rate;
4987                 if(!active)
4988                 {
4989                         activate();
4990                 }
4992                 if(get_buttonpress() == 1)
4993                 {
4994                         gui->unlock_window();
4995                         gui->mbuttons->transport->handle_transport(STOP, 1, 0, 0);
4996                         gui->lock_window("TrackCanvas::button_press_event");
4997                 }
4999                 int update_overlay = 0, update_cursor = 0, rerender = 0;
5001                 if(get_buttonpress() == 4)
5002                 {
5003                         if(shift_down())
5004                                 mwindow->expand_sample();
5005                         else
5006                                 mwindow->move_up(get_h() / 10);
5007                         result = 1;
5008                 }
5009                 else
5010                 if(get_buttonpress() == 5)
5011                 {
5012                         if(shift_down())
5013                                 mwindow->zoom_in_sample();
5014                         else
5015                                 mwindow->move_down(get_h() / 10);
5016                         result = 1;
5017                 }
5018                 else
5019                 switch(mwindow->edl->session->editing_mode)
5020                 {
5021 // Test handles and resource boundaries and highlight a track
5022                         case EDITING_ARROW:
5023                         {
5024                                 Edit *edit;
5025                                 int handle;
5026                                 if(mwindow->edl->session->auto_conf->transitions && 
5027                                         do_transitions(cursor_x, 
5028                                                 cursor_y, 
5029                                                 1, 
5030                                                 new_cursor, 
5031                                                 update_cursor))
5032                                 {
5033                                         break;
5034                                 }
5035                                 else
5036                                 if(do_keyframes(cursor_x, 
5037                                         cursor_y, 
5038                                         0, 
5039                                         get_buttonpress(), 
5040                                         new_cursor, 
5041                                         update_cursor,
5042                                         rerender))
5043                                 {
5044                                         break;
5045                                 }
5046                                 else
5047 // Test edit boundaries
5048                                 if(do_edit_handles(cursor_x, 
5049                                         cursor_y, 
5050                                         1, 
5051                                         new_cursor, 
5052                                         update_cursor))
5053                                 {
5054                                         break;
5055                                 }
5056                                 else
5057 // Test plugin boundaries
5058                                 if(do_plugin_handles(cursor_x, 
5059                                         cursor_y, 
5060                                         1, 
5061                                         new_cursor, 
5062                                         update_cursor))
5063                                 {
5064                                         break;
5065                                 }
5066                                 else
5067                                 if(do_edits(cursor_x, cursor_y, 1, 0, update_cursor, rerender, new_cursor, update_cursor))
5068                                 {
5069                                         break;
5070                                 }
5071                                 else
5072                                 if(do_plugins(cursor_x, cursor_y, 0, 1, update_cursor, rerender))
5073                                 {
5074                                         break;
5075                                 }
5076                                 else
5077                                 if(test_resources(cursor_x, cursor_y))
5078                                 {
5079                                         break;
5080                                 }
5081                                 else
5082                                 if(do_tracks(cursor_x, cursor_y, 1))
5083                                 {
5084                                         break;
5085                                 }
5086                                 break;
5087                         }
5089 // Test handles only and select a region
5090                         case EDITING_IBEAM:
5091                         {
5092 //printf("TrackCanvas::button_press_event %d\n", position);
5094                                 if(mwindow->edl->session->auto_conf->transitions && 
5095                                         do_transitions(cursor_x, 
5096                                                 cursor_y, 
5097                                                 1, 
5098                                                 new_cursor, 
5099                                                 update_cursor))
5100                                 {
5101                                         break;
5102                                 }
5103                                 else
5104                                 if(do_keyframes(cursor_x, 
5105                                         cursor_y, 
5106                                         0, 
5107                                         get_buttonpress(), 
5108                                         new_cursor, 
5109                                         update_cursor,
5110                                         rerender))
5111                                 {
5112                                         update_overlay = 1;
5113                                         break;
5114                                 }
5115                                 else
5116 // Test edit boundaries
5117                                 if(do_edit_handles(cursor_x, 
5118                                         cursor_y, 
5119                                         1, 
5120                                         new_cursor, 
5121                                         update_cursor))
5122                                 {
5123                                         break;
5124                                 }
5125                                 else
5126 // Test plugin boundaries
5127                                 if(do_plugin_handles(cursor_x, 
5128                                         cursor_y, 
5129                                         1, 
5130                                         new_cursor, 
5131                                         update_cursor))
5132                                 {
5133                                         break;
5134                                 }
5135                                 else
5136                                 if(do_edits(cursor_x, 
5137                                         cursor_y, 
5138                                         1, 
5139                                         0, 
5140                                         update_cursor, 
5141                                         rerender, 
5142                                         new_cursor, 
5143                                         update_cursor))
5144                                 {
5145                                         break;
5146                                 }
5147                                 else
5148                                 if(do_plugins(cursor_x, 
5149                                         cursor_y, 
5150                                         0, 
5151                                         1, 
5152                                         update_cursor, 
5153                                         rerender))
5154                                 {
5155                                         break;
5156                                 }
5157                                 else
5158                                 if(do_tracks(cursor_x, cursor_y, 1))
5159                                 {
5160                                         break;
5161                                 }
5162 // Highlight selection
5163                                 else
5164                                 {
5165 SET_TRACE
5166                                         rerender = start_selection(position);
5167 SET_TRACE
5168                                         mwindow->session->current_operation = SELECT_REGION;
5169                                         update_cursor = 1;
5170                                 }
5172                                 break;
5173                         }
5174                 }
5177 SET_TRACE
5178                 if(rerender)
5179                 {
5180                         gui->unlock_window();
5181                         mwindow->cwindow->update(1, 0, 0, 0, 1);
5183                         gui->lock_window("TrackCanvas::button_press_event 2");
5184 // Update faders
5185                         mwindow->update_plugin_guis();
5186                         gui->patchbay->update();
5187                 }
5189 SET_TRACE
5190                 if(update_overlay)
5191                 {
5192                         draw_overlays();
5193                         flash();
5194                 }
5196 SET_TRACE
5197                 if(update_cursor)
5198                 {
5199                         gui->timebar->update_highlights();
5200                         gui->cursor->hide();
5201                         gui->cursor->show();
5202                         gui->zoombar->update();
5203                         flash();
5204                         result = 1;
5205                 }
5207 SET_TRACE
5210         }
5211         return result;
5214 int TrackCanvas::start_selection(double position)
5216         int rerender = 0;
5217         position = mwindow->edl->align_to_frame(position, 0);
5220 // Extend a border
5221         if(shift_down())
5222         {
5223                 double midpoint = (mwindow->edl->local_session->get_selectionstart(1) + 
5224                         mwindow->edl->local_session->get_selectionend(1)) / 2;
5226                 if(position < midpoint)
5227                 {
5228                         mwindow->edl->local_session->set_selectionstart(position);
5229                         selection_midpoint1 = mwindow->edl->local_session->get_selectionend(1);
5230 // Que the CWindow
5231                         rerender = 1;
5232                 }
5233                 else
5234                 {
5235                         mwindow->edl->local_session->set_selectionend(position);
5236                         selection_midpoint1 = mwindow->edl->local_session->get_selectionstart(1);
5237 // Don't que the CWindow for the end
5238                 }
5239         }
5240         else
5241 // Start a new selection
5242         {
5243 //printf("TrackCanvas::start_selection %f\n", position);
5244                 mwindow->edl->local_session->set_selectionstart(position);
5245                 mwindow->edl->local_session->set_selectionend(position);
5246                 selection_midpoint1 = position;
5247 // Que the CWindow
5248                 rerender = 1;
5249         }
5250         
5251         return rerender;
5254 void TrackCanvas::end_edithandle_selection()
5256         mwindow->modify_edithandles();
5259 void TrackCanvas::end_pluginhandle_selection()
5261         mwindow->modify_pluginhandles();
5265 double TrackCanvas::time_visible()
5267         return (double)get_w() * 
5268                 mwindow->edl->local_session->zoom_sample / 
5269                 mwindow->edl->session->sample_rate;
5312 void TrackCanvas::draw_automation()
5317 int TrackCanvas::set_index_file(int flash, Asset *asset)
5319         return 0;
5323 int TrackCanvas::button_release()
5325         return 0;
5329 int TrackCanvas::auto_reposition(int &cursor_x, int &cursor_y, int64_t cursor_position)
5331         return 0;
5335 int TrackCanvas::draw_floating_handle(int flash)
5337         return 0;
5340 int TrackCanvas::draw_loop_point(int64_t position, int flash)
5342         return 0;
5345 int TrackCanvas::draw_playback_cursor(int pixel, int flash)
5347         return 0;
5351 int TrackCanvas::update_handle_selection(int64_t cursor_position)
5353         return 0;
5356 int TrackCanvas::end_translation()
5358         return 0;