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 "../wx/dcpomatic_button.h"
25 #include "../lib/util.h"
26 #include "../lib/config.h"
27 #include "../lib/cross.h"
28 #include "../lib/film.h"
29 #include "../lib/dcp_content.h"
30 #include "../lib/spl_entry.h"
31 #include "../lib/spl.h"
33 #include <wx/listctrl.h>
34 #include <wx/imaglist.h>
39 using boost::optional;
40 using boost::shared_ptr;
41 using boost::weak_ptr;
43 using boost::dynamic_pointer_cast;
45 class ContentDialog : public wxDialog, public ContentStore
48 ContentDialog (wxWindow* parent, weak_ptr<Film> film)
49 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
50 , _content_view (new ContentView(this))
52 _content_view->update ();
54 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
55 SetSizer (overall_sizer);
57 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
59 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
61 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
64 overall_sizer->Layout ();
67 shared_ptr<Content> selected () const
69 return _content_view->selected ();
72 shared_ptr<Content> get (string digest) const
74 return _content_view->get (digest);
78 ContentView* _content_view;
81 class DOMFrame : public wxFrame
84 explicit DOMFrame (wxString const & title)
85 : wxFrame (0, -1, title)
86 /* XXX: this is a bit of a hack, but we need it to be able to use the Content class hierarchy */
87 , _film (new Film(optional<boost::filesystem::path>()))
88 , _content_dialog (new ContentDialog(this, _film))
90 /* Use a panel as the only child of the Frame so that we avoid
91 the dark-grey background on Windows.
93 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
94 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
96 _list = new wxListCtrl (
97 overall_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
100 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
101 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
102 _list->AppendColumn (_("Type"), wxLIST_FORMAT_CENTRE, 100);
103 _list->AppendColumn (_("Format"), wxLIST_FORMAT_CENTRE, 75);
104 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
105 _list->AppendColumn (_("Skippable"), wxLIST_FORMAT_CENTRE, 90);
106 _list->AppendColumn (_("Disable timeline"), wxLIST_FORMAT_CENTRE, 125);
107 _list->AppendColumn (_("Stop after play"), wxLIST_FORMAT_CENTRE, 125);
109 wxImageList* images = new wxImageList (16, 16);
113 tick_icon.LoadFile ("tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
114 no_tick_icon.LoadFile ("no_tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
116 boost::filesystem::path tick_path = shared_path() / "tick.png";
117 tick_icon.LoadFile (std_to_wx(tick_path.string()));
118 boost::filesystem::path no_tick_path = shared_path() / "no_tick.png";
119 no_tick_icon.LoadFile (std_to_wx(no_tick_path.string()));
121 images->Add (tick_icon);
122 images->Add (no_tick_icon);
124 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
126 main_sizer->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
128 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
129 _up = new Button (overall_panel, _("Up"));
130 _down = new Button (overall_panel, _("Down"));
131 _add = new Button (overall_panel, _("Add"));
132 _remove = new Button (overall_panel, _("Remove"));
133 _save = new Button (overall_panel, _("Save playlist"));
134 _load = new Button (overall_panel, _("Load playlist"));
135 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
136 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
137 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
138 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
139 button_sizer->Add (_save, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
140 button_sizer->Add (_load, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
142 main_sizer->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
143 overall_panel->SetSizer (main_sizer);
145 _list->Bind (wxEVT_LEFT_DOWN, bind(&DOMFrame::list_left_click, this, _1));
146 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&DOMFrame::selection_changed, this));
147 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&DOMFrame::selection_changed, this));
148 _up->Bind (wxEVT_BUTTON, bind(&DOMFrame::up_clicked, this));
149 _down->Bind (wxEVT_BUTTON, bind(&DOMFrame::down_clicked, this));
150 _add->Bind (wxEVT_BUTTON, bind(&DOMFrame::add_clicked, this));
151 _remove->Bind (wxEVT_BUTTON, bind(&DOMFrame::remove_clicked, this));
152 _save->Bind (wxEVT_BUTTON, bind(&DOMFrame::save_clicked, this));
153 _load->Bind (wxEVT_BUTTON, bind(&DOMFrame::load_clicked, this));
155 setup_sensitivity ();
160 void add (SPLEntry e)
163 item.SetId (_list->GetItemCount());
164 long const N = _list->InsertItem (item);
169 void selection_changed ()
171 setup_sensitivity ();
174 void set_item (long N, SPLEntry e)
176 _list->SetItem (N, 0, std_to_wx(e.name));
177 _list->SetItem (N, 1, std_to_wx(e.id));
178 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
179 _list->SetItem (N, 3, e.type == SPLEntry::DCP ? _("DCP") : _("E-cinema"));
180 _list->SetItem (N, 4, e.encrypted ? _("Y") : _("N"));
181 _list->SetItem (N, COLUMN_SKIPPABLE, wxEmptyString, e.skippable ? 0 : 1);
182 _list->SetItem (N, COLUMN_DISABLE_TIMELINE, wxEmptyString, e.disable_timeline ? 0 : 1);
183 _list->SetItem (N, COLUMN_STOP_AFTER_PLAY, wxEmptyString, e.stop_after_play ? 0 : 1);
186 void setup_sensitivity ()
188 int const num_selected = _list->GetSelectedItemCount ();
189 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
190 _up->Enable (selected > 0);
191 _down->Enable (selected != -1 && selected < (_list->GetItemCount() - 1));
192 _remove->Enable (num_selected > 0);
195 void list_left_click (wxMouseEvent& ev)
198 long item = _list->HitTest (ev.GetPosition(), flags, 0);
199 int x = ev.GetPosition().x;
200 optional<int> column;
201 for (int i = 0; i < _list->GetColumnCount(); ++i) {
202 x -= _list->GetColumnWidth (i);
209 if (item != -1 && column) {
211 case COLUMN_SKIPPABLE:
212 _playlist[item].skippable = !_playlist[item].skippable;
214 case COLUMN_DISABLE_TIMELINE:
215 _playlist[item].disable_timeline = !_playlist[item].disable_timeline;
217 case COLUMN_STOP_AFTER_PLAY:
218 _playlist[item].stop_after_play = !_playlist[item].stop_after_play;
223 set_item (item, _playlist[item]);
231 int const r = _content_dialog->ShowModal ();
233 shared_ptr<Content> content = _content_dialog->selected ();
235 add (SPLEntry(content));
242 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
247 SPLEntry tmp = _playlist[s];
248 _playlist[s] = _playlist[s-1];
249 _playlist[s-1] = tmp;
251 set_item (s - 1, _playlist[s-1]);
252 set_item (s, _playlist[s]);
257 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
258 if (s > (_list->GetItemCount() - 1)) {
262 SPLEntry tmp = _playlist[s];
263 _playlist[s] = _playlist[s+1];
264 _playlist[s+1] = tmp;
266 set_item (s + 1, _playlist[s+1]);
267 set_item (s, _playlist[s]);
270 void remove_clicked ()
272 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
277 _playlist.remove (s);
278 _list->DeleteItem (s);
283 Config* c = Config::instance ();
284 wxString default_dir = c->player_playlist_directory() ? std_to_wx(c->player_playlist_directory()->string()) : wxString(wxEmptyString);
285 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), default_dir, wxEmptyString, wxT("XML files (*.xml)|*.xml"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
286 if (d->ShowModal() == wxID_OK) {
287 _playlist.write (wx_to_std(d->GetPath()));
293 Config* c = Config::instance ();
294 wxString default_dir = c->player_playlist_directory() ? std_to_wx(c->player_playlist_directory()->string()) : wxString(wxEmptyString);
295 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), default_dir, wxEmptyString, wxT("XML files (*.xml)|*.xml"));
296 if (d->ShowModal() == wxID_OK) {
297 _list->DeleteAllItems ();
298 if (!_playlist.read (wx_to_std(d->GetPath()), _content_dialog)) {
299 BOOST_FOREACH (SPLEntry i, _playlist.get()) {
303 error_dialog (this, _("Some content in this playlist was not found."));
315 boost::shared_ptr<Film> _film;
317 ContentDialog* _content_dialog;
320 COLUMN_SKIPPABLE = 5,
321 COLUMN_DISABLE_TIMELINE = 6,
322 COLUMN_STOP_AFTER_PLAY = 7
327 * @brief The magic App class for wxWidgets.
329 class App : public wxApp
342 SetAppName (_("DCP-o-matic KDM Creator"));
344 if (!wxApp::OnInit()) {
348 #ifdef DCPOMATIC_LINUX
349 unsetenv ("UBUNTU_MENUPROXY");
353 ProcessSerialNumber serial;
354 GetCurrentProcess (&serial);
355 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
358 dcpomatic_setup_path_encoding ();
360 /* Enable i18n; this will create a Config object
361 to look for a force-configured language. This Config
362 object will be wrong, however, because dcpomatic_setup
363 hasn't yet been called and there aren't any filters etc.
366 dcpomatic_setup_i18n ();
368 /* Set things up, including filters etc.
369 which will now be internationalised correctly.
373 /* Force the configuration to be re-loaded correctly next
378 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
379 SetTopWindow (_frame);
383 signal_manager = new wxSignalManager (this);
384 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
390 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
394 /* An unhandled exception has occurred inside the main event loop */
395 bool OnExceptionInMainLoop ()
399 } catch (FileError& e) {
403 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
404 std_to_wx (e.what()),
405 std_to_wx (e.file().string().c_str ())
408 } catch (exception& e) {
412 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
413 std_to_wx (e.what ())
417 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
420 /* This will terminate the program */
424 void OnUnhandledException ()
426 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
431 signal_manager->ui_idle ();