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