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