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