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 <wx/listctrl.h>
28 #include <dcp/language_tag.h>
34 LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
35 : wxDialog (parent, wxID_ANY, _("Language Tag"))
37 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(600, 700), wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
38 _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
39 _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 150);
40 auto add = new wxButton (this, wxID_ANY, _("Add language..."));
42 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
43 overall_sizer->Add (_list, 0, wxALL, DCPOMATIC_SIZER_GAP);
44 overall_sizer->Add (add, 0, wxALL, DCPOMATIC_SIZER_GAP);
46 auto buttons = CreateSeparatedButtonSizer (wxOK);
48 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
51 SetSizerAndFit (overall_sizer);
53 for (auto const& i: dcp::dcnc_tags()) {
54 _presets.push_back (dcp::LanguageTag(i.first));
57 std::sort (_presets.begin(), _presets.end(), [](dcp::LanguageTag const& i, dcp::LanguageTag const& j) {
58 return i.description() < j.description();
61 _custom = Config::instance()->custom_languages ();
67 add->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_language, this));
72 LanguageTagDialog::add_language ()
74 auto full = new FullLanguageTagDialog (GetParent());
75 auto r = full->ShowModal ();
77 Config::instance()->add_custom_language (full->get());
85 LanguageTagDialog::populate_list ()
87 _list->DeleteAllItems ();
89 auto add = [this](vector<dcp::LanguageTag> const& tags) {
90 for (auto const& i: tags) {
92 it.SetId (_list->GetItemCount());
94 it.SetText (std_to_wx(i.description()));
95 _list->InsertItem (it);
97 it.SetText (std_to_wx(i.to_string()));
108 LanguageTagDialog::set (dcp::LanguageTag tag)
110 size_t selection = 0;
112 auto iter = find(_presets.begin(), _presets.end(), tag);
113 if (iter == _presets.end()) {
114 iter = find(_custom.begin(), _custom.end(), tag);
115 if (iter == _custom.end()) {
116 _custom.push_back (tag);
117 selection = _presets.size() + _custom.size() - 1;
119 if (_list->GetItemCount() > 0) {
120 _list->EnsureVisible (_list->GetItemCount() - 1);
123 selection = _presets.size() + std::distance(_custom.begin(), iter);
126 selection = std::distance(_presets.begin(), iter);
129 _list->SetItemState (selection, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
130 if (static_cast<int>(selection) < _list->GetItemCount()) {
131 _list->EnsureVisible (selection);
137 LanguageTagDialog::get () const
139 auto selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
140 DCPOMATIC_ASSERT (selected >= 0);
142 if (selected < static_cast<long>(_presets.size())) {
143 return _presets[selected];
146 selected -= _presets.size();
148 DCPOMATIC_ASSERT (selected < static_cast<long>(_custom.size()));
149 return _custom[selected];