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