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