Debian Codegraph @

wxcrafter/controls/custom_control_wrapper.cpp

raw
1 #include "custom_control_wrapper.h"
2
3 #include "allocator_mgr.h"
4 #include "file_logger.h"
5 #include "wxc_settings.h"
6 #include "xmlutils.h"
7
8 #include <macromanager.h>
9 #include <wx/regex.h>
10
11 CustomControlWrapper::CustomControlWrapper()
12 : wxcWidget(ID_WXCUSTOMCONTROL)
13 {
14 DelProperty(_("Control Specific Settings"));
15 AddProperty(new CategoryProperty(m_templInfoName, "Custom Control"));
16 m_namePattern = wxT("m_custom");
17
18 // Generate a name if there isn't one; but don't overwrite e.g. an imported one
19 if(!GetName().empty()) {
20 SetName(GenerateName());
21 }
22 }
23
24 CustomControlWrapper::~CustomControlWrapper() {}
25
26 wxcWidget* CustomControlWrapper::Clone() const { return new CustomControlWrapper(); }
27
28 wxString CustomControlWrapper::CppCtorCode() const
29 {
30 CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
31 if(cct.IsValid() == false)
32 return wxT("");
33
34 // support for Subclass
35 wxString subclass = PropertyString(PROP_SUBCLASS_NAME);
36 subclass.Trim().Trim(false);
37
38 wxString cppCode = cct.GetAllocationLine();
39 cppCode.Trim().Trim(false);
40
41 // cppCode contains the allocation line
42 // something like:
43 // $name = new MyCustomClassName(...);
44 static wxRegEx reNewClass("new[ \\t]+([A-Za-z_]{1}[A-Za-z0-9_]*[ \\t]*\\()");
45
46 if(!subclass.IsEmpty() && reNewClass.IsValid() && reNewClass.Matches(cppCode)) {
47 reNewClass.Replace(&cppCode, "new " + subclass + "(");
48 }
49
50 if(!cppCode.EndsWith(wxT(";"))) {
51 cppCode << wxT(";");
52 }
53
54 // Replace $name with the actual control c++ name
55 cppCode = MacroManager::Instance()->Replace(cppCode, wxT("name"), GetName(), true);
56 cppCode = MacroManager::Instance()->Replace(cppCode, wxT("parent"), GetWindowParent(), true);
57 cppCode = MacroManager::Instance()->Replace(cppCode, wxT("id"), GetId(), true);
58 return cppCode;
59 }
60
61 void CustomControlWrapper::GetIncludeFile(wxArrayString& headers) const
62 {
63 CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
64 if(cct.IsValid() == false)
65 return;
66
67 wxString inclLine = cct.GetIncludeFile();
68 if(inclLine.IsEmpty())
69 return;
70
71 inclLine.Trim().Trim(false);
72 if(inclLine.EndsWith(wxT(";"))) {
73 inclLine.RemoveLast();
74 }
75 headers.Add(inclLine);
76 }
77
78 wxString CustomControlWrapper::GetWxClassName() const
79 {
80 CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
81 if(cct.IsValid() == false)
82 return wxT("");
83
84 return cct.GetClassName();
85 }
86
87 void CustomControlWrapper::ToXRC(wxString& text, XRC_TYPE type) const
88 {
89 CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
90 if(cct.IsValid() && !cct.GetXrcPreviewClass().IsEmpty())
91 text << wxT("<object class=\"") << cct.GetXrcPreviewClass() << wxT("\" name=\"") << GetName() << wxT("\">");
92 else
93 text << wxT("<object class=\"unknown\" name=\"") << GetName() << wxT("\">");
94
95 text << XRCSize() << XRCCommonAttributes() << XRCSuffix();
96 }
97
98 void CustomControlWrapper::Serialize(JSONElement& json) const
99 {
100 wxcWidget::Serialize(json);
101 json.addProperty(wxT("m_templInfoName"), m_templInfoName);
102 ms_customControlsUsed.insert(
103 std::make_pair(m_templInfoName, wxcSettings::Get().FindByControlName(m_templInfoName)));
104 }
105
106 void CustomControlWrapper::UnSerialize(const JSONElement& json)
107 {
108 m_templInfoName = json.namedObject(wxT("m_templInfoName")).toString();
109 DoUpdateEvents();
110 wxcWidget::UnSerialize(json);
111 m_properties.Item("Custom Control")->SetValue(m_templInfoName);
112 }
113
114 /*
115 void CustomControlWrapper::LoadPropertiesFromwxSmith(const wxXmlNode* node)
116 {
117 // First call the base-class for the standard things
118 wxcWidget::LoadPropertiesFromwxSmith(node);
119
120 wxString wxclassname, include, construction, declaration, name, xrcclassname, settings;
121 xrcclassname = XmlUtils::ReadString(node, wxT("name")); // I suppose that's right: "ID_CUSTOM1"
122 name = XmlUtils::ReadString(node, wxT("subclass")); // I suppose that's right: "MyCustomClass"
123
124 propertynode = XmlUtils::FindFirstByTagName(node, wxT("creating_code"));
125 if (propertynode) {
126 construction = propertynode->GetNodeContent());
127 }
128 propertynode = XmlUtils::FindFirstByTagName(node, wxT("include_file"));
129 if (propertynode) {
130 include = propertynode->GetNodeContent());
131 }
132
133 // Hmm. I can't find any way to find the superclass's wx name e.g. wxPanel, so I'll abort this atm
134 }*/
135
136 void CustomControlWrapper::LoadPropertiesFromwxFB(const wxXmlNode* node)
137 {
138 // First call the base-class for the standard things
139 wxcWidget::LoadPropertiesFromwxFB(node);
140
141 wxString include, construction, declaration, name, xrcclassname, settings;
142 wxXmlNode* propertynode = XmlUtils::FindNodeByName(node, "property", "name");
143 if(propertynode) {
144 name = propertynode->GetNodeContent();
145 }
146
147 propertynode = XmlUtils::FindNodeByName(node, "property", "declaration");
148 if(propertynode) {
149 declaration = propertynode->GetNodeContent();
150 }
151
152 propertynode = XmlUtils::FindNodeByName(node, "property", "include");
153 if(propertynode) {
154 include = propertynode->GetNodeContent();
155 }
156
157 propertynode = XmlUtils::FindNodeByName(node, "property", "construction");
158 if(propertynode) {
159 construction = propertynode->GetNodeContent();
160 }
161
162 propertynode = XmlUtils::FindNodeByName(node, "property", "settings");
163 if(propertynode) {
164 settings = propertynode->GetNodeContent();
165 }
166
167 propertynode = XmlUtils::FindNodeByName(node, "property", "class");
168 if(propertynode) {
169 xrcclassname = propertynode->GetNodeContent();
170 }
171
172 // The wxFB fields aren't the same as the wxC ones, so we need to do some guesswork here:
173 wxString wxclassname = declaration.BeforeFirst('*').Trim(); // Get the real classname from 'MyFoo * m_foo;'
174
175 if(wxclassname.empty() || construction.empty() ||
176 declaration.empty()) { // settings and xrcclassname are optional; 'include' probably is too
177 clWARNING() << "Failed to load a Custom Control from wxFB: not all necessary data was available" << endl;
178 return;
179 }
180
181 CustomControlTemplate controlData;
182 controlData.SetAllocationLine(construction);
183 controlData.SetClassName(wxclassname);
184 controlData.SetIncludeFile(include);
185 // controlData.Set/**/Class(settings); wxC doesn't do this atm
186 controlData.SetXrcPreviewClass(xrcclassname);
187 SetTemplInfoName(wxclassname);
188 wxcSettings::Get().RegisterCustomControl(controlData);
189 wxcSettings::Get().Save();
190 }
191
192 void CustomControlWrapper::SetTemplInfoName(const wxString& templInfoName)
193 {
194 this->m_templInfoName = templInfoName;
195 DoUpdateEvents();
196 }
197
198 void CustomControlWrapper::DoUpdateEvents()
199 {
200 m_controlEvents.Clear();
201 m_connectedEvents.Clear();
202
203 CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
204 if(cct.IsValid() == false)
205 return;
206
207 const wxStringMap_t& events = cct.GetEvents();
208 wxStringMap_t::const_iterator iter = events.begin();
209 for(; iter != events.end(); ++iter) {
210 RegisterEvent(iter->first, iter->second, "");
211 // m_controlEvents.Add(ConnectDetails(iter->first, iter->second, "", wxString() << iter->second << "Handler"));
212 }
213 }
214
215 void CustomControlWrapper::DoDeepCopy(const wxcWidget& rhs, enum DuplicatingOptions nametypesToChange,
216 const std::set<wxString>& existingNames, const wxString& chosenName,
217 const wxString& chosenInheritedName, const wxString& chosenFilename)
218 {
219 wxcWidget::DoDeepCopy(rhs, nametypesToChange, existingNames, chosenName, chosenInheritedName, chosenFilename);
220 /// copy some custom control specific content
221 const CustomControlWrapper& origin = static_cast<const CustomControlWrapper&>(rhs);
222 m_templInfoName = origin.m_templInfoName;
223 }