Supporters update.
[dcpomatic.git] / src / wx / dcpomatic_choice.cc
1 /*
2     Copyright (C) 2022 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 "dcpomatic_choice.h"
23 #include "wx_util.h"
24
25
26 using std::string;
27 using boost::optional;
28
29
30 Choice::Choice(wxWindow* parent)
31         : wxChoice(parent, wxID_ANY)
32 {
33         /* This hack works around a problem where the height of the wxChoice would be
34          * too small on KDE.  This added empty string will be removed in the first
35          * call to add().
36          */
37         Append("");
38         set(0);
39 }
40
41
42 void
43 Choice::add(string const& entry)
44 {
45         add(std_to_wx(entry));
46 }
47
48
49 void
50 Choice::add(wxString const& entry)
51 {
52         if (_needs_clearing) {
53                 Clear();
54                 _needs_clearing = false;
55         }
56
57         Append(entry);
58 }
59
60
61 void
62 Choice::add(wxString const& entry, wxClientData* data)
63 {
64         if (_needs_clearing) {
65                 Clear();
66                 _needs_clearing = false;
67         }
68
69         Append(entry, data);
70 }
71
72
73 void
74 Choice::add(wxString const& entry, wxString const& data)
75 {
76         if (_needs_clearing) {
77                 Clear();
78                 _needs_clearing = false;
79         }
80
81         Append(entry, new wxStringClientData(data));
82 }
83
84
85 void
86 Choice::set(int index)
87 {
88         SetSelection(index);
89 }
90
91
92 optional<int>
93 Choice::get() const
94 {
95         auto const sel = GetSelection();
96         if (sel == wxNOT_FOUND) {
97                 return {};
98         }
99
100         return sel;
101 }
102
103
104 optional<wxString>
105 Choice::get_data() const
106 {
107         auto index = get();
108         if (!index) {
109                 return {};
110         }
111
112         return dynamic_cast<wxStringClientData*>(GetClientObject(*index))->GetData();
113 }
114