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