2 Copyright (C) 2020 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/>.
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>
28 #include <boost/algorithm/string.hpp>
29 #include <boost/bind/bind.hpp>
30 #include <boost/optional.hpp>
31 #include <boost/signals2.hpp>
42 using boost::optional;
43 using std::shared_ptr;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
50 class SubtagListCtrl : public wxListCtrl
53 SubtagListCtrl (wxWindow* parent)
54 : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL)
56 AppendColumn ("", wxLIST_FORMAT_LEFT, 80);
57 AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
60 void set (dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
62 _all_subtags = dcp::LanguageTag::get_all(type);
65 vector<dcp::LanguageTag::SubtagData>::iterator i = find(_matching_subtags.begin(), _matching_subtags.end(), *subtag);
66 if (i != _matching_subtags.end()) {
67 long item = std::distance(_matching_subtags.begin(), i);
68 SetItemState (item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
72 if (GetItemCount() > 0) {
73 /* The new list sometimes isn't visible without this */
79 void set_search (string search)
82 _matching_subtags = _all_subtags;
84 _matching_subtags.clear ();
86 boost::algorithm::to_lower(search);
87 for (auto const& i: _all_subtags) {
89 (boost::algorithm::to_lower_copy(i.subtag).find(search) != string::npos) ||
90 (boost::algorithm::to_lower_copy(i.description).find(search) != string::npos)) {
91 _matching_subtags.push_back (i);
96 SetItemCount (_matching_subtags.size());
97 if (GetItemCount() > 0) {
98 RefreshItems (0, GetItemCount() - 1);
102 optional<dcp::LanguageTag::SubtagData> selected_subtag () const
104 long int selected = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
105 if (selected == -1) {
106 return optional<dcp::LanguageTag::SubtagData>();
109 DCPOMATIC_ASSERT (static_cast<size_t>(selected) < _matching_subtags.size());
110 return _matching_subtags[selected];
114 wxString OnGetItemText (long item, long column) const
117 return _matching_subtags[item].subtag;
119 return _matching_subtags[item].description;
123 std::vector<dcp::LanguageTag::SubtagData> _all_subtags;
124 std::vector<dcp::LanguageTag::SubtagData> _matching_subtags;
128 class LanguageSubtagPanel : public wxPanel
131 LanguageSubtagPanel (wxWindow* parent)
132 : wxPanel (parent, wxID_ANY)
135 int const height = 30;
137 int const height = -1;
140 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
141 _list = new SubtagListCtrl (this);
143 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
144 sizer->Add (_search, 0, wxALL, 8);
145 sizer->Add (_list, 1, wxALL, 8);
148 _search->Bind (wxEVT_TEXT, boost::bind(&LanguageSubtagPanel::search_changed, this));
149 _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
150 _list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
153 void set (dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
155 _list->set (type, search, subtag);
156 _search->SetValue (wxString(search));
159 optional<dcp::LanguageTag::RegionSubtag> get () const
161 if (!_list->selected_subtag()) {
162 return optional<dcp::LanguageTag::RegionSubtag>();
165 return dcp::LanguageTag::RegionSubtag(_list->selected_subtag()->subtag);
168 boost::signals2::signal<void (optional<dcp::LanguageTag::SubtagData>)> SelectionChanged;
169 boost::signals2::signal<void (string)> SearchChanged;
172 void search_changed ()
174 _list->set_search (_search->GetValue().ToStdString());
175 SearchChanged (_search->GetValue().ToStdString());
178 void selection_changed ()
180 SelectionChanged (_list->selected_subtag());
183 wxSearchCtrl* _search;
184 SubtagListCtrl* _list;
188 LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
189 : wxDialog (parent, wxID_ANY, "Language Tag", wxDefaultPosition, wxSize(-1, 500))
191 _current_tag_list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
192 _current_tag_list->AppendColumn ("", wxLIST_FORMAT_LEFT, 200);
193 _current_tag_list->AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
195 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
196 _add_script = new wxButton(this, wxID_ANY, "Add script");
197 button_sizer->Add (_add_script, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
198 _add_region = new wxButton(this, wxID_ANY, "Add region");
199 button_sizer->Add (_add_region, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
200 _add_variant = new wxButton(this, wxID_ANY, "Add variant");
201 button_sizer->Add (_add_variant, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
202 _add_external = new wxButton(this, wxID_ANY, "Add external");
203 button_sizer->Add (_add_external, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
204 _remove = new wxButton(this, wxID_ANY, "Remove");
205 button_sizer->Add (_remove, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
207 _choose_subtag_panel = new LanguageSubtagPanel (this);
208 _choose_subtag_panel->set (dcp::LanguageTag::LANGUAGE, "");
210 wxBoxSizer* ltor_sizer = new wxBoxSizer (wxHORIZONTAL);
211 ltor_sizer->Add (_current_tag_list, 1, wxALL, 8);
212 ltor_sizer->Add (button_sizer, 0, wxALL, 8);
213 ltor_sizer->Add (_choose_subtag_panel, 1, wxALL, 8);
215 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
216 overall_sizer->Add (ltor_sizer, 0);
218 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
220 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
223 SetSizerAndFit (overall_sizer);
227 _add_script->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::SCRIPT, boost::optional<dcp::LanguageTag::SubtagData>()));
228 _add_region->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::REGION, boost::optional<dcp::LanguageTag::SubtagData>()));
229 _add_variant->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::VARIANT, boost::optional<dcp::LanguageTag::SubtagData>()));
230 _add_external->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_to_current_tag, this, dcp::LanguageTag::EXTLANG, boost::optional<dcp::LanguageTag::SubtagData>()));
231 _remove->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::remove_from_current_tag, this));
232 _choose_subtag_panel->SelectionChanged.connect(bind(&LanguageTagDialog::chosen_subtag_changed, this, _1));
233 _choose_subtag_panel->SearchChanged.connect(bind(&LanguageTagDialog::search_changed, this, _1));
234 _current_tag_list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageTagDialog::current_tag_selection_changed, this));
235 _current_tag_list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageTagDialog::current_tag_selection_changed, this));
240 LanguageTagDialog::remove_from_current_tag ()
242 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
247 _current_tag_subtags.erase (_current_tag_subtags.begin() + selected);
248 _current_tag_list->DeleteItem (selected);
250 _current_tag_list->SetItemState (min(selected, _current_tag_list->GetItemCount() - 1L), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
252 setup_sensitivity ();
253 current_tag_selection_changed ();
257 dcp::LanguageTag LanguageTagDialog::get () const
259 dcp::LanguageTag tag;
261 vector<dcp::LanguageTag::VariantSubtag> variants;
262 vector<dcp::LanguageTag::ExtlangSubtag> extlangs;
264 for (auto i: _current_tag_subtags) {
269 case dcp::LanguageTag::LANGUAGE:
270 tag.set_language (i.subtag->subtag);
272 case dcp::LanguageTag::SCRIPT:
273 tag.set_script (i.subtag->subtag);
275 case dcp::LanguageTag::REGION:
276 tag.set_region (i.subtag->subtag);
278 case dcp::LanguageTag::VARIANT:
279 variants.push_back (i.subtag->subtag);
281 case dcp::LanguageTag::EXTLANG:
282 extlangs.push_back (i.subtag->subtag);
287 tag.set_variants (variants);
288 tag.set_extlangs (extlangs);
294 LanguageTagDialog::set (dcp::LanguageTag tag)
296 _current_tag_subtags.clear ();
297 _current_tag_list->DeleteAllItems ();
299 bool have_language = false;
300 vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> > subtags = tag.subtags();
301 for (vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> >::const_iterator i = subtags.begin(); i != subtags.end(); ++i) {
302 add_to_current_tag (i->first, i->second);
303 if (i->first == dcp::LanguageTag::LANGUAGE) {
304 have_language = true;
308 if (!have_language) {
309 add_to_current_tag (dcp::LanguageTag::LANGUAGE, dcp::LanguageTag::SubtagData("en", "English"));
314 string LanguageTagDialog::subtag_type_name (dcp::LanguageTag::SubtagType type)
317 case dcp::LanguageTag::LANGUAGE:
319 case dcp::LanguageTag::SCRIPT:
321 case dcp::LanguageTag::REGION:
323 case dcp::LanguageTag::VARIANT:
325 case dcp::LanguageTag::EXTLANG:
334 LanguageTagDialog::search_changed (string search)
336 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
338 _current_tag_subtags[selected].last_search = search;
344 LanguageTagDialog::add_to_current_tag (dcp::LanguageTag::SubtagType type, optional<dcp::LanguageTag::SubtagData> subtag)
346 _current_tag_subtags.push_back (Subtag(type, subtag));
348 it.SetId (_current_tag_list->GetItemCount());
350 it.SetText (subtag_type_name(type));
351 _current_tag_list->InsertItem (it);
354 it.SetText (subtag->description);
356 it.SetText ("Select...");
358 _current_tag_list->SetItem (it);
359 _current_tag_list->SetItemState (_current_tag_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
360 _choose_subtag_panel->set (type, "");
361 setup_sensitivity ();
362 current_tag_selection_changed ();
367 LanguageTagDialog::current_tag_selection_changed ()
369 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
371 _choose_subtag_panel->Enable (true);
372 _choose_subtag_panel->set (_current_tag_subtags[selected].type, _current_tag_subtags[selected].last_search, _current_tag_subtags[selected].subtag);
374 _choose_subtag_panel->Enable (false);
380 LanguageTagDialog::chosen_subtag_changed (optional<dcp::LanguageTag::SubtagData> selection)
386 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
388 _current_tag_subtags[selected].subtag = *selection;
389 _current_tag_list->SetItem (selected, 0, subtag_type_name(_current_tag_subtags[selected].type));
390 _current_tag_list->SetItem (selected, 1, selection->description);
393 setup_sensitivity ();
397 LanguageTagDialog::setup_sensitivity ()
399 _add_script->Enable ();
400 _add_region->Enable ();
401 _add_variant->Enable ();
402 _add_external->Enable ();
403 for (auto const& i: _current_tag_subtags) {
405 case dcp::LanguageTag::SCRIPT:
406 _add_script->Enable (false);
408 case dcp::LanguageTag::REGION:
409 _add_region->Enable (false);
411 case dcp::LanguageTag::VARIANT:
412 _add_variant->Enable (false);
414 case dcp::LanguageTag::EXTLANG:
415 _add_external->Enable (false);
421 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
422 _remove->Enable (selected > 0);
426 RegionSubtagDialog::RegionSubtagDialog (wxWindow* parent, dcp::LanguageTag::RegionSubtag region)
427 : wxDialog (parent, wxID_ANY, _("Region"), wxDefaultPosition, wxSize(-1, 500))
428 , _panel (new LanguageSubtagPanel (this))
430 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
431 sizer->Add (_panel, 1);
433 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
435 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
440 _panel->set (dcp::LanguageTag::REGION, "", *dcp::LanguageTag::get_subtag_data(region));
444 optional<dcp::LanguageTag::RegionSubtag>
445 RegionSubtagDialog::get () const
447 return _panel->get ();