2 Copyright (C) 2021 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 "full_language_tag_dialog.h"
23 #include "language_tag_dialog.h"
25 #include "lib/config.h"
26 #include <dcp/language_tag.h>
27 #include <dcp/warnings.h>
28 LIBDCP_DISABLE_WARNINGS
29 #include <wx/listctrl.h>
31 LIBDCP_ENABLE_WARNINGS
37 LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
38 : wxDialog (parent, wxID_ANY, _("Language Tag"))
40 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(600, 700), wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
41 _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
42 _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 150);
43 auto add = new wxButton (this, wxID_ANY, _("Add language..."));
45 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
46 overall_sizer->Add (_list, 0, wxALL, DCPOMATIC_SIZER_GAP);
47 overall_sizer->Add (add, 0, wxALL, DCPOMATIC_SIZER_GAP);
49 auto buttons = CreateSeparatedButtonSizer (wxOK);
51 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
54 SetSizerAndFit (overall_sizer);
56 for (auto const& i: dcp::dcnc_tags()) {
57 _presets.push_back (dcp::LanguageTag(i.first));
60 std::sort (_presets.begin(), _presets.end(), [](dcp::LanguageTag const& i, dcp::LanguageTag const& j) {
61 return i.description() < j.description();
64 _custom = Config::instance()->custom_languages ();
70 add->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_language, this));
75 LanguageTagDialog::add_language ()
77 auto full = new FullLanguageTagDialog (GetParent());
78 auto r = full->ShowModal ();
80 Config::instance()->add_custom_language (full->get());
88 LanguageTagDialog::populate_list ()
90 _list->DeleteAllItems ();
92 auto add = [this](vector<dcp::LanguageTag> const& tags) {
93 for (auto const& i: tags) {
95 it.SetId (_list->GetItemCount());
97 it.SetText (std_to_wx(i.description()));
98 _list->InsertItem (it);
100 it.SetText (std_to_wx(i.to_string()));
111 LanguageTagDialog::set (dcp::LanguageTag tag)
113 size_t selection = 0;
115 auto iter = find(_presets.begin(), _presets.end(), tag);
116 if (iter == _presets.end()) {
117 iter = find(_custom.begin(), _custom.end(), tag);
118 if (iter == _custom.end()) {
119 _custom.push_back (tag);
120 selection = _presets.size() + _custom.size() - 1;
122 if (_list->GetItemCount() > 0) {
123 _list->EnsureVisible (_list->GetItemCount() - 1);
126 selection = _presets.size() + std::distance(_custom.begin(), iter);
129 selection = std::distance(_presets.begin(), iter);
132 _list->SetItemState (selection, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
133 if (static_cast<int>(selection) < _list->GetItemCount()) {
134 _list->EnsureVisible (selection);
140 LanguageTagDialog::get () const
142 auto selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
143 DCPOMATIC_ASSERT (selected >= 0);
145 if (selected < static_cast<long>(_presets.size())) {
146 return _presets[selected];
149 selected -= _presets.size();
151 DCPOMATIC_ASSERT (selected < static_cast<long>(_custom.size()));
152 return _custom[selected];