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