Supporters update.
[dcpomatic.git] / src / wx / subtag_list_ctrl.cc
1 /*
2     Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "subtag_list_ctrl.h"
23 #include "lib/dcpomatic_assert.h"
24 LIBDCP_DISABLE_WARNINGS
25 #include <wx/wx.h>
26 LIBDCP_ENABLE_WARNINGS
27 #include <boost/algorithm/string.hpp>
28
29
30 using std::string;
31 using boost::optional;
32
33
34 SubtagListCtrl::SubtagListCtrl(wxWindow* parent)
35         : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL)
36 {
37         AppendColumn("", wxLIST_FORMAT_LEFT, 80);
38         AppendColumn("", wxLIST_FORMAT_LEFT, 400);
39 }
40
41
42 void
43 SubtagListCtrl::set(dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag)
44 {
45         _all_subtags = dcp::LanguageTag::get_all(type);
46         set_search(search);
47         if (subtag) {
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);
52                         EnsureVisible(item);
53                 }
54         } else {
55                 if (GetItemCount() > 0) {
56                         /* The new list sometimes isn't visible without this */
57                         EnsureVisible(0);
58                 }
59         }
60 }
61
62
63 void
64 SubtagListCtrl::set_search(string search)
65 {
66         if (search == "") {
67                 _matching_subtags = _all_subtags;
68         } else {
69                 _matching_subtags.clear();
70
71                 boost::algorithm::to_lower(search);
72                 for (auto const& i: _all_subtags) {
73                         if (
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);
77                         }
78                 }
79         }
80
81         SetItemCount(_matching_subtags.size());
82         if (GetItemCount() > 0) {
83                 RefreshItems(0, GetItemCount() - 1);
84         }
85 }
86
87
88 optional<dcp::LanguageTag::SubtagData>
89 SubtagListCtrl::selected_subtag() const
90 {
91         auto selected = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
92         if (selected == -1) {
93                 return {};
94         }
95
96         DCPOMATIC_ASSERT(static_cast<size_t>(selected) < _matching_subtags.size());
97         return _matching_subtags[selected];
98 }
99
100
101 wxString
102 SubtagListCtrl::OnGetItemText(long item, long column) const
103 {
104         if (column == 0) {
105                 return _matching_subtags[item].subtag;
106         } else {
107                 return _matching_subtags[item].description;
108         }
109 }
110