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