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