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 boost::shared_ptr;
49 using boost::weak_ptr;
51 using boost::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 ();
78 shared_ptr<Content> selected () const
80 return _content_view->selected ();
83 shared_ptr<Content> get (string digest) const
85 return _content_view->get (digest);
89 ContentView* _content_view;
97 PlaylistList (wxPanel* parent, ContentStore* content_store)
98 : _sizer (new wxBoxSizer(wxVERTICAL))
99 , _content_store (content_store)
101 wxStaticText* label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
102 label->SetLabelMarkup (_("<b>Playlists</b>"));
103 _sizer->Add (label, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
105 _list = new wxListCtrl (
106 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
109 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 840);
110 _list->AppendColumn (_("Length"), wxLIST_FORMAT_LEFT, 100);
112 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
113 _new = new Button (parent, _("New"));
114 button_sizer->Add (_new, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
115 _delete = new Button (parent, _("Delete"));
116 button_sizer->Add (_delete, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
118 wxSizer* list = new wxBoxSizer (wxHORIZONTAL);
119 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
120 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
126 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistList::selection_changed, this));
127 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistList::selection_changed, this));
128 _new->Bind (wxEVT_BUTTON, bind(&PlaylistList::new_playlist, this));
129 _delete->Bind (wxEVT_BUTTON, bind(&PlaylistList::delete_playlist, this));
137 shared_ptr<SignalSPL> first_playlist () const
139 if (_playlists.empty()) {
140 return shared_ptr<SignalSPL>();
143 return _playlists.front ();
146 boost::signals2::signal<void (shared_ptr<SignalSPL>)> Edit;
149 void add_playlist_to_view (shared_ptr<const SignalSPL> playlist)
152 item.SetId (_list->GetItemCount());
153 long const N = _list->InsertItem (item);
154 _list->SetItem (N, 0, std_to_wx(playlist->name()));
157 void add_playlist_to_model (shared_ptr<SignalSPL> playlist)
159 _playlists.push_back (playlist);
160 playlist->NameChanged.connect (bind(&PlaylistList::name_changed, this, weak_ptr<SignalSPL>(playlist)));
163 void name_changed (weak_ptr<SignalSPL> wp)
165 shared_ptr<SignalSPL> playlist = wp.lock ();
171 BOOST_FOREACH (shared_ptr<SignalSPL> i, _playlists) {
173 _list->SetItem (N, 0, std_to_wx(i->name()));
179 void load_playlists ()
181 optional<boost::filesystem::path> path = Config::instance()->player_playlist_directory();
186 _list->DeleteAllItems ();
188 for (boost::filesystem::directory_iterator i(*path); i != boost::filesystem::directory_iterator(); ++i) {
189 shared_ptr<SignalSPL> spl(new SignalSPL);
191 spl->read (*i, _content_store);
192 add_playlist_to_model (spl);
196 BOOST_FOREACH (shared_ptr<SignalSPL> i, _playlists) {
197 add_playlist_to_view (i);
203 shared_ptr<SignalSPL> spl (new SignalSPL(wx_to_std(_("New Playlist"))));
204 add_playlist_to_model (spl);
205 add_playlist_to_view (spl);
206 _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
209 void delete_playlist ()
211 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
212 if (selected < 0 || selected >= int(_playlists.size())) {
216 optional<boost::filesystem::path> dir = Config::instance()->player_playlist_directory();
221 boost::filesystem::remove (*dir / (_playlists[selected]->id() + ".xml"));
222 _list->DeleteItem (selected);
223 _playlists.erase (_playlists.begin() + selected);
225 Edit (shared_ptr<SignalSPL>());
228 void selection_changed ()
230 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
231 if (selected < 0 || selected >= int(_playlists.size())) {
232 Edit (shared_ptr<SignalSPL>());
234 Edit (_playlists[selected]);
242 vector<shared_ptr<SignalSPL> > _playlists;
243 ContentStore* _content_store;
247 class PlaylistContent
250 PlaylistContent (wxPanel* parent, ContentDialog* content_dialog)
251 : _content_dialog (content_dialog)
252 , _sizer (new wxBoxSizer(wxVERTICAL))
254 wxBoxSizer* title = new wxBoxSizer (wxHORIZONTAL);
255 wxStaticText* label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
256 label->SetLabelMarkup (_("<b>Playlist:</b>"));
257 title->Add (label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
258 _name = new wxTextCtrl (parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(400, -1));
259 title->Add (_name, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
260 _sizer->Add (title, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
262 wxBoxSizer* list = new wxBoxSizer (wxHORIZONTAL);
264 _list = new wxListCtrl (
265 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
268 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
269 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
270 _list->AppendColumn (_("Type"), wxLIST_FORMAT_LEFT, 100);
271 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
273 wxImageList* images = new wxImageList (16, 16);
276 tick_icon.LoadFile (bitmap_path("tick"), wxBITMAP_TYPE_PNG);
277 no_tick_icon.LoadFile (bitmap_path("no_tick"), wxBITMAP_TYPE_PNG);
278 images->Add (tick_icon);
279 images->Add (no_tick_icon);
281 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
283 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
285 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
286 _up = new Button (parent, _("Up"));
287 _down = new Button (parent, _("Down"));
288 _add = new Button (parent, _("Add"));
289 _remove = new Button (parent, _("Remove"));
290 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
291 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
292 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
293 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
295 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
299 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistContent::setup_sensitivity, this));
300 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistContent::setup_sensitivity, this));
301 _name->Bind (wxEVT_TEXT, bind(&PlaylistContent::name_changed, this));
302 _up->Bind (wxEVT_BUTTON, bind(&PlaylistContent::up_clicked, this));
303 _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
304 _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
305 _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
313 void set (shared_ptr<SignalSPL> playlist)
315 _playlist = playlist;
316 _list->DeleteAllItems ();
318 BOOST_FOREACH (SPLEntry i, _playlist->get()) {
321 _name->SetValue (std_to_wx(_playlist->name()));
323 _name->SetValue (wxT(""));
325 setup_sensitivity ();
328 shared_ptr<SignalSPL> playlist () const
338 _playlist->set_name (wx_to_std(_name->GetValue()));
342 void add (SPLEntry e)
345 item.SetId (_list->GetItemCount());
346 long const N = _list->InsertItem (item);
350 void set_item (long N, SPLEntry e)
352 _list->SetItem (N, 0, std_to_wx(e.name));
353 _list->SetItem (N, 1, std_to_wx(e.id));
354 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
355 _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
358 void setup_sensitivity ()
360 bool const have_list = static_cast<bool>(_playlist);
361 int const num_selected = _list->GetSelectedItemCount ();
362 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
363 _name->Enable (have_list);
364 _list->Enable (have_list);
365 _up->Enable (have_list && selected > 0);
366 _down->Enable (have_list && selected != -1 && selected < (_list->GetItemCount() - 1));
367 _add->Enable (have_list);
368 _remove->Enable (have_list && num_selected > 0);
373 int const r = _content_dialog->ShowModal ();
375 shared_ptr<Content> content = _content_dialog->selected ();
377 SPLEntry e (content);
379 DCPOMATIC_ASSERT (_playlist);
387 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
392 DCPOMATIC_ASSERT (_playlist);
394 SPLEntry tmp = (*_playlist)[s];
395 (*_playlist)[s] = (*_playlist)[s-1];
396 (*_playlist)[s-1] = tmp;
398 set_item (s - 1, (*_playlist)[s-1]);
399 set_item (s, (*_playlist)[s]);
404 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
405 if (s > (_list->GetItemCount() - 1)) {
409 DCPOMATIC_ASSERT (_playlist);
411 SPLEntry tmp = (*_playlist)[s];
412 (*_playlist)[s] = (*_playlist)[s+1];
413 (*_playlist)[s+1] = tmp;
415 set_item (s + 1, (*_playlist)[s+1]);
416 set_item (s, (*_playlist)[s]);
419 void remove_clicked ()
421 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
426 DCPOMATIC_ASSERT (_playlist);
427 _playlist->remove (s);
428 _list->DeleteItem (s);
431 ContentDialog* _content_dialog;
439 shared_ptr<SignalSPL> _playlist;
443 class DOMFrame : public wxFrame
446 explicit DOMFrame (wxString const & title)
447 : wxFrame (0, -1, title)
448 , _content_dialog (new ContentDialog(this))
451 wxMenuBar* bar = new wxMenuBar;
455 /* Use a panel as the only child of the Frame so that we avoid
456 the dark-grey background on Windows.
458 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
459 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
461 _playlist_list = new PlaylistList (overall_panel, _content_dialog);
462 _playlist_content = new PlaylistContent (overall_panel, _content_dialog);
464 sizer->Add (_playlist_list->sizer());
465 sizer->Add (_playlist_content->sizer());
467 overall_panel->SetSizer (sizer);
469 _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
471 _playlist_content->set (_playlist_list->first_playlist());
473 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
474 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
475 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
482 /* false here allows the close handler to veto the close request */
488 AboutDialog* d = new AboutDialog (this);
493 void edit_preferences ()
495 if (!_config_dialog) {
496 _config_dialog = create_playlist_editor_config_dialog ();
498 _config_dialog->Show (this);
501 void change_playlist (shared_ptr<SignalSPL> playlist)
503 shared_ptr<SignalSPL> old = _playlist_content->playlist ();
507 _playlist_content->set (playlist);
510 void save_playlist (shared_ptr<SignalSPL> playlist)
512 optional<boost::filesystem::path> dir = Config::instance()->player_playlist_directory();
514 error_dialog (this, _("No playlist folder is specified in preferences. Please set one and then try again."));
517 playlist->write (*dir / (playlist->id() + ".xml"));
520 void setup_menu (wxMenuBar* m)
522 wxMenu* file = new wxMenu;
524 file->Append (wxID_EXIT, _("&Exit"));
526 file->Append (wxID_EXIT, _("&Quit"));
530 wxMenu* edit = new wxMenu;
531 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
534 wxMenu* help = new wxMenu;
536 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
538 help->Append (wxID_ABOUT, _("About"));
541 m->Append (file, _("&File"));
543 m->Append (edit, _("&Edit"));
545 m->Append (help, _("&Help"));
548 ContentDialog* _content_dialog;
549 PlaylistList* _playlist_list;
550 PlaylistContent* _playlist_content;
551 wxPreferencesEditor* _config_dialog;
555 * @brief The magic App class for wxWidgets.
557 class App : public wxApp
570 wxInitAllImageHandlers ();
571 SetAppName (_("DCP-o-matic Playlist Editor"));
573 if (!wxApp::OnInit()) {
577 #ifdef DCPOMATIC_LINUX
578 unsetenv ("UBUNTU_MENUPROXY");
582 make_foreground_application ();
585 dcpomatic_setup_path_encoding ();
587 /* Enable i18n; this will create a Config object
588 to look for a force-configured language. This Config
589 object will be wrong, however, because dcpomatic_setup
590 hasn't yet been called and there aren't any filters etc.
593 dcpomatic_setup_i18n ();
595 /* Set things up, including filters etc.
596 which will now be internationalised correctly.
600 /* Force the configuration to be re-loaded correctly next
605 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
606 SetTopWindow (_frame);
610 signal_manager = new wxSignalManager (this);
611 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
617 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
621 /* An unhandled exception has occurred inside the main event loop */
622 bool OnExceptionInMainLoop ()
626 } catch (FileError& e) {
630 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
631 std_to_wx (e.what()),
632 std_to_wx (e.file().string().c_str ())
635 } catch (exception& e) {
639 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
640 std_to_wx (e.what ())
644 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
647 /* This will terminate the program */
651 void OnUnhandledException ()
653 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
658 signal_manager->ui_idle ();