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