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