Support CPL metadata.
[dcpomatic.git] / src / wx / language_tag_dialog.cc
1 /*
2     Copyright (C) 2020 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
22 #include "lib/dcpomatic_assert.h"
23 #include "language_tag_dialog.h"
24 #include <dcp/language_tag.h>
25 #include <wx/listctrl.h>
26 #include <wx/srchctrl.h>
27 #include <wx/wx.h>
28 #include <boost/algorithm/string.hpp>
29 #include <boost/bind.hpp>
30 #include <boost/foreach.hpp>
31 #include <boost/optional.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/signals2.hpp>
34 #include <iostream>
35 #include <iterator>
36 #include <string>
37 #include <vector>
38
39
40 using std::pair;
41 using std::string;
42 using std::vector;
43 using boost::optional;
44 using boost::shared_ptr;
45 using boost::weak_ptr;
46
47
48 class SubtagListCtrl : public wxListCtrl
49 {
50 public:
51         SubtagListCtrl (wxWindow* parent)
52                 : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL)
53         {
54                 AppendColumn ("", wxLIST_FORMAT_LEFT, 80);
55                 AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
56         }
57
58         void set (dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
59         {
60                 _all_subtags = dcp::LanguageTag::get_all(type);
61                 set_search (search);
62                 if (subtag) {
63                         vector<dcp::LanguageTag::SubtagData>::iterator i = find(_matching_subtags.begin(), _matching_subtags.end(), *subtag);
64                         if (i != _matching_subtags.end()) {
65                                 long item = std::distance(_matching_subtags.begin(), i);
66                                 SetItemState (item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
67                                 EnsureVisible (item);
68                         }
69                 }
70         }
71
72         void set_search (string search)
73         {
74                 if (search == "") {
75                         _matching_subtags = _all_subtags;
76                 } else {
77                         _matching_subtags.clear ();
78
79                         boost::algorithm::to_lower(search);
80                         BOOST_FOREACH (dcp::LanguageTag::SubtagData const& i, _all_subtags) {
81                                 if (
82                                         (boost::algorithm::to_lower_copy(i.subtag).find(search) != string::npos) ||
83                                         (boost::algorithm::to_lower_copy(i.description).find(search) != string::npos)) {
84                                         _matching_subtags.push_back (i);
85                                 }
86                         }
87                 }
88
89                 SetItemCount (_matching_subtags.size());
90                 if (GetItemCount() > 0) {
91                         RefreshItems (0, GetItemCount() - 1);
92                 }
93         }
94
95         optional<dcp::LanguageTag::SubtagData> selected_subtag () const
96         {
97                 long int selected = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
98                 if (selected == -1) {
99                         return optional<dcp::LanguageTag::SubtagData>();
100                 }
101
102                 DCPOMATIC_ASSERT (static_cast<size_t>(selected) < _matching_subtags.size());
103                 return _matching_subtags[selected];
104         }
105
106 private:
107         wxString OnGetItemText (long item, long column) const
108         {
109                 if (column == 0) {
110                         return _matching_subtags[item].subtag;
111                 } else {
112                         return _matching_subtags[item].description;
113                 }
114         }
115
116         std::vector<dcp::LanguageTag::SubtagData> _all_subtags;
117         std::vector<dcp::LanguageTag::SubtagData> _matching_subtags;
118 };
119
120
121 class LanguageSubtagPanel : public wxPanel
122 {
123 public:
124         LanguageSubtagPanel (wxWindow* parent)
125                 : wxPanel (parent, wxID_ANY)
126         {
127 #ifdef __WXGTK3__
128                 int const height = 30;
129 #else
130                 int const height = -1;
131 #endif
132
133                 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
134                 _list = new SubtagListCtrl (this);
135
136                 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
137                 sizer->Add (_search, 0, wxALL, 8);
138                 sizer->Add (_list, 1, wxALL, 8);
139                 SetSizer (sizer);
140
141                 _search->Bind (wxEVT_TEXT, boost::bind(&LanguageSubtagPanel::search_changed, this));
142                 _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
143                 _list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
144         }
145
146         void set (dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
147         {
148                 _list->set (type, search, subtag);
149                 _search->SetValue (wxString(search));
150         }
151
152         optional<dcp::LanguageTag::RegionSubtag> get () const
153         {
154                 if (!_list->selected_subtag()) {
155                         return optional<dcp::LanguageTag::RegionSubtag>();
156                 }
157
158                 return dcp::LanguageTag::RegionSubtag(_list->selected_subtag()->subtag);
159         }
160
161         boost::signals2::signal<void (optional<dcp::LanguageTag::SubtagData>)> SelectionChanged;
162         boost::signals2::signal<void (string)> SearchChanged;
163
164 private:
165         void search_changed ()
166         {
167                 _list->set_search (_search->GetValue().ToStdString());
168                 SearchChanged (_search->GetValue().ToStdString());
169         }
170
171         void selection_changed ()
172         {
173                 SelectionChanged (_list->selected_subtag());
174         }
175
176         wxSearchCtrl* _search;
177         SubtagListCtrl* _list;
178 };
179
180
181 LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
182         : wxDialog (parent, wxID_ANY, "Language Tag", wxDefaultPosition, wxSize(-1, 500))
183 {
184         _current_tag_list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
185         _current_tag_list->AppendColumn ("", wxLIST_FORMAT_LEFT, 200);
186         _current_tag_list->AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
187
188         wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
189         _add_script = new wxButton(this, wxID_ANY, "Add script");
190         button_sizer->Add (_add_script, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
191         _add_region = new wxButton(this, wxID_ANY, "Add region");
192         button_sizer->Add (_add_region, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
193         _add_variant = new wxButton(this, wxID_ANY, "Add variant");
194         button_sizer->Add (_add_variant, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
195         _add_external = new wxButton(this, wxID_ANY, "Add external");
196         button_sizer->Add (_add_external, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
197
198         _choose_subtag_panel = new LanguageSubtagPanel (this);
199         _choose_subtag_panel->set (dcp::LanguageTag::LANGUAGE, "");
200
201         wxBoxSizer* ltor_sizer = new wxBoxSizer (wxHORIZONTAL);
202         ltor_sizer->Add (_current_tag_list, 1, wxALL, 8);
203         ltor_sizer->Add (button_sizer, 0, wxALL, 8);
204         ltor_sizer->Add (_choose_subtag_panel, 1, wxALL, 8);
205
206         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
207         overall_sizer->Add (ltor_sizer, 0);
208
209         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
210         if (buttons) {
211                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
212         }
213
214         SetSizerAndFit (overall_sizer);
215
216         bool have_language = false;
217         vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> > subtags = tag.subtags();
218         for (vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> >::const_iterator i = subtags.begin(); i != subtags.end(); ++i) {
219                 add_to_current_tag (i->first, i->second);
220                 if (i->first == dcp::LanguageTag::LANGUAGE) {
221                         have_language = true;
222                 }
223         }
224
225         if (!have_language) {
226                 add_to_current_tag (dcp::LanguageTag::LANGUAGE, dcp::LanguageTag::SubtagData("en", "English"));
227         }
228
229         _add_script->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::SCRIPT, boost::optional<dcp::LanguageTag::SubtagData>()));
230         _add_region->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::REGION, boost::optional<dcp::LanguageTag::SubtagData>()));
231         _add_variant->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::VARIANT, boost::optional<dcp::LanguageTag::SubtagData>()));
232         _add_external->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::EXTLANG, boost::optional<dcp::LanguageTag::SubtagData>()));
233         _choose_subtag_panel->SelectionChanged.connect(bind(&LanguageTagDialog::chosen_subtag_changed, this, _1));
234         _choose_subtag_panel->SearchChanged.connect(bind(&LanguageTagDialog::search_changed, this, _1));
235         _current_tag_list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageTagDialog::current_tag_selection_changed, this));
236         _current_tag_list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageTagDialog::current_tag_selection_changed, this));
237 }
238
239
240 dcp::LanguageTag LanguageTagDialog::get () const
241 {
242         dcp::LanguageTag tag;
243
244         vector<dcp::LanguageTag::VariantSubtag> variants;
245         vector<dcp::LanguageTag::ExtlangSubtag> extlangs;
246
247         BOOST_FOREACH (Subtag i, _current_tag_subtags) {
248                 if (!i.subtag) {
249                         continue;
250                 }
251                 switch (i.type) {
252                         case dcp::LanguageTag::LANGUAGE:
253                                 tag.set_language (i.subtag->subtag);
254                                 break;
255                         case dcp::LanguageTag::SCRIPT:
256                                 tag.set_script (i.subtag->subtag);
257                                 break;
258                         case dcp::LanguageTag::REGION:
259                                 tag.set_region (i.subtag->subtag);
260                                 break;
261                         case dcp::LanguageTag::VARIANT:
262                                 variants.push_back (i.subtag->subtag);
263                                 break;
264                         case dcp::LanguageTag::EXTLANG:
265                                 extlangs.push_back (i.subtag->subtag);
266                                 break;
267                 }
268         }
269
270         tag.set_variants (variants);
271         tag.set_extlangs (extlangs);
272         return tag;
273 }
274
275
276 string LanguageTagDialog::subtag_type_name (dcp::LanguageTag::SubtagType type)
277 {
278         switch (type) {
279                 case dcp::LanguageTag::LANGUAGE:
280                         return "Language";
281                 case dcp::LanguageTag::SCRIPT:
282                         return "Script";
283                 case dcp::LanguageTag::REGION:
284                         return "Region";
285                 case dcp::LanguageTag::VARIANT:
286                         return "Variant";
287                 case dcp::LanguageTag::EXTLANG:
288                         return "External";
289         }
290
291         return "";
292 }
293
294
295 void
296 LanguageTagDialog::search_changed (string search)
297 {
298         long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
299         if (selected >= 0) {
300                 _current_tag_subtags[selected].last_search = search;
301         }
302 }
303
304
305 void
306 LanguageTagDialog::add_to_current_tag (dcp::LanguageTag::SubtagType type, optional<dcp::LanguageTag::SubtagData> subtag)
307 {
308         _current_tag_subtags.push_back (Subtag(type, subtag));
309         wxListItem it;
310         it.SetId (_current_tag_list->GetItemCount());
311         it.SetColumn (0);
312         it.SetText (subtag_type_name(type));
313         _current_tag_list->InsertItem (it);
314         it.SetColumn (1);
315         if (subtag) {
316                 it.SetText (subtag->description);
317         } else {
318                 it.SetText ("Select...");
319         }
320         _current_tag_list->SetItem (it);
321         _current_tag_list->SetItemState (_current_tag_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
322         _choose_subtag_panel->set (type, "");
323         setup_sensitivity ();
324         current_tag_selection_changed ();
325 }
326
327
328 void
329 LanguageTagDialog::current_tag_selection_changed ()
330 {
331         long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
332         if (selected >= 0) {
333                 _choose_subtag_panel->Enable (true);
334                 _choose_subtag_panel->set (_current_tag_subtags[selected].type, _current_tag_subtags[selected].last_search, _current_tag_subtags[selected].subtag);
335         } else {
336                 _choose_subtag_panel->Enable (false);
337         }
338 }
339
340
341 void
342 LanguageTagDialog::chosen_subtag_changed (optional<dcp::LanguageTag::SubtagData> selection)
343 {
344         if (!selection) {
345                 return;
346         }
347
348         long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
349         if (selected >= 0) {
350                 _current_tag_subtags[selected].subtag = *selection;
351                 _current_tag_list->SetItem (selected, 0, subtag_type_name(_current_tag_subtags[selected].type));
352                 _current_tag_list->SetItem (selected, 1, selection->description);
353         }
354 }
355
356 void
357 LanguageTagDialog::setup_sensitivity ()
358 {
359         _add_script->Enable ();
360         _add_region->Enable ();
361         _add_variant->Enable ();
362         _add_external->Enable ();
363         BOOST_FOREACH (Subtag const& i, _current_tag_subtags) {
364                 switch (i.type) {
365                         case dcp::LanguageTag::SCRIPT:
366                                 _add_script->Enable (false);
367                                 break;
368                         case dcp::LanguageTag::REGION:
369                                 _add_region->Enable (false);
370                                 break;
371                         case dcp::LanguageTag::VARIANT:
372                                 _add_variant->Enable (false);
373                                 break;
374                         case dcp::LanguageTag::EXTLANG:
375                                 _add_external->Enable (false);
376                                 break;
377                         default:
378                                 break;
379                 }
380         }
381 }
382
383
384 RegionSubtagDialog::RegionSubtagDialog (wxWindow* parent, dcp::LanguageTag::RegionSubtag region)
385         : wxDialog (parent, wxID_ANY, _("Region"), wxDefaultPosition, wxSize(-1, 500))
386         , _panel (new LanguageSubtagPanel (this))
387 {
388         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
389         sizer->Add (_panel, 1);
390
391         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
392         if (buttons) {
393                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
394         }
395
396         SetSizer (sizer);
397
398         _panel->set (dcp::LanguageTag::REGION, "", *dcp::LanguageTag::get_subtag_data(region));
399 }
400
401
402 optional<dcp::LanguageTag::RegionSubtag>
403 RegionSubtagDialog::get () const
404 {
405         return _panel->get ();
406 }
407
408