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