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