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