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