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