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