2 Copyright (C) 2018 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 "../lib/util.h"
25 #include "../lib/config.h"
26 #include "../lib/cross.h"
27 #include "../lib/film.h"
28 #include "../lib/dcp_content.h"
30 #include <wx/listctrl.h>
31 #include <wx/imaglist.h>
36 using boost::optional;
37 using boost::shared_ptr;
38 using boost::weak_ptr;
40 using boost::dynamic_pointer_cast;
45 PlaylistEntry (boost::shared_ptr<Content> content)
47 , disable_timeline (false)
48 , stop_after_play (false)
53 PlaylistEntry (boost::shared_ptr<Content> content, cxml::ConstNodePtr node)
54 : skippable (node->bool_child("Skippable"))
55 , disable_timeline (node->bool_child("DisableTimeline"))
56 , stop_after_play (node->bool_child("StopAfterPlay"))
61 void construct (shared_ptr<Content> content)
63 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (content);
64 digest = content->digest ();
67 DCPOMATIC_ASSERT (dcp->cpl());
69 kind = dcp->content_kind().get_value_or(dcp::FEATURE);
71 encrypted = dcp->encrypted ();
73 name = content->path(0).filename().string();
79 void as_xml (xmlpp::Element* e)
81 e->add_child("Digest")->add_child_text(digest);
82 e->add_child("Skippable")->add_child_text(skippable ? "1" : "0");
83 e->add_child("DisableTimeline")->add_child_text(disable_timeline ? "1" : "0");
84 e->add_child("StopAfterPlay")->add_child_text(stop_after_play ? "1" : "0");
88 /** Digest of this content */
90 /** CPL ID or something else for MP4 (?) */
92 dcp::ContentKind kind;
100 bool disable_timeline;
101 bool stop_after_play;
104 class ContentDialog : public wxDialog
107 ContentDialog (wxWindow* parent, weak_ptr<Film> film)
108 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
109 , _content_view (new ContentView(this, film))
111 _content_view->update ();
113 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
114 SetSizer (overall_sizer);
116 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
118 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
120 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
123 overall_sizer->Layout ();
126 shared_ptr<Content> selected () const
128 return _content_view->selected ();
131 shared_ptr<Content> get (string digest) const
133 return _content_view->get (digest);
137 ContentView* _content_view;
140 class DOMFrame : public wxFrame
143 explicit DOMFrame (wxString const & title)
144 : wxFrame (0, -1, title)
145 /* XXX: this is a bit of a hack, but we need it to be able to use the Content class hierarchy */
146 , _film (new Film(optional<boost::filesystem::path>()))
147 , _content_dialog (new ContentDialog(this, _film))
149 /* Use a panel as the only child of the Frame so that we avoid
150 the dark-grey background on Windows.
152 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
153 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
155 _list = new wxListCtrl (
156 overall_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
159 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
160 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
161 _list->AppendColumn (_("Type"), wxLIST_FORMAT_CENTRE, 100);
162 _list->AppendColumn (_("Format"), wxLIST_FORMAT_CENTRE, 75);
163 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
164 _list->AppendColumn (_("Skippable"), wxLIST_FORMAT_CENTRE, 90);
165 _list->AppendColumn (_("Disable timeline"), wxLIST_FORMAT_CENTRE, 125);
166 _list->AppendColumn (_("Stop after play"), wxLIST_FORMAT_CENTRE, 125);
168 wxImageList* images = new wxImageList (16, 16);
172 tick_icon.LoadFile ("tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
173 no_tick_icon.LoadFile ("no_tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
175 boost::filesystem::path tick_path = shared_path() / "tick.png";
176 tick_icon.LoadFile (std_to_wx(tick_path.string()));
177 boost::filesystem::path no_tick_path = shared_path() / "no_tick.png";
178 no_tick_icon.LoadFile (std_to_wx(no_tick_path.string()));
180 images->Add (tick_icon);
181 images->Add (no_tick_icon);
183 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
185 main_sizer->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
187 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
188 _up = new wxButton (overall_panel, wxID_ANY, _("Up"));
189 _down = new wxButton (overall_panel, wxID_ANY, _("Down"));
190 _add = new wxButton (overall_panel, wxID_ANY, _("Add"));
191 _remove = new wxButton (overall_panel, wxID_ANY, _("Remove"));
192 _save = new wxButton (overall_panel, wxID_ANY, _("Save playlist"));
193 _load = new wxButton (overall_panel, wxID_ANY, _("Load playlist"));
194 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
195 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
196 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
197 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
198 button_sizer->Add (_save, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
199 button_sizer->Add (_load, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
201 main_sizer->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
202 overall_panel->SetSizer (main_sizer);
204 _list->Bind (wxEVT_LEFT_DOWN, bind(&DOMFrame::list_left_click, this, _1));
205 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&DOMFrame::selection_changed, this));
206 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&DOMFrame::selection_changed, this));
207 _up->Bind (wxEVT_BUTTON, bind(&DOMFrame::up_clicked, this));
208 _down->Bind (wxEVT_BUTTON, bind(&DOMFrame::down_clicked, this));
209 _add->Bind (wxEVT_BUTTON, bind(&DOMFrame::add_clicked, this));
210 _remove->Bind (wxEVT_BUTTON, bind(&DOMFrame::remove_clicked, this));
211 _save->Bind (wxEVT_BUTTON, bind(&DOMFrame::save_clicked, this));
212 _load->Bind (wxEVT_BUTTON, bind(&DOMFrame::load_clicked, this));
214 setup_sensitivity ();
219 void add (PlaylistEntry e)
222 item.SetId (_list->GetItemCount());
223 long const N = _list->InsertItem (item);
225 _playlist.push_back (e);
228 void selection_changed ()
230 setup_sensitivity ();
233 void set_item (long N, PlaylistEntry e)
235 _list->SetItem (N, 0, std_to_wx(e.name));
236 _list->SetItem (N, 1, std_to_wx(e.id));
237 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
238 _list->SetItem (N, 3, e.type == PlaylistEntry::DCP ? _("DCP") : _("E-cinema"));
239 _list->SetItem (N, 4, e.encrypted ? _("Y") : _("N"));
240 _list->SetItem (N, COLUMN_SKIPPABLE, wxEmptyString, e.skippable ? 0 : 1);
241 _list->SetItem (N, COLUMN_DISABLE_TIMELINE, wxEmptyString, e.disable_timeline ? 0 : 1);
242 _list->SetItem (N, COLUMN_STOP_AFTER_PLAY, wxEmptyString, e.stop_after_play ? 0 : 1);
245 void setup_sensitivity ()
247 int const num_selected = _list->GetSelectedItemCount ();
248 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
249 _up->Enable (selected > 0);
250 _down->Enable (selected != -1 && selected < (_list->GetItemCount() - 1));
251 _remove->Enable (num_selected > 0);
254 void list_left_click (wxMouseEvent& ev)
257 long item = _list->HitTest (ev.GetPosition(), flags, 0);
258 int x = ev.GetPosition().x;
259 optional<int> column;
260 for (int i = 0; i < _list->GetColumnCount(); ++i) {
261 x -= _list->GetColumnWidth (i);
268 if (item != -1 && column) {
270 case COLUMN_SKIPPABLE:
271 _playlist[item].skippable = !_playlist[item].skippable;
273 case COLUMN_DISABLE_TIMELINE:
274 _playlist[item].disable_timeline = !_playlist[item].disable_timeline;
276 case COLUMN_STOP_AFTER_PLAY:
277 _playlist[item].stop_after_play = !_playlist[item].stop_after_play;
282 set_item (item, _playlist[item]);
290 int const r = _content_dialog->ShowModal ();
292 shared_ptr<Content> content = _content_dialog->selected ();
294 add (PlaylistEntry(content));
301 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
306 PlaylistEntry tmp = _playlist[s];
307 _playlist[s] = _playlist[s-1];
308 _playlist[s-1] = tmp;
310 set_item (s - 1, _playlist[s-1]);
311 set_item (s, _playlist[s]);
316 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
317 if (s > (_list->GetItemCount() - 1)) {
321 PlaylistEntry tmp = _playlist[s];
322 _playlist[s] = _playlist[s+1];
323 _playlist[s+1] = tmp;
325 set_item (s + 1, _playlist[s+1]);
326 set_item (s, _playlist[s]);
329 void remove_clicked ()
331 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
336 _playlist.erase (_playlist.begin() + s);
337 _list->DeleteItem (s);
342 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), wxEmptyString, wxEmptyString, wxT("XML files (*.xml)|*.xml"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
343 if (d->ShowModal() == wxID_OK) {
345 xmlpp::Element* root = doc.create_root_node ("SPL");
346 BOOST_FOREACH (PlaylistEntry i, _playlist) {
347 i.as_xml (root->add_child("Entry"));
349 doc.write_to_file_formatted (wx_to_std(d->GetPath()));
355 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), wxEmptyString, wxEmptyString, wxT("XML files (*.xml)|*.xml"));
356 if (d->ShowModal() == wxID_OK) {
357 _list->DeleteAllItems ();
359 cxml::Document doc ("SPL");
360 doc.read_file (wx_to_std(d->GetPath()));
361 bool missing = false;
362 BOOST_FOREACH (cxml::ConstNodePtr i, doc.node_children("Entry")) {
363 shared_ptr<Content> c = _content_dialog->get(i->string_child("Digest"));
365 add (PlaylistEntry(c, i));
371 error_dialog (this, _("Some content in this playlist was not found."));
383 boost::shared_ptr<Film> _film;
384 std::vector<PlaylistEntry> _playlist;
385 ContentDialog* _content_dialog;
388 COLUMN_SKIPPABLE = 5,
389 COLUMN_DISABLE_TIMELINE = 6,
390 COLUMN_STOP_AFTER_PLAY = 7
395 * @brief The magic App class for wxWidgets.
397 class App : public wxApp
410 SetAppName (_("DCP-o-matic KDM Creator"));
412 if (!wxApp::OnInit()) {
416 #ifdef DCPOMATIC_LINUX
417 unsetenv ("UBUNTU_MENUPROXY");
421 ProcessSerialNumber serial;
422 GetCurrentProcess (&serial);
423 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
426 dcpomatic_setup_path_encoding ();
428 /* Enable i18n; this will create a Config object
429 to look for a force-configured language. This Config
430 object will be wrong, however, because dcpomatic_setup
431 hasn't yet been called and there aren't any filters etc.
434 dcpomatic_setup_i18n ();
436 /* Set things up, including filters etc.
437 which will now be internationalised correctly.
441 /* Force the configuration to be re-loaded correctly next
446 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
447 SetTopWindow (_frame);
451 signal_manager = new wxSignalManager (this);
452 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
458 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
462 /* An unhandled exception has occurred inside the main event loop */
463 bool OnExceptionInMainLoop ()
467 } catch (FileError& e) {
471 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
472 std_to_wx (e.what()),
473 std_to_wx (e.file().string().c_str ())
476 } catch (exception& e) {
480 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
481 std_to_wx (e.what ())
485 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
488 /* This will terminate the program */
492 void OnUnhandledException ()
494 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
499 signal_manager->ui_idle ();