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