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