r936: Sane default sample zoom...
[cinelerra_cv/mob.git] / cinelerra / trackcanvas.C
blob04dad9ad3017d947f3dc3511895d1e228c01e12d
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, 1);
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 //                                      printf("Problematic insertion\n");
546 //                                      mwindow->edl->tracks->clear(position_f, 
547 //                                              position_f + asset_length_float, 0);
548 //                              }
549                                 mwindow->paste_assets(position_f, track, !insertion);
550                                 result = 1;    // need to be one no matter what, since we have track highlited so we have to cleanup....
551                         }
552                         break;
554                 case DRAG_EDIT:
555                         mwindow->session->current_operation = NO_OPERATION;
556                         if(mwindow->session->track_highlighted)
557                         {
558                                 if(mwindow->session->track_highlighted->data_type == mwindow->session->drag_edit->track->data_type)
559                                 {
560                                         int64_t position = 0;
561                                 
562                                         position = get_drop_position (&insertion, mwindow->session->drag_edit, mwindow->session->drag_edit->length);
564                                         if (position == -1)
565                                         {
566                                                 result = 1;
567                                                 break;          // Do not do anything
568                                         }
569                                         
570                                         double position_f = mwindow->session->track_highlighted->from_units(position);
571                                         Track *track = mwindow->session->track_highlighted;
572                                         mwindow->move_edits(mwindow->session->drag_edits,
573                                                 track,
574                                                 position_f,
575                                                 !insertion);
576                                 }
578                                 result = 1;
579                         }
580                         break;
581         }
583 // since we don't have subwindows we have to terminate any drag operation
584         if(result)
585         {
586                 if (mwindow->session->track_highlighted
587                         || mwindow->session->edit_highlighted
588                         || mwindow->session->plugin_highlighted
589                         || mwindow->session->pluginset_highlighted) 
590                         redraw = 1;
591                 mwindow->session->track_highlighted = 0;
592                 mwindow->session->edit_highlighted = 0;
593                 mwindow->session->plugin_highlighted = 0;
594                 mwindow->session->pluginset_highlighted = 0;
595                 mwindow->session->current_operation = NO_OPERATION;
596         }
599 //printf("TrackCanvas::drag_stop %d %d\n", redraw, mwindow->session->current_operation);
600         if(redraw)
601         {
602                 mwindow->edl->tracks->update_y_pixels(mwindow->theme);
603                 gui->get_scrollbars();
604                 draw();
605                 gui->patchbay->update();
606                 gui->cursor->update();
607                 flash();
608                 flush();
609         }
611         return result;
615 int64_t TrackCanvas::get_drop_position (int *is_insertion, Edit *moved_edit, int64_t moved_edit_length)
617         *is_insertion = 0;
619         // get the canvas/track position
620         int cursor_x = get_relative_cursor_x();
621         double pos = (double)cursor_x * 
622                 mwindow->edl->local_session->zoom_sample / 
623                 mwindow->edl->session->sample_rate + 
624                 (double)mwindow->edl->local_session->view_start * 
625                 mwindow->edl->local_session->zoom_sample /
626                 mwindow->edl->session->sample_rate;
627         // convert to track's units to operate with them
628         Track *track = mwindow->session->track_highlighted;
629         // cursor relative position - depending on where we started the drag inside the edit
630         int64_t cursor_position;
631         if (moved_edit)  // relative cursor position depends upon grab point
632                 cursor_position = track->to_units (pos - (mwindow->session->drag_position - moved_edit->track->from_units(moved_edit->startproject)), 1);
633         else             // for clips and assets acts as they were grabbed in the middle
634                 cursor_position = track->to_units (pos , 1) - moved_edit_length / 2;
635            
636         // we use real cursor position for affinity calculations
637         int64_t real_cursor_position = track->to_units (pos, 0); 
638         if (cursor_position < 0) cursor_position = 0;
639         if (real_cursor_position < 0) real_cursor_position = 0;
640         int64_t position = -1;
641         int64_t span_start = 0;
642         int64_t span_length = 0;
643         int span_asset = 0;
644         int last_ignore = 0; // used to make sure we can ignore the last edit if that is what we are dragging
646         if (!track->edits->last)
647         {
648                 // No edits -> no problems!
649                 position = cursor_position;
650         }
651         else
652         {
653                 Edit *fake_edit = new Edit(mwindow->edl, track);
654                 int last2 = 0; // last2 is a hack that let us make virtual edits at the end so thing works for last edit also
655                                // we do this by appending two VERY long virtual edits at the end
656                 
657                 for (Edit *edit = track->edits->first; edit || last2 < 2; )
658                 {
659                 
660                         if (!edit && last_ignore)
661                         {
662                                 span_length += 100000000000000LL;
663                                 last_ignore = 0;
664                                 span_asset = 0;
665                         } else
666                         if (edit && 
667                             ((moved_edit && edit == moved_edit && edit->previous && !edit->previous->asset) ||
668                             (moved_edit && edit->previous == moved_edit  && !edit->asset)))
669                         {
670                                 span_length += edit->length;        // our fake edit spans over the edit we are moving
671                                 last_ignore = 1;
672                         } else
673                         { // This is a virtual edit
674                                 fake_edit->startproject = span_start;
675                                 fake_edit->length = span_length;
676                                 int64_t edit_x, edit_y, edit_w, edit_h;
677                                 edit_dimensions(fake_edit, edit_x, edit_y, edit_w, edit_h);
678                                 if (labs(edit_x - cursor_x) < HANDLE_W)                 // cursor is close to the beginning of an edit -> insertion
679                                 {
680                                         *is_insertion = 1;
681                                         position = span_start;
682                                 } else
683                                 if (labs(edit_x + edit_w - cursor_x) < HANDLE_W)        // cursor is close to the end of an edit -> insertion
684                                 {
685                                         *is_insertion = 1;
686                                         position = span_start + span_length;
688                                 }  else
689                                 if (!span_asset &&              // we have enough empty space to position the edit where user wants 
690                                         span_start <= cursor_position &&
691                                         span_start + span_length >= cursor_position + moved_edit_length)
692                                 {
693                                         position = cursor_position; 
694                                 } else
695                                 if (!span_asset &                               // we are inside an empty edit, but cannot push the edit as far as user wants, so 'resist moving it further'
696                                         real_cursor_position >= span_start && 
697                                         real_cursor_position < span_start + span_length && 
698                                         span_length >= moved_edit_length)
699                                 {
700                                         if (llabs(real_cursor_position - span_start) < llabs(real_cursor_position - span_start - span_length))
701                                                 position = span_start;
702                                         else
703                                                 position = span_start + span_length - moved_edit_length;
704                                 } else
705                                 if (cursor_x > edit_x && cursor_x <= edit_x + edit_w / 2) // we are inside an nonempty edit, - snap to left
706                                 {
707                                         *is_insertion = 1;
708                                         position = span_start;                          
709                                 } else
710                                 if (cursor_x > edit_x + edit_w / 2 && cursor_x <= edit_x + edit_w) // we are inside an nonempty edit, - snap to right
711                                 {
712                                         *is_insertion = 1;
713                                         position = span_start + span_length;                            
714                                 }                               
715                                 
717                                 if (position != -1) 
718                                         break;
719                                 
720                                 // This is the new edit
721                                 if (edit)
722                                 {
723                                                 span_length = edit->length;             
724                                                 span_start = edit->startproject;  
725                                                 last_ignore = 0;
726                                                 if (!edit->asset || (!moved_edit || moved_edit == edit)) 
727                                                 {
728                                                         if (moved_edit && moved_edit == edit)
729                                                                 last_ignore = 1;
730                 
731                                                         span_asset = 0;
732                                                 } else 
733                                                         span_asset = 1;
734                                 } else
735                                 {
736                                         span_start = span_length + span_start;
737                                         span_length = 100000000000000LL;
738                                         span_asset = 0;
739                                 };
740                                 
742                         }
743                         if (edit)
744                                 edit = edit->next;
745                         else
746                                 last2++;
747                         
748                 }
749                 delete fake_edit;
751         }
752         if (real_cursor_position == 0) 
753         {
754                 position = 0;
755                 *is_insertion = 1;
756         }
757 //      printf("rcp: %lli, position: %lli, insertion: %i\n", real_cursor_position, position, *is_insertion);
758         return position;
763 void TrackCanvas::draw(int mode, int hide_cursor)
765 // Swap pixmap layers
766         if(get_w() != background_pixmap->get_w() ||
767                 get_h() != background_pixmap->get_h())
768         {
769                 delete background_pixmap;
770                 background_pixmap = new BC_Pixmap(this, get_w(), get_h());
771         }
773 // Cursor disappears after resize when this is called.
774 // Cursor doesn't redraw after editing when this isn't called.
775         if(gui->cursor && hide_cursor) gui->cursor->hide();
776         draw_top_background(get_parent(), 0, 0, get_w(), get_h(), background_pixmap);
777         draw_resources(mode);
778         draw_overlays();
781 void TrackCanvas::update_cursor()
783         switch(mwindow->edl->session->editing_mode)
784         {
785                 case EDITING_ARROW: set_cursor(ARROW_CURSOR); break;
786                 case EDITING_IBEAM: set_cursor(IBEAM_CURSOR); break;
787         }
791 void TrackCanvas::test_timer()
793         if(resource_timer->get_difference() > 1000 && 
794                 !hourglass_enabled)
795         {
796                 start_hourglass();
797                 hourglass_enabled = 1;
798         }
802 void TrackCanvas::draw_indexes(Asset *asset)
804 // Don't redraw raw samples
805         if(asset->index_zoom > mwindow->edl->local_session->zoom_sample)
806                 return;
808         draw_resources(0, 1, asset);
810         draw_overlays();
811         draw_automation();
812         flash();
813         flush();
816 void TrackCanvas::draw_resources(int mode, 
817         int indexes_only, 
818         Asset *index_asset)
820         if(!mwindow->edl->session->show_assets) return;
822         if(mode != 3 && !indexes_only)
823                 resource_thread->stop_draw(!indexes_only);
825         resource_timer->update();
827 // Age resource pixmaps for deletion
828         if(!indexes_only)
829                 for(int i = 0; i < resource_pixmaps.total; i++)
830                         resource_pixmaps.values[i]->visible--;
832         if(mode == 2)
833                 resource_pixmaps.remove_all_objects();
836 // Search every edit
837         for(Track *current = mwindow->edl->tracks->first;
838                 current;
839                 current = NEXT)
840         {
841                 for(Edit *edit = current->edits->first; edit; edit = edit->next)
842                 {
843                         if(!edit->asset) continue;
844                         if(indexes_only)
845                         {
846                                 if(edit->track->data_type != TRACK_AUDIO) continue;
847                                 if(!edit->asset->test_path(index_asset->path)) continue;
848                         }
850                         int64_t edit_x, edit_y, edit_w, edit_h;
851                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
853 // Edit is visible
854                         if(MWindowGUI::visible(edit_x, edit_x + edit_w, 0, get_w()) &&
855                                 MWindowGUI::visible(edit_y, edit_y + edit_h, 0, get_h()))
856                         {
857                                 int64_t pixmap_x, pixmap_w, pixmap_h;
859 // Search for existing pixmap containing edit
860                                 for(int i = 0; i < resource_pixmaps.total; i++)
861                                 {
862                                         ResourcePixmap* pixmap = resource_pixmaps.values[i];
863 // Same pointer can be different edit if editing took place
864                                         if(pixmap->edit_id == edit->id)
865                                         {
866                                                 pixmap->visible = 1;
867                                                 break;
868                                         }
869                                 }
871 // Get new size, offset of pixmap needed
872                                 get_pixmap_size(edit, 
873                                         edit_x, 
874                                         edit_w, 
875                                         pixmap_x, 
876                                         pixmap_w, 
877                                         pixmap_h);
879 // Draw new data
880                                 if(pixmap_w && pixmap_h)
881                                 {
882 // Create pixmap if it doesn't exist
883                                         ResourcePixmap* pixmap = create_pixmap(edit, 
884                                                 edit_x, 
885                                                 pixmap_x, 
886                                                 pixmap_w, 
887                                                 pixmap_h);
888 // Resize it if it's bigger
889                                         if(pixmap_w > pixmap->pixmap_w ||
890                                                 pixmap_h > pixmap->pixmap_h)
891                                                 pixmap->resize(pixmap_w, pixmap_h);
892                                         pixmap->draw_data(edit,
893                                                 edit_x, 
894                                                 edit_w, 
895                                                 pixmap_x, 
896                                                 pixmap_w, 
897                                                 pixmap_h, 
898                                                 mode,
899                                                 indexes_only);
900 // Resize it if it's smaller
901                                         if(pixmap_w < pixmap->pixmap_w ||
902                                                 pixmap_h < pixmap->pixmap_h)
903                                                 pixmap->resize(pixmap_w, pixmap_h);
905 // Copy pixmap to background canvas
906                                         background_pixmap->draw_pixmap(pixmap, 
907                                                 pixmap->pixmap_x, 
908                                                 current->y_pixel,
909                                                 pixmap->pixmap_w,
910                                                 edit_h);
911                                 }
912                         }
913                 }
914         }
916 // Delete unused pixmaps
917         if(!indexes_only)
918                 for(int i = resource_pixmaps.total - 1; i >= 0; i--)
919                         if(resource_pixmaps.values[i]->visible < -5)
920                         {
921                                 delete resource_pixmaps.values[i];
922                                 resource_pixmaps.remove(resource_pixmaps.values[i]);
923                         }
925         if(hourglass_enabled) 
926         {
927                 stop_hourglass();
928                 hourglass_enabled = 0;
929         }
931         if(mode != 3 && !indexes_only)
932                 resource_thread->start_draw();
935 ResourcePixmap* TrackCanvas::create_pixmap(Edit *edit, 
936         int64_t edit_x, 
937         int64_t pixmap_x, 
938         int64_t pixmap_w, 
939         int64_t pixmap_h)
941         ResourcePixmap *result = 0;
943         for(int i = 0; i < resource_pixmaps.total; i++)
944         {
945 //printf("TrackCanvas::create_pixmap 1 %d %d\n", edit->id, resource_pixmaps.values[i]->edit->id);
946                 if(resource_pixmaps.values[i]->edit_id == edit->id) 
947                 {
948                         result = resource_pixmaps.values[i];
949                         break;
950                 }
951         }
953         if(!result)
954         {
955 //printf("TrackCanvas::create_pixmap 2\n");
956                 result = new ResourcePixmap(mwindow, 
957                         this, 
958                         edit, 
959                         pixmap_w, 
960                         pixmap_h);
961                 resource_pixmaps.append(result);
962         }
964 //      result->resize(pixmap_w, pixmap_h);
965         return result;
968 void TrackCanvas::get_pixmap_size(Edit *edit, 
969         int64_t edit_x, 
970         int64_t edit_w, 
971         int64_t &pixmap_x, 
972         int64_t &pixmap_w,
973         int64_t &pixmap_h)
976 // Align x on frame boundaries
979 //      switch(edit->edits->track->data_type)
980 //      {
981 //              case TRACK_AUDIO:
983                         pixmap_x = edit_x;
984                         pixmap_w = edit_w;
985                         if(pixmap_x < 0)
986                         {
987                                 pixmap_w -= -edit_x;
988                                 pixmap_x = 0;
989                         }
991                         if(pixmap_x + pixmap_w > get_w())
992                         {
993                                 pixmap_w = get_w() - pixmap_x;
994                         }
996 //                      break;
997 // 
998 //              case TRACK_VIDEO:
999 //              {
1000 //                      int64_t picon_w = (int64_t)(edit->picon_w() + 0.5);
1001 //                      int64_t frame_w = (int64_t)(edit->frame_w() + 0.5);
1002 //                      int64_t pixel_increment = MAX(picon_w, frame_w);
1003 //                      int64_t pixmap_x1 = edit_x;
1004 //                      int64_t pixmap_x2 = edit_x + edit_w;
1005 // 
1006 //                      if(pixmap_x1 < 0)
1007 //                      {
1008 //                              pixmap_x1 = (int64_t)((double)-edit_x / pixel_increment) * 
1009 //                                      pixel_increment + 
1010 //                                      edit_x;
1011 //                      }
1012 // 
1013 //                      if(pixmap_x2 > get_w())
1014 //                      {
1015 //                              pixmap_x2 = (int64_t)((double)(get_w() - edit_x) / pixel_increment + 1) * 
1016 //                                      pixel_increment + 
1017 //                                      edit_x;
1018 //                      }
1019 //                      pixmap_x = pixmap_x1;
1020 //                      pixmap_w = pixmap_x2 - pixmap_x1;
1021 //                      break;
1022 //              }
1023 //      }
1025         pixmap_h = mwindow->edl->local_session->zoom_track;
1026         if(mwindow->edl->session->show_titles) pixmap_h += mwindow->theme->get_image("title_bg_data")->get_h();
1027 //printf("get_pixmap_size %d %d %d %d\n", edit_x, edit_w, pixmap_x, pixmap_w);
1030 void TrackCanvas::edit_dimensions(Edit *edit, 
1031         int64_t &x, 
1032         int64_t &y, 
1033         int64_t &w, 
1034         int64_t &h)
1036 //      w = Units::round(edit->track->from_units(edit->length) * 
1037 //              mwindow->edl->session->sample_rate / 
1038 //              mwindow->edl->local_session->zoom_sample);
1040         h = resource_h();
1042         x = Units::round(edit->track->from_units(edit->startproject) * 
1043                         mwindow->edl->session->sample_rate /
1044                         mwindow->edl->local_session->zoom_sample - 
1045                         mwindow->edl->local_session->view_start);
1047 // Method for calculating w so when edits are together we never get off by one error due to rounding
1048         int64_t x_next = Units::round(edit->track->from_units(edit->startproject + edit->length) * 
1049                         mwindow->edl->session->sample_rate /
1050                         mwindow->edl->local_session->zoom_sample - 
1051                         mwindow->edl->local_session->view_start);
1052         w = x_next - x;
1054         y = edit->edits->track->y_pixel;
1056         if(mwindow->edl->session->show_titles) 
1057                 h += mwindow->theme->get_image("title_bg_data")->get_h();
1060 void TrackCanvas::track_dimensions(Track *track, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1062         x = 0;
1063         w = get_w();
1064         y = track->y_pixel;
1065         h = track->vertical_span(mwindow->theme);
1069 void TrackCanvas::draw_paste_destination()
1071         int current_atrack = 0;
1072         int current_vtrack = 0;
1073         int current_aedit = 0;
1074         int current_vedit = 0;
1075         int64_t w = 0;
1076         int64_t x;
1077         double position;
1078         int insertion  = 0;
1081         if((mwindow->session->current_operation == DRAG_ASSET &&
1082                         (mwindow->session->drag_assets->total ||
1083                         mwindow->session->drag_clips->total)) ||
1084                 (mwindow->session->current_operation == DRAG_EDIT &&
1085                         mwindow->session->drag_edits->total))
1086         {
1088                 Asset *asset = 0;
1089                 EDL *clip = 0;
1090                 int draw_box = 0;
1092                 if(mwindow->session->current_operation == DRAG_ASSET &&
1093                         mwindow->session->drag_assets->total)
1094                         asset = mwindow->session->drag_assets->values[0];
1096                 if(mwindow->session->current_operation == DRAG_ASSET &&
1097                         mwindow->session->drag_clips->total)
1098                         clip = mwindow->session->drag_clips->values[0];
1100 // Get destination track
1101                 for(Track *dest = mwindow->session->track_highlighted; 
1102                         dest; 
1103                         dest = dest->next)
1104                 {
1105                         if(dest->record)
1106                         {
1107 // Get source width in pixels
1108                                 w = -1;
1110 // Use start of highlighted edit
1111                                 if(mwindow->session->edit_highlighted)
1112                                         position = mwindow->session->track_highlighted->from_units(
1113                                                 mwindow->session->edit_highlighted->startproject);
1114                                 else
1115 // Use end of highlighted track, disregarding effects
1116                                         position = mwindow->session->track_highlighted->from_units(
1117                                                 mwindow->session->track_highlighted->edits->last->startproject);
1120                                 if(dest->data_type == TRACK_AUDIO)
1121                                 {
1122                                         if(asset && current_atrack < asset->channels)
1123                                         {
1124                                                 w = Units::to_int64((double)asset->audio_length /
1125                                                         asset->sample_rate *
1126                                                         mwindow->edl->session->sample_rate / 
1127                                                         mwindow->edl->local_session->zoom_sample);
1129                                         // FIXME: more obvious, get_drop_position should be called only ONCE - for highlighted track
1130                                                 int64_t asset_length;
1131                                                 // we use video if we are over video and audio if we are over audio
1132                                                 if (asset->video_data && mwindow->session->track_highlighted->data_type == TRACK_VIDEO)
1133                                                 {
1134                                                         // Images have length -1
1135                                                         double video_length = asset->video_length;
1136                                                         if (video_length < 0)
1137                                                         {
1138                                                                 if(mwindow->edl->session->si_useduration)
1139                                                                         video_length = mwindow->edl->session->si_duration;
1140                                                                 else    
1141                                                                         video_length = 1.0 / mwindow->edl->session->frame_rate ; 
1142                                                         }
1143                                                         asset_length = mwindow->session->track_highlighted->to_units(video_length / asset->frame_rate, 1);
1144                                                 }
1145                                                 else
1146                                                         asset_length = mwindow->session->track_highlighted->to_units(asset->audio_length / asset->sample_rate, 1);
1148                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1149                                                 if (position < 0) 
1150                                                         w = -1;
1151                                                 else
1152                                                 {
1153                                                         current_atrack++;
1154                                                         draw_box = 1;
1155                                                 }
1156                                         }
1157                                         else
1158                                         if(clip && current_atrack < clip->tracks->total_audio_tracks())
1159                                         {
1160                                                 w = Units::to_int64((double)clip->tracks->total_length() *
1161                                                         mwindow->edl->session->sample_rate / 
1162                                                         mwindow->edl->local_session->zoom_sample);
1163 //printf("draw_paste_destination %d\n", x);
1164                                                 int64_t asset_length = mwindow->session->track_highlighted->to_units((double)clip->tracks->total_length(), 1);
1166                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1167                                                 if (position < 0) 
1168                                                         w = -1;
1169                                                 else
1170                                                 {
1171                                                         current_atrack++;
1172                                                         draw_box = 1;
1173                                                 }
1174                                         }
1175                                         else
1176                                         if(mwindow->session->current_operation == DRAG_EDIT &&
1177                                                 current_aedit < mwindow->session->drag_edits->total)
1178                                         {
1179                                                 Edit *edit;
1180                                                 while(current_aedit < mwindow->session->drag_edits->total &&
1181                                                         mwindow->session->drag_edits->values[current_aedit]->track->data_type != TRACK_AUDIO)
1182                                                         current_aedit++;
1184                                                 if(current_aedit < mwindow->session->drag_edits->total)
1185                                                 {
1186                                                         edit = mwindow->session->drag_edits->values[current_aedit];
1187                                                         w = Units::to_int64(edit->length / mwindow->edl->local_session->zoom_sample);
1189                                                         position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, mwindow->session->drag_edit, mwindow->session->drag_edit->length));
1190                                                         if (position < 0) 
1191                                                                 w = -1;
1192                                                         else
1193                                                         {
1194                                                                 current_aedit++;
1195                                                                 draw_box = 1;
1196                                                         }
1197                                                 }
1198                                         }
1199                                 }
1200                                 else
1201                                 if(dest->data_type == TRACK_VIDEO)
1202                                 {
1203 //printf("draw_paste_destination 1\n");
1204                                         if(asset && current_vtrack < asset->layers)
1205                                         {
1206                                                 // Images have length -1
1207                                                 double video_length = asset->video_length;
1208                                                 if (video_length < 0)
1209                                                 {
1210                                                         if(mwindow->edl->session->si_useduration)
1211                                                                 video_length = mwindow->edl->session->si_duration;
1212                                                         else    
1213                                                                 video_length = 1.0 / mwindow->edl->session->frame_rate ; 
1214                                                 }
1215                                                 w = Units::to_int64((double)video_length / 
1216                                                         asset->frame_rate *
1217                                                         mwindow->edl->session->sample_rate /
1218                                                         mwindow->edl->local_session->zoom_sample);
1219                                                 int64_t asset_length = mwindow->session->track_highlighted->to_units((double)video_length / 
1220                                                         asset->frame_rate, 1);
1222                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1223                                                 if (position < 0) 
1224                                                         w = -1;
1225                                                 else
1226                                                 {
1227                                                         current_vtrack++;
1228                                                         draw_box = 1;
1229                                                 }
1230                                         }
1231                                         else
1232                                         if(clip && current_vtrack < clip->tracks->total_video_tracks())
1233                                         {
1234                                                 w = Units::to_int64(clip->tracks->total_length() *
1235                                                         mwindow->edl->session->sample_rate / 
1236                                                         mwindow->edl->local_session->zoom_sample);
1237                                                 int64_t asset_length = mwindow->session->track_highlighted->to_units((double)clip->tracks->total_length(), 1);
1239                                                 position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, NULL, asset_length));
1240                                                 if (position < 0) 
1241                                                         w = -1;
1242                                                 else
1243                                                 {
1244                                                         current_vtrack++;
1245                                                         draw_box = 1;
1246                                                 }
1247                                         }
1248                                         else
1249                                         if(mwindow->session->current_operation == DRAG_EDIT &&
1250                                                 current_vedit < mwindow->session->drag_edits->total)
1251                                         {
1252                                                 Edit *edit;
1253                                                 while(current_vedit < mwindow->session->drag_edits->total &&
1254                                                         mwindow->session->drag_edits->values[current_vedit]->track->data_type != TRACK_VIDEO)
1255                                                         current_vedit++;
1257                                                 if(current_vedit < mwindow->session->drag_edits->total)
1258                                                 {
1259                                                         edit = mwindow->session->drag_edits->values[current_vedit];
1260                                                         w = Units::to_int64(edit->track->from_units(edit->length) *
1261                                                                 mwindow->edl->session->sample_rate / 
1262                                                                 mwindow->edl->local_session->zoom_sample);
1264                                                         position = mwindow->session->track_highlighted->from_units(get_drop_position(&insertion, mwindow->session->drag_edit, mwindow->session->drag_edit->length));
1265                                                         if (position < 0) 
1266                                                                 w = -1;
1267                                                         else
1268                                                         {
1269                                                                 current_vedit++;
1270                                                                 draw_box = 1;
1271                                                         }
1272                                                 }
1273                                         }
1274                                 }
1276                                 if(w >= 0)
1277                                 {
1278 // Get the x coordinate
1279                                         x = Units::to_int64(position * 
1280                                                 mwindow->edl->session->sample_rate /
1281                                                 mwindow->edl->local_session->zoom_sample) - 
1282                                                 mwindow->edl->local_session->view_start;
1283                                         int y = dest->y_pixel;
1284                                         int h = dest->vertical_span(mwindow->theme);
1287 //printf("TrackCanvas::draw_paste_destination 2 %d %d %d %d\n", x, y, w, h);
1288                                         if (insertion)
1289                                                 draw_highlight_insertion(x, y, w, h);
1290                                         else
1291                                                 draw_highlight_rectangle(x, y, w, h);
1292                                 }
1293                         }
1294                 }
1295         }
1298 void TrackCanvas::plugin_dimensions(Plugin *plugin, int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1300         x = Units::round(plugin->track->from_units(plugin->startproject) *
1301                 mwindow->edl->session->sample_rate / 
1302                 mwindow->edl->local_session->zoom_sample - 
1303                 mwindow->edl->local_session->view_start);
1304         w = Units::round(plugin->track->from_units(plugin->length) *
1305                 mwindow->edl->session->sample_rate / 
1306                 mwindow->edl->local_session->zoom_sample);
1307         y = plugin->track->y_pixel + 
1308                         mwindow->edl->local_session->zoom_track +
1309                         plugin->plugin_set->get_number() * 
1310                         mwindow->theme->get_image("plugin_bg_data")->get_h();
1311         if(mwindow->edl->session->show_titles)
1312                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1313         h = mwindow->theme->get_image("plugin_bg_data")->get_h();
1316 int TrackCanvas::resource_h()
1318         return mwindow->edl->local_session->zoom_track;
1321 void TrackCanvas::draw_highlight_rectangle(int x, int y, int w, int h)
1324 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport, 
1325 // just draw arrows, so user has indication that something is there
1326 // FIXME: get better colors
1328         if (x + w <= 0)
1329         {
1330                 draw_triangle_left(0, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1331                 return;
1332         } else
1333         if (x >= get_w())
1334         {
1335                 draw_triangle_right(get_w() - h * 2/3, y + h /6, h * 2/3, h * 2/3, BLACK, GREEN, YELLOW, RED, BLUE);
1336                 return;
1337         }
1339 // Fix bug in heroines & cvs version as of 22.8.2005:
1340 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1341         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1342         if(x < -10)
1343         {
1344                 w += x - -10;
1345                 x = -10;
1346         }
1347         if(y < -10)
1348         {
1349                 h += y - -10;
1350                 y = -10;
1351         }
1352         w = MIN(w, get_w() + 20);
1353         h = MIN(h, get_h() + 20);
1354         set_color(WHITE);
1355         set_inverse();
1356         draw_rectangle(x, y, w, h);
1357         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1358         set_opaque();
1359 //printf("TrackCanvas::draw_highlight_rectangle %d %d %d %d\n", x, y, w, h);
1362 void TrackCanvas::draw_highlight_insertion(int x, int y, int w, int h)
1365 // if we have to draw a highlighted rectangle completely on the left or completely on the right of the viewport, 
1366 // just draw arrows, so user has indication that something is there
1367 // FIXME: get better colors
1370         
1371         int h1 = h / 8;
1372         int h2 = h / 4;
1373         
1374         set_inverse();
1376 /* these don't look so good
1378         draw_line(x, y, x, y+h);
1379         draw_line(x - h2 * 2, y + h1*2,   x - h2, y+h1*2);
1380         draw_line(x - h2 * 2, y + h1*2+1, x - h2, y+h1*2+1);
1381         draw_line(x - h2 * 2, y + h1*6,   x - h2, y+h1*6);
1382         draw_line(x - h2 * 2, y + h1*6+1, x - h2, y+h1*6+1);
1384         draw_triangle_right(x - h2, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1385         draw_triangle_right(x - h2, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1387 /*      draw_line(x + h2 * 2, y + h1*2,   x + h2, y+h1*2);
1388         draw_line(x + h2 * 2, y + h1*2+1, x + h2, y+h1*2+1);
1389         draw_line(x + h2 * 2, y + h1*6,   x + h2, y+h1*6);
1390         draw_line(x - h2 * 2, y + h1*6+1, x + h2, y+h1*6+1);
1392         draw_triangle_left(x, y + h1, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1393         draw_triangle_left(x, y + h1*5, h2, h2, BLACK, GREEN, YELLOW, RED, BLUE);
1394         
1395 // draw the box centred around x
1396         x -= w / 2;
1397 // Fix bug in heroines & cvs version as of 22.8.2005:
1398 // If we grab when zoomed in and zoom out while dragging, when edit gets really narrow strange things start happening
1399         if (w >= 0 && w < 3) {x -= w /2; w = 3;};
1400         if(x < -10)
1401         {
1402                 w += x - -10;
1403                 x = -10;
1404         }
1405         if(y < -10)
1406         {
1407                 h += y - -10;
1408                 y = -10;
1409         }
1410         w = MIN(w, get_w() + 20);
1411         h = MIN(h, get_h() + 20);
1412         set_color(WHITE);
1413         set_inverse();
1414         draw_rectangle(x, y, w, h);
1415         draw_rectangle(x + 1, y + 1, w - 2, h - 2);
1416         set_opaque();
1417 //printf("TrackCanvas::draw_highlight_insertion %d %d %d %d\n", x, y, w, h);
1420 void TrackCanvas::draw_playback_cursor()
1422 // Called before playback_cursor exists
1423 //      if(mwindow->playback_cursor && mwindow->playback_cursor->visible)
1424 //      {
1425 //              mwindow->playback_cursor->visible = 0;
1426 //              mwindow->playback_cursor->draw();
1427 //      }
1430 void TrackCanvas::get_handle_coords(Edit *edit, int64_t &x, int64_t &y, int64_t &w, int64_t &h, int side)
1432         int handle_w = mwindow->theme->edithandlein_data[0]->get_w();
1433         int handle_h = mwindow->theme->edithandlein_data[0]->get_h();
1435         edit_dimensions(edit, x, y, w, h);
1437         if(mwindow->edl->session->show_titles)
1438         {
1439                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1440         }
1441         else
1442         {
1443                 y = 0;
1444         }
1446         if(side == EDIT_OUT)
1447         {
1448                 x += w - handle_w;
1449         }
1451         h = handle_h;
1452         w = handle_w;
1455 void TrackCanvas::get_transition_coords(int64_t &x, int64_t &y, int64_t &w, int64_t &h)
1457 //printf("TrackCanvas::get_transition_coords 1\n");
1458 //      int transition_w = mwindow->theme->transitionhandle_data[0]->get_w();
1459 //      int transition_h = mwindow->theme->transitionhandle_data[0]->get_h();
1460         int transition_w = 30;
1461         int transition_h = 30;
1462 //printf("TrackCanvas::get_transition_coords 1\n");
1464         if(mwindow->edl->session->show_titles)
1465                 y += mwindow->theme->get_image("title_bg_data")->get_h();
1466 //printf("TrackCanvas::get_transition_coords 2\n");
1468         y += (h - mwindow->theme->get_image("title_bg_data")->get_h()) / 2 - transition_h / 2;
1469         x -= transition_w / 2;
1471         h = transition_h;
1472         w = transition_w;
1475 void TrackCanvas::draw_highlighting()
1477         int64_t x, y, w, h;
1478         int draw_box = 0;
1483         switch(mwindow->session->current_operation)
1484         {
1485                 case DRAG_ATRANSITION:
1486                 case DRAG_VTRANSITION:
1487 //printf("TrackCanvas::draw_highlighting 1 %p %p\n", 
1488 //      mwindow->session->track_highlighted, mwindow->session->edit_highlighted);
1489                         if(mwindow->session->edit_highlighted)
1490                         {
1491 //printf("TrackCanvas::draw_highlighting 2\n");
1492                                 if((mwindow->session->current_operation == DRAG_ATRANSITION && 
1493                                         mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1494                                         (mwindow->session->current_operation == DRAG_VTRANSITION && 
1495                                         mwindow->session->track_highlighted->data_type == TRACK_VIDEO))
1496                                 {
1497 //printf("TrackCanvas::draw_highlighting 2\n");
1498                                         edit_dimensions(mwindow->session->edit_highlighted, x, y, w, h);
1499 //printf("TrackCanvas::draw_highlighting 2\n");
1501                                         if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1502                                                 MWindowGUI::visible(y, y + h, 0, get_h()))
1503                                         {
1504                                                 draw_box = 1;
1505                                                 get_transition_coords(x, y, w, h);
1506                                         }
1507 //printf("TrackCanvas::draw_highlighting 3\n");
1508                                 }
1509                         }
1510                         break;
1514 // Dragging a new effect from the Resource window
1515                 case DRAG_AEFFECT:
1516                 case DRAG_VEFFECT:
1517                         if(mwindow->session->track_highlighted &&
1518                                 ((mwindow->session->current_operation == DRAG_AEFFECT && mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1519                                         (mwindow->session->current_operation == DRAG_VEFFECT && mwindow->session->track_highlighted->data_type == TRACK_VIDEO)))
1520                         {
1521 // Put it before another plugin
1522                                 if(mwindow->session->plugin_highlighted)
1523                                 {
1524                                         plugin_dimensions(mwindow->session->plugin_highlighted, 
1525                                                 x, 
1526                                                 y, 
1527                                                 w, 
1528                                                 h);
1529 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1530                                 }
1531                                 else
1532 // Put it after a plugin set
1533                                 if(mwindow->session->pluginset_highlighted &&
1534                                         mwindow->session->pluginset_highlighted->last)
1535                                 {
1536                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last, 
1537                                                 x, 
1538                                                 y, 
1539                                                 w, 
1540                                                 h);
1541 //printf("TrackCanvas::draw_highlighting 1 %d %d\n", x, w);
1542                                         int64_t track_x, track_y, track_w, track_h;
1543                                         track_dimensions(mwindow->session->track_highlighted, 
1544                                                 track_x, 
1545                                                 track_y, 
1546                                                 track_w, 
1547                                                 track_h);
1549                                         x += w;
1550                                         w = Units::round(
1551                                                         mwindow->session->track_highlighted->get_length() *
1552                                                         mwindow->edl->session->sample_rate / 
1553                                                         mwindow->edl->local_session->zoom_sample - 
1554                                                         mwindow->edl->local_session->view_start) -
1555                                                 x;
1556 //printf("TrackCanvas::draw_highlighting 2 %d\n", w);
1557                                         if(w <= 0) w = track_w;
1558                                 }
1559                                 else
1560                                 {
1561                                         track_dimensions(mwindow->session->track_highlighted, 
1562                                                 x, 
1563                                                 y, 
1564                                                 w, 
1565                                                 h);
1567 //printf("TrackCanvas::draw_highlighting 1 %d %d %d %d\n", x, y, w, h);
1568 // Put it in a new plugin set determined by the selected range
1569                                         if(mwindow->edl->local_session->get_selectionend() > 
1570                                                 mwindow->edl->local_session->get_selectionstart())
1571                                         {
1572                                                 x = Units::to_int64(mwindow->edl->local_session->get_selectionstart() *
1573                                                         mwindow->edl->session->sample_rate / 
1574                                                         mwindow->edl->local_session->zoom_sample -
1575                                                         mwindow->edl->local_session->view_start);
1576                                                 w = Units::to_int64((mwindow->edl->local_session->get_selectionend() - 
1577                                                         mwindow->edl->local_session->get_selectionstart()) *
1578                                                         mwindow->edl->session->sample_rate / 
1579                                                         mwindow->edl->local_session->zoom_sample);
1580                                         }
1581 // Put it in a new plugin set determined by an edit boundary
1582                                         else
1583                                         if(mwindow->session->edit_highlighted)
1584                                         {
1585                                                 int64_t temp_y, temp_h;
1586                                                 edit_dimensions(mwindow->session->edit_highlighted, 
1587                                                         x, 
1588                                                         temp_y, 
1589                                                         w, 
1590                                                         temp_h);
1591                                         }
1592 // Put it at the beginning of the track in a new plugin set
1593                                 }
1595                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1596                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1597                                 {
1598 //printf("TrackCanvas::draw_highlighting 1\n");
1599                                         draw_box = 1;
1600                                 }
1601                         }
1602                         break;
1603                 
1604                 case DRAG_ASSET:
1605                         if(mwindow->session->track_highlighted)
1606                         {
1607                                 track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1609                                 if(MWindowGUI::visible(y, y + h, 0, get_h()))
1610                                 {
1611                                         draw_paste_destination();
1612                                 }
1613                         }
1614                         break;
1616 // Dragging an effect from the timeline
1617                 case DRAG_AEFFECT_COPY:
1618                 case DRAG_VEFFECT_COPY:
1619                         if((mwindow->session->plugin_highlighted || mwindow->session->track_highlighted) &&
1620                                 ((mwindow->session->current_operation == DRAG_AEFFECT_COPY && mwindow->session->track_highlighted->data_type == TRACK_AUDIO) ||
1621                                 (mwindow->session->current_operation == DRAG_VEFFECT_COPY && mwindow->session->track_highlighted->data_type == TRACK_VIDEO)))
1622                         {
1623 // Put it before another plugin
1624                                 if(mwindow->session->plugin_highlighted)
1625                                         plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1626                                 else
1627 // Put it after a plugin set
1628                                 if(mwindow->session->pluginset_highlighted &&
1629                                         mwindow->session->pluginset_highlighted->last)
1630                                 {
1631                                         plugin_dimensions((Plugin*)mwindow->session->pluginset_highlighted->last, x, y, w, h);
1632                                         x += w;
1633                                 }
1634                                 else
1635                                 if(mwindow->session->track_highlighted)
1636                                 {
1637                                         track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1639 // Put it in a new plugin set determined by an edit boundary
1640                                         if(mwindow->session->edit_highlighted)
1641                                         {
1642                                                 int64_t temp_y, temp_h;
1643                                                 edit_dimensions(mwindow->session->edit_highlighted, 
1644                                                         x, 
1645                                                         temp_y, 
1646                                                         w, 
1647                                                         temp_h);
1648                                         }
1649 // Put it in a new plugin set at the start of the track
1650                                 }
1652 // Calculate length of plugin based on data type of track and units
1653                                 if(mwindow->session->track_highlighted->data_type == TRACK_VIDEO)
1654                                 {
1655                                         w = (int64_t)((double)mwindow->session->drag_plugin->length / 
1656                                                 mwindow->edl->session->frame_rate *
1657                                                 mwindow->edl->session->sample_rate /
1658                                                 mwindow->edl->local_session->zoom_sample);
1659                                 }
1660                                 else
1661                                 {
1662                                         w = (int64_t)mwindow->session->drag_plugin->length /
1663                                                 mwindow->edl->local_session->zoom_sample;
1664                                 }
1666                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1667                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1668                                 {
1669                                         draw_box = 1;
1670                                 }
1671                         }
1672                         break;
1674                 case DRAG_PLUGINKEY:
1675                         if(mwindow->session->plugin_highlighted && 
1676                            mwindow->session->current_operation == DRAG_PLUGINKEY)
1677                         {
1678 // Just highlight the plugin
1679                                 plugin_dimensions(mwindow->session->plugin_highlighted, x, y, w, h);
1681                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1682                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1683                                 {
1684                                         draw_box = 1;
1685                                 }
1686                         }
1687                         break;
1689                 case DRAG_EDIT:
1690                         if(mwindow->session->track_highlighted)
1691                         {
1692                                 track_dimensions(mwindow->session->track_highlighted, x, y, w, h);
1694                                 if(MWindowGUI::visible(y, y + h, 0, get_h()))
1695                                 {
1696                                         draw_paste_destination();
1697                                 }
1698                         }
1699                         break;
1700         }
1703         if(draw_box)
1704         {
1705                 draw_highlight_rectangle(x, y, w, h);
1706         }
1709 void TrackCanvas::draw_plugins()
1711         char string[BCTEXTLEN];
1712         int current_toggle = 0;
1714         if(!mwindow->edl->session->show_assets) goto done;
1716         for(int i = 0; i < plugin_on_toggles.total; i++)
1717                 plugin_on_toggles.values[i]->in_use = 0;
1718         for(int i = 0; i < plugin_show_toggles.total; i++)
1719                 plugin_show_toggles.values[i]->in_use = 0;
1722         for(Track *track = mwindow->edl->tracks->first;
1723                 track;
1724                 track = track->next)
1725         {
1726                 if(track->expand_view)
1727                 {
1728                         for(int i = 0; i < track->plugin_set.total; i++)
1729                         {
1730                                 PluginSet *pluginset = track->plugin_set.values[i];
1732                                 for(Plugin *plugin = (Plugin*)pluginset->first; plugin; plugin = (Plugin*)plugin->next)
1733                                 {
1734                                         int64_t total_x, y, total_w, h;
1735                                         plugin_dimensions(plugin, total_x, y, total_w, h);
1736                                         
1737                                         if(MWindowGUI::visible(total_x, total_x + total_w, 0, get_w()) &&
1738                                                 MWindowGUI::visible(y, y + h, 0, get_h()) &&
1739                                                 plugin->plugin_type != PLUGIN_NONE)
1740                                         {
1741                                                 int x = total_x, w = total_w, left_margin = 5;
1742                                                 int right_margin = 5;
1743                                                 if(x < 0)
1744                                                 {
1745                                                         w -= -x;
1746                                                         x = 0;
1747                                                 }
1748                                                 if(w + x > get_w()) w -= (w + x) - get_w();
1750                                                 draw_3segmenth(x, 
1751                                                         y, 
1752                                                         w, 
1753                                                         total_x,
1754                                                         total_w,
1755                                                         mwindow->theme->get_image("plugin_bg_data"),
1756                                                         0);
1757                                                 set_color(get_resources()->default_text_color);
1758                                                 set_font(MEDIUMFONT_3D);
1759                                                 plugin->calculate_title(string, 0);
1761 // Truncate string to int64_test visible in background
1762                                                 int len = strlen(string), j;
1763                                                 for(j = len; j >= 0; j--)
1764                                                 {
1765                                                         if(left_margin + get_text_width(MEDIUMFONT_3D, string) > w)
1766                                                         {
1767                                                                 string[j] = 0;
1768                                                         }
1769                                                         else
1770                                                                 break;
1771                                                 }
1773 // Justify the text on the left boundary of the edit if it is visible.
1774 // Otherwise justify it on the left side of the screen.
1775                                                 int text_x = total_x + left_margin;
1776                                                 text_x = MAX(left_margin, text_x);
1777                                                 draw_text(text_x, 
1778                                                         y + get_text_ascent(MEDIUMFONT_3D) + 2, 
1779                                                         string,
1780                                                         strlen(string),
1781                                                         0);
1784 // Update plugin toggles
1785                                                 int toggle_x = total_x + total_w;
1786                                                 toggle_x = MIN(get_w() - right_margin, toggle_x);
1787                                                 toggle_x -= PluginOn::calculate_w(mwindow) + 10;
1788                                                 int toggle_y = y;
1789                                                 if(current_toggle >= plugin_on_toggles.total)
1790                                                 {
1791                                                         PluginOn *plugin_on = new PluginOn(mwindow, toggle_x, toggle_y, plugin);
1792                                                         add_subwindow(plugin_on);
1793                                                         plugin_on_toggles.append(plugin_on);
1794                                                 }
1795                                                 else
1796                                                 {
1797                                                         plugin_on_toggles.values[current_toggle]->update(toggle_x, toggle_y, plugin);
1798                                                 }
1800                                                 toggle_x -= PluginShow::calculate_w(mwindow) + 10;
1801                                                 if(current_toggle >= plugin_show_toggles.total)
1802                                                 {
1803                                                         PluginShow *plugin_off = new PluginShow(mwindow, toggle_x, toggle_y, plugin);
1804                                                         add_subwindow(plugin_off);
1805                                                         plugin_show_toggles.append(plugin_off);
1806                                                 }
1807                                                 else
1808                                                 {
1809                                                         plugin_show_toggles.values[current_toggle]->update(toggle_x, toggle_y, plugin);
1810                                                 }
1811                                                 current_toggle++;
1812                                         }
1813                                 }
1814                         }
1815                 }
1816         }
1819 done:
1820         int i = current_toggle;
1821         while(i < plugin_on_toggles.total &&
1822                 i < plugin_show_toggles.total)
1823         {
1824                 plugin_on_toggles.remove_object_number(current_toggle);
1825                 plugin_show_toggles.remove_object_number(current_toggle);
1826         }
1829 void TrackCanvas::refresh_plugintoggles()
1831         for(int i = 0; i < plugin_on_toggles.total; i++)
1832         {
1833                 PluginOn *on = plugin_on_toggles.values[i];
1834                 on->reposition_window(on->get_x(), on->get_y());
1835         }
1836         for(int i = 0; i < plugin_show_toggles.total; i++)
1837         {
1838                 PluginShow *show = plugin_show_toggles.values[i];
1839                 show->reposition_window(show->get_x(), show->get_y());
1840         }
1843 void TrackCanvas::draw_inout_points()
1848 void TrackCanvas::draw_drag_handle()
1850         if(mwindow->session->current_operation == DRAG_EDITHANDLE2 ||
1851                 mwindow->session->current_operation == DRAG_PLUGINHANDLE2)
1852         {
1853 //printf("TrackCanvas::draw_drag_handle 1 %ld %ld\n", mwindow->session->drag_sample, mwindow->edl->local_session->view_start);
1854                 int64_t pixel1 = Units::round(mwindow->session->drag_position * 
1855                         mwindow->edl->session->sample_rate /
1856                         mwindow->edl->local_session->zoom_sample - 
1857                         mwindow->edl->local_session->view_start);
1858 //printf("TrackCanvas::draw_drag_handle 2 %d\n", pixel1);
1859                 set_color(GREEN);
1860                 set_inverse();
1861 //printf("TrackCanvas::draw_drag_handle 3\n");
1862                 draw_line(pixel1, 0, pixel1, get_h());
1863                 set_opaque();
1864 //printf("TrackCanvas::draw_drag_handle 4\n");
1865         }
1869 void TrackCanvas::draw_transitions()
1871         int64_t x, y, w, h;
1873         if(!mwindow->edl->session->show_assets) return;
1875         for(Track *track = mwindow->edl->tracks->first;
1876                 track;
1877                 track = track->next)
1878         {
1879                 for(Edit *edit = track->edits->first;
1880                         edit;
1881                         edit = edit->next)
1882                 {
1883                         if(edit->transition)
1884                         {
1885                                 int64_t strip_w, strip_x, strip_y;
1886                                 edit_dimensions(edit, x, y, w, h);
1887                                 strip_x = x ;
1888                                 strip_y = y;
1889                                 if(mwindow->edl->session->show_titles)
1890                                         strip_y += mwindow->theme->get_image("title_bg_data")->get_h();
1892                                 get_transition_coords(x, y, w, h);
1893                                 strip_w = Units::round(edit->track->from_units(edit->transition->length) * 
1894                                         mwindow->edl->session->sample_rate / 
1895                                         mwindow->edl->local_session->zoom_sample);
1897                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
1898                                         MWindowGUI::visible(y, y + h, 0, get_h()))
1899                                 {
1900                                         PluginServer *server = mwindow->scan_plugindb(edit->transition->title,
1901                                                 track->data_type);
1902                                         draw_vframe(server->picon, 
1903                                                 x, 
1904                                                 y, 
1905                                                 w, 
1906                                                 h, 
1907                                                 0, 
1908                                                 0, 
1909                                                 server->picon->get_w(), 
1910                                                 server->picon->get_h());
1911                                 }
1912                                 if(MWindowGUI::visible(strip_x, strip_x + strip_w, 0, get_w()) &&
1913                                         MWindowGUI::visible(strip_y, strip_y + h, 0, get_h()))
1914                                 {
1915                                         int x = strip_x, w = strip_w, left_margin = 5;
1916                                         if(x < 0)
1917                                         {
1918                                                 w -= -x;
1919                                                 x = 0;
1920                                         }
1921                                         if(w + x > get_w()) w -= (w + x) - get_w();
1922                                 
1923                                         draw_3segmenth(
1924                                                 x, 
1925                                                 strip_y, 
1926                                                 w, 
1927                                                 strip_x,
1928                                                 strip_w,
1929                                                 mwindow->theme->get_image("plugin_bg_data"),
1930                                                 0);
1932                                 }
1933                         }
1934                 }
1935         }
1938 void TrackCanvas::draw_loop_points()
1940 //printf("TrackCanvas::draw_loop_points 1\n");
1941         if(mwindow->edl->local_session->loop_playback)
1942         {
1943 //printf("TrackCanvas::draw_loop_points 2\n");
1944                 int64_t x = Units::round(mwindow->edl->local_session->loop_start *
1945                         mwindow->edl->session->sample_rate /
1946                         mwindow->edl->local_session->zoom_sample - 
1947                         mwindow->edl->local_session->view_start);
1948 //printf("TrackCanvas::draw_loop_points 3\n");
1950                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1951                 {
1952                         set_color(GREEN);
1953                         draw_line(x, 0, x, get_h());
1954                 }
1955 //printf("TrackCanvas::draw_loop_points 4\n");
1957                 x = Units::round(mwindow->edl->local_session->loop_end *
1958                         mwindow->edl->session->sample_rate /
1959                         mwindow->edl->local_session->zoom_sample - 
1960                         mwindow->edl->local_session->view_start);
1961 //printf("TrackCanvas::draw_loop_points 5\n");
1963                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1964                 {
1965                         set_color(GREEN);
1966                         draw_line(x, 0, x, get_h());
1967                 }
1968 //printf("TrackCanvas::draw_loop_points 6\n");
1969         }
1970 //printf("TrackCanvas::draw_loop_points 7\n");
1973 void TrackCanvas::draw_brender_start()
1975         if(mwindow->preferences->use_brender)
1976         {
1977                 int64_t x = Units::round(mwindow->edl->session->brender_start *
1978                         mwindow->edl->session->sample_rate /
1979                         mwindow->edl->local_session->zoom_sample - 
1980                         mwindow->edl->local_session->view_start);
1982                 if(MWindowGUI::visible(x, x + 1, 0, get_w()))
1983                 {
1984                         set_color(RED);
1985                         draw_line(x, 0, x, get_h());
1986                 }
1987         }
1990 static int auto_colors[] = 
1992         BLUE,
1993         RED,
1994         GREEN,
1995         BLUE,
1996         RED,
1997         GREEN,
1998         BLUE,
1999         WHITE,
2000         0,
2001         0,
2002         0,
2003         0
2006 // The operations which correspond to each automation type
2007 static int auto_operations[] = 
2009         DRAG_MUTE,
2010         DRAG_CAMERA_X,
2011         DRAG_CAMERA_Y,
2012         DRAG_CAMERA_Z,
2013         DRAG_PROJECTOR_X,
2014         DRAG_PROJECTOR_Y,
2015         DRAG_PROJECTOR_Z,
2016         DRAG_FADE,
2017         DRAG_PAN,
2018         DRAG_MODE,
2019         DRAG_MASK,
2020         DRAG_NUDGE
2023 // The buttonpress operations, so nothing changes unless the mouse moves
2024 // a certain amount.  This allows the keyframe to be used to position the
2025 // insertion point without moving itself.
2026 static int pre_auto_operations[] =
2028         DRAG_MUTE,
2029         DRAG_CAMERA_X,
2030         DRAG_CAMERA_Y,
2031         DRAG_CAMERA_Z,
2032         DRAG_PROJECTOR_X,
2033         DRAG_PROJECTOR_Y,
2034         DRAG_PROJECTOR_Z,
2035         DRAG_FADE,
2036         DRAG_PAN_PRE,
2037         DRAG_MODE_PRE,
2038         DRAG_MASK_PRE,
2039         DRAG_NUDGE
2043 int TrackCanvas::do_keyframes(int cursor_x, 
2044         int cursor_y, 
2045         int draw, 
2046         int buttonpress, 
2047         int &new_cursor,
2048         int &update_cursor,
2049         int &rerender)
2051 // Note: button 3 (right mouse button) is not eaten to allow
2052 // track context menu to appear
2053         int current_tool = 0;
2054         int result = 0;
2055         EDLSession *session = mwindow->edl->session;
2059         BC_Pixmap *auto_pixmaps[] = 
2060         {
2061                 0,
2062                 0,
2063                 0,
2064                 0,
2065                 0,
2066                 0,
2067                 0,
2068                 0,
2069                 pankeyframe_pixmap,
2070                 modekeyframe_pixmap,
2071                 maskkeyframe_pixmap,
2072                 0,
2073         };
2077         for(Track *track = mwindow->edl->tracks->first;
2078                 track && !result;
2079                 track = track->next)
2080         {
2081         Auto *auto_keyframe;
2082                 Automation *automation = track->automation;
2085 // Handle float autos
2086                 for(int i = 0; i < AUTOMATION_TOTAL && !result; i++)
2087                 {
2088 // Event not trapped and automation visible
2089                         Autos *autos = automation->autos[i];
2090                         if(!result && session->auto_conf->autos[i] && autos)
2091                         {
2092                                 switch(i)
2093                                 {
2094                                         case AUTOMATION_MODE:
2095                                         case AUTOMATION_PAN:
2096                                         case AUTOMATION_MASK:
2097                                                 result = do_autos(track, 
2098                                                         automation->autos[i],
2099                                                         cursor_x, 
2100                                                         cursor_y, 
2101                                                         draw, 
2102                                                         buttonpress,
2103                                                         auto_pixmaps[i],
2104                             auto_keyframe);
2105                                                 break;
2107                                         default:
2108                                                 switch(autos->get_type())
2109                                                 {
2110                                                         case AUTOMATION_TYPE_FLOAT:
2111                                                         {
2112                                                                 Automation *dummy = new Automation(0,track);
2113                                                                 int autogrouptype = dummy->autogrouptype(i,track);
2114                                                                 result = do_float_autos(track, 
2115                                                                         autos,
2116                                                                         cursor_x, 
2117                                                                         cursor_y, 
2118                                                                         draw, 
2119                                                                         buttonpress, 
2120                                                                         auto_colors[i],
2121                                                                         auto_keyframe,
2122                                                                         autogrouptype);
2123                                                         }
2124                                                         break;
2126                                                         case AUTOMATION_TYPE_INT:
2127                                                                 result = do_toggle_autos(track, 
2128                                                                         autos,
2129                                                                         cursor_x, 
2130                                                                         cursor_y, 
2131                                                                         draw, 
2132                                                                         buttonpress,
2133                                                                         auto_colors[i],
2134                                                                         auto_keyframe);
2135                                                                 break;
2136                                                 }
2137                                                 break;
2138                                 }
2139                         
2142                                 if(result)
2143                                 {
2144                                         if(mwindow->session->current_operation == auto_operations[i])
2145                                                 rerender = 1;
2146                                         if(buttonpress)
2147                                         {
2148                         if (buttonpress != 3)
2149                         {
2150                                                         if(i == AUTOMATION_FADE) 
2151                                                                 synchronize_autos(0, 
2152                                                                         track, 
2153                                                                         (FloatAuto*)mwindow->session->drag_auto, 
2154                                                                         1);
2155                                                         mwindow->session->current_operation = pre_auto_operations[i];
2156                                                         update_drag_caption();
2157                                                         }
2158                                                         else
2159                                                         {
2160                                 gui->keyframe_menu->update(automation, autos, auto_keyframe);
2161                                 gui->keyframe_menu->activate_menu();
2162                                 rerender = 1; // the position changes
2163                                                         }
2164                                         }
2165                                 }
2166                         }
2167                 }
2172                 if(!result && 
2173                         session->auto_conf->plugins &&
2174                         mwindow->edl->session->show_assets)
2175                 {
2176                         Plugin *plugin;
2177                         KeyFrame *keyframe;
2178                         result = do_plugin_autos(track,
2179                                 cursor_x, 
2180                                 cursor_y, 
2181                                 draw, 
2182                                 buttonpress,
2183                                 plugin,
2184                                 keyframe);
2185                         if(result && mwindow->session->current_operation == DRAG_PLUGINKEY)
2186                         {
2187                                 rerender = 1;
2188                         }
2189                         if(result && (buttonpress == 1))
2190                         {
2191                                 mwindow->session->current_operation = DRAG_PLUGINKEY_PRE;
2192                                 update_drag_caption();
2193                                 rerender = 1;
2194                         } else
2195                         if (result && (buttonpress == 3))
2196                         {
2197                                 gui->keyframe_menu->update(plugin, keyframe);
2198                                 gui->keyframe_menu->activate_menu();
2199                                 rerender = 1; // the position changes
2200                         }
2201                 }
2202         }
2204 // Final pass to trap event
2205         for(int i = 0; i < AUTOMATION_TOTAL; i++)
2206         {
2207                 if(mwindow->session->current_operation == pre_auto_operations[i] ||
2208                         mwindow->session->current_operation == auto_operations[i])
2209                         result = 1;
2210         }
2212         if(mwindow->session->current_operation == DRAG_PLUGINKEY ||
2213                 mwindow->session->current_operation == DRAG_PLUGINKEY_PRE)
2214         {
2215                 result = 1;
2216         }
2218         update_cursor = 1;
2219         if(result)
2220         {
2221                 new_cursor = UPRIGHT_ARROW_CURSOR;
2222         }
2224         return result;
2227 void TrackCanvas::draw_auto(Auto *current, 
2228         int x, 
2229         int y, 
2230         int center_pixel, 
2231         int zoom_track,
2232         int color)
2234         int x1, y1, x2, y2;
2235         char string[BCTEXTLEN];
2237         x1 = x - HANDLE_W / 2;
2238         x2 = x + HANDLE_W / 2;
2239         y1 = center_pixel + y - HANDLE_W / 2;
2240         y2 = center_pixel + y + HANDLE_W / 2;
2242         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2243         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2245         set_color(BLACK);
2246         draw_box(x1 + 1, y1 + 1, x2 - x1, y2 - y1);
2247         set_color(color);
2248         draw_box(x1, y1, x2 - x1, y2 - y1);
2251 void TrackCanvas::draw_floatauto(Auto *current, 
2252         int x, 
2253         int y, 
2254         int in_x, 
2255         int in_y, 
2256         int out_x, 
2257         int out_y, 
2258         int center_pixel, 
2259         int zoom_track,
2260         int color)
2262         int x1, y1, x2, y2;
2263         int in_x1, in_y1, in_x2, in_y2;
2264         int out_x1, out_y1, out_x2, out_y2;
2265         char string[BCTEXTLEN];
2267 // Center
2268         x1 = x - HANDLE_W / 2;
2269         x2 = x + HANDLE_W / 2;
2270         y1 = center_pixel + y - HANDLE_W / 2;
2271         y2 = center_pixel + y + HANDLE_W / 2;
2273         CLAMP(y1, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2274         CLAMP(y2, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2276         if(y2 - 1 > y1)
2277         {
2278                 set_color(BLACK);
2279                 draw_box(x1 + 1, y1 + 1, x2 - x1, y2 - y1);
2280                 set_color(color);
2281                 draw_box(x1, y1, x2 - x1, y2 - y1);
2282         }
2284 // In handle
2285         in_x1 = in_x - HANDLE_W / 2;
2286         in_x2 = in_x + HANDLE_W / 2;
2287         in_y1 = center_pixel + in_y - HANDLE_W / 2;
2288         in_y2 = center_pixel + in_y + HANDLE_W / 2;
2290         CLAMP(in_y1, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2291         CLAMP(in_y2, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2292         CLAMP(in_y, -zoom_track / 2, zoom_track / 2);
2294         if(in_y2 > in_y1)
2295         {
2296                 set_color(BLACK);
2297                 draw_line(x + 1, center_pixel + y + 1, in_x + 1, center_pixel + in_y + 1);
2298                 draw_box(in_x1 + 1, in_y1 + 1, in_x2 - in_x1, in_y2 - in_y1);
2299                 set_color(color);
2300                 draw_line(x, center_pixel + y, in_x, center_pixel + in_y);
2301                 draw_box(in_x1, in_y1, in_x2 - in_x1, in_y2 - in_y1);
2302         }
2305 // Out handle
2306         out_x1 = out_x - HANDLE_W / 2;
2307         out_x2 = out_x + HANDLE_W / 2;
2308         out_y1 = center_pixel + out_y - HANDLE_W / 2;
2309         out_y2 = center_pixel + out_y + HANDLE_W / 2;
2311         CLAMP(out_y1, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2312         CLAMP(out_y2, center_pixel + -zoom_track / 2, center_pixel + zoom_track / 2);
2313         CLAMP(out_y, -zoom_track / 2, zoom_track / 2);
2315         if(out_y2 > out_y1)
2316         {
2317                 set_color(BLACK);
2318                 draw_line(x + 1, center_pixel + y + 1, out_x + 1, center_pixel + out_y + 1);
2319                 draw_box(out_x1 + 1, out_y1 + 1, out_x2 - out_x1, out_y2 - out_y1);
2320                 set_color(color);
2321                 draw_line(x, center_pixel + y, out_x, center_pixel + out_y);
2322                 draw_box(out_x1, out_y1, out_x2 - out_x1, out_y2 - out_y1);
2323         }
2326 int TrackCanvas::test_auto(Auto *current, 
2327         int x, 
2328         int y, 
2329         int center_pixel, 
2330         int zoom_track, 
2331         int cursor_x, 
2332         int cursor_y, 
2333         int buttonpress)
2335         int x1, y1, x2, y2;
2336         char string[BCTEXTLEN];
2337         int result = 0;
2339         x1 = x - HANDLE_W / 2;
2340         x2 = x + HANDLE_W / 2;
2341         y1 = center_pixel + y - HANDLE_W / 2;
2342         y2 = center_pixel + y + HANDLE_W / 2;
2344         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2345         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2347         if(cursor_x >= x1 && cursor_x < x2 && cursor_y >= y1 && cursor_y < y2)
2348         {
2349                 if(buttonpress && buttonpress != 3)
2350                 {
2351                         mwindow->session->drag_auto = current;
2352                         mwindow->session->drag_start_percentage = current->value_to_percentage();
2353                         mwindow->session->drag_start_position = current->position;
2354                         mwindow->session->drag_origin_x = cursor_x;
2355                         mwindow->session->drag_origin_y = cursor_y;
2356                 }
2357                 result = 1;
2358         }
2360         return result;
2363 int TrackCanvas::test_floatauto(Auto *current, 
2364         int x, 
2365         int y, 
2366         int in_x,
2367         int in_y,
2368         int out_x,
2369         int out_y,
2370         int center_pixel, 
2371         int zoom_track, 
2372         int cursor_x, 
2373         int cursor_y, 
2374         int buttonpress)
2376         int x1, y1, x2, y2;
2377         int in_x1, in_y1, in_x2, in_y2;
2378         int out_x1, out_y1, out_x2, out_y2;
2379         char string[BCTEXTLEN];
2380         int result = 0;
2382         x1 = x - HANDLE_W / 2;
2383         x2 = x + HANDLE_W / 2;
2384         y1 = center_pixel + y - HANDLE_W / 2;
2385         y2 = center_pixel + y + HANDLE_W / 2;
2387         if(y1 < center_pixel + -zoom_track / 2) y1 = center_pixel + -zoom_track / 2;
2388         if(y2 > center_pixel + zoom_track / 2) y2 = center_pixel + zoom_track / 2;
2390         in_x1 = in_x - HANDLE_W / 2;
2391         in_x2 = in_x + HANDLE_W / 2;
2392         in_y1 = center_pixel + in_y - HANDLE_W / 2;
2393         in_y2 = center_pixel + in_y + HANDLE_W / 2;
2395         if(in_y1 < center_pixel + -zoom_track / 2) in_y1 = center_pixel + -zoom_track / 2;
2396         if(in_y2 > center_pixel + zoom_track / 2) in_y2 = center_pixel + zoom_track / 2;
2398         out_x1 = out_x - HANDLE_W / 2;
2399         out_x2 = out_x + HANDLE_W / 2;
2400         out_y1 = center_pixel + out_y - HANDLE_W / 2;
2401         out_y2 = center_pixel + out_y + HANDLE_W / 2;
2403         if(out_y1 < center_pixel + -zoom_track / 2) out_y1 = center_pixel + -zoom_track / 2;
2404         if(out_y2 > center_pixel + zoom_track / 2) out_y2 = center_pixel + zoom_track / 2;
2408 //printf("TrackCanvas::test_floatauto %d %d %d %d %d %d\n", cursor_x, cursor_y, x1, x2, y1, y2);
2409 // Test value
2410         if(!ctrl_down() &&
2411                 cursor_x >= x1 && 
2412                 cursor_x < x2 && 
2413                 cursor_y >= y1 && 
2414                 cursor_y < y2)
2415         {
2416                 if(buttonpress && (buttonpress != 3))
2417                 {
2418                         mwindow->session->drag_auto = current;
2419                         mwindow->session->drag_start_percentage = current->value_to_percentage();
2420                         mwindow->session->drag_start_position = current->position;
2421                         mwindow->session->drag_origin_x = cursor_x;
2422                         mwindow->session->drag_origin_y = cursor_y;
2423                         mwindow->session->drag_handle = 0;
2424                 }
2425                 result = 1;
2426         }
2427         else
2428 // Test in control
2429         if(ctrl_down() &&
2430                 cursor_x >= in_x1 && 
2431                 cursor_x < in_x2 && 
2432                 cursor_y >= in_y1 && 
2433                 cursor_y < in_y2 &&
2434                 current->position > 0)
2435         {
2436                 if(buttonpress && (buttonpress != 3))
2437                 {
2438                         mwindow->session->drag_auto = current;
2439                         mwindow->session->drag_start_percentage = 
2440                                 current->invalue_to_percentage();
2441                         mwindow->session->drag_start_position = 
2442                                 ((FloatAuto*)current)->control_in_position;
2443                         mwindow->session->drag_origin_x = cursor_x;
2444                         mwindow->session->drag_origin_y = cursor_y;
2445                         mwindow->session->drag_handle = 1;
2446                 }
2447                 result = 1;
2448         }
2449         else
2450 // Test out control
2451         if(ctrl_down() &&
2452                 cursor_x >= out_x1 && 
2453                 cursor_x < out_x2 && 
2454                 cursor_y >= out_y1 && 
2455                 cursor_y < out_y2)
2456         {
2457                 if(buttonpress && (buttonpress != 3))
2458                 {
2459                         mwindow->session->drag_auto = current;
2460                         mwindow->session->drag_start_percentage = 
2461                                 current->outvalue_to_percentage();
2462                         mwindow->session->drag_start_position = 
2463                                 ((FloatAuto*)current)->control_out_position;
2464                         mwindow->session->drag_origin_x = cursor_x;
2465                         mwindow->session->drag_origin_y = cursor_y;
2466                         mwindow->session->drag_handle = 2;
2467                 }
2468                 result = 1;
2469         }
2471 // if(buttonpress) 
2472 // 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", 
2473 // mwindow->session->drag_handle,
2474 // ctrl_down(),
2475 // cursor_x,
2476 // cursor_y,
2477 // x1, x2, y1, y2);
2479         return result;
2482 void TrackCanvas::draw_floatline(int center_pixel, 
2483         FloatAuto *previous,
2484         FloatAuto *next,
2485         FloatAutos *autos,
2486         double unit_start,
2487         double zoom_units,
2488         double yscale,
2489         int x1,
2490         int y1,
2491         int x2,
2492         int y2,
2493         int color,
2494         int autogrouptype)
2496 // Solve bezier equation for either every pixel or a certain large number of
2497 // points.
2501 // Not using slope intercept
2502         x1 = MAX(0, x1);
2507         int prev_y;
2508 // Call by reference fails for some reason here
2509         FloatAuto *previous1 = previous, *next1 = next;
2510         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2511         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2512         float automation_range = automation_max - automation_min;
2514         for(int x = x1; x < x2; x++)
2515         {
2516                 int64_t position = (int64_t)(unit_start + x * zoom_units);
2517                 float value = autos->get_value(position, PLAY_FORWARD, previous1, next1);
2518                 AUTOMATIONCLAMPS(value, autogrouptype);
2520                 int y = center_pixel + 
2521                         (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2523                 if(x > x1 && 
2524                         y >= center_pixel - yscale / 2 && 
2525                         y < center_pixel + yscale / 2 - 1)
2526                 {
2527                         set_color(BLACK);
2528                         draw_line(x - 1, prev_y + 1, x, y + 1);
2529                         set_color(color);
2530                         draw_line(x - 1, prev_y, x, y);
2531                 }
2532                 prev_y = y;
2533         }
2538 void TrackCanvas::synchronize_autos(float change, 
2539         Track *skip, 
2540         FloatAuto *fauto, 
2541         int fill_gangs)
2543 // fill mwindow->session->drag_auto_gang
2544         if (fill_gangs == 1 && skip->gang)
2545         {
2546                 for(Track *current = mwindow->edl->tracks->first;
2547                         current;
2548                         current = NEXT)
2549                 {
2550                         if(current->data_type == skip->data_type &&
2551                                 current->gang && 
2552                                 current->record && 
2553                                 current != skip)
2554                         {
2555                                 FloatAutos *fade_autos = (FloatAutos*)current->automation->autos[AUTOMATION_FADE];
2556                                 double position = skip->from_units(fauto->position);
2557                                 FloatAuto *previous = 0, *next = 0;
2559                                 float init_value = fade_autos->get_value(fauto->position, PLAY_FORWARD, previous, next);
2560                                 FloatAuto *keyframe;
2561                                 keyframe = (FloatAuto*)fade_autos->get_auto_at_position(position);
2562                                 
2563                                 if (!keyframe)
2564                                 {
2565 // create keyframe at exactly this point in time
2566                                         keyframe = (FloatAuto*)fade_autos->insert_auto(fauto->position);
2567                                         keyframe->value = init_value;
2568                                 } 
2569                                 else
2570                                 { 
2571 // keyframe exists, just change it
2572                                         keyframe->value += change;              
2573                                 } 
2574                                 
2575                                 keyframe->position = fauto->position;
2576                                 keyframe->control_out_position = fauto->control_out_position;
2577                                 keyframe->control_in_position = fauto->control_in_position;
2578                                 keyframe->control_out_value = fauto->control_out_value;
2579                                 keyframe->control_in_value = fauto->control_in_value;
2581                                 mwindow->session->drag_auto_gang->append((Auto *)keyframe);
2582                         }
2583                 }
2584         } else 
2585 // move the gangs
2586         if (fill_gangs == 0)      
2587         {
2588 // Move the gang!
2589                 for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++)
2590                 {
2591                         FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2592                         
2593                         keyframe->value += change;
2594                         keyframe->position = fauto->position;
2595                         CLAMP(keyframe->value, 
2596                               mwindow->edl->local_session->automation_mins[keyframe->autos->autogrouptype],
2597                               mwindow->edl->local_session->automation_maxs[keyframe->autos->autogrouptype]);
2598                         keyframe->control_out_position = fauto->control_out_position;
2599                         keyframe->control_in_position = fauto->control_in_position;
2600                         keyframe->control_out_value = fauto->control_out_value;
2601                         keyframe->control_in_value = fauto->control_in_value;
2602                 } 
2604         } 
2605         else
2606 // remove the gangs
2607         if (fill_gangs == -1)      
2608         {
2609                 for (int i = 0; i < mwindow->session->drag_auto_gang->total; i++)
2610                 {
2611                         FloatAuto *keyframe = (FloatAuto *)mwindow->session->drag_auto_gang->values[i];
2612                         keyframe->autos->remove_nonsequential(
2613                                         keyframe);
2614                 } 
2615                 mwindow->session->drag_auto_gang->remove_all();
2616         }
2620 int TrackCanvas::test_floatline(int center_pixel, 
2621                 FloatAutos *autos,
2622                 double unit_start,
2623                 double zoom_units,
2624                 double yscale,
2625                 int x1,
2626                 int x2,
2627                 int cursor_x, 
2628                 int cursor_y, 
2629                 int buttonpress,
2630                 int autogrouptype)
2632         int result = 0;
2635         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2636         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2637         float automation_range = automation_max - automation_min;
2638         int64_t position = (int64_t)(unit_start + cursor_x * zoom_units);
2639 // Call by reference fails for some reason here
2640         FloatAuto *previous = 0, *next = 0;
2641         float value = autos->get_value(position, PLAY_FORWARD, previous, next);
2642         AUTOMATIONCLAMPS(value,autogrouptype);
2643         int y = center_pixel + 
2644                 (int)(((value - automation_min) / automation_range - 0.5) * -yscale);
2646         if(cursor_x >= x1 && 
2647                 cursor_x < x2 &&
2648                 cursor_y >= y - HANDLE_W / 2 && 
2649                 cursor_y < y + HANDLE_W / 2 &&
2650                 !ctrl_down())
2651         {
2652                 result = 1;
2655                 if(buttonpress)
2656                 {
2659                         Auto *current;
2660                         current = mwindow->session->drag_auto = autos->insert_auto(position);
2661                         ((FloatAuto*)current)->value = value;
2662                         mwindow->session->drag_start_percentage = current->value_to_percentage();
2663                         mwindow->session->drag_start_position = current->position;
2664                         mwindow->session->drag_origin_x = cursor_x;
2665                         mwindow->session->drag_origin_y = cursor_y;
2666                         mwindow->session->drag_handle = 0;
2668                 }
2669         }
2672         return result;
2675 void TrackCanvas::draw_toggleline(int center_pixel, 
2676         int x1,
2677         int y1,
2678         int x2,
2679         int y2,
2680         int color)
2682         set_color(BLACK);
2683         draw_line(x1, center_pixel + y1 + 1, x2, center_pixel + y1 + 1);
2684         set_color(color);
2685         draw_line(x1, center_pixel + y1, x2, center_pixel + y1);
2687         if(y2 != y1)
2688         {
2689                 set_color(BLACK);
2690                 draw_line(x2 + 1, center_pixel + y1, x2 + 1, center_pixel + y2);
2691                 set_color(color);
2692                 draw_line(x2, center_pixel + y1, x2, center_pixel + y2);
2693         }
2696 int TrackCanvas::test_toggleline(Autos *autos,
2697         int center_pixel, 
2698         int x1,
2699         int y1,
2700         int x2,
2701         int y2, 
2702         int cursor_x, 
2703         int cursor_y, 
2704         int buttonpress)
2706         int result = 0;
2707         if(cursor_x >= x1 && cursor_x < x2)
2708         {
2709                 int miny = center_pixel + y1 - HANDLE_W / 2;
2710                 int maxy = center_pixel + y1 + HANDLE_W / 2;
2711                 if(cursor_y >= miny && cursor_y < maxy) 
2712                 {
2713                         result = 1;
2715                         if(buttonpress)
2716                         {
2719                                 Auto *current;
2720                                 double position = (double)(cursor_x +
2721                                                 mwindow->edl->local_session->view_start) * 
2722                                         mwindow->edl->local_session->zoom_sample / 
2723                                         mwindow->edl->session->sample_rate;
2724                                 int64_t unit_position = autos->track->to_units(position, 0);
2725                                 int new_value = (int)((IntAutos*)autos)->get_automation_constant(unit_position, unit_position);
2727                                 current = mwindow->session->drag_auto = autos->insert_auto(unit_position);
2728                                 ((IntAuto*)current)->value = new_value;
2729                                 mwindow->session->drag_start_percentage = current->value_to_percentage();
2730                                 mwindow->session->drag_start_position = current->position;
2731                                 mwindow->session->drag_origin_x = cursor_x;
2732                                 mwindow->session->drag_origin_y = cursor_y;
2734                         }
2735                 }
2736         };
2737         return result;
2740 void TrackCanvas::calculate_viewport(Track *track, 
2741         double &view_start,   // Seconds
2742         double &unit_start,
2743         double &view_end,     // Seconds
2744         double &unit_end,
2745         double &yscale,
2746         int &center_pixel,
2747         double &zoom_sample,
2748         double &zoom_units)
2750         view_start = (double)mwindow->edl->local_session->view_start * 
2751                 mwindow->edl->local_session->zoom_sample /
2752                 mwindow->edl->session->sample_rate;
2753         unit_start = track->to_doubleunits(view_start);
2754         view_end = (double)(mwindow->edl->local_session->view_start + 
2755                 get_w()) * 
2756                 mwindow->edl->local_session->zoom_sample / 
2757                 mwindow->edl->session->sample_rate;
2758         unit_end = track->to_doubleunits(view_end);
2759         yscale = mwindow->edl->local_session->zoom_track;
2760         center_pixel = (int)(track->y_pixel + yscale / 2) + 
2761                 (mwindow->edl->session->show_titles ? 
2762                         mwindow->theme->get_image("title_bg_data")->get_h() : 
2763                         0);
2764         zoom_sample = mwindow->edl->local_session->zoom_sample;
2766         zoom_units = track->to_doubleunits(zoom_sample / mwindow->edl->session->sample_rate);
2769 float TrackCanvas::percentage_to_value(float percentage, 
2770         int is_toggle,
2771         Auto *reference,
2772         int autogrouptype)
2774         float result;
2775         if(is_toggle)
2776         {
2777                 if(percentage > 0.5) 
2778                         result = 1;
2779                 else
2780                         result = 0;
2781         }
2782         else
2783         {
2784                 float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2785                 float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2786                 float automation_range = automation_max - automation_min;
2788                 result = percentage * automation_range + automation_min;
2789                 if(reference)
2790                 {
2791                         FloatAuto *ptr = (FloatAuto*)reference;
2792                         result -= ptr->value;
2793                 }
2794         }
2795         return result;
2799 void TrackCanvas::calculate_auto_position(double *x, 
2800         double *y,
2801         double *in_x,
2802         double *in_y,
2803         double *out_x,
2804         double *out_y,
2805         Auto *current,
2806         double unit_start,
2807         double zoom_units,
2808         double yscale,
2809         int autogrouptype)
2811         float automation_min = mwindow->edl->local_session->automation_mins[autogrouptype];
2812         float automation_max = mwindow->edl->local_session->automation_maxs[autogrouptype];
2813         float automation_range = automation_max - automation_min;
2814         FloatAuto *ptr = (FloatAuto*)current;
2815         *x = (double)(ptr->position - unit_start) / zoom_units;
2816         *y = ((ptr->value - automation_min) /
2817                 automation_range - 0.5) * 
2818                 -yscale;
2819         if(in_x)
2820         {
2821                 *in_x = (double)(ptr->position + 
2822                         ptr->control_in_position - 
2823                         unit_start) /
2824                         zoom_units;
2825         }
2826         if(in_y)
2827         {
2828                 *in_y = (((ptr->value + ptr->control_in_value) -
2829                         automation_min) /
2830                         automation_range - 0.5) *
2831                         -yscale;
2832         }
2833         if(out_x)
2834         {
2835                 *out_x = (double)(ptr->position + 
2836                         ptr->control_out_position - 
2837                         unit_start) /
2838                         zoom_units;
2839         }
2840         if(out_y)
2841         {
2842                 *out_y = (((ptr->value + ptr->control_out_value) -
2843                         automation_min) /
2844                         automation_range - 0.5) *
2845                         -yscale;
2846         }
2853 int TrackCanvas::do_float_autos(Track *track, 
2854                 Autos *autos, 
2855                 int cursor_x, 
2856                 int cursor_y, 
2857                 int draw, 
2858                 int buttonpress,
2859                 int color,
2860                 Auto* &auto_instance,
2861                 int autogrouptype)
2863         int result = 0;
2865         double view_start;
2866         double unit_start;
2867         double view_end;
2868         double unit_end;
2869         double yscale;
2870         int center_pixel;
2871         double zoom_sample;
2872         double zoom_units;
2873         double ax, ay, ax2, ay2;
2874         double in_x2, in_y2, out_x2, out_y2;
2875         int draw_auto;
2876         double slope;
2877         int skip = 0;
2878         
2879         auto_instance = 0;
2881         calculate_viewport(track, 
2882                 view_start,
2883                 unit_start,
2884                 view_end,
2885                 unit_end,
2886                 yscale,
2887                 center_pixel,
2888                 zoom_sample,
2889                 zoom_units);
2893 // Get first auto before start
2894         Auto *current = 0;
2895         Auto *previous = 0;
2896         for(current = autos->last; 
2897                 current && current->position >= unit_start; 
2898                 current = PREVIOUS)
2899                 ;
2901         if(current)
2902         {
2903                 calculate_auto_position(&ax, 
2904                         &ay,
2905                         0,
2906                         0,
2907                         0,
2908                         0,
2909                         current,
2910                         unit_start,
2911                         zoom_units,
2912                         yscale,
2913                         autogrouptype);
2914                 current = NEXT;
2915         }
2916         else
2917         {
2918                 current = autos->first ? autos->first : autos->default_auto;
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                         ax = 0;
2933                 }
2934                 else
2935                 {
2936                         ax = 0;
2937                         ay = 0;
2938                 }
2939         }
2945         do
2946         {
2947                 skip = 0;
2948                 draw_auto = 1;
2950                 if(current)
2951                 {
2952                         calculate_auto_position(&ax2, 
2953                                 &ay2,
2954                                 &in_x2,
2955                                 &in_y2,
2956                                 &out_x2,
2957                                 &out_y2,
2958                                 current,
2959                                 unit_start,
2960                                 zoom_units,
2961                                 yscale,
2962                                 autogrouptype);
2963                 }
2964                 else
2965                 {
2966                         ax2 = get_w();
2967                         ay2 = ay;
2968                         skip = 1;
2969                 }
2971                 slope = (ay2 - ay) / (ax2 - ax);
2973                 if(ax2 > get_w())
2974                 {
2975                         draw_auto = 0;
2976                         ax2 = get_w();
2977                         ay2 = ay + slope * (get_w() - ax);
2978                 }
2979                 
2980                 if(ax < 0)
2981                 {
2982                         ay = ay + slope * (0 - ax);
2983                         ax = 0;
2984                 }
2999 // Draw handle
3000                 if(current && !result)
3001                 {
3002                         if(current != autos->default_auto)
3003                         {
3004                                 if(!draw)
3005                                 {
3006                                         if(track->record)
3007                                                 result = test_floatauto(current, 
3008                                                         (int)ax2, 
3009                                                         (int)ay2, 
3010                                                         (int)in_x2,
3011                                                         (int)in_y2,
3012                                                         (int)out_x2,
3013                                                         (int)out_y2,
3014                                                         (int)center_pixel, 
3015                                                         (int)yscale, 
3016                                                         cursor_x, 
3017                                                         cursor_y, 
3018                                                         buttonpress);
3019                                         if (result) 
3020                                                 auto_instance = current;
3021                                 }
3022                                 else
3023                                 if(draw_auto)
3024                                         draw_floatauto(current, 
3025                                                 (int)ax2, 
3026                                                 (int)ay2, 
3027                                                 (int)in_x2,
3028                                                 (int)in_y2,
3029                                                 (int)out_x2,
3030                                                 (int)out_y2,
3031                                                 (int)center_pixel, 
3032                                                 (int)yscale,
3033                                                 color);
3034                         }
3035                 }
3041 // Draw joining line
3042                 if(!draw)
3043                 {
3044                         if(!result)
3045                         {
3046                                 if(track->record && buttonpress != 3)
3047                                 {
3048                                         result = test_floatline(center_pixel, 
3049                                                 (FloatAutos*)autos,
3050                                                 unit_start,
3051                                                 zoom_units,
3052                                                 yscale,
3053                                                 (int)ax,
3054 // Exclude auto coverage from the end of the line.  The auto overlaps
3055                                                 (int)ax2 - HANDLE_W / 2,
3056                                                 cursor_x, 
3057                                                 cursor_y, 
3058                                                 buttonpress,
3059                                                 autogrouptype);
3060                                 }
3061                         }
3062                 }
3063                 else
3064                         draw_floatline(center_pixel,
3065                                 (FloatAuto*)previous,
3066                                 (FloatAuto*)current,
3067                                 (FloatAutos*)autos,
3068                                 unit_start,
3069                                 zoom_units,
3070                                 yscale,
3071                                 (int)ax, 
3072                                 (int)ay, 
3073                                 (int)ax2, 
3074                                 (int)ay2,
3075                                 color,
3076                                 autogrouptype);
3084                 if(current)
3085                 {
3086                         previous = current;
3087                         current = NEXT;
3088                 }
3092                 ax = ax2;
3093                 ay = ay2;
3094         }while(current && 
3095                 current->position <= unit_end && 
3096                 !result);
3098 //printf("TrackCanvas::do_float_autos 100\n");
3107         if(ax < get_w() && !result)
3108         {
3109                 ax2 = get_w();
3110                 ay2 = ay;
3111                 if(!draw)
3112                 {
3113                         if(track->record && buttonpress != 3)
3114                         {
3115                                 result = test_floatline(center_pixel, 
3116                                         (FloatAutos*)autos,
3117                                         unit_start,
3118                                         zoom_units,
3119                                         yscale,
3120                                         (int)ax,
3121                                         (int)ax2,
3122                                         cursor_x, 
3123                                         cursor_y, 
3124                                         buttonpress,
3125                                         autogrouptype);
3126                         }
3127                 }
3128                 else
3129                         draw_floatline(center_pixel, 
3130                                 (FloatAuto*)previous,
3131                                 (FloatAuto*)current,
3132                                 (FloatAutos*)autos,
3133                                 unit_start,
3134                                 zoom_units,
3135                                 yscale,
3136                                 (int)ax, 
3137                                 (int)ay, 
3138                                 (int)ax2, 
3139                                 (int)ay2,
3140                                 color,
3141                                 autogrouptype);
3142         }
3151         return result;
3155 int TrackCanvas::do_toggle_autos(Track *track, 
3156                 Autos *autos, 
3157                 int cursor_x, 
3158                 int cursor_y, 
3159                 int draw, 
3160                 int buttonpress,
3161                 int color,
3162                 Auto * &auto_instance)
3164         int result = 0;
3165         double view_start;
3166         double unit_start;
3167         double view_end;
3168         double unit_end;
3169         double yscale;
3170         int center_pixel;
3171         double zoom_sample;
3172         double zoom_units;
3173         double ax, ay, ax2, ay2;
3174         
3175         auto_instance = 0;
3177         calculate_viewport(track, 
3178                 view_start,
3179                 unit_start,
3180                 view_end,
3181                 unit_end,
3182                 yscale,
3183                 center_pixel,
3184                 zoom_sample,
3185                 zoom_units);
3188         double high = -yscale * 0.8 / 2;
3189         double low = yscale * 0.8 / 2;
3191 // Get first auto before start
3192         Auto *current;
3193         for(current = autos->last; current && current->position >= unit_start; current = PREVIOUS)
3194                 ;
3196         if(current)
3197         {
3198                 ax = 0;
3199                 ay = ((IntAuto*)current)->value > 0 ? high : low;
3200                 current = NEXT;
3201         }
3202         else
3203         {
3204                 current = autos->first ? autos->first : autos->default_auto;
3205                 if(current)
3206                 {
3207                         ax = 0;
3208                         ay = ((IntAuto*)current)->value > 0 ? high : low;
3209                 }
3210                 else
3211                 {
3212                         ax = 0;
3213                         ay = yscale;
3214                 }
3215         }
3217         do
3218         {
3219                 if(current)
3220                 {
3221                         ax2 = (double)(current->position - unit_start) / zoom_units;
3222                         ay2 = ((IntAuto*)current)->value > 0 ? high : low;
3223                 }
3224                 else
3225                 {
3226                         ax2 = get_w();
3227                         ay2 = ay;
3228                 }
3230                 if(ax2 > get_w()) ax2 = get_w();
3232             if(current && !result) 
3233                 {
3234                         if(current != autos->default_auto)
3235                         {
3236                                 if(!draw)
3237                                 {
3238                                         if(track->record)
3239                                         {
3240                                                 result = test_auto(current, 
3241                                                         (int)ax2, 
3242                                                         (int)ay2, 
3243                                                         (int)center_pixel, 
3244                                                         (int)yscale, 
3245                                                         cursor_x, 
3246                                                         cursor_y, 
3247                                                         buttonpress);
3248                                                 if (result)
3249                                                         auto_instance = current;
3250                                         }
3251                                 }
3252                                 else
3253                                         draw_auto(current, 
3254                                                 (int)ax2, 
3255                                                 (int)ay2, 
3256                                                 (int)center_pixel, 
3257                                                 (int)yscale,
3258                                                 color);
3259                         }
3261                         current = NEXT;
3262                 }
3264                 if(!draw)
3265                 {
3266                         if(!result)
3267                         {
3268                                 if(track->record && buttonpress != 3)
3269                                 {
3270                                         result = test_toggleline(autos, 
3271                                                 center_pixel, 
3272                                                 (int)ax, 
3273                                                 (int)ay, 
3274                                                 (int)ax2, 
3275                                                 (int)ay2,
3276                                                 cursor_x, 
3277                                                 cursor_y, 
3278                                                 buttonpress);
3279                                 }
3280                         }
3281                 }
3282                 else
3283                         draw_toggleline(center_pixel, 
3284                                 (int)ax, 
3285                                 (int)ay, 
3286                                 (int)ax2, 
3287                                 (int)ay2,
3288                                 color);
3290                 ax = ax2;
3291                 ay = ay2;
3292         }while(current && current->position <= unit_end && !result);
3294         if(ax < get_w() && !result)
3295         {
3296                 ax2 = get_w();
3297                 ay2 = ay;
3298                 if(!draw)
3299                 {
3300                         if(track->record && buttonpress != 3)
3301                         {
3302                                 result = test_toggleline(autos,
3303                                         center_pixel, 
3304                                         (int)ax, 
3305                                         (int)ay, 
3306                                         (int)ax2, 
3307                                         (int)ay2,
3308                                         cursor_x, 
3309                                         cursor_y, 
3310                                         buttonpress);
3311                         }
3312                 }
3313                 else
3314                         draw_toggleline(center_pixel, 
3315                                 (int)ax, 
3316                                 (int)ay, 
3317                                 (int)ax2, 
3318                                 (int)ay2,
3319                                 color);
3320         }
3321         return result;
3324 int TrackCanvas::do_autos(Track *track, 
3325                 Autos *autos, 
3326                 int cursor_x, 
3327                 int cursor_y, 
3328                 int draw, 
3329                 int buttonpress,
3330                 BC_Pixmap *pixmap,
3331                 Auto * &auto_instance)
3333         int result = 0;
3335         double view_start;
3336         double unit_start;
3337         double view_end;
3338         double unit_end;
3339         double yscale;
3340         int center_pixel;
3341         double zoom_sample;
3342         double zoom_units;
3344         calculate_viewport(track, 
3345                 view_start,
3346                 unit_start,
3347                 view_end,
3348                 unit_end,
3349                 yscale,
3350                 center_pixel,
3351                 zoom_sample,
3352                 zoom_units);
3354         Auto *current;
3355         auto_instance = 0;
3357         for(current = autos->first; current && !result; current = NEXT)
3358         {
3359                 if(current->position >= unit_start && current->position < unit_end)
3360                 {
3361                         int64_t x, y;
3362                         x = (int64_t)((double)(current->position - unit_start) / 
3363                                 zoom_units - (pixmap->get_w() / 2 + 0.5));
3364                         y = center_pixel - pixmap->get_h() / 2;
3366                         if(!draw)
3367                         {
3368                                 if(cursor_x >= x && cursor_y >= y &&
3369                                         cursor_x < x + pixmap->get_w() &&
3370                                         cursor_y < y + pixmap->get_h())
3371                                 {
3372                                         result = 1;
3373                                         auto_instance = current;
3375                                         if(buttonpress && (buttonpress != 3))
3376                                         {
3377                                                 mwindow->session->drag_auto = current;
3378                                                 mwindow->session->drag_start_position = current->position;
3379                                                 mwindow->session->drag_origin_x = cursor_x;
3380                                                 mwindow->session->drag_origin_y = cursor_y;
3382                                                 double position = autos->track->from_units(current->position);
3383                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3384                                                         mwindow->edl->local_session->get_selectionend(1)) / 
3385                                                         2;
3387                                                 if(!shift_down())
3388                                                 {
3389                                                         mwindow->edl->local_session->set_selectionstart(position);
3390                                                         mwindow->edl->local_session->set_selectionend(position);
3391                                                 }
3392                                                 else
3393                                                 if(position < center)
3394                                                 {
3395                                                         mwindow->edl->local_session->set_selectionstart(position);
3396                                                 }
3397                                                 else
3398                                                         mwindow->edl->local_session->set_selectionend(position);
3399                                         }
3400                                 }
3401                         }
3402                         else
3403                                 draw_pixmap(pixmap, x, y);
3404                 }
3405         }
3406         return result;
3409 // so this means it is always >0 when keyframe is found 
3410 int TrackCanvas::do_plugin_autos(Track *track, 
3411                 int cursor_x, 
3412                 int cursor_y, 
3413                 int draw, 
3414                 int buttonpress,
3415                 Plugin* &keyframe_plugin,
3416                 KeyFrame* &keyframe_instance)
3418         int result = 0;
3420         double view_start;
3421         double unit_start;
3422         double view_end;
3423         double unit_end;
3424         double yscale;
3425         int center_pixel;
3426         double zoom_sample;
3427         double zoom_units;
3429         if(!track->expand_view) return 0;
3431         calculate_viewport(track, 
3432                 view_start,
3433                 unit_start,
3434                 view_end,
3435                 unit_end,
3436                 yscale,
3437                 center_pixel,
3438                 zoom_sample,
3439                 zoom_units);
3443         for(int i = 0; i < track->plugin_set.total && !result; i++)
3444         {
3445                 PluginSet *plugin_set = track->plugin_set.values[i];
3446                 int center_pixel = (int)(track->y_pixel + 
3447                         mwindow->edl->local_session->zoom_track +
3448                         (i + 0.5) * mwindow->theme->get_image("plugin_bg_data")->get_h() + 
3449                         (mwindow->edl->session->show_titles ? mwindow->theme->get_image("title_bg_data")->get_h() : 0));
3451                 for(Plugin *plugin = (Plugin*)plugin_set->first; 
3452                         plugin && !result; 
3453                         plugin = (Plugin*)plugin->next)
3454                 {
3455                         for(KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first; 
3456                                 keyframe && !result; 
3457                                 keyframe = (KeyFrame*)keyframe->next)
3458                         {
3459 //printf("TrackCanvas::draw_plugin_autos 3 %d\n", keyframe->position);
3460                                 if(keyframe->position >= unit_start && keyframe->position < unit_end)
3461                                 {
3462                                         int64_t x = (int64_t)((keyframe->position - unit_start) / zoom_units);
3463                                         int y = center_pixel - keyframe_pixmap->get_h() / 2;
3465 //printf("TrackCanvas::draw_plugin_autos 4 %d %d\n", x, center_pixel);
3466                                         if(!draw)
3467                                         {
3468                                                 if(cursor_x >= x && cursor_y >= y &&
3469                                                         cursor_x < x + keyframe_pixmap->get_w() &&
3470                                                         cursor_y < y + keyframe_pixmap->get_h())
3471                                                 {
3472                                                         result = 1;
3473                                                         keyframe_plugin = plugin;
3474                                                         keyframe_instance = keyframe;
3476                                                         if(buttonpress)
3477                                                         {
3478                                                                 mwindow->session->drag_auto = keyframe;
3479                                                                 mwindow->session->drag_start_position = keyframe->position;
3480                                                                 mwindow->session->drag_origin_x = cursor_x;
3481                                                                 mwindow->session->drag_origin_y = cursor_y;
3483                                                                 double position = track->from_units(keyframe->position);
3484                                                                 double center = (mwindow->edl->local_session->get_selectionstart(1) +
3485                                                                         mwindow->edl->local_session->get_selectionend(1)) / 
3486                                                                         2;
3488                                                                 if(!shift_down())
3489                                                                 {
3490                                                                         mwindow->edl->local_session->set_selectionstart(position);
3491                                                                         mwindow->edl->local_session->set_selectionend(position);
3492                                                                 }
3493                                                                 else
3494                                                                 if(position < center)
3495                                                                 {
3496                                                                         mwindow->edl->local_session->set_selectionstart(position);
3497                                                                 }
3498                                                                 else
3499                                                                         mwindow->edl->local_session->set_selectionend(position);
3500                                                         }
3501                                                 }
3502                                         }
3503                                         else
3504                                                 draw_pixmap(keyframe_pixmap, 
3505                                                         x, 
3506                                                         y);
3507                                 }
3508                         }
3509                 }
3510         }
3511 //printf("TrackCanvas::draw_plugin_autos 5\n");
3512         return result;
3515 void TrackCanvas::draw_overlays()
3517         int new_cursor, update_cursor, rerender;
3519 // Move background pixmap to foreground pixmap
3520         draw_pixmap(background_pixmap, 
3521                 0, 
3522                 0,
3523                 get_w(),
3524                 get_h(),
3525                 0,
3526                 0);
3528 // In/Out points
3529         draw_inout_points();
3531 // Transitions
3532         if(mwindow->edl->session->auto_conf->transitions) draw_transitions();
3534 // Plugins
3535         draw_plugins();
3537 // Loop points
3538         draw_loop_points();
3539         draw_brender_start();
3541 // Highlighted areas
3542         draw_highlighting();
3544 // Automation
3545         do_keyframes(0, 
3546                 0, 
3547                 1, 
3548                 0, 
3549                 new_cursor, 
3550                 update_cursor,
3551                 rerender);
3553 // Selection cursor
3554         if(gui->cursor) gui->cursor->restore(1);
3556 // Handle dragging
3557         draw_drag_handle();
3559 // Playback cursor
3560         draw_playback_cursor();
3564 int TrackCanvas::activate()
3566         if(!active)
3567         {
3568                 get_top_level()->deactivate();
3569                 active = 1;
3570                 set_active_subwindow(this);
3571                 gui->cursor->activate();
3572         }
3573         return 0;
3576 int TrackCanvas::deactivate()
3578         if(active)
3579         {
3580                 active = 0;
3581                 gui->cursor->deactivate();
3582         }
3583         return 0;
3587 void TrackCanvas::update_drag_handle()
3589         double new_position;
3591         new_position = 
3592                 (double)(get_cursor_x() + mwindow->edl->local_session->view_start) *
3593                 mwindow->edl->local_session->zoom_sample /
3594                 mwindow->edl->session->sample_rate;
3595         new_position = 
3596                 mwindow->edl->align_to_frame(new_position, 0);
3599         if(new_position != mwindow->session->drag_position)
3600         {
3601                 mwindow->session->drag_position = new_position;
3602                 gui->mainclock->update(new_position);
3603 // Que the CWindow.  Doesn't do anything if selectionstart and selection end 
3604 // aren't changed.
3605 //              mwindow->cwindow->update(1, 0, 0);
3606         }
3609 int TrackCanvas::update_drag_edit()
3611         int result = 0;
3612         
3613         
3614         
3615         return result;
3618 #define UPDATE_DRAG_HEAD(do_clamp) \
3619         int result = 0; \
3620         int x = cursor_x - mwindow->session->drag_origin_x; \
3621         int y = cursor_y - mwindow->session->drag_origin_y; \
3623         if(!current->autos->track->record) return 0; \
3624         double view_start; \
3625         double unit_start; \
3626         double view_end; \
3627         double unit_end; \
3628         double yscale; \
3629         int center_pixel; \
3630         double zoom_sample; \
3631         double zoom_units; \
3633         calculate_viewport(current->autos->track,  \
3634                 view_start, \
3635                 unit_start, \
3636                 view_end, \
3637                 unit_end, \
3638                 yscale, \
3639                 center_pixel, \
3640                 zoom_sample, \
3641                 zoom_units); \
3643         float percentage = (float)(mwindow->session->drag_origin_y - cursor_y) / \
3644                 yscale +  \
3645                 mwindow->session->drag_start_percentage; \
3646         if(do_clamp) CLAMP(percentage, 0, 1); \
3648         int64_t position = Units::to_int64(zoom_units * \
3649                 (cursor_x - mwindow->session->drag_origin_x) + \
3650                 mwindow->session->drag_start_position); \
3652         if((do_clamp) && position < 0) position = 0;
3662 int TrackCanvas::update_drag_floatauto(int cursor_x, int cursor_y)
3664         FloatAuto *current = (FloatAuto*)mwindow->session->drag_auto;
3666         UPDATE_DRAG_HEAD(mwindow->session->drag_handle == 0);
3668         float value;
3669         float old_value;
3671         switch(mwindow->session->drag_handle)
3672         {
3673 // Center
3674                 case 0:
3675 // Snap to nearby values
3676                         old_value = current->value;
3677                         if(shift_down())
3678                         {
3679                                 double value1;
3680                                 double distance1;
3681                                 double value2;
3682                                 double distance2;
3684                                 if(current->previous)
3685                                 {
3686                                         int autogrouptype = current->previous->autos->autogrouptype;
3687                                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
3688                                         value1 = ((FloatAuto*)current->previous)->value;
3689                                         distance1 = fabs(value - value1);
3690                                         current->value = value1;
3691                                 }
3693                                 if(current->next)
3694                                 {
3695                                         int autogrouptype = current->next->autos->autogrouptype;
3696                                         value = percentage_to_value(percentage, 0, 0, autogrouptype);
3697                                         value2 = ((FloatAuto*)current->next)->value;
3698                                         distance2 = fabs(value - value2);
3699                                         if(!current->previous || distance2 < distance1)
3700                                         {
3701                                                 current->value = value2;
3702                                         }
3703                                 }
3705                                 if(!current->previous && !current->next)
3706                                 {
3707                                         current->value = ((FloatAutos*)current->autos)->default_;
3708                                 }
3709                                 value = current->value;
3710                         }
3711                         else
3712                         {
3713                                 int autogrouptype = current->autos->autogrouptype;
3714                                 value = percentage_to_value(percentage, 0, 0, autogrouptype);
3715                         }
3717                         if(value != old_value || position != current->position)
3718                         {
3719                                 result = 1;
3720                                 float change = value - old_value;               
3721                                 current->value = value;
3722                                 current->position = position;
3723                                 synchronize_autos(change, current->autos->track, current, 0);
3725                                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3726                                 Units::totext(string2, 
3727                                         current->autos->track->from_units(current->position),
3728                                         mwindow->edl->session->time_format,
3729                                         mwindow->edl->session->sample_rate,
3730                                         mwindow->edl->session->frame_rate,
3731                                         mwindow->edl->session->frames_per_foot);
3732                                 sprintf(string, "%s, %.2f", string2, current->value);
3733                                 gui->show_message(string);
3734                         }
3735                         break;
3737 // In control
3738                 case 1:
3739                 {
3740                         int autogrouptype = current->autos->autogrouptype;
3741                         value = percentage_to_value(percentage, 0, current, autogrouptype);
3742                         position = MIN(0, position);
3743                         if(value != current->control_in_value || 
3744                                 position != current->control_in_position)
3745                         {
3746                                 result = 1;
3747                                 current->control_in_value = value;
3748                                 current->control_in_position = position;
3749                                 synchronize_autos(0, current->autos->track, current, 0);
3751                                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3752                                 Units::totext(string2, 
3753                                         current->autos->track->from_units(current->control_in_position),
3754                                         mwindow->edl->session->time_format,
3755                                         mwindow->edl->session->sample_rate,
3756                                         mwindow->edl->session->frame_rate,
3757                                         mwindow->edl->session->frames_per_foot);
3758                                 sprintf(string, "%s, %.2f", string2, current->control_in_value);
3759                                 gui->show_message(string);
3760                         }
3761                 }
3762                         break;
3764 // Out control
3765                 case 2:
3766                 {
3767                         int autogrouptype = current->autos->autogrouptype;
3768                         value = percentage_to_value(percentage, 0, current, autogrouptype);
3769                         position = MAX(0, position);
3770                         if(value != current->control_out_value || 
3771                                 position != current->control_out_position)
3772                         {
3773                                 result = 1;
3774                                 current->control_out_value = value;
3775                                 current->control_out_position = position;
3776                                 synchronize_autos(0, current->autos->track, current, 0);
3778                                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3779                                 Units::totext(string2, 
3780                                         current->autos->track->from_units(
3781                                                 ((FloatAuto*)current)->control_out_position),
3782                                         mwindow->edl->session->time_format,
3783                                         mwindow->edl->session->sample_rate,
3784                                         mwindow->edl->session->frame_rate,
3785                                         mwindow->edl->session->frames_per_foot);
3786                                 sprintf(string, "%s, %.2f", 
3787                                         string2, 
3788                                         ((FloatAuto*)current)->control_out_value);
3789                                 gui->show_message(string);
3790                         }
3791                 }
3792                         break;
3793         }
3795         return result;
3798 int TrackCanvas::update_drag_toggleauto(int cursor_x, int cursor_y)
3800         IntAuto *current = (IntAuto*)mwindow->session->drag_auto;
3802         UPDATE_DRAG_HEAD(1);
3803         int value = (int)percentage_to_value(percentage, 1, 0, AUTOGROUPTYPE_INT255);
3805         if(value != current->value || position != current->position)
3806         {
3807                 result = 1;
3808                 current->value = value;
3809                 current->position = position;
3811                 char string[BCTEXTLEN], string2[BCTEXTLEN];
3812                 Units::totext(string2, 
3813                         current->autos->track->from_units(current->position),
3814                         mwindow->edl->session->time_format,
3815                         mwindow->edl->session->sample_rate,
3816                         mwindow->edl->session->frame_rate,
3817                         mwindow->edl->session->frames_per_foot);
3818                 sprintf(string, "%s, %d", string2, current->value);
3819                 gui->show_message(string);
3820         }
3822         return result;
3825 // Autos which can't change value through dragging.
3827 int TrackCanvas::update_drag_auto(int cursor_x, int cursor_y)
3829         Auto *current = (Auto*)mwindow->session->drag_auto;
3831         UPDATE_DRAG_HEAD(1)
3832         if(position != current->position)
3833         {
3834                 result = 1;
3835                 current->position = position;
3837                 char string[BCTEXTLEN];
3838                 Units::totext(string, 
3839                         current->autos->track->from_units(current->position),
3840                         mwindow->edl->session->time_format,
3841                         mwindow->edl->session->sample_rate,
3842                         mwindow->edl->session->frame_rate,
3843                         mwindow->edl->session->frames_per_foot);
3844                 gui->show_message(string);
3846                 double position_f = current->autos->track->from_units(current->position);
3847                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3848                         mwindow->edl->local_session->get_selectionend(1)) / 
3849                         2;
3850                 if(!shift_down())
3851                 {
3852                         mwindow->edl->local_session->set_selectionstart(position_f);
3853                         mwindow->edl->local_session->set_selectionend(position_f);
3854                 }
3855                 else
3856                 if(position_f < center_f)
3857                 {
3858                         mwindow->edl->local_session->set_selectionstart(position_f);
3859                 }
3860                 else
3861                         mwindow->edl->local_session->set_selectionend(position_f);
3862         }
3865         return result;
3868 int TrackCanvas::update_drag_pluginauto(int cursor_x, int cursor_y)
3870         KeyFrame *current = (KeyFrame*)mwindow->session->drag_auto;
3872         UPDATE_DRAG_HEAD(1)
3873         if(position != current->position)
3874         {
3875 //      printf("uida: autos: %p, track: %p ta: %p\n", current->autos, current->autos->track, current->autos->track->automation);
3876                 Track *track = current->autos->track;
3877                 PluginAutos *pluginautos = (PluginAutos *)current->autos;
3878                 PluginSet *pluginset;
3879                 Plugin *plugin;
3880 // figure out the correct pluginset & correct plugin 
3881                 int found = 0;
3882                 for(int i = 0; i < track->plugin_set.total; i++)
3883                 {
3884                         pluginset = track->plugin_set.values[i];
3885                         for(plugin = (Plugin *)pluginset->first; plugin; plugin = (Plugin *)plugin->next)
3886                         {
3887                                 KeyFrames *keyframes = plugin->keyframes;
3888                                 for(KeyFrame *currentkeyframe = (KeyFrame *)keyframes->first; currentkeyframe; currentkeyframe = (KeyFrame *) currentkeyframe->next)
3889                                 {
3890                                         if (currentkeyframe == current) 
3891                                         {
3892                                                 found = 1;
3893                                                 break;
3894                                         }
3896                                 }
3897                                 if (found) break;                       
3898                         }
3899                         if (found) break;                       
3900                 }       
3901         
3902                 mwindow->session->plugin_highlighted = plugin;
3903                 mwindow->session->track_highlighted = track;
3904                 result = 1;
3905                 current->position = position;
3907                 char string[BCTEXTLEN];
3908                 Units::totext(string, 
3909                         current->autos->track->from_units(current->position),
3910                         mwindow->edl->session->time_format,
3911                         mwindow->edl->session->sample_rate,
3912                         mwindow->edl->session->frame_rate,
3913                         mwindow->edl->session->frames_per_foot);
3914                 gui->show_message(string);
3916                 double position_f = current->autos->track->from_units(current->position);
3917                 double center_f = (mwindow->edl->local_session->get_selectionstart(1) +
3918                         mwindow->edl->local_session->get_selectionend(1)) / 
3919                         2;
3920                 if(!shift_down())
3921                 {
3922                         mwindow->edl->local_session->set_selectionstart(position_f);
3923                         mwindow->edl->local_session->set_selectionend(position_f);
3924                 }
3925                 else
3926                 if(position_f < center_f)
3927                 {
3928                         mwindow->edl->local_session->set_selectionstart(position_f);
3929                 }
3930                 else
3931                         mwindow->edl->local_session->set_selectionend(position_f);
3932         }
3935         return result;
3938 void TrackCanvas::update_drag_caption()
3940         switch(mwindow->session->current_operation)
3941         {
3942                 case DRAG_FADE:
3943                         
3944                         break;
3945         }
3950 int TrackCanvas::cursor_motion_event()
3952         int result, cursor_x, cursor_y;
3953         int update_clock = 0;
3954         int update_zoom = 0;
3955         int update_scroll = 0;
3956         int update_overlay = 0;
3957         int update_cursor = 0;
3958         int new_cursor = 0;
3959         int rerender = 0;
3960         double position = 0;
3961 //printf("TrackCanvas::cursor_motion_event 1\n");
3962         result = 0;
3964 // Default cursor
3965         switch(mwindow->edl->session->editing_mode)
3966         {
3967                 case EDITING_ARROW: new_cursor = ARROW_CURSOR; break;
3968                 case EDITING_IBEAM: new_cursor = IBEAM_CURSOR; break;
3969         }
3971         switch(mwindow->session->current_operation)
3972         {
3973                 case DRAG_EDITHANDLE1:
3974 // Outside threshold.  Upgrade status
3975                         if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
3976                         {
3977                                 mwindow->session->current_operation = DRAG_EDITHANDLE2;
3978                                 update_overlay = 1;
3979                         }
3980                         break;
3982                 case DRAG_EDITHANDLE2:
3983                         update_drag_handle();
3984                         update_overlay = 1;
3985                         break;
3987                 case DRAG_PLUGINHANDLE1:
3988                         if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
3989                         {
3990                                 mwindow->session->current_operation = DRAG_PLUGINHANDLE2;
3991                                 update_overlay = 1;
3992                         }
3993                         break;
3995                 case DRAG_PLUGINHANDLE2:
3996                         update_drag_handle();
3997                         update_overlay = 1;
3998                         break;
4000 // Rubber band curves
4001                 case DRAG_FADE:
4002                 case DRAG_CZOOM:
4003                 case DRAG_PZOOM:
4004                 case DRAG_CAMERA_X:
4005                 case DRAG_CAMERA_Y:
4006                 case DRAG_CAMERA_Z:
4007                 case DRAG_PROJECTOR_X:
4008                 case DRAG_PROJECTOR_Y:
4009                 case DRAG_PROJECTOR_Z:
4010                         rerender = update_overlay = update_drag_floatauto(get_cursor_x(), get_cursor_y());
4011                         break;
4013                 case DRAG_PLAY:
4014                         rerender = update_overlay = update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4015                         break;
4017                 case DRAG_MUTE:
4018                         rerender = update_overlay = update_drag_toggleauto(get_cursor_x(), get_cursor_y());
4019                         break;
4021 // Keyframe icons are sticky
4022                 case DRAG_PAN_PRE:
4023                 case DRAG_MASK_PRE:
4024                 case DRAG_MODE_PRE:
4025                 case DRAG_PLUGINKEY_PRE:
4026                         if(labs(get_cursor_x() - mwindow->session->drag_origin_x) > HANDLE_W)
4027                         {
4028                                 mwindow->session->current_operation++;
4029                                 update_overlay = 1;
4030                         }
4031                         break;
4033                 case DRAG_PAN:
4034                 case DRAG_MASK:
4035                 case DRAG_MODE:
4036                         rerender = update_overlay = 
4037                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4038                         break;
4040                 case DRAG_PLUGINKEY:
4041                         rerender = update_overlay = 
4042                                 update_drag_pluginauto(get_cursor_x(), get_cursor_y());
4043                         break;
4045                 case SELECT_REGION:
4046                 {
4047                         cursor_x = get_cursor_x();
4048                         cursor_y = get_cursor_y();
4049                         position = (double)(cursor_x + mwindow->edl->local_session->view_start) * 
4050                                 mwindow->edl->local_session->zoom_sample /
4051                                 mwindow->edl->session->sample_rate;
4053                         position = mwindow->edl->align_to_frame(position, 0);
4054                         position = MAX(position, 0);
4056                         if(position < selection_midpoint1)
4057                         {
4058                                 mwindow->edl->local_session->set_selectionend(selection_midpoint1);
4059                                 mwindow->edl->local_session->set_selectionstart(position);
4060 // Que the CWindow
4061                                 gui->unlock_window();
4062                                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4063                                 gui->lock_window("TrackCanvas::cursor_motion_event 1");
4064 // Update the faders
4065                                 mwindow->update_plugin_guis();
4066                                 gui->patchbay->update();
4067                         }
4068                         else
4069                         {
4070                                 mwindow->edl->local_session->set_selectionstart(selection_midpoint1);
4071                                 mwindow->edl->local_session->set_selectionend(position);
4072 // Don't que the CWindow
4073                         }
4075                         gui->cursor->hide(0);
4076                         gui->cursor->draw(1);
4077                         flash();
4078                         result = 1;
4079                         update_clock = 1;
4080                         update_zoom = 1;
4081                         update_scroll = 1;
4082                         break;
4083                 }
4085                 default:
4086                         if(is_event_win() && cursor_inside())
4087                         {
4088 // Update clocks
4089                                 cursor_x = get_cursor_x();
4090                                 position = (double)cursor_x * 
4091                                         (double)mwindow->edl->local_session->zoom_sample / 
4092                                         (double)mwindow->edl->session->sample_rate + 
4093                                         (double)mwindow->edl->local_session->view_start * 
4094                                         (double)mwindow->edl->local_session->zoom_sample / 
4095                                         (double)mwindow->edl->session->sample_rate;
4096                                 position = mwindow->edl->align_to_frame(position, 0);
4097                                 update_clock = 1;
4099 // Update cursor
4100                                 if(do_transitions(get_cursor_x(), 
4101                                                 get_cursor_y(), 
4102                                                 0, 
4103                                                 new_cursor, 
4104                                                 update_cursor))
4105                                 {
4106                                         break;
4107                                 }
4108                                 else
4109 // Update cursor
4110                                 if(do_keyframes(get_cursor_x(), 
4111                                         get_cursor_y(), 
4112                                         0, 
4113                                         0, 
4114                                         new_cursor,
4115                                         update_cursor,
4116                                         rerender))
4117                                 {
4118                                         break;
4119                                 }
4120                                 else
4121 // Edit boundaries
4122                                 if(do_edit_handles(get_cursor_x(), 
4123                                         get_cursor_y(), 
4124                                         0, 
4125                                         new_cursor,
4126                                         update_cursor))
4127                                 {
4128                                         break;
4129                                 }
4130                                 else
4131 // Plugin boundaries
4132                                 if(do_plugin_handles(get_cursor_x(), 
4133                                         get_cursor_y(), 
4134                                         0, 
4135                                         new_cursor,
4136                                         update_cursor))
4137                                 {
4138                                         break;
4139                                 }
4140                                 else
4141                                 if(do_edits(get_cursor_x(), 
4142                                         get_cursor_y(), 
4143                                         0, 
4144                                         0, 
4145                                         update_overlay, 
4146                                         rerender,
4147                                         new_cursor,
4148                                         update_cursor))
4149                                 {
4150                                         break;
4151                                 }
4152                         }
4153                         break;
4154         }
4156 //printf("TrackCanvas::cursor_motion_event 1\n");
4157         if(update_cursor && new_cursor != get_cursor())
4158         {
4159                 set_cursor(new_cursor);
4160         }
4162 //printf("TrackCanvas::cursor_motion_event 1 %d\n", rerender);
4163         if(rerender)
4164         {
4165                 mwindow->restart_brender();
4166                 mwindow->sync_parameters(CHANGE_PARAMS);
4167                 mwindow->update_plugin_guis();
4168                 gui->unlock_window();
4169                 mwindow->cwindow->update(1, 0, 0, 0, 1);
4170                 gui->lock_window("TrackCanvas::cursor_motion_event 2");
4171 // Update faders
4172                 gui->patchbay->update();
4173         }
4176         if(update_clock)
4177         {
4178                 if(!mwindow->cwindow->playback_engine->is_playing_back)
4179                         gui->mainclock->update(position);
4180         }
4182         if(update_zoom)
4183         {
4184                 gui->zoombar->update();
4185         }
4187         if(update_scroll)
4188         {
4189                 if(!drag_scroll && 
4190                         (cursor_x >= get_w() || cursor_x < 0 || cursor_y >= get_h() || cursor_y < 0))
4191                         start_dragscroll();
4192                 else
4193                 if(drag_scroll &&
4194                         (cursor_x < get_w() && cursor_x >= 0 && cursor_y < get_h() && cursor_y >= 0))
4195                         stop_dragscroll();
4196         }
4198         if(update_overlay)
4199         {
4200                 draw_overlays();
4201                 flash();
4202         }
4205 //printf("TrackCanvas::cursor_motion_event 100\n");
4206         return result;
4209 void TrackCanvas::start_dragscroll()
4211         if(!drag_scroll)
4212         {
4213                 drag_scroll = 1;
4214                 set_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4215 //printf("TrackCanvas::start_dragscroll 1\n");
4216         }
4219 void TrackCanvas::stop_dragscroll()
4221         if(drag_scroll)
4222         {
4223                 drag_scroll = 0;
4224                 unset_repeat(BC_WindowBase::get_resources()->scroll_repeat);
4225 //printf("TrackCanvas::stop_dragscroll 1\n");
4226         }
4229 int TrackCanvas::repeat_event(int64_t duration)
4231         if(!drag_scroll) return 0;
4232         if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
4234         int sample_movement = 0;
4235         int track_movement = 0;
4236         int64_t x_distance = 0;
4237         int64_t y_distance = 0;
4238         double position = 0;
4239         int result = 0;
4241         switch(mwindow->session->current_operation)
4242         {
4243                 case SELECT_REGION:
4244 //printf("TrackCanvas::repeat_event 1 %d\n", mwindow->edl->local_session->view_start);
4245                         if(get_cursor_x() > get_w())
4246                         {
4247                                 x_distance = get_cursor_x() - get_w();
4248                                 sample_movement = 1;
4249                         }
4250                         else
4251                         if(get_cursor_x() < 0)
4252                         {
4253                                 x_distance = get_cursor_x();
4254                                 sample_movement = 1;
4255                         }
4257                         if(get_cursor_y() > get_h())
4258                         {
4259                                 y_distance = get_cursor_y() - get_h();
4260                                 track_movement = 1;
4261                         }
4262                         else
4263                         if(get_cursor_y() < 0)
4264                         {
4265                                 y_distance = get_cursor_y();
4266                                 track_movement = 1;
4267                         }
4268                         result = 1;
4269                         break;
4270         }
4273         if(sample_movement)
4274         {
4275                 position = (double)(get_cursor_x() + 
4276                         mwindow->edl->local_session->view_start + 
4277                         x_distance) * 
4278                         mwindow->edl->local_session->zoom_sample /
4279                         mwindow->edl->session->sample_rate;
4280                 position = mwindow->edl->align_to_frame(position, 0);
4281                 position = MAX(position, 0);
4283 //printf("TrackCanvas::repeat_event 1 %f\n", position);
4284                 switch(mwindow->session->current_operation)
4285                 {
4286                         case SELECT_REGION:
4287                                 if(position < selection_midpoint1)
4288                                 {
4289                                         mwindow->edl->local_session->set_selectionend(selection_midpoint1);
4290                                         mwindow->edl->local_session->set_selectionstart(position);
4291 // Que the CWindow
4292                                         gui->unlock_window();
4293                                         mwindow->cwindow->update(1, 0, 0);
4294                                         gui->lock_window("TrackCanvas::repeat_event");
4295 // Update the faders
4296                                         mwindow->update_plugin_guis();
4297                                         gui->patchbay->update();
4298                                 }
4299                                 else
4300                                 {
4301                                         mwindow->edl->local_session->set_selectionstart(selection_midpoint1);
4302                                         mwindow->edl->local_session->set_selectionend(position);
4303 // Don't que the CWindow
4304                                 }
4305                                 break;
4306                 }
4308                 mwindow->samplemovement(mwindow->edl->local_session->view_start + 
4309                         x_distance);
4310         }
4312         if(track_movement)
4313         {
4314                 mwindow->trackmovement(mwindow->edl->local_session->track_start + 
4315                         y_distance);
4316         }
4318         return result;
4321 int TrackCanvas::button_release_event()
4323         int redraw = 0, update_overlay = 0, result = 0;
4325         switch(mwindow->session->current_operation)
4326         {
4327                 case DRAG_EDITHANDLE2:
4328                         mwindow->session->current_operation = NO_OPERATION;
4329                         drag_scroll = 0;
4330                         result = 1;
4331                         
4332                         end_edithandle_selection();
4333                         break;
4335                 case DRAG_EDITHANDLE1:
4336                         mwindow->session->current_operation = NO_OPERATION;
4337                         drag_scroll = 0;
4338                         result = 1;
4339                         break;
4341                 case DRAG_PLUGINHANDLE2:
4342                         mwindow->session->current_operation = NO_OPERATION;
4343                         drag_scroll = 0;
4344                         result = 1;
4345                         
4346                         end_pluginhandle_selection();
4347                         break;
4349                 case DRAG_PLUGINHANDLE1:
4350                         mwindow->session->current_operation = NO_OPERATION;
4351                         drag_scroll = 0;
4352                         result = 1;
4353                         break;
4355                 case DRAG_FADE:
4356 // delete the drag_auto_gang first and remove out of order keys
4357                         synchronize_autos(0, 0, 0, -1); 
4358                 case DRAG_CZOOM:
4359                 case DRAG_PZOOM:
4360                 case DRAG_PLAY:
4361                 case DRAG_MUTE:
4362                 case DRAG_MASK:
4363                 case DRAG_MODE:
4364                 case DRAG_PAN:
4365                 case DRAG_CAMERA_X:
4366                 case DRAG_CAMERA_Y:
4367                 case DRAG_CAMERA_Z:
4368                 case DRAG_PROJECTOR_X:
4369                 case DRAG_PROJECTOR_Y:
4370                 case DRAG_PROJECTOR_Z:
4371                 case DRAG_PLUGINKEY:
4372                         mwindow->session->current_operation = NO_OPERATION;
4373                         mwindow->session->drag_handle = 0;
4374 // Remove any out-of-order keyframe
4375                         if(mwindow->session->drag_auto)
4376                         {
4377                                 mwindow->session->drag_auto->autos->remove_nonsequential(
4378                                         mwindow->session->drag_auto);
4379 //                              mwindow->session->drag_auto->autos->optimize();
4380                                 update_overlay = 1;
4381                         }
4382                         mwindow->undo->update_undo(_("keyframe"), LOAD_AUTOMATION);
4383                         result = 1;
4384                         break;
4386                 case DRAG_EDIT:
4387                 case DRAG_AEFFECT_COPY:
4388                 case DRAG_VEFFECT_COPY:
4389 // Trap in drag stop
4391                         break;
4394                 default:
4395                         if(mwindow->session->current_operation)
4396                         {
4397                                 if(mwindow->session->current_operation == SELECT_REGION)
4398                                 {
4399                                         mwindow->undo->update_undo(_("select"), LOAD_SESSION, 0, 0);
4400                                 }
4402                                 mwindow->session->current_operation = NO_OPERATION;
4403                                 drag_scroll = 0;
4404 // Traps button release events
4405 //                              result = 1;
4406                         }
4407                         break;
4408         }
4409         if (result) 
4410                 cursor_motion_event();
4411         if(update_overlay)
4412         {
4413                 draw_overlays();
4414                 flash();
4415         }
4416         if(redraw)
4417         {
4418                 draw();
4419                 flash();
4420         }
4421         return result;
4424 int TrackCanvas::do_edit_handles(int cursor_x, 
4425         int cursor_y, 
4426         int button_press, 
4427         int &new_cursor,
4428         int &update_cursor)
4430         Edit *edit_result = 0;
4431         int handle_result = 0;
4432         int result = 0;
4434         if(!mwindow->edl->session->show_assets) return 0;
4436         for(Track *track = mwindow->edl->tracks->first;
4437                 track && !result;
4438                 track = track->next)
4439         {
4440                 for(Edit *edit = track->edits->first;
4441                         edit && !result;
4442                         edit = edit->next)
4443                 {
4444                         int64_t edit_x, edit_y, edit_w, edit_h;
4445                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4447                         if(cursor_x >= edit_x && cursor_x <= edit_x + edit_w &&
4448                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h)
4449                         {
4450                                 if(cursor_x < edit_x + HANDLE_W)
4451                                 {
4452                                         edit_result = edit;
4453                                         handle_result = 0;
4454                                         result = 1;
4455                                 }
4456                                 else
4457                                 if(cursor_x >= edit_x + edit_w - HANDLE_W)
4458                                 {
4459                                         edit_result = edit;
4460                                         handle_result = 1;
4461                                         result = 1;
4462                                 }
4463                                 else
4464                                 {
4465                                         result = 0;
4466                                 }
4467                         }
4468                 }
4469         }
4471         update_cursor = 1;
4472         if(result)
4473         {
4474                 double position;
4475                 if(handle_result == 0)
4476                 {
4477                         position = edit_result->track->from_units(edit_result->startproject);
4478                         new_cursor = LEFT_CURSOR;
4479                 }
4480                 else
4481                 if(handle_result == 1)
4482                 {
4483                         position = edit_result->track->from_units(edit_result->startproject + edit_result->length);
4484                         new_cursor = RIGHT_CURSOR;
4485                 }
4487 // Reposition cursor
4488                 if(button_press)
4489                 {
4490                         mwindow->session->drag_edit = edit_result;
4491                         mwindow->session->drag_handle = handle_result;
4492                         mwindow->session->drag_button = get_buttonpress() - 1;
4493                         mwindow->session->drag_position = position;
4494                         mwindow->session->current_operation = DRAG_EDITHANDLE1;
4495                         mwindow->session->drag_origin_x = get_cursor_x();
4496                         mwindow->session->drag_origin_y = get_cursor_y();
4497                         mwindow->session->drag_start = position;
4499                         int rerender = start_selection(position);
4500                         if(rerender)
4501                         {
4502                                 gui->unlock_window();
4503                                 mwindow->cwindow->update(1, 0, 0);
4504                                 gui->lock_window("TrackCanvas::do_edit_handles");
4505                         }
4506                         gui->timebar->update_highlights();
4507                         gui->zoombar->update();
4508                         gui->cursor->hide(0);
4509                         gui->cursor->draw(1);
4510                         draw_overlays();
4511                         flash();
4512                         flush();
4513                 }
4514         }
4516         return result;
4519 int TrackCanvas::do_plugin_handles(int cursor_x, 
4520         int cursor_y, 
4521         int button_press,
4522         int &new_cursor,
4523         int &update_cursor)
4525         Plugin *plugin_result = 0;
4526         int handle_result = 0;
4527         int result = 0;
4529         if(!mwindow->edl->session->show_assets) return 0;
4531         for(Track *track = mwindow->edl->tracks->first;
4532                 track && !result;
4533                 track = track->next)
4534         {
4535                 for(int i = 0; i < track->plugin_set.total && !result; i++)
4536                 {
4537                         PluginSet *plugin_set = track->plugin_set.values[i];
4538                         for(Plugin *plugin = (Plugin*)plugin_set->first; 
4539                                 plugin && !result; 
4540                                 plugin = (Plugin*)plugin->next)
4541                         {
4542                                 int64_t plugin_x, plugin_y, plugin_w, plugin_h;
4543                                 plugin_dimensions(plugin, plugin_x, plugin_y, plugin_w, plugin_h);
4545                                 if(cursor_x >= plugin_x && cursor_x <= plugin_x + plugin_w &&
4546                                         cursor_y >= plugin_y && cursor_y < plugin_y + plugin_h)
4547                                 {
4548                                         if(cursor_x < plugin_x + HANDLE_W)
4549                                         {
4550                                                 plugin_result = plugin;
4551                                                 handle_result = 0;
4552                                                 result = 1;
4553                                         }
4554                                         else
4555                                         if(cursor_x >= plugin_x + plugin_w - HANDLE_W)
4556                                         {
4557                                                 plugin_result = plugin;
4558                                                 handle_result = 1;
4559                                                 result = 1;
4560                                         }
4561                                 }
4562                         }
4564                         if(result && shift_down())
4565                                 mwindow->session->trim_edits = plugin_set;
4566                 }
4567         }
4569         update_cursor = 1;
4570         if(result)
4571         {
4572                 double position;
4573                 if(handle_result == 0)
4574                 {
4575                         position = plugin_result->track->from_units(plugin_result->startproject);
4576                         new_cursor = LEFT_CURSOR;
4577                 }
4578                 else
4579                 if(handle_result == 1)
4580                 {
4581                         position = plugin_result->track->from_units(plugin_result->startproject + plugin_result->length);
4582                         new_cursor = RIGHT_CURSOR;
4583                 }
4584                 
4585                 if(button_press)
4586                 {
4587                         mwindow->session->drag_plugin = plugin_result;
4588                         mwindow->session->drag_handle = handle_result;
4589                         mwindow->session->drag_button = get_buttonpress() - 1;
4590                         mwindow->session->drag_position = position;
4591                         mwindow->session->current_operation = DRAG_PLUGINHANDLE1;
4592                         mwindow->session->drag_origin_x = get_cursor_x();
4593                         mwindow->session->drag_origin_y = get_cursor_y();
4594                         mwindow->session->drag_start = position;
4596                         int rerender = start_selection(position);
4597                         if(rerender) 
4598                         {
4599                                 gui->unlock_window();
4600                                 mwindow->cwindow->update(1, 0, 0);
4601                                 gui->lock_window("TrackCanvas::do_plugin_handles");
4602                         }
4603                         gui->timebar->update_highlights();
4604                         gui->zoombar->update();
4605                         gui->cursor->hide(0);
4606                         gui->cursor->draw(1);
4607                         draw_overlays();
4608                         flash();
4609                         flush();
4610                 }
4611         }
4612         
4613         return result;
4617 int TrackCanvas::do_tracks(int cursor_x, 
4618                 int cursor_y,
4619                 int button_press)
4621         int result = 0;
4624         if(!mwindow->edl->session->show_assets) return 0;
4627         for(Track *track = mwindow->edl->tracks->first;
4628                 track && !result;
4629                 track = track->next)
4630         {
4631                 int64_t track_x, track_y, track_w, track_h;
4632                 track_dimensions(track, track_x, track_y, track_w, track_h);
4634                 if(button_press && 
4635                         get_buttonpress() == 3 &&
4636                         cursor_y >= track_y && 
4637                         cursor_y < track_y + track_h)
4638                 {
4639                         gui->edit_menu->update(track, 0);
4640                         gui->edit_menu->activate_menu();
4641                         result = 1;
4642                 }
4643         }
4644         return result;
4647 int TrackCanvas::do_edits(int cursor_x, 
4648         int cursor_y, 
4649         int button_press,
4650         int drag_start,
4651         int &redraw,
4652         int &rerender,
4653         int &new_cursor,
4654         int &update_cursor)
4656         int result = 0;
4657         int over_edit_handle = 0;
4659         if(!mwindow->edl->session->show_assets) return 0;
4661         for(Track *track = mwindow->edl->tracks->first;
4662                 track && !result;
4663                 track = track->next)
4664         {
4665                 for(Edit *edit = track->edits->first;
4666                         edit != track->edits->last && !result;
4667                         edit = edit->next)
4668                 {
4669                         int64_t edit_x, edit_y, edit_w, edit_h;
4670                         edit_dimensions(edit, edit_x, edit_y, edit_w, edit_h);
4672 // Cursor inside a track
4673 // Cursor inside an edit
4674                         if(cursor_x >= edit_x && cursor_x < edit_x + edit_w &&
4675                                 cursor_y >= edit_y && cursor_y < edit_y + edit_h)
4676                         {
4677 // Select duration of edit
4678                                 if(button_press)
4679                                 {
4680                                         if(get_double_click() && !drag_start)
4681                                         {
4682                                                 mwindow->edl->local_session->set_selectionstart(edit->track->from_units(edit->startproject));
4683                                                 mwindow->edl->local_session->set_selectionend(edit->track->from_units(edit->startproject) + 
4684                                                         edit->track->from_units(edit->length));
4685                                                 if(mwindow->edl->session->cursor_on_frames) 
4686                                                 {
4687                                                         mwindow->edl->local_session->set_selectionstart(
4688                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4689                                                         mwindow->edl->local_session->set_selectionend(
4690                                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4691                                                 }
4692                                                 redraw = 1;
4693                                                 rerender = 1;
4694                                                 result = 1;
4695                                         }
4696                                 }
4697                                 else
4698                                 if(drag_start && track->record)
4699                                 {
4700                                         if(mwindow->edl->session->editing_mode == EDITING_ARROW)
4701                                         {
4702 // Need to create drag window
4703                                                 mwindow->session->current_operation = DRAG_EDIT;
4704                                                 mwindow->session->drag_edit = edit;
4705 //printf("TrackCanvas::do_edits 2\n");
4707 // Drag only one edit
4708                                                 if(ctrl_down())
4709                                                 {
4710                                                         mwindow->session->drag_edits->remove_all();
4711                                                         mwindow->session->drag_edits->append(edit);
4712                                                 }
4713                                                 else
4714 // Construct list of all affected edits
4715                                                 {
4716                                                         mwindow->edl->tracks->get_affected_edits(
4717                                                                 mwindow->session->drag_edits, 
4718                                                                 edit->track->from_units(edit->startproject),
4719                                                                 edit->track);
4720                                                 }
4721                                                 mwindow->session->drag_origin_x = cursor_x;
4722                                                 mwindow->session->drag_origin_y = cursor_y;
4723                                                 // Where the drag started, so we know relative position inside the edit later
4724                                                 mwindow->session->drag_position = (double)cursor_x * 
4725                                                         mwindow->edl->local_session->zoom_sample / 
4726                                                         mwindow->edl->session->sample_rate + 
4727                                                         (double)mwindow->edl->local_session->view_start * 
4728                                                         mwindow->edl->local_session->zoom_sample /
4729                                                         mwindow->edl->session->sample_rate;
4731                                                 drag_popup = new BC_DragWindow(gui, 
4732                                                         mwindow->theme->get_image("clip_icon"), 
4733                                                         get_abs_cursor_x(0) - mwindow->theme->get_image("clip_icon")->get_w() / 2,
4734                                                         get_abs_cursor_y(0) - mwindow->theme->get_image("clip_icon")->get_h() / 2);
4736                                                 result = 1;
4737                                         }
4738                                 }
4739                         }
4740                 }
4741         }
4742         return result;
4746 int TrackCanvas::test_resources(int cursor_x, int cursor_y)
4748         return 0;
4751 int TrackCanvas::do_plugins(int cursor_x, 
4752         int cursor_y, 
4753         int drag_start,
4754         int button_press,
4755         int &redraw,
4756         int &rerender)
4758         Plugin *plugin = 0;
4759         int result = 0;
4760         int done = 0;
4761         int64_t x, y, w, h;
4762         Track *track = 0;
4765         if(!mwindow->edl->session->show_assets) return 0;
4768         for(track = mwindow->edl->tracks->first;
4769                 track && !done;
4770                 track = track->next)
4771         {
4772                 if(!track->expand_view) continue;
4775                 for(int i = 0; i < track->plugin_set.total && !done; i++)
4776                 {
4777                         // first check if plugins are visible at all
4778                         if (!track->expand_view)
4779                                 continue;
4780                         PluginSet *plugin_set = track->plugin_set.values[i];
4781                         for(plugin = (Plugin*)plugin_set->first;
4782                                 plugin && !done;
4783                                 plugin = (Plugin*)plugin->next)
4784                         {
4785                                 plugin_dimensions(plugin, x, y, w, h);
4786                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
4787                                         MWindowGUI::visible(y, y + h, 0, get_h()))
4788                                 {
4789                                         if(cursor_x >= x && cursor_x < x + w &&
4790                                                 cursor_y >= y && cursor_y < y + h)
4791                                         {
4792                                                 done = 1;
4793                                                 break;
4794                                         }
4795                                 }
4796                         }
4797                 }
4798         }
4800         if(plugin)
4801         {
4802 // Start plugin popup
4803                 if(button_press)
4804                 {
4805                         if(get_buttonpress() == 3)
4806                         {
4807                                 gui->plugin_menu->update(plugin);
4808                                 gui->plugin_menu->activate_menu();
4809                                 result = 1;
4810                         } 
4811                         else
4812 // Select range of plugin on doubleclick over plugin
4813                         if (get_double_click() && !drag_start)
4814                         {
4815                                 mwindow->edl->local_session->set_selectionstart(plugin->track->from_units(plugin->startproject));
4816                                 mwindow->edl->local_session->set_selectionend(plugin->track->from_units(plugin->startproject) + 
4817                                         plugin->track->from_units(plugin->length));
4818                                 if(mwindow->edl->session->cursor_on_frames) 
4819                                 {
4820                                         mwindow->edl->local_session->set_selectionstart(
4821                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionstart(1), 0));
4822                                         mwindow->edl->local_session->set_selectionend(
4823                                                 mwindow->edl->align_to_frame(mwindow->edl->local_session->get_selectionend(1), 1));
4824                                 }
4825                                 rerender = 1;
4826                                 redraw = 1;
4827                                 result = 1;
4828                         }
4829                 }
4830                 else
4831 // Move plugin
4832                 if(drag_start && plugin->track->record)
4833                 {
4834                         if(mwindow->edl->session->editing_mode == EDITING_ARROW)
4835                         {
4836                                 if(plugin->track->data_type == TRACK_AUDIO)
4837                                         mwindow->session->current_operation = DRAG_AEFFECT_COPY;
4838                                 else
4839                                 if(plugin->track->data_type == TRACK_VIDEO)
4840                                         mwindow->session->current_operation = DRAG_VEFFECT_COPY;
4842                                 mwindow->session->drag_plugin = plugin;
4848 // Create picon
4849                                 switch(plugin->plugin_type)
4850                                 {
4851                                         case PLUGIN_STANDALONE:
4852                                         {
4853                                                 PluginServer *server = mwindow->scan_plugindb(
4854                                                         plugin->title,
4855                                                         plugin->track->data_type);
4856                                                 if (server) 
4857                                                 {
4858                                                         VFrame *frame = server->picon;
4860                                                         drag_popup = new BC_DragWindow(gui, 
4861                                                                 frame, 
4862                                                                 get_abs_cursor_x(0) - frame->get_w() / 2,
4863                                                                 get_abs_cursor_y(0) - frame->get_h() / 2);
4864                                                 }
4865                                                 break;
4866                                         }
4867                                         
4868                                         case PLUGIN_SHAREDPLUGIN:
4869                                         case PLUGIN_SHAREDMODULE:
4870                                                 drag_popup = new BC_DragWindow(gui, 
4871                                                         mwindow->theme->get_image("clip_icon"), 
4872                                                         get_abs_cursor_x(0) - mwindow->theme->get_image("clip_icon")->get_w() / 2,
4873                                                         get_abs_cursor_y(0) - mwindow->theme->get_image("clip_icon")->get_h() / 2);
4874                                                 break;
4875                                 }
4878                                 result = 1;
4879                         }
4880                 }
4881         }
4883         return result;
4886 int TrackCanvas::do_transitions(int cursor_x, 
4887         int cursor_y, 
4888         int button_press,
4889         int &new_cursor,
4890         int &update_cursor)
4892         Transition *transition = 0;
4893         int result = 0;
4894         int64_t x, y, w, h;
4898         if(!mwindow->edl->session->show_assets ||
4899                 !mwindow->edl->session->auto_conf->transitions) return 0;
4900                                         
4903         for(Track *track = mwindow->edl->tracks->first;
4904                 track && !result;
4905                 track = track->next)
4906         {
4907                 for(Edit *edit = track->edits->first;
4908                         edit;
4909                         edit = edit->next)
4910                 {
4911                         if(edit->transition)
4912                         {
4913                                 edit_dimensions(edit, x, y, w, h);
4914                                 get_transition_coords(x, y, w, h);
4916                                 if(MWindowGUI::visible(x, x + w, 0, get_w()) &&
4917                                         MWindowGUI::visible(y, y + h, 0, get_h()))
4918                                 {
4919                                         if(cursor_x >= x && cursor_x < x + w &&
4920                                                 cursor_y >= y && cursor_y < y + h)
4921                                         {
4922                                                 transition = edit->transition;
4923                                                 result = 1;
4924                                                 break;
4925                                         }
4926                                 }
4927                         }
4928                 }
4929         }
4930         
4931         update_cursor = 1;
4932         if(transition)
4933         {
4934                 if(!button_press)
4935                 {
4936                         new_cursor = UPRIGHT_ARROW_CURSOR;
4937                 }
4938                 else
4939                 if(get_buttonpress() == 3)
4940                 {
4941                         gui->transition_menu->update(transition);
4942                         gui->transition_menu->activate_menu();
4943                 }
4944         }
4946         return result;
4949 int TrackCanvas::button_press_event()
4951         int result = 0;
4952         int cursor_x, cursor_y;
4953         int new_cursor, update_cursor;
4955         cursor_x = get_cursor_x();
4956         cursor_y = get_cursor_y();
4957         mwindow->session->trim_edits = 0;
4959         if(is_event_win() && cursor_inside())
4960         {
4961                 double position = (double)cursor_x * 
4962                         mwindow->edl->local_session->zoom_sample /
4963                         mwindow->edl->session->sample_rate + 
4964                         (double)mwindow->edl->local_session->view_start * 
4965                         mwindow->edl->local_session->zoom_sample /
4966                         mwindow->edl->session->sample_rate;
4969                 if(!active)
4970                 {
4971                         activate();
4972                 }
4974                 if(get_buttonpress() == 1)
4975                 {
4976                         gui->unlock_window();
4977                         gui->mbuttons->transport->handle_transport(STOP, 1, 0, 0);
4978                         gui->lock_window("TrackCanvas::button_press_event");
4979                 }
4981                 int update_overlay = 0, update_cursor = 0, rerender = 0;
4983                 if(get_buttonpress() == 4)
4984                 {
4985                         if(shift_down())
4986                                 mwindow->expand_sample();
4987                         else
4988                                 mwindow->move_up(get_h() / 10);
4989                         result = 1;
4990                 }
4991                 else
4992                 if(get_buttonpress() == 5)
4993                 {
4994                         if(shift_down())
4995                                 mwindow->zoom_in_sample();
4996                         else
4997                                 mwindow->move_down(get_h() / 10);
4998                         result = 1;
4999                 }
5000                 else
5001                 switch(mwindow->edl->session->editing_mode)
5002                 {
5003 // Test handles and resource boundaries and highlight a track
5004                         case EDITING_ARROW:
5005                         {
5006                                 Edit *edit;
5007                                 int handle;
5008                                 if(mwindow->edl->session->auto_conf->transitions && 
5009                                         do_transitions(cursor_x, 
5010                                                 cursor_y, 
5011                                                 1, 
5012                                                 new_cursor, 
5013                                                 update_cursor))
5014                                 {
5015                                         break;
5016                                 }
5017                                 else
5018                                 if(do_keyframes(cursor_x, 
5019                                         cursor_y, 
5020                                         0, 
5021                                         get_buttonpress(), 
5022                                         new_cursor, 
5023                                         update_cursor,
5024                                         rerender))
5025                                 {
5026                                         break;
5027                                 }
5028                                 else
5029 // Test edit boundaries
5030                                 if(do_edit_handles(cursor_x, 
5031                                         cursor_y, 
5032                                         1, 
5033                                         new_cursor, 
5034                                         update_cursor))
5035                                 {
5036                                         break;
5037                                 }
5038                                 else
5039 // Test plugin boundaries
5040                                 if(do_plugin_handles(cursor_x, 
5041                                         cursor_y, 
5042                                         1, 
5043                                         new_cursor, 
5044                                         update_cursor))
5045                                 {
5046                                         break;
5047                                 }
5048                                 else
5049                                 if(do_edits(cursor_x, cursor_y, 1, 0, update_cursor, rerender, new_cursor, update_cursor))
5050                                 {
5051                                         break;
5052                                 }
5053                                 else
5054                                 if(do_plugins(cursor_x, cursor_y, 0, 1, update_cursor, rerender))
5055                                 {
5056                                         break;
5057                                 }
5058                                 else
5059                                 if(test_resources(cursor_x, cursor_y))
5060                                 {
5061                                         break;
5062                                 }
5063                                 else
5064                                 if(do_tracks(cursor_x, cursor_y, 1))
5065                                 {
5066                                         break;
5067                                 }
5068                                 break;
5069                         }
5071 // Test handles only and select a region
5072                         case EDITING_IBEAM:
5073                         {
5074 //printf("TrackCanvas::button_press_event %d\n", position);
5076                                 if(mwindow->edl->session->auto_conf->transitions && 
5077                                         do_transitions(cursor_x, 
5078                                                 cursor_y, 
5079                                                 1, 
5080                                                 new_cursor, 
5081                                                 update_cursor))
5082                                 {
5083                                         break;
5084                                 }
5085                                 else
5086                                 if(do_keyframes(cursor_x, 
5087                                         cursor_y, 
5088                                         0, 
5089                                         get_buttonpress(), 
5090                                         new_cursor, 
5091                                         update_cursor,
5092                                         rerender))
5093                                 {
5094                                         update_overlay = 1;
5095                                         break;
5096                                 }
5097                                 else
5098 // Test edit boundaries
5099                                 if(do_edit_handles(cursor_x, 
5100                                         cursor_y, 
5101                                         1, 
5102                                         new_cursor, 
5103                                         update_cursor))
5104                                 {
5105                                         break;
5106                                 }
5107                                 else
5108 // Test plugin boundaries
5109                                 if(do_plugin_handles(cursor_x, 
5110                                         cursor_y, 
5111                                         1, 
5112                                         new_cursor, 
5113                                         update_cursor))
5114                                 {
5115                                         break;
5116                                 }
5117                                 else
5118                                 if(do_edits(cursor_x, 
5119                                         cursor_y, 
5120                                         1, 
5121                                         0, 
5122                                         update_cursor, 
5123                                         rerender, 
5124                                         new_cursor, 
5125                                         update_cursor))
5126                                 {
5127                                         break;
5128                                 }
5129                                 else
5130                                 if(do_plugins(cursor_x, 
5131                                         cursor_y, 
5132                                         0, 
5133                                         1, 
5134                                         update_cursor, 
5135                                         rerender))
5136                                 {
5137                                         break;
5138                                 }
5139                                 else
5140                                 if(do_tracks(cursor_x, cursor_y, 1))
5141                                 {
5142                                         break;
5143                                 }
5144 // Highlight selection
5145                                 else
5146                                 {
5147 SET_TRACE
5148                                         rerender = start_selection(position);
5149 SET_TRACE
5150                                         mwindow->session->current_operation = SELECT_REGION;
5151                                         update_cursor = 1;
5152                                 }
5154                                 break;
5155                         }
5156                 }
5159 SET_TRACE
5160                 if(rerender)
5161                 {
5162                         gui->unlock_window();
5163                         mwindow->cwindow->update(1, 0, 0, 0, 1);
5165                         gui->lock_window("TrackCanvas::button_press_event 2");
5166 // Update faders
5167                         mwindow->update_plugin_guis();
5168                         gui->patchbay->update();
5169                 }
5171 SET_TRACE
5172                 if(update_overlay)
5173                 {
5174                         draw_overlays();
5175                         flash();
5176                 }
5178 SET_TRACE
5179                 if(update_cursor)
5180                 {
5181                         gui->timebar->update_highlights();
5182                         gui->cursor->hide();
5183                         gui->cursor->show();
5184                         gui->zoombar->update();
5185                         flash();
5186                         result = 1;
5187                 }
5189 SET_TRACE
5192         }
5193         return result;
5196 int TrackCanvas::start_selection(double position)
5198         int rerender = 0;
5199         position = mwindow->edl->align_to_frame(position, 0);
5202 // Extend a border
5203         if(shift_down())
5204         {
5205                 double midpoint = (mwindow->edl->local_session->get_selectionstart(1) + 
5206                         mwindow->edl->local_session->get_selectionend(1)) / 2;
5208                 if(position < midpoint)
5209                 {
5210                         mwindow->edl->local_session->set_selectionstart(position);
5211                         selection_midpoint1 = mwindow->edl->local_session->get_selectionend(1);
5212 // Que the CWindow
5213                         rerender = 1;
5214                 }
5215                 else
5216                 {
5217                         mwindow->edl->local_session->set_selectionend(position);
5218                         selection_midpoint1 = mwindow->edl->local_session->get_selectionstart(1);
5219 // Don't que the CWindow for the end
5220                 }
5221         }
5222         else
5223 // Start a new selection
5224         {
5225 //printf("TrackCanvas::start_selection %f\n", position);
5226                 mwindow->edl->local_session->set_selectionstart(position);
5227                 mwindow->edl->local_session->set_selectionend(position);
5228                 selection_midpoint1 = position;
5229 // Que the CWindow
5230                 rerender = 1;
5231         }
5232         
5233         return rerender;
5236 void TrackCanvas::end_edithandle_selection()
5238         mwindow->modify_edithandles();
5241 void TrackCanvas::end_pluginhandle_selection()
5243         mwindow->modify_pluginhandles();
5247 double TrackCanvas::time_visible()
5249         return (double)get_w() * 
5250                 mwindow->edl->local_session->zoom_sample / 
5251                 mwindow->edl->session->sample_rate;
5294 void TrackCanvas::draw_automation()
5299 int TrackCanvas::set_index_file(int flash, Asset *asset)
5301         return 0;
5305 int TrackCanvas::button_release()
5307         return 0;
5311 int TrackCanvas::auto_reposition(int &cursor_x, int &cursor_y, int64_t cursor_position)
5313         return 0;
5317 int TrackCanvas::draw_floating_handle(int flash)
5319         return 0;
5322 int TrackCanvas::draw_loop_point(int64_t position, int flash)
5324         return 0;
5327 int TrackCanvas::draw_playback_cursor(int pixel, int flash)
5329         return 0;
5333 int TrackCanvas::update_handle_selection(int64_t cursor_position)
5335         return 0;
5338 int TrackCanvas::end_translation()
5340         return 0;