Add a simple DCP editor.
[dcpomatic.git] / src / tools / dcpomatic_editor.cc
1 /*
2     Copyright (C) 2022 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
22 #include "wx/about_dialog.h"
23 #include "wx/editable_list.h"
24 #include "wx/wx_signal_manager.h"
25 #include "wx/wx_util.h"
26 #include "lib/cross.h"
27 #include "lib/dcpomatic_log.h"
28 #include "lib/null_log.h"
29 #include "lib/util.h"
30 #include <dcp/cpl.h>
31 #include <dcp/dcp.h>
32 #include <dcp/reel.h>
33 #include <dcp/reel_picture_asset.h>
34 #include <dcp/reel_sound_asset.h>
35 #include <dcp/reel_subtitle_asset.h>
36 #include <dcp/warnings.h>
37 LIBDCP_DISABLE_WARNINGS
38 #include <wx/cmdline.h>
39 #include <wx/notebook.h>
40 #include <wx/spinctrl.h>
41 #include <wx/splash.h>
42 #include <wx/stdpaths.h>
43 #include <wx/wx.h>
44 LIBDCP_ENABLE_WARNINGS
45 #ifdef __WXGTK__
46 #include <X11/Xlib.h>
47 #endif
48 #include <iostream>
49
50
51 using std::exception;
52 using std::make_shared;
53 using std::shared_ptr;
54 using std::vector;
55 using boost::optional;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
58 #endif
59
60
61 enum {
62         ID_file_open = 1,
63         ID_file_save,
64 };
65
66
67 class AssetPanel : public wxPanel
68 {
69 public:
70         AssetPanel(wxWindow* parent, shared_ptr<dcp::ReelAsset> asset)
71                 : wxPanel(parent, wxID_ANY)
72                 , _asset(asset)
73         {
74                 auto sizer = new wxGridBagSizer(DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
75
76                 int r = 0;
77
78                 add_label_to_sizer(sizer, this, _("Annotation text"), true, wxGBPosition(r, 0));
79                 _annotation_text = new wxTextCtrl(this, wxID_ANY, std_to_wx(asset->annotation_text().get_value_or("")), wxDefaultPosition, wxSize(600, -1));
80                 sizer->Add(_annotation_text, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
81                 ++r;
82
83                 add_label_to_sizer(sizer, this, _("Entry point"), true, wxGBPosition(r, 0));
84                 _entry_point = new wxSpinCtrl(this, wxID_ANY);
85                 sizer->Add(_entry_point, wxGBPosition(r, 1), wxDefaultSpan);
86                 ++r;
87
88                 add_label_to_sizer(sizer, this, _("Duration"), true, wxGBPosition(r, 0));
89                 _duration = new wxSpinCtrl(this, wxID_ANY);
90                 sizer->Add(_duration, wxGBPosition(r, 1), wxDefaultSpan);
91                 ++r;
92
93                 add_label_to_sizer(sizer, this, _("Intrinsic duration"), true, wxGBPosition(r, 0));
94                 auto intrinsic_duration = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
95                 sizer->Add(intrinsic_duration, wxGBPosition(r, 1), wxDefaultSpan);
96                 ++r;
97
98                 auto space = new wxBoxSizer(wxVERTICAL);
99                 space->Add(sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
100                 SetSizerAndFit(space);
101
102                 _entry_point->SetRange(0, 259200);
103                 _entry_point->SetValue(asset->entry_point().get_value_or(0));
104
105                 _duration->SetRange(0, 259200);
106                 _duration->SetValue(asset->duration().get_value_or(0));
107
108                 intrinsic_duration->SetValue(wxString::Format("%ld", asset->intrinsic_duration()));
109
110                 _annotation_text->Bind(wxEVT_TEXT, boost::bind(&AssetPanel::annotation_text_changed, this));
111                 _entry_point->Bind(wxEVT_SPINCTRL, boost::bind(&AssetPanel::entry_point_changed, this));
112                 _duration->Bind(wxEVT_SPINCTRL, boost::bind(&AssetPanel::duration_changed, this));
113         }
114
115 private:
116         void annotation_text_changed()
117         {
118                 _asset->set_annotation_text(wx_to_std(_annotation_text->GetValue()));
119         }
120
121         void entry_point_changed()
122         {
123                 _asset->set_entry_point(_entry_point->GetValue());
124                 auto const fixed_duration = std::min(_asset->intrinsic_duration() - _asset->entry_point().get_value_or(0LL), _asset->duration().get_value_or(_asset->intrinsic_duration()));
125                 _duration->SetValue(fixed_duration);
126                 _asset->set_duration(fixed_duration);
127         }
128
129         void duration_changed()
130         {
131                 _asset->set_duration(_duration->GetValue());
132                 auto const fixed_entry_point = std::min(_asset->intrinsic_duration() - _asset->duration().get_value_or(_asset->intrinsic_duration()), _asset->entry_point().get_value_or(0LL));
133                 _entry_point->SetValue(fixed_entry_point);
134                 _asset->set_entry_point(fixed_entry_point);
135         }
136
137         wxTextCtrl* _annotation_text = nullptr;
138         wxSpinCtrl* _entry_point = nullptr;
139         wxSpinCtrl* _duration = nullptr;
140         shared_ptr<dcp::ReelAsset> _asset;
141 };
142
143
144 class ReelEditor : public wxDialog
145 {
146 public:
147         ReelEditor(wxWindow* parent)
148                 : wxDialog(parent, wxID_ANY, _("Edit reel"))
149         {
150                 auto sizer = new wxBoxSizer(wxVERTICAL);
151                 _notebook = new wxNotebook(this, wxID_ANY);
152                 sizer->Add(_notebook, wxEXPAND | wxALL, 1, DCPOMATIC_DIALOG_BORDER);
153                 SetSizerAndFit(sizer);
154         }
155
156         optional<shared_ptr<dcp::Reel>> get() {
157                 return _reel;
158         }
159
160         void set(shared_ptr<dcp::Reel> reel)
161         {
162                 _reel = reel;
163
164                 _notebook->DeleteAllPages();
165                 if (_reel->main_picture()) {
166                         _notebook->AddPage(new AssetPanel(_notebook, _reel->main_picture()), _("Picture"));
167                 }
168                 if (_reel->main_sound()) {
169                         _notebook->AddPage(new AssetPanel(_notebook, _reel->main_sound()), _("Sound"));
170                 }
171                 if (_reel->main_subtitle()) {
172                         _notebook->AddPage(new AssetPanel(_notebook, _reel->main_subtitle()), _("Subtitle"));
173                 }
174         }
175
176 private:
177         wxNotebook* _notebook = nullptr;
178         shared_ptr<dcp::Reel> _reel;
179 };
180
181
182 class CPLPanel : public wxPanel
183 {
184 public:
185         CPLPanel(wxWindow* parent, shared_ptr<dcp::CPL> cpl)
186                 : wxPanel(parent, wxID_ANY)
187                 , _cpl(cpl)
188         {
189                 auto sizer = new wxGridBagSizer(DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
190
191                 int r = 0;
192
193                 add_label_to_sizer(sizer, this, _("Annotation text"), true, wxGBPosition(r, 0));
194                 _annotation_text = new wxTextCtrl(this, wxID_ANY, std_to_wx(cpl->annotation_text().get_value_or("")), wxDefaultPosition, wxSize(600, -1));
195                 sizer->Add(_annotation_text, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
196                 ++r;
197
198                 add_label_to_sizer(sizer, this, _("Issuer"), true, wxGBPosition(r, 0));
199                 _issuer = new wxTextCtrl(this, wxID_ANY, std_to_wx(cpl->issuer()), wxDefaultPosition, wxSize(600, -1));
200                 sizer->Add(_issuer, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
201                 ++r;
202
203                 add_label_to_sizer(sizer, this, _("Creator"), true, wxGBPosition(r, 0));
204                 _creator = new wxTextCtrl(this, wxID_ANY, std_to_wx(cpl->creator()), wxDefaultPosition, wxSize(600, -1));
205                 sizer->Add(_creator, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
206                 ++r;
207
208                 add_label_to_sizer(sizer, this, _("Content title text"), true, wxGBPosition(r, 0));
209                 _content_title_text = new wxTextCtrl(this, wxID_ANY, std_to_wx(cpl->content_title_text()), wxDefaultPosition, wxSize(600, -1));
210                 sizer->Add(_content_title_text, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
211                 ++r;
212
213                 add_label_to_sizer(sizer, this, _("Reels"), true, wxGBPosition(r, 0));
214                 _reels = new EditableList<shared_ptr<dcp::Reel>, ReelEditor>(
215                         this,
216                         { EditableListColumn("Name", 600, true) },
217                         [this]() { return _cpl->reels(); },
218                         [this](vector<shared_ptr<dcp::Reel>> reels) {
219                                 _cpl->set(reels);
220                         },
221                         [](shared_ptr<dcp::Reel> reel, int) {
222                                 return reel->id();
223                         },
224                         false,
225                         EditableListButton::EDIT
226                 );
227                 sizer->Add(_reels, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
228
229                 auto space = new wxBoxSizer(wxVERTICAL);
230                 space->Add(sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
231                 SetSizerAndFit(space);
232
233                 _annotation_text->Bind(wxEVT_TEXT, boost::bind(&CPLPanel::annotation_text_changed, this));
234                 _issuer->Bind(wxEVT_TEXT, boost::bind(&CPLPanel::issuer_changed, this));
235                 _creator->Bind(wxEVT_TEXT, boost::bind(&CPLPanel::creator_changed, this));
236                 _content_title_text->Bind(wxEVT_TEXT, boost::bind(&CPLPanel::content_title_text_changed, this));
237         }
238
239 private:
240         void annotation_text_changed()
241         {
242                 _cpl->set_annotation_text(wx_to_std(_annotation_text->GetValue()));
243         }
244
245         void issuer_changed()
246         {
247                 _cpl->set_issuer(wx_to_std(_issuer->GetValue()));
248         }
249
250         void creator_changed()
251         {
252                 _cpl->set_creator(wx_to_std(_creator->GetValue()));
253         }
254
255         void content_title_text_changed()
256         {
257                 _cpl->set_content_title_text(wx_to_std(_content_title_text->GetValue()));
258         }
259
260         std::shared_ptr<dcp::CPL> _cpl;
261         wxTextCtrl* _annotation_text = nullptr;
262         wxTextCtrl* _issuer = nullptr;
263         wxTextCtrl* _creator = nullptr;
264         wxTextCtrl* _content_title_text = nullptr;
265         EditableList<shared_ptr<dcp::Reel>, ReelEditor>* _reels;
266 };
267
268
269 class DummyPanel : public wxPanel
270 {
271 public:
272         DummyPanel(wxWindow* parent)
273                 : wxPanel(parent, wxID_ANY)
274         {
275                 auto sizer = new wxBoxSizer(wxVERTICAL);
276                 add_label_to_sizer(sizer, this, _("Open a DCP using File -> Open"), false);
277                 auto space = new wxBoxSizer(wxVERTICAL);
278                 space->Add(sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
279                 SetSizerAndFit(space);
280         }
281 };
282
283
284 class DOMFrame : public wxFrame
285 {
286 public:
287         DOMFrame ()
288                 : wxFrame(nullptr, -1, _("DCP-o-matic Editor"))
289                 , _main_sizer(new wxBoxSizer(wxVERTICAL))
290         {
291                 dcpomatic_log = make_shared<NullLog>();
292
293 #if defined(DCPOMATIC_WINDOWS)
294                 maybe_open_console();
295                 std::cout << "DCP-o-matic Editor is starting." << "\n";
296 #endif
297
298                 auto bar = new wxMenuBar;
299                 setup_menu(bar);
300                 SetMenuBar(bar);
301
302 #ifdef DCPOMATIC_WINDOWS
303                 SetIcon(wxIcon(std_to_wx("id")));
304 #endif
305
306                 Bind(wxEVT_MENU, boost::bind(&DOMFrame::file_open, this), ID_file_open);
307                 Bind(wxEVT_MENU, boost::bind(&DOMFrame::file_save, this), ID_file_save);
308                 Bind(wxEVT_MENU, boost::bind(&DOMFrame::file_exit, this), wxID_EXIT);
309                 Bind(wxEVT_MENU, boost::bind(&DOMFrame::help_about, this), wxID_ABOUT);
310
311                 /* Use a panel as the only child of the Frame so that we avoid
312                    the dark-grey background on Windows.
313                 */
314                 _overall_panel = new wxPanel (this, wxID_ANY);
315
316                 auto sizer = new wxBoxSizer(wxVERTICAL);
317
318                 _notebook = new wxNotebook(_overall_panel, wxID_ANY);
319                 _notebook->AddPage(new DummyPanel(_notebook), _("CPL"));
320
321                 sizer->Add(_notebook, 1, wxEXPAND);
322                 _overall_panel->SetSizerAndFit(sizer);
323         }
324
325         void load_dcp (boost::filesystem::path path)
326         {
327                 _notebook->DeleteAllPages();
328
329                 _dcp = dcp::DCP(path);
330                 _dcp->read();
331                 for (auto cpl: _dcp->cpls()) {
332                         _notebook->AddPage(new CPLPanel(_notebook, cpl), wx_to_std(cpl->annotation_text().get_value_or(cpl->id())));
333                 }
334         }
335
336 private:
337
338         void setup_menu (wxMenuBar* m)
339         {
340                 _file_menu = new wxMenu;
341                 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
342                 _file_menu->AppendSeparator ();
343                 _file_menu->Append (ID_file_save, _("&Save\tCtrl-S"));
344                 _file_menu->AppendSeparator ();
345 #ifdef __WXOSX__
346                 _file_menu->Append (wxID_EXIT, _("&Exit"));
347 #else
348                 _file_menu->Append (wxID_EXIT, _("&Quit"));
349 #endif
350
351                 auto help = new wxMenu;
352 #ifdef __WXOSX__
353                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
354 #else
355                 help->Append (wxID_ABOUT, _("About"));
356 #endif
357
358                 m->Append (_file_menu, _("&File"));
359                 m->Append (help, _("&Help"));
360         }
361
362         void file_open ()
363         {
364                 auto d = wxStandardPaths::Get().GetDocumentsDir();
365                 auto c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
366
367                 int r;
368                 while (true) {
369                         r = c->ShowModal ();
370                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
371                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
372                         } else {
373                                 break;
374                         }
375                 }
376
377                 if (r == wxID_OK) {
378                         boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
379                         load_dcp (dcp);
380                 }
381
382                 c->Destroy ();
383         }
384
385         void file_save ()
386         {
387                 _dcp->write_xml();
388         }
389
390         void file_exit ()
391         {
392                 Close ();
393         }
394
395         void help_about ()
396         {
397                 auto d = new AboutDialog (this);
398                 d->ShowModal ();
399                 d->Destroy ();
400         }
401
402         wxPanel* _overall_panel = nullptr;
403         wxMenu* _file_menu = nullptr;
404         wxSizer* _main_sizer = nullptr;
405         wxNotebook* _notebook = nullptr;
406         optional<dcp::DCP> _dcp;
407 };
408
409
410 static const wxCmdLineEntryDesc command_line_description[] = {
411         { wxCMD_LINE_PARAM, 0, 0, "DCP to edit", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
412         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
413 };
414
415
416 /** @class App
417  *  @brief The magic App class for wxWidgets.
418  */
419 class App : public wxApp
420 {
421 public:
422         App ()
423                 : wxApp ()
424         {
425 #ifdef DCPOMATIC_LINUX
426                 XInitThreads ();
427 #endif
428         }
429
430 private:
431
432         bool OnInit () override
433         {
434                 wxSplashScreen* splash = nullptr;
435                 try {
436                         wxInitAllImageHandlers ();
437
438                         splash = maybe_show_splash ();
439
440                         SetAppName (_("DCP-o-matic Editor"));
441
442                         if (!wxApp::OnInit()) {
443                                 return false;
444                         }
445
446 #ifdef DCPOMATIC_LINUX
447                         unsetenv ("UBUNTU_MENUPROXY");
448 #endif
449
450 #ifdef DCPOMATIC_OSX
451                         make_foreground_application ();
452 #endif
453
454                         dcpomatic_setup_path_encoding ();
455
456                         /* Enable i18n; this will create a Config object
457                            to look for a force-configured language.  This Config
458                            object will be wrong, however, because dcpomatic_setup
459                            hasn't yet been called and there aren't any filters etc.
460                            set up yet.
461                         */
462                         dcpomatic_setup_i18n ();
463
464                         /* Set things up, including filters etc.
465                            which will now be internationalised correctly.
466                         */
467                         dcpomatic_setup ();
468
469                         signal_manager = new wxSignalManager (this);
470
471                         _frame = new DOMFrame ();
472                         SetTopWindow (_frame);
473                         _frame->Maximize ();
474                         if (splash) {
475                                 splash->Destroy ();
476                                 splash = nullptr;
477                         }
478                         _frame->Show ();
479
480                         if (_dcp_to_load) {
481                                 _frame->load_dcp(*_dcp_to_load);
482                         }
483
484                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
485                 }
486                 catch (exception& e)
487                 {
488                         if (splash) {
489                                 splash->Destroy ();
490                         }
491                         error_dialog (0, _("DCP-o-matic Editor could not start."), std_to_wx(e.what()));
492                 }
493
494                 return true;
495         }
496
497         void OnInitCmdLine (wxCmdLineParser& parser) override
498         {
499                 parser.SetDesc (command_line_description);
500                 parser.SetSwitchChars (wxT ("-"));
501         }
502
503         bool OnCmdLineParsed (wxCmdLineParser& parser) override
504         {
505                 if (parser.GetParamCount() > 0) {
506                         _dcp_to_load = wx_to_std(parser.GetParam(0));
507                 }
508
509                 return true;
510         }
511
512         void report_exception ()
513         {
514                 try {
515                         throw;
516                 } catch (FileError& e) {
517                         error_dialog (
518                                 0,
519                                 wxString::Format (
520                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
521                                         std_to_wx (e.what()),
522                                         std_to_wx (e.file().string().c_str ())
523                                         )
524                                 );
525                 } catch (exception& e) {
526                         error_dialog (
527                                 0,
528                                 wxString::Format (
529                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
530                                         std_to_wx (e.what ())
531                                         )
532                                 );
533                 } catch (...) {
534                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
535                 }
536         }
537
538         /* An unhandled exception has occurred inside the main event loop */
539         bool OnExceptionInMainLoop () override
540         {
541                 report_exception ();
542                 /* This will terminate the program */
543                 return false;
544         }
545
546         void OnUnhandledException () override
547         {
548                 report_exception ();
549         }
550
551         void idle ()
552         {
553                 signal_manager->ui_idle ();
554         }
555
556         DOMFrame* _frame = nullptr;
557         optional<boost::filesystem::path> _dcp_to_load;
558 };
559
560
561 IMPLEMENT_APP (App)