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