tweak zoom/fullscr to remember cwdw scale after fullscr
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / canvas.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008-2017 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 "bcsignals.h"
23 #include "canvas.h"
24 #include "clip.h"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "keys.h"
28 #include "language.h"
29 #include "mainsession.h"
30 #include "mwindowgui.h"
31 #include "mutex.h"
32 #include "mwindow.h"
33 #include "playback3d.h"
34 #include "videodevice.h"
35 #include "vframe.h"
36
37
38
39 Canvas::Canvas(MWindow *mwindow, BC_WindowBase *subwindow,
40         int x, int y, int w, int h, int output_w, int output_h,
41         int use_scrollbars)
42 {
43         reset();
44
45         int xs10 = xS(10), ys10 = yS(10);
46         if(w < xs10) w = xs10;
47         if(h < ys10) h = ys10;
48         this->mwindow = mwindow;
49         this->subwindow = subwindow;
50         this->x = x;
51         this->y = y;
52         this->w = w;
53         this->h = h;
54         this->output_w = output_w;
55         this->output_h = output_h;
56         this->use_scrollbars = use_scrollbars;
57         this->canvas_auxwindow = 0;
58         this->scr_w0 = subwindow->get_screen_w(0, 0);
59         this->root_w = subwindow->get_root_w(0);
60         this->root_h = subwindow->get_root_h(0);
61         canvas_lock = new Condition(1, "Canvas::canvas_lock", 1);
62 }
63
64 Canvas::~Canvas()
65 {
66         if(refresh_frame) delete refresh_frame;
67         delete canvas_menu;
68         if(yscroll) delete yscroll;
69         if(xscroll) delete xscroll;
70         delete canvas_subwindow;
71         delete canvas_fullscreen;
72         delete canvas_lock;
73 }
74
75 void Canvas::reset()
76 {
77         use_scrollbars = 0;
78         output_w = 0;
79         output_h = 0;
80         xscroll = 0;
81         yscroll = 0;
82         refresh_frame = 0;
83         canvas_subwindow = 0;
84         canvas_fullscreen = 0;
85         is_processing = 0;
86         is_fullscreen = 0;
87         cursor_inside = 0;
88 }
89
90 BC_WindowBase *Canvas::lock_canvas(const char *loc)
91 {
92         canvas_lock->lock(loc);
93         BC_WindowBase *wdw = get_canvas();
94         if( wdw ) wdw->lock_window(loc);
95         return wdw;
96 }
97
98 void Canvas::unlock_canvas()
99 {
100         BC_WindowBase *wdw = get_canvas();
101         if( wdw ) wdw->unlock_window();
102         canvas_lock->unlock();
103 }
104
105 BC_WindowBase* Canvas::get_canvas()
106 {
107         if(get_fullscreen() && canvas_fullscreen)
108                 return canvas_fullscreen;
109         return canvas_auxwindow ? canvas_auxwindow : canvas_subwindow;
110 }
111
112 void Canvas::use_auxwindow(BC_WindowBase *aux)
113 {
114         canvas_auxwindow = aux;
115 }
116
117 void Canvas::use_cwindow()
118 {
119         canvas_menu->use_cwindow();
120         fullscreen_menu->use_cwindow();
121 }
122
123 void Canvas::use_rwindow()
124 {
125         canvas_menu->use_rwindow();
126 }
127
128 void Canvas::use_vwindow()
129 {
130         canvas_menu->use_vwindow();
131 }
132
133 int Canvas::get_fullscreen()
134 {
135         return is_fullscreen;
136 }
137
138 // Get dimensions given a zoom
139 void Canvas::calculate_sizes(float aspect_ratio,
140                 int output_w, int output_h, float zoom,
141                 int &w, int &h)
142 {
143 // Horizontal stretch
144         if( (float)output_w/output_h <= aspect_ratio ) {
145                 w = (float)output_h * aspect_ratio * zoom;
146                 h = (float)output_h * zoom;
147         }
148         else {
149 // Vertical stretch
150                 h = (float)output_w / aspect_ratio * zoom;
151                 w = (float)output_w * zoom;
152         }
153 }
154
155 float Canvas::get_x_offset(EDL *edl, int single_channel,
156                 float zoom_x, float conformed_w, float conformed_h)
157 {
158         if( use_scrollbars ) return get_xscroll();
159         int canvas_w = get_canvas()->get_w(), out_w = canvas_w;
160         int canvas_h = get_canvas()->get_h(), out_h = canvas_h;
161         float conformed_ratio = conformed_w/conformed_h;
162         if( (float)out_w/out_h > conformed_ratio )
163                 out_w = out_h * conformed_ratio + 0.5f;
164         float ret = out_w >= canvas_w ? 0 : -0.5f*(canvas_w - out_w) / zoom_x;
165         return ret;
166 }
167
168 float Canvas::get_y_offset(EDL *edl, int single_channel,
169                 float zoom_y, float conformed_w, float conformed_h)
170 {
171         if( use_scrollbars ) return get_yscroll();
172         int canvas_w = get_canvas()->get_w(), out_w = canvas_w;
173         int canvas_h = get_canvas()->get_h(), out_h = canvas_h;
174         float conformed_ratio = conformed_w/conformed_h;
175         if( (float)out_w/out_h <= conformed_ratio )
176                 out_h = out_w / conformed_ratio + 0.5f;
177         float ret = out_h >= canvas_h ? 0 : -0.5f*(canvas_h - out_h) / zoom_y;
178         return ret;
179 }
180
181 // This may not be used anymore
182 void Canvas::check_boundaries(EDL *edl, int &x, int &y, float &zoom)
183 {
184         if(x + w_visible > w_needed) x = w_needed - w_visible;
185         if(y + h_visible > h_needed) y = h_needed - h_visible;
186
187         if(x < 0) x = 0;
188         if(y < 0) y = 0;
189 }
190
191 void Canvas::update_scrollbars(EDL *edl, int flush)
192 {
193         if( edl )
194                 get_scrollbars(edl);
195         if( use_scrollbars ) {
196                 if( xscroll ) xscroll->update_length(w_needed, get_xscroll(), w_visible, flush);
197                 if( yscroll ) yscroll->update_length(h_needed, get_yscroll(), h_visible, flush);
198         }
199 }
200
201 float Canvas::get_auto_zoom(EDL *edl)
202 {
203         float conformed_w, conformed_h;
204         edl->calculate_conformed_dimensions(0, conformed_w, conformed_h);
205         BC_WindowBase *window = get_canvas();
206         int window_w = window ? window->get_w() : w;
207         int window_h = window ? window->get_h() : h;
208         float zoom_x = window_w / conformed_w;
209         float zoom_y = window_h / conformed_h;
210         return zoom_x < zoom_y ? zoom_x : zoom_y;
211 }
212 void Canvas::zoom_auto()
213 {
214         use_scrollbars = 0;
215 }
216
217 void Canvas::get_zooms(EDL *edl, int single_channel,
218                 float &zoom_x, float &zoom_y,
219                 float &conformed_w, float &conformed_h)
220 {
221         edl->calculate_conformed_dimensions(single_channel,
222                         conformed_w, conformed_h);
223
224         float zoom = get_zoom();
225         if( !use_scrollbars || !zoom ) zoom = get_auto_zoom(edl);
226         zoom_x = zoom * conformed_w / get_output_w(edl);
227         zoom_y = zoom * conformed_h / get_output_h(edl);
228 }
229
230 void Canvas::set_zoom(EDL *edl, float zoom)
231 {
232         BC_WindowBase *window = get_canvas();
233         int cw = window ? window->get_w() : w;
234         int ch = window ? window->get_h() : h;
235         float cx = 0.5f * cw, cy = 0.5f * ch;
236         set_zoom(edl, zoom, cx, cy);
237 }
238
239 void Canvas::set_zoom(EDL *edl, float zoom, float cx, float cy)
240 {
241         float output_x = cx, output_y = cy;
242         canvas_to_output(edl, 0, output_x, output_y);
243         update_zoom(0, 0, zoom);
244         float zoom_x, zoom_y, conformed_w, conformed_h;
245         get_zooms(edl, 0, zoom_x, zoom_y, conformed_w, conformed_h);
246         if( zoom ) {
247                 output_x -= cx / zoom_x;
248                 output_y -= cy / zoom_y;
249         }
250         else {
251                 output_x = get_x_offset(edl, 0, zoom_x, conformed_w, conformed_h);
252                 output_y = get_y_offset(edl, 0, zoom_y, conformed_w, conformed_h);
253         }
254         update_zoom(output_x, output_y, zoom);
255 }
256
257
258 // Convert a coordinate on the canvas to a coordinate on the output
259 void Canvas::canvas_to_output(EDL *edl, int single_channel, float &x, float &y)
260 {
261         float zoom_x, zoom_y, conformed_w, conformed_h;
262         get_zooms(edl, single_channel, zoom_x, zoom_y, conformed_w, conformed_h);
263
264 //printf("Canvas::canvas_to_output y=%f zoom_y=%f y_offset=%f\n",
265 //      y, zoom_y, get_y_offset(edl, single_channel, zoom_y, conformed_w, conformed_h));
266
267         x = (float)x / zoom_x + get_x_offset(edl, single_channel, zoom_x, conformed_w, conformed_h);
268         y = (float)y / zoom_y + get_y_offset(edl, single_channel, zoom_y, conformed_w, conformed_h);
269 }
270
271 void Canvas::output_to_canvas(EDL *edl, int single_channel, float &x, float &y)
272 {
273         float zoom_x, zoom_y, conformed_w, conformed_h;
274         get_zooms(edl, single_channel, zoom_x, zoom_y, conformed_w, conformed_h);
275
276 //printf("Canvas::output_to_canvas x=%f zoom_x=%f x_offset=%f\n", x, zoom_x, get_x_offset(edl, single_channel, zoom_x, conformed_w));
277
278         x = (float)zoom_x * (x - get_x_offset(edl, single_channel, zoom_x, conformed_w, conformed_h));
279         y = (float)zoom_y * (y - get_y_offset(edl, single_channel, zoom_y, conformed_w, conformed_h));
280 }
281
282
283
284 void Canvas::get_transfers(EDL *edl,
285         float &output_x1, float &output_y1, float &output_x2, float &output_y2,
286         float &canvas_x1, float &canvas_y1, float &canvas_x2, float &canvas_y2,
287         int canvas_w, int canvas_h)
288 {
289 //printf("Canvas::get_transfers %d canvas_w=%d canvas_h=%d\n", 
290 // __LINE__,  canvas_w, canvas_h);
291 // automatic canvas size detection
292         if( canvas_w < 0 ) canvas_w = get_canvas()->get_w();
293         if( canvas_h < 0 ) canvas_h = get_canvas()->get_h();
294
295         float in_x1, in_y1, in_x2, in_y2;
296         float out_x1, out_y1, out_x2, out_y2;
297         float zoom_x, zoom_y, conformed_w, conformed_h;
298
299         get_zooms(edl, 0, zoom_x, zoom_y, conformed_w, conformed_h);
300         out_x1 = 0;  out_x2 = canvas_w;
301         out_y1 = 0;  out_y2 = canvas_h;
302         in_x1 = 0;   in_x2  = canvas_w;
303         in_y1 = 0;   in_y2  = canvas_h;
304         canvas_to_output(edl, 0, in_x1, in_y1);
305         canvas_to_output(edl, 0, in_x2, in_y2);
306
307         if( in_x1 < 0 ) {
308                 out_x1 += -in_x1 * zoom_x;
309                 in_x1 = 0;
310         }
311
312         if( in_y1 < 0 ) {
313                 out_y1 += -in_y1 * zoom_y;
314                 in_y1 = 0;
315         }
316
317         int output_w = get_output_w(edl);
318         int output_h = get_output_h(edl);
319         if( in_x2 > output_w ) {
320                 out_x2 -= (in_x2 - output_w) * zoom_x;
321                 in_x2 = output_w;
322         }
323
324         if( in_y2 > output_h ) {
325                 out_y2 -= (in_y2 - output_h) * zoom_y;
326                 in_y2 = output_h;
327         }
328
329         output_x1 = in_x1;   output_x2 = in_x2;
330         output_y1 = in_y1;   output_y2 = in_y2;
331         canvas_x1 = out_x1;  canvas_x2 = out_x2;
332         canvas_y1 = out_y1;  canvas_y2 = out_y2;
333
334 // Clamp to minimum value
335         if( output_x1 < 0 ) output_x1 = 0;
336         if( output_x2 < output_x1 ) output_x2 = output_x1;
337         if( canvas_x1 < 0 ) canvas_x1 = 0;
338         if( canvas_x2 < canvas_x1 ) canvas_x2 = canvas_x1;
339 // printf("Canvas::get_transfers %d %f,%f %f,%f -> %f,%f %f,%f\n", __LINE__,
340 //  output_x1, output_y1, output_x2, output_y2,
341 //  canvas_x1, canvas_y1, canvas_x2, canvas_y2);
342 }
343
344 int Canvas::scrollbars_exist()
345 {
346         return use_scrollbars && (xscroll || yscroll);
347 }
348
349 int Canvas::get_output_w(EDL *edl)
350 {
351         return edl->session->output_w;
352 }
353
354 int Canvas::get_output_h(EDL *edl)
355 {
356         return edl->session->output_h;
357 }
358
359
360 int Canvas::get_scrollbars(EDL *edl)
361 {
362         int ret = 0;
363         BC_WindowBase *window = get_canvas();
364         if( !window ) use_scrollbars = 0;
365         int canvas_w = w, canvas_h = h;
366
367         w_needed = w_visible = edl ? edl->session->output_w : view_w;
368         h_needed = h_visible = edl ? edl->session->output_h : view_h;
369
370         int need_xscroll = 0, need_yscroll = 0;
371         if( edl && use_scrollbars ) {
372                 float zoom_x, zoom_y, conformed_w, conformed_h;
373                 get_zooms(edl, 0, zoom_x, zoom_y, conformed_w, conformed_h);
374                 w_visible = canvas_w / zoom_x;
375                 h_visible = canvas_h / zoom_y;
376                 float output_x = 0, output_y = 0;
377                 output_to_canvas(edl, 0, output_x, output_y);
378                 if( output_x < 0 ) need_xscroll = 1;
379                 if( output_y < 0 ) need_yscroll = 1;
380                 output_x = w_needed, output_y = h_needed;
381                 output_to_canvas(edl, 0, output_x, output_y);
382                 if( output_x > canvas_w ) need_xscroll = 1;
383                 if( output_y > canvas_h ) need_yscroll = 1;
384                 if( need_xscroll ) {
385                         canvas_h -= BC_ScrollBar::get_span(SCROLL_HORIZ);
386                         h_visible = canvas_h / zoom_y;
387                 }
388                 if( need_yscroll ) {
389                         canvas_w -= BC_ScrollBar::get_span(SCROLL_VERT);
390                         w_visible = canvas_w / zoom_x;
391                 }
392                 view_w = canvas_w;
393                 view_h = canvas_h;
394         }
395
396         if( need_xscroll ) {
397                 if( !xscroll ) {
398                         xscroll = new CanvasXScroll(edl, this, view_x, view_y + view_h,
399                                         w_needed, get_xscroll(), w_visible, view_w);
400                         subwindow->add_subwindow(xscroll);
401                         xscroll->show_window(0);
402                 }
403                 else
404                         xscroll->reposition_window(view_x, view_y + view_h, view_w);
405
406                 if( xscroll->get_length() != w_needed ||
407                     xscroll->get_handlelength() != w_visible )
408                         xscroll->update_length(w_needed, get_xscroll(), w_visible, 0);
409                 ret = 1;
410         }
411         else if( xscroll ) {
412                 delete xscroll;  xscroll = 0;
413                 ret = 1;
414         }
415
416         if( need_yscroll ) {
417                 if( !yscroll ) {
418                         yscroll = new CanvasYScroll(edl, this, view_x + view_w, view_y,
419                                         h_needed, get_yscroll(), h_visible, view_h);
420                         subwindow->add_subwindow(yscroll);
421                         yscroll->show_window(0);
422                 }
423                 else
424                         yscroll->reposition_window(view_x + view_w, view_y, view_h);
425
426                 if( yscroll->get_length() != edl->session->output_h ||
427                     yscroll->get_handlelength() != h_visible )
428                         yscroll->update_length(h_needed, get_yscroll(), h_visible, 0);
429                 ret = 1;
430         }
431         else if( yscroll ) {
432                 delete yscroll;  yscroll = 0;
433                 ret = 1;
434         }
435         return ret;
436 }
437
438
439 void Canvas::update_geometry(EDL *edl, int x, int y, int w, int h)
440 {
441         int redraw = 0;
442         if( this->x != x || this->y != y ||
443             this->w != w || this->h != h )
444                 redraw = 1;
445         if( !redraw )
446                 redraw = get_scrollbars(edl);
447         if( redraw )
448                 reposition_window(edl, x, y, w, h);
449 }
450
451 void Canvas::reposition_window(EDL *edl, int x, int y, int w, int h)
452 {
453         this->x = view_x = x;  this->y = view_y = y;
454         this->w = view_w = w;  this->h = view_h = h;
455 //printf("Canvas::reposition_window 1\n");
456         get_scrollbars(edl);
457 //printf("Canvas::reposition_window %d %d %d %d\n", view_x, view_y, view_w, view_h);
458         if( canvas_subwindow ) {
459                 canvas_subwindow->reposition_window(view_x, view_y, view_w, view_h);
460
461 // Need to clear out the garbage in the back
462                 if( canvas_subwindow->get_video_on() )
463                         clear_borders(edl);
464         }
465         refresh(0);
466 }
467
468 // must hold window lock
469 int Canvas::refresh(int flush)
470 {
471         BC_WindowBase *window = get_canvas();
472         if( !window ) return 0;
473 // relock in lock order to prevent deadlock
474         window->unlock_window();
475         lock_canvas("Canvas::refresh");
476         draw_refresh(flush);
477         canvas_lock->unlock();
478         return 1;
479 }
480 // must not hold locks
481 int Canvas::redraw(int flush)
482 {
483         lock_canvas("Canvas::redraw");
484         draw_refresh(flush);
485         unlock_canvas();
486         return 1;
487 }
488
489 void Canvas::set_cursor(int cursor)
490 {
491         get_canvas()->set_cursor(cursor, 0, 1);
492 }
493
494 int Canvas::get_cursor_x()
495 {
496         return get_canvas()->get_cursor_x();
497 }
498
499 int Canvas::get_cursor_y()
500 {
501         return get_canvas()->get_cursor_y();
502 }
503
504 int Canvas::get_buttonpress()
505 {
506         return get_canvas()->get_buttonpress();
507 }
508
509
510 void Canvas::create_objects(EDL *edl)
511 {
512         view_x = x;  view_y = y;
513         view_w = w;  view_h = h;
514         get_scrollbars(edl);
515
516         subwindow->unlock_window();
517         create_canvas();
518         subwindow->lock_window("Canvas::create_objects");
519
520         subwindow->add_subwindow(canvas_menu = new CanvasPopup(this));
521         canvas_menu->create_objects();
522
523         subwindow->add_subwindow(fullscreen_menu = new CanvasFullScreenPopup(this));
524         fullscreen_menu->create_objects();
525
526 }
527
528 int Canvas::button_press_event()
529 {
530         int result = 0;
531
532         if(get_canvas()->get_buttonpress() == 3)
533         {
534                 if(get_fullscreen())
535                         fullscreen_menu->activate_menu();
536                 else
537                         canvas_menu->activate_menu();
538                 result = 1;
539         }
540
541         return result;
542 }
543
544 void Canvas::start_single()
545 {
546         is_processing = 1;
547         status_event();
548 }
549
550 void Canvas::stop_single()
551 {
552         is_processing = 0;
553         status_event();
554 }
555
556 void Canvas::start_video()
557 {
558         if(get_canvas())
559         {
560                 get_canvas()->start_video();
561                 status_event();
562         }
563 }
564
565 void Canvas::stop_video()
566 {
567         if(get_canvas())
568         {
569                 get_canvas()->stop_video();
570                 status_event();
571         }
572 }
573
574 int Canvas::set_fullscreen(int on, int unlock)
575 {
576         int ret = 0;
577         BC_WindowBase *window = get_canvas();
578         if( unlock )
579                 window->unlock_window();
580         if( on && !get_fullscreen() ) {
581                 start_fullscreen();
582                 ret = 1;
583         }
584         if( !on && get_fullscreen() ) {
585                 stop_fullscreen();
586                 ret = 1;
587         }
588         if( unlock )
589                 window->lock_window("Canvas::set_fullscreen");
590         return ret;
591 }
592
593 void Canvas::start_fullscreen()
594 {
595         is_fullscreen = 1;
596         create_canvas();
597 }
598
599 void Canvas::stop_fullscreen()
600 {
601         is_fullscreen = 0;
602         create_canvas();
603 }
604
605 void Canvas::create_canvas()
606 {
607         canvas_lock->lock("Canvas::create_canvas");
608         int video_on = 0;
609         BC_WindowBase *wdw = 0;
610         if( !get_fullscreen() ) {
611 // Enter windowed
612                 if( canvas_fullscreen ) {
613                         canvas_fullscreen->lock_window("Canvas::create_canvas 1");
614                         video_on = canvas_fullscreen->get_video_on();
615                         if( video_on ) canvas_fullscreen->stop_video();
616                         canvas_fullscreen->hide_window();
617                         canvas_fullscreen->unlock_window();
618                 }
619                 if( !canvas_auxwindow && !canvas_subwindow ) {
620                         subwindow->add_subwindow(canvas_subwindow = new CanvasOutput(this,
621                                 view_x, view_y, view_w, view_h));
622                 }
623                 wdw = get_canvas();
624                 wdw->lock_window("Canvas::create_canvas 2");
625         }
626         else {
627 // Enter fullscreen
628                 wdw = canvas_auxwindow ? canvas_auxwindow : canvas_subwindow;
629                 wdw->lock_window("Canvas::create_canvas 3");
630                 video_on = wdw->get_video_on();
631                 if( video_on ) wdw->stop_video();
632                 int x, y, w, h;
633                 wdw->get_fullscreen_geometry(x, y, w, h);
634                 wdw->unlock_window();
635                 if( canvas_fullscreen ) {
636                         if( x != canvas_fullscreen->get_x() ||
637                             y != canvas_fullscreen->get_y() ||
638                             w != canvas_fullscreen->get_w() ||
639                             h != canvas_fullscreen->get_h() ) {
640                                 delete canvas_fullscreen;
641                                 canvas_fullscreen = 0;
642                         }
643                 }
644                 if( !canvas_fullscreen )
645                         canvas_fullscreen = new CanvasFullScreen(this, w, h);
646                 wdw = canvas_fullscreen;
647                 wdw->lock_window("Canvas::create_canvas 4");
648                 wdw->show_window();
649                 wdw->sync_display();
650                 wdw->reposition_window(x, y);
651         }
652
653         if( video_on )
654                 wdw->start_video();
655         else
656                 draw_refresh(1);
657
658         wdw->focus();
659         wdw->unlock_window();
660         canvas_lock->unlock();
661 }
662
663
664 int Canvas::cursor_leave_event_base(BC_WindowBase *caller)
665 {
666         int result = 0;
667         if(cursor_inside) result = cursor_leave_event();
668         cursor_inside = 0;
669         return result;
670 }
671
672 int Canvas::cursor_enter_event_base(BC_WindowBase *caller)
673 {
674         int result = 0;
675         if(caller->is_event_win() && caller->cursor_inside())
676         {
677                 cursor_inside = 1;
678                 result = cursor_enter_event();
679         }
680         return result;
681 }
682
683 int Canvas::button_press_event_base(BC_WindowBase *caller)
684 {
685         if(caller->is_event_win() && caller->cursor_inside())
686         {
687                 return button_press_event();
688         }
689         return 0;
690 }
691
692 int Canvas::keypress_event(BC_WindowBase *caller)
693 {
694         int key = caller->get_keypress();
695         int on = -1;
696         switch( key ) {
697         case 'f':
698                 on = get_fullscreen() ? 0 : 1;
699                 break;
700         case ESC:
701                 on = 0;
702                 break;
703         default:
704                 return 0;
705         }
706         if( on >= 0 )
707                 set_fullscreen(on);
708         return 1;
709 }
710
711 void Canvas::update_refresh(VideoDevice *device, VFrame *output_frame)
712 {
713         int best_color_model = output_frame->get_color_model();
714         int use_opengl =
715                 device->out_config->driver == PLAYBACK_X11_GL &&
716                 output_frame->get_opengl_state() != VFrame::RAM;
717
718 // OpenGL does YUV->RGB in the compositing step
719         if( use_opengl )
720                 best_color_model = BC_RGB888;
721         else if( BC_CModels::has_alpha(best_color_model) ) {
722                 best_color_model =
723                         BC_CModels::is_float(best_color_model ) ?
724                                 BC_RGB_FLOAT :
725                         BC_CModels::is_yuv(best_color_model ) ?
726                                 ( BC_CModels::calculate_pixelsize(best_color_model) > 8 ?
727                                         BC_YUV161616 : BC_YUV888 ) :
728                                 ( BC_CModels::calculate_pixelsize(best_color_model) > 8 ?
729                                         BC_RGB161616 : BC_RGB888 ) ;
730         }
731         int out_w = output_frame->get_w();
732         int out_h = output_frame->get_h();
733         if( refresh_frame &&
734            (refresh_frame->get_w() != out_w ||
735             refresh_frame->get_h() != out_h ||
736             refresh_frame->get_color_model() != best_color_model ) ) {
737 // x11 direct render uses BC_BGR8888, use tranfer_from to remap
738                 delete refresh_frame;  refresh_frame = 0;
739         }
740
741         if( !refresh_frame ) {
742                 refresh_frame =
743                         new VFrame(out_w, out_h, best_color_model);
744         }
745
746         if( use_opengl ) {
747                 unlock_canvas();
748                 mwindow->playback_3d->copy_from(this, refresh_frame, output_frame, 0);
749                 lock_canvas("Canvas::update_refresh");
750         }
751         else
752                 refresh_frame->transfer_from(output_frame, -1);
753 }
754
755 void Canvas::clear(int flash)
756 {
757         BC_WindowBase *window = get_canvas();
758         if( !window ) return;
759         window->set_bg_color(get_clear_color());
760         window->clear_box(0,0, window->get_w(), window->get_h());
761         if( flash ) window->flash();
762 }
763
764 void Canvas::clear_borders(EDL *edl)
765 {
766         BC_WindowBase *window = get_canvas();
767         if( !window ) return;
768         int window_w = window->get_w();
769         int window_h = window->get_h();
770         int color = get_clear_color();
771         window->set_color(color);
772
773         if( !edl ) {
774                 window->draw_box(0, 0, window_w, window_h);
775                 window->flash(0);
776                 return;
777         }
778
779         float output_x1,output_y1, output_x2,output_y2;
780         float canvas_x1,canvas_y1, canvas_x2,canvas_y2;
781         get_transfers(edl,
782                 output_x1, output_y1, output_x2, output_y2,
783                 canvas_x1, canvas_y1, canvas_x2, canvas_y2);
784
785         if( canvas_y1 > 0 ) {
786                 window->draw_box(0, 0, window_w, canvas_y1);
787                 window->flash(0, 0, window_w, canvas_y1);
788         }
789
790         if( canvas_y2 < window_h ) {
791                 window->draw_box(0, canvas_y2, window_w, window_h-canvas_y2);
792                 window->flash(0, canvas_y2, window_w, window_h-canvas_y2);
793         }
794
795         if( canvas_x1 > 0 ) {
796                 window->draw_box(0, canvas_y1, canvas_x1, canvas_y2-canvas_y1);
797                 window->flash(0, canvas_y1, canvas_x1, canvas_y2-canvas_y1);
798         }
799
800         if( canvas_x2 < window_w ) {
801                 window->draw_box(canvas_x2, canvas_y1,
802                         window_w-canvas_x2, canvas_y2-canvas_y1);
803                 window->flash(canvas_x2, canvas_y1,
804                         window_w-canvas_x2, canvas_y2-canvas_y1);
805         }
806 }
807
808 int Canvas::get_clear_color()
809 {
810         BC_WindowBase *cwdw = get_canvas();
811         if( !cwdw ) return 0;
812         return cwdw->get_bg_color();
813 }
814
815 CanvasOutput::CanvasOutput(Canvas *canvas,
816     int x, int y, int w, int h)
817  : BC_SubWindow(x, y, w, h, canvas->get_clear_color())
818 {
819         this->canvas = canvas;
820 }
821
822 CanvasOutput::~CanvasOutput()
823 {
824 }
825
826 int CanvasOutput::cursor_leave_event()
827 {
828         return canvas->cursor_leave_event_base(canvas->get_canvas());
829 }
830
831 int CanvasOutput::cursor_enter_event()
832 {
833         return canvas->cursor_enter_event_base(canvas->get_canvas());
834 }
835
836 int CanvasOutput::button_press_event()
837 {
838         return canvas->button_press_event_base(canvas->get_canvas());
839 }
840
841 int CanvasOutput::button_release_event()
842 {
843         return canvas->button_release_event();
844 }
845
846 int CanvasOutput::cursor_motion_event()
847 {
848         return canvas->cursor_motion_event();
849 }
850
851 int CanvasOutput::keypress_event()
852 {
853         return canvas->keypress_event(canvas->get_canvas());
854 }
855
856
857
858 CanvasFullScreen::CanvasFullScreen(Canvas *canvas, int w, int h)
859  : BC_FullScreen(canvas->subwindow, w, h, canvas->get_clear_color(), 0, 0, 0)
860 {
861         this->canvas = canvas;
862 }
863
864 CanvasFullScreen::~CanvasFullScreen()
865 {
866 }
867
868
869 CanvasXScroll::CanvasXScroll(EDL *edl, Canvas *canvas, int x, int y,
870         int length, int position, int handle_length, int pixels)
871  : BC_ScrollBar(x, y, SCROLL_HORIZ, pixels, length, position, handle_length)
872 {
873         this->canvas = canvas;
874 }
875
876 CanvasXScroll::~CanvasXScroll()
877 {
878 }
879
880 int CanvasXScroll::handle_event()
881 {
882         canvas->update_zoom(get_value(), canvas->get_yscroll(),
883                         canvas->get_zoom());
884         return canvas->refresh(1);
885 }
886
887
888 CanvasYScroll::CanvasYScroll(EDL *edl, Canvas *canvas, int x, int y,
889         int length, int position, int handle_length, int pixels)
890  : BC_ScrollBar(x, y, SCROLL_VERT, pixels, length, position, handle_length)
891 {
892         this->canvas = canvas;
893 }
894
895 CanvasYScroll::~CanvasYScroll()
896 {
897 }
898
899 int CanvasYScroll::handle_event()
900 {
901         canvas->update_zoom(canvas->get_xscroll(), get_value(),
902                         canvas->get_zoom());
903         return canvas->refresh(1);
904 }
905
906
907 CanvasFullScreenPopup::CanvasFullScreenPopup(Canvas *canvas)
908  : BC_PopupMenu(0, 0, 0, "", 0)
909 {
910         this->canvas = canvas;
911 }
912
913
914 void CanvasFullScreenPopup::create_objects()
915 {
916         add_item(new CanvasSubWindowItem(canvas));
917 }
918
919 void CanvasFullScreenPopup::use_cwindow()
920 {
921         add_item(new CanvasPopupAuto(canvas));
922 }
923
924 CanvasSubWindowItem::CanvasSubWindowItem(Canvas *canvas)
925  : BC_MenuItem(_("Windowed"), "f", 'f')
926 {
927         this->canvas = canvas;
928 }
929
930 int CanvasSubWindowItem::handle_event()
931 {
932 // It isn't a problem to delete the canvas from in here because the event
933 // dispatcher is the canvas subwindow.
934         canvas->set_fullscreen(0);
935         return 1;
936 }
937
938
939 CanvasPopup::CanvasPopup(Canvas *canvas)
940  : BC_PopupMenu(0, 0, 0, "", 0)
941 {
942         this->canvas = canvas;
943 }
944
945 CanvasPopup::~CanvasPopup()
946 {
947 }
948
949 CanvasZoomSize::CanvasZoomSize(Canvas *canvas)
950  : BC_MenuItem(_("Zoom..."))
951 {
952         this->canvas = canvas;
953 }
954
955 CanvasSizeSubMenu::CanvasSizeSubMenu(CanvasZoomSize *zoom_size)
956 {
957         this->zoom_size = zoom_size;
958 }
959
960 void CanvasPopup::create_objects()
961 {
962         add_item(new BC_MenuItem("-"));
963         add_item(new CanvasFullScreenItem(canvas));
964
965         CanvasZoomSize *zoom_size = new CanvasZoomSize(canvas);
966         add_item(zoom_size);
967         CanvasSizeSubMenu *submenu = new CanvasSizeSubMenu(zoom_size);
968         zoom_size->add_submenu(submenu);
969
970         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 25%"), 0.25));
971         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 33%"), 0.33));
972         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 50%"), 0.5));
973         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 75%"), 0.75));
974         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 100%"), 1.0));
975         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 150%"), 1.5));
976         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 200%"), 2.0));
977         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 300%"), 3.0));
978         submenu->add_submenuitem(new CanvasPopupSize(canvas, _("Zoom 400%"), 4.0));
979 }
980
981 void CanvasPopup::use_cwindow()
982 {
983         add_item(new CanvasPopupAuto(canvas));
984         add_item(new CanvasPopupResetCamera(canvas));
985         add_item(new CanvasPopupResetProjector(canvas));
986         add_item(new CanvasPopupCameraKeyframe(canvas));
987         add_item(new CanvasPopupProjectorKeyframe(canvas));
988         add_item(toggle_controls = new CanvasToggleControls(canvas));
989 }
990
991 void CanvasPopup::use_rwindow()
992 {
993         add_item(new CanvasPopupResetTranslation(canvas));
994 }
995
996 void CanvasPopup::use_vwindow()
997 {
998         add_item(new CanvasPopupRemoveSource(canvas));
999 }
1000
1001
1002 CanvasPopupAuto::CanvasPopupAuto(Canvas *canvas)
1003  : BC_MenuItem(_("Zoom Auto"))
1004 {
1005         this->canvas = canvas;
1006 }
1007
1008 int CanvasPopupAuto::handle_event()
1009 {
1010         canvas->zoom_auto();
1011         return 1;
1012 }
1013
1014
1015 CanvasPopupSize::CanvasPopupSize(Canvas *canvas, char *text, float percentage)
1016  : BC_MenuItem(text)
1017 {
1018         this->canvas = canvas;
1019         this->percentage = percentage;
1020 }
1021 CanvasPopupSize::~CanvasPopupSize()
1022 {
1023 }
1024 int CanvasPopupSize::handle_event()
1025 {
1026         canvas->zoom_resize_window(percentage);
1027         return 1;
1028 }
1029
1030
1031 CanvasPopupResetCamera::CanvasPopupResetCamera(Canvas *canvas)
1032  : BC_MenuItem(_("Reset camera"), _("F11"), KEY_F11)
1033 {
1034         this->canvas = canvas;
1035 }
1036 int CanvasPopupResetCamera::handle_event()
1037 {
1038         canvas->reset_camera();
1039         return 1;
1040 }
1041
1042 CanvasPopupResetProjector::CanvasPopupResetProjector(Canvas *canvas)
1043  : BC_MenuItem(_("Reset projector"), _("F12"), KEY_F12)
1044 {
1045         this->canvas = canvas;
1046 }
1047 int CanvasPopupResetProjector::handle_event()
1048 {
1049         canvas->reset_projector();
1050         return 1;
1051 }
1052
1053
1054 CanvasPopupCameraKeyframe::CanvasPopupCameraKeyframe(Canvas *canvas)
1055  : BC_MenuItem(_("Camera keyframe"), _("Shift-F11"), KEY_F11)
1056 {
1057         this->canvas = canvas;
1058         set_shift(1);
1059 }
1060 int CanvasPopupCameraKeyframe::handle_event()
1061 {
1062         canvas->camera_keyframe();
1063         return 1;
1064 }
1065
1066 CanvasPopupProjectorKeyframe::CanvasPopupProjectorKeyframe(Canvas *canvas)
1067  : BC_MenuItem(_("Projector keyframe"), _("Shift-F12"), KEY_F12)
1068 {
1069         this->canvas = canvas;
1070         set_shift(1);
1071 }
1072 int CanvasPopupProjectorKeyframe::handle_event()
1073 {
1074         canvas->projector_keyframe();
1075         return 1;
1076 }
1077
1078
1079
1080 CanvasPopupResetTranslation::CanvasPopupResetTranslation(Canvas *canvas)
1081  : BC_MenuItem(_("Reset translation"))
1082 {
1083         this->canvas = canvas;
1084 }
1085 int CanvasPopupResetTranslation::handle_event()
1086 {
1087         canvas->reset_translation();
1088         return 1;
1089 }
1090
1091
1092
1093 CanvasToggleControls::CanvasToggleControls(Canvas *canvas)
1094  : BC_MenuItem(calculate_text(canvas->get_cwindow_controls()))
1095 {
1096         this->canvas = canvas;
1097 }
1098 int CanvasToggleControls::handle_event()
1099 {
1100         canvas->toggle_controls();
1101         set_text(calculate_text(canvas->get_cwindow_controls()));
1102         return 1;
1103 }
1104
1105 char* CanvasToggleControls::calculate_text(int cwindow_controls)
1106 {
1107         return !cwindow_controls ? _("Show controls") : _("Hide controls");
1108 }
1109
1110
1111 CanvasFullScreenItem::CanvasFullScreenItem(Canvas *canvas)
1112  : BC_MenuItem(_("Fullscreen"), "f", 'f')
1113 {
1114         this->canvas = canvas;
1115 }
1116 int CanvasFullScreenItem::handle_event()
1117 {
1118         canvas->set_fullscreen(1);
1119         return 1;
1120 }
1121
1122
1123 CanvasPopupRemoveSource::CanvasPopupRemoveSource(Canvas *canvas)
1124  : BC_MenuItem(_("Close source"))
1125 {
1126         this->canvas = canvas;
1127 }
1128 int CanvasPopupRemoveSource::handle_event()
1129 {
1130         canvas->close_source();
1131         return 1;
1132 }
1133
1134