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