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 | wxCANCEL);
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));
71 _list->Bind(wxEVT_LIST_ITEM_ACTIVATED, boost::bind(&LanguageTagDialog::EndModal, this, wxID_OK));
76 LanguageTagDialog::add_language ()
78 FullLanguageTagDialog full(GetParent());
79 if (full.ShowModal() == wxID_OK) {
80 Config::instance()->add_custom_language(full.get());
87 LanguageTagDialog::populate_list ()
89 _list->DeleteAllItems ();
91 auto add = [this](vector<dcp::LanguageTag> const& tags) {
92 for (auto const& i: tags) {
94 it.SetId (_list->GetItemCount());
96 it.SetText (std_to_wx(i.description()));
97 _list->InsertItem (it);
99 it.SetText (std_to_wx(i.to_string()));
110 LanguageTagDialog::set (dcp::LanguageTag tag)
112 size_t selection = 0;
114 auto iter = find(_presets.begin(), _presets.end(), tag);
115 if (iter == _presets.end()) {
116 iter = find(_custom.begin(), _custom.end(), tag);
117 if (iter == _custom.end()) {
118 _custom.push_back (tag);
119 selection = _presets.size() + _custom.size() - 1;
121 if (_list->GetItemCount() > 0) {
122 _list->EnsureVisible (_list->GetItemCount() - 1);
125 selection = _presets.size() + std::distance(_custom.begin(), iter);
128 selection = std::distance(_presets.begin(), iter);
131 _list->SetItemState (selection, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
132 if (static_cast<int>(selection) < _list->GetItemCount()) {
133 _list->EnsureVisible (selection);
139 LanguageTagDialog::get () const
141 auto selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
142 DCPOMATIC_ASSERT (selected >= 0);
144 if (selected < static_cast<long>(_presets.size())) {
145 return _presets[selected];
148 selected -= _presets.size();
150 DCPOMATIC_ASSERT (selected < static_cast<long>(_custom.size()));
151 return _custom[selected];