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