More stack-allocated Dialog objects.
[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/constants.h"
27 #include "lib/cross.h"
28 #include "lib/dcpomatic_log.h"
29 #include "lib/null_log.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                         EditableListTitle::INVISIBLE,
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                 wxDirDialog dialog(this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
366
367                 int r;
368                 while (true) {
369                         r = dialog.ShowModal();
370                         if (r == wxID_OK && dialog.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(dialog.GetPath()));
379                         load_dcp (dcp);
380                 }
381         }
382
383         void file_save ()
384         {
385                 _dcp->write_xml();
386         }
387
388         void file_exit ()
389         {
390                 Close ();
391         }
392
393         void help_about ()
394         {
395                 AboutDialog dialog(this);
396                 dialog.ShowModal();
397         }
398
399         wxPanel* _overall_panel = nullptr;
400         wxMenu* _file_menu = nullptr;
401         wxSizer* _main_sizer = nullptr;
402         wxNotebook* _notebook = nullptr;
403         optional<dcp::DCP> _dcp;
404 };
405
406
407 static const wxCmdLineEntryDesc command_line_description[] = {
408         { wxCMD_LINE_PARAM, 0, 0, "DCP to edit", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
409         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
410 };
411
412
413 /** @class App
414  *  @brief The magic App class for wxWidgets.
415  */
416 class App : public wxApp
417 {
418 public:
419         App ()
420                 : wxApp ()
421         {
422 #ifdef DCPOMATIC_LINUX
423                 XInitThreads ();
424 #endif
425         }
426
427 private:
428
429         bool OnInit () override
430         {
431                 wx_ptr<wxSplashScreen> splash;
432                 try {
433                         wxInitAllImageHandlers ();
434
435                         splash = maybe_show_splash ();
436
437                         SetAppName (_("DCP-o-matic Editor"));
438
439                         if (!wxApp::OnInit()) {
440                                 return false;
441                         }
442
443 #ifdef DCPOMATIC_LINUX
444                         unsetenv ("UBUNTU_MENUPROXY");
445 #endif
446
447 #ifdef DCPOMATIC_OSX
448                         make_foreground_application ();
449 #endif
450
451                         dcpomatic_setup_path_encoding ();
452
453                         /* Enable i18n; this will create a Config object
454                            to look for a force-configured language.  This Config
455                            object will be wrong, however, because dcpomatic_setup
456                            hasn't yet been called and there aren't any filters etc.
457                            set up yet.
458                         */
459                         dcpomatic_setup_i18n ();
460
461                         /* Set things up, including filters etc.
462                            which will now be internationalised correctly.
463                         */
464                         dcpomatic_setup ();
465
466                         signal_manager = new wxSignalManager (this);
467
468                         _frame = new DOMFrame ();
469                         SetTopWindow (_frame);
470                         _frame->Maximize ();
471                         if (splash) {
472                                 splash->Destroy ();
473                                 splash = nullptr;
474                         }
475                         _frame->Show ();
476
477                         if (_dcp_to_load) {
478                                 _frame->load_dcp(*_dcp_to_load);
479                         }
480
481                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
482                 }
483                 catch (exception& e)
484                 {
485                         if (splash) {
486                                 splash->Destroy ();
487                         }
488                         error_dialog (0, _("DCP-o-matic Editor could not start."), std_to_wx(e.what()));
489                 }
490
491                 return true;
492         }
493
494         void OnInitCmdLine (wxCmdLineParser& parser) override
495         {
496                 parser.SetDesc (command_line_description);
497                 parser.SetSwitchChars (wxT ("-"));
498         }
499
500         bool OnCmdLineParsed (wxCmdLineParser& parser) override
501         {
502                 if (parser.GetParamCount() > 0) {
503                         _dcp_to_load = wx_to_std(parser.GetParam(0));
504                 }
505
506                 return true;
507         }
508
509         void report_exception ()
510         {
511                 try {
512                         throw;
513                 } catch (FileError& e) {
514                         error_dialog (
515                                 0,
516                                 wxString::Format (
517                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
518                                         std_to_wx (e.what()),
519                                         std_to_wx (e.file().string().c_str ())
520                                         )
521                                 );
522                 } catch (exception& e) {
523                         error_dialog (
524                                 0,
525                                 wxString::Format (
526                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
527                                         std_to_wx (e.what ())
528                                         )
529                                 );
530                 } catch (...) {
531                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
532                 }
533         }
534
535         /* An unhandled exception has occurred inside the main event loop */
536         bool OnExceptionInMainLoop () override
537         {
538                 report_exception ();
539                 /* This will terminate the program */
540                 return false;
541         }
542
543         void OnUnhandledException () override
544         {
545                 report_exception ();
546         }
547
548         void idle ()
549         {
550                 signal_manager->ui_idle ();
551         }
552
553         DOMFrame* _frame = nullptr;
554         optional<boost::filesystem::path> _dcp_to_load;
555 };
556
557
558 IMPLEMENT_APP (App)