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