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.hpp>
30 #include <boost/foreach.hpp>
31 #include <boost/optional.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/signals2.hpp>
44 using boost::optional;
45 using boost::shared_ptr;
46 using boost::weak_ptr;
47 #if BOOST_VERSION >= 106100
48 using namespace boost::placeholders;
52 class SubtagListCtrl : public wxListCtrl
55 SubtagListCtrl (wxWindow* parent)
56 : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL)
58 AppendColumn ("", wxLIST_FORMAT_LEFT, 80);
59 AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
62 void set (dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
64 _all_subtags = dcp::LanguageTag::get_all(type);
67 vector<dcp::LanguageTag::SubtagData>::iterator i = find(_matching_subtags.begin(), _matching_subtags.end(), *subtag);
68 if (i != _matching_subtags.end()) {
69 long item = std::distance(_matching_subtags.begin(), i);
70 SetItemState (item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
74 if (GetItemCount() > 0) {
75 /* The new list sometimes isn't visible without this */
81 void set_search (string search)
84 _matching_subtags = _all_subtags;
86 _matching_subtags.clear ();
88 boost::algorithm::to_lower(search);
89 BOOST_FOREACH (dcp::LanguageTag::SubtagData const& i, _all_subtags) {
91 (boost::algorithm::to_lower_copy(i.subtag).find(search) != string::npos) ||
92 (boost::algorithm::to_lower_copy(i.description).find(search) != string::npos)) {
93 _matching_subtags.push_back (i);
98 SetItemCount (_matching_subtags.size());
99 if (GetItemCount() > 0) {
100 RefreshItems (0, GetItemCount() - 1);
104 optional<dcp::LanguageTag::SubtagData> selected_subtag () const
106 long int selected = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
107 if (selected == -1) {
108 return optional<dcp::LanguageTag::SubtagData>();
111 DCPOMATIC_ASSERT (static_cast<size_t>(selected) < _matching_subtags.size());
112 return _matching_subtags[selected];
116 wxString OnGetItemText (long item, long column) const
119 return _matching_subtags[item].subtag;
121 return _matching_subtags[item].description;
125 std::vector<dcp::LanguageTag::SubtagData> _all_subtags;
126 std::vector<dcp::LanguageTag::SubtagData> _matching_subtags;
130 class LanguageSubtagPanel : public wxPanel
133 LanguageSubtagPanel (wxWindow* parent)
134 : wxPanel (parent, wxID_ANY)
137 int const height = 30;
139 int const height = -1;
142 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
143 _list = new SubtagListCtrl (this);
145 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
146 sizer->Add (_search, 0, wxALL, 8);
147 sizer->Add (_list, 1, wxALL, 8);
150 _search->Bind (wxEVT_TEXT, boost::bind(&LanguageSubtagPanel::search_changed, this));
151 _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
152 _list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
155 void set (dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
157 _list->set (type, search, subtag);
158 _search->SetValue (wxString(search));
161 optional<dcp::LanguageTag::RegionSubtag> get () const
163 if (!_list->selected_subtag()) {
164 return optional<dcp::LanguageTag::RegionSubtag>();
167 return dcp::LanguageTag::RegionSubtag(_list->selected_subtag()->subtag);
170 boost::signals2::signal<void (optional<dcp::LanguageTag::SubtagData>)> SelectionChanged;
171 boost::signals2::signal<void (string)> SearchChanged;
174 void search_changed ()
176 _list->set_search (_search->GetValue().ToStdString());
177 SearchChanged (_search->GetValue().ToStdString());
180 void selection_changed ()
182 SelectionChanged (_list->selected_subtag());
185 wxSearchCtrl* _search;
186 SubtagListCtrl* _list;
190 LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
191 : wxDialog (parent, wxID_ANY, "Language Tag", wxDefaultPosition, wxSize(-1, 500))
193 _current_tag_list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
194 _current_tag_list->AppendColumn ("", wxLIST_FORMAT_LEFT, 200);
195 _current_tag_list->AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
197 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
198 _add_script = new wxButton(this, wxID_ANY, "Add script");
199 button_sizer->Add (_add_script, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
200 _add_region = new wxButton(this, wxID_ANY, "Add region");
201 button_sizer->Add (_add_region, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
202 _add_variant = new wxButton(this, wxID_ANY, "Add variant");
203 button_sizer->Add (_add_variant, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
204 _add_external = new wxButton(this, wxID_ANY, "Add external");
205 button_sizer->Add (_add_external, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
206 _remove = new wxButton(this, wxID_ANY, "Remove");
207 button_sizer->Add (_remove, 0, wxTOP | wxBOTTOM | wxEXPAND, 2);
209 _choose_subtag_panel = new LanguageSubtagPanel (this);
210 _choose_subtag_panel->set (dcp::LanguageTag::LANGUAGE, "");
212 wxBoxSizer* ltor_sizer = new wxBoxSizer (wxHORIZONTAL);
213 ltor_sizer->Add (_current_tag_list, 1, wxALL, 8);
214 ltor_sizer->Add (button_sizer, 0, wxALL, 8);
215 ltor_sizer->Add (_choose_subtag_panel, 1, wxALL, 8);
217 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
218 overall_sizer->Add (ltor_sizer, 0);
220 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
222 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
225 SetSizerAndFit (overall_sizer);
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 _remove->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::remove_from_current_tag, this));
234 _choose_subtag_panel->SelectionChanged.connect(bind(&LanguageTagDialog::chosen_subtag_changed, this, _1));
235 _choose_subtag_panel->SearchChanged.connect(bind(&LanguageTagDialog::search_changed, this, _1));
236 _current_tag_list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageTagDialog::current_tag_selection_changed, this));
237 _current_tag_list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageTagDialog::current_tag_selection_changed, this));
242 LanguageTagDialog::remove_from_current_tag ()
244 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
249 _current_tag_subtags.erase (_current_tag_subtags.begin() + selected);
250 _current_tag_list->DeleteItem (selected);
252 _current_tag_list->SetItemState (min(selected, _current_tag_list->GetItemCount() - 1L), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
254 setup_sensitivity ();
255 current_tag_selection_changed ();
259 dcp::LanguageTag LanguageTagDialog::get () const
261 dcp::LanguageTag tag;
263 vector<dcp::LanguageTag::VariantSubtag> variants;
264 vector<dcp::LanguageTag::ExtlangSubtag> extlangs;
266 BOOST_FOREACH (Subtag i, _current_tag_subtags) {
271 case dcp::LanguageTag::LANGUAGE:
272 tag.set_language (i.subtag->subtag);
274 case dcp::LanguageTag::SCRIPT:
275 tag.set_script (i.subtag->subtag);
277 case dcp::LanguageTag::REGION:
278 tag.set_region (i.subtag->subtag);
280 case dcp::LanguageTag::VARIANT:
281 variants.push_back (i.subtag->subtag);
283 case dcp::LanguageTag::EXTLANG:
284 extlangs.push_back (i.subtag->subtag);
289 tag.set_variants (variants);
290 tag.set_extlangs (extlangs);
296 LanguageTagDialog::set (dcp::LanguageTag tag)
298 _current_tag_subtags.clear ();
299 _current_tag_list->DeleteAllItems ();
301 bool have_language = false;
302 vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> > subtags = tag.subtags();
303 for (vector<pair<dcp::LanguageTag::SubtagType, dcp::LanguageTag::SubtagData> >::const_iterator i = subtags.begin(); i != subtags.end(); ++i) {
304 add_to_current_tag (i->first, i->second);
305 if (i->first == dcp::LanguageTag::LANGUAGE) {
306 have_language = true;
310 if (!have_language) {
311 add_to_current_tag (dcp::LanguageTag::LANGUAGE, dcp::LanguageTag::SubtagData("en", "English"));
316 string LanguageTagDialog::subtag_type_name (dcp::LanguageTag::SubtagType type)
319 case dcp::LanguageTag::LANGUAGE:
321 case dcp::LanguageTag::SCRIPT:
323 case dcp::LanguageTag::REGION:
325 case dcp::LanguageTag::VARIANT:
327 case dcp::LanguageTag::EXTLANG:
336 LanguageTagDialog::search_changed (string search)
338 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
340 _current_tag_subtags[selected].last_search = search;
346 LanguageTagDialog::add_to_current_tag (dcp::LanguageTag::SubtagType type, optional<dcp::LanguageTag::SubtagData> subtag)
348 _current_tag_subtags.push_back (Subtag(type, subtag));
350 it.SetId (_current_tag_list->GetItemCount());
352 it.SetText (subtag_type_name(type));
353 _current_tag_list->InsertItem (it);
356 it.SetText (subtag->description);
358 it.SetText ("Select...");
360 _current_tag_list->SetItem (it);
361 _current_tag_list->SetItemState (_current_tag_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
362 _choose_subtag_panel->set (type, "");
363 setup_sensitivity ();
364 current_tag_selection_changed ();
369 LanguageTagDialog::current_tag_selection_changed ()
371 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
373 _choose_subtag_panel->Enable (true);
374 _choose_subtag_panel->set (_current_tag_subtags[selected].type, _current_tag_subtags[selected].last_search, _current_tag_subtags[selected].subtag);
376 _choose_subtag_panel->Enable (false);
382 LanguageTagDialog::chosen_subtag_changed (optional<dcp::LanguageTag::SubtagData> selection)
388 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
390 _current_tag_subtags[selected].subtag = *selection;
391 _current_tag_list->SetItem (selected, 0, subtag_type_name(_current_tag_subtags[selected].type));
392 _current_tag_list->SetItem (selected, 1, selection->description);
395 setup_sensitivity ();
399 LanguageTagDialog::setup_sensitivity ()
401 _add_script->Enable ();
402 _add_region->Enable ();
403 _add_variant->Enable ();
404 _add_external->Enable ();
405 BOOST_FOREACH (Subtag const& i, _current_tag_subtags) {
407 case dcp::LanguageTag::SCRIPT:
408 _add_script->Enable (false);
410 case dcp::LanguageTag::REGION:
411 _add_region->Enable (false);
413 case dcp::LanguageTag::VARIANT:
414 _add_variant->Enable (false);
416 case dcp::LanguageTag::EXTLANG:
417 _add_external->Enable (false);
423 long int selected = _current_tag_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
424 _remove->Enable (selected > 0);
428 RegionSubtagDialog::RegionSubtagDialog (wxWindow* parent, dcp::LanguageTag::RegionSubtag region)
429 : wxDialog (parent, wxID_ANY, _("Region"), wxDefaultPosition, wxSize(-1, 500))
430 , _panel (new LanguageSubtagPanel (this))
432 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
433 sizer->Add (_panel, 1);
435 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
437 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
442 _panel->set (dcp::LanguageTag::REGION, "", *dcp::LanguageTag::get_subtag_data(region));
446 optional<dcp::LanguageTag::RegionSubtag>
447 RegionSubtagDialog::get () const
449 return _panel->get ();