Support buttons.
[dcpomatic.git] / src / wx / kdm_cpl_panel.cc
1 /*
2     Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "kdm_cpl_panel.h"
22 #include "wx_util.h"
23 #include "static_text.h"
24 #include "dcpomatic_button.h"
25 #include <libcxml/cxml.h>
26
27 using std::vector;
28
29 KDMCPLPanel::KDMCPLPanel (wxWindow* parent, vector<CPLSummary> cpls)
30         : wxPanel (parent, wxID_ANY)
31         , _cpls (cpls)
32 {
33         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
34
35         /* CPL choice */
36         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
37         add_label_to_sizer (s, this, _("CPL"), true);
38         _cpl = new wxChoice (this, wxID_ANY);
39         s->Add (_cpl, 1, wxEXPAND);
40         _cpl_browse = new Button (this, _("Browse..."));
41         s->Add (_cpl_browse, 0, wxALIGN_CENTER_VERTICAL);
42         vertical->Add (s, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
43
44         /* CPL details */
45         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
46         add_label_to_sizer (table, this, _("DCP directory"), true);
47         _dcp_directory = new StaticText (this, "");
48         table->Add (_dcp_directory);
49         add_label_to_sizer (table, this, _("CPL ID"), true);
50         _cpl_id = new StaticText (this, "");
51         table->Add (_cpl_id);
52         add_label_to_sizer (table, this, _("CPL annotation text"), true);
53         _cpl_annotation_text = new StaticText (this, "");
54         table->Add (_cpl_annotation_text);
55         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
56
57         update_cpl_choice ();
58
59         _cpl->Bind        (wxEVT_CHOICE, boost::bind (&KDMCPLPanel::update_cpl_summary, this));
60         _cpl_browse->Bind (wxEVT_BUTTON,  boost::bind (&KDMCPLPanel::cpl_browse_clicked, this));
61
62         SetSizerAndFit (vertical);
63 }
64
65 void
66 KDMCPLPanel::update_cpl_choice ()
67 {
68         _cpl->Clear ();
69
70         for (vector<CPLSummary>::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
71                 _cpl->Append (std_to_wx (i->cpl_id));
72
73                 if (_cpls.size() > 0) {
74                         _cpl->SetSelection (0);
75                 }
76         }
77
78         update_cpl_summary ();
79 }
80
81 void
82 KDMCPLPanel::update_cpl_summary ()
83 {
84         int const n = _cpl->GetSelection();
85         if (n == wxNOT_FOUND) {
86                 return;
87         }
88
89         _dcp_directory->SetLabel (std_to_wx (_cpls[n].dcp_directory));
90         _cpl_id->SetLabel (std_to_wx (_cpls[n].cpl_id));
91         _cpl_annotation_text->SetLabel (std_to_wx (_cpls[n].cpl_annotation_text));
92 }
93
94 void
95 KDMCPLPanel::cpl_browse_clicked ()
96 {
97         wxFileDialog* d = new wxFileDialog (this, _("Select CPL XML file"), wxEmptyString, wxEmptyString, "*.xml");
98         if (d->ShowModal() == wxID_CANCEL) {
99                 d->Destroy ();
100                 return;
101         }
102
103         boost::filesystem::path cpl_file (wx_to_std (d->GetPath ()));
104         boost::filesystem::path dcp_dir = cpl_file.parent_path ();
105
106         d->Destroy ();
107
108         /* XXX: hack alert */
109         cxml::Document cpl_document ("CompositionPlaylist");
110         cpl_document.read_file (cpl_file);
111
112         try {
113                 _cpls.push_back (
114                         CPLSummary (
115                                 dcp_dir.filename().string(),
116                                 cpl_document.string_child("Id").substr (9),
117                                 cpl_document.string_child ("ContentTitleText"),
118                                 cpl_file
119                                 )
120                         );
121         } catch (cxml::Error) {
122                 error_dialog (this, _("This is not a valid CPL file"));
123                 return;
124         }
125
126         update_cpl_choice ();
127         _cpl->SetSelection (_cpls.size() - 1);
128         update_cpl_summary ();
129 }
130
131 boost::filesystem::path
132 KDMCPLPanel::cpl () const
133 {
134         int const item = _cpl->GetSelection ();
135         DCPOMATIC_ASSERT (item >= 0);
136         return _cpls[item].cpl_file;
137 }
138
139 bool
140 KDMCPLPanel::has_selected () const
141 {
142         return _cpl->GetSelection() != -1;
143 }