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