add 1:1 convert, add es.po: thx sergio, cwdw zoom tweak, add done beep pots, bd forma...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / cwindowgui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "automation.h"
23 #include "autos.h"
24 #include "bcsignals.h"
25 #include "canvas.h"
26 #include "clip.h"
27 #include "cpanel.h"
28 #include "cplayback.h"
29 #include "ctimebar.h"
30 #include "cursors.h"
31 #include "cwindowgui.h"
32 #include "cwindow.h"
33 #include "cwindowtool.h"
34 #include "editpanel.h"
35 #include "edl.h"
36 #include "edlsession.h"
37 #include "floatauto.h"
38 #include "floatautos.h"
39 #include "keys.h"
40 #include "language.h"
41 #include "localsession.h"
42 #include "mainclock.h"
43 #include "mainmenu.h"
44 #include "mainundo.h"
45 #include "mainsession.h"
46 #include "maskauto.h"
47 #include "maskautos.h"
48 #include "mbuttons.h"
49 #include "meterpanel.h"
50 #include "mwindowgui.h"
51 #include "mwindow.h"
52 #include "mwindow.h"
53 #include "playback3d.h"
54 #include "playtransport.h"
55 #include "theme.h"
56 #include "trackcanvas.h"
57 #include "tracks.h"
58 #include "transportque.h"
59 #include "vtrack.h"
60
61
62 static double my_zoom_table[] =
63 {
64         0.25,
65         0.33,
66         0.50,
67         0.75,
68         1.0,
69         1.5,
70         2.0,
71         3.0,
72         4.0
73 };
74
75 static int total_zooms = sizeof(my_zoom_table) / sizeof(double);
76
77
78 CWindowGUI::CWindowGUI(MWindow *mwindow, CWindow *cwindow)
79  : BC_Window(_(PROGRAM_NAME ": Compositor"),
80         mwindow->session->cwindow_x,
81         mwindow->session->cwindow_y,
82         mwindow->session->cwindow_w,
83         mwindow->session->cwindow_h,
84         xS(100), yS(100), 1, 1, 1,
85         BC_WindowBase::get_resources()->bg_color,
86         mwindow->get_cwindow_display())
87 {
88         this->mwindow = mwindow;
89         this->cwindow = cwindow;
90         affected_track = 0;
91         affected_x = affected_y = affected_z = 0;
92         mask_keyframe = 0;
93         orig_mask_keyframe = new MaskAuto(0, 0);
94         affected_point = 0;
95         x_offset = y_offset = 0;
96         x_origin = y_origin = 0;
97         current_operation = CWINDOW_NONE;
98         tool_panel = 0;
99         active = 0;
100         inactive = 0;
101         crop_handle = -1; crop_translate = 0;
102         crop_origin_x = crop_origin_y = 0;
103         crop_origin_x1 = crop_origin_y1 = 0;
104         crop_origin_x2 = crop_origin_y2 = 0;
105         eyedrop_visible = 0;
106         eyedrop_x = eyedrop_y = 0;
107         ruler_origin_x = ruler_origin_y = 0;
108         ruler_handle = -1; ruler_translate = 0;
109         center_x = center_y = center_z = 0;
110         control_in_x = control_in_y = 0;
111         control_out_x = control_out_y = 0;
112         translating_zoom = 0;
113         highlighted = 0;
114 }
115
116 CWindowGUI::~CWindowGUI()
117 {
118         cwindow->stop_playback(1);
119         if( tool_panel ) delete tool_panel;
120         delete meters;
121         delete composite_panel;
122         delete canvas;
123         delete transport;
124         delete edit_panel;
125         delete zoom_panel;
126         delete active;
127         delete inactive;
128         delete focus_frame;
129         delete orig_mask_keyframe;
130 }
131
132 void CWindowGUI::create_objects()
133 {
134         lock_window("CWindowGUI::create_objects");
135         set_icon(mwindow->theme->get_image("cwindow_icon"));
136
137         active = new BC_Pixmap(this, mwindow->theme->get_image("cwindow_active"));
138         inactive = new BC_Pixmap(this, mwindow->theme->get_image("cwindow_inactive"));
139         focus_frame = new VFramePng(mwindow->theme->get_image_data("cwindow_focus.png"));
140
141         mwindow->theme->get_cwindow_sizes(this, mwindow->session->cwindow_controls);
142         mwindow->theme->draw_cwindow_bg(this);
143         flash();
144
145 // Meters required by composite panel
146         meters = new CWindowMeters(mwindow,
147                 this,
148                 mwindow->theme->cmeter_x,
149                 mwindow->theme->cmeter_y,
150                 mwindow->theme->cmeter_h);
151         meters->create_objects();
152
153
154         composite_panel = new CPanel(mwindow,
155                 this,
156                 mwindow->theme->ccomposite_x,
157                 mwindow->theme->ccomposite_y,
158                 mwindow->theme->ccomposite_w,
159                 mwindow->theme->ccomposite_h);
160         composite_panel->create_objects();
161
162         canvas = new CWindowCanvas(mwindow, this);
163
164         canvas->create_objects(mwindow->edl);
165         canvas->use_cwindow();
166
167
168         add_subwindow(timebar = new CTimeBar(mwindow,
169                 this,
170                 mwindow->theme->ctimebar_x,
171                 mwindow->theme->ctimebar_y,
172                 mwindow->theme->ctimebar_w,
173                 mwindow->theme->ctimebar_h));
174         timebar->create_objects();
175
176 #ifdef USE_SLIDER
177         add_subwindow(slider = new CWindowSlider(mwindow,
178                 cwindow,
179                 mwindow->theme->cslider_x,
180                 mwindow->theme->cslider_y,
181                 mwindow->theme->cslider_w));
182 #endif
183
184         transport = new CWindowTransport(mwindow,
185                 this,
186                 mwindow->theme->ctransport_x,
187                 mwindow->theme->ctransport_y);
188         transport->create_objects();
189 #ifdef USE_SLIDER
190         transport->set_slider(slider);
191 #endif
192
193         edit_panel = new CWindowEditing(mwindow, cwindow);
194         edit_panel->set_meters(meters);
195         edit_panel->create_objects();
196
197 //      add_subwindow(clock = new MainClock(mwindow,
198 //              mwindow->theme->ctime_x,
199 //              mwindow->theme->ctime_y));
200
201         zoom_panel = new CWindowZoom(mwindow,
202                 this,
203                 mwindow->theme->czoom_x,
204                 mwindow->theme->czoom_y,
205                 mwindow->theme->czoom_w);
206         zoom_panel->create_objects();
207         zoom_panel->zoom_text->add_item(new BC_MenuItem(auto_zoom = _(AUTO_ZOOM)));
208         if( !mwindow->edl->session->cwindow_scrollbars )
209                 zoom_panel->set_text(auto_zoom);
210
211 // Must create after meter panel
212         tool_panel = new CWindowTool(mwindow, this);
213         tool_panel->Thread::start();
214
215         set_operation(mwindow->edl->session->cwindow_operation);
216         draw_status(0);
217         unlock_window();
218 }
219
220 int CWindowGUI::translation_event()
221 {
222         mwindow->session->cwindow_x = get_x();
223         mwindow->session->cwindow_y = get_y();
224         return 0;
225 }
226
227 int CWindowGUI::resize_event(int w, int h)
228 {
229         mwindow->session->cwindow_x = get_x();
230         mwindow->session->cwindow_y = get_y();
231         mwindow->session->cwindow_w = w;
232         mwindow->session->cwindow_h = h;
233
234         mwindow->theme->get_cwindow_sizes(this, mwindow->session->cwindow_controls);
235         mwindow->theme->draw_cwindow_bg(this);
236         flash(0);
237
238         composite_panel->reposition_buttons(mwindow->theme->ccomposite_x,
239                 mwindow->theme->ccomposite_y, mwindow->theme->ccomposite_h);
240
241         canvas->set_zoom(mwindow->edl, canvas->get_zoom());
242         update_canvas();
243
244         timebar->resize_event();
245
246 #ifdef USE_SLIDER
247         slider->reposition_window(mwindow->theme->cslider_x,
248                 mwindow->theme->cslider_y,
249                 mwindow->theme->cslider_w);
250 // Recalibrate pointer motion range
251         slider->set_position();
252 #endif
253
254         transport->reposition_buttons(mwindow->theme->ctransport_x,
255                 mwindow->theme->ctransport_y);
256
257         edit_panel->reposition_buttons(mwindow->theme->cedit_x,
258                 mwindow->theme->cedit_y);
259
260 //      clock->reposition_window(mwindow->theme->ctime_x,
261 //              mwindow->theme->ctime_y);
262
263         zoom_panel->reposition_window(mwindow->theme->czoom_x,
264                 mwindow->theme->czoom_y);
265
266 //      destination->reposition_window(mwindow->theme->cdest_x,
267 //              mwindow->theme->cdest_y);
268
269         meters->reposition_window(mwindow->theme->cmeter_x,
270                 mwindow->theme->cmeter_y,
271                 -1,
272                 mwindow->theme->cmeter_h);
273
274         draw_status(0);
275
276         BC_WindowBase::resize_event(w, h);
277         return 1;
278 }
279
280 int CWindowGUI::button_press_event()
281 {
282         if( current_operation == CWINDOW_NONE &&
283             mwindow->edl != 0 && canvas->get_canvas() &&
284             mwindow->edl->session->cwindow_click2play &&
285             canvas->get_canvas()->get_cursor_over_window() ) {
286                 switch( get_buttonpress() ) {
287                 case LEFT_BUTTON:
288                         if( !cwindow->playback_engine->is_playing_back ) {
289                                 double length = mwindow->edl->tracks->total_playable_length();
290                                 double position = cwindow->playback_engine->get_tracking_position();
291                                 if( position >= length ) transport->goto_start();
292                         }
293                         return transport->forward_play->handle_event();
294                 case MIDDLE_BUTTON:
295                         if( !cwindow->playback_engine->is_playing_back ) {
296                                 double position = cwindow->playback_engine->get_tracking_position();
297                                 if( position <= 0 ) transport->goto_end();
298                         }
299                         return transport->reverse_play->handle_event();
300                 case RIGHT_BUTTON:  // activates popup
301                         break;
302                 case WHEEL_UP:
303                         return transport->frame_forward_play->handle_event();
304                 case WHEEL_DOWN:
305                         return transport->frame_reverse_play->handle_event();
306                 }
307         }
308         if( canvas->get_canvas() )
309                 return canvas->button_press_event_base(canvas->get_canvas());
310         return 0;
311 }
312
313 int CWindowGUI::cursor_leave_event()
314 {
315         if( canvas->get_canvas() )
316                 return canvas->cursor_leave_event_base(canvas->get_canvas());
317         return 0;
318 }
319
320 int CWindowGUI::cursor_enter_event()
321 {
322         if( canvas->get_canvas() )
323                 return canvas->cursor_enter_event_base(canvas->get_canvas());
324         return 0;
325 }
326
327 int CWindowGUI::button_release_event()
328 {
329         if( canvas->get_canvas() )
330                 return canvas->button_release_event();
331         return 0;
332 }
333
334 int CWindowGUI::cursor_motion_event()
335 {
336         if( canvas->get_canvas() ) {
337                 canvas->get_canvas()->unhide_cursor();
338                 return canvas->cursor_motion_event();
339         }
340         return 0;
341 }
342
343
344
345
346
347
348
349 void CWindowGUI::draw_status(int flush)
350 {
351         if( (canvas->get_canvas() && canvas->get_canvas()->get_video_on()) ||
352                 canvas->is_processing ) {
353                 draw_pixmap(active,
354                         mwindow->theme->cstatus_x,
355                         mwindow->theme->cstatus_y);
356         }
357         else {
358                 draw_pixmap(inactive,
359                         mwindow->theme->cstatus_x,
360                         mwindow->theme->cstatus_y);
361         }
362
363
364 // printf("CWindowGUI::draw_status %d %d %d\n", __LINE__, mwindow->theme->cstatus_x,
365 //              mwindow->theme->cstatus_y);
366         flash(mwindow->theme->cstatus_x,
367                 mwindow->theme->cstatus_y,
368                 active->get_w(),
369                 active->get_h(),
370                 flush);
371 }
372
373 void CWindowGUI::update_canvas(int redraw)
374 {
375         float zoom = canvas->get_zoom();
376         zoom_panel->update(zoom);
377         if( mwindow->edl->session->cwindow_operation == CWINDOW_ZOOM ) {
378                 if( !zoom ) zoom = canvas->get_auto_zoom(mwindow->edl);
379                 composite_panel->cpanel_zoom->update(zoom);
380         }
381         canvas->update_scrollbars(mwindow->edl, 0);
382         canvas->reposition_window(mwindow->edl,
383                 mwindow->theme->ccanvas_x, mwindow->theme->ccanvas_y,
384                 mwindow->theme->ccanvas_w, mwindow->theme->ccanvas_h);
385         if( redraw )
386                 canvas->refresh(0);
387 }
388
389 void CWindowGUI::set_operation(int value)
390 {
391         switch( value ) {
392         case CWINDOW_TOOL_WINDOW:
393                 mwindow->edl->session->tool_window = !mwindow->edl->session->tool_window;
394                 composite_panel->operation[CWINDOW_TOOL_WINDOW]->update(mwindow->edl->session->tool_window);
395                 tool_panel->update_show_window();
396                 return;
397         case CWINDOW_TITLESAFE:
398                 mwindow->edl->session->safe_regions = !mwindow->edl->session->safe_regions;
399                 composite_panel->operation[CWINDOW_TITLESAFE]->update(mwindow->edl->session->safe_regions);
400                 value = mwindow->edl->session->cwindow_operation;
401                 break;
402         default:
403                 mwindow->edl->session->cwindow_operation = value;
404                 composite_panel->set_operation(value);
405                 break;
406         }
407
408         edit_panel->update();
409         tool_panel->start_tool(value);
410         canvas->refresh(0);
411 }
412
413 void CWindowGUI::update_tool()
414 {
415         tool_panel->update_values();
416 }
417
418 int CWindowGUI::close_event()
419 {
420         cwindow->hide_window();
421         return 1;
422 }
423
424
425 int CWindowGUI::keypress_event()
426 {
427         int result = 0;
428         int cwindow_operation = CWINDOW_NONE;
429
430         switch( get_keypress() ) {
431         case 'w':
432         case 'W':
433                 close_event();
434                 result = 1;
435                 break;
436         case '+':
437         case '=':
438                 keyboard_zoomin();
439                 result = 1;
440                 break;
441         case '-':
442                 keyboard_zoomout();
443                 result = 1;
444                 break;
445         case 'f':
446                 unlock_window();
447                 canvas->use_fullscreen(canvas->get_fullscreen() ? 0 : 1);
448                 lock_window("CWindowGUI::keypress_event 1");
449                 result = 1;
450                 break;
451         case 'x':
452                 if( ctrl_down() || shift_down() || alt_down() ) break;
453                 unlock_window();
454                 mwindow->gui->lock_window("CWindowGUI::keypress_event 2");
455                 mwindow->cut();
456                 mwindow->gui->unlock_window();
457                 lock_window("CWindowGUI::keypress_event 2");
458                 result = 1;
459                 break;
460         case DELETE:
461                 unlock_window();
462                 mwindow->gui->lock_window("CWindowGUI::keypress_event 2");
463                 mwindow->clear_entry();
464                 mwindow->gui->unlock_window();
465                 lock_window("CWindowGUI::keypress_event 3");
466                 result = 1;
467                 break;
468         case ESC:
469                 unlock_window();
470                 canvas->use_fullscreen(0);
471                 lock_window("CWindowGUI::keypress_event 4");
472                 result = 1;
473                 break;
474         case LEFT:
475                 if( !ctrl_down() ) {
476                         int alt_down = this->alt_down();
477                         int shift_down = this->shift_down();
478                         unlock_window();
479                         stop_transport(0);
480                         mwindow->gui->lock_window("CWindowGUI::keypress_event 5");
481                         if( alt_down )
482                                 mwindow->prev_edit_handle(shift_down);
483                         else
484                                 mwindow->move_left();
485                         mwindow->gui->unlock_window();
486                         lock_window("CWindowGUI::keypress_event 6");
487                         result = 1;
488                 }
489                 break;
490
491         case ',':
492                 if( !ctrl_down() && !alt_down() ) {
493                         unlock_window();
494                         stop_transport(0);
495                         mwindow->gui->lock_window("CWindowGUI::keypress_event 7");
496                         mwindow->move_left();
497                         mwindow->gui->unlock_window();
498                         lock_window("CWindowGUI::keypress_event 8");
499                         result = 1;
500                 }
501                 break;
502
503         case RIGHT:
504                 if( !ctrl_down() ) {
505                         int alt_down = this->alt_down();
506                         int shift_down = this->shift_down();
507                         unlock_window();
508                         stop_transport(0);
509                         mwindow->gui->lock_window("CWindowGUI::keypress_event 8");
510                         if( alt_down )
511                                 mwindow->next_edit_handle(shift_down);
512                         else
513                                 mwindow->move_right();
514                         mwindow->gui->unlock_window();
515                         lock_window("CWindowGUI::keypress_event 9");
516                         result = 1;
517                 }
518                 break;
519
520         case '.':
521                 if( !ctrl_down() && !alt_down() ) {
522                         unlock_window();
523                         stop_transport(0);
524                         mwindow->gui->lock_window("CWindowGUI::keypress_event 10");
525                         mwindow->move_right();
526                         mwindow->gui->unlock_window();
527                         lock_window("CWindowGUI::keypress_event 11");
528                         result = 1;
529                 }
530                 break;
531         }
532
533         if( !result && cwindow_operation < 0 && ctrl_down() && shift_down() ) {
534                 switch( get_keypress() ) {
535                 case KEY_F1 ... KEY_F4: // mainmenu, load layout
536                         resend_event(mwindow->gui);
537                         result = 1;
538                         break;
539                 }
540         }
541         if( !result && !ctrl_down() ) {
542                 switch( get_keypress() ) {
543                 case KEY_F1:
544                         if( shift_down() ) {
545                                 mwindow->toggle_camera_xyz();  result = 1;
546                         }
547                         else
548                                 cwindow_operation = CWINDOW_PROTECT;
549                         break;
550                 case KEY_F2:
551                         if( shift_down() ) {
552                                 mwindow->toggle_projector_xyz();  result = 1;
553                         }
554                         else
555                                 cwindow_operation = CWINDOW_ZOOM;
556                         break;
557                 }
558         }
559         if( !result && cwindow_operation < 0 && !ctrl_down() && !shift_down() ) {
560                 switch( get_keypress() ) {
561                 case KEY_F3:    cwindow_operation = CWINDOW_MASK;       break;
562                 case KEY_F4:    cwindow_operation = CWINDOW_RULER;      break;
563                 case KEY_F5:    cwindow_operation = CWINDOW_CAMERA;     break;
564                 case KEY_F6:    cwindow_operation = CWINDOW_PROJECTOR;  break;
565                 case KEY_F7:    cwindow_operation = CWINDOW_CROP;       break;
566                 case KEY_F8:    cwindow_operation = CWINDOW_EYEDROP;    break;
567                 case KEY_F9:    cwindow_operation = CWINDOW_TOOL_WINDOW; break;
568                 case KEY_F10:   cwindow_operation = CWINDOW_TITLESAFE;  break;
569                 }
570         }
571         if( !result && cwindow_operation < 0 && !ctrl_down() ) {
572                 switch( get_keypress() ) {
573                 case KEY_F11:
574                         if( !shift_down() )
575                                 canvas->reset_camera();
576                         else
577                                 canvas->camera_keyframe();
578                         result = 1;
579                         break;
580                 case KEY_F12:
581                         if( !shift_down() )
582                                 canvas->reset_projector();
583                         else
584                                 canvas->projector_keyframe();
585                         result = 1;
586                         break;
587                 }
588         }
589
590         if( cwindow_operation >= 0 ) {
591                 set_operation(cwindow_operation);
592                 result = 1;
593         }
594
595         if( !result )
596                 result = transport->keypress_event();
597
598         return result;
599 }
600
601
602 void CWindowGUI::reset_affected()
603 {
604         affected_x = 0;
605         affected_y = 0;
606         affected_z = 0;
607 }
608
609 void CWindowGUI::keyboard_zoomin()
610 {
611         zoom_panel->zoom_tumbler->handle_up_event();
612 }
613
614 void CWindowGUI::keyboard_zoomout()
615 {
616         zoom_panel->zoom_tumbler->handle_down_event();
617 }
618
619 void CWindowGUI::sync_parameters(int change_type, int redraw, int overlay)
620 {
621         if( redraw ) {
622                 update_tool();
623                 canvas->refresh(1);
624         }
625         if( change_type < 0 && !overlay ) return;
626         unlock_window();
627         if( change_type >= 0 ) {
628                 mwindow->restart_brender();
629                 mwindow->sync_parameters(change_type);
630         }
631         if( overlay ) {
632                 mwindow->gui->lock_window("CWindowGUI::sync_parameters");
633                 mwindow->gui->draw_overlays(1);
634                 mwindow->gui->unlock_window();
635         }
636         lock_window("CWindowGUI::sync_parameters");
637 }
638
639 void CWindowGUI::drag_motion()
640 {
641         if( get_hidden() ) return;
642
643         if( mwindow->session->current_operation != DRAG_ASSET &&
644                 mwindow->session->current_operation != DRAG_VTRANSITION &&
645                 mwindow->session->current_operation != DRAG_VEFFECT ) return;
646         int need_highlight = cursor_above() && get_cursor_over_window();
647         if( highlighted == need_highlight ) return;
648         highlighted = need_highlight;
649         canvas->refresh(1);
650 }
651
652 int CWindowGUI::drag_stop()
653 {
654         int result = 0;
655         if( get_hidden() ) return 0;
656         if( !highlighted ) return 0;
657         if( mwindow->session->current_operation != DRAG_ASSET &&
658             mwindow->session->current_operation != DRAG_VTRANSITION &&
659             mwindow->session->current_operation != DRAG_VEFFECT) return 0;
660         highlighted = 0;
661         canvas->refresh(1);
662         result = 1;
663
664         if( mwindow->session->current_operation == DRAG_ASSET ) {
665                 if( mwindow->session->drag_assets->total ||
666                         mwindow->session->drag_clips->total ) {
667                         mwindow->gui->lock_window("CWindowGUI::drag_stop 1");
668                         mwindow->undo->update_undo_before(_("insert assets"), 0);
669                 }
670
671                 if( mwindow->session->drag_assets->total ) {
672                         mwindow->clear(0);
673                         mwindow->load_assets(mwindow->session->drag_assets,
674                                 mwindow->edl->local_session->get_selectionstart(),
675                                 LOADMODE_PASTE,
676                                 mwindow->session->track_highlighted,
677                                 0,
678                                 mwindow->edl->session->labels_follow_edits,
679                                 mwindow->edl->session->plugins_follow_edits,
680                                 mwindow->edl->session->autos_follow_edits,
681                                 0); // overwrite
682                 }
683
684                 if( mwindow->session->drag_clips->total ) {
685                         mwindow->clear(0);
686                         mwindow->paste_edls(mwindow->session->drag_clips,
687                                 LOADMODE_PASTE,
688                                 mwindow->session->track_highlighted,
689                                 mwindow->edl->local_session->get_selectionstart(),
690                                 mwindow->edl->session->labels_follow_edits,
691                                 mwindow->edl->session->plugins_follow_edits,
692                                 mwindow->edl->session->autos_follow_edits,
693                                 0); // overwrite
694                 }
695
696                 if( mwindow->session->drag_assets->total ||
697                         mwindow->session->drag_clips->total ) {
698                         mwindow->save_backup();
699                         mwindow->restart_brender();
700                         mwindow->gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
701                         mwindow->undo->update_undo_after(_("insert assets"), LOAD_ALL);
702                         mwindow->gui->unlock_window();
703                         sync_parameters(CHANGE_ALL);
704                 }
705         }
706
707         if( mwindow->session->current_operation == DRAG_VEFFECT ) {
708 //printf("CWindowGUI::drag_stop 1\n");
709                 Track *affected_track = cwindow->calculate_affected_track();
710 //printf("CWindowGUI::drag_stop 2\n");
711
712                 mwindow->gui->lock_window("CWindowGUI::drag_stop 3");
713                 mwindow->insert_effects_cwindow(affected_track);
714                 mwindow->session->current_operation = NO_OPERATION;
715                 mwindow->gui->unlock_window();
716         }
717
718         if( mwindow->session->current_operation == DRAG_VTRANSITION ) {
719                 Track *affected_track = cwindow->calculate_affected_track();
720                 mwindow->gui->lock_window("CWindowGUI::drag_stop 4");
721                 mwindow->paste_transition_cwindow(affected_track);
722                 mwindow->session->current_operation = NO_OPERATION;
723                 mwindow->gui->unlock_window();
724         }
725
726         return result;
727 }
728
729 void CWindowGUI::update_meters()
730 {
731         if( mwindow->edl->session->cwindow_meter != meters->visible ) {
732                 meters->set_meters(meters->meter_count, mwindow->edl->session->cwindow_meter);
733                 mwindow->theme->get_cwindow_sizes(this, mwindow->session->cwindow_controls);
734                 resize_event(get_w(), get_h());
735         }
736 }
737
738 void CWindowGUI::stop_transport(const char *lock_msg)
739 {
740         if( lock_msg ) unlock_window();
741         mwindow->stop_transport();
742         if( lock_msg ) lock_window(lock_msg);
743 }
744
745
746 CWindowEditing::CWindowEditing(MWindow *mwindow, CWindow *cwindow)
747  : EditPanel(mwindow, cwindow->gui, CWINDOW_ID,
748                 mwindow->theme->cedit_x, mwindow->theme->cedit_y,
749                 mwindow->edl->session->editing_mode,
750                 0, // use_editing_mode
751                 1, // use_keyframe
752                 0, // use_splice
753                 0, // use_overwrite
754                 1, // use_copy
755                 1, // use_paste
756                 1, // use_undo
757                 0, // use_fit
758                 0, // locklabels
759                 1, // use_labels
760                 1, // use_toclip
761                 1, // use_meters
762                 1, // use_cut
763                 0, // use_commerical
764                 0, // use_goto
765                 1) // use_clk2play
766 {
767         this->mwindow = mwindow;
768         this->cwindow = cwindow;
769 }
770
771 #define relock_cm(s) \
772  cwindow->gui->unlock_window(); \
773  mwindow->gui->lock_window("CWindowEditing::" s)
774 #define relock_mc(s) \
775  mwindow->gui->unlock_window(); \
776  cwindow->gui->lock_window("CWindowEditing::" s)
777 #define panel_fn(fn, args, s) \
778  CWindowEditing::fn args { relock_cm(#fn); s; relock_mc(#fn); }
779
780 // transmit lock to mwindow, and run mbutton->edit_panel->s
781 #define panel_btn(fn, args, s) \
782  panel_fn(panel_##fn, args, mwindow->gui->mbuttons->edit_panel->panel_##s)
783
784
785 double CWindowEditing::get_position()
786 {
787         relock_cm("get_position");
788         double ret = mwindow->edl->local_session->get_selectionstart(1);
789         relock_mc("get_position");
790         return ret;
791 }
792
793 void CWindowEditing::set_position(double position)
794 {
795         relock_cm("set_position");
796         mwindow->gui->mbuttons->edit_panel->set_position(position);
797         relock_mc("set_position");
798 }
799
800 void CWindowEditing::set_click_to_play(int v)
801 {
802         relock_cm("set_position");
803         mwindow->edl->session->cwindow_click2play = v;
804         relock_mc("set_position");
805         click2play->update(v);
806 }
807
808
809 void panel_btn(stop_transport,(), stop_transport())
810 void panel_btn(toggle_label,(), toggle_label())
811 void panel_btn(next_label,(int cut), next_label(cut))
812 void panel_btn(prev_label,(int cut), prev_label(cut))
813 void panel_btn(next_edit,(int cut), next_edit(cut))
814 void panel_btn(prev_edit,(int cut), prev_edit(cut))
815 void panel_fn(panel_copy_selection,(), mwindow->copy())
816 void CWindowEditing::panel_overwrite_selection() {} // not used
817 void CWindowEditing::panel_splice_selection() {} // not used
818 void panel_btn(set_inpoint,(), set_inpoint())
819 void panel_btn(set_outpoint,(), set_outpoint())
820 void panel_btn(unset_inoutpoint,(), unset_inoutpoint())
821 void panel_fn(panel_to_clip,(), mwindow->to_clip(mwindow->edl, _("main window: "), 0))
822 void panel_btn(cut,(), cut())
823 void panel_btn(paste,(), paste())
824 void panel_btn(fit_selection,(), fit_selection())
825 void panel_btn(fit_autos,(int all), fit_autos(all))
826 void panel_btn(set_editing_mode,(int mode), set_editing_mode(mode))
827 void panel_btn(set_auto_keyframes,(int v), set_auto_keyframes(v))
828 void panel_btn(set_span_keyframes,(int v), set_span_keyframes(v))
829 void panel_btn(set_labels_follow_edits,(int v), set_labels_follow_edits(v))
830
831
832 CWindowMeters::CWindowMeters(MWindow *mwindow,
833         CWindowGUI *gui, int x, int y, int h)
834  : MeterPanel(mwindow, gui, x, y, -1, h,
835                 mwindow->edl->session->audio_channels,
836                 mwindow->edl->session->cwindow_meter,
837                 0, 0)
838 {
839         this->mwindow = mwindow;
840         this->gui = gui;
841 }
842
843 CWindowMeters::~CWindowMeters()
844 {
845 }
846
847 int CWindowMeters::change_status_event(int new_status)
848 {
849         mwindow->edl->session->cwindow_meter = new_status;
850         gui->update_meters();
851         return 1;
852 }
853
854
855
856
857 CWindowZoom::CWindowZoom(MWindow *mwindow, CWindowGUI *gui, int x, int y, int w)
858  : ZoomPanel(mwindow, gui, (double)mwindow->edl->session->cwindow_zoom,
859         x, y, w, my_zoom_table, total_zooms, ZOOM_PERCENTAGE)
860 {
861         this->mwindow = mwindow;
862         this->gui = gui;
863 }
864
865 CWindowZoom::~CWindowZoom()
866 {
867 }
868
869 void CWindowZoom::update(double value)
870 {
871         char string[BCSTRLEN];
872         const char *cp = string;
873         if( value ) {
874                 this->value = value;
875                 int frac = value >= 1.0f ? 1 :
876                            value >= 0.1f ? 2 :
877                            value >= .01f ? 3 : 4;
878                 sprintf(string, "x %.*f", frac, value);
879         }
880         else
881                 cp = gui->auto_zoom;
882         ZoomPanel::update(cp);
883 }
884
885 int CWindowZoom::handle_event()
886 {
887         double value = !strcasecmp(gui->auto_zoom, get_text()) ? 0 : get_value();
888         gui->canvas->set_zoom(mwindow->edl, value);
889         gui->update_canvas();
890         return 1;
891 }
892
893
894
895 #ifdef USE_SLIDER
896 CWindowSlider::CWindowSlider(MWindow *mwindow, CWindow *cwindow, int x, int y, int pixels)
897  : BC_PercentageSlider(x, y, 0, pixels, pixels, 0, 1, 0)
898 {
899         this->mwindow = mwindow;
900         this->cwindow = cwindow;
901         set_precision(0.00001);
902 }
903
904 CWindowSlider::~CWindowSlider()
905 {
906 }
907
908 int CWindowSlider::handle_event()
909 {
910         cwindow->update_position((double)get_value());
911         return 1;
912 }
913
914 void CWindowSlider::set_position()
915 {
916         double new_length = mwindow->edl->tracks->total_length();
917         update(mwindow->theme->cslider_w,
918                 mwindow->edl->local_session->get_selectionstart(1),
919                 0, new_length);
920 }
921
922
923 int CWindowSlider::increase_value()
924 {
925         unlock_window();
926         cwindow->gui->transport->handle_transport(SINGLE_FRAME_FWD);
927         lock_window("CWindowSlider::increase_value");
928         return 1;
929 }
930
931 int CWindowSlider::decrease_value()
932 {
933         unlock_window();
934         cwindow->gui->transport->handle_transport(SINGLE_FRAME_REWIND);
935         lock_window("CWindowSlider::decrease_value");
936         return 1;
937 }
938 #endif // USE_SLIDER
939
940
941 CWindowTransport::CWindowTransport(MWindow *mwindow,
942         CWindowGUI *gui, int x, int y)
943  : PlayTransport(mwindow, gui, x, y)
944 {
945         this->gui = gui;
946 }
947
948 EDL* CWindowTransport::get_edl()
949 {
950         return mwindow->edl;
951 }
952
953 void CWindowTransport::goto_start()
954 {
955         gui->unlock_window();
956         handle_transport(REWIND, 1);
957
958         mwindow->gui->lock_window("CWindowTransport::goto_start 1");
959         mwindow->goto_start();
960         mwindow->gui->unlock_window();
961
962         gui->lock_window("CWindowTransport::goto_start 2");
963 }
964
965 void CWindowTransport::goto_end()
966 {
967         gui->unlock_window();
968         handle_transport(GOTO_END, 1);
969
970         mwindow->gui->lock_window("CWindowTransport::goto_end 1");
971         mwindow->goto_end();
972         mwindow->gui->unlock_window();
973
974         gui->lock_window("CWindowTransport::goto_end 2");
975 }
976
977
978
979 CWindowCanvas::CWindowCanvas(MWindow *mwindow, CWindowGUI *gui)
980  : Canvas(mwindow, gui,
981         mwindow->theme->ccanvas_x, mwindow->theme->ccanvas_y,
982         mwindow->theme->ccanvas_w, mwindow->theme->ccanvas_h,
983         0, 0, mwindow->edl->session->cwindow_scrollbars)
984 {
985         this->mwindow = mwindow;
986         this->gui = gui;
987 }
988
989 void CWindowCanvas::status_event()
990 {
991         gui->draw_status(1);
992 }
993
994 void CWindowCanvas::update_zoom(int x, int y, float zoom)
995 {
996         mwindow->edl->session->cwindow_xscroll = x;
997         mwindow->edl->session->cwindow_yscroll = y;
998         mwindow->edl->session->cwindow_zoom = zoom;
999         use_scrollbars = !zoom ? 0 : 1;
1000         mwindow->edl->session->cwindow_scrollbars = use_scrollbars;
1001 }
1002
1003 int CWindowCanvas::use_fullscreen(int on)
1004 {
1005         if( Canvas::use_fullscreen(on) ) {
1006                 gui->lock_window("CWindowCanvas::use_fullscreen");
1007                 zoom_auto();
1008                 if( !on ) gui->zoom_panel->update(0);
1009                 gui->unlock_window();
1010         }
1011         return 1;
1012 }
1013
1014 int CWindowCanvas::get_xscroll()
1015 {
1016         return mwindow->edl->session->cwindow_xscroll;
1017 }
1018
1019 int CWindowCanvas::get_yscroll()
1020 {
1021         return mwindow->edl->session->cwindow_yscroll;
1022 }
1023
1024
1025 float CWindowCanvas::get_zoom()
1026 {
1027         return mwindow->edl->session->cwindow_zoom;
1028 }
1029
1030 void CWindowCanvas::zoom_auto()
1031 {
1032         use_scrollbars = 0;
1033         set_zoom(mwindow->edl, 0);
1034         gui->update_canvas();
1035 }
1036
1037 int CWindowCanvas::do_scroll(EDL *edl, float cursor_x, float cursor_y)
1038 {
1039         float zoom = get_zoom();
1040         float zoom_x, zoom_y, conformed_w, conformed_h;
1041         get_zooms(edl, 0, zoom_x, zoom_y, conformed_w, conformed_h);
1042         if( !zoom ) {
1043                 x = get_x_offset(mwindow->edl, 0, zoom_x, conformed_w, conformed_h);
1044                 y = get_y_offset(mwindow->edl, 0, zoom_y, conformed_w, conformed_h);
1045                 zoom = get_auto_zoom(mwindow->edl);
1046         }
1047         else {
1048                 x = gui->x_origin - cursor_x / zoom_x;
1049                 y = gui->y_origin - cursor_y / zoom_y;
1050         }
1051         update_zoom(x, y, zoom);
1052         gui->update_canvas();
1053         return 1;
1054 }
1055
1056 void CWindowCanvas::draw_refresh(int flush)
1057 {
1058         BC_WindowBase *window = get_canvas();
1059         if( window && !window->get_video_on() ) {
1060                 clear(0);
1061                 if( refresh_frame && refresh_frame->get_w()>0 && refresh_frame->get_h()>0 ) {
1062                         float in_x1, in_y1, in_x2, in_y2;
1063                         float out_x1, out_y1, out_x2, out_y2;
1064                         get_transfers(mwindow->edl,
1065                                 in_x1, in_y1, in_x2, in_y2,
1066                                 out_x1, out_y1, out_x2, out_y2);
1067
1068
1069 //printf("CWindowCanvas::draw_refresh %.2f %.2f %.2f %.2f -> %.2f %.2f %.2f %.2f\n",
1070 //in_x1, in_y1, in_x2, in_y2, out_x1, out_y1, out_x2, out_y2);
1071
1072
1073                         if( out_x2 > out_x1 && out_y2 > out_y1 &&
1074                             in_x2 > in_x1 && in_y2 > in_y1 ) {
1075 // input scaled from session to refresh frame coordinates
1076                                 int ow = get_output_w(mwindow->edl);
1077                                 int oh = get_output_h(mwindow->edl);
1078                                 int rw = refresh_frame->get_w();
1079                                 int rh = refresh_frame->get_h();
1080                                 float xs = (float)rw / ow;
1081                                 float ys = (float)rh / oh;
1082                                 in_x1 *= xs;  in_x2 *= xs;
1083                                 in_y1 *= ys;  in_y2 *= ys;
1084 // Can't use OpenGL here because it is called asynchronously of the
1085 // playback operation.
1086                                 window->draw_vframe(refresh_frame,
1087                                                 (int)out_x1, (int)out_y1,
1088                                                 (int)(out_x2 - out_x1),
1089                                                 (int)(out_y2 - out_y1),
1090                                                 (int)in_x1, (int)in_y1,
1091                                                 (int)(in_x2 - in_x1),
1092                                                 (int)(in_y2 - in_y1),
1093                                                 0);
1094                         }
1095                 }
1096 //usleep(10000);
1097                 draw_overlays();
1098                 window->flash(flush);
1099         }
1100 //printf("CWindowCanvas::draw_refresh 10\n");
1101 }
1102
1103 #define CROPHANDLE_W xS(10)
1104 #define CROPHANDLE_H yS(10)
1105
1106 void CWindowCanvas::draw_crophandle(int x, int y)
1107 {
1108         get_canvas()->draw_box(x, y, CROPHANDLE_W, CROPHANDLE_H);
1109 }
1110
1111
1112 #define CONTROL_W xS(10)
1113 #define CONTROL_H yS(10)
1114 #define FIRST_CONTROL_W xS(20)
1115 #define FIRST_CONTROL_H yS(20)
1116 #undef BC_INFINITY
1117 #define BC_INFINITY 65536
1118
1119 #define RULERHANDLE_W xS(16)
1120 #define RULERHANDLE_H yS(16)
1121
1122 int CWindowCanvas::do_ruler(int draw, int motion,
1123                 int button_press, int button_release)
1124 {
1125         int result = 0;
1126         EDLSession *session = mwindow->edl->session;
1127         float x1 = session->ruler_x1;
1128         float y1 = session->ruler_y1;
1129         float x2 = session->ruler_x2;
1130         float y2 = session->ruler_y2;
1131         float canvas_x1 = x1;
1132         float canvas_y1 = y1;
1133         float canvas_x2 = x2;
1134         float canvas_y2 = y2;
1135         float output_x = get_cursor_x();
1136         float output_y = get_cursor_y();
1137         float canvas_cursor_x = output_x;
1138         float canvas_cursor_y = output_y;
1139
1140         canvas_to_output(mwindow->edl, 0, output_x, output_y);
1141         output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
1142         output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
1143         mwindow->session->cwindow_output_x = roundf(output_x);
1144         mwindow->session->cwindow_output_y = roundf(output_y);
1145
1146         if( button_press && get_buttonpress() == 1 ) {
1147                 gui->ruler_handle = -1;
1148                 gui->ruler_translate = 0;
1149                 if( gui->alt_down() ) {
1150                         gui->ruler_translate = 1;
1151                         gui->ruler_origin_x = x1;
1152                         gui->ruler_origin_y = y1;
1153                 }
1154                 else
1155                 if( canvas_cursor_x >= canvas_x1 - RULERHANDLE_W / 2 &&
1156                         canvas_cursor_x < canvas_x1 + RULERHANDLE_W / 2 &&
1157                         canvas_cursor_y >= canvas_y1 - RULERHANDLE_W &&
1158                         canvas_cursor_y < canvas_y1 + RULERHANDLE_H / 2 ) {
1159                         gui->ruler_handle = 0;
1160                         gui->ruler_origin_x = x1;
1161                         gui->ruler_origin_y = y1;
1162                 }
1163                 else
1164                 if( canvas_cursor_x >= canvas_x2 - RULERHANDLE_W / 2 &&
1165                         canvas_cursor_x < canvas_x2 + RULERHANDLE_W / 2 &&
1166                         canvas_cursor_y >= canvas_y2 - RULERHANDLE_W &&
1167                         canvas_cursor_y < canvas_y2 + RULERHANDLE_H / 2 ) {
1168                         gui->ruler_handle = 1;
1169                         gui->ruler_origin_x = x2;
1170                         gui->ruler_origin_y = y2;
1171                 }
1172
1173
1174 // Start new selection
1175                 if( !gui->ruler_translate &&
1176                         (gui->ruler_handle < 0 ||
1177                         (EQUIV(x2, x1) &&
1178                         EQUIV(y2, y1))) ) {
1179 // Hide previous
1180                         do_ruler(1, 0, 0, 0);
1181                         get_canvas()->flash();
1182                         gui->ruler_handle = 1;
1183                         session->ruler_x1 = output_x;
1184                         session->ruler_y1 = output_y;
1185                         session->ruler_x2 = output_x;
1186                         session->ruler_y2 = output_y;
1187                         gui->ruler_origin_x = session->ruler_x2;
1188                         gui->ruler_origin_y = session->ruler_y2;
1189                 }
1190
1191                 gui->x_origin = output_x;
1192                 gui->y_origin = output_y;
1193                 gui->current_operation = CWINDOW_RULER;
1194                 gui->tool_panel->raise_window();
1195                 result = 1;
1196         }
1197
1198         if( motion ) {
1199                 if( gui->current_operation == CWINDOW_RULER ) {
1200                         if( gui->ruler_translate ) {
1201 // Hide ruler
1202                                 do_ruler(1, 0, 0, 0);
1203                                 float x_difference = session->ruler_x1;
1204                                 float y_difference = session->ruler_y1;
1205                                 session->ruler_x1 = output_x - gui->x_origin + gui->ruler_origin_x;
1206                                 session->ruler_y1 = output_y - gui->y_origin + gui->ruler_origin_y;
1207                                 x_difference -= session->ruler_x1;
1208                                 y_difference -= session->ruler_y1;
1209                                 session->ruler_x2 -= x_difference;
1210                                 session->ruler_y2 -= y_difference;
1211 // Show ruler
1212                                 do_ruler(1, 0, 0, 0);
1213                                 get_canvas()->flash();
1214                                 gui->update_tool();
1215                         }
1216                         else {
1217                                 switch( gui->ruler_handle ) {
1218                                 case 0:
1219                                         do_ruler(1, 0, 0, 0);
1220                                         session->ruler_x1 = output_x - gui->x_origin + gui->ruler_origin_x;
1221                                         session->ruler_y1 = output_y - gui->y_origin + gui->ruler_origin_y;
1222                                         if( gui->alt_down() || gui->ctrl_down() ) {
1223                                                 double angle_value = fabs(atan((session->ruler_y2 - session->ruler_y1) /
1224                                                         (session->ruler_x2 - session->ruler_x1)) *
1225                                                         360 / 2 / M_PI);
1226                                                 double distance_value =
1227                                                         sqrt(SQR(session->ruler_x2 - session->ruler_x1) +
1228                                                         SQR(session->ruler_y2 - session->ruler_y1));
1229                                                 if( angle_value < 22 )
1230                                                         session->ruler_y1 = session->ruler_y2;
1231                                                 else
1232                                                 if( angle_value > 67 )
1233                                                         session->ruler_x1 = session->ruler_x2;
1234                                                 else
1235                                                 if( session->ruler_x1 < session->ruler_x2 &&
1236                                                         session->ruler_y1 < session->ruler_y2 ) {
1237                                                         session->ruler_x1 = session->ruler_x2 - distance_value / 1.414214;
1238                                                         session->ruler_y1 = session->ruler_y2 - distance_value / 1.414214;
1239                                                 }
1240                                                 else
1241                                                 if( session->ruler_x1 < session->ruler_x2 && session->ruler_y1 > session->ruler_y2 ) {
1242                                                         session->ruler_x1 = session->ruler_x2 - distance_value / 1.414214;
1243                                                         session->ruler_y1 = session->ruler_y2 + distance_value / 1.414214;
1244                                                 }
1245                                                 else
1246                                                 if( session->ruler_x1 > session->ruler_x2 &&
1247                                                         session->ruler_y1 < session->ruler_y2 ) {
1248                                                         session->ruler_x1 = session->ruler_x2 + distance_value / 1.414214;
1249                                                         session->ruler_y1 = session->ruler_y2 - distance_value / 1.414214;
1250                                                 }
1251                                                 else {
1252                                                         session->ruler_x1 = session->ruler_x2 + distance_value / 1.414214;
1253                                                         session->ruler_y1 = session->ruler_y2 + distance_value / 1.414214;
1254                                                 }
1255                                         }
1256                                         do_ruler(1, 0, 0, 0);
1257                                         get_canvas()->flash();
1258                                         gui->update_tool();
1259                                         break;
1260
1261                                 case 1:
1262                                         do_ruler(1, 0, 0, 0);
1263                                         session->ruler_x2 = output_x - gui->x_origin + gui->ruler_origin_x;
1264                                         session->ruler_y2 = output_y - gui->y_origin + gui->ruler_origin_y;
1265                                         if( gui->alt_down() || gui->ctrl_down() ) {
1266                                                 double angle_value = fabs(atan((session->ruler_y2 - session->ruler_y1) /
1267                                                         (session->ruler_x2 - session->ruler_x1)) *
1268                                                         360 / 2 / M_PI);
1269                                                 double distance_value =
1270                                                         sqrt(SQR(session->ruler_x2 - session->ruler_x1) +
1271                                                         SQR(session->ruler_y2 - session->ruler_y1));
1272                                                 if( angle_value < 22 )
1273                                                         session->ruler_y2 = session->ruler_y1;
1274                                                 else
1275                                                 if( angle_value > 67 )
1276                                                         session->ruler_x2 = session->ruler_x1;
1277                                                 else
1278                                                 if( session->ruler_x2 < session->ruler_x1 &&
1279                                                         session->ruler_y2 < session->ruler_y1 ) {
1280                                                         session->ruler_x2 = session->ruler_x1 - distance_value / 1.414214;
1281                                                         session->ruler_y2 = session->ruler_y1 - distance_value / 1.414214;
1282                                                 }
1283                                                 else
1284                                                 if( session->ruler_x2 < session->ruler_x1 &&
1285                                                         session->ruler_y2 > session->ruler_y1 ) {
1286                                                         session->ruler_x2 = session->ruler_x1 - distance_value / 1.414214;
1287                                                         session->ruler_y2 = session->ruler_y1 + distance_value / 1.414214;
1288                                                 }
1289                                                 else
1290                                                 if( session->ruler_x2 > session->ruler_x1 && session->ruler_y2 < session->ruler_y1 ) {
1291                                                         session->ruler_x2 = session->ruler_x1 + distance_value / 1.414214;
1292                                                         session->ruler_y2 = session->ruler_y1 - distance_value / 1.414214;
1293                                                 }
1294                                                 else {
1295                                                         session->ruler_x2 = session->ruler_x1 + distance_value / 1.414214;
1296                                                         session->ruler_y2 = session->ruler_y1 + distance_value / 1.414214;
1297                                                 }
1298                                         }
1299                                         do_ruler(1, 0, 0, 0);
1300                                         get_canvas()->flash();
1301                                         gui->update_tool();
1302                                         break;
1303                                 }
1304                         }
1305 //printf("CWindowCanvas::do_ruler 2 %f %f %f %f\n", gui->ruler_x1, gui->ruler_y1, gui->ruler_x2, gui->ruler_y2);
1306                 }
1307                 else {
1308 // printf("CWindowCanvas::do_ruler 2 %f %f %f %f\n",
1309 // canvas_cursor_x,
1310 // canvas_cursor_y,
1311 // canvas_x1,
1312 // canvas_y1);
1313                         if( canvas_cursor_x >= canvas_x1 - RULERHANDLE_W / 2 &&
1314                                 canvas_cursor_x < canvas_x1 + RULERHANDLE_W / 2 &&
1315                                 canvas_cursor_y >= canvas_y1 - RULERHANDLE_W &&
1316                                 canvas_cursor_y < canvas_y1 + RULERHANDLE_H / 2 ) {
1317                                 set_cursor(UPRIGHT_ARROW_CURSOR);
1318                         }
1319                         else
1320                         if( canvas_cursor_x >= canvas_x2 - RULERHANDLE_W / 2 &&
1321                                 canvas_cursor_x < canvas_x2 + RULERHANDLE_W / 2 &&
1322                                 canvas_cursor_y >= canvas_y2 - RULERHANDLE_W &&
1323                                 canvas_cursor_y < canvas_y2 + RULERHANDLE_H / 2 ) {
1324                                 set_cursor(UPRIGHT_ARROW_CURSOR);
1325                         }
1326                         else
1327                                 set_cursor(CROSS_CURSOR);
1328
1329 // Update current position
1330                         gui->update_tool();
1331                 }
1332         }
1333
1334 // Assume no ruler measurement if 0 length
1335         if( draw && (!EQUIV(x2, x1) || !EQUIV(y2, y1)) ) {
1336                 get_canvas()->set_inverse();
1337                 get_canvas()->set_color(WHITE);
1338                 get_canvas()->draw_line((int)canvas_x1,
1339                         (int)canvas_y1,
1340                         (int)canvas_x2,
1341                         (int)canvas_y2);
1342                 get_canvas()->draw_line(roundf(canvas_x1) - RULERHANDLE_W / 2,
1343                         roundf(canvas_y1),
1344                         roundf(canvas_x1) + RULERHANDLE_W / 2,
1345                         roundf(canvas_y1));
1346                 get_canvas()->draw_line(roundf(canvas_x1),
1347                         roundf(canvas_y1) - RULERHANDLE_H / 2,
1348                         roundf(canvas_x1),
1349                         roundf(canvas_y1) + RULERHANDLE_H / 2);
1350                 get_canvas()->draw_line(roundf(canvas_x2) - RULERHANDLE_W / 2,
1351                         roundf(canvas_y2),
1352                         roundf(canvas_x2) + RULERHANDLE_W / 2,
1353                         roundf(canvas_y2));
1354                 get_canvas()->draw_line(roundf(canvas_x2),
1355                         roundf(canvas_y2) - RULERHANDLE_H / 2,
1356                         roundf(canvas_x2),
1357                         roundf(canvas_y2) + RULERHANDLE_H / 2);
1358                 get_canvas()->set_opaque();
1359         }
1360
1361         return result;
1362 }
1363
1364
1365 static inline double line_dist(float cx,float cy, float tx,float ty)
1366 {
1367         double dx = tx-cx, dy = ty-cy;
1368         return sqrt(dx*dx + dy*dy);
1369 }
1370
1371 static inline bool test_bbox(int cx, int cy, int tx, int ty)
1372 {
1373 //      printf("test_bbox %d,%d - %d,%d = %f\n",cx,cy,tx,ty,line_dist(cx,cy,tx,ty));
1374         return (llabs(cx-tx) < CONTROL_W/2 && llabs(cy-ty) < CONTROL_H/2);
1375 }
1376
1377
1378 int CWindowCanvas::do_mask(int &redraw, int &rerender,
1379                 int button_press, int cursor_motion, int draw)
1380 {
1381 // Retrieve points from top recordable track
1382 //printf("CWindowCanvas::do_mask 1\n");
1383         Track *track = gui->cwindow->calculate_mask_track();
1384 //printf("CWindowCanvas::do_mask 2\n");
1385
1386         if( !track ) return 0;
1387 //printf("CWindowCanvas::do_mask 3\n");
1388         CWindowMaskGUI *mask_gui = (CWindowMaskGUI *)
1389                 (gui->tool_panel ? gui->tool_panel->tool_gui : 0);
1390         int draw_markers = mask_gui ? mask_gui->markers : 0;
1391         int draw_boundary = mask_gui ? mask_gui->boundary : 0;
1392         MaskAutos *mask_autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1393         int64_t position = track->to_units(
1394                 mwindow->edl->local_session->get_selectionstart(1),
1395                 0);
1396         Auto *prev_auto = 0;
1397         mask_autos->get_prev_auto(position, PLAY_FORWARD, (Auto *&)prev_auto, 1);
1398         MaskAuto *prev_mask = (MaskAuto *)prev_auto;
1399         MaskPoints points;
1400         int update_points = 1;
1401 // Determine the points based on whether
1402 // new keyframes will be generated or drawing is performed.
1403 // If keyframe generation occurs, use the interpolated mask.
1404 // If no keyframe generation occurs, use the previous mask.
1405         int use_interpolated = 0;
1406         if( button_press || cursor_motion ) {
1407 #ifdef USE_KEYFRAME_SPANNING
1408                 double selection_start = mwindow->edl->local_session->get_selectionstart(0);
1409                 double selection_end = mwindow->edl->local_session->get_selectionend(0);
1410                 int64_t start_pos = track->to_units(selection_start, 0);
1411                 int64_t end_pos = track->to_units(selection_end, 0);
1412                 Auto *first = 0, *last = 0;
1413                 mask_autos->get_prev_auto(start_pos, PLAY_FORWARD, first, 1);
1414                 mask_autos->get_prev_auto(end_pos, PLAY_FORWARD, last, 1);
1415                 if( last == first && (!mwindow->edl->session->auto_keyframes) )
1416                         use_interpolated = 0;
1417                 else
1418 // If keyframe spanning occurs, use the interpolated points.
1419 // If new keyframe is generated, use the interpolated points.
1420                         use_interpolated = 1;
1421
1422 #else
1423                 if( mwindow->edl->session->auto_keyframes )
1424                         use_interpolated = 1;
1425 #endif
1426         }
1427         else
1428                 use_interpolated = 1;
1429
1430         if( use_interpolated ) {
1431 // Interpolate the points to get exactly what is being rendered at this position.
1432                 mask_autos->get_points(&points,
1433                         mwindow->edl->session->cwindow_mask,
1434                         position, PLAY_FORWARD);
1435         }
1436         else {
1437 // Use the prev mask
1438                 prev_mask->get_points(&points,
1439                         mwindow->edl->session->cwindow_mask);
1440         }
1441
1442 // Projector zooms relative to the center of the track output.
1443         float half_track_w = (float)track->track_w / 2;
1444         float half_track_h = (float)track->track_h / 2;
1445 // Translate mask to projection
1446         float projector_x, projector_y, projector_z;
1447         track->automation->get_projector(
1448                 &projector_x, &projector_y, &projector_z,
1449                 position, PLAY_FORWARD);
1450
1451
1452 // Get position of cursor relative to mask
1453         float cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
1454         float mask_cursor_x = cursor_x, mask_cursor_y = cursor_y;
1455         canvas_to_output(mwindow->edl, 0, mask_cursor_x, mask_cursor_y);
1456
1457         projector_x += mwindow->edl->session->output_w / 2;
1458         projector_y += mwindow->edl->session->output_h / 2;
1459         mask_cursor_x = (mask_cursor_x - projector_x) / projector_z + half_track_w;
1460         mask_cursor_y = (mask_cursor_y - projector_y) / projector_z + half_track_h;
1461
1462 // Fix cursor origin
1463         if( button_press ) {
1464                 gui->x_origin = mask_cursor_x;
1465                 gui->y_origin = mask_cursor_y;
1466         }
1467
1468         int result = 0;
1469 // Points of closest line
1470         int shortest_point1 = -1;
1471         int shortest_point2 = -1;
1472 // Closest point
1473         int shortest_point = -1;
1474 // Distance to closest line
1475         float shortest_line_distance = BC_INFINITY;
1476 // Distance to closest point
1477         float shortest_point_distance = BC_INFINITY;
1478         int selected_point = -1;
1479         int selected_control_point = -1;
1480         float selected_control_point_distance = BC_INFINITY;
1481         ArrayList<int> x_points;
1482         ArrayList<int> y_points;
1483
1484         if( !cursor_motion ) {
1485                 if( draw ) {
1486                         get_canvas()->set_color(WHITE);
1487                         get_canvas()->set_inverse();
1488                 }
1489 //printf("CWindowCanvas::do_mask 1 %d\n", points.size());
1490
1491 // Never draw closed polygon and a closed
1492 // polygon is harder to add points to.
1493                 for( int i = 0; i < points.size() && !result; i++ ) {
1494                         MaskPoint *point1 = points.get(i);
1495                         MaskPoint *point2 = (i >= points.size() - 1) ?
1496                                 points.get(0) : points.get(i + 1);
1497                         if( button_press ) {
1498                                 float point_distance1 = line_dist(point1->x,point1->y, mask_cursor_x,mask_cursor_y);
1499                                 if( point_distance1 < shortest_point_distance || shortest_point < 0 ) {
1500                                         shortest_point_distance = point_distance1;
1501                                         shortest_point = i;
1502                                 }
1503
1504                                 float point_distance2 = line_dist(point2->x,point2->y, mask_cursor_x,mask_cursor_y);
1505                                 if( point_distance2 < shortest_point_distance || shortest_point < 0 ) {
1506                                         shortest_point_distance = point_distance2;
1507                                         shortest_point = (i >= points.size() - 1) ? 0 : (i + 1);
1508                                 }
1509                         }
1510
1511                         int segments = 1 + line_dist(point1->x,point1->y, point2->x,point2->y);
1512
1513 //printf("CWindowCanvas::do_mask 1 %f, %f -> %f, %f projectorz=%f\n",
1514 //point1->x, point1->y, point2->x, point2->y, projector_z);
1515                         for( int j = 0; j <= segments && !result; j++ ) {
1516 //printf("CWindowCanvas::do_mask 1 %f, %f -> %f, %f\n", x0, y0, x3, y3);
1517                                 float x0 = point1->x, y0 = point1->y;
1518                                 float x1 = point1->x + point1->control_x2;
1519                                 float y1 = point1->y + point1->control_y2;
1520                                 float x2 = point2->x + point2->control_x1;
1521                                 float y2 = point2->y + point2->control_y1;
1522                                 float x3 = point2->x, y3 = point2->y;
1523                                 float canvas_x0 = (x0 - half_track_w) * projector_z + projector_x;
1524                                 float canvas_y0 = (y0 - half_track_h) * projector_z + projector_y;
1525                                 float canvas_x1 = (x1 - half_track_w) * projector_z + projector_x;
1526                                 float canvas_y1 = (y1 - half_track_h) * projector_z + projector_y;
1527                                 float canvas_x2 = (x2 - half_track_w) * projector_z + projector_x;
1528                                 float canvas_y2 = (y2 - half_track_h) * projector_z + projector_y;
1529                                 float canvas_x3 = (x3 - half_track_w) * projector_z + projector_x;
1530                                 float canvas_y3 = (y3 - half_track_h) * projector_z + projector_y;
1531
1532                                 float t = (float)j / segments;
1533                                 float tpow2 = t * t;
1534                                 float tpow3 = t * t * t;
1535                                 float invt = 1 - t;
1536                                 float invtpow2 = invt * invt;
1537                                 float invtpow3 = invt * invt * invt;
1538
1539                                 float x = (           invtpow3 * x0
1540                                         + 3 * t     * invtpow2 * x1
1541                                         + 3 * tpow2 * invt     * x2
1542                                         +     tpow3            * x3);
1543                                 float y = (           invtpow3 * y0
1544                                         + 3 * t     * invtpow2 * y1
1545                                         + 3 * tpow2 * invt     * y2
1546                                         +     tpow3            * y3);
1547                                 float canvas_x = (x - half_track_w) * projector_z + projector_x;
1548                                 float canvas_y = (y - half_track_h) * projector_z + projector_y;
1549 // Test new point addition
1550                                 if( button_press ) {
1551                                         float line_distance = line_dist(x,y, mask_cursor_x,mask_cursor_y);
1552
1553 //printf("CWindowCanvas::do_mask 1 x=%f cursor_x=%f y=%f cursor_y=%f %f %f %d, %d\n",
1554 //  x, cursor_x, y, cursor_y, line_distance, shortest_line_distance, shortest_point1, shortest_point2);
1555                                         if( line_distance < shortest_line_distance ||
1556                                                 shortest_point1 < 0 ) {
1557                                                 shortest_line_distance = line_distance;
1558                                                 shortest_point1 = i;
1559                                                 shortest_point2 = (i >= points.size() - 1) ? 0 : (i + 1);
1560 //printf("CWindowCanvas::do_mask 2 %f %f %d, %d\n",
1561 //  line_distance, shortest_line_distance, shortest_point1, shortest_point2);
1562                                         }
1563
1564 // Test existing point selection
1565 // Test first point
1566                                         if( gui->ctrl_down() ) {
1567                                                 float distance = line_dist(x1,y1, mask_cursor_x,mask_cursor_y);
1568
1569                                                 if( distance < selected_control_point_distance ) {
1570                                                         selected_point = i;
1571                                                         selected_control_point = 1;
1572                                                         selected_control_point_distance = distance;
1573                                                 }
1574                                         }
1575                                         else {
1576                                                 if( !gui->shift_down() ) {
1577                                                         output_to_canvas(mwindow->edl, 0, canvas_x0, canvas_y0);
1578                                                         if( test_bbox(cursor_x, cursor_y, canvas_x0, canvas_y0) ) {
1579                                                                 selected_point = i;
1580                                                         }
1581                                                 }
1582                                                 else {
1583                                                         selected_point = shortest_point;
1584                                                 }
1585                                         }
1586 // Test second point
1587                                         if( gui->ctrl_down() ) {
1588                                                 float distance = line_dist(x2,y2, mask_cursor_x,mask_cursor_y);
1589
1590 //printf("CWindowCanvas::do_mask %d %f %f\n", i, distance, selected_control_point_distance);
1591                                                 if( distance < selected_control_point_distance ) {
1592                                                         selected_point = (i < points.size() - 1 ? i + 1 : 0);
1593                                                         selected_control_point = 0;
1594                                                         selected_control_point_distance = distance;
1595                                                 }
1596                                         }
1597                                         else if( i < points.size() - 1 ) {
1598                                                 if( !gui->shift_down() ) {
1599                                                         output_to_canvas(mwindow->edl, 0, canvas_x3, canvas_y3);
1600                                                         if( test_bbox(cursor_x, cursor_y, canvas_x3, canvas_y3) ) {
1601                                                                 selected_point = (i < points.size() - 1 ? i + 1 : 0);
1602                                                         }
1603                                                 }
1604                                                 else {
1605                                                         selected_point = shortest_point;
1606                                                 }
1607                                         }
1608                                 }
1609
1610                                 output_to_canvas(mwindow->edl, 0, canvas_x, canvas_y);
1611
1612                                 if( j > 0 ) {
1613
1614                                         if( draw ) { // Draw joining line
1615                                                 x_points.append((int)canvas_x);
1616                                                 y_points.append((int)canvas_y);
1617                                         }
1618
1619                                         if( j == segments ) {
1620                                                 if( draw && draw_markers ) { // Draw second anchor
1621                                                         if( i < points.size() - 1 ) {
1622                                                                 if( i == gui->affected_point - 1 )
1623                                                                         get_canvas()->draw_disc(
1624                                                                                 (int)canvas_x - CONTROL_W / 2,
1625                                                                                 (int)canvas_y - CONTROL_W / 2,
1626                                                                                 CONTROL_W, CONTROL_H);
1627                                                                 else
1628                                                                         get_canvas()->draw_circle(
1629                                                                                 (int)canvas_x - CONTROL_W / 2,
1630                                                                                 (int)canvas_y - CONTROL_W / 2,
1631                                                                                 CONTROL_W, CONTROL_H);
1632 // char string[BCTEXTLEN];
1633 // sprintf(string, "%d", (i < points.size() - 1 ? i + 1 : 0));
1634 // canvas->draw_text((int)canvas_x + CONTROL_W, (int)canvas_y + CONTROL_W, string);
1635                                                         }
1636 // Draw second control point.
1637                                                         output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
1638                                                         get_canvas()->draw_line(
1639                                                                 (int)canvas_x, (int)canvas_y,
1640                                                                 (int)canvas_x2, (int)canvas_y2);
1641                                                         get_canvas()->draw_rectangle(
1642                                                                 (int)canvas_x2 - CONTROL_W / 2,
1643                                                                 (int)canvas_y2 - CONTROL_H / 2,
1644                                                                 CONTROL_W, CONTROL_H);
1645                                                 }
1646                                         }
1647                                 }
1648                                 else {
1649 // Draw first anchor
1650                                         if( i == 0 && draw ) {
1651                                                 if( draw_boundary ) {
1652                                                         char mask_label[BCSTRLEN];
1653                                                         int k = mwindow->edl->session->cwindow_mask;
1654                                                         if( !prev_mask || k < 0 || k >= prev_mask->masks.size() )
1655                                                                 sprintf(mask_label, "%d", k);
1656                                                         else
1657                                                                 sprintf(mask_label, "%s", prev_mask->masks[k]->name);
1658                                                         get_canvas()->draw_text(
1659                                                                 (int)canvas_x - FIRST_CONTROL_W,
1660                                                                 (int)canvas_y - FIRST_CONTROL_H,
1661                                                                 mask_label);
1662                                                 }
1663                                                 if( draw_markers ) {
1664                                                         get_canvas()->draw_disc(
1665                                                                 (int)canvas_x - FIRST_CONTROL_W / 2,
1666                                                                 (int)canvas_y - FIRST_CONTROL_H / 2,
1667                                                                 FIRST_CONTROL_W, FIRST_CONTROL_H);
1668                                                 }
1669                                         }
1670
1671 // Draw first control point.
1672                                         if( draw && draw_markers ) {
1673                                                 output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
1674                                                 get_canvas()->draw_line(
1675                                                         (int)canvas_x, (int)canvas_y,
1676                                                         (int)canvas_x1, (int)canvas_y1);
1677                                                 get_canvas()->draw_rectangle(
1678                                                         (int)canvas_x1 - CONTROL_W / 2,
1679                                                         (int)canvas_y1 - CONTROL_H / 2,
1680                                                         CONTROL_W, CONTROL_H);
1681
1682                                                 x_points.append((int)canvas_x);
1683                                                 y_points.append((int)canvas_y);
1684                                         }
1685                                 }
1686 //printf("CWindowCanvas::do_mask 1\n");
1687
1688                         }
1689                 }
1690 //printf("CWindowCanvas::do_mask 1\n");
1691
1692                 BC_WindowBase *cvs_win = get_canvas();
1693                 if( draw && draw_boundary ) {
1694                         cvs_win->draw_polygon(&x_points, &y_points);
1695                         cvs_win->set_opaque();
1696                 }
1697                 if( draw && mask_gui && mask_gui->focused ) {
1698                         float fx = atof(mask_gui->focus_x->get_text());
1699                         float fy = atof(mask_gui->focus_y->get_text());
1700                         fx = (fx - half_track_w) * projector_z + projector_x;
1701                         fy = (fy - half_track_h) * projector_z + projector_y;
1702                         output_to_canvas(mwindow->edl, 0, fx, fy);
1703                         float r = bmax(cvs_win->get_w(), cvs_win->get_h());
1704                         float d = 0.005*r;
1705 #if 1
1706                         int fw = 2*d+3, fh = fw;
1707                         VFrame focus(fw,fh, BC_RGBA8888);
1708                         focus.transfer_from(gui->focus_frame);
1709                         fx -= fw/2.f;  fy -= fh/2.f;
1710                         BC_Pixmap focus_pixmap(cvs_win, &focus, PIXMAP_ALPHA);
1711                         cvs_win->draw_pixmap(&focus_pixmap,fx,fy);
1712 #else
1713                         cvs_win->set_line_width((int)(0.0025*r) + 1);
1714                         cvs_win->set_color(BLUE);
1715                         cvs_win->draw_line(fx-d,fy-d, fx+d, fy+d);
1716                         cvs_win->draw_line(fx-d,fy+d, fx+d, fy-d);
1717                         cvs_win->set_line_width(0);
1718                         cvs_win->set_color(WHITE);
1719 #endif
1720                 }
1721                 if( draw && mask_gui && draw_markers && points.size() ) {
1722                         float cx = 0, cy = 0;
1723                         int n = points.size();
1724                         for( int i=0; i<n; ++i ) {
1725                                 MaskPoint *point = points.get(i);
1726                                 cx += point->x;  cy += point->y;
1727                         }
1728                         cx /= n;  cy /= n;
1729                         if( !mask_gui->focused )
1730                                 mask_gui->set_focused(0, cx, cy);
1731                         cx = (cx - half_track_w) * projector_z + projector_x;
1732                         cy = (cy - half_track_h) * projector_z + projector_y;
1733                         output_to_canvas(mwindow->edl, 0, cx, cy);
1734                         float r = bmax(cvs_win->get_w(), cvs_win->get_h());
1735                         float d = 0.007*r;
1736                         cvs_win->set_line_width((int)(0.002*r) + 1);
1737                         cvs_win->set_color(ORANGE);
1738                         cvs_win->draw_line(cx-d,cy, cx+d, cy);
1739                         cvs_win->draw_line(cx,cy-d, cx, cy+d);
1740                         cvs_win->set_line_width(0);
1741                         cvs_win->set_color(WHITE);
1742                 }
1743 //printf("CWindowCanvas::do_mask 1\n");
1744         }
1745
1746         if( button_press && !result ) {
1747                 gui->affected_track = gui->cwindow->calculate_mask_track();
1748
1749 // Get keyframe outside the EDL to edit.  This must be rendered
1750 // instead of the EDL keyframes when it exists.  Then it must be
1751 // applied to the EDL keyframes on buttonrelease.
1752                 if( gui->affected_track ) {
1753 #ifdef USE_KEYFRAME_SPANNING
1754 // Make copy of current parameters in local keyframe
1755                         gui->mask_keyframe =
1756                                 (MaskAuto*)gui->cwindow->calculate_affected_auto(
1757                                         mask_autos, 0);
1758                         gui->orig_mask_keyframe->copy_data(gui->mask_keyframe);
1759 #else
1760
1761                         gui->mask_keyframe =
1762                                 (MaskAuto*)gui->cwindow->calculate_affected_auto(
1763                                         mask_autos, 1);
1764 #endif
1765                 }
1766                 SubMask *mask = gui->mask_keyframe->get_submask(mwindow->edl->session->cwindow_mask);
1767
1768                 if( get_buttonpress() == WHEEL_UP || get_buttonpress() == WHEEL_DOWN ) {
1769                         if( !gui->shift_down() ) {
1770                                 mwindow->undo->update_undo_before(_("mask rotate"), this);
1771                                 gui->current_operation = CWINDOW_MASK_ROTATE;
1772                         }
1773                         else {
1774                                 mwindow->undo->update_undo_before(_("mask scale"), this);
1775                                 gui->current_operation = CWINDOW_MASK_SCALE;
1776                         }
1777                         gui->affected_point = 0;
1778                 }
1779                 else
1780 // Translate entire keyframe
1781                 if( gui->alt_down() && mask->points.size() ) {
1782                         mwindow->undo->update_undo_before(_("mask translate"), 0);
1783                         gui->current_operation = CWINDOW_MASK_TRANSLATE;
1784                         gui->affected_point = 0;
1785                 }
1786                 else
1787 // Existing point or control point was selected
1788                 if( selected_point >= 0 ) {
1789                         mwindow->undo->update_undo_before(_("mask adjust"), 0);
1790                         gui->affected_point = selected_point;
1791
1792                         if( selected_control_point == 0 )
1793                                 gui->current_operation = CWINDOW_MASK_CONTROL_IN;
1794                         else
1795                         if( selected_control_point == 1 )
1796                                 gui->current_operation = CWINDOW_MASK_CONTROL_OUT;
1797                         else
1798                                 gui->current_operation = mwindow->edl->session->cwindow_operation;
1799                 }
1800                 else // No existing point or control point was selected so create a new one
1801                 if( !gui->ctrl_down() && !gui->alt_down() ) {
1802                         mwindow->undo->update_undo_before(_("mask point"), 0);
1803 // Create the template
1804                         MaskPoint *point = new MaskPoint;
1805                         point->x = mask_cursor_x;
1806                         point->y = mask_cursor_y;
1807                         point->control_x1 = 0;
1808                         point->control_y1 = 0;
1809                         point->control_x2 = 0;
1810                         point->control_y2 = 0;
1811
1812
1813                         if( shortest_point2 < shortest_point1 ) {
1814                                 shortest_point2 ^= shortest_point1;
1815                                 shortest_point1 ^= shortest_point2;
1816                                 shortest_point2 ^= shortest_point1;
1817                         }
1818
1819
1820
1821 // printf("CWindowGUI::do_mask 40\n");
1822 // mwindow->edl->dump();
1823 // printf("CWindowGUI::do_mask 50\n");
1824
1825
1826
1827 //printf("CWindowCanvas::do_mask 1 %f %f %d %d\n",
1828 //      shortest_line_distance, shortest_point_distance, shortest_point1, shortest_point2);
1829 //printf("CWindowCanvas::do_mask %d %d\n", shortest_point1, shortest_point2);
1830
1831 // Append to end of list
1832                         if( shortest_point1 == shortest_point2 ||
1833                             labs(shortest_point1 - shortest_point2) > 1) {
1834 #ifdef USE_KEYFRAME_SPANNING
1835
1836                                 MaskPoint *new_point = new MaskPoint;
1837                                 points.append(new_point);
1838                                 *new_point = *point;
1839                                 gui->affected_point = points.size() - 1;
1840
1841 #else
1842
1843 // Need to apply the new point to every keyframe
1844                                 for( MaskAuto *current = (MaskAuto*)mask_autos->default_auto; current;  ) {
1845                                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1846                                         MaskPoint *new_point = new MaskPoint;
1847                                         submask->points.append(new_point);
1848                                         *new_point = *point;
1849                                         if( current == (MaskAuto*)mask_autos->default_auto )
1850                                                 current = (MaskAuto*)mask_autos->first;
1851                                         else
1852                                                 current = (MaskAuto*)NEXT;
1853                                 }
1854                                 gui->affected_point = mask->points.size() - 1;
1855 #endif
1856
1857                                 result = 1;
1858                         }
1859                         else
1860 // Insert between 2 points, shifting back point 2
1861                         if( shortest_point1 >= 0 && shortest_point2 >= 0 ) {
1862
1863 #ifdef USE_KEYFRAME_SPANNING
1864 // In case the keyframe point count isn't synchronized with the rest of the keyframes,
1865 // avoid a crash.
1866                                 if( points.size() >= shortest_point2 ) {
1867                                         MaskPoint *new_point = new MaskPoint;
1868                                         points.append(0);
1869                                         for( int i = points.size() - 1;
1870                                                 i > shortest_point2;
1871                                                 i-- )
1872                                                 points.values[i] = points.values[i - 1];
1873                                         points.values[shortest_point2] = new_point;
1874
1875                                         *new_point = *point;
1876                                 }
1877
1878 #else
1879
1880                                 for( MaskAuto *current = (MaskAuto*)mask_autos->default_auto; current;  ) {
1881                                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1882 // In case the keyframe point count isn't synchronized with the rest of the keyframes,
1883 // avoid a crash.
1884                                         if( submask->points.size() >= shortest_point2 ) {
1885                                                 MaskPoint *new_point = new MaskPoint;
1886                                                 submask->points.append(0);
1887                                                 for( int i = submask->points.size() - 1;
1888                                                         i > shortest_point2;
1889                                                         i-- )
1890                                                         submask->points.values[i] = submask->points.values[i - 1];
1891                                                 submask->points.values[shortest_point2] = new_point;
1892
1893                                                 *new_point = *point;
1894                                         }
1895
1896                                         if( current == (MaskAuto*)mask_autos->default_auto )
1897                                                 current = (MaskAuto*)mask_autos->first;
1898                                         else
1899                                                 current = (MaskAuto*)NEXT;
1900                                 }
1901
1902 #endif
1903                                 gui->affected_point = shortest_point2;
1904                                 result = 1;
1905                         }
1906
1907
1908 // printf("CWindowGUI::do_mask 20\n");
1909 // mwindow->edl->dump();
1910 // printf("CWindowGUI::do_mask 30\n");
1911
1912                         if( !result ) {
1913 //printf("CWindowCanvas::do_mask 1\n");
1914 // Create the first point.
1915 #ifdef USE_KEYFRAME_SPANNING
1916                                 MaskPoint *new_point = new MaskPoint;
1917                                 points.append(new_point);
1918                                 *new_point = *point;
1919                                 gui->affected_point = points.size() - 1;
1920 #else
1921                                 for( MaskAuto *current = (MaskAuto*)mask_autos->default_auto; current;  ) {
1922                                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1923                                         MaskPoint *new_point = new MaskPoint;
1924                                         submask->points.append(new_point);
1925                                         *new_point = *point;
1926                                         if( current == (MaskAuto*)mask_autos->default_auto )
1927                                                 current = (MaskAuto*)mask_autos->first;
1928                                         else
1929                                                 current = (MaskAuto*)NEXT;
1930                                 }
1931                                 gui->affected_point = points.size() - 1;
1932 #endif
1933
1934 //printf("CWindowCanvas::do_mask 3 %d\n", mask->points.size());
1935                         }
1936
1937                         gui->current_operation = mwindow->edl->session->cwindow_operation;
1938 // Delete the template
1939                         delete point;
1940                 }
1941
1942                 result = 1;
1943                 rerender = 1;
1944                 redraw = 1;
1945         }
1946
1947         if( button_press && result ) {
1948 #ifdef USE_KEYFRAME_SPANNING
1949                 MaskPoints &mask_points = points;
1950 #else
1951                 SubMask *mask = gui->mask_keyframe->get_submask(mwindow->edl->session->cwindow_mask);
1952                 MaskPoints &mask_points = mask->points;
1953 #endif
1954                 int k = gui->affected_point;
1955                 if( k >= 0 && k < mask_points.size() ) {
1956                         MaskPoint *point = mask_points.values[k];
1957                         gui->center_x = point->x;
1958                         gui->center_y = point->y;
1959                         gui->control_in_x = point->control_x1;
1960                         gui->control_in_y = point->control_y1;
1961                         gui->control_out_x = point->control_x2;
1962                         gui->control_out_y = point->control_y2;
1963                 }
1964                 else {
1965                         gui->center_x = gui->center_y = 0;
1966                         gui->control_in_x = gui->control_in_y = 0;
1967                         gui->control_out_x = gui->control_out_y = 0;
1968                 }
1969                 gui->tool_panel->raise_window();
1970         }
1971
1972 //printf("CWindowCanvas::do_mask 8\n");
1973         if( cursor_motion ) {
1974
1975 #ifdef USE_KEYFRAME_SPANNING
1976 // Must update the reference keyframes for every cursor motion
1977                 gui->mask_keyframe = (MaskAuto*)gui->cwindow->
1978                         calculate_affected_auto(mask_autos, 0);
1979                 gui->orig_mask_keyframe->copy_data(gui->mask_keyframe);
1980 #endif
1981
1982 //printf("CWindowCanvas::do_mask %d %d\n", __LINE__, gui->affected_point);
1983
1984                 SubMask *mask = gui->mask_keyframe->get_submask(mwindow->edl->session->cwindow_mask);
1985                 if( mask && gui->affected_point >= 0 &&
1986                     gui->affected_point < mask->points.size() &&
1987                     gui->current_operation != CWINDOW_NONE ) {
1988 //                      mwindow->undo->update_undo_before(_("mask point"), this);
1989 #ifdef USE_KEYFRAME_SPANNING
1990                         MaskPoints &mask_points = points;
1991 #else
1992                         MaskPoints &mask_points = mask->points;
1993 #endif
1994                         MaskPoint *point = mask_points.get(gui->affected_point);
1995 //                      canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
1996 //printf("CWindowCanvas::do_mask 9 %d %d\n", mask_points.size(), gui->affected_point);
1997
1998                         float last_x = point->x;
1999                         float last_y = point->y;
2000                         float last_control_x1 = point->control_x1;
2001                         float last_control_y1 = point->control_y1;
2002                         float last_control_x2 = point->control_x2;
2003                         float last_control_y2 = point->control_y2;
2004                         int rotate = 0;
2005
2006                         switch( gui->current_operation ) {
2007                         case CWINDOW_MASK:
2008 //printf("CWindowCanvas::do_mask %d %d\n", __LINE__, gui->affected_point);
2009                                 point->x = mask_cursor_x - gui->x_origin + gui->center_x;
2010                                 point->y = mask_cursor_y - gui->y_origin + gui->center_y;
2011                                 break;
2012
2013                         case CWINDOW_MASK_CONTROL_IN:
2014                                 point->control_x1 = mask_cursor_x - gui->x_origin + gui->control_in_x;
2015                                 point->control_y1 = mask_cursor_y - gui->y_origin + gui->control_in_y;
2016                                 break;
2017
2018                         case CWINDOW_MASK_CONTROL_OUT:
2019                                 point->control_x2 = mask_cursor_x - gui->x_origin + gui->control_out_x;
2020                                 point->control_y2 = mask_cursor_y - gui->y_origin + gui->control_out_y;
2021                                 break;
2022
2023                         case CWINDOW_MASK_TRANSLATE: {
2024                                 if( !mask_gui ) break;
2025                                 int mode = mask_gui->scale_mode;
2026                                 MaskAuto *keyframe = gui->mask_keyframe;
2027                                 int gang = mask_gui->gang_focus->get_value();
2028                                 float dx = mask_cursor_x - gui->x_origin;
2029                                 float dy = mask_cursor_y - gui->y_origin;
2030                                 if( !dx && !dy ) break;
2031                                 int k = mwindow->edl->session->cwindow_mask;
2032                                 int n = gang ? keyframe->masks.size() : k+1;
2033                                 for( int j=gang? 0 : k; j<n; ++j ) {
2034                                         if( !mask_gui->mask_enables[j]->get_value() ) continue;
2035                                         SubMask *sub_mask = keyframe->get_submask(j);
2036                                         if( !sub_mask ) continue;
2037                                         MaskPoints &points = sub_mask->points;
2038                                         for( int i=0; i<points.size(); ++i ) {
2039                                                 MaskPoint *point = points[i];
2040                                                 if( mode == MASK_SCALE_X || mode == MASK_SCALE_XY ) point->x += dx;
2041                                                 if( mode == MASK_SCALE_Y || mode == MASK_SCALE_XY ) point->y += dy;
2042                                         }
2043                                 }
2044                                 gui->x_origin = mask_cursor_x;
2045                                 gui->y_origin = mask_cursor_y;
2046                                 rerender = 1;
2047                                 redraw = 1;
2048                                 update_points = 0;
2049                                 break; }
2050                         case CWINDOW_MASK_ROTATE:
2051                                 rotate = 1;
2052                         case CWINDOW_MASK_SCALE: {
2053                                 if( !mask_gui || !mask_points.size() ) break;
2054                                 float cx = gui->x_origin, cy = gui->y_origin;
2055                                 if( mask_gui->focused ) {
2056                                         cx = atof(mask_gui->focus_x->get_text());
2057                                         cy = atof(mask_gui->focus_y->get_text());
2058                                 }
2059                                 else if( !gui->ctrl_down() ) {
2060                                         cx = cy = 0;
2061                                         int n = mask_points.size();
2062                                         for( int i=0; i<n; ++i ) {
2063                                                 MaskPoint *point = mask_points.values[i];
2064                                                 cx += point->x;  cy += point->y;
2065                                         }
2066                                         cx /= n;  cy /= n;
2067                                         mask_gui->set_focused(0, cx, cy);
2068                                 }
2069                                 gui->x_origin = cx;
2070                                 gui->y_origin = cy;
2071                                 double accel =
2072                                         gui->get_triple_click() ? 8. :
2073                                         gui->get_double_click() ? 4. :
2074                                         1.;
2075                                 int button_no = get_buttonpress();
2076                                 double ds = accel/64., dt = accel*M_PI/360.;
2077                                 double scale = button_no == WHEEL_UP ? 1.+ds : 1.-ds;
2078                                 int mode = mask_gui->scale_mode;
2079                                 double xscale = !rotate && (mode == MASK_SCALE_X ||
2080                                         mode == MASK_SCALE_XY ) ? scale : 1.;
2081                                 double yscale = !rotate && (mode == MASK_SCALE_Y ||
2082                                         mode == MASK_SCALE_XY ) ? scale : 1.;
2083                                 double theta = button_no == WHEEL_UP ? dt : -dt;
2084                                 if( rotate ? theta==0 : scale==1 ) break;
2085                                 float st = sin(theta), ct = cos(theta);
2086                                 MaskAuto *keyframe = gui->mask_keyframe;
2087                                 int gang = mask_gui->gang_focus->get_value();
2088                                 int k = mwindow->edl->session->cwindow_mask;
2089                                 int n = gang ? keyframe->masks.size() : k+1;
2090                                 for( int j=gang? 0 : k; j<n; ++j ) {
2091                                         if( !mask_gui->mask_enables[j]->get_value() ) continue;
2092                                         SubMask *sub_mask = keyframe->get_submask(j);
2093                                         if( !sub_mask ) continue;
2094                                         MaskPoints &points = sub_mask->points;
2095                                         for( int i=0; i<points.size(); ++i ) {
2096                                                 MaskPoint *point = points[i];
2097                                                 float px = point->x - gui->x_origin;
2098                                                 float py = point->y - gui->y_origin;
2099                                                 float nx = !rotate ? px*xscale : px*ct + py*st;
2100                                                 float ny = !rotate ? py*yscale : py*ct - px*st;
2101                                                 point->x = nx + gui->x_origin;
2102                                                 point->y = ny + gui->y_origin;
2103                                                 px = point->control_x1;  py = point->control_y1;
2104                                                 point->control_x1 = !rotate ? px*xscale : px*ct + py*st;
2105                                                 point->control_y1 = !rotate ? py*yscale : py*ct - px*st;
2106                                                 px = point->control_x2;  py = point->control_y2;
2107                                                 point->control_x2 = !rotate ? px*xscale : px*ct + py*st;
2108                                                 point->control_y2 = !rotate ? py*yscale : py*ct - px*st;
2109                                         }
2110                                 }
2111                                 rerender = 1;
2112                                 redraw = 1;
2113                                 update_points = 0;
2114                                 break; }
2115                         }
2116
2117                         if( !(rerender && redraw) && (!EQUIV(last_x, point->x) ||
2118                                 !EQUIV(last_y, point->y) ||
2119                                 !EQUIV(last_control_x1, point->control_x1) ||
2120                                 !EQUIV(last_control_y1, point->control_y1) ||
2121                                 !EQUIV(last_control_x2, point->control_x2) ||
2122                                 !EQUIV(last_control_y2, point->control_y2)) ) {
2123                                 rerender = 1;
2124                                 redraw = 1;
2125                         }
2126                 }
2127                 else
2128                 if( gui->current_operation == CWINDOW_NONE ) {
2129 //                      printf("CWindowCanvas::do_mask %d\n", __LINE__);
2130                         int over_point = 0;
2131                         for( int i = 0; i < points.size() && !over_point; i++ ) {
2132                                 MaskPoint *point = points.get(i);
2133                                 float x0 = point->x;
2134                                 float y0 = point->y;
2135                                 float x1 = point->x + point->control_x1;
2136                                 float y1 = point->y + point->control_y1;
2137                                 float x2 = point->x + point->control_x2;
2138                                 float y2 = point->y + point->control_y2;
2139                                 float canvas_x0 = (x0 - half_track_w) * projector_z + projector_x;
2140                                 float canvas_y0 = (y0 - half_track_h) * projector_z + projector_y;
2141
2142                                 output_to_canvas(mwindow->edl, 0, canvas_x0, canvas_y0);
2143                                 if( test_bbox(cursor_x, cursor_y, canvas_x0, canvas_y0) ) {
2144                                         over_point = 1;
2145                                 }
2146
2147                                 if( !over_point && gui->ctrl_down() ) {
2148                                         float canvas_x1 = (x1 - half_track_w) * projector_z + projector_x;
2149                                         float canvas_y1 = (y1 - half_track_h) * projector_z + projector_y;
2150                                         output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
2151                                         if( test_bbox(cursor_x, cursor_y, canvas_x1, canvas_y1) ) {
2152                                                 over_point = 1;
2153                                         }
2154                                         else {
2155                                                 float canvas_x2 = (x2 - half_track_w) * projector_z + projector_x;
2156                                                 float canvas_y2 = (y2 - half_track_h) * projector_z + projector_y;
2157                                                 output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
2158                                                 if( test_bbox(cursor_x, cursor_y, canvas_x2, canvas_y2) ) {
2159                                                         over_point = 1;
2160                                                 }
2161                                         }
2162                                 }
2163                         }
2164
2165                         set_cursor( over_point ? ARROW_CURSOR : CROSS_CURSOR );
2166                 }
2167
2168                 result = 1;
2169         }
2170 //printf("CWindowCanvas::do_mask 2 %d %d %d\n", result, rerender, redraw);
2171
2172
2173 #ifdef USE_KEYFRAME_SPANNING
2174 // Must commit change after operation.
2175         if( rerender && track ) {
2176 // Swap EDL keyframe with original.
2177 // Apply new values to keyframe span
2178                 MaskAuto temp_keyframe(mwindow->edl, mask_autos);
2179                 temp_keyframe.copy_data(gui->mask_keyframe);
2180 // Apply interpolated points back to keyframe
2181                 if( update_points )
2182                         temp_keyframe.set_points(&points, mwindow->edl->session->cwindow_mask);
2183                 gui->mask_keyframe->copy_data(gui->orig_mask_keyframe);
2184                 mask_autos->update_parameter(&temp_keyframe);
2185         }
2186 #endif
2187 //printf("CWindowCanvas::do_mask 20\n");
2188
2189         if( draw && draw_boundary && !draw_markers ) {
2190                 BC_WindowBase *cvs_win = get_canvas();
2191                 cvs_win->set_inverse();
2192                 cvs_win->set_color(RED+GREEN);
2193                 for( int k=0; k<SUBMASKS; ++k ) {
2194                         if( k == mwindow->edl->session->cwindow_mask ) continue;
2195                         points.remove_all_objects();
2196                         if( use_interpolated )
2197                                 mask_autos->get_points(&points, k, position, PLAY_FORWARD);
2198                         else
2199                                 prev_mask->get_points(&points, k);
2200                         MaskEdge edge;
2201                         edge.load(points, 0);
2202                         for( int i=0; i<edge.size(); ++i ) {
2203                                 MaskCoord a = edge[i];
2204                                 MaskCoord b = i<edge.size()-1 ? edge[i+1] : edge[0];
2205                                 float ax = a.x, ay = a.y;
2206                                 float bx = b.x, by = b.y;
2207                                 output_to_canvas(mwindow->edl, 0, ax, ay);
2208                                 output_to_canvas(mwindow->edl, 0, bx, by);
2209                                 cvs_win->draw_line(ax,ay, bx,by);
2210                         }
2211                 }
2212         }
2213
2214         return result;
2215 }
2216
2217 int CWindowCanvas::do_mask_focus()
2218 {
2219         Track *track = gui->cwindow->calculate_affected_track();
2220         int64_t position = track->to_units(
2221                 mwindow->edl->local_session->get_selectionstart(1),
2222                 0);
2223         float projector_x, projector_y, projector_z;
2224         track->automation->get_projector(
2225                 &projector_x, &projector_y, &projector_z,
2226                 position, PLAY_FORWARD);
2227         float cx = get_cursor_x(), cy = get_cursor_y();
2228         canvas_to_output(mwindow->edl, 0, cx, cy);
2229         projector_x += mwindow->edl->session->output_w / 2;
2230         projector_y += mwindow->edl->session->output_h / 2;
2231         float half_track_w = (float)track->track_w / 2;
2232         float half_track_h = (float)track->track_h / 2;
2233         cx = (cx - projector_x) / projector_z + half_track_w;
2234         cy = (cy - projector_y) / projector_z + half_track_h;
2235         CWindowMaskGUI *mask_gui = (CWindowMaskGUI*) gui->tool_panel->tool_gui;
2236         mask_gui->set_focused(1, cx, cy);
2237         return 1;
2238 }
2239
2240 int CWindowCanvas::do_eyedrop(int &rerender, int button_press, int draw)
2241 {
2242         int result = 0;
2243         int radius = mwindow->edl->session->eyedrop_radius;
2244         int row1 = 0;
2245         int row2 = 0;
2246         int column1 = 0;
2247         int column2 = 0;
2248
2249
2250
2251         if( refresh_frame && refresh_frame->get_w()>0 && refresh_frame->get_h()>0 ) {
2252
2253                 if( draw ) {
2254                         row1 = gui->eyedrop_y - radius;
2255                         row2 = gui->eyedrop_y + radius;
2256                         column1 = gui->eyedrop_x - radius;
2257                         column2 = gui->eyedrop_x + radius;
2258
2259                         CLAMP(row1, 0, refresh_frame->get_h() - 1);
2260                         CLAMP(row2, 0, refresh_frame->get_h() - 1);
2261                         CLAMP(column1, 0, refresh_frame->get_w() - 1);
2262                         CLAMP(column2, 0, refresh_frame->get_w() - 1);
2263
2264                         if( row2 <= row1 ) row2 = row1 + 1;
2265                         if( column2 <= column1 ) column2 = column1 + 1;
2266
2267                         float x1 = column1;
2268                         float y1 = row1;
2269                         float x2 = column2;
2270                         float y2 = row2;
2271
2272                         output_to_canvas(mwindow->edl, 0, x1, y1);
2273                         output_to_canvas(mwindow->edl, 0, x2, y2);
2274 //printf("CWindowCanvas::do_eyedrop %d %f %f %f %f\n", __LINE__, x1, x2, y1, y2);
2275
2276                         if( x2 - x1 >= 1 && y2 - y1 >= 1 ) {
2277                                 get_canvas()->set_inverse();
2278                                 get_canvas()->set_color(WHITE);
2279
2280                                 get_canvas()->draw_rectangle((int)x1,
2281                                         (int)y1,
2282                                         (int)(x2 - x1),
2283                                         (int)(y2 - y1));
2284
2285                                 get_canvas()->set_opaque();
2286                                 get_canvas()->flash();
2287                         }
2288                         return 0;
2289                 }
2290         }
2291
2292         if( button_press ) {
2293                 gui->current_operation = CWINDOW_EYEDROP;
2294                 gui->tool_panel->raise_window();
2295         }
2296
2297         if( gui->current_operation == CWINDOW_EYEDROP ) {
2298                 mwindow->undo->update_undo_before(_("Eyedrop"), this);
2299
2300 // Get color out of frame.
2301 // Doesn't work during playback because that bypasses the refresh frame.
2302                 if( refresh_frame && refresh_frame->get_w()>0 && refresh_frame->get_h()>0 ) {
2303                         float cursor_x = get_cursor_x();
2304                         float cursor_y = get_cursor_y();
2305                         canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
2306                         CLAMP(cursor_x, 0, refresh_frame->get_w() - 1);
2307                         CLAMP(cursor_y, 0, refresh_frame->get_h() - 1);
2308
2309                         row1 = cursor_y - radius;
2310                         row2 = cursor_y + radius;
2311                         column1 = cursor_x - radius;
2312                         column2 = cursor_x + radius;
2313                         CLAMP(row1, 0, refresh_frame->get_h() - 1);
2314                         CLAMP(row2, 0, refresh_frame->get_h() - 1);
2315                         CLAMP(column1, 0, refresh_frame->get_w() - 1);
2316                         CLAMP(column2, 0, refresh_frame->get_w() - 1);
2317                         if( row2 <= row1 ) row2 = row1 + 1;
2318                         if( column2 <= column1 ) column2 = column1 + 1;
2319
2320
2321 // hide it
2322                         if( gui->eyedrop_visible ) {
2323                                 int temp;
2324                                 do_eyedrop(temp, 0, 1);
2325                                 gui->eyedrop_visible = 0;
2326                         }
2327
2328                         gui->eyedrop_x = cursor_x;
2329                         gui->eyedrop_y = cursor_y;
2330
2331 // show it
2332                         {
2333                                 int temp;
2334                                 do_eyedrop(temp, 0, 1);
2335                                 gui->eyedrop_visible = 1;
2336                         }
2337
2338 // Decompression coefficients straight out of jpeglib
2339 #define V_TO_R    1.40200
2340 #define V_TO_G    -0.71414
2341
2342 #define U_TO_G    -0.34414
2343 #define U_TO_B    1.77200
2344
2345 #define GET_COLOR(type, components, max, do_yuv) \
2346 { \
2347         type *row = (type*)(refresh_frame->get_rows()[i]) + \
2348                 j * components; \
2349         float red = (float)*row++ / max; \
2350         float green = (float)*row++ / max; \
2351         float blue = (float)*row++ / max; \
2352         if( do_yuv ) \
2353         { \
2354                 float r = red + V_TO_R * (blue - 0.5); \
2355                 float g = red + U_TO_G * (green - 0.5) + V_TO_G * (blue - 0.5); \
2356                 float b = red + U_TO_B * (green - 0.5); \
2357                 mwindow->edl->local_session->red += r; \
2358                 mwindow->edl->local_session->green += g; \
2359                 mwindow->edl->local_session->blue += b; \
2360                 if( r > mwindow->edl->local_session->red_max ) mwindow->edl->local_session->red_max = r; \
2361                 if( g > mwindow->edl->local_session->green_max ) mwindow->edl->local_session->green_max = g; \
2362                 if( b > mwindow->edl->local_session->blue_max ) mwindow->edl->local_session->blue_max = b; \
2363         } \
2364         else \
2365         { \
2366                 mwindow->edl->local_session->red += red; \
2367                 mwindow->edl->local_session->green += green; \
2368                 mwindow->edl->local_session->blue += blue; \
2369                 if( red > mwindow->edl->local_session->red_max ) mwindow->edl->local_session->red_max = red; \
2370                 if( green > mwindow->edl->local_session->green_max ) mwindow->edl->local_session->green_max = green; \
2371                 if( blue > mwindow->edl->local_session->blue_max ) mwindow->edl->local_session->blue_max = blue; \
2372         } \
2373 }
2374
2375
2376
2377                         mwindow->edl->local_session->red = 0;
2378                         mwindow->edl->local_session->green = 0;
2379                         mwindow->edl->local_session->blue = 0;
2380                         mwindow->edl->local_session->red_max = 0;
2381                         mwindow->edl->local_session->green_max = 0;
2382                         mwindow->edl->local_session->blue_max = 0;
2383                         for( int i = row1; i < row2; i++ ) {
2384                                 for( int j = column1; j < column2; j++ ) {
2385                                         switch( refresh_frame->get_color_model() )
2386                                         {
2387                                                 case BC_YUV888:
2388                                                         GET_COLOR(unsigned char, 3, 0xff, 1);
2389                                                         break;
2390                                                 case BC_YUVA8888:
2391                                                         GET_COLOR(unsigned char, 4, 0xff, 1);
2392                                                         break;
2393                                                 case BC_YUV161616:
2394                                                         GET_COLOR(uint16_t, 3, 0xffff, 1);
2395                                                         break;
2396                                                 case BC_YUVA16161616:
2397                                                         GET_COLOR(uint16_t, 4, 0xffff, 1);
2398                                                         break;
2399                                                 case BC_RGB888:
2400                                                         GET_COLOR(unsigned char, 3, 0xff, 0);
2401                                                         break;
2402                                                 case BC_RGBA8888:
2403                                                         GET_COLOR(unsigned char, 4, 0xff, 0);
2404                                                         break;
2405                                                 case BC_RGB_FLOAT:
2406                                                         GET_COLOR(float, 3, 1.0, 0);
2407                                                         break;
2408                                                 case BC_RGBA_FLOAT:
2409                                                         GET_COLOR(float, 4, 1.0, 0);
2410                                                         break;
2411                                         }
2412                                 }
2413                         }
2414
2415                         mwindow->edl->local_session->red /= (row2 - row1) * (column2 - column1);
2416                         mwindow->edl->local_session->green /= (row2 - row1) * (column2 - column1);
2417                         mwindow->edl->local_session->blue /= (row2 - row1) * (column2 - column1);
2418
2419                 }
2420                 else {
2421                         mwindow->edl->local_session->red = 0;
2422                         mwindow->edl->local_session->green = 0;
2423                         mwindow->edl->local_session->blue = 0;
2424                         gui->eyedrop_visible = 0;
2425                 }
2426
2427
2428                 gui->update_tool();
2429
2430
2431
2432                 result = 1;
2433 // Can't rerender since the color value is from the output of any effect it
2434 // goes into.
2435 //              rerender = 1;
2436                 mwindow->undo->update_undo_after(_("Eyedrop"), LOAD_SESSION);
2437         }
2438
2439         return result;
2440 }
2441
2442 int CWindowCanvas::need_overlays()
2443 {
2444         if( mwindow->edl->session->safe_regions ) return 1;
2445         if( mwindow->edl->session->cwindow_scrollbars ) return 1;
2446         if( gui->highlighted ) return 1;
2447         switch( mwindow->edl->session->cwindow_operation ) {
2448                 case CWINDOW_EYEDROP:
2449                         if( ! gui->eyedrop_visible ) break;
2450                 case CWINDOW_CAMERA:
2451                 case CWINDOW_PROJECTOR:
2452                 case CWINDOW_CROP:
2453                 case CWINDOW_MASK:
2454                 case CWINDOW_RULER:
2455                         return 1;
2456         }
2457         return 0;
2458 }
2459
2460 void CWindowCanvas::draw_overlays()
2461 {
2462         if( mwindow->edl->session->safe_regions ) {
2463                 draw_safe_regions();
2464         }
2465
2466         if( mwindow->edl->session->cwindow_scrollbars ) {
2467 // Always draw output rectangle
2468                 float x1, y1, x2, y2;
2469                 x1 = 0;
2470                 x2 = mwindow->edl->session->output_w;
2471                 y1 = 0;
2472                 y2 = mwindow->edl->session->output_h;
2473                 output_to_canvas(mwindow->edl, 0, x1, y1);
2474                 output_to_canvas(mwindow->edl, 0, x2, y2);
2475
2476                 get_canvas()->set_inverse();
2477                 get_canvas()->set_color(WHITE);
2478                 int ix1 = x1-1, iy1 = y1-1, ix2 = x2+1, iy2 = y2+1;
2479                 get_canvas()->draw_rectangle(ix1, iy1, ix2-ix1, iy2-iy1);
2480                 get_canvas()->set_opaque();
2481         }
2482
2483         if( gui->highlighted ) {
2484                 get_canvas()->set_color(WHITE);
2485                 get_canvas()->set_inverse();
2486                 get_canvas()->draw_rectangle(0, 0, get_canvas()->get_w(), get_canvas()->get_h());
2487                 get_canvas()->draw_rectangle(1, 1, get_canvas()->get_w() - 2, get_canvas()->get_h() - 2);
2488                 get_canvas()->set_opaque();
2489         }
2490
2491         int temp1 = 0, temp2 = 0;
2492 //printf("CWindowCanvas::draw_overlays 1 %d\n", mwindow->edl->session->cwindow_operation);
2493         switch( mwindow->edl->session->cwindow_operation )
2494         {
2495                 case CWINDOW_CAMERA:
2496                         draw_outlines(1);
2497                         break;
2498
2499                 case CWINDOW_PROJECTOR:
2500                         draw_outlines(0);
2501                         break;
2502
2503                 case CWINDOW_CROP:
2504                         draw_crop();
2505                         break;
2506
2507                 case CWINDOW_MASK:
2508                         do_mask(temp1, temp2, 0, 0, 1);
2509                         break;
2510
2511                 case CWINDOW_RULER:
2512                         do_ruler(1, 0, 0, 0);
2513                         break;
2514
2515                 case CWINDOW_EYEDROP:
2516                 if( gui->eyedrop_visible ) {
2517                         int rerender;
2518                         do_eyedrop(rerender, 0, 1);
2519                         gui->eyedrop_visible = 1;
2520                         break;
2521                 }
2522         }
2523 }
2524
2525 void CWindowCanvas::draw_safe_regions()
2526 {
2527         float action_x1, action_x2, action_y1, action_y2;
2528         float title_x1, title_x2, title_y1, title_y2;
2529
2530         action_x1 = mwindow->edl->session->output_w / 2 - mwindow->edl->session->output_w / 2 * 0.9;
2531         action_x2 = mwindow->edl->session->output_w / 2 + mwindow->edl->session->output_w / 2 * 0.9;
2532         action_y1 = mwindow->edl->session->output_h / 2 - mwindow->edl->session->output_h / 2 * 0.9;
2533         action_y2 = mwindow->edl->session->output_h / 2 + mwindow->edl->session->output_h / 2 * 0.9;
2534         title_x1 = mwindow->edl->session->output_w / 2 - mwindow->edl->session->output_w / 2 * 0.8;
2535         title_x2 = mwindow->edl->session->output_w / 2 + mwindow->edl->session->output_w / 2 * 0.8;
2536         title_y1 = mwindow->edl->session->output_h / 2 - mwindow->edl->session->output_h / 2 * 0.8;
2537         title_y2 = mwindow->edl->session->output_h / 2 + mwindow->edl->session->output_h / 2 * 0.8;
2538
2539         output_to_canvas(mwindow->edl, 0, action_x1, action_y1);
2540         output_to_canvas(mwindow->edl, 0, action_x2, action_y2);
2541         output_to_canvas(mwindow->edl, 0, title_x1, title_y1);
2542         output_to_canvas(mwindow->edl, 0, title_x2, title_y2);
2543
2544         get_canvas()->set_inverse();
2545         get_canvas()->set_color(WHITE);
2546
2547         get_canvas()->draw_rectangle((int)action_x1,
2548                         (int)action_y1,
2549                         (int)(action_x2 - action_x1),
2550                         (int)(action_y2 - action_y1));
2551         get_canvas()->draw_rectangle((int)title_x1,
2552                         (int)title_y1,
2553                         (int)(title_x2 - title_x1),
2554                         (int)(title_y2 - title_y1));
2555
2556         get_canvas()->set_opaque();
2557 }
2558
2559
2560 void CWindowCanvas::create_keyframe(int do_camera)
2561 {
2562         Track *affected_track = gui->cwindow->calculate_affected_track();
2563         if( affected_track ) {
2564                 double pos = mwindow->edl->local_session->get_selectionstart(1);
2565                 int64_t position = affected_track->to_units(pos, 0);
2566                 int ix = do_camera ? AUTOMATION_CAMERA_X : AUTOMATION_PROJECTOR_X;
2567                 int iy = do_camera ? AUTOMATION_CAMERA_Y : AUTOMATION_PROJECTOR_Y;
2568                 int iz = do_camera ? AUTOMATION_CAMERA_Z : AUTOMATION_PROJECTOR_Z;
2569                 FloatAuto *prev, *next;
2570                 FloatAutos **autos = (FloatAutos**)affected_track->automation->autos;
2571                 FloatAutos *x_autos = autos[ix];  prev = 0;  next = 0;
2572                 float x_value = x_autos->get_value(position, PLAY_FORWARD, prev, next);
2573                 FloatAutos *y_autos = autos[iy];  prev = 0;  next = 0;
2574                 float y_value = y_autos->get_value(position, PLAY_FORWARD, prev, next);
2575                 FloatAutos *z_autos = autos[iz];  prev = 0;  next = 0;
2576                 float z_value = z_autos->get_value(position, PLAY_FORWARD, prev, next);
2577                 FloatAuto *x_keyframe = 0, *y_keyframe = 0, *z_keyframe = 0;
2578
2579                 gui->cwindow->calculate_affected_autos(affected_track,
2580                         &x_keyframe, &y_keyframe, &z_keyframe,
2581                         do_camera, -1, -1, -1, 0);
2582                 x_keyframe->set_value(x_value);
2583                 y_keyframe->set_value(y_value);
2584                 z_keyframe->set_value(z_value);
2585
2586                 gui->sync_parameters(CHANGE_PARAMS, 1, 1);
2587         }
2588 }
2589
2590 void CWindowCanvas::camera_keyframe() { create_keyframe(1); }
2591 void CWindowCanvas::projector_keyframe() { create_keyframe(0); }
2592
2593 void CWindowCanvas::reset_keyframe(int do_camera)
2594 {
2595         Track *affected_track = gui->cwindow->calculate_affected_track();
2596         if( affected_track ) {
2597                 FloatAuto *x_keyframe = 0, *y_keyframe = 0, *z_keyframe = 0;
2598                 gui->cwindow->calculate_affected_autos(affected_track,
2599                         &x_keyframe, &y_keyframe, &z_keyframe,
2600                         do_camera, 1, 1, 1);
2601
2602                 x_keyframe->set_value(0);
2603                 y_keyframe->set_value(0);
2604                 z_keyframe->set_value(1);
2605
2606                 gui->sync_parameters(CHANGE_PARAMS, 1, 1);
2607         }
2608 }
2609
2610 void CWindowCanvas::reset_camera() { reset_keyframe(1); }
2611 void CWindowCanvas::reset_projector() { reset_keyframe(0); }
2612
2613
2614 int CWindowCanvas::test_crop(int button_press, int &redraw)
2615 {
2616         int result = 0;
2617         int handle_selected = -1;
2618         float x1 = mwindow->edl->session->crop_x1;
2619         float y1 = mwindow->edl->session->crop_y1;
2620         float x2 = mwindow->edl->session->crop_x2;
2621         float y2 = mwindow->edl->session->crop_y2;
2622         float cursor_x = get_cursor_x();
2623         float cursor_y = get_cursor_y();
2624         float canvas_x1 = x1;
2625         float canvas_y1 = y1;
2626         float canvas_x2 = x2;
2627         float canvas_y2 = y2;
2628         float canvas_cursor_x = cursor_x;
2629         float canvas_cursor_y = cursor_y;
2630
2631         canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
2632 // Use screen normalized coordinates for hot spot tests.
2633         output_to_canvas(mwindow->edl, 0, canvas_x1, canvas_y1);
2634         output_to_canvas(mwindow->edl, 0, canvas_x2, canvas_y2);
2635
2636
2637         if( gui->current_operation == CWINDOW_CROP ) {
2638                 handle_selected = gui->crop_handle;
2639         }
2640         else
2641         if( canvas_cursor_x >= canvas_x1 && canvas_cursor_x < canvas_x1 + CROPHANDLE_W &&
2642                 canvas_cursor_y >= canvas_y1 && canvas_cursor_y < canvas_y1 + CROPHANDLE_H ) {
2643                 handle_selected = 0;
2644                 gui->crop_origin_x = x1;
2645                 gui->crop_origin_y = y1;
2646         }
2647         else
2648         if( canvas_cursor_x >= canvas_x2 - CROPHANDLE_W && canvas_cursor_x < canvas_x2 &&
2649                 canvas_cursor_y >= canvas_y1 && canvas_cursor_y < canvas_y1 + CROPHANDLE_H ) {
2650                 handle_selected = 1;
2651                 gui->crop_origin_x = x2;
2652                 gui->crop_origin_y = y1;
2653         }
2654         else
2655         if( canvas_cursor_x >= canvas_x1 && canvas_cursor_x < canvas_x1 + CROPHANDLE_W &&
2656                 canvas_cursor_y >= canvas_y2 - CROPHANDLE_H && canvas_cursor_y < canvas_y2 ) {
2657                 handle_selected = 2;
2658                 gui->crop_origin_x = x1;
2659                 gui->crop_origin_y = y2;
2660         }
2661         else
2662         if( canvas_cursor_x >= canvas_x2 - CROPHANDLE_W && canvas_cursor_x < canvas_x2 &&
2663                 canvas_cursor_y >= canvas_y2 - CROPHANDLE_H && canvas_cursor_y < canvas_y2 ) {
2664                 handle_selected = 3;
2665                 gui->crop_origin_x = x2;
2666                 gui->crop_origin_y = y2;
2667         }
2668         else
2669 // Start new box
2670         {
2671                 gui->crop_origin_x = cursor_x;
2672                 gui->crop_origin_y = cursor_y;
2673         }
2674
2675 // printf("test crop %d %d\n",
2676 //      gui->current_operation,
2677 //      handle_selected);
2678
2679 // Start dragging.
2680         if( button_press ) {
2681                 if( gui->alt_down() ) {
2682                         gui->crop_translate = 1;
2683                         gui->crop_origin_x1 = x1;
2684                         gui->crop_origin_y1 = y1;
2685                         gui->crop_origin_x2 = x2;
2686                         gui->crop_origin_y2 = y2;
2687                 }
2688                 else
2689                         gui->crop_translate = 0;
2690
2691                 gui->current_operation = CWINDOW_CROP;
2692                 gui->crop_handle = handle_selected;
2693                 gui->x_origin = cursor_x;
2694                 gui->y_origin = cursor_y;
2695                 gui->tool_panel->raise_window();
2696                 result = 1;
2697
2698                 if( handle_selected < 0 && !gui->crop_translate ) {
2699                         x2 = x1 = cursor_x;
2700                         y2 = y1 = cursor_y;
2701                         mwindow->edl->session->crop_x1 = (int)x1;
2702                         mwindow->edl->session->crop_y1 = (int)y1;
2703                         mwindow->edl->session->crop_x2 = (int)x2;
2704                         mwindow->edl->session->crop_y2 = (int)y2;
2705                         redraw = 1;
2706                 }
2707         }
2708         else
2709 // Translate all 4 points
2710         if( gui->current_operation == CWINDOW_CROP && gui->crop_translate ) {
2711                 x1 = cursor_x - gui->x_origin + gui->crop_origin_x1;
2712                 y1 = cursor_y - gui->y_origin + gui->crop_origin_y1;
2713                 x2 = cursor_x - gui->x_origin + gui->crop_origin_x2;
2714                 y2 = cursor_y - gui->y_origin + gui->crop_origin_y2;
2715
2716                 mwindow->edl->session->crop_x1 = (int)x1;
2717                 mwindow->edl->session->crop_y1 = (int)y1;
2718                 mwindow->edl->session->crop_x2 = (int)x2;
2719                 mwindow->edl->session->crop_y2 = (int)y2;
2720                 result = 1;
2721                 redraw = 1;
2722         }
2723         else
2724 // Update dragging
2725         if( gui->current_operation == CWINDOW_CROP ) {
2726                 switch( gui->crop_handle ) {
2727                 case -1:
2728                         x1 = gui->crop_origin_x;
2729                         y1 = gui->crop_origin_y;
2730                         x2 = gui->crop_origin_x;
2731                         y2 = gui->crop_origin_y;
2732                         if( cursor_x < gui->x_origin ) {
2733                                 if( cursor_y < gui->y_origin ) {
2734                                                 x1 = cursor_x;
2735                                                 y1 = cursor_y;
2736                                         }
2737                                         else
2738                                         if( cursor_y >= gui->y_origin ) {
2739                                                 x1 = cursor_x;
2740                                                 y2 = cursor_y;
2741                                         }
2742                                 }
2743                                 else
2744                                 if( cursor_x  >= gui->x_origin ) {
2745                                         if( cursor_y < gui->y_origin ) {
2746                                                 y1 = cursor_y;
2747                                                 x2 = cursor_x;
2748                                         }
2749                                         else
2750                                         if( cursor_y >= gui->y_origin ) {
2751                                                 x2 = cursor_x;
2752                                                 y2 = cursor_y;
2753                                         }
2754                                 }
2755
2756 // printf("test crop %d %d %d %d\n",
2757 //      mwindow->edl->session->crop_x1,
2758 //      mwindow->edl->session->crop_y1,
2759 //      mwindow->edl->session->crop_x2,
2760 //      mwindow->edl->session->crop_y2);
2761                                 break;
2762                         case 0:
2763                                 x1 = cursor_x - gui->x_origin + gui->crop_origin_x;
2764                                 y1 = cursor_y - gui->y_origin + gui->crop_origin_y;
2765                                 break;
2766                         case 1:
2767                                 x2 = cursor_x - gui->x_origin + gui->crop_origin_x;
2768                                 y1 = cursor_y - gui->y_origin + gui->crop_origin_y;
2769                                 break;
2770                         case 2: