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