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