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