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;
64 save_playlist(shared_ptr<const SPL> playlist)
66 if (auto dir = Config::instance()->player_playlist_directory()) {
67 playlist->write(*dir / (playlist->id() + ".xml"));
72 class ContentDialog : public wxDialog, public ContentStore
75 ContentDialog (wxWindow* parent)
76 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
77 , _content_view (new ContentView(this))
79 _content_view->update ();
81 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
82 SetSizer (overall_sizer);
84 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
86 auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
88 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
91 overall_sizer->Layout ();
93 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&ContentView::update, _content_view));
96 shared_ptr<Content> selected () const
98 return _content_view->selected ();
101 shared_ptr<Content> get (string digest) const override
103 return _content_view->get (digest);
107 ContentView* _content_view;
108 boost::signals2::scoped_connection _config_changed_connection;
116 PlaylistList (wxPanel* parent, ContentStore* content_store)
117 : _sizer (new wxBoxSizer(wxVERTICAL))
118 , _content_store (content_store)
121 auto label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
122 label->SetLabelMarkup (_("<b>Playlists</b>"));
123 _sizer->Add (label, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
125 _list = new wxListCtrl (
126 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
129 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 840);
130 _list->AppendColumn (_("Length"), wxLIST_FORMAT_LEFT, 100);
132 auto button_sizer = new wxBoxSizer (wxVERTICAL);
133 _new = new Button (parent, _("New"));
134 button_sizer->Add (_new, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
135 _delete = new Button (parent, _("Delete"));
136 button_sizer->Add (_delete, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
138 auto list = new wxBoxSizer (wxHORIZONTAL);
139 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
140 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
146 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistList::selection_changed, this));
147 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistList::selection_changed, this));
148 _new->Bind (wxEVT_BUTTON, bind(&PlaylistList::new_playlist, this));
149 _delete->Bind (wxEVT_BUTTON, bind(&PlaylistList::delete_playlist, this));
159 shared_ptr<SignalSPL> first_playlist () const
161 if (_playlists.empty()) {
165 return _playlists.front ();
168 boost::signals2::signal<void (shared_ptr<SignalSPL>)> Edit;
171 void setup_sensitivity()
173 _delete->Enable(static_cast<bool>(selected()));
176 void add_playlist_to_view (shared_ptr<const SignalSPL> playlist)
179 item.SetId (_list->GetItemCount());
180 long const N = _list->InsertItem (item);
181 _list->SetItem (N, 0, std_to_wx(playlist->name()));
184 void add_playlist_to_model (shared_ptr<SignalSPL> playlist)
186 _playlists.push_back (playlist);
187 playlist->Changed.connect(bind(&PlaylistList::changed, this, weak_ptr<SignalSPL>(playlist), _1));
190 void changed(weak_ptr<SignalSPL> wp, SignalSPL::Change change)
192 auto playlist = wp.lock ();
198 case SignalSPL::Change::NAME:
201 for (auto i: _playlists) {
203 _list->SetItem (N, 0, std_to_wx(i->name()));
209 case SignalSPL::Change::CONTENT:
210 save_playlist(playlist);
215 void load_playlists ()
217 auto path = Config::instance()->player_playlist_directory();
222 _list->DeleteAllItems ();
224 for (auto i: boost::filesystem::directory_iterator(*path)) {
225 auto spl = make_shared<SignalSPL>();
227 spl->read (i, _content_store);
228 add_playlist_to_model (spl);
232 for (auto i: _playlists) {
233 add_playlist_to_view (i);
239 auto dir = Config::instance()->player_playlist_directory();
241 error_dialog(_parent, _("No playlist folder is specified in preferences. Please set one and then try again."));
245 shared_ptr<SignalSPL> spl (new SignalSPL(wx_to_std(_("New Playlist"))));
246 add_playlist_to_model (spl);
247 add_playlist_to_view (spl);
248 _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
251 boost::optional<int> selected() const
253 long int selected = _list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
254 if (selected < 0 || selected >= int(_playlists.size())) {
261 void delete_playlist ()
263 auto index = selected();
268 auto dir = Config::instance()->player_playlist_directory();
273 boost::filesystem::remove(*dir / (_playlists[*index]->id() + ".xml"));
274 _list->DeleteItem(*index);
275 _playlists.erase(_playlists.begin() + *index);
277 Edit(shared_ptr<SignalSPL>());
280 void selection_changed ()
282 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
283 if (selected < 0 || selected >= int(_playlists.size())) {
284 Edit (shared_ptr<SignalSPL>());
286 Edit (_playlists[selected]);
296 vector<shared_ptr<SignalSPL>> _playlists;
297 ContentStore* _content_store;
302 class PlaylistContent
305 PlaylistContent (wxPanel* parent, ContentDialog* content_dialog)
306 : _content_dialog (content_dialog)
307 , _sizer (new wxBoxSizer(wxVERTICAL))
309 auto title = new wxBoxSizer (wxHORIZONTAL);
310 auto label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
311 label->SetLabelMarkup (_("<b>Playlist:</b>"));
312 title->Add (label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
313 _name = new wxTextCtrl (parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(400, -1));
314 title->Add (_name, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
315 _sizer->Add (title, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
317 auto list = new wxBoxSizer (wxHORIZONTAL);
319 _list = new wxListCtrl (
320 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
323 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
324 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
325 _list->AppendColumn (_("Type"), wxLIST_FORMAT_LEFT, 100);
326 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
328 auto images = new wxImageList (16, 16);
331 tick_icon.LoadFile (bitmap_path("tick.png"), wxBITMAP_TYPE_PNG);
332 no_tick_icon.LoadFile (bitmap_path("no_tick.png"), wxBITMAP_TYPE_PNG);
333 images->Add (tick_icon);
334 images->Add (no_tick_icon);
336 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
338 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
340 auto button_sizer = new wxBoxSizer (wxVERTICAL);
341 _up = new Button (parent, _("Up"));
342 _down = new Button (parent, _("Down"));
343 _add = new Button (parent, _("Add"));
344 _remove = new Button (parent, _("Remove"));
345 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
346 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
347 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
348 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
350 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
354 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistContent::setup_sensitivity, this));
355 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistContent::setup_sensitivity, this));
356 _name->Bind (wxEVT_TEXT, bind(&PlaylistContent::name_changed, this));
357 _up->Bind (wxEVT_BUTTON, bind(&PlaylistContent::up_clicked, this));
358 _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
359 _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
360 _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
370 void set (shared_ptr<SignalSPL> playlist)
372 _playlist = playlist;
373 _list->DeleteAllItems ();
375 for (auto i: _playlist->get()) {
378 _name->SetValue (std_to_wx(_playlist->name()));
380 _name->SetValue (wxT(""));
382 setup_sensitivity ();
385 shared_ptr<SignalSPL> playlist () const
395 _playlist->set_name (wx_to_std(_name->GetValue()));
399 void add (SPLEntry e)
402 item.SetId (_list->GetItemCount());
403 long const N = _list->InsertItem (item);
407 void set_item (long N, SPLEntry e)
409 _list->SetItem (N, 0, std_to_wx(e.name));
410 _list->SetItem (N, 1, std_to_wx(e.id));
411 _list->SetItem (N, 2, std_to_wx(e.kind->name()));
412 _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
415 void setup_sensitivity ()
417 bool const have_list = static_cast<bool>(_playlist);
418 int const num_selected = _list->GetSelectedItemCount ();
419 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
420 _name->Enable (have_list);
421 _list->Enable (have_list);
422 _up->Enable (have_list && selected > 0);
423 _down->Enable (have_list && selected != -1 && selected < (_list->GetItemCount() - 1));
424 _add->Enable (have_list);
425 _remove->Enable (have_list && num_selected > 0);
430 int const r = _content_dialog->ShowModal ();
432 auto content = _content_dialog->selected ();
434 SPLEntry e (content);
436 DCPOMATIC_ASSERT (_playlist);
444 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
449 DCPOMATIC_ASSERT (_playlist);
451 _playlist->swap(s, s - 1);
453 set_item (s - 1, (*_playlist)[s-1]);
454 set_item (s, (*_playlist)[s]);
459 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
460 if (s > (_list->GetItemCount() - 1)) {
464 DCPOMATIC_ASSERT (_playlist);
466 _playlist->swap(s, s + 1);
468 set_item (s + 1, (*_playlist)[s+1]);
469 set_item (s, (*_playlist)[s]);
472 void remove_clicked ()
474 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
479 DCPOMATIC_ASSERT (_playlist);
480 _playlist->remove (s);
481 _list->DeleteItem (s);
484 ContentDialog* _content_dialog;
492 shared_ptr<SignalSPL> _playlist;
496 class DOMFrame : public wxFrame
499 explicit DOMFrame (wxString const & title)
500 : wxFrame (nullptr, wxID_ANY, title)
501 , _content_dialog (new ContentDialog(this))
502 , _config_dialog (nullptr)
504 auto bar = new wxMenuBar;
508 /* Use a panel as the only child of the Frame so that we avoid
509 the dark-grey background on Windows.
511 auto overall_panel = new wxPanel (this, wxID_ANY);
512 auto sizer = new wxBoxSizer (wxVERTICAL);
514 _playlist_list = new PlaylistList (overall_panel, _content_dialog);
515 _playlist_content = new PlaylistContent (overall_panel, _content_dialog);
517 sizer->Add (_playlist_list->sizer());
518 sizer->Add (_playlist_content->sizer());
520 overall_panel->SetSizer (sizer);
522 _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
524 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
525 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
526 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
528 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&DOMFrame::config_changed, this));
535 /* false here allows the close handler to veto the close request */
541 auto d = new AboutDialog (this);
546 void edit_preferences ()
548 if (!_config_dialog) {
549 _config_dialog = create_playlist_editor_config_dialog ();
551 _config_dialog->Show (this);
554 void change_playlist (shared_ptr<SignalSPL> playlist)
556 auto old = _playlist_content->playlist ();
560 _playlist_content->set (playlist);
563 void setup_menu (wxMenuBar* m)
565 auto file = new wxMenu;
567 file->Append (wxID_EXIT, _("&Exit"));
569 file->Append (wxID_EXIT, _("&Quit"));
573 auto edit = new wxMenu;
574 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
577 auto help = new wxMenu;
579 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
581 help->Append (wxID_ABOUT, _("About"));
584 m->Append (file, _("&File"));
586 m->Append (edit, _("&Edit"));
588 m->Append (help, _("&Help"));
592 void config_changed ()
595 Config::instance()->write_config();
596 } catch (exception& e) {
600 _("Could not write to config file at %s. Your changes have not been saved."),
601 std_to_wx (Config::instance()->cinemas_file().string()).data()
607 ContentDialog* _content_dialog;
608 PlaylistList* _playlist_list;
609 PlaylistContent* _playlist_content;
610 wxPreferencesEditor* _config_dialog;
611 boost::signals2::scoped_connection _config_changed_connection;
616 * @brief The magic App class for wxWidgets.
618 class App : public wxApp
628 bool OnInit () override
631 wxInitAllImageHandlers ();
632 SetAppName (_("DCP-o-matic Playlist Editor"));
634 if (!wxApp::OnInit()) {
638 #ifdef DCPOMATIC_LINUX
639 unsetenv ("UBUNTU_MENUPROXY");
643 make_foreground_application ();
646 dcpomatic_setup_path_encoding ();
648 /* Enable i18n; this will create a Config object
649 to look for a force-configured language. This Config
650 object will be wrong, however, because dcpomatic_setup
651 hasn't yet been called and there aren't any filters etc.
654 dcpomatic_setup_i18n ();
656 /* Set things up, including filters etc.
657 which will now be internationalised correctly.
661 /* Force the configuration to be re-loaded correctly next
666 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
667 SetTopWindow (_frame);
671 signal_manager = new wxSignalManager (this);
672 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
678 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
682 /* An unhandled exception has occurred inside the main event loop */
683 bool OnExceptionInMainLoop () override
687 } catch (FileError& e) {
691 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
692 std_to_wx (e.what()),
693 std_to_wx (e.file().string().c_str ())
696 } catch (exception& e) {
700 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
701 std_to_wx (e.what ())
705 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
708 /* This will terminate the program */
712 void OnUnhandledException () override
714 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
719 signal_manager->ui_idle ();