Debian Codegraph @

wxcrafter/src/choice_property.cpp

raw
1 #include "choice_property.h"
2 #include "wxgui_defs.h"
3 #include <wx/choice.h>
4
5 ChoiceProperty::ChoiceProperty()
6 : PropertyBase(wxT(""))
7 , m_selection(wxNOT_FOUND)
8 {
9 }
10
11 ChoiceProperty::ChoiceProperty(const wxString& label, const wxArrayString& options, int selection, const wxString& tip)
12 : PropertyBase(tip)
13 {
14 m_options.insert(m_options.end(), options.begin(), options.end());
15 m_selection = selection;
16 SetLabel(label);
17 }
18
19 ChoiceProperty::~ChoiceProperty() {}
20
21 void ChoiceProperty::SetSelection(int selection)
22 {
23 if(selection < 0 || selection >= (int)m_options.GetCount()) return;
24
25 this->m_selection = selection;
26 }
27
28 wxString ChoiceProperty::GetValue() const
29 {
30 if(m_selection < 0 || m_selection >= (int)m_options.GetCount()) return wxT("");
31 return m_options.Item(m_selection);
32 }
33
34 void ChoiceProperty::SetValue(const wxString& value)
35 {
36 int where = m_options.Index(value);
37 if(where == wxNOT_FOUND) {
38 m_selection = m_options.Add(value);
39
40 } else {
41 m_selection = where;
42 }
43 }
44
45 void ChoiceProperty::Add(const wxString& value)
46 {
47 int where = m_options.Index(value);
48 if(where == wxNOT_FOUND) m_options.Add(value);
49 }
50
51 JSONElement ChoiceProperty::Serialize() const
52 {
53 JSONElement json = JSONElement::createObject();
54 json.addProperty(wxT("type"), wxT("choice"));
55 DoBaseSerialize(json);
56 json.addProperty(wxT("m_selection"), m_selection);
57 json.addProperty(wxT("m_options"), m_options);
58 return json;
59 }
60
61 void ChoiceProperty::UnSerialize(const JSONElement& json)
62 {
63 DoBaseUnSerialize(json);
64 m_selection = json.namedObject(wxT("m_selection")).toInt();
65 m_options = json.namedObject(wxT("m_options")).toArrayString();
66
67 // // This hack is needed to support migration from the old values
68 // if ( GetLabel() == PROP_SPLIT_MODE ) {
69 // m_options.Clear();
70 // // The order matters here: we must add vertical and then horizontal
71 // m_options.Add("wxSPLIT_VERTICAL");
72 // m_options.Add("wxSPLIT_HORIZONTAL");
73 // }
74 }