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