Supporters update.
[dcpomatic.git] / src / wx / language_subtag_panel.cc
1 /*
2     Copyright (C) 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 "language_subtag_panel.h"
23 #include <dcp/warnings.h>
24 LIBDCP_DISABLE_WARNINGS
25 #include <wx/srchctrl.h>
26 #include <wx/wx.h>
27 LIBDCP_ENABLE_WARNINGS
28
29
30 using std::string;
31 using boost::optional;
32
33
34 LanguageSubtagPanel::LanguageSubtagPanel(wxWindow* parent)
35         : wxPanel (parent, wxID_ANY)
36 {
37 #ifdef __WXGTK3__
38         int const height = 30;
39 #else
40         int const height = -1;
41 #endif
42
43         _search = new wxSearchCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
44         _list = new SubtagListCtrl(this);
45
46         auto sizer = new wxBoxSizer(wxVERTICAL);
47         sizer->Add(_search, 0, wxALL, 8);
48         sizer->Add(_list, 1, wxALL, 8);
49         SetSizer(sizer);
50
51         _search->Bind(wxEVT_TEXT, boost::bind(&LanguageSubtagPanel::search_changed, this));
52         _list->Bind(wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
53         _list->Bind(wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
54 }
55
56
57 void
58 LanguageSubtagPanel::set(dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag)
59 {
60         _list->set(type, search, subtag);
61         _search->SetValue(wxString(search));
62 }
63
64
65 optional<dcp::LanguageTag::RegionSubtag>
66 LanguageSubtagPanel::get() const
67 {
68         if (!_list->selected_subtag()) {
69                 return {};
70         }
71
72         return dcp::LanguageTag::RegionSubtag(_list->selected_subtag()->subtag);
73 }
74
75
76 void
77 LanguageSubtagPanel::search_changed()
78 {
79         auto search = _search->GetValue();
80         _list->set_search(search.ToStdString());
81         if (search.Length() > 0 && _list->GetItemCount() > 0) {
82                 _list->EnsureVisible (0);
83         }
84         SearchChanged(_search->GetValue().ToStdString());
85 }
86
87
88 void
89 LanguageSubtagPanel::selection_changed()
90 {
91         SelectionChanged(_list->selected_subtag());
92 }
93