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