Extract SubtagListCtrl to its own files.
[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 #include <wx/wx.h>
25 #include <boost/algorithm/string.hpp>
26
27
28 using std::string;
29 using boost::optional;
30
31
32 SubtagListCtrl::SubtagListCtrl(wxWindow* parent)
33         : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL)
34 {
35         AppendColumn("", wxLIST_FORMAT_LEFT, 80);
36         AppendColumn("", wxLIST_FORMAT_LEFT, 400);
37 }
38
39
40 void
41 SubtagListCtrl::set(dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag)
42 {
43         _all_subtags = dcp::LanguageTag::get_all(type);
44         set_search(search);
45         if (subtag) {
46                 auto i = find(_matching_subtags.begin(), _matching_subtags.end(), *subtag);
47                 if (i != _matching_subtags.end()) {
48                         auto item = std::distance(_matching_subtags.begin(), i);
49                         SetItemState(item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
50                         EnsureVisible(item);
51                 }
52         } else {
53                 if (GetItemCount() > 0) {
54                         /* The new list sometimes isn't visible without this */
55                         EnsureVisible(0);
56                 }
57         }
58 }
59
60
61 void
62 SubtagListCtrl::set_search(string search)
63 {
64         if (search == "") {
65                 _matching_subtags = _all_subtags;
66         } else {
67                 _matching_subtags.clear();
68
69                 boost::algorithm::to_lower(search);
70                 for (auto const& i: _all_subtags) {
71                         if (
72                                 (boost::algorithm::to_lower_copy(i.subtag).find(search) != string::npos) ||
73                                 (boost::algorithm::to_lower_copy(i.description).find(search) != string::npos)) {
74                                 _matching_subtags.push_back (i);
75                         }
76                 }
77         }
78
79         SetItemCount(_matching_subtags.size());
80         if (GetItemCount() > 0) {
81                 RefreshItems(0, GetItemCount() - 1);
82         }
83 }
84
85
86 optional<dcp::LanguageTag::SubtagData>
87 SubtagListCtrl::selected_subtag() const
88 {
89         auto selected = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
90         if (selected == -1) {
91                 return {};
92         }
93
94         DCPOMATIC_ASSERT(static_cast<size_t>(selected) < _matching_subtags.size());
95         return _matching_subtags[selected];
96 }
97
98
99 wxString
100 SubtagListCtrl::OnGetItemText(long item, long column) const
101 {
102         if (column == 0) {
103                 return _matching_subtags[item].subtag;
104         } else {
105                 return _matching_subtags[item].description;
106         }
107 }
108