2 Copyright (C) 2020-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 "subtag_list_ctrl.h"
23 #include "lib/dcpomatic_assert.h"
24 LIBDCP_DISABLE_WARNINGS
26 LIBDCP_ENABLE_WARNINGS
27 #include <boost/algorithm/string.hpp>
31 using boost::optional;
34 SubtagListCtrl::SubtagListCtrl(wxWindow* parent)
35 : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL)
37 AppendColumn("", wxLIST_FORMAT_LEFT, 80);
38 AppendColumn("", wxLIST_FORMAT_LEFT, 400);
43 SubtagListCtrl::set(dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag)
45 _all_subtags = dcp::LanguageTag::get_all(type);
48 auto i = find(_matching_subtags.begin(), _matching_subtags.end(), *subtag);
49 if (i != _matching_subtags.end()) {
50 auto item = std::distance(_matching_subtags.begin(), i);
51 SetItemState(item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
55 if (GetItemCount() > 0) {
56 /* The new list sometimes isn't visible without this */
64 SubtagListCtrl::set_search(string search)
67 _matching_subtags = _all_subtags;
69 _matching_subtags.clear();
71 boost::algorithm::to_lower(search);
72 for (auto const& i: _all_subtags) {
74 (boost::algorithm::to_lower_copy(i.subtag).find(search) != string::npos) ||
75 (boost::algorithm::to_lower_copy(i.description).find(search) != string::npos)) {
76 _matching_subtags.push_back (i);
81 SetItemCount(_matching_subtags.size());
82 if (GetItemCount() > 0) {
83 RefreshItems(0, GetItemCount() - 1);
88 optional<dcp::LanguageTag::SubtagData>
89 SubtagListCtrl::selected_subtag() const
91 auto selected = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
96 DCPOMATIC_ASSERT(static_cast<size_t>(selected) < _matching_subtags.size());
97 return _matching_subtags[selected];
102 SubtagListCtrl::OnGetItemText(long item, long column) const
105 return _matching_subtags[item].subtag;
107 return _matching_subtags[item].description;