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