Support buttons.
[dcpomatic.git] / src / tools / dcpomatic_playlist.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "../wx/wx_util.h"
22 #include "../wx/wx_signal_manager.h"
23 #include "../wx/content_view.h"
24 #include "../lib/util.h"
25 #include "../lib/config.h"
26 #include "../lib/cross.h"
27 #include "../lib/film.h"
28 #include "../lib/dcp_content.h"
29 #include "../lib/spl_entry.h"
30 #include "../lib/spl.h"
31 #include <wx/wx.h>
32 #include <wx/listctrl.h>
33 #include <wx/imaglist.h>
34
35 using std::exception;
36 using std::cout;
37 using std::string;
38 using boost::optional;
39 using boost::shared_ptr;
40 using boost::weak_ptr;
41 using boost::bind;
42 using boost::dynamic_pointer_cast;
43
44 class ContentDialog : public wxDialog, public ContentStore
45 {
46 public:
47         ContentDialog (wxWindow* parent, weak_ptr<Film> film)
48                 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
49                 , _content_view (new ContentView(this))
50         {
51                 _content_view->update ();
52
53                 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
54                 SetSizer (overall_sizer);
55
56                 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
57
58                 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
59                 if (buttons) {
60                         overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
61                 }
62
63                 overall_sizer->Layout ();
64         }
65
66         shared_ptr<Content> selected () const
67         {
68                 return _content_view->selected ();
69         }
70
71         shared_ptr<Content> get (string digest) const
72         {
73                 return _content_view->get (digest);
74         }
75
76 private:
77         ContentView* _content_view;
78 };
79
80 class DOMFrame : public wxFrame
81 {
82 public:
83         explicit DOMFrame (wxString const & title)
84                 : wxFrame (0, -1, title)
85                 /* XXX: this is a bit of a hack, but we need it to be able to use the Content class hierarchy */
86                 , _film (new Film(optional<boost::filesystem::path>()))
87                 , _content_dialog (new ContentDialog(this, _film))
88         {
89                 /* Use a panel as the only child of the Frame so that we avoid
90                    the dark-grey background on Windows.
91                 */
92                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
93                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
94
95                 _list = new wxListCtrl (
96                         overall_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
97                         );
98
99                 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
100                 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
101                 _list->AppendColumn (_("Type"), wxLIST_FORMAT_CENTRE, 100);
102                 _list->AppendColumn (_("Format"), wxLIST_FORMAT_CENTRE, 75);
103                 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
104                 _list->AppendColumn (_("Skippable"), wxLIST_FORMAT_CENTRE, 90);
105                 _list->AppendColumn (_("Disable timeline"), wxLIST_FORMAT_CENTRE, 125);
106                 _list->AppendColumn (_("Stop after play"), wxLIST_FORMAT_CENTRE, 125);
107
108                 wxImageList* images = new wxImageList (16, 16);
109                 wxIcon tick_icon;
110                 wxIcon no_tick_icon;
111 #ifdef DCPOMATIX_OSX
112                 tick_icon.LoadFile ("tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
113                 no_tick_icon.LoadFile ("no_tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
114 #else
115                 boost::filesystem::path tick_path = shared_path() / "tick.png";
116                 tick_icon.LoadFile (std_to_wx(tick_path.string()));
117                 boost::filesystem::path no_tick_path = shared_path() / "no_tick.png";
118                 no_tick_icon.LoadFile (std_to_wx(no_tick_path.string()));
119 #endif
120                 images->Add (tick_icon);
121                 images->Add (no_tick_icon);
122
123                 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
124
125                 main_sizer->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
126
127                 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
128                 _up = new Button (overall_panel, _("Up"));
129                 _down = new Button (overall_panel, _("Down"));
130                 _add = new Button (overall_panel, _("Add"));
131                 _remove = new Button (overall_panel, _("Remove"));
132                 _save = new Button (overall_panel, _("Save playlist"));
133                 _load = new Button (overall_panel, _("Load playlist"));
134                 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
135                 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
136                 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
137                 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
138                 button_sizer->Add (_save, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
139                 button_sizer->Add (_load, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
140
141                 main_sizer->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
142                 overall_panel->SetSizer (main_sizer);
143
144                 _list->Bind (wxEVT_LEFT_DOWN, bind(&DOMFrame::list_left_click, this, _1));
145                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&DOMFrame::selection_changed, this));
146                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&DOMFrame::selection_changed, this));
147                 _up->Bind (wxEVT_BUTTON, bind(&DOMFrame::up_clicked, this));
148                 _down->Bind (wxEVT_BUTTON, bind(&DOMFrame::down_clicked, this));
149                 _add->Bind (wxEVT_BUTTON, bind(&DOMFrame::add_clicked, this));
150                 _remove->Bind (wxEVT_BUTTON, bind(&DOMFrame::remove_clicked, this));
151                 _save->Bind (wxEVT_BUTTON, bind(&DOMFrame::save_clicked, this));
152                 _load->Bind (wxEVT_BUTTON, bind(&DOMFrame::load_clicked, this));
153
154                 setup_sensitivity ();
155         }
156
157 private:
158
159         void add (SPLEntry e)
160         {
161                 wxListItem item;
162                 item.SetId (_list->GetItemCount());
163                 long const N = _list->InsertItem (item);
164                 set_item (N, e);
165                 _playlist.add (e);
166         }
167
168         void selection_changed ()
169         {
170                 setup_sensitivity ();
171         }
172
173         void set_item (long N, SPLEntry e)
174         {
175                 _list->SetItem (N, 0, std_to_wx(e.name));
176                 _list->SetItem (N, 1, std_to_wx(e.id));
177                 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
178                 _list->SetItem (N, 3, e.type == SPLEntry::DCP ? _("DCP") : _("E-cinema"));
179                 _list->SetItem (N, 4, e.encrypted ? _("Y") : _("N"));
180                 _list->SetItem (N, COLUMN_SKIPPABLE, wxEmptyString, e.skippable ? 0 : 1);
181                 _list->SetItem (N, COLUMN_DISABLE_TIMELINE, wxEmptyString, e.disable_timeline ? 0 : 1);
182                 _list->SetItem (N, COLUMN_STOP_AFTER_PLAY, wxEmptyString, e.stop_after_play ? 0 : 1);
183         }
184
185         void setup_sensitivity ()
186         {
187                 int const num_selected = _list->GetSelectedItemCount ();
188                 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
189                 _up->Enable (selected > 0);
190                 _down->Enable (selected != -1 && selected < (_list->GetItemCount() - 1));
191                 _remove->Enable (num_selected > 0);
192         }
193
194         void list_left_click (wxMouseEvent& ev)
195         {
196                 int flags;
197                 long item = _list->HitTest (ev.GetPosition(), flags, 0);
198                 int x = ev.GetPosition().x;
199                 optional<int> column;
200                 for (int i = 0; i < _list->GetColumnCount(); ++i) {
201                         x -= _list->GetColumnWidth (i);
202                         if (x < 0) {
203                                 column = i;
204                                 break;
205                         }
206                 }
207
208                 if (item != -1 && column) {
209                         switch (*column) {
210                         case COLUMN_SKIPPABLE:
211                                 _playlist[item].skippable = !_playlist[item].skippable;
212                                 break;
213                         case COLUMN_DISABLE_TIMELINE:
214                                 _playlist[item].disable_timeline = !_playlist[item].disable_timeline;
215                                 break;
216                         case COLUMN_STOP_AFTER_PLAY:
217                                 _playlist[item].stop_after_play = !_playlist[item].stop_after_play;
218                                 break;
219                         default:
220                                 ev.Skip ();
221                         }
222                         set_item (item, _playlist[item]);
223                 } else {
224                         ev.Skip ();
225                 }
226         }
227
228         void add_clicked ()
229         {
230                 int const r = _content_dialog->ShowModal ();
231                 if (r == wxID_OK) {
232                         shared_ptr<Content> content = _content_dialog->selected ();
233                         if (content) {
234                                 add (SPLEntry(content));
235                         }
236                 }
237         }
238
239         void up_clicked ()
240         {
241                 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
242                 if (s < 1) {
243                         return;
244                 }
245
246                 SPLEntry tmp = _playlist[s];
247                 _playlist[s] = _playlist[s-1];
248                 _playlist[s-1] = tmp;
249
250                 set_item (s - 1, _playlist[s-1]);
251                 set_item (s, _playlist[s]);
252         }
253
254         void down_clicked ()
255         {
256                 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
257                 if (s > (_list->GetItemCount() - 1)) {
258                         return;
259                 }
260
261                 SPLEntry tmp = _playlist[s];
262                 _playlist[s] = _playlist[s+1];
263                 _playlist[s+1] = tmp;
264
265                 set_item (s + 1, _playlist[s+1]);
266                 set_item (s, _playlist[s]);
267         }
268
269         void remove_clicked ()
270         {
271                 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
272                 if (s == -1) {
273                         return;
274                 }
275
276                 _playlist.remove (s);
277                 _list->DeleteItem (s);
278         }
279
280         void save_clicked ()
281         {
282                 Config* c = Config::instance ();
283                 wxString default_dir = c->player_playlist_directory() ? std_to_wx(c->player_playlist_directory()->string()) : wxString(wxEmptyString);
284                 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), default_dir, wxEmptyString, wxT("XML files (*.xml)|*.xml"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
285                 if (d->ShowModal() == wxID_OK) {
286                         _playlist.write (wx_to_std(d->GetPath()));
287                 }
288         }
289
290         void load_clicked ()
291         {
292                 Config* c = Config::instance ();
293                 wxString default_dir = c->player_playlist_directory() ? std_to_wx(c->player_playlist_directory()->string()) : wxString(wxEmptyString);
294                 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), default_dir, wxEmptyString, wxT("XML files (*.xml)|*.xml"));
295                 if (d->ShowModal() == wxID_OK) {
296                         _list->DeleteAllItems ();
297                         if (!_playlist.read (wx_to_std(d->GetPath()), _content_dialog)) {
298                                 BOOST_FOREACH (SPLEntry i, _playlist.get()) {
299                                         add (i);
300                                 }
301                         } else {
302                                 error_dialog (this, _("Some content in this playlist was not found."));
303                         }
304                 }
305         }
306
307         wxListCtrl* _list;
308         wxButton* _up;
309         wxButton* _down;
310         wxButton* _add;
311         wxButton* _remove;
312         wxButton* _save;
313         wxButton* _load;
314         boost::shared_ptr<Film> _film;
315         SPL _playlist;
316         ContentDialog* _content_dialog;
317
318         enum {
319                 COLUMN_SKIPPABLE = 5,
320                 COLUMN_DISABLE_TIMELINE = 6,
321                 COLUMN_STOP_AFTER_PLAY = 7
322         };
323 };
324
325 /** @class App
326  *  @brief The magic App class for wxWidgets.
327  */
328 class App : public wxApp
329 {
330 public:
331         App ()
332                 : wxApp ()
333                 , _frame (0)
334         {}
335
336 private:
337
338         bool OnInit ()
339         try
340         {
341                 SetAppName (_("DCP-o-matic KDM Creator"));
342
343                 if (!wxApp::OnInit()) {
344                         return false;
345                 }
346
347 #ifdef DCPOMATIC_LINUX
348                 unsetenv ("UBUNTU_MENUPROXY");
349 #endif
350
351                 #ifdef __WXOSX__
352                 ProcessSerialNumber serial;
353                 GetCurrentProcess (&serial);
354                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
355 #endif
356
357                 dcpomatic_setup_path_encoding ();
358
359                 /* Enable i18n; this will create a Config object
360                    to look for a force-configured language.  This Config
361                    object will be wrong, however, because dcpomatic_setup
362                    hasn't yet been called and there aren't any filters etc.
363                    set up yet.
364                 */
365                 dcpomatic_setup_i18n ();
366
367                 /* Set things up, including filters etc.
368                    which will now be internationalised correctly.
369                 */
370                 dcpomatic_setup ();
371
372                 /* Force the configuration to be re-loaded correctly next
373                    time it is needed.
374                 */
375                 Config::drop ();
376
377                 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
378                 SetTopWindow (_frame);
379                 _frame->Maximize ();
380                 _frame->Show ();
381
382                 signal_manager = new wxSignalManager (this);
383                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
384
385                 return true;
386         }
387         catch (exception& e)
388         {
389                 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
390                 return true;
391         }
392
393         /* An unhandled exception has occurred inside the main event loop */
394         bool OnExceptionInMainLoop ()
395         {
396                 try {
397                         throw;
398                 } catch (FileError& e) {
399                         error_dialog (
400                                 0,
401                                 wxString::Format (
402                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
403                                         std_to_wx (e.what()),
404                                         std_to_wx (e.file().string().c_str ())
405                                         )
406                                 );
407                 } catch (exception& e) {
408                         error_dialog (
409                                 0,
410                                 wxString::Format (
411                                         _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
412                                         std_to_wx (e.what ())
413                                         )
414                                 );
415                 } catch (...) {
416                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
417                 }
418
419                 /* This will terminate the program */
420                 return false;
421         }
422
423         void OnUnhandledException ()
424         {
425                 error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
426         }
427
428         void idle ()
429         {
430                 signal_manager->ui_idle ();
431         }
432
433         DOMFrame* _frame;
434 };
435
436 IMPLEMENT_APP (App)