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>
40 #include <ApplicationServices/ApplicationServices.h>
42 #include <boost/foreach.hpp>
50 using boost::optional;
51 using boost::shared_ptr;
52 using boost::weak_ptr;
54 using boost::dynamic_pointer_cast;
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);
277 tick_icon.LoadFile ("tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
278 no_tick_icon.LoadFile ("no_tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
280 boost::filesystem::path tick_path = shared_path() / "tick.png";
281 tick_icon.LoadFile (std_to_wx(tick_path.string()), wxBITMAP_TYPE_PNG);
282 boost::filesystem::path no_tick_path = shared_path() / "no_tick.png";
283 no_tick_icon.LoadFile (std_to_wx(no_tick_path.string()), wxBITMAP_TYPE_PNG);
285 images->Add (tick_icon);
286 images->Add (no_tick_icon);
288 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
290 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
292 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
293 _up = new Button (parent, _("Up"));
294 _down = new Button (parent, _("Down"));
295 _add = new Button (parent, _("Add"));
296 _remove = new Button (parent, _("Remove"));
297 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
298 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
299 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
300 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
302 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
306 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistContent::setup_sensitivity, this));
307 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistContent::setup_sensitivity, this));
308 _name->Bind (wxEVT_TEXT, bind(&PlaylistContent::name_changed, this));
309 _up->Bind (wxEVT_BUTTON, bind(&PlaylistContent::up_clicked, this));
310 _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
311 _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
312 _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
320 void set (shared_ptr<SignalSPL> playlist)
322 _playlist = playlist;
323 _list->DeleteAllItems ();
325 BOOST_FOREACH (SPLEntry i, _playlist->get()) {
328 _name->SetValue (std_to_wx(_playlist->name()));
330 _name->SetValue (wxT(""));
332 setup_sensitivity ();
335 shared_ptr<SignalSPL> playlist () const
345 _playlist->set_name (wx_to_std(_name->GetValue()));
349 void add (SPLEntry e)
352 item.SetId (_list->GetItemCount());
353 long const N = _list->InsertItem (item);
357 void set_item (long N, SPLEntry e)
359 _list->SetItem (N, 0, std_to_wx(e.name));
360 _list->SetItem (N, 1, std_to_wx(e.id));
361 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
362 _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
365 void setup_sensitivity ()
367 bool const have_list = static_cast<bool>(_playlist);
368 int const num_selected = _list->GetSelectedItemCount ();
369 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
370 _name->Enable (have_list);
371 _list->Enable (have_list);
372 _up->Enable (have_list && selected > 0);
373 _down->Enable (have_list && selected != -1 && selected < (_list->GetItemCount() - 1));
374 _add->Enable (have_list);
375 _remove->Enable (have_list && num_selected > 0);
380 int const r = _content_dialog->ShowModal ();
382 shared_ptr<Content> content = _content_dialog->selected ();
384 SPLEntry e (content);
386 DCPOMATIC_ASSERT (_playlist);
394 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
399 DCPOMATIC_ASSERT (_playlist);
401 SPLEntry tmp = (*_playlist)[s];
402 (*_playlist)[s] = (*_playlist)[s-1];
403 (*_playlist)[s-1] = tmp;
405 set_item (s - 1, (*_playlist)[s-1]);
406 set_item (s, (*_playlist)[s]);
411 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
412 if (s > (_list->GetItemCount() - 1)) {
416 DCPOMATIC_ASSERT (_playlist);
418 SPLEntry tmp = (*_playlist)[s];
419 (*_playlist)[s] = (*_playlist)[s+1];
420 (*_playlist)[s+1] = tmp;
422 set_item (s + 1, (*_playlist)[s+1]);
423 set_item (s, (*_playlist)[s]);
426 void remove_clicked ()
428 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
433 DCPOMATIC_ASSERT (_playlist);
434 _playlist->remove (s);
435 _list->DeleteItem (s);
438 ContentDialog* _content_dialog;
446 shared_ptr<SignalSPL> _playlist;
450 class DOMFrame : public wxFrame
453 explicit DOMFrame (wxString const & title)
454 : wxFrame (0, -1, title)
455 , _content_dialog (new ContentDialog(this))
458 wxMenuBar* bar = new wxMenuBar;
462 /* Use a panel as the only child of the Frame so that we avoid
463 the dark-grey background on Windows.
465 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
466 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
468 _playlist_list = new PlaylistList (overall_panel, _content_dialog);
469 _playlist_content = new PlaylistContent (overall_panel, _content_dialog);
471 sizer->Add (_playlist_list->sizer());
472 sizer->Add (_playlist_content->sizer());
474 overall_panel->SetSizer (sizer);
476 _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
478 _playlist_content->set (_playlist_list->first_playlist());
480 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
481 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
482 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
489 /* false here allows the close handler to veto the close request */
495 AboutDialog* d = new AboutDialog (this);
500 void edit_preferences ()
502 if (!_config_dialog) {
503 _config_dialog = create_playlist_editor_config_dialog ();
505 _config_dialog->Show (this);
508 void change_playlist (shared_ptr<SignalSPL> playlist)
510 shared_ptr<SignalSPL> old = _playlist_content->playlist ();
514 _playlist_content->set (playlist);
517 void save_playlist (shared_ptr<SignalSPL> playlist)
519 optional<boost::filesystem::path> dir = Config::instance()->player_playlist_directory();
521 error_dialog (this, _("No playlist folder is specified in preferences. Please set on and then try again."));
524 playlist->write (*dir / (playlist->id() + ".xml"));
527 void setup_menu (wxMenuBar* m)
529 wxMenu* file = new wxMenu;
531 file->Append (wxID_EXIT, _("&Exit"));
533 file->Append (wxID_EXIT, _("&Quit"));
537 wxMenu* edit = new wxMenu;
538 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
541 wxMenu* help = new wxMenu;
543 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
545 help->Append (wxID_ABOUT, _("About"));
548 m->Append (file, _("&File"));
550 m->Append (edit, _("&Edit"));
552 m->Append (help, _("&Help"));
555 ContentDialog* _content_dialog;
556 PlaylistList* _playlist_list;
557 PlaylistContent* _playlist_content;
558 wxPreferencesEditor* _config_dialog;
562 * @brief The magic App class for wxWidgets.
564 class App : public wxApp
577 wxInitAllImageHandlers ();
578 SetAppName (_("DCP-o-matic Playlist Editor"));
580 if (!wxApp::OnInit()) {
584 #ifdef DCPOMATIC_LINUX
585 unsetenv ("UBUNTU_MENUPROXY");
589 ProcessSerialNumber serial;
590 GetCurrentProcess (&serial);
591 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
594 dcpomatic_setup_path_encoding ();
596 /* Enable i18n; this will create a Config object
597 to look for a force-configured language. This Config
598 object will be wrong, however, because dcpomatic_setup
599 hasn't yet been called and there aren't any filters etc.
602 dcpomatic_setup_i18n ();
604 /* Set things up, including filters etc.
605 which will now be internationalised correctly.
609 /* Force the configuration to be re-loaded correctly next
614 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
615 SetTopWindow (_frame);
619 signal_manager = new wxSignalManager (this);
620 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
626 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
630 /* An unhandled exception has occurred inside the main event loop */
631 bool OnExceptionInMainLoop ()
635 } catch (FileError& e) {
639 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
640 std_to_wx (e.what()),
641 std_to_wx (e.file().string().c_str ())
644 } catch (exception& e) {
648 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
649 std_to_wx (e.what ())
653 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
656 /* This will terminate the program */
660 void OnUnhandledException ()
662 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
667 signal_manager->ui_idle ();