2 Copyright (C) 2018-2020 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/>.
21 #include "../wx/wx_util.h"
22 #include "../wx/wx_signal_manager.h"
23 #include "../wx/content_view.h"
24 #include "../wx/dcpomatic_button.h"
25 #include "../wx/about_dialog.h"
26 #include "../wx/playlist_editor_config_dialog.h"
27 #include "../lib/util.h"
28 #include "../lib/config.h"
29 #include "../lib/cross.h"
30 #include "../lib/film.h"
31 #include "../lib/dcp_content.h"
32 #include "../lib/spl_entry.h"
33 #include "../lib/spl.h"
35 #include <wx/listctrl.h>
36 #include <wx/imaglist.h>
37 #include <wx/spinctrl.h>
38 #include <wx/preferences.h>
46 using boost::optional;
47 using std::shared_ptr;
50 using std::dynamic_pointer_cast;
51 #if BOOST_VERSION >= 106100
52 using namespace boost::placeholders;
55 class ContentDialog : public wxDialog, public ContentStore
58 ContentDialog (wxWindow* parent)
59 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
60 , _content_view (new ContentView(this))
62 _content_view->update ();
64 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
65 SetSizer (overall_sizer);
67 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
69 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
71 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
74 overall_sizer->Layout ();
76 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&ContentView::update, _content_view));
79 shared_ptr<Content> selected () const
81 return _content_view->selected ();
84 shared_ptr<Content> get (string digest) const
86 return _content_view->get (digest);
90 ContentView* _content_view;
91 boost::signals2::scoped_connection _config_changed_connection;
99 PlaylistList (wxPanel* parent, ContentStore* content_store)
100 : _sizer (new wxBoxSizer(wxVERTICAL))
101 , _content_store (content_store)
103 wxStaticText* label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
104 label->SetLabelMarkup (_("<b>Playlists</b>"));
105 _sizer->Add (label, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
107 _list = new wxListCtrl (
108 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
111 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 840);
112 _list->AppendColumn (_("Length"), wxLIST_FORMAT_LEFT, 100);
114 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
115 _new = new Button (parent, _("New"));
116 button_sizer->Add (_new, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
117 _delete = new Button (parent, _("Delete"));
118 button_sizer->Add (_delete, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
120 wxSizer* list = new wxBoxSizer (wxHORIZONTAL);
121 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
122 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
128 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistList::selection_changed, this));
129 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistList::selection_changed, this));
130 _new->Bind (wxEVT_BUTTON, bind(&PlaylistList::new_playlist, this));
131 _delete->Bind (wxEVT_BUTTON, bind(&PlaylistList::delete_playlist, this));
139 shared_ptr<SignalSPL> first_playlist () const
141 if (_playlists.empty()) {
142 return shared_ptr<SignalSPL>();
145 return _playlists.front ();
148 boost::signals2::signal<void (shared_ptr<SignalSPL>)> Edit;
151 void add_playlist_to_view (shared_ptr<const SignalSPL> playlist)
154 item.SetId (_list->GetItemCount());
155 long const N = _list->InsertItem (item);
156 _list->SetItem (N, 0, std_to_wx(playlist->name()));
159 void add_playlist_to_model (shared_ptr<SignalSPL> playlist)
161 _playlists.push_back (playlist);
162 playlist->NameChanged.connect (bind(&PlaylistList::name_changed, this, weak_ptr<SignalSPL>(playlist)));
165 void name_changed (weak_ptr<SignalSPL> wp)
167 shared_ptr<SignalSPL> playlist = wp.lock ();
173 for (auto i: _playlists) {
175 _list->SetItem (N, 0, std_to_wx(i->name()));
181 void load_playlists ()
183 optional<boost::filesystem::path> path = Config::instance()->player_playlist_directory();
188 _list->DeleteAllItems ();
190 for (boost::filesystem::directory_iterator i(*path); i != boost::filesystem::directory_iterator(); ++i) {
191 shared_ptr<SignalSPL> spl(new SignalSPL);
193 spl->read (*i, _content_store);
194 add_playlist_to_model (spl);
198 for (auto i: _playlists) {
199 add_playlist_to_view (i);
205 shared_ptr<SignalSPL> spl (new SignalSPL(wx_to_std(_("New Playlist"))));
206 add_playlist_to_model (spl);
207 add_playlist_to_view (spl);
208 _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
211 void delete_playlist ()
213 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
214 if (selected < 0 || selected >= int(_playlists.size())) {
218 optional<boost::filesystem::path> dir = Config::instance()->player_playlist_directory();
223 boost::filesystem::remove (*dir / (_playlists[selected]->id() + ".xml"));
224 _list->DeleteItem (selected);
225 _playlists.erase (_playlists.begin() + selected);
227 Edit (shared_ptr<SignalSPL>());
230 void selection_changed ()
232 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
233 if (selected < 0 || selected >= int(_playlists.size())) {
234 Edit (shared_ptr<SignalSPL>());
236 Edit (_playlists[selected]);
244 vector<shared_ptr<SignalSPL> > _playlists;
245 ContentStore* _content_store;
249 class PlaylistContent
252 PlaylistContent (wxPanel* parent, ContentDialog* content_dialog)
253 : _content_dialog (content_dialog)
254 , _sizer (new wxBoxSizer(wxVERTICAL))
256 wxBoxSizer* title = new wxBoxSizer (wxHORIZONTAL);
257 wxStaticText* label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
258 label->SetLabelMarkup (_("<b>Playlist:</b>"));
259 title->Add (label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
260 _name = new wxTextCtrl (parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(400, -1));
261 title->Add (_name, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
262 _sizer->Add (title, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
264 wxBoxSizer* list = new wxBoxSizer (wxHORIZONTAL);
266 _list = new wxListCtrl (
267 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
270 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
271 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
272 _list->AppendColumn (_("Type"), wxLIST_FORMAT_LEFT, 100);
273 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
275 wxImageList* images = new wxImageList (16, 16);
278 tick_icon.LoadFile (bitmap_path("tick"), wxBITMAP_TYPE_PNG);
279 no_tick_icon.LoadFile (bitmap_path("no_tick"), wxBITMAP_TYPE_PNG);
280 images->Add (tick_icon);
281 images->Add (no_tick_icon);
283 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
285 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
287 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
288 _up = new Button (parent, _("Up"));
289 _down = new Button (parent, _("Down"));
290 _add = new Button (parent, _("Add"));
291 _remove = new Button (parent, _("Remove"));
292 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
293 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
294 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
295 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
297 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
301 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistContent::setup_sensitivity, this));
302 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistContent::setup_sensitivity, this));
303 _name->Bind (wxEVT_TEXT, bind(&PlaylistContent::name_changed, this));
304 _up->Bind (wxEVT_BUTTON, bind(&PlaylistContent::up_clicked, this));
305 _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
306 _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
307 _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
315 void set (shared_ptr<SignalSPL> playlist)
317 _playlist = playlist;
318 _list->DeleteAllItems ();
320 for (auto i: _playlist->get()) {
323 _name->SetValue (std_to_wx(_playlist->name()));
325 _name->SetValue (wxT(""));
327 setup_sensitivity ();
330 shared_ptr<SignalSPL> playlist () const
340 _playlist->set_name (wx_to_std(_name->GetValue()));
344 void add (SPLEntry e)
347 item.SetId (_list->GetItemCount());
348 long const N = _list->InsertItem (item);
352 void set_item (long N, SPLEntry e)
354 _list->SetItem (N, 0, std_to_wx(e.name));
355 _list->SetItem (N, 1, std_to_wx(e.id));
356 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
357 _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
360 void setup_sensitivity ()
362 bool const have_list = static_cast<bool>(_playlist);
363 int const num_selected = _list->GetSelectedItemCount ();
364 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
365 _name->Enable (have_list);
366 _list->Enable (have_list);
367 _up->Enable (have_list && selected > 0);
368 _down->Enable (have_list && selected != -1 && selected < (_list->GetItemCount() - 1));
369 _add->Enable (have_list);
370 _remove->Enable (have_list && num_selected > 0);
375 int const r = _content_dialog->ShowModal ();
377 shared_ptr<Content> content = _content_dialog->selected ();
379 SPLEntry e (content);
381 DCPOMATIC_ASSERT (_playlist);
389 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
394 DCPOMATIC_ASSERT (_playlist);
396 SPLEntry tmp = (*_playlist)[s];
397 (*_playlist)[s] = (*_playlist)[s-1];
398 (*_playlist)[s-1] = tmp;
400 set_item (s - 1, (*_playlist)[s-1]);
401 set_item (s, (*_playlist)[s]);
406 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
407 if (s > (_list->GetItemCount() - 1)) {
411 DCPOMATIC_ASSERT (_playlist);
413 SPLEntry tmp = (*_playlist)[s];
414 (*_playlist)[s] = (*_playlist)[s+1];
415 (*_playlist)[s+1] = tmp;
417 set_item (s + 1, (*_playlist)[s+1]);
418 set_item (s, (*_playlist)[s]);
421 void remove_clicked ()
423 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
428 DCPOMATIC_ASSERT (_playlist);
429 _playlist->remove (s);
430 _list->DeleteItem (s);
433 ContentDialog* _content_dialog;
441 shared_ptr<SignalSPL> _playlist;
445 class DOMFrame : public wxFrame
448 explicit DOMFrame (wxString const & title)
449 : wxFrame (0, -1, title)
450 , _content_dialog (new ContentDialog(this))
453 wxMenuBar* bar = new wxMenuBar;
457 /* Use a panel as the only child of the Frame so that we avoid
458 the dark-grey background on Windows.
460 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
461 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
463 _playlist_list = new PlaylistList (overall_panel, _content_dialog);
464 _playlist_content = new PlaylistContent (overall_panel, _content_dialog);
466 sizer->Add (_playlist_list->sizer());
467 sizer->Add (_playlist_content->sizer());
469 overall_panel->SetSizer (sizer);
471 _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
473 _playlist_content->set (_playlist_list->first_playlist());
475 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
476 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
477 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
479 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&DOMFrame::config_changed, this));
486 /* false here allows the close handler to veto the close request */
492 AboutDialog* d = new AboutDialog (this);
497 void edit_preferences ()
499 if (!_config_dialog) {
500 _config_dialog = create_playlist_editor_config_dialog ();
502 _config_dialog->Show (this);
505 void change_playlist (shared_ptr<SignalSPL> playlist)
507 shared_ptr<SignalSPL> old = _playlist_content->playlist ();
511 _playlist_content->set (playlist);
514 void save_playlist (shared_ptr<SignalSPL> playlist)
516 optional<boost::filesystem::path> dir = Config::instance()->player_playlist_directory();
518 error_dialog (this, _("No playlist folder is specified in preferences. Please set one and then try again."));
521 playlist->write (*dir / (playlist->id() + ".xml"));
524 void setup_menu (wxMenuBar* m)
526 wxMenu* file = new wxMenu;
528 file->Append (wxID_EXIT, _("&Exit"));
530 file->Append (wxID_EXIT, _("&Quit"));
534 wxMenu* edit = new wxMenu;
535 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
538 wxMenu* help = new wxMenu;
540 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
542 help->Append (wxID_ABOUT, _("About"));
545 m->Append (file, _("&File"));
547 m->Append (edit, _("&Edit"));
549 m->Append (help, _("&Help"));
553 void config_changed ()
556 Config::instance()->write_config();
557 } catch (exception& e) {
561 _("Could not write to config file at %s. Your changes have not been saved."),
562 std_to_wx (Config::instance()->cinemas_file().string()).data()
569 ContentDialog* _content_dialog;
570 PlaylistList* _playlist_list;
571 PlaylistContent* _playlist_content;
572 wxPreferencesEditor* _config_dialog;
573 boost::signals2::scoped_connection _config_changed_connection;
577 * @brief The magic App class for wxWidgets.
579 class App : public wxApp
592 wxInitAllImageHandlers ();
593 SetAppName (_("DCP-o-matic Playlist Editor"));
595 if (!wxApp::OnInit()) {
599 #ifdef DCPOMATIC_LINUX
600 unsetenv ("UBUNTU_MENUPROXY");
604 make_foreground_application ();
607 dcpomatic_setup_path_encoding ();
609 /* Enable i18n; this will create a Config object
610 to look for a force-configured language. This Config
611 object will be wrong, however, because dcpomatic_setup
612 hasn't yet been called and there aren't any filters etc.
615 dcpomatic_setup_i18n ();
617 /* Set things up, including filters etc.
618 which will now be internationalised correctly.
622 /* Force the configuration to be re-loaded correctly next
627 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
628 SetTopWindow (_frame);
632 signal_manager = new wxSignalManager (this);
633 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
639 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
643 /* An unhandled exception has occurred inside the main event loop */
644 bool OnExceptionInMainLoop ()
648 } catch (FileError& e) {
652 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
653 std_to_wx (e.what()),
654 std_to_wx (e.file().string().c_str ())
657 } catch (exception& e) {
661 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
662 std_to_wx (e.what ())
666 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
669 /* This will terminate the program */
673 void OnUnhandledException ()
675 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
680 signal_manager->ui_idle ();