Always make a DC in the paint event handler, otherwise all manner of strange shit...
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/film_viewer.cc
21  *  @brief A wx widget to view `thumbnails' of a Film.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include "lib/film.h"
27 #include "lib/format.h"
28 #include "lib/util.h"
29 #include "lib/thumbs_job.h"
30 #include "lib/job_manager.h"
31 #include "lib/film_state.h"
32 #include "lib/options.h"
33 #include "film_viewer.h"
34
35 using namespace std;
36 using namespace boost;
37
38 class ThumbPanel : public wxPanel
39 {
40 public:
41         ThumbPanel (wxPanel* parent, Film* film)
42                 : wxPanel (parent)
43                 , _film (film)
44                 , _image (0)
45                 , _bitmap (0)
46                 , _left_crop (0)
47                 , _right_crop (0)
48                 , _top_crop (0)
49                 , _bottom_crop (0)
50         {
51         }
52
53         void paint_event (wxPaintEvent& ev)
54         {
55                 wxPaintDC dc (this);
56                 if (_bitmap) {
57                         dc.DrawBitmap (*_bitmap, 0, 0, false);
58                 }
59         }
60
61         void size_event (wxSizeEvent &)
62         {
63                 if (!_image) {
64                         return;
65                 }
66
67                 resize ();
68         }
69
70         void resize ()
71         {
72                 int vw, vh;
73                 GetSize (&vw, &vh);
74
75                 float const target = _film->format()->ratio_as_float ();
76
77                 _cropped_image = _image->GetSubImage (
78                         wxRect (_left_crop, _top_crop, _image->GetWidth() - (_left_crop + _right_crop), _image->GetHeight() - (_top_crop + _bottom_crop))
79                         );
80
81                 if ((float (vw) / vh) > target) {
82                         /* view is longer (horizontally) than the ratio; fit height */
83                         _cropped_image.Rescale (vh * target, vh);
84                 } else {
85                         /* view is shorter (horizontally) than the ratio; fit width */
86                         _cropped_image.Rescale (vw, vw / target);
87                 }
88
89                 delete _bitmap;
90                 _bitmap = new wxBitmap (_cropped_image);
91
92                 Refresh ();
93         }
94
95         void load (string f)
96         {
97                 delete _image;
98                 _image = new wxImage (wxString (f.c_str(), wxConvUTF8));
99                 resize ();
100         }
101
102         void set_crop (int l, int r, int t, int b)
103         {
104                 _left_crop = l;
105                 _right_crop = r;
106                 _top_crop = t;
107                 _bottom_crop = b;
108                 resize ();
109         }
110
111         void clear ()
112         {
113                 delete _bitmap;
114                 _bitmap = 0;
115                 delete _image;
116                 _image = 0;
117         }
118
119         DECLARE_EVENT_TABLE ();
120
121 private:
122         Film* _film;
123         wxImage* _image;
124         wxImage _cropped_image;
125         wxBitmap* _bitmap;
126         int _left_crop;
127         int _right_crop;
128         int _top_crop;
129         int _bottom_crop;
130 };
131
132 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
133 EVT_PAINT (ThumbPanel::paint_event)
134 EVT_SIZE (ThumbPanel::size_event)
135 END_EVENT_TABLE ()
136
137 FilmViewer::FilmViewer (Film* f, wxWindow* p)
138         : wxPanel (p)
139         , _film (f)
140 {
141         _sizer = new wxBoxSizer (wxVERTICAL);
142         SetSizer (_sizer);
143         
144         _thumb_panel = new ThumbPanel (this, f);
145         _sizer->Add (_thumb_panel, 1, wxEXPAND);
146
147         int const max = f ? f->num_thumbs() - 1 : 0;
148         _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
149         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
150         load_thumbnail (0);
151
152         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
153
154         set_film (_film);
155 }
156
157 void
158 FilmViewer::load_thumbnail (int n)
159 {
160         if (_film == 0 || _film->num_thumbs() <= n) {
161                 return;
162         }
163
164         _thumb_panel->load (_film->thumb_file(n));
165 }
166
167 void
168 FilmViewer::reload_current_thumbnail ()
169 {
170         load_thumbnail (_slider->GetValue ());
171 }
172
173 void
174 FilmViewer::slider_changed (wxCommandEvent &)
175 {
176         reload_current_thumbnail ();
177 }
178
179 void
180 FilmViewer::film_changed (Film::Property p)
181 {
182         if (p == Film::LEFT_CROP || p == Film::RIGHT_CROP || p == Film::TOP_CROP || p == Film::BOTTOM_CROP) {
183                 _thumb_panel->set_crop (_film->left_crop(), _film->right_crop(), _film->top_crop(), _film->bottom_crop ());
184         } else if (p == Film::THUMBS) {
185                 if (_film && _film->num_thumbs() > 1) {
186                         _slider->SetRange (0, _film->num_thumbs () - 1);
187                 } else {
188                         _thumb_panel->clear ();
189                         _slider->SetRange (0, 1);
190                 }
191                 
192                 _slider->SetValue (0);
193                 reload_current_thumbnail ();
194         } else if (p == Film::FORMAT) {
195                 reload_current_thumbnail ();
196         } else if (p == Film::CONTENT) {
197                 setup_visibility ();
198                 _film->examine_content ();
199                 update_thumbs ();
200         }
201 }
202
203 void
204 FilmViewer::set_film (Film* f)
205 {
206         _film = f;
207
208         if (!_film) {
209                 _thumb_panel->clear ();
210                 return;
211         }
212
213         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
214
215         film_changed (Film::THUMBS);
216 }
217
218 void
219 FilmViewer::update_thumbs ()
220 {
221         if (!_film) {
222                 return;
223         }
224
225         _film->update_thumbs_pre_gui ();
226
227         shared_ptr<const FilmState> s = _film->state_copy ();
228         shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".tiff", ""));
229         o->out_size = _film->size ();
230         o->apply_crop = false;
231         o->decode_audio = false;
232         o->decode_video_frequency = 128;
233         
234         shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
235         j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
236         JobManager::instance()->add (j);
237 }
238
239 void
240 FilmViewer::setup_visibility ()
241 {
242         if (!_film) {
243                 return;
244         }
245
246         ContentType const c = _film->content_type ();
247         _slider->Show (c == VIDEO);
248 }