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