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