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