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