2 Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "wx/about_dialog.h"
23 #include "wx/content_view.h"
24 #include "wx/dcpomatic_button.h"
25 #include "wx/playlist_editor_config_dialog.h"
26 #include "wx/wx_signal_manager.h"
27 #include "wx/wx_util.h"
28 #include "lib/config.h"
29 #include "lib/cross.h"
30 #include "lib/dcp_content.h"
33 #include "lib/spl_entry.h"
35 #include <dcp/warnings.h>
36 LIBDCP_DISABLE_WARNINGS
37 #include <wx/imaglist.h>
38 #include <wx/listctrl.h>
39 #include <wx/preferences.h>
40 #include <wx/spinctrl.h>
42 LIBDCP_ENABLE_WARNINGS
48 using std::make_shared;
50 using std::shared_ptr;
55 using boost::optional;
56 using std::dynamic_pointer_cast;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
62 class ContentDialog : public wxDialog, public ContentStore
65 ContentDialog (wxWindow* parent)
66 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
67 , _content_view (new ContentView(this))
69 _content_view->update ();
71 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
72 SetSizer (overall_sizer);
74 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
76 auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
78 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
81 overall_sizer->Layout ();
83 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&ContentView::update, _content_view));
86 shared_ptr<Content> selected () const
88 return _content_view->selected ();
91 shared_ptr<Content> get (string digest) const override
93 return _content_view->get (digest);
97 ContentView* _content_view;
98 boost::signals2::scoped_connection _config_changed_connection;
106 PlaylistList (wxPanel* parent, ContentStore* content_store)
107 : _sizer (new wxBoxSizer(wxVERTICAL))
108 , _content_store (content_store)
110 auto label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
111 label->SetLabelMarkup (_("<b>Playlists</b>"));
112 _sizer->Add (label, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
114 _list = new wxListCtrl (
115 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
118 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 840);
119 _list->AppendColumn (_("Length"), wxLIST_FORMAT_LEFT, 100);
121 auto button_sizer = new wxBoxSizer (wxVERTICAL);
122 _new = new Button (parent, _("New"));
123 button_sizer->Add (_new, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
124 _delete = new Button (parent, _("Delete"));
125 button_sizer->Add (_delete, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
127 auto list = new wxBoxSizer (wxHORIZONTAL);
128 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
129 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
135 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistList::selection_changed, this));
136 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistList::selection_changed, this));
137 _new->Bind (wxEVT_BUTTON, bind(&PlaylistList::new_playlist, this));
138 _delete->Bind (wxEVT_BUTTON, bind(&PlaylistList::delete_playlist, this));
146 shared_ptr<SignalSPL> first_playlist () const
148 if (_playlists.empty()) {
152 return _playlists.front ();
155 boost::signals2::signal<void (shared_ptr<SignalSPL>)> Edit;
158 void add_playlist_to_view (shared_ptr<const SignalSPL> playlist)
161 item.SetId (_list->GetItemCount());
162 long const N = _list->InsertItem (item);
163 _list->SetItem (N, 0, std_to_wx(playlist->name()));
166 void add_playlist_to_model (shared_ptr<SignalSPL> playlist)
168 _playlists.push_back (playlist);
169 playlist->Changed.connect(bind(&PlaylistList::changed, this, weak_ptr<SignalSPL>(playlist), _1));
172 void changed(weak_ptr<SignalSPL> wp, SignalSPL::Change change)
174 auto playlist = wp.lock ();
180 case SignalSPL::Change::NAME:
183 for (auto i: _playlists) {
185 _list->SetItem (N, 0, std_to_wx(i->name()));
191 case SignalSPL::Change::CONTENT:
192 if (auto dir = Config::instance()->player_playlist_directory()) {
193 playlist->write(*dir / (playlist->id() + ".xml"));
199 void load_playlists ()
201 auto path = Config::instance()->player_playlist_directory();
206 _list->DeleteAllItems ();
208 for (auto i: boost::filesystem::directory_iterator(*path)) {
209 auto spl = make_shared<SignalSPL>();
211 spl->read (i, _content_store);
212 add_playlist_to_model (spl);
216 for (auto i: _playlists) {
217 add_playlist_to_view (i);
223 shared_ptr<SignalSPL> spl (new SignalSPL(wx_to_std(_("New Playlist"))));
224 add_playlist_to_model (spl);
225 add_playlist_to_view (spl);
226 _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
229 void delete_playlist ()
231 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
232 if (selected < 0 || selected >= int(_playlists.size())) {
236 auto dir = Config::instance()->player_playlist_directory();
241 boost::filesystem::remove (*dir / (_playlists[selected]->id() + ".xml"));
242 _list->DeleteItem (selected);
243 _playlists.erase (_playlists.begin() + selected);
245 Edit (shared_ptr<SignalSPL>());
248 void selection_changed ()
250 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
251 if (selected < 0 || selected >= int(_playlists.size())) {
252 Edit (shared_ptr<SignalSPL>());
254 Edit (_playlists[selected]);
262 vector<shared_ptr<SignalSPL>> _playlists;
263 ContentStore* _content_store;
267 class PlaylistContent
270 PlaylistContent (wxPanel* parent, ContentDialog* content_dialog)
271 : _content_dialog (content_dialog)
272 , _sizer (new wxBoxSizer(wxVERTICAL))
274 auto title = new wxBoxSizer (wxHORIZONTAL);
275 auto label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
276 label->SetLabelMarkup (_("<b>Playlist:</b>"));
277 title->Add (label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
278 _name = new wxTextCtrl (parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(400, -1));
279 title->Add (_name, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
280 _sizer->Add (title, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
282 auto list = new wxBoxSizer (wxHORIZONTAL);
284 _list = new wxListCtrl (
285 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
288 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
289 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
290 _list->AppendColumn (_("Type"), wxLIST_FORMAT_LEFT, 100);
291 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
293 auto images = new wxImageList (16, 16);
296 tick_icon.LoadFile (bitmap_path("tick.png"), wxBITMAP_TYPE_PNG);
297 no_tick_icon.LoadFile (bitmap_path("no_tick.png"), wxBITMAP_TYPE_PNG);
298 images->Add (tick_icon);
299 images->Add (no_tick_icon);
301 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
303 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
305 auto button_sizer = new wxBoxSizer (wxVERTICAL);
306 _up = new Button (parent, _("Up"));
307 _down = new Button (parent, _("Down"));
308 _add = new Button (parent, _("Add"));
309 _remove = new Button (parent, _("Remove"));
310 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
311 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
312 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
313 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
315 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
319 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistContent::setup_sensitivity, this));
320 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistContent::setup_sensitivity, this));
321 _name->Bind (wxEVT_TEXT, bind(&PlaylistContent::name_changed, this));
322 _up->Bind (wxEVT_BUTTON, bind(&PlaylistContent::up_clicked, this));
323 _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
324 _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
325 _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
335 void set (shared_ptr<SignalSPL> playlist)
337 _playlist = playlist;
338 _list->DeleteAllItems ();
340 for (auto i: _playlist->get()) {
343 _name->SetValue (std_to_wx(_playlist->name()));
345 _name->SetValue (wxT(""));
347 setup_sensitivity ();
350 shared_ptr<SignalSPL> playlist () const
360 _playlist->set_name (wx_to_std(_name->GetValue()));
364 void add (SPLEntry e)
367 item.SetId (_list->GetItemCount());
368 long const N = _list->InsertItem (item);
372 void set_item (long N, SPLEntry e)
374 _list->SetItem (N, 0, std_to_wx(e.name));
375 _list->SetItem (N, 1, std_to_wx(e.id));
376 _list->SetItem (N, 2, std_to_wx(e.kind->name()));
377 _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
380 void setup_sensitivity ()
382 bool const have_list = static_cast<bool>(_playlist);
383 int const num_selected = _list->GetSelectedItemCount ();
384 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
385 _name->Enable (have_list);
386 _list->Enable (have_list);
387 _up->Enable (have_list && selected > 0);
388 _down->Enable (have_list && selected != -1 && selected < (_list->GetItemCount() - 1));
389 _add->Enable (have_list);
390 _remove->Enable (have_list && num_selected > 0);
395 int const r = _content_dialog->ShowModal ();
397 auto content = _content_dialog->selected ();
399 SPLEntry e (content);
401 DCPOMATIC_ASSERT (_playlist);
409 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
414 DCPOMATIC_ASSERT (_playlist);
416 _playlist->swap(s, s - 1);
418 set_item (s - 1, (*_playlist)[s-1]);
419 set_item (s, (*_playlist)[s]);
424 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
425 if (s > (_list->GetItemCount() - 1)) {
429 DCPOMATIC_ASSERT (_playlist);
431 _playlist->swap(s, s + 1);
433 set_item (s + 1, (*_playlist)[s+1]);
434 set_item (s, (*_playlist)[s]);
437 void remove_clicked ()
439 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
444 DCPOMATIC_ASSERT (_playlist);
445 _playlist->remove (s);
446 _list->DeleteItem (s);
449 ContentDialog* _content_dialog;
457 shared_ptr<SignalSPL> _playlist;
461 class DOMFrame : public wxFrame
464 explicit DOMFrame (wxString const & title)
465 : wxFrame (nullptr, wxID_ANY, title)
466 , _content_dialog (new ContentDialog(this))
467 , _config_dialog (nullptr)
469 auto bar = new wxMenuBar;
473 /* Use a panel as the only child of the Frame so that we avoid
474 the dark-grey background on Windows.
476 auto overall_panel = new wxPanel (this, wxID_ANY);
477 auto sizer = new wxBoxSizer (wxVERTICAL);
479 _playlist_list = new PlaylistList (overall_panel, _content_dialog);
480 _playlist_content = new PlaylistContent (overall_panel, _content_dialog);
482 sizer->Add (_playlist_list->sizer());
483 sizer->Add (_playlist_content->sizer());
485 overall_panel->SetSizer (sizer);
487 _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
489 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
490 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
491 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
493 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&DOMFrame::config_changed, this));
500 /* false here allows the close handler to veto the close request */
506 auto d = new AboutDialog (this);
511 void edit_preferences ()
513 if (!_config_dialog) {
514 _config_dialog = create_playlist_editor_config_dialog ();
516 _config_dialog->Show (this);
519 void change_playlist (shared_ptr<SignalSPL> playlist)
521 auto old = _playlist_content->playlist ();
525 _playlist_content->set (playlist);
528 void save_playlist (shared_ptr<SignalSPL> playlist)
530 auto dir = Config::instance()->player_playlist_directory();
532 error_dialog (this, _("No playlist folder is specified in preferences. Please set one and then try again."));
535 playlist->write (*dir / (playlist->id() + ".xml"));
538 void setup_menu (wxMenuBar* m)
540 auto file = new wxMenu;
542 file->Append (wxID_EXIT, _("&Exit"));
544 file->Append (wxID_EXIT, _("&Quit"));
548 auto edit = new wxMenu;
549 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
552 auto help = new wxMenu;
554 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
556 help->Append (wxID_ABOUT, _("About"));
559 m->Append (file, _("&File"));
561 m->Append (edit, _("&Edit"));
563 m->Append (help, _("&Help"));
567 void config_changed ()
570 Config::instance()->write_config();
571 } catch (exception& e) {
575 _("Could not write to config file at %s. Your changes have not been saved."),
576 std_to_wx (Config::instance()->cinemas_file().string()).data()
582 ContentDialog* _content_dialog;
583 PlaylistList* _playlist_list;
584 PlaylistContent* _playlist_content;
585 wxPreferencesEditor* _config_dialog;
586 boost::signals2::scoped_connection _config_changed_connection;
591 * @brief The magic App class for wxWidgets.
593 class App : public wxApp
603 bool OnInit () override
606 wxInitAllImageHandlers ();
607 SetAppName (_("DCP-o-matic Playlist Editor"));
609 if (!wxApp::OnInit()) {
613 #ifdef DCPOMATIC_LINUX
614 unsetenv ("UBUNTU_MENUPROXY");
618 make_foreground_application ();
621 dcpomatic_setup_path_encoding ();
623 /* Enable i18n; this will create a Config object
624 to look for a force-configured language. This Config
625 object will be wrong, however, because dcpomatic_setup
626 hasn't yet been called and there aren't any filters etc.
629 dcpomatic_setup_i18n ();
631 /* Set things up, including filters etc.
632 which will now be internationalised correctly.
636 /* Force the configuration to be re-loaded correctly next
641 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
642 SetTopWindow (_frame);
646 signal_manager = new wxSignalManager (this);
647 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
653 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
657 /* An unhandled exception has occurred inside the main event loop */
658 bool OnExceptionInMainLoop () override
662 } catch (FileError& e) {
666 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
667 std_to_wx (e.what()),
668 std_to_wx (e.file().string().c_str ())
671 } catch (exception& e) {
675 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
676 std_to_wx (e.what ())
680 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
683 /* This will terminate the program */
687 void OnUnhandledException () override
689 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
694 signal_manager->ui_idle ();