4 #include "edlsession.h"
10 Canvas::Canvas(BC_WindowBase *subwindow,
23 this->subwindow = subwindow;
28 this->output_w = output_w;
29 this->output_h = output_h;
30 this->use_scrollbars = use_scrollbars;
31 this->use_cwindow = use_cwindow;
32 this->use_rwindow = use_rwindow;
33 this->use_vwindow = use_vwindow;
38 if(refresh_frame) delete refresh_frame;
40 if(yscroll) delete yscroll;
41 if(xscroll) delete xscroll;
54 // Get dimensions given a zoom
55 void Canvas::calculate_sizes(float aspect_ratio,
63 if((float)output_w / output_h <= aspect_ratio)
65 w = (int)((float)output_h * aspect_ratio * zoom);
66 h = (int)((float)output_h * zoom);
71 h = (int)((float)output_w / aspect_ratio * zoom);
72 w = (int)((float)output_w * zoom);
76 float Canvas::get_x_offset(EDL *edl,
86 // If the projection is smaller than the canvas, this forces it in the center.
87 // if(conformed_w < w_visible)
88 // return -(float)(w_visible - conformed_w) / 2;
90 return (float)get_xscroll();
93 return ((float)-canvas->get_w() / zoom_x +
94 edl->calculate_output_w(single_channel)) / 2;
99 int canvas_w = canvas->get_w();
100 int canvas_h = canvas->get_h();
104 if((float)out_w / out_h > conformed_w / conformed_h)
106 out_w = (int)(out_h * conformed_w / conformed_h + 0.5);
110 return -(canvas_w - out_w) / 2 / zoom_x;
116 float Canvas::get_y_offset(EDL *edl,
126 // If the projection is smaller than the canvas, this forces it in the center.
127 // if(conformed_h < h_visible)
128 // return -(float)(h_visible - conformed_h) / 2;
130 return (float)get_yscroll();
133 return ((float)-canvas->get_h() / zoom_y +
134 edl->calculate_output_h(single_channel)) / 2;
139 int canvas_w = canvas->get_w();
140 int canvas_h = canvas->get_h();
144 if((float)out_w / out_h <= conformed_w / conformed_h)
146 out_h = (int)((float)out_w / (conformed_w / conformed_h) + 0.5);
149 //printf("Canvas::get_y_offset 1 %d %d %f\n", out_h, canvas_h, -((float)canvas_h - out_h) / 2);
151 return -((float)canvas_h - out_h) / 2 / zoom_y;
157 // This may not be used anymore
158 void Canvas::check_boundaries(EDL *edl, int &x, int &y, float &zoom)
160 if(x + w_visible > w_needed) x = w_needed - w_visible;
161 if(y + h_visible > h_needed) y = h_needed - h_visible;
167 void Canvas::update_scrollbars()
171 if(xscroll) xscroll->update_length(w_needed, get_xscroll(), w_visible);
172 if(yscroll) yscroll->update_length(h_needed, get_yscroll(), h_visible);
176 void Canvas::get_zooms(EDL *edl,
183 edl->calculate_conformed_dimensions(single_channel,
189 zoom_x = get_zoom() *
191 edl->calculate_output_w(single_channel);
192 zoom_y = get_zoom() *
194 edl->calculate_output_h(single_channel);
199 int canvas_w = canvas->get_w();
200 int canvas_h = canvas->get_h();
205 if((float)out_w / out_h > conformed_w / conformed_h)
207 out_w = (int)((float)out_h * conformed_w / conformed_h + 0.5);
211 out_h = (int)((float)out_w / (conformed_w / conformed_h) + 0.5);
214 zoom_x = (float)out_w / edl->calculate_output_w(single_channel);
215 zoom_y = (float)out_h / edl->calculate_output_h(single_channel);
216 //printf("get zooms 2 %d %d %f %f\n", canvas_w, canvas_h, conformed_w, conformed_h);
220 // Convert a coordinate on the canvas to a coordinate on the output
221 void Canvas::canvas_to_output(EDL *edl, int single_channel, float &x, float &y)
223 float zoom_x, zoom_y, conformed_w, conformed_h;
224 get_zooms(edl, single_channel, zoom_x, zoom_y, conformed_w, conformed_h);
226 //printf("Canvas::canvas_to_output y=%f zoom_y=%f y_offset=%f\n",
227 // y, zoom_y, get_y_offset(edl, single_channel, zoom_y, conformed_w, conformed_h));
229 x = (float)x / zoom_x + get_x_offset(edl, single_channel, zoom_x, conformed_w, conformed_h);
230 y = (float)y / zoom_y + get_y_offset(edl, single_channel, zoom_y, conformed_w, conformed_h);
233 void Canvas::output_to_canvas(EDL *edl, int single_channel, float &x, float &y)
235 float zoom_x, zoom_y, conformed_w, conformed_h;
236 get_zooms(edl, single_channel, zoom_x, zoom_y, conformed_w, conformed_h);
238 //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));
240 x = (float)zoom_x * (x - get_x_offset(edl, single_channel, zoom_x, conformed_w, conformed_h));
241 y = (float)zoom_y * (y - get_y_offset(edl, single_channel, zoom_y, conformed_w, conformed_h));
246 void Canvas::get_transfers(EDL *edl,
258 // printf("Canvas::get_transfers %d %d\n", canvas_w,
260 if(canvas_w < 0) canvas_w = canvas->get_w();
261 if(canvas_h < 0) canvas_h = canvas->get_h();
265 float in_x1, in_y1, in_x2, in_y2;
266 float out_x1, out_y1, out_x2, out_y2;
267 float zoom_x, zoom_y, conformed_w, conformed_h;
269 get_zooms(edl, 0, zoom_x, zoom_y, conformed_w, conformed_h);
279 canvas_to_output(edl, 0, in_x1, in_y1);
280 canvas_to_output(edl, 0, in_x2, in_y2);
282 //printf("Canvas::get_transfers 1 %.0f %.0f %.0f %.0f -> %.0f %.0f %.0f %.0f\n",
283 //in_x1, in_y1, in_x2, in_y2, out_x1, out_y1, out_x2, out_y2);
287 out_x1 += -in_x1 * zoom_x;
293 out_y1 += -in_y1 * zoom_y;
297 int output_w = get_output_w(edl);
298 int output_h = get_output_h(edl);
302 out_x2 -= (in_x2 - output_w) * zoom_x;
308 out_y2 -= (in_y2 - output_h) * zoom_y;
311 // printf("Canvas::get_transfers 2 %.0f %.0f %.0f %.0f -> %.0f %.0f %.0f %.0f\n",
312 // in_x1, in_y1, in_x2, in_y2, out_x1, out_y1, out_x2, out_y2);
316 in_w = (int)(in_x2 - in_x1);
317 in_h = (int)(in_y2 - in_y1);
320 out_w = (int)(out_x2 - out_x1);
321 out_h = (int)(out_y2 - out_y1);
324 // if(!scrollbars_exist())
326 // out_x = canvas_w / 2 - out_w / 2;
327 // out_y = canvas_h / 2 - out_h / 2;
330 // printf("Canvas::get_transfers 2 %d %d %d %d -> %d %d %d %d\n",in_x,
348 if((float)out_w / out_h > edl->get_aspect_ratio())
350 out_w = (int)(out_h * edl->get_aspect_ratio() + 0.5);
351 out_x = canvas_w / 2 - out_w / 2;
355 out_h = (int)(out_w / edl->get_aspect_ratio() + 0.5);
356 out_y = canvas_h / 2 - out_h / 2;
360 in_w = get_output_w(edl);
361 in_h = get_output_h(edl);
367 in_w = this->output_w;
368 in_h = this->output_h;
376 out_x = MAX(0, out_x);
377 out_y = MAX(0, out_y);
378 out_w = MAX(0, out_w);
379 out_h = MAX(0, out_h);
382 int Canvas::scrollbars_exist()
384 return(use_scrollbars && (xscroll || yscroll));
387 int Canvas::get_output_w(EDL *edl)
390 return edl->calculate_output_w(0);
392 return edl->session->output_w;
395 int Canvas::get_output_h(EDL *edl)
400 return edl->calculate_output_h(0);
402 return edl->session->output_h;
408 void Canvas::get_scrollbars(EDL *edl,
414 int need_xscroll = 0;
415 int need_yscroll = 0;
417 float zoom_x, zoom_y, conformed_w, conformed_h;
421 w_needed = edl->calculate_output_w(0);
422 h_needed = edl->calculate_output_h(0);
423 w_visible = w_needed;
424 h_visible = h_needed;
426 //printf("Canvas::get_scrollbars 1 %d %d\n", get_xscroll(), get_yscroll());
430 w_needed = edl->calculate_output_w(0);
431 h_needed = edl->calculate_output_h(0);
432 get_zooms(edl, 0, zoom_x, zoom_y, conformed_w, conformed_h);
433 //printf("Canvas::get_scrollbars 2 %d %d\n", get_xscroll(), get_yscroll());
437 w_visible = (int)(canvas_w / zoom_x);
438 h_visible = (int)(canvas_h / zoom_y);
441 // if(w_needed > w_visible)
447 canvas_h -= BC_ScrollBar::get_span(SCROLL_HORIZ);
454 // if(h_needed > h_visible)
460 canvas_w -= BC_ScrollBar::get_span(SCROLL_VERT);
467 //printf("Canvas::get_scrollbars %d %d %d %d %d %d\n", canvas_w, canvas_h, w_needed, h_needed, w_visible, h_visible);
468 //printf("Canvas::get_scrollbars 3 %d %d\n", get_xscroll(), get_yscroll());
470 w_visible = (int)(canvas_w / zoom_x);
471 h_visible = (int)(canvas_h / zoom_y);
477 subwindow->add_subwindow(xscroll = new CanvasXScroll(edl,
486 xscroll->reposition_window(canvas_x, canvas_y + canvas_h, canvas_w);
488 if(xscroll->get_length() != w_needed ||
489 xscroll->get_handlelength() != w_visible)
490 xscroll->update_length(w_needed, get_xscroll(), w_visible);
494 if(xscroll) delete xscroll;
497 //printf("Canvas::get_scrollbars 4 %d %d\n", get_xscroll(), get_yscroll());
502 subwindow->add_subwindow(yscroll = new CanvasYScroll(edl,
511 yscroll->reposition_window(canvas_x + canvas_w, canvas_y, canvas_h);
513 if(yscroll->get_length() != edl->calculate_output_h(0) ||
514 yscroll->get_handlelength() != h_visible)
515 yscroll->update_length(h_needed, get_yscroll(), h_visible);
519 if(yscroll) delete yscroll;
522 //printf("Canvas::get_scrollbars 5 %d %d\n", get_xscroll(), get_yscroll());
525 void Canvas::reposition_window(EDL *edl, int x, int y, int w, int h)
531 int view_x = x, view_y = y, view_w = w, view_h = h;
532 //printf("Canvas::reposition_window 1\n");
533 get_scrollbars(edl, view_x, view_y, view_w, view_h);
534 //printf("Canvas::reposition_window %d %d %d %d\n", view_x, view_y, view_w, view_h);
535 canvas->reposition_window(view_x, view_y, view_w, view_h);
537 //printf("Canvas::reposition_window 2\n");
540 void Canvas::set_cursor(int cursor)
542 canvas->set_cursor(cursor);
545 int Canvas::get_cursor_x()
547 return canvas->get_cursor_x();
550 int Canvas::get_cursor_y()
552 return canvas->get_cursor_y();
555 int Canvas::get_buttonpress()
557 return canvas->get_buttonpress();
561 int Canvas::create_objects(EDL *edl)
563 int view_x = x, view_y = y, view_w = w, view_h = h;
564 get_scrollbars(edl, view_x, view_y, view_w, view_h);
566 subwindow->add_subwindow(canvas = new CanvasOutput(edl,
573 subwindow->add_subwindow(canvas_menu = new CanvasPopup(this));
574 canvas_menu->create_objects();
579 int Canvas::button_press_event()
583 if(canvas->get_buttonpress() == 3)
585 canvas_menu->activate_menu();
595 CanvasOutput::CanvasOutput(EDL *edl,
601 : BC_SubWindow(x, y, w, h, BC_WindowBase::get_resources()->bg_color)
603 this->canvas = canvas;
607 CanvasOutput::~CanvasOutput()
611 int CanvasOutput::handle_event()
616 int CanvasOutput::cursor_leave_event()
619 if(cursor_inside) result = canvas->cursor_leave_event();
624 int CanvasOutput::cursor_enter_event()
627 if(is_event_win() && BC_WindowBase::cursor_inside())
630 result = canvas->cursor_enter_event();
635 int CanvasOutput::button_press_event()
637 if(is_event_win() && BC_WindowBase::cursor_inside())
639 return canvas->button_press_event();
644 int CanvasOutput::button_release_event()
646 return canvas->button_release_event();
649 int CanvasOutput::cursor_motion_event()
651 return canvas->cursor_motion_event();
656 CanvasXScroll::CanvasXScroll(EDL *edl,
672 this->canvas = canvas;
675 CanvasXScroll::~CanvasXScroll()
679 int CanvasXScroll::handle_event()
681 //printf("CanvasXScroll::handle_event %d %d %d\n", get_length(), get_value(), get_handlelength());
682 canvas->update_zoom(get_value(), canvas->get_yscroll(), canvas->get_zoom());
683 canvas->draw_refresh();
692 CanvasYScroll::CanvasYScroll(EDL *edl,
708 this->canvas = canvas;
711 CanvasYScroll::~CanvasYScroll()
715 int CanvasYScroll::handle_event()
717 //printf("CanvasYScroll::handle_event %d %d\n", get_value(), get_length());
718 canvas->update_zoom(canvas->get_xscroll(), get_value(), canvas->get_zoom());
719 canvas->draw_refresh();
726 CanvasPopup::CanvasPopup(Canvas *canvas)
733 this->canvas = canvas;
736 CanvasPopup::~CanvasPopup()
740 void CanvasPopup::create_objects()
742 add_item(new CanvasPopupSize(canvas, _("Zoom 25%"), 0.25));
743 add_item(new CanvasPopupSize(canvas, _("Zoom 50%"), 0.5));
744 add_item(new CanvasPopupSize(canvas, _("Zoom 100%"), 1.0));
745 add_item(new CanvasPopupSize(canvas, _("Zoom 200%"), 2.0));
746 if(canvas->use_cwindow)
748 add_item(new CanvasPopupResetCamera(canvas));
749 add_item(new CanvasPopupResetProjector(canvas));
750 add_item(toggle_controls = new CanvasToggleControls(canvas));
752 if(canvas->use_rwindow)
754 add_item(new CanvasPopupResetTranslation(canvas));
756 if(canvas->use_vwindow)
758 add_item(new CanvasPopupRemoveSource(canvas));
764 CanvasPopupSize::CanvasPopupSize(Canvas *canvas, char *text, float percentage)
767 this->canvas = canvas;
768 this->percentage = percentage;
770 CanvasPopupSize::~CanvasPopupSize()
773 int CanvasPopupSize::handle_event()
775 canvas->zoom_resize_window(percentage);
781 CanvasPopupResetCamera::CanvasPopupResetCamera(Canvas *canvas)
782 : BC_MenuItem(_("Reset camera"))
784 this->canvas = canvas;
786 int CanvasPopupResetCamera::handle_event()
788 canvas->reset_camera();
794 CanvasPopupResetProjector::CanvasPopupResetProjector(Canvas *canvas)
795 : BC_MenuItem(_("Reset projector"))
797 this->canvas = canvas;
799 int CanvasPopupResetProjector::handle_event()
801 canvas->reset_projector();
807 CanvasPopupResetTranslation::CanvasPopupResetTranslation(Canvas *canvas)
808 : BC_MenuItem(_("Reset translation"))
810 this->canvas = canvas;
812 int CanvasPopupResetTranslation::handle_event()
814 canvas->reset_translation();
820 CanvasToggleControls::CanvasToggleControls(Canvas *canvas)
821 : BC_MenuItem(calculate_text(canvas->get_cwindow_controls()))
823 this->canvas = canvas;
825 int CanvasToggleControls::handle_event()
827 canvas->toggle_controls();
828 set_text(calculate_text(canvas->get_cwindow_controls()));
832 char* CanvasToggleControls::calculate_text(int cwindow_controls)
834 if(!cwindow_controls)
835 return _("Show controls");
837 return _("Hide controls");
843 CanvasPopupRemoveSource::CanvasPopupRemoveSource(Canvas *canvas)
844 : BC_MenuItem(_("Close source"))
846 this->canvas = canvas;
848 int CanvasPopupRemoveSource::handle_event()
850 canvas->close_source();