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>
39 #include <boost/foreach.hpp>
47 using boost::optional;
48 using std::shared_ptr;
51 using std::dynamic_pointer_cast;
52 #if BOOST_VERSION >= 106100
53 using namespace boost::placeholders;
56 class ContentDialog : public wxDialog, public ContentStore
59 ContentDialog (wxWindow* parent)
60 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
61 , _content_view (new ContentView(this))
63 _content_view->update ();
65 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
66 SetSizer (overall_sizer);
68 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
70 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
72 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
75 overall_sizer->Layout ();
77 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&ContentView::update, _content_view));
80 shared_ptr<Content> selected () const
82 return _content_view->selected ();
85 shared_ptr<Content> get (string digest) const
87 return _content_view->get (digest);
91 ContentView* _content_view;
92 boost::signals2::scoped_connection _config_changed_connection;
100 PlaylistList (wxPanel* parent, ContentStore* content_store)
101 : _sizer (new wxBoxSizer(wxVERTICAL))
102 , _content_store (content_store)
104 wxStaticText* label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
105 label->SetLabelMarkup (_("<b>Playlists</b>"));
106 _sizer->Add (label, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
108 _list = new wxListCtrl (
109 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
112 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 840);
113 _list->AppendColumn (_("Length"), wxLIST_FORMAT_LEFT, 100);
115 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
116 _new = new Button (parent, _("New"));
117 button_sizer->Add (_new, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
118 _delete = new Button (parent, _("Delete"));
119 button_sizer->Add (_delete, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
121 wxSizer* list = new wxBoxSizer (wxHORIZONTAL);
122 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
123 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
129 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistList::selection_changed, this));
130 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistList::selection_changed, this));
131 _new->Bind (wxEVT_BUTTON, bind(&PlaylistList::new_playlist, this));
132 _delete->Bind (wxEVT_BUTTON, bind(&PlaylistList::delete_playlist, this));
140 shared_ptr<SignalSPL> first_playlist () const
142 if (_playlists.empty()) {
143 return shared_ptr<SignalSPL>();
146 return _playlists.front ();
149 boost::signals2::signal<void (shared_ptr<SignalSPL>)> Edit;
152 void add_playlist_to_view (shared_ptr<const SignalSPL> playlist)
155 item.SetId (_list->GetItemCount());
156 long const N = _list->InsertItem (item);
157 _list->SetItem (N, 0, std_to_wx(playlist->name()));
160 void add_playlist_to_model (shared_ptr<SignalSPL> playlist)
162 _playlists.push_back (playlist);
163 playlist->NameChanged.connect (bind(&PlaylistList::name_changed, this, weak_ptr<SignalSPL>(playlist)));
166 void name_changed (weak_ptr<SignalSPL> wp)
168 shared_ptr<SignalSPL> playlist = wp.lock ();
174 BOOST_FOREACH (shared_ptr<SignalSPL> i, _playlists) {
176 _list->SetItem (N, 0, std_to_wx(i->name()));
182 void load_playlists ()
184 optional<boost::filesystem::path> path = Config::instance()->player_playlist_directory();
189 _list->DeleteAllItems ();
191 for (boost::filesystem::directory_iterator i(*path); i != boost::filesystem::directory_iterator(); ++i) {
192 shared_ptr<SignalSPL> spl(new SignalSPL);
194 spl->read (*i, _content_store);
195 add_playlist_to_model (spl);
199 BOOST_FOREACH (shared_ptr<SignalSPL> i, _playlists) {
200 add_playlist_to_view (i);
206 shared_ptr<SignalSPL> spl (new SignalSPL(wx_to_std(_("New Playlist"))));
207 add_playlist_to_model (spl);
208 add_playlist_to_view (spl);
209 _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
212 void delete_playlist ()
214 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
215 if (selected < 0 || selected >= int(_playlists.size())) {
219 optional<boost::filesystem::path> dir = Config::instance()->player_playlist_directory();
224 boost::filesystem::remove (*dir / (_playlists[selected]->id() + ".xml"));
225 _list->DeleteItem (selected);
226 _playlists.erase (_playlists.begin() + selected);
228 Edit (shared_ptr<SignalSPL>());
231 void selection_changed ()
233 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
234 if (selected < 0 || selected >= int(_playlists.size())) {
235 Edit (shared_ptr<SignalSPL>());
237 Edit (_playlists[selected]);
245 vector<shared_ptr<SignalSPL> > _playlists;
246 ContentStore* _content_store;
250 class PlaylistContent
253 PlaylistContent (wxPanel* parent, ContentDialog* content_dialog)
254 : _content_dialog (content_dialog)
255 , _sizer (new wxBoxSizer(wxVERTICAL))
257 wxBoxSizer* title = new wxBoxSizer (wxHORIZONTAL);
258 wxStaticText* label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
259 label->SetLabelMarkup (_("<b>Playlist:</b>"));
260 title->Add (label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
261 _name = new wxTextCtrl (parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(400, -1));
262 title->Add (_name, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
263 _sizer->Add (title, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
265 wxBoxSizer* list = new wxBoxSizer (wxHORIZONTAL);
267 _list = new wxListCtrl (
268 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
271 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
272 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
273 _list->AppendColumn (_("Type"), wxLIST_FORMAT_LEFT, 100);
274 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
276 wxImageList* images = new wxImageList (16, 16);
279 tick_icon.LoadFile (bitmap_path("tick"), wxBITMAP_TYPE_PNG);
280 no_tick_icon.LoadFile (bitmap_path("no_tick"), wxBITMAP_TYPE_PNG);
281 images->Add (tick_icon);
282 images->Add (no_tick_icon);
284 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
286 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
288 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
289 _up = new Button (parent, _("Up"));
290 _down = new Button (parent, _("Down"));
291 _add = new Button (parent, _("Add"));
292 _remove = new Button (parent, _("Remove"));
293 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
294 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
295 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
296 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
298 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
302 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistContent::setup_sensitivity, this));
303 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistContent::setup_sensitivity, this));
304 _name->Bind (wxEVT_TEXT, bind(&PlaylistContent::name_changed, this));
305 _up->Bind (wxEVT_BUTTON, bind(&PlaylistContent::up_clicked, this));
306 _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
307 _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
308 _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
316 void set (shared_ptr<SignalSPL> playlist)
318 _playlist = playlist;
319 _list->DeleteAllItems ();
321 BOOST_FOREACH (SPLEntry i, _playlist->get()) {
324 _name->SetValue (std_to_wx(_playlist->name()));
326 _name->SetValue (wxT(""));
328 setup_sensitivity ();
331 shared_ptr<SignalSPL> playlist () const
341 _playlist->set_name (wx_to_std(_name->GetValue()));
345 void add (SPLEntry e)
348 item.SetId (_list->GetItemCount());
349 long const N = _list->InsertItem (item);
353 void set_item (long N, SPLEntry e)
355 _list->SetItem (N, 0, std_to_wx(e.name));
356 _list->SetItem (N, 1, std_to_wx(e.id));
357 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
358 _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
361 void setup_sensitivity ()
363 bool const have_list = static_cast<bool>(_playlist);
364 int const num_selected = _list->GetSelectedItemCount ();
365 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
366 _name->Enable (have_list);
367 _list->Enable (have_list);
368 _up->Enable (have_list && selected > 0);
369 _down->Enable (have_list && selected != -1 && selected < (_list->GetItemCount() - 1));
370 _add->Enable (have_list);
371 _remove->Enable (have_list && num_selected > 0);
376 int const r = _content_dialog->ShowModal ();
378 shared_ptr<Content> content = _content_dialog->selected ();
380 SPLEntry e (content);
382 DCPOMATIC_ASSERT (_playlist);
390 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
395 DCPOMATIC_ASSERT (_playlist);
397 SPLEntry tmp = (*_playlist)[s];
398 (*_playlist)[s] = (*_playlist)[s-1];
399 (*_playlist)[s-1] = tmp;
401 set_item (s - 1, (*_playlist)[s-1]);
402 set_item (s, (*_playlist)[s]);
407 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
408 if (s > (_list->GetItemCount() - 1)) {
412 DCPOMATIC_ASSERT (_playlist);
414 SPLEntry tmp = (*_playlist)[s];
415 (*_playlist)[s] = (*_playlist)[s+1];
416 (*_playlist)[s+1] = tmp;
418 set_item (s + 1, (*_playlist)[s+1]);
419 set_item (s, (*_playlist)[s]);
422 void remove_clicked ()
424 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
429 DCPOMATIC_ASSERT (_playlist);
430 _playlist->remove (s);
431 _list->DeleteItem (s);
434 ContentDialog* _content_dialog;
442 shared_ptr<SignalSPL> _playlist;
446 class DOMFrame : public wxFrame
449 explicit DOMFrame (wxString const & title)
450 : wxFrame (0, -1, title)
451 , _content_dialog (new ContentDialog(this))
454 wxMenuBar* bar = new wxMenuBar;
458 /* Use a panel as the only child of the Frame so that we avoid
459 the dark-grey background on Windows.
461 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
462 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
464 _playlist_list = new PlaylistList (overall_panel, _content_dialog);
465 _playlist_content = new PlaylistContent (overall_panel, _content_dialog);
467 sizer->Add (_playlist_list->sizer());
468 sizer->Add (_playlist_content->sizer());
470 overall_panel->SetSizer (sizer);
472 _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
474 _playlist_content->set (_playlist_list->first_playlist());
476 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
477 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
478 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
480 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&DOMFrame::config_changed, this));
487 /* false here allows the close handler to veto the close request */
493 AboutDialog* d = new AboutDialog (this);
498 void edit_preferences ()
500 if (!_config_dialog) {
501 _config_dialog = create_playlist_editor_config_dialog ();
503 _config_dialog->Show (this);
506 void change_playlist (shared_ptr<SignalSPL> playlist)
508 shared_ptr<SignalSPL> old = _playlist_content->playlist ();
512 _playlist_content->set (playlist);
515 void save_playlist (shared_ptr<SignalSPL> playlist)
517 optional<boost::filesystem::path> dir = Config::instance()->player_playlist_directory();
519 error_dialog (this, _("No playlist folder is specified in preferences. Please set one and then try again."));
522 playlist->write (*dir / (playlist->id() + ".xml"));
525 void setup_menu (wxMenuBar* m)
527 wxMenu* file = new wxMenu;
529 file->Append (wxID_EXIT, _("&Exit"));
531 file->Append (wxID_EXIT, _("&Quit"));
535 wxMenu* edit = new wxMenu;
536 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
539 wxMenu* help = new wxMenu;
541 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
543 help->Append (wxID_ABOUT, _("About"));
546 m->Append (file, _("&File"));
548 m->Append (edit, _("&Edit"));
550 m->Append (help, _("&Help"));
554 void config_changed ()
557 Config::instance()->write_config();
558 } catch (exception& e) {
562 _("Could not write to config file at %s. Your changes have not been saved."),
563 std_to_wx (Config::instance()->cinemas_file().string()).data()
570 ContentDialog* _content_dialog;
571 PlaylistList* _playlist_list;
572 PlaylistContent* _playlist_content;
573 wxPreferencesEditor* _config_dialog;
574 boost::signals2::scoped_connection _config_changed_connection;
578 * @brief The magic App class for wxWidgets.
580 class App : public wxApp
593 wxInitAllImageHandlers ();
594 SetAppName (_("DCP-o-matic Playlist Editor"));
596 if (!wxApp::OnInit()) {
600 #ifdef DCPOMATIC_LINUX
601 unsetenv ("UBUNTU_MENUPROXY");
605 make_foreground_application ();
608 dcpomatic_setup_path_encoding ();
610 /* Enable i18n; this will create a Config object
611 to look for a force-configured language. This Config
612 object will be wrong, however, because dcpomatic_setup
613 hasn't yet been called and there aren't any filters etc.
616 dcpomatic_setup_i18n ();
618 /* Set things up, including filters etc.
619 which will now be internationalised correctly.
623 /* Force the configuration to be re-loaded correctly next
628 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
629 SetTopWindow (_frame);
633 signal_manager = new wxSignalManager (this);
634 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
640 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
644 /* An unhandled exception has occurred inside the main event loop */
645 bool OnExceptionInMainLoop ()
649 } catch (FileError& e) {
653 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
654 std_to_wx (e.what()),
655 std_to_wx (e.file().string().c_str ())
658 } catch (exception& e) {
662 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
663 std_to_wx (e.what ())
667 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
670 /* This will terminate the program */
674 void OnUnhandledException ()
676 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
681 signal_manager->ui_idle ();