shuttlerc, boobytraps, vwindow hang, lock cleanup
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / cwindowgui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "automation.h"
23 #include "autos.h"
24 #include "bcsignals.h"
25 #include "canvas.h"
26 #include "clip.h"
27 #include "cpanel.h"
28 #include "cplayback.h"
29 #include "ctimebar.h"
30 #include "cursors.h"
31 #include "cwindowgui.h"
32 #include "cwindow.h"
33 #include "cwindowtool.h"
34 #include "editpanel.h"
35 #include "edl.h"
36 #include "edlsession.h"
37 #include "floatauto.h"
38 #include "floatautos.h"
39 #include "keys.h"
40 #include "language.h"
41 #include "localsession.h"
42 #include "mainclock.h"
43 #include "mainmenu.h"
44 #include "mainundo.h"
45 #include "mainsession.h"
46 #include "maskauto.h"
47 #include "maskautos.h"
48 #include "mbuttons.h"
49 #include "meterpanel.h"
50 #include "mwindowgui.h"
51 #include "mwindow.h"
52 #include "mwindow.h"
53 #include "playback3d.h"
54 #include "playtransport.h"
55 #include "theme.h"
56 #include "trackcanvas.h"
57 #include "tracks.h"
58 #include "transportque.h"
59 #include "vtrack.h"
60
61
62 static double my_zoom_table[] =
63 {
64         0.25,
65         0.33,
66         0.50,
67         0.75,
68         1.0,
69         1.5,
70         2.0,
71         3.0,
72         4.0
73 };
74
75 static int total_zooms = sizeof(my_zoom_table) / sizeof(double);
76
77
78 CWindowGUI::CWindowGUI(MWindow *mwindow, CWindow *cwindow)
79  : BC_Window(_(PROGRAM_NAME ": Compositor"),
80         mwindow->session->cwindow_x,
81     mwindow->session->cwindow_y,
82     mwindow->session->cwindow_w,
83     mwindow->session->cwindow_h,
84     100,
85     100,
86     1,
87     1,
88     1,
89         BC_WindowBase::get_resources()->bg_color,
90         mwindow->get_cwindow_display())
91 {
92         this->mwindow = mwindow;
93         this->cwindow = cwindow;
94         affected_track = 0;
95         affected_x = affected_y = affected_z = 0;
96         mask_keyframe = 0;
97         orig_mask_keyframe = new MaskAuto(0, 0);
98         affected_point = 0;
99         x_offset = y_offset = 0;
100         x_origin = y_origin = 0;
101         current_operation = CWINDOW_NONE;
102         tool_panel = 0;
103         active = 0;
104         inactive = 0;
105         crop_handle = -1; crop_translate = 0;
106         crop_origin_x = crop_origin_y = 0;
107         crop_origin_x1 = crop_origin_y1 = 0;
108         crop_origin_x2 = crop_origin_y2 = 0;
109         eyedrop_visible = 0;
110         eyedrop_x = eyedrop_y = 0;
111         ruler_origin_x = ruler_origin_y = 0;
112         ruler_handle = -1; ruler_translate = 0;
113         center_x = center_y = center_z = 0;
114         control_in_x = control_in_y = 0;
115         control_out_x = control_out_y = 0;
116         translating_zoom = 0;
117         highlighted = 0;
118 }
119
120 CWindowGUI::~CWindowGUI()
121 {
122         if(tool_panel) delete tool_panel;
123         delete meters;
124         delete composite_panel;
125         delete canvas;
126         delete transport;
127         delete edit_panel;
128         delete zoom_panel;
129         delete active;
130         delete inactive;
131         delete orig_mask_keyframe;
132 }
133
134 void CWindowGUI::create_objects()
135 {
136         lock_window("CWindowGUI::create_objects");
137         set_icon(mwindow->theme->get_image("cwindow_icon"));
138
139         active = new BC_Pixmap(this, mwindow->theme->get_image("cwindow_active"));
140         inactive = new BC_Pixmap(this, mwindow->theme->get_image("cwindow_inactive"));
141
142         mwindow->theme->get_cwindow_sizes(this, mwindow->session->cwindow_controls);
143         mwindow->theme->draw_cwindow_bg(this);
144         flash();
145
146 // Meters required by composite panel
147         meters = new CWindowMeters(mwindow,
148                 this,
149                 mwindow->theme->cmeter_x,
150                 mwindow->theme->cmeter_y,
151                 mwindow->theme->cmeter_h);
152         meters->create_objects();
153
154
155         composite_panel = new CPanel(mwindow,
156                 this,
157                 mwindow->theme->ccomposite_x,
158                 mwindow->theme->ccomposite_y,
159                 mwindow->theme->ccomposite_w,
160                 mwindow->theme->ccomposite_h);
161         composite_panel->create_objects();
162
163         canvas = new CWindowCanvas(mwindow, this);
164
165         canvas->create_objects(mwindow->edl);
166         canvas->use_cwindow();
167
168
169         add_subwindow(timebar = new CTimeBar(mwindow,
170                 this,
171                 mwindow->theme->ctimebar_x,
172                 mwindow->theme->ctimebar_y,
173                 mwindow->theme->ctimebar_w,
174                 mwindow->theme->ctimebar_h));
175         timebar->create_objects();
176
177 #ifdef USE_SLIDER
178         add_subwindow(slider = new CWindowSlider(mwindow,
179                 cwindow,
180                 mwindow->theme->cslider_x,
181                 mwindow->theme->cslider_y,
182                 mwindow->theme->cslider_w));
183 #endif
184
185         transport = new CWindowTransport(mwindow,
186                 this,
187                 mwindow->theme->ctransport_x,
188                 mwindow->theme->ctransport_y);
189         transport->create_objects();
190 #ifdef USE_SLIDER
191         transport->set_slider(slider);
192 #endif
193
194         edit_panel = new CWindowEditing(mwindow, cwindow);
195         edit_panel->set_meters(meters);
196         edit_panel->create_objects();
197
198 //      add_subwindow(clock = new MainClock(mwindow,
199 //              mwindow->theme->ctime_x,
200 //              mwindow->theme->ctime_y));
201
202         zoom_panel = new CWindowZoom(mwindow,
203                 this,
204                 mwindow->theme->czoom_x,
205                 mwindow->theme->czoom_y,
206                 mwindow->theme->czoom_w);
207         zoom_panel->create_objects();
208         zoom_panel->zoom_text->add_item(new BC_MenuItem(auto_zoom = _(AUTO_ZOOM)));
209         if( !mwindow->edl->session->cwindow_scrollbars )
210                 zoom_panel->set_text(auto_zoom);
211
212 //      destination = new CWindowDestination(mwindow,
213 //              this,
214 //              mwindow->theme->cdest_x,
215 //              mwindow->theme->cdest_y);
216 //      destination->create_objects();
217
218 // Must create after meter panel
219         tool_panel = new CWindowTool(mwindow, this);
220         tool_panel->Thread::start();
221
222         set_operation(mwindow->edl->session->cwindow_operation);
223         draw_status(0);
224         unlock_window();
225 }
226
227 int CWindowGUI::translation_event()
228 {
229         mwindow->session->cwindow_x = get_x();
230         mwindow->session->cwindow_y = get_y();
231         return 0;
232 }
233
234 int CWindowGUI::resize_event(int w, int h)
235 {
236         mwindow->session->cwindow_x = get_x();
237         mwindow->session->cwindow_y = get_y();
238         mwindow->session->cwindow_w = w;
239         mwindow->session->cwindow_h = h;
240
241         mwindow->theme->get_cwindow_sizes(this, mwindow->session->cwindow_controls);
242         mwindow->theme->draw_cwindow_bg(this);
243         flash(0);
244
245         composite_panel->reposition_buttons(mwindow->theme->ccomposite_x,
246                 mwindow->theme->ccomposite_y, mwindow->theme->ccomposite_h);
247
248         canvas->reposition_window(mwindow->edl,
249                 mwindow->theme->ccanvas_x,
250                 mwindow->theme->ccanvas_y,
251                 mwindow->theme->ccanvas_w,
252                 mwindow->theme->ccanvas_h);
253
254         timebar->resize_event();
255
256 #ifdef USE_SLIDER
257         slider->reposition_window(mwindow->theme->cslider_x,
258                 mwindow->theme->cslider_y,
259                 mwindow->theme->cslider_w);
260 // Recalibrate pointer motion range
261         slider->set_position();
262 #endif
263
264         transport->reposition_buttons(mwindow->theme->ctransport_x,
265                 mwindow->theme->ctransport_y);
266
267         edit_panel->reposition_buttons(mwindow->theme->cedit_x,
268                 mwindow->theme->cedit_y);
269
270 //      clock->reposition_window(mwindow->theme->ctime_x,
271 //              mwindow->theme->ctime_y);
272
273         zoom_panel->reposition_window(mwindow->theme->czoom_x,
274                 mwindow->theme->czoom_y);
275
276 //      destination->reposition_window(mwindow->theme->cdest_x,
277 //              mwindow->theme->cdest_y);
278
279         meters->reposition_window(mwindow->theme->cmeter_x,
280                 mwindow->theme->cmeter_y,
281                 -1,
282                 mwindow->theme->cmeter_h);
283
284         draw_status(0);
285
286         BC_WindowBase::resize_event(w, h);
287         return 1;
288 }
289
290 int CWindowGUI::button_press_event()
291 {
292         if( current_operation == CWINDOW_NONE &&
293             mwindow->edl != 0 && canvas->get_canvas() &&
294             mwindow->edl->session->cwindow_click2play &&
295             canvas->get_canvas()->get_cursor_over_window() ) {
296                 switch( get_buttonpress() ) {
297                 case LEFT_BUTTON:
298                         if( !cwindow->playback_engine->is_playing_back ) {
299                                 double length = mwindow->edl->tracks->total_playable_length();
300                                 double position = cwindow->playback_engine->get_tracking_position();
301                                 if( position >= length ) transport->goto_start();
302                         }
303                         return transport->forward_play->handle_event();
304                 case MIDDLE_BUTTON:
305                         if( !cwindow->playback_engine->is_playing_back ) {
306                                 double position = cwindow->playback_engine->get_tracking_position();
307                                 if( position <= 0 ) transport->goto_end();
308                         }
309                         return transport->reverse_play->handle_event();
310                 case RIGHT_BUTTON:  // activates popup
311                         break;
312                 case WHEEL_UP:
313                         return transport->frame_forward_play->handle_event();
314                 case WHEEL_DOWN:
315                         return transport->frame_reverse_play->handle_event();
316                 }
317         }
318         if(canvas->get_canvas())
319                 return canvas->button_press_event_base(canvas->get_canvas());
320         return 0;
321 }
322
323 int CWindowGUI::cursor_leave_event()
324 {
325         if(canvas->get_canvas())
326                 return canvas->cursor_leave_event_base(canvas->get_canvas());
327         return 0;
328 }
329
330 int CWindowGUI::cursor_enter_event()
331 {
332         if(canvas->get_canvas())
333                 return canvas->cursor_enter_event_base(canvas->get_canvas());
334         return 0;
335 }
336
337 int CWindowGUI::button_release_event()
338 {
339         if(canvas->get_canvas())
340                 return canvas->button_release_event();
341         return 0;
342 }
343
344 int CWindowGUI::cursor_motion_event()
345 {
346         if(canvas->get_canvas())
347         {
348                 canvas->get_canvas()->unhide_cursor();
349                 return canvas->cursor_motion_event();
350         }
351         return 0;
352 }
353
354
355
356
357
358
359
360 void CWindowGUI::draw_status(int flush)
361 {
362         if( (canvas->get_canvas() && canvas->get_canvas()->get_video_on()) ||
363                 canvas->is_processing )
364         {
365                 draw_pixmap(active,
366                         mwindow->theme->cstatus_x,
367                         mwindow->theme->cstatus_y);
368         }
369         else
370         {
371                 draw_pixmap(inactive,
372                         mwindow->theme->cstatus_x,
373                         mwindow->theme->cstatus_y);
374         }
375
376
377 // printf("CWindowGUI::draw_status %d %d %d\n", __LINE__, mwindow->theme->cstatus_x,
378 //              mwindow->theme->cstatus_y);
379         flash(mwindow->theme->cstatus_x,
380                 mwindow->theme->cstatus_y,
381                 active->get_w(),
382                 active->get_h(),
383                 flush);
384 }
385
386 float CWindowGUI::get_auto_zoom()
387 {
388         float conformed_w, conformed_h;
389         mwindow->edl->calculate_conformed_dimensions(0, conformed_w, conformed_h);
390         float zoom_x = canvas->w / conformed_w;
391         float zoom_y = canvas->h / conformed_h;
392         return zoom_x < zoom_y ? zoom_x : zoom_y;
393 }
394
395 void CWindowGUI::zoom_canvas(double value, int update_menu)
396 {
397         float x = 0, y = 0;
398         float zoom = !value ? get_auto_zoom() : value;
399         EDL *edl = mwindow->edl;
400         edl->session->cwindow_scrollbars = !value ? 0 : 1;
401         if( value ) {
402                 float cx = 0.5f * canvas->w;  x = cx;
403                 float cy = 0.5f * canvas->h;  y = cy;
404                 canvas->canvas_to_output(edl, 0, x, y);
405                 canvas->update_zoom(0, 0, zoom);
406                 float zoom_x, zoom_y, conformed_w, conformed_h;
407                 canvas->get_zooms(edl, 0, zoom_x, zoom_y, conformed_w, conformed_h);
408                 x -= cx / zoom_x;
409                 y -= cy / zoom_y;
410
411         }
412         canvas->update_zoom((int)(x+0.5), (int)(y+0.5), zoom);
413
414         if( update_menu )
415                 zoom_panel->update(value);
416         if( mwindow->edl->session->cwindow_operation == CWINDOW_ZOOM )
417                 composite_panel->cpanel_zoom->update(zoom);
418
419         canvas->reposition_window(mwindow->edl,
420                 mwindow->theme->ccanvas_x, mwindow->theme->ccanvas_y,
421                 mwindow->theme->ccanvas_w, mwindow->theme->ccanvas_h);
422         canvas->draw_refresh();
423 }
424
425 void CWindowGUI::set_operation(int value)
426 {
427         switch( value ) {
428         case CWINDOW_TOOL_WINDOW:
429                 mwindow->edl->session->tool_window = !mwindow->edl->session->tool_window;
430                 composite_panel->operation[CWINDOW_TOOL_WINDOW]->update(mwindow->edl->session->tool_window);
431                 tool_panel->update_show_window();
432                 return;
433         case CWINDOW_TITLESAFE:
434                 mwindow->edl->session->safe_regions = !mwindow->edl->session->safe_regions;
435                 composite_panel->operation[CWINDOW_TITLESAFE]->update(mwindow->edl->session->safe_regions);
436                 value = mwindow->edl->session->cwindow_operation;
437                 break;
438         default:
439                 mwindow->edl->session->cwindow_operation = value;
440                 composite_panel->set_operation(value);
441                 break;
442         }
443
444         edit_panel->update();
445         tool_panel->start_tool(value);
446         canvas->draw_refresh();
447 }
448
449 void CWindowGUI::update_tool()
450 {
451         tool_panel->update_values();
452 }
453
454 int CWindowGUI::close_event()
455 {
456         cwindow->hide_window();
457         return 1;
458 }
459
460
461 int CWindowGUI::keypress_event()
462 {
463         int result = 0;
464         int cwindow_operation = CWINDOW_NONE;
465
466         switch( get_keypress() ) {
467         case 'w':
468         case 'W':
469                 close_event();
470                 result = 1;
471                 break;
472         case '+':
473         case '=':
474                 keyboard_zoomin();
475                 result = 1;
476                 break;
477         case '-':
478                 keyboard_zoomout();
479                 result = 1;
480                 break;
481         case 'f':
482                 unlock_window();
483                 if(mwindow->session->cwindow_fullscreen)
484                         canvas->stop_fullscreen();
485                 else
486                         canvas->start_fullscreen();
487                 lock_window("CWindowGUI::keypress_event 1");
488                 result = 1;
489                 break;
490         case 'x':
491                 if( ctrl_down() || shift_down() || alt_down() ) break;
492                 unlock_window();
493                 mwindow->gui->lock_window("CWindowGUI::keypress_event 2");
494                 mwindow->cut();
495                 mwindow->gui->unlock_window();
496                 lock_window("CWindowGUI::keypress_event 2");
497                 result = 1;
498                 break;
499         case DELETE:
500                 unlock_window();
501                 mwindow->gui->lock_window("CWindowGUI::keypress_event 2");
502                 mwindow->clear_entry();
503                 mwindow->gui->unlock_window();
504                 lock_window("CWindowGUI::keypress_event 3");
505                 result = 1;
506                 break;
507         case ESC:
508                 unlock_window();
509                 if(mwindow->session->cwindow_fullscreen)
510                         canvas->stop_fullscreen();
511                 lock_window("CWindowGUI::keypress_event 4");
512                 result = 1;
513                 break;
514         case LEFT:
515                 if( !ctrl_down() ) {
516                         int alt_down = this->alt_down();
517                         int shift_down = this->shift_down();
518                         unlock_window();
519                         stop_transport(0);
520                         mwindow->gui->lock_window("CWindowGUI::keypress_event 5");
521                         if( alt_down )
522                                 mwindow->prev_edit_handle(shift_down);
523                         else
524                                 mwindow->move_left();
525                         mwindow->gui->unlock_window();
526                         lock_window("CWindowGUI::keypress_event 6");
527                         result = 1;
528                 }
529                 break;
530
531         case ',':
532                 if( !ctrl_down() && !alt_down() ) {
533                         unlock_window();
534                         stop_transport(0);
535                         mwindow->gui->lock_window("CWindowGUI::keypress_event 7");
536                         mwindow->move_left();
537                         mwindow->gui->unlock_window();
538                         lock_window("CWindowGUI::keypress_event 8");
539                         result = 1;
540                 }
541                 break;
542
543         case RIGHT:
544                 if( !ctrl_down() ) {
545                         int alt_down = this->alt_down();
546                         int shift_down = this->shift_down();
547                         unlock_window();
548                         stop_transport(0);
549                         mwindow->gui->lock_window("CWindowGUI::keypress_event 8");
550                         if( alt_down )
551                                 mwindow->next_edit_handle(shift_down);
552                         else
553                                 mwindow->move_right();
554                         mwindow->gui->unlock_window();
555                         lock_window("CWindowGUI::keypress_event 9");
556                         result = 1;
557                 }
558                 break;
559
560         case '.':
561                 if( !ctrl_down() && !alt_down() ) {
562                         unlock_window();
563                         stop_transport(0);
564                         mwindow->gui->lock_window("CWindowGUI::keypress_event 10");
565                         mwindow->move_right();
566                         mwindow->gui->unlock_window();
567                         lock_window("CWindowGUI::keypress_event 11");
568                         result = 1;
569                 }
570                 break;
571         }
572
573         if( !result && cwindow_operation < 0 && ctrl_down() && shift_down() ) {
574                 switch( get_keypress() ) {
575                 case KEY_F1:
576                 case KEY_F2:
577                 case KEY_F3:
578                 case KEY_F4:
579                         resend_event(mwindow->gui);
580                         result = 1;
581                         break;
582                 }
583         }
584         if( !result && !ctrl_down() ) {
585                 switch( get_keypress() ) {
586                 case KEY_F1:
587                         if( shift_down() ) {
588                                 mwindow->toggle_camera_xyz();  result = 1;
589                         }
590                         else
591                                 cwindow_operation = CWINDOW_PROTECT;
592                         break;
593                 case KEY_F2:
594                         if( shift_down() ) {
595                                 mwindow->toggle_projector_xyz();  result = 1;
596                         }
597                         else
598                                 cwindow_operation = CWINDOW_ZOOM;
599                         break;
600                 }
601         }
602         if( !result && cwindow_operation < 0 && !ctrl_down() && !shift_down() ) {
603                 switch( get_keypress() ) {
604                 case KEY_F3:    cwindow_operation = CWINDOW_MASK;       break;
605                 case KEY_F4:    cwindow_operation = CWINDOW_RULER;      break;
606                 case KEY_F5:    cwindow_operation = CWINDOW_CAMERA;     break;
607                 case KEY_F6:    cwindow_operation = CWINDOW_PROJECTOR;  break;
608                 case KEY_F7:    cwindow_operation = CWINDOW_CROP;       break;
609                 case KEY_F8:    cwindow_operation = CWINDOW_EYEDROP;    break;
610                 case KEY_F9:    cwindow_operation = CWINDOW_TOOL_WINDOW; break;
611                 case KEY_F10:   cwindow_operation = CWINDOW_TITLESAFE;  break;
612                 }
613         }
614         if( !result && cwindow_operation < 0 && !ctrl_down() ) {
615                 switch( get_keypress() ) {
616                 case KEY_F11:
617                         if( !shift_down() )
618                                 canvas->reset_camera();
619                         else
620                                 canvas->camera_keyframe();
621                         result = 1;
622                         break;
623                 case KEY_F12:
624                         if( !shift_down() )
625                                 canvas->reset_projector();
626                         else
627                                 canvas->projector_keyframe();
628                         result = 1;
629                         break;
630                 }
631         }
632
633         if( cwindow_operation >= 0 ) {
634                 set_operation(cwindow_operation);
635                 result = 1;
636         }
637
638         if( !result )
639                 result = transport->keypress_event();
640
641         return result;
642 }
643
644
645 void CWindowGUI::reset_affected()
646 {
647         affected_x = 0;
648         affected_y = 0;
649         affected_z = 0;
650 }
651
652 void CWindowGUI::keyboard_zoomin()
653 {
654 //      if(mwindow->edl->session->cwindow_scrollbars)
655 //      {
656                 zoom_panel->zoom_tumbler->handle_up_event();
657 //      }
658 //      else
659 //      {
660 //      }
661 }
662
663 void CWindowGUI::keyboard_zoomout()
664 {
665 //      if(mwindow->edl->session->cwindow_scrollbars)
666 //      {
667                 zoom_panel->zoom_tumbler->handle_down_event();
668 //      }
669 //      else
670 //      {
671 //      }
672 }
673
674 void CWindowGUI::sync_parameters(int change_type, int tool, int overlay)
675 {
676         if( tool ) update_tool();
677         if( change_type < 0 && !overlay ) return;
678         unlock_window();
679         if( change_type >= 0 ) {
680                 mwindow->restart_brender();
681                 mwindow->sync_parameters(CHANGE_PARAMS);
682         }
683         if( overlay ) {
684                 mwindow->gui->lock_window("CWindow::camera_keyframe");
685                 mwindow->gui->draw_overlays(1);
686                 mwindow->gui->unlock_window();
687         }
688         lock_window("CWindowGUI::sync_parameters");
689 }
690
691 void CWindowGUI::drag_motion()
692 {
693         if(get_hidden()) return;
694
695         if(mwindow->session->current_operation != DRAG_ASSET &&
696                 mwindow->session->current_operation != DRAG_VTRANSITION &&
697                 mwindow->session->current_operation != DRAG_VEFFECT) return;
698         int need_highlight = cursor_above() && get_cursor_over_window();
699         if( highlighted == need_highlight ) return;
700         highlighted = need_highlight;
701         canvas->draw_refresh();
702 }
703
704 int CWindowGUI::drag_stop()
705 {
706         int result = 0;
707         if(get_hidden()) return 0;
708         if( !highlighted ) return 0;
709         if( mwindow->session->current_operation != DRAG_ASSET &&
710             mwindow->session->current_operation != DRAG_VTRANSITION &&
711             mwindow->session->current_operation != DRAG_VEFFECT) return 0;
712         highlighted = 0;
713         canvas->draw_refresh();
714         result = 1;
715
716         if(mwindow->session->current_operation == DRAG_ASSET)
717         {
718                 if(mwindow->session->drag_assets->total ||
719                         mwindow->session->drag_clips->total)
720                 {
721                         mwindow->gui->lock_window("CWindowGUI::drag_stop 1");
722                         mwindow->undo->update_undo_before(_("insert assets"), 0);
723                 }
724
725                 if(mwindow->session->drag_assets->total)
726                 {
727                         mwindow->clear(0);
728                         mwindow->load_assets(mwindow->session->drag_assets,
729                                 mwindow->edl->local_session->get_selectionstart(),
730                                 LOADMODE_PASTE,
731                                 mwindow->session->track_highlighted,
732                                 0,
733                                 mwindow->edl->session->labels_follow_edits,
734                                 mwindow->edl->session->plugins_follow_edits,
735                                 mwindow->edl->session->autos_follow_edits,
736                                 0); // overwrite
737                 }
738
739                 if(mwindow->session->drag_clips->total)
740                 {
741                         mwindow->clear(0);
742                         mwindow->paste_edls(mwindow->session->drag_clips,
743                                 LOADMODE_PASTE,
744                                 mwindow->session->track_highlighted,
745                                 mwindow->edl->local_session->get_selectionstart(),
746                                 mwindow->edl->session->labels_follow_edits,
747                                 mwindow->edl->session->plugins_follow_edits,
748                                 mwindow->edl->session->autos_follow_edits,
749                                 0); // overwrite
750                 }
751
752                 if(mwindow->session->drag_assets->total ||
753                         mwindow->session->drag_clips->total)
754                 {
755                         mwindow->save_backup();
756                         mwindow->restart_brender();
757                         mwindow->gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
758                         mwindow->undo->update_undo_after(_("insert assets"), LOAD_ALL);
759                         mwindow->gui->unlock_window();
760                         sync_parameters(CHANGE_ALL);
761                 }
762         }
763
764         if(mwindow->session->current_operation == DRAG_VEFFECT)
765         {
766 //printf("CWindowGUI::drag_stop 1\n");
767                 Track *affected_track = cwindow->calculate_affected_track();
768 //printf("CWindowGUI::drag_stop 2\n");
769
770                 mwindow->gui->lock_window("CWindowGUI::drag_stop 3");
771                 mwindow->insert_effects_cwindow(affected_track);
772                 mwindow->session->current_operation = NO_OPERATION;
773                 mwindow->gui->unlock_window();
774         }
775
776         if(mwindow->session->current_operation == DRAG_VTRANSITION)
777         {
778                 Track *affected_track = cwindow->calculate_affected_track();
779                 mwindow->gui->lock_window("CWindowGUI::drag_stop 4");
780                 mwindow->paste_transition_cwindow(affected_track);
781                 mwindow->session->current_operation = NO_OPERATION;
782                 mwindow->gui->unlock_window();
783         }
784
785         return result;
786 }
787
788 void CWindowGUI::update_meters()
789 {
790         if(mwindow->edl->session->cwindow_meter != meters->visible)
791         {
792                 meters->set_meters(meters->meter_count, mwindow->edl->session->cwindow_meter);
793                 mwindow->theme->get_cwindow_sizes(this, mwindow->session->cwindow_controls);
794                 resize_event(get_w(), get_h());
795         }
796 }
797
798 void CWindowGUI::stop_transport(const char *lock_msg)
799 {
800         if( lock_msg ) unlock_window();
801         mwindow->stop_transport();
802         if( lock_msg ) lock_window(lock_msg);
803 }
804
805
806 CWindowEditing::CWindowEditing(MWindow *mwindow, CWindow *cwindow)
807  : EditPanel(mwindow, cwindow->gui, CWINDOW_ID,
808                 mwindow->theme->cedit_x, mwindow->theme->cedit_y,
809                 mwindow->edl->session->editing_mode,
810                 0, // use_editing_mode
811                 1, // use_keyframe
812                 0, // use_splice
813                 0, // use_overwrite
814                 1, // use_copy
815                 1, // use_paste
816                 1, // use_undo
817                 0, // use_fit
818                 0, // locklabels
819                 1, // use_labels
820                 1, // use_toclip
821                 1, // use_meters
822                 1, // use_cut
823                 0, // use_commerical
824                 0, // use_goto
825                 1) // use_clk2play
826 {
827         this->mwindow = mwindow;
828         this->cwindow = cwindow;
829 }
830
831 #define relock_cm(s) \
832  cwindow->gui->unlock_window(); \
833  mwindow->gui->lock_window("CWindowEditing::" s)
834 #define relock_mc(s) \
835  mwindow->gui->unlock_window(); \
836  cwindow->gui->lock_window("CWindowEditing::" s)
837 #define panel_fn(fn, args, s) \
838  CWindowEditing::fn args { relock_cm(#fn); s; relock_mc(#fn); }
839
840 // transmit lock to mwindow, and run mbutton->edit_panel->s
841 #define panel_btn(fn, args, s) \
842  panel_fn(panel_##fn, args, mwindow->gui->mbuttons->edit_panel->panel_##s)
843
844
845 double CWindowEditing::get_position()
846 {
847         relock_cm("get_position");
848         double ret = mwindow->edl->local_session->get_selectionstart(1);
849         relock_mc("get_position");
850         return ret;
851 }
852
853 void CWindowEditing::set_position(double position)
854 {
855         relock_cm("set_position");
856         mwindow->gui->mbuttons->edit_panel->set_position(position);
857         relock_mc("set_position");
858 }
859
860 void CWindowEditing::set_click_to_play(int v)
861 {
862         relock_cm("set_position");
863         mwindow->edl->session->cwindow_click2play = v;
864         relock_mc("set_position");
865         click2play->update(v);
866 }
867
868
869 void panel_btn(stop_transport,(), stop_transport())
870 void panel_btn(toggle_label,(), toggle_label())
871 void panel_btn(next_label,(int cut), next_label(cut))
872 void panel_btn(prev_label,(int cut), prev_label(cut))
873 void panel_btn(next_edit,(int cut), next_edit(cut))
874 void panel_btn(prev_edit,(int cut), prev_edit(cut))
875 void panel_fn(panel_copy_selection,(), mwindow->copy())
876 void CWindowEditing::panel_overwrite_selection() {} // not used
877 void CWindowEditing::panel_splice_selection() {} // not used
878 void panel_btn(set_inpoint,(), set_inpoint())
879 void panel_btn(set_outpoint,(), set_outpoint())
880 void panel_btn(unset_inoutpoint,(), unset_inoutpoint())
881 void panel_fn(panel_to_clip,(), mwindow->to_clip(mwindow->edl, _("main window: "), 0))
882 void panel_btn(cut,(), cut())
883 void panel_btn(paste,(), paste())
884 void panel_btn(fit_selection,(), fit_selection())
885 void panel_btn(fit_autos,(int all), fit_autos(all))
886 void panel_btn(set_editing_mode,(int mode), set_editing_mode(mode))
887 void panel_btn(set_auto_keyframes,(int v), set_auto_keyframes(v))
888 void panel_btn(set_labels_follow_edits,(int v), set_labels_follow_edits(v))
889
890
891 CWindowMeters::CWindowMeters(MWindow *mwindow,
892         CWindowGUI *gui, int x, int y, int h)
893  : MeterPanel(mwindow, gui, x, y, -1, h,
894                 mwindow->edl->session->audio_channels,
895                 mwindow->edl->session->cwindow_meter,
896                 0, 0)
897 {
898         this->mwindow = mwindow;
899         this->gui = gui;
900 }
901
902 CWindowMeters::~CWindowMeters()
903 {
904 }
905
906 int CWindowMeters::change_status_event(int new_status)
907 {
908         mwindow->edl->session->cwindow_meter = new_status;
909         gui->update_meters();
910         return 1;
911 }
912
913
914
915
916 CWindowZoom::CWindowZoom(MWindow *mwindow, CWindowGUI *gui, int x, int y, int w)
917  : ZoomPanel(mwindow, gui, (double)mwindow->edl->session->cwindow_zoom,
918         x, y, w, my_zoom_table, total_zooms, ZOOM_PERCENTAGE)
919 {
920         this->mwindow = mwindow;
921         this->gui = gui;
922 }
923
924 CWindowZoom::~CWindowZoom()
925 {
926 }
927
928 void CWindowZoom::update(double value)
929 {
930         char string[BCSTRLEN];
931         const char *cp = string;
932         if( value ) {
933                 this->value = value;
934                 int frac = value >= 1.0f ? 1 :
935                            value >= 0.1f ? 2 :
936                            value >= .01f ? 3 : 4;
937                 sprintf(string, "x %.*f", frac, value);
938         }
939         else
940                 cp = gui->auto_zoom;
941         ZoomPanel::update(cp);
942 }
943
944 int CWindowZoom::handle_event()
945 {
946         double value = !strcasecmp(gui->auto_zoom, get_text()) ? 0 : get_value();
947         gui->zoom_canvas(value, 0);
948         return 1;
949 }
950
951
952
953 #ifdef USE_SLIDER
954 CWindowSlider::CWindowSlider(MWindow *mwindow, CWindow *cwindow, int x, int y, int pixels)
955  : BC_PercentageSlider(x,
956                         y,
957                         0,
958                         pixels,
959                         pixels,
960                         0,
961                         1,
962                         0)
963 {
964         this->mwindow = mwindow;
965         this->cwindow = cwindow;
966         set_precision(0.00001);
967 }
968
969 CWindowSlider::~CWindowSlider()
970 {
971 }
972
973 int CWindowSlider::handle_event()
974 {
975         cwindow->update_position((double)get_value());
976         return 1;
977 }
978
979 void CWindowSlider::set_position()
980 {
981         double new_length = mwindow->edl->tracks->total_length();
982         update(mwindow->theme->cslider_w,
983                 mwindow->edl->local_session->get_selectionstart(1),
984                 0, new_length);
985 }
986
987
988 int CWindowSlider::increase_value()
989 {
990         unlock_window();
991         cwindow->gui->transport->handle_transport(SINGLE_FRAME_FWD);
992         lock_window("CWindowSlider::increase_value");
993         return 1;
994 }
995
996 int CWindowSlider::decrease_value()
997 {
998         unlock_window();
999         cwindow->gui->transport->handle_transport(SINGLE_FRAME_REWIND);
1000         lock_window("CWindowSlider::decrease_value");
1001         return 1;
1002 }
1003
1004
1005 // CWindowDestination::CWindowDestination(MWindow *mwindow, CWindowGUI *cwindow, int x, int y)
1006 //  : BC_PopupTextBox(cwindow,
1007 //      &cwindow->destinations,
1008 //      cwindow->destinations.values[cwindow->cwindow->destination]->get_text(),
1009 //      x,
1010 //      y,
1011 //      70,
1012 //      200)
1013 // {
1014 //      this->mwindow = mwindow;
1015 //      this->cwindow = cwindow;
1016 // }
1017 //
1018 // CWindowDestination::~CWindowDestination()
1019 // {
1020 // }
1021 //
1022 // int CWindowDestination::handle_event()
1023 // {
1024 //      return 1;
1025 // }
1026 #endif // USE_SLIDER
1027
1028
1029
1030
1031
1032
1033 CWindowTransport::CWindowTransport(MWindow *mwindow,
1034         CWindowGUI *gui,
1035         int x,
1036         int y)
1037  : PlayTransport(mwindow,
1038         gui,
1039         x,
1040         y)
1041 {
1042         this->gui = gui;
1043 }
1044
1045 EDL* CWindowTransport::get_edl()
1046 {
1047         return mwindow->edl;
1048 }
1049
1050 void CWindowTransport::goto_start()
1051 {
1052         gui->unlock_window();
1053         handle_transport(REWIND, 1);
1054
1055         mwindow->gui->lock_window("CWindowTransport::goto_start 1");
1056         mwindow->goto_start();
1057         mwindow->gui->unlock_window();
1058
1059         gui->lock_window("CWindowTransport::goto_start 2");
1060 }
1061
1062 void CWindowTransport::goto_end()
1063 {
1064         gui->unlock_window();
1065         handle_transport(GOTO_END, 1);
1066
1067         mwindow->gui->lock_window("CWindowTransport::goto_end 1");
1068         mwindow->goto_end();
1069         mwindow->gui->unlock_window();
1070
1071         gui->lock_window("CWindowTransport::goto_end 2");
1072 }
1073
1074
1075
1076 CWindowCanvas::CWindowCanvas(MWindow *mwindow, CWindowGUI *gui)
1077  : Canvas(mwindow,
1078         gui,
1079         mwindow->theme->ccanvas_x,
1080         mwindow->theme->ccanvas_y,
1081         mwindow->theme->ccanvas_w,
1082         mwindow->theme->ccanvas_h,
1083         0,
1084         0,
1085         mwindow->edl->session->cwindow_scrollbars)
1086 {
1087         this->mwindow = mwindow;
1088         this->gui = gui;
1089 }
1090
1091 void CWindowCanvas::status_event()
1092 {
1093         gui->draw_status(1);
1094 }
1095
1096 int CWindowCanvas::get_fullscreen()
1097 {
1098         return mwindow->session->cwindow_fullscreen;
1099 }
1100
1101 void CWindowCanvas::set_fullscreen(int value)
1102 {
1103         mwindow->session->cwindow_fullscreen = value;
1104 }
1105
1106
1107 void CWindowCanvas::update_zoom(int x, int y, float zoom)
1108 {
1109         use_scrollbars = mwindow->edl->session->cwindow_scrollbars;
1110
1111         mwindow->edl->session->cwindow_xscroll = x;
1112         mwindow->edl->session->cwindow_yscroll = y;
1113         mwindow->edl->session->cwindow_zoom = zoom;
1114 }
1115
1116 void CWindowCanvas::zoom_auto()
1117 {
1118         gui->zoom_canvas(0, 1);
1119 }
1120
1121 int CWindowCanvas::get_xscroll()
1122 {
1123         return mwindow->edl->session->cwindow_xscroll;
1124 }
1125
1126 int CWindowCanvas::get_yscroll()
1127 {
1128         return mwindow->edl->session->cwindow_yscroll;
1129 }
1130
1131
1132 float CWindowCanvas::get_zoom()
1133 {
1134         return mwindow->edl->session->cwindow_zoom;
1135 }
1136
1137 void CWindowCanvas::draw_refresh(int flush)
1138 {
1139         if(get_canvas() && !get_canvas()->get_video_on())
1140         {
1141
1142                 if(refresh_frame && refresh_frame->get_w()>0 && refresh_frame->get_h()>0)
1143                 {
1144                         float in_x1, in_y1, in_x2, in_y2;
1145                         float out_x1, out_y1, out_x2, out_y2;
1146                         get_transfers(mwindow->edl,
1147                                 in_x1,
1148                                 in_y1,
1149                                 in_x2,
1150                                 in_y2,
1151                                 out_x1,
1152                                 out_y1,
1153                                 out_x2,
1154                                 out_y2);
1155
1156                         if(!EQUIV(out_x1, 0) ||
1157                                 !EQUIV(out_y1, 0) ||
1158                                 !EQUIV(out_x2, get_canvas()->get_w()) ||
1159                                 !EQUIV(out_y2, get_canvas()->get_h()))
1160                         {
1161                                 get_canvas()->clear_box(0,
1162                                         0,
1163                                         get_canvas()->get_w(),
1164                                         get_canvas()->get_h());
1165                         }
1166
1167 //printf("CWindowCanvas::draw_refresh %.2f %.2f %.2f %.2f -> %.2f %.2f %.2f %.2f\n",
1168 //in_x1, in_y1, in_x2, in_y2, out_x1, out_y1, out_x2, out_y2);
1169
1170
1171                         if(out_x2 > out_x1 &&
1172                                 out_y2 > out_y1 &&
1173                                 in_x2 > in_x1 &&
1174                                 in_y2 > in_y1)
1175                         {
1176 // input scaled from session to refresh frame coordinates
1177                                 int ow = get_output_w(mwindow->edl);
1178                                 int oh = get_output_h(mwindow->edl);
1179                                 int rw = refresh_frame->get_w();
1180                                 int rh = refresh_frame->get_h();
1181                                 float xs = (float)rw / ow;
1182                                 float ys = (float)rh / oh;
1183                                 in_x1 *= xs;  in_x2 *= xs;
1184                                 in_y1 *= ys;  in_y2 *= ys;
1185 // Can't use OpenGL here because it is called asynchronously of the
1186 // playback operation.
1187                                 get_canvas()->draw_vframe(refresh_frame,
1188                                                 (int)out_x1,
1189                                                 (int)out_y1,
1190                                                 (int)(out_x2 - out_x1),
1191                                                 (int)(out_y2 - out_y1),
1192                                                 (int)in_x1,
1193                                                 (int)in_y1,
1194                                                 (int)(in_x2 - in_x1),
1195                                                 (int)(in_y2 - in_y1),
1196                                                 0);
1197                         }
1198                 }
1199                 else
1200                 {
1201                         get_canvas()->clear_box(0,
1202                                 0,
1203                                 get_canvas()->get_w(),
1204                                 get_canvas()->get_h());
1205                 }
1206
1207                 draw_overlays();
1208 // allow last opengl write to complete before redraw
1209 // tried sync_display, glFlush, glxMake*Current(0..)
1210 usleep(20000);
1211                 get_canvas()->flash(flush);
1212         }
1213 //printf("CWindowCanvas::draw_refresh 10\n");
1214 }
1215
1216 #define CROPHANDLE_W 10
1217 #define CROPHANDLE_H 10
1218
1219 void CWindowCanvas::draw_crophandle(int x, int y)
1220 {
1221         get_canvas()->draw_box(x, y, CROPHANDLE_W, CROPHANDLE_H);
1222 }
1223
1224
1225
1226
1227
1228
1229 #define CONTROL_W 10
1230 #define CONTROL_H 10
1231 #define FIRST_CONTROL_W 20
1232 #define FIRST_CONTROL_H 20
1233 #undef BC_INFINITY
1234 #define BC_INFINITY 65536
1235
1236 #define RULERHANDLE_W 16
1237 #define RULERHANDLE_H 16
1238
1239
1240
1241 int CWindowCanvas::do_ruler(int draw,
1242         int motion,
1243         int button_press,
1244         int button_release)
1245 {
1246         int result = 0;
1247         float x1 = mwindow->edl->session->ruler_x1;
1248         float y1 = mwindow->edl->session->ruler_y1;
1249         float x2 = mwindow->edl->session->ruler_x2;
1250         float y2 = mwindow->edl->session->ruler_y2;
1251         float canvas_x1 = x1;
1252         float canvas_y1 = y1;
1253         float canvas_x2 = x2;
1254         float canvas_y2 = y2;
1255         float output_x = get_cursor_x();
1256         float output_y = get_cursor_y();
1257         float canvas_cursor_x = output_x;
1258         float canvas_cursor_y = output_y;
1259
1260         canvas_to_output(mwindow->edl, 0, output_x, output_y);
1261         output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
1262         output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
1263         mwindow->session->cwindow_output_x = roundf(output_x);
1264         mwindow->session->cwindow_output_y = roundf(output_y);
1265
1266         if(button_press && get_buttonpress() == 1)
1267         {
1268                 gui->ruler_handle = -1;
1269                 gui->ruler_translate = 0;
1270                 if(gui->alt_down())
1271                 {
1272                         gui->ruler_translate = 1;
1273                         gui->ruler_origin_x = x1;
1274                         gui->ruler_origin_y = y1;
1275                 }
1276                 else
1277                 if(canvas_cursor_x >= canvas_x1 - RULERHANDLE_W / 2 &&
1278                         canvas_cursor_x < canvas_x1 + RULERHANDLE_W / 2 &&
1279                         canvas_cursor_y >= canvas_y1 - RULERHANDLE_W &&
1280                         canvas_cursor_y < canvas_y1 + RULERHANDLE_H / 2)
1281                 {
1282                         gui->ruler_handle = 0;
1283                         gui->ruler_origin_x = x1;
1284                         gui->ruler_origin_y = y1;
1285                 }
1286                 else
1287                 if(canvas_cursor_x >= canvas_x2 - RULERHANDLE_W / 2 &&
1288                         canvas_cursor_x < canvas_x2 + RULERHANDLE_W / 2 &&
1289                         canvas_cursor_y >= canvas_y2 - RULERHANDLE_W &&
1290                         canvas_cursor_y < canvas_y2 + RULERHANDLE_H / 2)
1291                 {
1292                         gui->ruler_handle = 1;
1293                         gui->ruler_origin_x = x2;
1294                         gui->ruler_origin_y = y2;
1295                 }
1296
1297
1298 // Start new selection
1299                 if(!gui->ruler_translate &&
1300                         (gui->ruler_handle < 0 ||
1301                         (EQUIV(x2, x1) &&
1302                         EQUIV(y2, y1))))
1303                 {
1304 // Hide previous
1305                         do_ruler(1, 0, 0, 0);
1306                         get_canvas()->flash();
1307                         gui->ruler_handle = 1;
1308                         mwindow->edl->session->ruler_x1 = output_x;
1309                         mwindow->edl->session->ruler_y1 = output_y;
1310                         mwindow->edl->session->ruler_x2 = output_x;
1311                         mwindow->edl->session->ruler_y2 = output_y;
1312                         gui->ruler_origin_x = mwindow->edl->session->ruler_x2;
1313                         gui->ruler_origin_y = mwindow->edl->session->ruler_y2;
1314                 }
1315
1316                 gui->x_origin = output_x;
1317                 gui->y_origin = output_y;
1318                 gui->current_operation = CWINDOW_RULER;
1319                 gui->tool_panel->raise_window();
1320                 result = 1;
1321         }
1322
1323         if(motion)
1324         {
1325                 if(gui->current_operation == CWINDOW_RULER)
1326                 {
1327                         if(gui->ruler_translate)
1328                         {
1329 // Hide ruler
1330                                 do_ruler(1, 0, 0, 0);
1331                                 float x_difference = mwindow->edl->session->ruler_x1;
1332                                 float y_difference = mwindow->edl->session->ruler_y1;
1333                                 mwindow->edl->session->ruler_x1 = output_x - gui->x_origin + gui->ruler_origin_x;
1334                                 mwindow->edl->session->ruler_y1 = output_y - gui->y_origin + gui->ruler_origin_y;
1335                                 x_difference -= mwindow->edl->session->ruler_x1;
1336                                 y_difference -= mwindow->edl->session->ruler_y1;
1337                                 mwindow->edl->session->ruler_x2 -= x_difference;
1338                                 mwindow->edl->session->ruler_y2 -= y_difference;
1339 // Show ruler
1340                                 do_ruler(1, 0, 0, 0);
1341                                 get_canvas()->flash();
1342                                 gui->update_tool();
1343                         }
1344                         else
1345                         switch(gui->ruler_handle)
1346                         {
1347                                 case 0:
1348                                         do_ruler(1, 0, 0, 0);
1349                                         mwindow->edl->session->ruler_x1 = output_x - gui->x_origin + gui->ruler_origin_x;
1350                                         mwindow->edl->session->ruler_y1 = output_y - gui->y_origin + gui->ruler_origin_y;
1351                                         if(gui->alt_down() || gui->ctrl_down())
1352                                         {
1353                                                 double angle_value = fabs(atan((mwindow->edl->session->ruler_y2 - mwindow->edl->session->ruler_y1) /
1354                                                         (mwindow->edl->session->ruler_x2 - mwindow->edl->session->ruler_x1)) *
1355                                                         360 /
1356                                                         2 /
1357                                                         M_PI);
1358                                                 double distance_value =
1359                                                         sqrt(SQR(mwindow->edl->session->ruler_x2 - mwindow->edl->session->ruler_x1) +
1360                                                         SQR(mwindow->edl->session->ruler_y2 - mwindow->edl->session->ruler_y1));
1361                                                 if(angle_value < 22)
1362                                                         mwindow->edl->session->ruler_y1 = mwindow->edl->session->ruler_y2;
1363                                                 else
1364                                                 if(angle_value > 67)
1365                                                         mwindow->edl->session->ruler_x1 = mwindow->edl->session->ruler_x2;
1366                                                 else
1367                                                 if(mwindow->edl->session->ruler_x1 < mwindow->edl->session->ruler_x2 &&
1368                                                         mwindow->edl->session->ruler_y1 < mwindow->edl->session->ruler_y2)
1369                                                 {
1370                                                         mwindow->edl->session->ruler_x1 = mwindow->edl->session->ruler_x2 - distance_value / 1.414214;
1371                                                         mwindow->edl->session->ruler_y1 = mwindow->edl->session->ruler_y2 - distance_value / 1.414214;
1372                                                 }
1373                                                 else
1374                                                 if(mwindow->edl->session->ruler_x1 < mwindow->edl->session->ruler_x2 && mwindow->edl->session->ruler_y1 > mwindow->edl->session->ruler_y2)
1375                                                 {
1376                                                         mwindow->edl->session->ruler_x1 = mwindow->edl->session->ruler_x2 - distance_value / 1.414214;
1377                                                         mwindow->edl->session->ruler_y1 = mwindow->edl->session->ruler_y2 + distance_value / 1.414214;
1378                                                 }
1379                                                 else
1380                                                 if(mwindow->edl->session->ruler_x1 > mwindow->edl->session->ruler_x2 &&
1381                                                         mwindow->edl->session->ruler_y1 < mwindow->edl->session->ruler_y2)
1382                                                 {
1383                                                         mwindow->edl->session->ruler_x1 = mwindow->edl->session->ruler_x2 + distance_value / 1.414214;
1384                                                         mwindow->edl->session->ruler_y1 = mwindow->edl->session->ruler_y2 - distance_value / 1.414214;
1385                                                 }
1386                                                 else
1387                                                 {
1388                                                         mwindow->edl->session->ruler_x1 = mwindow->edl->session->ruler_x2 + distance_value / 1.414214;
1389                                                         mwindow->edl->session->ruler_y1 = mwindow->edl->session->ruler_y2 + distance_value / 1.414214;
1390                                                 }
1391                                         }
1392                                         do_ruler(1, 0, 0, 0);
1393                                         get_canvas()->flash();
1394                                         gui->update_tool();
1395                                         break;
1396
1397                                 case 1:
1398                                         do_ruler(1, 0, 0, 0);
1399                                         mwindow->edl->session->ruler_x2 = output_x - gui->x_origin + gui->ruler_origin_x;
1400                                         mwindow->edl->session->ruler_y2 = output_y - gui->y_origin + gui->ruler_origin_y;
1401                                         if(gui->alt_down() || gui->ctrl_down())
1402                                         {
1403                                                 double angle_value = fabs(atan((mwindow->edl->session->ruler_y2 - mwindow->edl->session->ruler_y1) /
1404                                                         (mwindow->edl->session->ruler_x2 - mwindow->edl->session->ruler_x1)) *
1405                                                         360 /
1406                                                         2 /
1407                                                         M_PI);
1408                                                 double distance_value =
1409                                                         sqrt(SQR(mwindow->edl->session->ruler_x2 - mwindow->edl->session->ruler_x1) +
1410                                                         SQR(mwindow->edl->session->ruler_y2 - mwindow->edl->session->ruler_y1));
1411                                                 if(angle_value < 22)
1412                                                         mwindow->edl->session->ruler_y2 = mwindow->edl->session->ruler_y1;
1413                                                 else
1414                                                 if(angle_value > 67)
1415                                                         mwindow->edl->session->ruler_x2 = mwindow->edl->session->ruler_x1;
1416                                                 else
1417                                                 if(mwindow->edl->session->ruler_x2 < mwindow->edl->session->ruler_x1 &&
1418                                                         mwindow->edl->session->ruler_y2 < mwindow->edl->session->ruler_y1)
1419                                                 {
1420                                                         mwindow->edl->session->ruler_x2 = mwindow->edl->session->ruler_x1 - distance_value / 1.414214;
1421                                                         mwindow->edl->session->ruler_y2 = mwindow->edl->session->ruler_y1 - distance_value / 1.414214;
1422                                                 }
1423                                                 else
1424                                                 if(mwindow->edl->session->ruler_x2 < mwindow->edl->session->ruler_x1 &&
1425                                                         mwindow->edl->session->ruler_y2 > mwindow->edl->session->ruler_y1)
1426                                                 {
1427                                                         mwindow->edl->session->ruler_x2 = mwindow->edl->session->ruler_x1 - distance_value / 1.414214;
1428                                                         mwindow->edl->session->ruler_y2 = mwindow->edl->session->ruler_y1 + distance_value / 1.414214;
1429                                                 }
1430                                                 else
1431                                                 if(mwindow->edl->session->ruler_x2 > mwindow->edl->session->ruler_x1 && mwindow->edl->session->ruler_y2 < mwindow->edl->session->ruler_y1)
1432                                                 {
1433                                                         mwindow->edl->session->ruler_x2 = mwindow->edl->session->ruler_x1 + distance_value / 1.414214;
1434                                                         mwindow->edl->session->ruler_y2 = mwindow->edl->session->ruler_y1 - distance_value / 1.414214;
1435                                                 }
1436                                                 else
1437                                                 {
1438                                                         mwindow->edl->session->ruler_x2 = mwindow->edl->session->ruler_x1 + distance_value / 1.414214;
1439                                                         mwindow->edl->session->ruler_y2 = mwindow->edl->session->ruler_y1 + distance_value / 1.414214;
1440                                                 }
1441                                         }
1442                                         do_ruler(1, 0, 0, 0);
1443                                         get_canvas()->flash();
1444                                         gui->update_tool();
1445                                         break;
1446                         }
1447 //printf("CWindowCanvas::do_ruler 2 %f %f %f %f\n", gui->ruler_x1, gui->ruler_y1, gui->ruler_x2, gui->ruler_y2);
1448                 }
1449                 else
1450                 {
1451 // printf("CWindowCanvas::do_ruler 2 %f %f %f %f\n",
1452 // canvas_cursor_x,
1453 // canvas_cursor_y,
1454 // canvas_x1,
1455 // canvas_y1);
1456                         if(canvas_cursor_x >= canvas_x1 - RULERHANDLE_W / 2 &&
1457                                 canvas_cursor_x < canvas_x1 + RULERHANDLE_W / 2 &&
1458                                 canvas_cursor_y >= canvas_y1 - RULERHANDLE_W &&
1459                                 canvas_cursor_y < canvas_y1 + RULERHANDLE_H / 2)
1460                         {
1461                                 set_cursor(UPRIGHT_ARROW_CURSOR);
1462                         }
1463                         else
1464                         if(canvas_cursor_x >= canvas_x2 - RULERHANDLE_W / 2 &&
1465                                 canvas_cursor_x < canvas_x2 + RULERHANDLE_W / 2 &&
1466                                 canvas_cursor_y >= canvas_y2 - RULERHANDLE_W &&
1467                                 canvas_cursor_y < canvas_y2 + RULERHANDLE_H / 2)
1468                         {
1469                                 set_cursor(UPRIGHT_ARROW_CURSOR);
1470                         }
1471                         else
1472                                 set_cursor(CROSS_CURSOR);
1473
1474 // Update current position
1475                         gui->update_tool();
1476                 }
1477         }
1478
1479 // Assume no ruler measurement if 0 length
1480         if(draw && (!EQUIV(x2, x1) || !EQUIV(y2, y1)))
1481         {
1482                 get_canvas()->set_inverse();
1483                 get_canvas()->set_color(WHITE);
1484                 get_canvas()->draw_line((int)canvas_x1,
1485                         (int)canvas_y1,
1486                         (int)canvas_x2,
1487                         (int)canvas_y2);
1488                 get_canvas()->draw_line(roundf(canvas_x1) - RULERHANDLE_W / 2,
1489                         roundf(canvas_y1),
1490                         roundf(canvas_x1) + RULERHANDLE_W / 2,
1491                         roundf(canvas_y1));
1492                 get_canvas()->draw_line(roundf(canvas_x1),
1493                         roundf(canvas_y1) - RULERHANDLE_H / 2,
1494                         roundf(canvas_x1),
1495                         roundf(canvas_y1) + RULERHANDLE_H / 2);
1496                 get_canvas()->draw_line(roundf(canvas_x2) - RULERHANDLE_W / 2,
1497                         roundf(canvas_y2),
1498                         roundf(canvas_x2) + RULERHANDLE_W / 2,
1499                         roundf(canvas_y2));
1500                 get_canvas()->draw_line(roundf(canvas_x2),
1501                         roundf(canvas_y2) - RULERHANDLE_H / 2,
1502                         roundf(canvas_x2),
1503                         roundf(canvas_y2) + RULERHANDLE_H / 2);
1504                 get_canvas()->set_opaque();
1505         }
1506
1507         return result;
1508 }
1509
1510
1511 static inline double line_dist(float cx,float cy, float tx,float ty)
1512 {
1513         double dx = tx-cx, dy = ty-cy;
1514         return sqrt(dx*dx + dy*dy);
1515 }
1516
1517 static inline bool test_bbox(int cx, int cy, int tx, int ty)
1518 {
1519 //      printf("test_bbox %d,%d - %d,%d = %f\n",cx,cy,tx,ty,line_dist(cx,cy,tx,ty));
1520         return (llabs(cx-tx) < CONTROL_W/2 && llabs(cy-ty) < CONTROL_H/2);
1521 }
1522
1523
1524 int CWindowCanvas::do_mask(int &redraw, int &rerender,
1525                 int button_press, int cursor_motion, int draw)
1526 {
1527 // Retrieve points from top recordable track
1528 //printf("CWindowCanvas::do_mask 1\n");
1529         Track *track = gui->cwindow->calculate_affected_track();
1530 //printf("CWindowCanvas::do_mask 2\n");
1531
1532         if(!track) return 0;
1533 //printf("CWindowCanvas::do_mask 3\n");
1534
1535         MaskAutos *mask_autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1536         int64_t position = track->to_units(
1537                 mwindow->edl->local_session->get_selectionstart(1),
1538                 0);
1539         ArrayList<MaskPoint*> points;
1540
1541 // Determine the points based on whether
1542 // new keyframes will be generated or drawing is performed.
1543 // If keyframe generation occurs, use the interpolated mask.
1544 // If no keyframe generation occurs, use the previous mask.
1545         int use_interpolated = 0;
1546         if(button_press || cursor_motion) {
1547 #ifdef USE_KEYFRAME_SPANNING
1548                 double selection_start = mwindow->edl->local_session->get_selectionstart(0);
1549                 double selection_end = mwindow->edl->local_session->get_selectionend(0);
1550
1551                 Auto *first = 0;
1552                 mask_autos->get_prev_auto(track->to_units(selection_start, 0),
1553                         PLAY_FORWARD, first, 1);
1554                 Auto *last = 0;
1555                 mask_autos->get_prev_auto(track->to_units(selection_end, 0),
1556                         PLAY_FORWARD, last, 1);
1557
1558                 if(last == first && (!mwindow->edl->session->auto_keyframes))
1559                         use_interpolated = 0;
1560                 else
1561 // If keyframe spanning occurs, use the interpolated points.
1562 // If new keyframe is generated, use the interpolated points.
1563                         use_interpolated = 1;
1564
1565 #else
1566                 if(mwindow->edl->session->auto_keyframes)
1567                         use_interpolated = 1;
1568 #endif
1569         }
1570         else
1571                 use_interpolated = 1;
1572
1573         if(use_interpolated) {
1574 // Interpolate the points to get exactly what is being rendered at this position.
1575                 mask_autos->get_points(&points,
1576                         mwindow->edl->session->cwindow_mask,
1577                         position,
1578                         PLAY_FORWARD);
1579         }
1580         else {
1581 // Use the prev mask
1582                 Auto *prev = 0;
1583                 mask_autos->get_prev_auto(position,
1584                         PLAY_FORWARD,
1585                         prev,
1586                         1);
1587                 ((MaskAuto*)prev)->get_points(&points,
1588                         mwindow->edl->session->cwindow_mask);
1589         }
1590
1591 // Projector zooms relative to the center of the track output.
1592         float half_track_w = (float)track->track_w / 2;
1593         float half_track_h = (float)track->track_h / 2;
1594 // Translate mask to projection
1595         float projector_x, projector_y, projector_z;
1596         track->automation->get_projector(
1597                 &projector_x, &projector_y, &projector_z,
1598                 position, PLAY_FORWARD);
1599
1600
1601 // Get position of cursor relative to mask
1602         float cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
1603         float mask_cursor_x = cursor_x, mask_cursor_y = cursor_y;
1604         canvas_to_output(mwindow->edl, 0, mask_cursor_x, mask_cursor_y);
1605
1606         projector_x += mwindow->edl->session->output_w / 2;
1607         projector_y += mwindow->edl->session->output_h / 2;
1608         mask_cursor_x = (mask_cursor_x - projector_x) / projector_z + half_track_w;
1609         mask_cursor_y = (mask_cursor_y - projector_y) / projector_z + half_track_h;
1610
1611 // Fix cursor origin
1612         if(button_press) {
1613                 gui->x_origin = mask_cursor_x;
1614                 gui->y_origin = mask_cursor_y;
1615         }
1616
1617         int result = 0;
1618 // Points of closest line
1619         int shortest_point1 = -1;
1620         int shortest_point2 = -1;
1621 // Closest point
1622         int shortest_point = -1;
1623 // Distance to closest line
1624         float shortest_line_distance = BC_INFINITY;
1625 // Distance to closest point
1626         float shortest_point_distance = BC_INFINITY;
1627         int selected_point = -1;
1628         int selected_control_point = -1;
1629         float selected_control_point_distance = BC_INFINITY;
1630         ArrayList<int> x_points;
1631         ArrayList<int> y_points;
1632
1633         if(!cursor_motion) {
1634                 if(draw) {
1635                         get_canvas()->set_color(WHITE);
1636                         get_canvas()->set_inverse();
1637                 }
1638 //printf("CWindowCanvas::do_mask 1 %d\n", points.size());
1639
1640 // Never draw closed polygon and a closed
1641 // polygon is harder to add points to.
1642                 for(int i = 0; i < points.size() && !result; i++) {
1643                         MaskPoint *point1 = points.get(i);
1644                         MaskPoint *point2 = (i >= points.size() - 1) ?
1645                                 points.get(0) : points.get(i + 1);
1646                         if(button_press) {
1647                                 float point_distance1 = line_dist(point1->x,point1->y, mask_cursor_x,mask_cursor_y);
1648                                 if(point_distance1 < shortest_point_distance || shortest_point < 0) {
1649                                         shortest_point_distance = point_distance1;
1650                                         shortest_point = i;
1651                                 }
1652
1653                                 float point_distance2 = line_dist(point2->x,point2->y, mask_cursor_x,mask_cursor_y);
1654                                 if(point_distance2 < shortest_point_distance || shortest_point < 0) {
1655                                         shortest_point_distance = point_distance2;
1656                                         shortest_point = (i >= points.size() - 1) ? 0 : (i + 1);
1657                                 }
1658                         }
1659
1660                         int segments = 1 + line_dist(point1->x,point1->y, point2->x,point2->y);
1661
1662 //printf("CWindowCanvas::do_mask 1 %f, %f -> %f, %f projectorz=%f\n",
1663 //point1->x, point1->y, point2->x, point2->y, projector_z);
1664                         for(int j = 0; j <= segments && !result; j++) {
1665 //printf("CWindowCanvas::do_mask 1 %f, %f -> %f, %f\n", x0, y0, x3, y3);
1666                                 float x0 = point1->x, y0 = point1->y;
1667                                 float x1 = point1->x + point1->control_x2;
1668                                 float y1 = point1->y + point1->control_y2;
1669                                 float x2 = point2->x + point2->control_x1;
1670                                 float y2 = point2->y + point2->control_y1;
1671                                 float x3 = point2->x, y3 = point2->y;
1672                                 float canvas_x0 = (x0 - half_track_w) * projector_z + projector_x;
1673                                 float canvas_y0 = (y0 - half_track_h) * projector_z + projector_y;
1674                                 float canvas_x1 = (x1 - half_track_w) * projector_z + projector_x;
1675                                 float canvas_y1 = (y1 - half_track_h) * projector_z + projector_y;
1676                                 float canvas_x2 = (x2 - half_track_w) * projector_z + projector_x;
1677                                 float canvas_y2 = (y2 - half_track_h) * projector_z + projector_y;
1678                                 float canvas_x3 = (x3 - half_track_w) * projector_z + projector_x;
1679                                 float canvas_y3 = (y3 - half_track_h) * projector_z + projector_y;
1680
1681                                 float t = (float)j / segments;
1682                                 float tpow2 = t * t;
1683                                 float tpow3 = t * t * t;
1684                                 float invt = 1 - t;
1685                                 float invtpow2 = invt * invt;
1686                                 float invtpow3 = invt * invt * invt;
1687
1688                                 float x = (           invtpow3 * x0
1689                                         + 3 * t     * invtpow2 * x1
1690                                         + 3 * tpow2 * invt     * x2
1691                                         +     tpow3            * x3);
1692                                 float y = (           invtpow3 * y0
1693                                         + 3 * t     * invtpow2 * y1
1694                                         + 3 * tpow2 * invt     * y2
1695                                         +     tpow3            * y3);
1696                                 float canvas_x = (x - half_track_w) * projector_z + projector_x;
1697                                 float canvas_y = (y - half_track_h) * projector_z + projector_y;
1698 // Test new point addition
1699                                 if(button_press) {
1700                                         float line_distance = line_dist(x,y, mask_cursor_x,mask_cursor_y);
1701
1702 //printf("CWindowCanvas::do_mask 1 x=%f cursor_x=%f y=%f cursor_y=%f %f %f %d, %d\n",
1703 //  x, cursor_x, y, cursor_y, line_distance, shortest_line_distance, shortest_point1, shortest_point2);
1704                                         if(line_distance < shortest_line_distance ||
1705                                                 shortest_point1 < 0) {
1706                                                 shortest_line_distance = line_distance;
1707                                                 shortest_point1 = i;
1708                                                 shortest_point2 = (i >= points.size() - 1) ? 0 : (i + 1);
1709 //printf("CWindowCanvas::do_mask 2 %f %f %d, %d\n",
1710 //  line_distance, shortest_line_distance, shortest_point1, shortest_point2);
1711                                         }
1712
1713 // Test existing point selection
1714 // Test first point
1715                                         if(gui->ctrl_down()) {
1716                                                 float distance = line_dist(x1,y1, mask_cursor_x,mask_cursor_y);
1717
1718                                                 if(distance < selected_control_point_distance) {
1719                                                         selected_point = i;
1720                                                         selected_control_point = 1;
1721                                                         selected_control_point_distance = distance;
1722                                                 }
1723                                         }
1724                                         else {
1725                                                 if(!gui->shift_down()) {
1726                                                         output_to_canvas(mwindow->edl, 0, canvas_x0, canvas_y0);
1727                                                         if(test_bbox(cursor_x, cursor_y, canvas_x0, canvas_y0)) {
1728                                                                 selected_point = i;
1729                                                         }
1730                                                 }
1731                                                 else {
1732                                                         selected_point = shortest_point;
1733                                                 }
1734                                         }
1735 // Test second point
1736                                         if(gui->ctrl_down()) {
1737                                                 float distance = line_dist(x2,y2, mask_cursor_x,mask_cursor_y);
1738
1739 //printf("CWindowCanvas::do_mask %d %f %f\n", i, distance, selected_control_point_distance);
1740                                                 if(distance < selected_control_point_distance) {
1741                                                         selected_point = (i < points.size() - 1 ? i + 1 : 0);
1742                                                         selected_control_point = 0;
1743                                                         selected_control_point_distance = distance;
1744                                                 }
1745                                         }
1746                                         else if(i < points.size() - 1) {
1747                                                 if(!gui->shift_down()) {
1748                                                         output_to_canvas(mwindow->edl, 0, canvas_x3, canvas_y3);
1749                                                         if(test_bbox(cursor_x, cursor_y, canvas_x3, canvas_y3)) {
1750                                                                 selected_point = (i < points.size() - 1 ? i + 1 : 0);
1751                                                         }
1752                                                 }
1753                                                 else {
1754                                                         selected_point = shortest_point;
1755                                                 }
1756                                         }
1757                                 }
1758
1759                                 output_to_canvas(mwindow->edl, 0, canvas_x, canvas_y);
1760
1761                                 if(j > 0) {
1762
1763                                         if(draw) { // Draw joining line
1764                                                 x_points.append((int)canvas_x);
1765                                                 y_points.append((int)canvas_y);
1766                                         }
1767
1768                                         if(j == segments) {
1769                                                 if(draw) { // Draw second anchor
1770                                                         if(i < points.size() - 1) {
1771                                                                 if(i == gui->affected_point - 1)
1772                                                                         get_canvas()->draw_disc(
1773                                                                                 (int)canvas_x - CONTROL_W / 2,
1774                                                                                 (int)canvas_y - CONTROL_W / 2,
1775                                                                                 CONTROL_W, CONTROL_H);
1776                                                                 else
1777                                                                         get_canvas()->draw_circle(
1778                                                                                 (int)canvas_x - CONTROL_W / 2,
1779                                                                                 (int)canvas_y - CONTROL_W / 2,
1780                                                                                 CONTROL_W, CONTROL_H);
1781 // char string[BCTEXTLEN];
1782 // sprintf(string, "%d", (i < points.size() - 1 ? i + 1 : 0));
1783 // canvas->draw_text((int)canvas_x + CONTROL_W, (int)canvas_y + CONTROL_W, string);
1784                                                         }
1785 // Draw second control point.
1786                                                         output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
1787                                                         get_canvas()->draw_line(
1788                                                                 (int)canvas_x, (int)canvas_y,
1789                                                                 (int)canvas_x2, (int)canvas_y2);
1790                                                         get_canvas()->draw_rectangle(
1791                                                                 (int)canvas_x2 - CONTROL_W / 2,
1792                                                                 (int)canvas_y2 - CONTROL_H / 2,
1793                                                                 CONTROL_W, CONTROL_H);
1794                                                 }
1795                                         }
1796                                 }
1797                                 else {
1798 // Draw first anchor
1799                                         if(i == 0 && draw) {
1800                                                 char mask_label[BCSTRLEN];
1801                                                 sprintf(mask_label, "%d",
1802                                                         mwindow->edl->session->cwindow_mask);
1803                                                 get_canvas()->draw_text(
1804                                                         (int)canvas_x - FIRST_CONTROL_W,
1805                                                         (int)canvas_y - FIRST_CONTROL_H,
1806                                                         mask_label);
1807
1808                                                 get_canvas()->draw_disc(
1809                                                         (int)canvas_x - FIRST_CONTROL_W / 2,
1810                                                         (int)canvas_y - FIRST_CONTROL_H / 2,
1811                                                         FIRST_CONTROL_W, FIRST_CONTROL_H);
1812                                         }
1813
1814 // Draw first control point.
1815                                         if(draw) {
1816                                                 output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
1817                                                 get_canvas()->draw_line(
1818                                                         (int)canvas_x, (int)canvas_y,
1819                                                         (int)canvas_x1, (int)canvas_y1);
1820                                                 get_canvas()->draw_rectangle(
1821                                                         (int)canvas_x1 - CONTROL_W / 2,
1822                                                         (int)canvas_y1 - CONTROL_H / 2,
1823                                                         CONTROL_W, CONTROL_H);
1824
1825                                                 x_points.append((int)canvas_x);
1826                                                 y_points.append((int)canvas_y);
1827                                         }
1828                                 }
1829 //printf("CWindowCanvas::do_mask 1\n");
1830
1831                         }
1832                 }
1833 //printf("CWindowCanvas::do_mask 1\n");
1834
1835                 if(draw) {
1836                         get_canvas()->draw_polygon(&x_points, &y_points);
1837                         get_canvas()->set_opaque();
1838                 }
1839 //printf("CWindowCanvas::do_mask 1\n");
1840         }
1841
1842         if(button_press && !result) {
1843                 gui->affected_track = gui->cwindow->calculate_affected_track();
1844
1845 // Get keyframe outside the EDL to edit.  This must be rendered
1846 // instead of the EDL keyframes when it exists.  Then it must be
1847 // applied to the EDL keyframes on buttonrelease.
1848                 if(gui->affected_track) {
1849 #ifdef USE_KEYFRAME_SPANNING
1850 // Make copy of current parameters in local keyframe
1851                         gui->mask_keyframe =
1852                                 (MaskAuto*)gui->cwindow->calculate_affected_auto(
1853                                         mask_autos,
1854                                         0);
1855                         gui->orig_mask_keyframe->copy_data(gui->mask_keyframe);
1856 #else
1857
1858                         gui->mask_keyframe =
1859                                 (MaskAuto*)gui->cwindow->calculate_affected_auto(
1860                                         mask_autos,
1861                                         1);
1862 #endif
1863                 }
1864                 SubMask *mask = gui->mask_keyframe->get_submask(mwindow->edl->session->cwindow_mask);
1865
1866
1867 // Translate entire keyframe
1868                 if(gui->alt_down() && mask->points.size()) {
1869                         mwindow->undo->update_undo_before(_("mask translate"), 0);
1870                         gui->current_operation = CWINDOW_MASK_TRANSLATE;
1871                         gui->affected_point = 0;
1872                 }
1873                 else
1874 // Existing point or control point was selected
1875                 if(selected_point >= 0) {
1876                         mwindow->undo->update_undo_before(_("mask adjust"), 0);
1877                         gui->affected_point = selected_point;
1878
1879                         if(selected_control_point == 0)
1880                                 gui->current_operation = CWINDOW_MASK_CONTROL_IN;
1881                         else
1882                         if(selected_control_point == 1)
1883                                 gui->current_operation = CWINDOW_MASK_CONTROL_OUT;
1884                         else
1885                                 gui->current_operation = mwindow->edl->session->cwindow_operation;
1886                 }
1887                 else // No existing point or control point was selected so create a new one
1888                 if(!gui->ctrl_down() && !gui->alt_down()) {
1889                         mwindow->undo->update_undo_before(_("mask point"), 0);
1890 // Create the template
1891                         MaskPoint *point = new MaskPoint;
1892                         point->x = mask_cursor_x;
1893                         point->y = mask_cursor_y;
1894                         point->control_x1 = 0;
1895                         point->control_y1 = 0;
1896                         point->control_x2 = 0;
1897                         point->control_y2 = 0;
1898
1899
1900                         if(shortest_point2 < shortest_point1) {
1901                                 shortest_point2 ^= shortest_point1;
1902                                 shortest_point1 ^= shortest_point2;
1903                                 shortest_point2 ^= shortest_point1;
1904                         }
1905
1906
1907
1908 // printf("CWindowGUI::do_mask 40\n");
1909 // mwindow->edl->dump();
1910 // printf("CWindowGUI::do_mask 50\n");
1911
1912
1913
1914 //printf("CWindowCanvas::do_mask 1 %f %f %d %d\n",
1915 //      shortest_line_distance, shortest_point_distance, shortest_point1, shortest_point2);
1916 //printf("CWindowCanvas::do_mask %d %d\n", shortest_point1, shortest_point2);
1917
1918 // Append to end of list
1919                         if( shortest_point1 == shortest_point2 ||
1920                             labs(shortest_point1 - shortest_point2) > 1) {
1921 #ifdef USE_KEYFRAME_SPANNING
1922
1923                                 MaskPoint *new_point = new MaskPoint;
1924                                 points.append(new_point);
1925                                 *new_point = *point;
1926                                 gui->affected_point = points.size() - 1;
1927
1928 #else
1929
1930 // Need to apply the new point to every keyframe
1931                                 for(MaskAuto *current = (MaskAuto*)mask_autos->default_auto; current; ) {
1932                                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1933                                         MaskPoint *new_point = new MaskPoint;
1934                                         submask->points.append(new_point);
1935                                         *new_point = *point;
1936                                         if(current == (MaskAuto*)mask_autos->default_auto)
1937                                                 current = (MaskAuto*)mask_autos->first;
1938                                         else
1939                                                 current = (MaskAuto*)NEXT;
1940                                 }
1941                                 gui->affected_point = mask->points.size() - 1;
1942 #endif
1943
1944                                 result = 1;
1945                         }
1946                         else
1947 // Insert between 2 points, shifting back point 2
1948                         if(shortest_point1 >= 0 && shortest_point2 >= 0) {
1949
1950 #ifdef USE_KEYFRAME_SPANNING
1951 // In case the keyframe point count isn't synchronized with the rest of the keyframes,
1952 // avoid a crash.
1953                                 if(points.size() >= shortest_point2) {
1954                                         MaskPoint *new_point = new MaskPoint;
1955                                         points.append(0);
1956                                         for(int i = points.size() - 1;
1957                                                 i > shortest_point2;
1958                                                 i--)
1959                                                 points.values[i] = points.values[i - 1];
1960                                         points.values[shortest_point2] = new_point;
1961
1962                                         *new_point = *point;
1963                                 }
1964
1965 #else
1966
1967                                 for(MaskAuto *current = (MaskAuto*)mask_autos->default_auto; current; ) {
1968                                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1969 // In case the keyframe point count isn't synchronized with the rest of the keyframes,
1970 // avoid a crash.
1971                                         if(submask->points.size() >= shortest_point2) {
1972                                                 MaskPoint *new_point = new MaskPoint;
1973                                                 submask->points.append(0);
1974                                                 for(int i = submask->points.size() - 1;
1975                                                         i > shortest_point2;
1976                                                         i--)
1977                                                         submask->points.values[i] = submask->points.values[i - 1];
1978                                                 submask->points.values[shortest_point2] = new_point;
1979
1980                                                 *new_point = *point;
1981                                         }
1982
1983                                         if(current == (MaskAuto*)mask_autos->default_auto)
1984                                                 current = (MaskAuto*)mask_autos->first;
1985                                         else
1986                                                 current = (MaskAuto*)NEXT;
1987                                 }
1988
1989 #endif
1990                                 gui->affected_point = shortest_point2;
1991                                 result = 1;
1992                         }
1993
1994
1995 // printf("CWindowGUI::do_mask 20\n");
1996 // mwindow->edl->dump();
1997 // printf("CWindowGUI::do_mask 30\n");
1998
1999                         if(!result) {
2000 //printf("CWindowCanvas::do_mask 1\n");
2001 // Create the first point.
2002 #ifdef USE_KEYFRAME_SPANNING
2003                                 MaskPoint *new_point = new MaskPoint;
2004                                 points.append(new_point);
2005                                 *new_point = *point;
2006                                 gui->affected_point = points.size() - 1;
2007 #else
2008                                 for(MaskAuto *current = (MaskAuto*)mask_autos->default_auto; current; ) {
2009                                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
2010                                         MaskPoint *new_point = new MaskPoint;
2011                                         submask->points.append(new_point);
2012                                         *new_point = *point;
2013                                         if(current == (MaskAuto*)mask_autos->default_auto)
2014                                                 current = (MaskAuto*)mask_autos->first;
2015                                         else
2016                                                 current = (MaskAuto*)NEXT;
2017                                 }
2018                                 gui->affected_point = points.size() - 1;
2019 #endif
2020
2021 //printf("CWindowCanvas::do_mask 3 %d\n", mask->points.size());
2022                         }
2023
2024                         gui->current_operation = mwindow->edl->session->cwindow_operation;
2025 // Delete the template
2026                         delete point;
2027                 }
2028
2029                 result = 1;
2030                 rerender = 1;
2031                 redraw = 1;
2032         }
2033
2034         if(button_press && result) {
2035 #ifdef USE_KEYFRAME_SPANNING
2036                 MaskPoint *point = points.values[gui->affected_point];
2037                 gui->center_x = point->x;
2038                 gui->center_y = point->y;
2039                 gui->control_in_x = point->control_x1;
2040                 gui->control_in_y = point->control_y1;
2041                 gui->control_out_x = point->control_x2;
2042                 gui->control_out_y = point->control_y2;
2043                 gui->tool_panel->raise_window();
2044 #else
2045                 SubMask *mask = gui->mask_keyframe->get_submask(mwindow->edl->session->cwindow_mask);
2046                 MaskPoint *point = mask->points.values[gui->affected_point];
2047                 gui->center_x = point->x;
2048                 gui->center_y = point->y;
2049                 gui->control_in_x = point->control_x1;
2050                 gui->control_in_y = point->control_y1;
2051                 gui->control_out_x = point->control_x2;
2052                 gui->control_out_y = point->control_y2;
2053                 gui->tool_panel->raise_window();
2054 #endif
2055         }
2056
2057 //printf("CWindowCanvas::do_mask 8\n");
2058         if(cursor_motion) {
2059
2060 #ifdef USE_KEYFRAME_SPANNING
2061 // Must update the reference keyframes for every cursor motion
2062                 gui->mask_keyframe =
2063                         (MaskAuto*)gui->cwindow->calculate_affected_auto(
2064                                 mask_autos,
2065                                 0);
2066                 gui->orig_mask_keyframe->copy_data(gui->mask_keyframe);
2067 #endif
2068
2069 //printf("CWindowCanvas::do_mask %d %d\n", __LINE__, gui->affected_point);
2070
2071                 SubMask *mask = gui->mask_keyframe->get_submask(mwindow->edl->session->cwindow_mask);
2072                 if( gui->affected_point >= 0 && gui->affected_point < mask->points.size() &&
2073                         gui->current_operation != CWINDOW_NONE) {
2074 //                      mwindow->undo->update_undo_before(_("mask point"), this);
2075 #ifdef USE_KEYFRAME_SPANNING
2076                         MaskPoint *point = points.get(gui->affected_point);
2077 #else
2078                         MaskPoint *point = mask->points.get(gui->affected_point);
2079 #endif
2080 //                      canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
2081 //printf("CWindowCanvas::do_mask 9 %d %d\n", mask->points.size(), gui->affected_point);
2082
2083                         float last_x = point->x;
2084                         float last_y = point->y;
2085                         float last_control_x1 = point->control_x1;
2086                         float last_control_y1 = point->control_y1;
2087                         float last_control_x2 = point->control_x2;
2088                         float last_control_y2 = point->control_y2;
2089
2090                         switch(gui->current_operation) {
2091                                 case CWINDOW_MASK:
2092 //printf("CWindowCanvas::do_mask %d %d\n", __LINE__, gui->affected_point);
2093                                         point->x = mask_cursor_x - gui->x_origin + gui->center_x;
2094                                         point->y = mask_cursor_y - gui->y_origin + gui->center_y;
2095                                         break;
2096
2097                                 case CWINDOW_MASK_CONTROL_IN:
2098                                         point->control_x1 = mask_cursor_x - gui->x_origin + gui->control_in_x;
2099                                         point->control_y1 = mask_cursor_y - gui->y_origin + gui->control_in_y;
2100                                         break;
2101
2102                                 case CWINDOW_MASK_CONTROL_OUT:
2103                                         point->control_x2 = mask_cursor_x - gui->x_origin + gui->control_out_x;
2104                                         point->control_y2 = mask_cursor_y - gui->y_origin + gui->control_out_y;
2105                                         break;
2106
2107                                 case CWINDOW_MASK_TRANSLATE:
2108 #ifdef USE_KEYFRAME_SPANNING
2109                                         for(int i = 0; i < points.size(); i++) {
2110                                                 points.values[i]->x += mask_cursor_x - gui->x_origin;
2111                                                 points.values[i]->y += mask_cursor_y - gui->y_origin;
2112                                         }
2113 #else
2114                                         for(int i = 0; i < mask->points.size(); i++) {
2115                                                 mask->points.values[i]->x += mask_cursor_x - gui->x_origin;
2116                                                 mask->points.values[i]->y += mask_cursor_y - gui->y_origin;
2117                                         }
2118 #endif
2119                                         gui->x_origin = mask_cursor_x;
2120                                         gui->y_origin = mask_cursor_y;
2121                                         break;
2122                         }
2123
2124                         if( !EQUIV(last_x, point->x) ||
2125                                 !EQUIV(last_y, point->y) ||
2126                                 !EQUIV(last_control_x1, point->control_x1) ||
2127                                 !EQUIV(last_control_y1, point->control_y1) ||
2128                                 !EQUIV(last_control_x2, point->control_x2) ||
2129                                 !EQUIV(last_control_y2, point->control_y2)) {
2130                                 rerender = 1;
2131                                 redraw = 1;
2132                         }
2133                 }
2134                 else
2135                 if(gui->current_operation == CWINDOW_NONE) {
2136 //                      printf("CWindowCanvas::do_mask %d\n", __LINE__);
2137                         int over_point = 0;
2138                         for(int i = 0; i < points.size() && !over_point; i++) {
2139                                 MaskPoint *point = points.get(i);
2140                                 float x0 = point->x;
2141                                 float y0 = point->y;
2142                                 float x1 = point->x + point->control_x1;
2143                                 float y1 = point->y + point->control_y1;
2144                                 float x2 = point->x + point->control_x2;
2145                                 float y2 = point->y + point->control_y2;
2146                                 float canvas_x0 = (x0 - half_track_w) * projector_z + projector_x;
2147                                 float canvas_y0 = (y0 - half_track_h) * projector_z + projector_y;
2148
2149                                 output_to_canvas(mwindow->edl, 0, canvas_x0, canvas_y0);
2150                                 if(test_bbox(cursor_x, cursor_y, canvas_x0, canvas_y0)) {
2151                                         over_point = 1;
2152                                 }
2153
2154                                 if(!over_point && gui->ctrl_down()) {
2155                                         float canvas_x1 = (x1 - half_track_w) * projector_z + projector_x;
2156                                         float canvas_y1 = (y1 - half_track_h) * projector_z + projector_y;
2157                                         output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
2158                                         if(test_bbox(cursor_x, cursor_y, canvas_x1, canvas_y1)) {
2159                                                 over_point = 1;
2160                                         }
2161                                         else {
2162                                                 float canvas_x2 = (x2 - half_track_w) * projector_z + projector_x;
2163                                                 float canvas_y2 = (y2 - half_track_h) * projector_z + projector_y;
2164                                                 output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
2165                                                 if(test_bbox(cursor_x, cursor_y, canvas_x2, canvas_y2)) {
2166                                                         over_point = 1;
2167                                                 }
2168                                         }
2169                                 }
2170                         }
2171
2172                         set_cursor( over_point ? ARROW_CURSOR : CROSS_CURSOR );
2173                 }
2174
2175                 result = 1;
2176         }
2177 //printf("CWindowCanvas::do_mask 2 %d %d %d\n", result, rerender, redraw);
2178
2179
2180 #ifdef USE_KEYFRAME_SPANNING
2181 // Must commit change after operation.
2182         if(rerender && track) {
2183 // Swap EDL keyframe with original.
2184 // Apply new values to keyframe span
2185                 MaskAuto temp_keyframe(mwindow->edl, mask_autos);
2186                 temp_keyframe.copy_data(gui->mask_keyframe);
2187 // Apply interpolated points back to keyframe
2188                 temp_keyframe.set_points(&points, mwindow->edl->session->cwindow_mask);
2189                 gui->mask_keyframe->copy_data(gui->orig_mask_keyframe);
2190                 mask_autos->update_parameter(&temp_keyframe);
2191         }
2192 #endif
2193
2194         points.remove_all_objects();
2195 //printf("CWindowCanvas::do_mask 20\n");
2196         return result;
2197 }
2198
2199 int CWindowCanvas::do_eyedrop(int &rerender, int button_press, int draw)
2200 {
2201         int result = 0;
2202         int radius = mwindow->edl->session->eyedrop_radius;
2203         int row1 = 0;
2204         int row2 = 0;
2205         int column1 = 0;
2206         int column2 = 0;
2207
2208
2209
2210         if(refresh_frame && refresh_frame->get_w()>0 && refresh_frame->get_h()>0)
2211         {
2212
2213                 if(draw)
2214                 {
2215                         row1 = gui->eyedrop_y - radius;
2216                         row2 = gui->eyedrop_y + radius;
2217                         column1 = gui->eyedrop_x - radius;
2218                         column2 = gui->eyedrop_x + radius;
2219
2220                         CLAMP(row1, 0, refresh_frame->get_h() - 1);
2221                         CLAMP(row2, 0, refresh_frame->get_h() - 1);
2222                         CLAMP(column1, 0, refresh_frame->get_w() - 1);
2223                         CLAMP(column2, 0, refresh_frame->get_w() - 1);
2224
2225                         if(row2 <= row1) row2 = row1 + 1;
2226                         if(column2 <= column1) column2 = column1 + 1;
2227
2228                         float x1 = column1;
2229                         float y1 = row1;
2230                         float x2 = column2;
2231                         float y2 = row2;
2232
2233                         output_to_canvas(mwindow->edl, 0, x1, y1);
2234                         output_to_canvas(mwindow->edl, 0, x2, y2);
2235 //printf("CWindowCanvas::do_eyedrop %d %f %f %f %f\n", __LINE__, x1, x2, y1, y2);
2236
2237                         if(x2 - x1 >= 1 && y2 - y1 >= 1)
2238                         {
2239                                 get_canvas()->set_inverse();
2240                                 get_canvas()->set_color(WHITE);
2241
2242                                 get_canvas()->draw_rectangle((int)x1,
2243                                         (int)y1,
2244                                         (int)(x2 - x1),
2245                                         (int)(y2 - y1));
2246
2247                                 get_canvas()->set_opaque();
2248                                 get_canvas()->flash();
2249                         }
2250                         return 0;
2251                 }
2252         }
2253
2254         if(button_press)
2255         {
2256                 gui->current_operation = CWINDOW_EYEDROP;
2257                 gui->tool_panel->raise_window();
2258         }
2259
2260         if(gui->current_operation == CWINDOW_EYEDROP)
2261         {
2262                 mwindow->undo->update_undo_before(_("Eyedrop"), this);
2263
2264 // Get color out of frame.
2265 // Doesn't work during playback because that bypasses the refresh frame.
2266                 if(refresh_frame && refresh_frame->get_w()>0 && refresh_frame->get_h()>0)
2267                 {
2268                         float cursor_x = get_cursor_x();
2269                         float cursor_y = get_cursor_y();
2270                         canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
2271                         CLAMP(cursor_x, 0, refresh_frame->get_w() - 1);
2272                         CLAMP(cursor_y, 0, refresh_frame->get_h() - 1);
2273
2274                         row1 = cursor_y - radius;
2275                         row2 = cursor_y + radius;
2276                         column1 = cursor_x - radius;
2277                         column2 = cursor_x + radius;
2278                         CLAMP(row1, 0, refresh_frame->get_h() - 1);
2279                         CLAMP(row2, 0, refresh_frame->get_h() - 1);
2280                         CLAMP(column1, 0, refresh_frame->get_w() - 1);
2281                         CLAMP(column2, 0, refresh_frame->get_w() - 1);
2282                         if(row2 <= row1) row2 = row1 + 1;
2283                         if(column2 <= column1) column2 = column1 + 1;
2284
2285
2286 // hide it
2287                         if(gui->eyedrop_visible)
2288                         {
2289                                 int temp;
2290                                 do_eyedrop(temp, 0, 1);
2291                                 gui->eyedrop_visible = 0;
2292                         }
2293
2294                         gui->eyedrop_x = cursor_x;
2295                         gui->eyedrop_y = cursor_y;
2296
2297 // show it
2298                         {
2299                                 int temp;
2300                                 do_eyedrop(temp, 0, 1);
2301                                 gui->eyedrop_visible = 1;
2302                         }
2303
2304 // Decompression coefficients straight out of jpeglib
2305 #define V_TO_R    1.40200
2306 #define V_TO_G    -0.71414
2307
2308 #define U_TO_G    -0.34414
2309 #define U_TO_B    1.77200
2310
2311 #define GET_COLOR(type, components, max, do_yuv) \
2312 { \
2313         type *row = (type*)(refresh_frame->get_rows()[i]) + \
2314                 j * components; \
2315         float red = (float)*row++ / max; \
2316         float green = (float)*row++ / max; \
2317         float blue = (float)*row++ / max; \
2318         if(do_yuv) \
2319         { \
2320                 float r = red + V_TO_R * (blue - 0.5); \
2321                 float g = red + U_TO_G * (green - 0.5) + V_TO_G * (blue - 0.5); \
2322                 float b = red + U_TO_B * (green - 0.5); \
2323                 mwindow->edl->local_session->red += r; \
2324                 mwindow->edl->local_session->green += g; \
2325                 mwindow->edl->local_session->blue += b; \
2326                 if(r > mwindow->edl->local_session->red_max) mwindow->edl->local_session->red_max = r; \
2327                 if(g > mwindow->edl->local_session->green_max) mwindow->edl->local_session->green_max = g; \
2328                 if(b > mwindow->edl->local_session->blue_max) mwindow->edl->local_session->blue_max = b; \
2329         } \
2330         else \
2331         { \
2332                 mwindow->edl->local_session->red += red; \
2333                 mwindow->edl->local_session->green += green; \
2334                 mwindow->edl->local_session->blue += blue; \
2335                 if(red > mwindow->edl->local_session->red_max) mwindow->edl->local_session->red_max = red; \
2336                 if(green > mwindow->edl->local_session->green_max) mwindow->edl->local_session->green_max = green; \
2337                 if(blue > mwindow->edl->local_session->blue_max) mwindow->edl->local_session->blue_max = blue; \
2338         } \
2339 }
2340
2341
2342
2343                         mwindow->edl->local_session->red = 0;
2344                         mwindow->edl->local_session->green = 0;
2345                         mwindow->edl->local_session->blue = 0;
2346                         mwindow->edl->local_session->red_max = 0;
2347                         mwindow->edl->local_session->green_max = 0;
2348                         mwindow->edl->local_session->blue_max = 0;
2349                         for(int i = row1; i < row2; i++)
2350                         {
2351                                 for(int j = column1; j < column2; j++)
2352                                 {
2353                                         switch(refresh_frame->get_color_model())
2354                                         {
2355                                                 case BC_YUV888:
2356                                                         GET_COLOR(unsigned char, 3, 0xff, 1);
2357                                                         break;
2358                                                 case BC_YUVA8888:
2359                                                         GET_COLOR(unsigned char, 4, 0xff, 1);
2360                                                         break;
2361                                                 case BC_YUV161616:
2362                                                         GET_COLOR(uint16_t, 3, 0xffff, 1);
2363                                                         break;
2364                                                 case BC_YUVA16161616:
2365                                                         GET_COLOR(uint16_t, 4, 0xffff, 1);
2366                                                         break;
2367                                                 case BC_RGB888:
2368                                                         GET_COLOR(unsigned char, 3, 0xff, 0);
2369                                                         break;
2370                                                 case BC_RGBA8888:
2371                                                         GET_COLOR(unsigned char, 4, 0xff, 0);
2372                                                         break;
2373                                                 case BC_RGB_FLOAT:
2374                                                         GET_COLOR(float, 3, 1.0, 0);
2375                                                         break;
2376                                                 case BC_RGBA_FLOAT:
2377                                                         GET_COLOR(float, 4, 1.0, 0);
2378                                                         break;
2379                                         }
2380                                 }
2381                         }
2382
2383                         mwindow->edl->local_session->red /= (row2 - row1) * (column2 - column1);
2384                         mwindow->edl->local_session->green /= (row2 - row1) * (column2 - column1);
2385                         mwindow->edl->local_session->blue /= (row2 - row1) * (column2 - column1);
2386
2387                 }
2388                 else
2389                 {
2390                         mwindow->edl->local_session->red = 0;
2391                         mwindow->edl->local_session->green = 0;
2392                         mwindow->edl->local_session->blue = 0;
2393                         gui->eyedrop_visible = 0;
2394                 }
2395
2396
2397                 gui->update_tool();
2398
2399
2400
2401                 result = 1;
2402 // Can't rerender since the color value is from the output of any effect it
2403 // goes into.
2404 //              rerender = 1;
2405                 mwindow->undo->update_undo_after(_("Eyedrop"), LOAD_SESSION);
2406         }
2407
2408         return result;
2409 }
2410
2411 int CWindowCanvas::need_overlays()
2412 {
2413         if( mwindow->edl->session->safe_regions ) return 1;
2414         if( mwindow->edl->session->cwindow_scrollbars ) return 1;
2415         if( gui->highlighted ) return 1;
2416         switch( mwindow->edl->session->cwindow_operation ) {
2417                 case CWINDOW_EYEDROP:
2418                         if( ! gui->eyedrop_visible ) break;
2419                 case CWINDOW_CAMERA:
2420                 case CWINDOW_PROJECTOR:
2421                 case CWINDOW_CROP:
2422                 case CWINDOW_MASK:
2423                 case CWINDOW_RULER:
2424                         return 1;
2425         }
2426         return 0;
2427 }
2428
2429 void CWindowCanvas::draw_overlays()
2430 {
2431         if(mwindow->edl->session->safe_regions)
2432         {
2433                 draw_safe_regions();
2434         }
2435
2436         if(mwindow->edl->session->cwindow_scrollbars)
2437         {
2438 // Always draw output rectangle
2439                 float x1, y1, x2, y2;
2440                 x1 = 0;
2441                 x2 = mwindow->edl->session->output_w;
2442                 y1 = 0;
2443                 y2 = mwindow->edl->session->output_h;
2444                 output_to_canvas(mwindow->edl, 0, x1, y1);
2445                 output_to_canvas(mwindow->edl, 0, x2, y2);
2446
2447                 get_canvas()->set_inverse();
2448                 get_canvas()->set_color(WHITE);
2449
2450                 get_canvas()->draw_rectangle((int)x1,
2451                                 (int)y1,
2452                                 (int)(x2 - x1),
2453                                 (int)(y2 - y1));
2454
2455                 get_canvas()->set_opaque();
2456         }
2457
2458         if(gui->highlighted)
2459         {
2460                 get_canvas()->set_color(WHITE);
2461                 get_canvas()->set_inverse();
2462                 get_canvas()->draw_rectangle(0, 0, get_canvas()->get_w(), get_canvas()->get_h());
2463                 get_canvas()->draw_rectangle(1, 1, get_canvas()->get_w() - 2, get_canvas()->get_h() - 2);
2464                 get_canvas()->set_opaque();
2465         }
2466
2467         int temp1 = 0, temp2 = 0;
2468 //printf("CWindowCanvas::draw_overlays 1 %d\n", mwindow->edl->session->cwindow_operation);
2469         switch(mwindow->edl->session->cwindow_operation)
2470         {
2471                 case CWINDOW_CAMERA:
2472                         draw_bezier(1);
2473                         break;
2474
2475                 case CWINDOW_PROJECTOR:
2476                         draw_bezier(0);
2477                         break;
2478
2479                 case CWINDOW_CROP:
2480                         draw_crop();
2481                         break;
2482
2483                 case CWINDOW_MASK:
2484                         do_mask(temp1, temp2, 0, 0, 1);
2485                         break;
2486
2487                 case CWINDOW_RULER:
2488                         do_ruler(1, 0, 0, 0);
2489                         break;
2490
2491                 case CWINDOW_EYEDROP:
2492                 if(gui->eyedrop_visible)
2493                 {
2494                         int rerender;
2495                         do_eyedrop(rerender, 0, 1);
2496                         gui->eyedrop_visible = 1;
2497                         break;
2498                 }
2499         }
2500 }
2501
2502 void CWindowCanvas::draw_safe_regions()
2503 {
2504         float action_x1, action_x2, action_y1, action_y2;
2505         float title_x1, title_x2, title_y1, title_y2;
2506
2507         action_x1 = mwindow->edl->session->output_w / 2 - mwindow->edl->session->output_w / 2 * 0.9;
2508         action_x2 = mwindow->edl->session->output_w / 2 + mwindow->edl->session->output_w / 2 * 0.9;
2509         action_y1 = mwindow->edl->session->output_h / 2 - mwindow->edl->session->output_h / 2 * 0.9;
2510         action_y2 = mwindow->edl->session->output_h / 2 + mwindow->edl->session->output_h / 2 * 0.9;
2511         title_x1 = mwindow->edl->session->output_w / 2 - mwindow->edl->session->output_w / 2 * 0.8;
2512         title_x2 = mwindow->edl->session->output_w / 2 + mwindow->edl->session->output_w / 2 * 0.8;
2513         title_y1 = mwindow->edl->session->output_h / 2 - mwindow->edl->session->output_h / 2 * 0.8;
2514         title_y2 = mwindow->edl->session->output_h / 2 + mwindow->edl->session->output_h / 2 * 0.8;
2515
2516         output_to_canvas(mwindow->edl, 0, action_x1, action_y1);
2517         output_to_canvas(mwindow->edl, 0, action_x2, action_y2);
2518         output_to_canvas(mwindow->edl, 0, title_x1, title_y1);
2519         output_to_canvas(mwindow->edl, 0, title_x2, title_y2);
2520
2521         get_canvas()->set_inverse();
2522         get_canvas()->set_color(WHITE);
2523
2524         get_canvas()->draw_rectangle((int)action_x1,
2525                         (int)action_y1,
2526                         (int)(action_x2 - action_x1),
2527                         (int)(action_y2 - action_y1));
2528         get_canvas()->draw_rectangle((int)title_x1,
2529                         (int)title_y1,
2530                         (int)(title_x2 - title_x1),
2531                         (int)(title_y2 - title_y1));
2532
2533         get_canvas()->set_opaque();
2534 }
2535
2536
2537 void CWindowCanvas::create_keyframe(int do_camera)
2538 {
2539         Track *affected_track = gui->cwindow->calculate_affected_track();
2540         if( affected_track ) {
2541                 double pos = mwindow->edl->local_session->get_selectionstart(1);
2542                 int64_t position = affected_track->to_units(pos, 0);
2543                 int ix = do_camera ? AUTOMATION_CAMERA_X : AUTOMATION_PROJECTOR_X;
2544                 int iy = do_camera ? AUTOMATION_CAMERA_Y : AUTOMATION_PROJECTOR_Y;
2545                 int iz = do_camera ? AUTOMATION_CAMERA_Z : AUTOMATION_PROJECTOR_Z;
2546                 FloatAuto *prev, *next;
2547                 FloatAutos **autos = (FloatAutos**)affected_track->automation->autos;
2548                 FloatAutos *x_autos = autos[ix];  prev = 0;  next = 0;
2549                 float x_value = x_autos->get_value(position, PLAY_FORWARD, prev, next);
2550                 FloatAutos *y_autos = autos[iy];  prev = 0;  next = 0;
2551                 float y_value = y_autos->get_value(position, PLAY_FORWARD, prev, next);
2552                 FloatAutos *z_autos = autos[iz];  prev = 0;  next = 0;
2553                 float z_value = z_autos->get_value(position, PLAY_FORWARD, prev, next);
2554                 FloatAuto *x_keyframe = 0, *y_keyframe = 0, *z_keyframe = 0;
2555
2556                 gui->cwindow->calculate_affected_autos(affected_track,
2557                         &x_keyframe, &y_keyframe, &z_keyframe,
2558                         do_camera, -1, -1, -1, 0);
2559                 x_keyframe->set_value(x_value);
2560                 y_keyframe->set_value(y_value);
2561                 z_keyframe->set_value(z_value);
2562
2563                 gui->sync_parameters(CHANGE_PARAMS, 1, 1);
2564         }
2565 }
2566
2567 void CWindowCanvas::camera_keyframe() { create_keyframe(1); }
2568 void CWindowCanvas::projector_keyframe() { create_keyframe(0); }
2569
2570 void CWindowCanvas::reset_keyframe(int do_camera)
2571 {
2572         Track *affected_track = gui->cwindow->calculate_affected_track();
2573         if( affected_track ) {
2574                 FloatAuto *x_keyframe = 0, *y_keyframe = 0, *z_keyframe = 0;
2575                 gui->cwindow->calculate_affected_autos(affected_track,
2576                         &x_keyframe, &y_keyframe, &z_keyframe,
2577                         do_camera, 1, 1, 1);
2578
2579                 x_keyframe->set_value(0);
2580                 y_keyframe->set_value(0);
2581                 z_keyframe->set_value(1);
2582
2583                 gui->sync_parameters(CHANGE_PARAMS, 1, 1);
2584         }
2585 }
2586
2587 void CWindowCanvas::reset_camera() { reset_keyframe(1); }
2588 void CWindowCanvas::reset_projector() { reset_keyframe(0); }
2589
2590
2591 int CWindowCanvas::test_crop(int button_press, int &redraw)
2592 {
2593         int result = 0;
2594         int handle_selected = -1;
2595         float x1 = mwindow->edl->session->crop_x1;
2596         float y1 = mwindow->edl->session->crop_y1;
2597         float x2 = mwindow->edl->session->crop_x2;
2598         float y2 = mwindow->edl->session->crop_y2;
2599         float cursor_x = get_cursor_x();
2600         float cursor_y = get_cursor_y();
2601         float canvas_x1 = x1;
2602         float canvas_y1 = y1;
2603         float canvas_x2 = x2;
2604         float canvas_y2 = y2;
2605         float canvas_cursor_x = cursor_x;
2606         float canvas_cursor_y = cursor_y;
2607
2608         canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
2609 // Use screen normalized coordinates for hot spot tests.
2610         output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
2611         output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
2612
2613
2614         if(gui->current_operation == CWINDOW_CROP)
2615         {
2616                 handle_selected = gui->crop_handle;
2617         }
2618         else
2619         if(canvas_cursor_x >= canvas_x1 && canvas_cursor_x < canvas_x1 + CROPHANDLE_W &&
2620                 canvas_cursor_y >= canvas_y1 && canvas_cursor_y < canvas_y1 + CROPHANDLE_H)
2621         {
2622                 handle_selected = 0;
2623                 gui->crop_origin_x = x1;
2624                 gui->crop_origin_y = y1;
2625         }
2626         else
2627         if(canvas_cursor_x >= canvas_x2 - CROPHANDLE_W && canvas_cursor_x < canvas_x2 &&
2628                 canvas_cursor_y >= canvas_y1 && canvas_cursor_y < canvas_y1 + CROPHANDLE_H)
2629         {
2630                 handle_selected = 1;
2631                 gui->crop_origin_x = x2;
2632                 gui->crop_origin_y = y1;
2633         }
2634         else
2635         if(canvas_cursor_x >= canvas_x1 && canvas_cursor_x < canvas_x1 + CROPHANDLE_W &&
2636                 canvas_cursor_y >= canvas_y2 - CROPHANDLE_H && canvas_cursor_y < canvas_y2)
2637         {
2638                 handle_selected = 2;
2639                 gui->crop_origin_x = x1;
2640                 gui->crop_origin_y = y2;
2641         }
2642         else
2643         if(canvas_cursor_x >= canvas_x2 - CROPHANDLE_W && canvas_cursor_x < canvas_x2 &&
2644                 canvas_cursor_y >= canvas_y2 - CROPHANDLE_H && canvas_cursor_y < canvas_y2)
2645         {
2646                 handle_selected = 3;
2647                 gui->crop_origin_x = x2;
2648                 gui->crop_origin_y = y2;
2649         }
2650         else
2651 // Start new box
2652         {
2653                 gui->crop_origin_x = cursor_x;
2654                 gui->crop_origin_y = cursor_y;
2655         }
2656
2657 // printf("test crop %d %d\n",
2658 //      gui->current_operation,
2659 //      handle_selected);
2660
2661 // Start dragging.
2662         if(button_press)
2663         {
2664                 if(gui->alt_down())
2665                 {
2666                         gui->crop_translate = 1;
2667                         gui->crop_origin_x1 = x1;
2668                         gui->crop_origin_y1 = y1;
2669                         gui->crop_origin_x2 = x2;
2670                         gui->crop_origin_y2 = y2;
2671                 }
2672                 else
2673                         gui->crop_translate = 0;
2674
2675                 gui->current_operation = CWINDOW_CROP;
2676                 gui->crop_handle = handle_selected;
2677                 gui->x_origin = cursor_x;
2678                 gui->y_origin = cursor_y;
2679                 gui->tool_panel->raise_window();
2680                 result = 1;
2681
2682                 if(handle_selected < 0 && !gui->crop_translate)
2683                 {
2684                         x2 = x1 = cursor_x;
2685                         y2 = y1 = cursor_y;
2686                         mwindow->edl->session->crop_x1 = (int)x1;
2687                         mwindow->edl->session->crop_y1 = (int)y1;
2688                         mwindow->edl->session->crop_x2 = (int)x2;
2689                         mwindow->edl->session->crop_y2 = (int)y2;
2690                         redraw = 1;
2691                 }
2692         }
2693         else
2694 // Translate all 4 points
2695         if(gui->current_operation == CWINDOW_CROP && gui->crop_translate)
2696         {
2697                 x1 = cursor_x - gui->x_origin + gui->crop_origin_x1;
2698                 y1 = cursor_y - gui->y_origin + gui->crop_origin_y1;
2699                 x2 = cursor_x - gui->x_origin + gui->crop_origin_x2;
2700                 y2 = cursor_y - gui->y_origin + gui->crop_origin_y2;
2701
2702                 mwindow->edl->session->crop_x1 = (int)x1;
2703                 mwindow->edl->session->crop_y1 = (int)y1;
2704                 mwindow->edl->session->crop_x2 = (int)x2;
2705                 mwindow->edl->session->crop_y2 = (int)y2;
2706                 result = 1;
2707                 redraw = 1;
2708         }
2709         else
2710 // Update dragging
2711         if(gui->current_operation == CWINDOW_CROP)
2712         {
2713                 switch(gui->crop_handle)
2714                 {
2715                         case -1:
2716                                 x1 = gui->crop_origin_x;
2717                                 y1 = gui->crop_origin_y;
2718                                 x2 = gui->crop_origin_x;
2719                                 y2 = gui->crop_origin_y;
2720                                 if(cursor_x < gui->x_origin)
2721                                 {
2722                                         if(cursor_y < gui->y_origin)
2723                                         {
2724                                                 x1 = cursor_x;
2725                                                 y1 = cursor_y;
2726                                         }
2727                                         else
2728                                         if(cursor_y >= gui->y_origin)
2729                                         {
2730                                                 x1 = cursor_x;
2731                                                 y2 = cursor_y;
2732                                         }
2733                                 }
2734                                 else
2735                                 if(cursor_x  >= gui->x_origin)
2736                                 {
2737                                         if(cursor_y < gui->y_origin)
2738                                         {
2739                                                 y1 = cursor_y;
2740                                                 x2 = cursor_x;
2741                                         }
2742                                         else
2743                                         if(cursor_y >= gui->y_origin)
2744                                         {
2745                                                 x2 = cursor_x;
2746                                                 y2 = cursor_y;
2747                                         }
2748                                 }
2749
2750 // printf("test crop %d %d %d %d\n",
2751 //      mwindow->edl->session->crop_x1,
2752 //      mwindow->edl->session->crop_y1,
2753 //      mwindow->edl->session->crop_x2,
2754 //      mwindow->edl->session->crop_y2);
2755                                 break;
2756                         case 0:
2757                                 x1 = cursor_x - gui->x_origin + gui->crop_origin_x;
2758                                 y1 = cursor_y - gui->y_origin + gui->crop_origin_y;
2759                                 break;
2760                         case 1:
2761                                 x2 = cursor_x - gui->x_origin + gui->crop_origin_x;
2762                                 y1 = cursor_y - gui->y_origin + gui->crop_origin_y;
2763                                 break;
2764                         case 2:
2765                                 x1 = cursor_x - gui->x_origin + gui->crop_origin_x;
2766                                 y2 = cursor_y - gui->y_origin + gui->crop_origin_y;
2767                                 break;
2768                         case 3:
2769                                 x2 = cursor_x - gui->x_origin + gui->crop_origin_x;
2770                                 y2 = cursor_y - gui->y_origin + gui->crop_origin_y;
2771                                 break;
2772                 }
2773
2774                 if(!EQUIV(mwindow->edl->session->crop_x1, x1) ||
2775                         !EQUIV(mwindow->edl->session->crop_x2, x2) ||
2776                         !EQUIV(mwindow->edl->session->crop_y1, y1) ||
2777                         !EQUIV(mwindow->edl->session->crop_y2, y2))
2778                 {
2779                         if (x1 > x2)
2780                         {
2781                                 float tmp = x1;
2782                                 x1 = x2;
2783                                 x2 = tmp;
2784                                 switch (gui->crop_handle)
2785                                 {
2786                                         case 0: gui->crop_handle = 1; break;
2787                                         case 1: gui->crop_handle = 0; break;
2788                                         case 2: gui->crop_handle = 3; break;
2789                                         case 3: gui->crop_handle = 2; break;
2790                                         default: break;
2791                                 }
2792                         }
2793
2794                         if (y1 > y2)
2795                         {
2796                                 float tmp = y1;
2797                                 y1 = y2;
2798                                 y2 = tmp;
2799                                 switch (gui->crop_handle)
2800                                 {
2801                                         case 0: gui->crop_handle = 2; break;
2802                                         case 1: gui->crop_handle = 3; break;
2803                                         case 2: gui->crop_handle = 0; break;
2804                                         case 3: gui->crop_handle = 1; break;
2805                                         default: break;
2806                                 }
2807                         }
2808
2809                         mwindow->edl->session->crop_x1 = (int)x1;
2810                         mwindow->edl->session->crop_y1 = (int)y1;
2811                         mwindow->edl->session->crop_x2 = (int)x2;
2812                         mwindow->edl->session->crop_y2 = (int)y2;
2813                         result = 1;
2814                         redraw = 1;
2815                 }
2816         }
2817         else
2818 // Update cursor font
2819         if(handle_selected >= 0)
2820         {
2821                 switch(handle_selected)
2822                 {
2823                         case 0:
2824                                 set_cursor(UPLEFT_RESIZE);
2825                                 break;
2826                         case 1:
2827                                 set_cursor(UPRIGHT_RESIZE);
2828                                 break;
2829                         case 2:
2830                                 set_cursor(DOWNLEFT_RESIZE);
2831                                 break;
2832                         case 3:
2833                                 set_cursor(DOWNRIGHT_RESIZE);
2834                                 break;
2835                 }
2836                 result = 1;
2837         }
2838         else
2839         {
2840                 set_cursor(ARROW_CURSOR);
2841         }
2842 #define CLAMP(x, y, z) ((x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x))))
2843
2844         if(redraw)
2845         {
2846                 CLAMP(mwindow->edl->session->crop_x1, 0, mwindow->edl->session->output_w);
2847                 CLAMP(mwindow->edl->session->crop_x2, 0, mwindow->edl->session->output_w);
2848                 CLAMP(mwindow->edl->session->crop_y1, 0, mwindow->edl->session->output_h);
2849                 CLAMP(mwindow->edl->session->crop_y2, 0, mwindow->edl->session->output_h);
2850 // printf("CWindowCanvas::test_crop %d %d %d %d\n",
2851 //      mwindow->edl->session->crop_x2,
2852 //      mwindow->edl->session->crop_y2,
2853 //      mwindow->edl->calculate_output_w(0),
2854 //      mwindow->edl->calculate_output_h(0));
2855         }
2856         return result;
2857 }
2858
2859
2860 void CWindowCanvas::draw_crop()
2861 {
2862         get_canvas()->set_inverse();
2863         get_canvas()->set_color(WHITE);
2864
2865         float x1 = mwindow->edl->session->crop_x1;
2866         float y1 = mwindow->edl->session->crop_y1;
2867         float x2 = mwindow->edl->session->crop_x2;
2868         float y2 = mwindow->edl->session->crop_y2;
2869
2870         output_to_canvas(mwindow->edl, 0, x1, y1);
2871         output_to_canvas(mwindow->edl, 0, x2, y2);
2872
2873         if(x2 - x1 && y2 - y1)
2874                 get_canvas()->draw_rectangle((int)x1,
2875                         (int)y1,
2876                         (int)(x2 - x1),
2877                         (int)(y2 - y1));
2878
2879         draw_crophandle((int)x1, (int)y1);
2880         draw_crophandle((int)x2 - CROPHANDLE_W, (int)y1);
2881         draw_crophandle((int)x1, (int)y2 - CROPHANDLE_H);
2882         draw_crophandle((int)x2 - CROPHANDLE_W, (int)y2 - CROPHANDLE_H);
2883         get_canvas()->set_opaque();
2884 }
2885
2886
2887
2888
2889
2890
2891
2892
2893 void CWindowCanvas::draw_bezier(int do_camera)
2894 {
2895         Track *track = gui->cwindow->calculate_affected_track();
2896
2897         if(!track) return;
2898
2899         float center_x;
2900         float center_y;
2901         float center_z;
2902         int64_t position = track->to_units(
2903                 mwindow->edl->local_session->get_selectionstart(1),
2904                 0);
2905         if( do_camera ) {
2906                 track->automation->get_camera(&center_x,
2907                         &center_y, &center_z, position, PLAY_FORWARD);
2908 // follow image, not camera
2909                 center_x = -center_x;  center_y = -center_y;
2910         }
2911         else
2912                 track->automation->get_projector(&center_x,
2913                         &center_y, &center_z, position, PLAY_FORWARD);
2914
2915 //      center_x += track->track_w / 2;
2916 //      center_y += track->track_h / 2;
2917         center_x += mwindow->edl->session->output_w / 2;
2918         center_y += mwindow->edl->session->output_h / 2;
2919         float track_x1 = center_x - track->track_w / 2 * center_z;
2920         float track_y1 = center_y - track->track_h / 2 * center_z;
2921         float track_x2 = track_x1 + track->track_w * center_z;
2922         float track_y2 = track_y1 + track->track_h * center_z;
2923
2924         output_to_canvas(mwindow->edl, 0, track_x1, track_y1);
2925         output_to_canvas(mwindow->edl, 0, track_x2, track_y2);
2926
2927 #define DRAW_PROJECTION(offset) \
2928         get_canvas()->draw_rectangle((int)track_x1 + offset, \
2929                 (int)track_y1 + offset, \
2930                 (int)(track_x2 - track_x1), \
2931                 (int)(track_y2 - track_y1)); \
2932         get_canvas()->draw_line((int)track_x1 + offset,  \
2933                 (int)track_y1 + offset, \
2934                 (int)track_x2 + offset, \
2935                 (int)track_y2 + offset); \
2936         get_canvas()->draw_line((int)track_x2 + offset,  \
2937                 (int)track_y1 + offset, \
2938                 (int)track_x1 + offset, \
2939                 (int)track_y2 + offset); \
2940
2941
2942 // Drop shadow
2943         get_canvas()->set_color(BLACK);
2944         DRAW_PROJECTION(1);
2945
2946 //      canvas->set_inverse();
2947         if(do_camera)
2948                 get_canvas()->set_color(GREEN);
2949         else
2950                 get_canvas()->set_color(RED);
2951
2952         DRAW_PROJECTION(0);
2953 //      canvas->set_opaque();
2954
2955 }
2956
2957 int CWindowCanvas::test_bezier(int button_press,
2958         int &redraw,
2959         int &redraw_canvas,
2960         int &rerender,
2961         int do_camera)
2962 {
2963         int result = 0;
2964
2965 // Processing drag operation.
2966 // Create keyframe during first cursor motion.
2967         if(!button_press)
2968         {
2969
2970                 float cursor_x = get_cursor_x();
2971                 float cursor_y = get_cursor_y();
2972                 canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
2973
2974                 if(gui->current_operation == CWINDOW_CAMERA ||
2975                         gui->current_operation == CWINDOW_PROJECTOR)
2976                 {
2977                         if(!gui->ctrl_down() && gui->shift_down() && !gui->translating_zoom)
2978                         {
2979                                 gui->translating_zoom = 1;
2980                                 gui->reset_affected();
2981                         }
2982                         else
2983                         if(!gui->ctrl_down() && !gui->shift_down() && gui->translating_zoom)
2984                         {
2985                                 gui->translating_zoom = 0;
2986                                 gui->reset_affected();
2987                         }
2988
2989 // Get target keyframe
2990                         float last_center_x;
2991                         float last_center_y;
2992                         float last_center_z;
2993                         int created;
2994
2995                         if(!gui->affected_x && !gui->affected_y && !gui->affected_z)
2996                         {
2997                                 FloatAutos *affected_x_autos;
2998                                 FloatAutos *affected_y_autos;
2999                                 FloatAutos *affected_z_autos;
3000                                 if(!gui->affected_track) return 0;
3001                                 double position = mwindow->edl->local_session->get_selectionstart(1);
3002                                 int64_t track_position = gui->affected_track->to_units(position, 0);
3003
3004                                 if(mwindow->edl->session->cwindow_operation == CWINDOW_CAMERA)
3005                                 {
3006                                         affected_x_autos = (FloatAutos*)gui->affected_track->automation->autos[AUTOMATION_CAMERA_X];
3007                                         affected_y_autos = (FloatAutos*)gui->affected_track->automation->autos[AUTOMATION_CAMERA_Y];
3008                                         affected_z_autos = (FloatAutos*)gui->affected_track->automation->autos[AUTOMATION_CAMERA_Z];
3009                                 }
3010                                 else
3011                                 {
3012                                         affected_x_autos = (FloatAutos*)gui->affected_track->automation->autos[AUTOMATION_PROJECTOR_X];
3013                                         affected_y_autos = (FloatAutos*)gui->affected_track->automation->autos[AUTOMATION_PROJECTOR_Y];
3014                                         affected_z_autos = (FloatAutos*)gui->affected_track->automation->autos[AUTOMATION_PROJECTOR_Z];
3015                                 }
3016
3017
3018                                 if(gui->translating_zoom)
3019                                 {
3020                                         FloatAuto *previous = 0;
3021                                         FloatAuto *next = 0;
3022                                         float new_z = affected_z_autos->get_value(
3023                                                 track_position,
3024                                                 PLAY_FORWARD,
3025                                                 previous,
3026                                                 next);
3027                                         gui->affected_z =
3028                                                 (FloatAuto*)gui->cwindow->calculate_affected_auto(
3029                                                         affected_z_autos, 1, &created, 0);
3030                                         if(created) {
3031                                                 gui->affected_z->set_value(new_z);
3032                                                 redraw_canvas = 1;
3033                                         }
3034                                 }
3035                                 else
3036                                 {
3037                                         FloatAuto *previous = 0;
3038                                         FloatAuto *next = 0;
3039                                         float new_x = affected_x_autos->get_value(
3040                                                 track_position,
3041                                                 PLAY_FORWARD,
3042                                                 previous,
3043                                                 next);
3044                                         previous = 0;
3045                                         next = 0;
3046                                         float new_y = affected_y_autos->get_value(
3047                                                 track_position,
3048                                                 PLAY_FORWARD,
3049                                                 previous,
3050                                                 next);
3051                                         gui->affected_x =
3052                                                 (FloatAuto*)gui->cwindow->calculate_affected_auto(
3053                                                         affected_x_autos, 1, &created, 0);
3054                                         if(created) {
3055                                                 gui->affected_x->set_value(new_x);
3056                                                 redraw_canvas = 1;
3057                                         }
3058                                         gui->affected_y =
3059                                                 (FloatAuto*)gui->cwindow->calculate_affected_auto(
3060                                                         affected_y_autos, 1, &created, 0);
3061                                         if(created) {
3062                                                 gui->affected_y->set_value(new_y);
3063                                                 redraw_canvas = 1;
3064                                         }
3065                                 }
3066
3067                                 calculate_origin();
3068
3069                                 if(gui->translating_zoom)
3070                                 {
3071                                         gui->center_z = gui->affected_z->get_value();
3072                                 }
3073                                 else
3074                                 {
3075                                         gui->center_x = gui->affected_x->get_value();
3076                                         gui->center_y = gui->affected_y->get_value();
3077                                 }
3078
3079                                 rerender = 1;
3080                                 redraw = 1;
3081                         }
3082
3083                         if(gui->translating_zoom)
3084                         {
3085                                 last_center_z = gui->affected_z->get_value();
3086                                 float z = gui->center_z + (cursor_y - gui->y_origin) / 128;
3087                                 if( z < 0 ) z = 0;
3088                                 if(!EQUIV(last_center_z, z))
3089                                 {
3090                                         rerender = 1;
3091                                         redraw = 1;
3092                                         redraw_canvas = 1;
3093                                 }
3094                                 gui->affected_z->set_value(z);
3095                         }
3096                         else
3097                         {
3098                                 last_center_x = gui->affected_x->get_value();
3099                                 last_center_y = gui->affected_y->get_value();
3100                                 float dx = cursor_x - gui->x_origin;
3101                                 float dy = cursor_y - gui->y_origin;
3102 // follow image, not camera
3103                                 if(gui->current_operation == CWINDOW_CAMERA ) {
3104                                         dx = -dx;  dy = -dy;
3105                                 }
3106                                 float x = gui->center_x + dx;
3107                                 float y = gui->center_y + dy;
3108                                 gui->affected_x->set_value(x);
3109                                 gui->affected_y->set_value(y);
3110                                 if( !EQUIV(last_center_x, x) || !EQUIV(last_center_y, y) )
3111                                 {
3112                                         rerender = 1;
3113                                         redraw = 1;
3114                                         redraw_canvas = 1;
3115                                 }
3116                                 gui->affected_x->set_value(x);
3117                                 gui->affected_y->set_value(y);
3118                         }
3119                 }
3120
3121                 result = 1;
3122         }
3123         else
3124 // Begin drag operation.  Don't create keyframe here.
3125         {
3126 // Get affected track off of the first recordable video track.
3127 // Calculating based on the alpha channel would require recording what layer
3128 // each output pixel belongs to as they're rendered and stacked.  Forget it.
3129                 gui->affected_track = gui->cwindow->calculate_affected_track();
3130                 gui->reset_affected();
3131
3132                 if(gui->affected_track)
3133                 {
3134                         if( do_camera )
3135                                 mwindow->undo->update_undo_before(_("camera"), this);
3136                         else
3137                                 mwindow->undo->update_undo_before(_("projector"), this);
3138
3139                         gui->current_operation =
3140                                 mwindow->edl->session->cwindow_operation;
3141                         gui->tool_panel->raise_window();
3142                         result = 1;
3143                 }
3144         }
3145
3146         return result;
3147 }
3148
3149
3150 int CWindowCanvas::test_zoom(int &redraw)
3151 {
3152         int result = 0;
3153         float x, y;
3154         float zoom = 0;
3155
3156         if( mwindow->edl->session->cwindow_scrollbars ) {
3157                 if( *gui->zoom_panel->get_text() != 'x' ) {
3158 // Find current zoom in table
3159                         int idx = total_zooms;  float old_zoom = get_zoom();
3160                         while( --idx >= 0 && !EQUIV(my_zoom_table[idx], old_zoom) );
3161                         if( idx >= 0 ) {
3162                                 idx += get_buttonpress() == 5 ||
3163                                          gui->ctrl_down() || gui->shift_down() ?  -1 : +1 ;
3164                                 CLAMP(idx, 0, total_zooms-1);
3165                                 zoom = my_zoom_table[idx];
3166                         }
3167                 }
3168                 x = get_cursor_x();  y = get_cursor_y();
3169                 if( !zoom ) {
3170                         mwindow->edl->session->cwindow_scrollbars = 0;
3171                         gui->zoom_panel->update(0);
3172                         zoom = gui->get_auto_zoom();
3173                 }
3174                 else {
3175         &