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