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