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