Use a panel in the window to make Windows look better. Some tweaks to visibility.
[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                 setup ();
68         }
69
70         void setup ()
71         {
72                 if (!_film || !_image) {
73                         return;
74                 }
75                 
76                 int vw, vh;
77                 GetSize (&vw, &vh);
78
79                 float const target = _film->format() ? _film->format()->ratio_as_float () : 1.78;
80
81                 _cropped_image = _image->GetSubImage (
82                         wxRect (_left_crop, _top_crop, _image->GetWidth() - (_left_crop + _right_crop), _image->GetHeight() - (_top_crop + _bottom_crop))
83                         );
84
85                 if ((float (vw) / vh) > target) {
86                         /* view is longer (horizontally) than the ratio; fit height */
87                         _cropped_image.Rescale (vh * target, vh, wxIMAGE_QUALITY_HIGH);
88                 } else {
89                         /* view is shorter (horizontally) than the ratio; fit width */
90                         _cropped_image.Rescale (vw, vw / target, wxIMAGE_QUALITY_HIGH);
91                 }
92
93                 delete _bitmap;
94                 _bitmap = new wxBitmap (_cropped_image);
95
96                 Refresh ();
97         }
98
99         void load (string f)
100         {
101                 delete _image;
102                 _image = new wxImage (wxString (f.c_str(), wxConvUTF8));
103                 setup ();
104         }
105
106         void set_crop (int l, int r, int t, int b)
107         {
108                 _left_crop = l;
109                 _right_crop = r;
110                 _top_crop = t;
111                 _bottom_crop = b;
112                 setup ();
113         }
114
115         void set_film (Film* f)
116         {
117                 _film = f;
118                 if (!_film) {
119                         clear ();
120                 } else {
121                         setup ();
122                 }
123         }
124
125         void clear ()
126         {
127                 delete _bitmap;
128                 _bitmap = 0;
129                 delete _image;
130                 _image = 0;
131         }
132
133         DECLARE_EVENT_TABLE ();
134
135 private:
136         Film* _film;
137         wxImage* _image;
138         wxImage _cropped_image;
139         wxBitmap* _bitmap;
140         int _left_crop;
141         int _right_crop;
142         int _top_crop;
143         int _bottom_crop;
144 };
145
146 BEGIN_EVENT_TABLE (ThumbPanel, wxPanel)
147 EVT_PAINT (ThumbPanel::paint_event)
148 EVT_SIZE (ThumbPanel::size_event)
149 END_EVENT_TABLE ()
150
151 FilmViewer::FilmViewer (Film* f, wxWindow* p)
152         : wxPanel (p)
153         , _film (f)
154 {
155         _sizer = new wxBoxSizer (wxVERTICAL);
156         SetSizer (_sizer);
157         
158         _thumb_panel = new ThumbPanel (this, f);
159         _sizer->Add (_thumb_panel, 1, wxEXPAND);
160
161         int const max = f ? f->num_thumbs() - 1 : 0;
162         _slider = new wxSlider (this, wxID_ANY, 0, 0, max);
163         _sizer->Add (_slider, 0, wxEXPAND | wxLEFT | wxRIGHT);
164         load_thumbnail (0);
165
166         _slider->Connect (wxID_ANY, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler (FilmViewer::slider_changed), 0, this);
167
168         set_film (_film);
169 }
170
171 void
172 FilmViewer::load_thumbnail (int n)
173 {
174         if (_film == 0 || _film->num_thumbs() <= n) {
175                 return;
176         }
177
178         _thumb_panel->load (_film->thumb_file(n));
179 }
180
181 void
182 FilmViewer::reload_current_thumbnail ()
183 {
184         load_thumbnail (_slider->GetValue ());
185 }
186
187 void
188 FilmViewer::slider_changed (wxCommandEvent &)
189 {
190         reload_current_thumbnail ();
191 }
192
193 void
194 FilmViewer::film_changed (Film::Property p)
195 {
196         if (p == Film::LEFT_CROP || p == Film::RIGHT_CROP || p == Film::TOP_CROP || p == Film::BOTTOM_CROP) {
197                 _thumb_panel->set_crop (_film->left_crop(), _film->right_crop(), _film->top_crop(), _film->bottom_crop ());
198         } else if (p == Film::THUMBS) {
199                 if (_film && _film->num_thumbs() > 1) {
200                         _slider->SetRange (0, _film->num_thumbs () - 1);
201                 } else {
202                         _thumb_panel->clear ();
203                         _slider->SetRange (0, 1);
204                 }
205                 
206                 _slider->SetValue (0);
207                 reload_current_thumbnail ();
208         } else if (p == Film::FORMAT) {
209                 reload_current_thumbnail ();
210         } else if (p == Film::CONTENT) {
211                 setup_visibility ();
212                 _film->examine_content ();
213                 update_thumbs ();
214         }
215 }
216
217 void
218 FilmViewer::set_film (Film* f)
219 {
220         _film = f;
221         _thumb_panel->set_film (_film);
222
223         if (!_film) {
224                 return;
225         }
226
227         _film->Changed.connect (sigc::mem_fun (*this, &FilmViewer::film_changed));
228         film_changed (Film::THUMBS);
229         reload_current_thumbnail ();
230         setup_visibility ();
231 }
232
233 void
234 FilmViewer::update_thumbs ()
235 {
236         if (!_film) {
237                 return;
238         }
239
240         _film->update_thumbs_pre_gui ();
241
242         shared_ptr<const FilmState> s = _film->state_copy ();
243         shared_ptr<Options> o (new Options (s->dir ("thumbs"), ".tiff", ""));
244         o->out_size = _film->size ();
245         o->apply_crop = false;
246         o->decode_audio = false;
247         o->decode_video_frequency = 128;
248         
249         shared_ptr<Job> j (new ThumbsJob (s, o, _film->log ()));
250         j->Finished.connect (sigc::mem_fun (_film, &Film::update_thumbs_post_gui));
251         JobManager::instance()->add (j);
252 }
253
254 void
255 FilmViewer::setup_visibility ()
256 {
257         if (!_film) {
258                 return;
259         }
260
261         ContentType const c = _film->content_type ();
262         _slider->Show (c == VIDEO);
263 }