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