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