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