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