-class ThumbPanel : public wxPanel
-{
-public:
- ThumbPanel (wxPanel* parent, shared_ptr<Film> film)
- : wxPanel (parent)
- , _film (film)
- , _index (0)
- , _frame_rebuild_needed (false)
- , _composition_needed (false)
- {}
-
- /** Handle a paint event */
- void paint_event (wxPaintEvent& ev)
- {
- if (!_film || _film->thumbs().size() == 0) {
- wxPaintDC dc (this);
- return;
- }
-
- if (_frame_rebuild_needed) {
- _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index))));
-
- _subtitle.reset ();
- pair<Position, string> s = _film->thumb_subtitle (_index);
- if (!s.second.empty ()) {
- _subtitle.reset (new SubtitleView (s.first, std_to_wx (s.second)));
- }
-
- _frame_rebuild_needed = false;
- compose ();
- }
-
- if (_composition_needed) {
- compose ();
- }
-
- wxPaintDC dc (this);
- if (_bitmap) {
- dc.DrawBitmap (*_bitmap, 0, 0, false);
- }
-
- if (_film->with_subtitles() && _subtitle) {
- dc.DrawBitmap (*_subtitle->bitmap, _subtitle->transformed_area.x, _subtitle->transformed_area.y, true);
- }
- }
-
- /** Handle a size event */
- void size_event (wxSizeEvent &)
- {
- if (!_image) {
- return;
- }
-
- recompose ();
- }
-
- /** @param n Thumbnail index */
- void set (int n)
- {
- _index = n;
- _frame_rebuild_needed = true;
- Refresh ();
- }
-
- void set_film (shared_ptr<Film> f)
- {
- _film = f;
- if (!_film) {
- clear ();
- _frame_rebuild_needed = true;
- Refresh ();
- } else {
- _frame_rebuild_needed = true;
- Refresh ();
- }
- }
-
- /** Clear our thumbnail image */
- void clear ()
- {
- _bitmap.reset ();
- _image.reset ();
- _subtitle.reset ();
- }
-
- void recompose ()
- {
- _composition_needed = true;
- Refresh ();
- }
-
- DECLARE_EVENT_TABLE ();
-
-private:
-
- void compose ()
- {
- _composition_needed = false;
-
- if (!_film || !_image) {
- return;
- }
-
- /* Size of the view */
- int vw, vh;
- GetSize (&vw, &vh);
-
- Crop const fc = _film->crop ();
-
- /* Cropped rectangle */
- Rect cropped_area (
- fc.left,
- fc.top,
- _image->GetWidth() - (fc.left + fc.right),
- _image->GetHeight() - (fc.top + fc.bottom)
- );
-
- /* Target ratio */
- float const target = _film->format() ? _film->format()->ratio_as_float (_film) : 1.78;
-
- _transformed_image = _image->GetSubImage (wxRect (cropped_area.x, cropped_area.y, cropped_area.width, cropped_area.height));
-
- float x_scale = 1;
- float y_scale = 1;
-
- if ((float (vw) / vh) > target) {
- /* view is longer (horizontally) than the ratio; fit height */
- _transformed_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
- x_scale = vh * target / cropped_area.width;
- y_scale = float (vh) / cropped_area.height;
- } else {
- /* view is shorter (horizontally) than the ratio; fit width */
- _transformed_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
- x_scale = float (vw) / cropped_area.width;
- y_scale = (vw / target) / cropped_area.height;
- }
-
- _bitmap.reset (new wxBitmap (_transformed_image));
-
- if (_subtitle) {
-
- _subtitle->transformed_area = subtitle_transformed_area (
- x_scale, y_scale, _subtitle->base_area, _film->subtitle_offset(), _film->subtitle_scale()
- );
-
- _subtitle->transformed_image = _subtitle->base_image;
- _subtitle->transformed_image.Rescale (_subtitle->transformed_area.width, _subtitle->transformed_area.height, wxIMAGE_QUALITY_HIGH);
- _subtitle->transformed_area.x -= rint (_film->crop().left * x_scale);
- _subtitle->transformed_area.y -= rint (_film->crop().top * y_scale);
- _subtitle->bitmap.reset (new wxBitmap (_subtitle->transformed_image));
- }
- }
-
- shared_ptr<Film> _film;
- shared_ptr<wxImage> _image;
- wxImage _transformed_image;
- /** currently-displayed thumbnail index */
- int _index;
- shared_ptr<wxBitmap> _bitmap;
- bool _frame_rebuild_needed;
- bool _composition_needed;
-
- struct SubtitleView
- {
- SubtitleView (Position p, wxString const & i)
- : base_image (i)
- {
- base_area.x = p.x;
- base_area.y = p.y;
- base_area.width = base_image.GetWidth ();
- base_area.height = base_image.GetHeight ();
- }
-
- Rect base_area;
- Rect transformed_area;
- wxImage base_image;
- wxImage transformed_image;
- shared_ptr<wxBitmap> bitmap;
- };
-
- shared_ptr<SubtitleView> _subtitle;
-};
-
-BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
-EVT_PAINT (ThumbPanel::paint_event)
-EVT_SIZE (ThumbPanel::size_event)
-END_EVENT_TABLE ()
-