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