Separate code for the content panel out into a separate class.
[dcpomatic.git] / src / wx / content_panel.cc
1 /*
2     Copyright (C) 2012-2014 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 #include <wx/wx.h>
21 #include <wx/notebook.h>
22 #include <wx/listctrl.h>
23 #include "lib/audio_content.h"
24 #include "lib/subtitle_content.h"
25 #include "lib/video_content.h"
26 #include "lib/ffmpeg_content.h"
27 #include "lib/content_factory.h"
28 #include "lib/image_content.h"
29 #include "lib/dcp_content.h"
30 #include "lib/playlist.h"
31 #include "content_panel.h"
32 #include "wx_util.h"
33 #include "video_panel.h"
34 #include "audio_panel.h"
35 #include "subtitle_panel.h"
36 #include "timing_panel.h"
37 #include "timeline_dialog.h"
38
39 using std::list;
40 using std::string;
41 using boost::shared_ptr;
42 using boost::weak_ptr;
43 using boost::dynamic_pointer_cast;
44
45 ContentPanel::ContentPanel (wxNotebook* n, boost::shared_ptr<Film> f)
46         : _timeline_dialog (0)
47         , _film (f)
48         , _generally_sensitive (true)
49 {
50         _panel = new wxPanel (n);
51         _sizer = new wxBoxSizer (wxVERTICAL);
52         _panel->SetSizer (_sizer);
53
54         _menu = new ContentMenu (_panel);
55
56         {
57                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
58                 
59                 _content = new wxListCtrl (_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
60                 s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
61
62                 _content->InsertColumn (0, wxT(""));
63                 _content->SetColumnWidth (0, 512);
64
65                 wxBoxSizer* b = new wxBoxSizer (wxVERTICAL);
66                 _add_file = new wxButton (_panel, wxID_ANY, _("Add file(s)..."));
67                 b->Add (_add_file, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
68                 _add_folder = new wxButton (_panel, wxID_ANY, _("Add folder..."));
69                 b->Add (_add_folder, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
70                 _remove = new wxButton (_panel, wxID_ANY, _("Remove"));
71                 b->Add (_remove, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
72                 _earlier = new wxButton (_panel, wxID_ANY, _("Up"));
73                 b->Add (_earlier, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
74                 _later = new wxButton (_panel, wxID_ANY, _("Down"));
75                 b->Add (_later, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
76                 _timeline = new wxButton (_panel, wxID_ANY, _("Timeline..."));
77                 b->Add (_timeline, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
78
79                 s->Add (b, 0, wxALL, 4);
80
81                 _sizer->Add (s, 0, wxEXPAND | wxALL, 6);
82         }
83
84         _sequence_video = new wxCheckBox (_panel, wxID_ANY, _("Keep video in sequence"));
85         _sizer->Add (_sequence_video);
86
87         _notebook = new wxNotebook (_panel, wxID_ANY);
88         _sizer->Add (_notebook, 1, wxEXPAND | wxTOP, 6);
89
90         _video_panel = new VideoPanel (this);
91         _panels.push_back (_video_panel);
92         _audio_panel = new AudioPanel (this);
93         _panels.push_back (_audio_panel);
94         _subtitle_panel = new SubtitlePanel (this);
95         _panels.push_back (_subtitle_panel);
96         _timing_panel = new TimingPanel (this);
97         _panels.push_back (_timing_panel);
98
99         _content->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&ContentPanel::selection_changed, this));
100         _content->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&ContentPanel::selection_changed, this));
101         _content->Bind (wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, boost::bind (&ContentPanel::right_click, this, _1));
102         _add_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::add_file_clicked, this));
103         _add_folder->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::add_folder_clicked, this));
104         _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::remove_clicked, this));
105         _earlier->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::earlier_clicked, this));
106         _later->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::later_clicked, this));
107         _timeline->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::timeline_clicked, this));
108         _sequence_video->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ContentPanel::sequence_video_changed, this));
109 }
110
111 ContentList
112 ContentPanel::selected ()
113 {
114         ContentList sel;
115         long int s = -1;
116         while (true) {
117                 s = _content->GetNextItem (s, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
118                 if (s == -1) {
119                         break;
120                 }
121
122                 if (s < int (_film->content().size ())) {
123                         sel.push_back (_film->content()[s]);
124                 }
125         }
126
127         return sel;
128 }
129
130 VideoContentList
131 ContentPanel::selected_video ()
132 {
133         ContentList c = selected ();
134         VideoContentList vc;
135         
136         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
137                 shared_ptr<VideoContent> t = dynamic_pointer_cast<VideoContent> (*i);
138                 if (t) {
139                         vc.push_back (t);
140                 }
141         }
142
143         return vc;
144 }
145
146 AudioContentList
147 ContentPanel::selected_audio ()
148 {
149         ContentList c = selected ();
150         AudioContentList ac;
151         
152         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
153                 shared_ptr<AudioContent> t = dynamic_pointer_cast<AudioContent> (*i);
154                 if (t) {
155                         ac.push_back (t);
156                 }
157         }
158
159         return ac;
160 }
161
162 SubtitleContentList
163 ContentPanel::selected_subtitle ()
164 {
165         ContentList c = selected ();
166         SubtitleContentList sc;
167         
168         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
169                 shared_ptr<SubtitleContent> t = dynamic_pointer_cast<SubtitleContent> (*i);
170                 if (t) {
171                         sc.push_back (t);
172                 }
173         }
174
175         return sc;
176 }
177
178 FFmpegContentList
179 ContentPanel::selected_ffmpeg ()
180 {
181         ContentList c = selected ();
182         FFmpegContentList sc;
183         
184         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
185                 shared_ptr<FFmpegContent> t = dynamic_pointer_cast<FFmpegContent> (*i);
186                 if (t) {
187                         sc.push_back (t);
188                 }
189         }
190
191         return sc;
192 }
193
194 void
195 ContentPanel::sequence_video_changed ()
196 {
197         if (!_film) {
198                 return;
199         }
200         
201         _film->set_sequence_video (_sequence_video->GetValue ());
202 }
203
204 void
205 ContentPanel::film_changed (Film::Property p)
206 {
207         switch (p) {
208         case Film::CONTENT:
209                 setup ();
210                 break;
211         case Film::SEQUENCE_VIDEO:
212                 checked_set (_sequence_video, _film->sequence_video ());
213                 break;
214         default:
215                 break;
216         }
217
218         for (list<ContentSubPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
219                 (*i)->film_changed (p);
220         }
221 }       
222
223 void
224 ContentPanel::selection_changed ()
225 {
226         setup_sensitivity ();
227
228         for (list<ContentSubPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
229                 (*i)->content_selection_changed ();
230         }
231 }
232
233 void
234 ContentPanel::add_file_clicked ()
235 {
236         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
237            non-Latin filenames or paths.
238         */
239         wxFileDialog* d = new wxFileDialog (_panel, _("Choose a file or files"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE | wxFD_CHANGE_DIR);
240         int const r = d->ShowModal ();
241
242         if (r != wxID_OK) {
243                 d->Destroy ();
244                 return;
245         }
246
247         wxArrayString paths;
248         d->GetPaths (paths);
249
250         /* XXX: check for lots of files here and do something */
251
252         for (unsigned int i = 0; i < paths.GetCount(); ++i) {
253                 _film->examine_and_add_content (content_factory (_film, wx_to_std (paths[i])));
254         }
255
256         d->Destroy ();
257 }
258
259 void
260 ContentPanel::add_folder_clicked ()
261 {
262         wxDirDialog* d = new wxDirDialog (_panel, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
263         int const r = d->ShowModal ();
264         d->Destroy ();
265         
266         if (r != wxID_OK) {
267                 return;
268         }
269
270         shared_ptr<Content> content;
271         
272         try {
273                 content.reset (new ImageContent (_film, boost::filesystem::path (wx_to_std (d->GetPath ()))));
274         } catch (...) {
275                 try {
276                         content.reset (new DCPContent (_film, boost::filesystem::path (wx_to_std (d->GetPath ()))));
277                 } catch (...) {
278                         error_dialog (_panel, _("Could not find any images nor a DCP in that folder"));
279                         return;
280                 }
281         }
282
283         if (content) {
284                 _film->examine_and_add_content (content);
285         }
286 }
287
288 void
289 ContentPanel::remove_clicked ()
290 {
291         ContentList c = selected ();
292         if (c.size() == 1) {
293                 _film->remove_content (c.front ());
294         }
295
296         selection_changed ();
297 }
298
299 void
300 ContentPanel::timeline_clicked ()
301 {
302         if (_timeline_dialog) {
303                 _timeline_dialog->Destroy ();
304                 _timeline_dialog = 0;
305         }
306         
307         _timeline_dialog = new TimelineDialog (this, _film);
308         _timeline_dialog->Show ();
309 }
310
311 void
312 ContentPanel::right_click (wxListEvent& ev)
313 {
314         _menu->popup (_film, selected (), ev.GetPoint ());
315 }
316
317 /** Set up broad sensitivity based on the type of content that is selected */
318 void
319 ContentPanel::setup_sensitivity ()
320 {
321         _add_file->Enable (_generally_sensitive);
322         _add_folder->Enable (_generally_sensitive);
323
324         ContentList selection = selected ();
325         VideoContentList video_selection = selected_video ();
326         AudioContentList audio_selection = selected_audio ();
327
328         _remove->Enable   (selection.size() == 1 && _generally_sensitive);
329         _earlier->Enable  (selection.size() == 1 && _generally_sensitive);
330         _later->Enable    (selection.size() == 1 && _generally_sensitive);
331         _timeline->Enable (!_film->content().empty() && _generally_sensitive);
332
333         _video_panel->Enable    (video_selection.size() > 0 && _generally_sensitive);
334         _audio_panel->Enable    (audio_selection.size() > 0 && _generally_sensitive);
335         _subtitle_panel->Enable (selection.size() == 1 && dynamic_pointer_cast<SubtitleContent> (selection.front()) && _generally_sensitive);
336         _timing_panel->Enable   (selection.size() == 1 && _generally_sensitive);
337 }
338
339 void
340 ContentPanel::set_film (shared_ptr<Film> f)
341 {
342         _film = f;
343         selection_changed ();
344 }
345
346 void
347 ContentPanel::set_general_sensitivity (bool s)
348 {
349         _generally_sensitive = s;
350
351         _content->Enable (s);
352         _add_file->Enable (s);
353         _add_folder->Enable (s);
354         _remove->Enable (s);
355         _earlier->Enable (s);
356         _later->Enable (s);
357         _timeline->Enable (s);
358         _sequence_video->Enable (s);
359
360         /* Set the panels in the content notebook */
361         for (list<ContentSubPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
362                 (*i)->Enable (s);
363         }
364 }
365
366 void
367 ContentPanel::earlier_clicked ()
368 {
369         ContentList sel = selected ();
370         if (sel.size() == 1) {
371                 _film->move_content_earlier (sel.front ());
372                 selection_changed ();
373         }
374 }
375
376 void
377 ContentPanel::later_clicked ()
378 {
379         ContentList sel = selected ();
380         if (sel.size() == 1) {
381                 _film->move_content_later (sel.front ());
382                 selection_changed ();
383         }
384 }
385
386 void
387 ContentPanel::set_selection (weak_ptr<Content> wc)
388 {
389         ContentList content = _film->content ();
390         for (size_t i = 0; i < content.size(); ++i) {
391                 if (content[i] == wc.lock ()) {
392                         _content->SetItemState (i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
393                 } else {
394                         _content->SetItemState (i, 0, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
395                 }
396         }
397 }
398
399 void
400 ContentPanel::film_content_changed (int property)
401 {
402         if (property == ContentProperty::PATH || property == ContentProperty::POSITION) {
403                 setup ();
404         }
405                 
406         for (list<ContentSubPanel*>::iterator i = _panels.begin(); i != _panels.end(); ++i) {
407                 (*i)->film_content_changed (property);
408         }
409 }
410
411 void
412 ContentPanel::setup ()
413 {
414         string selected_summary;
415         int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
416         if (s != -1) {
417                 selected_summary = wx_to_std (_content->GetItemText (s));
418         }
419         
420         _content->DeleteAllItems ();
421
422         ContentList content = _film->content ();
423         sort (content.begin(), content.end(), ContentSorter ());
424         
425         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
426                 int const t = _content->GetItemCount ();
427                 bool const valid = (*i)->paths_valid ();
428
429                 string s = (*i)->summary ();
430                 if (!valid) {
431                         s = _("MISSING: ") + s;
432                 }
433
434                 _content->InsertItem (t, std_to_wx (s));
435
436                 if ((*i)->summary() == selected_summary) {
437                         _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
438                 }
439
440                 if (!valid) {
441                         _content->SetItemTextColour (t, *wxRED);
442                 }
443         }
444
445         if (selected_summary.empty () && !content.empty ()) {
446                 /* Select the item of content if none was selected before */
447                 _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
448         }
449 }
450