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