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