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