Debian Codegraph @

wxcrafter/src/import_from_xrc.cpp

raw
1 #include "import_from_xrc.h"
2
3 #include "allocator_mgr.h"
4 #include "file_logger.h"
5 #include "import_dlg.h"
6 #include "notebook_base_wrapper.h"
7 #include "notebook_page_wrapper.h"
8 #include "wxc_project_metadata.h"
9 #include "wxgui_helpers.h"
10 #include "xmlutils.h"
11
12 #include <wx/log.h>
13 #include <wx/msgdlg.h>
14 #include <wx/window.h>
15 #include <wx/xml/xml.h>
16
17 ImportFromXrc::ImportFromXrc(wxWindow* parent)
18 : m_Parent(parent)
19 {
20 }
21
22 ImportFromXrc::~ImportFromXrc() {}
23
24 bool ImportFromXrc::ImportProject(ImportDlg::ImportFileData& data) const
25 {
26 ImportDlg dlg(ImportDlg::IPD_XRC, m_Parent);
27 if(dlg.ShowModal() != wxID_OK) {
28 return false;
29 }
30
31 wxString filepath = dlg.GetFilepath();
32
33 if(filepath.empty() || !wxFileExists(filepath)) {
34 return false;
35 }
36
37 wxXmlDocument doc(filepath);
38 if(!doc.IsOk()) {
39 wxMessageBox(_("Failed to load the file to import"), _("CodeLite"), wxICON_ERROR | wxOK, m_Parent);
40 return false;
41 }
42
43 wxcWidget::List_t toplevels;
44 if(ParseFile(doc, toplevels) && !toplevels.empty()) {
45 wxcProjectMetadata::Get().Serialize(toplevels, wxFileName(dlg.GetOutputFilepath()));
46 data = dlg.GetData();
47 return true;
48 }
49 return false;
50 }
51
52 bool ImportFromXrc::ParseFile(wxXmlDocument& doc, wxcWidget::List_t& toplevels) const
53 {
54 wxXmlNode* toplevelnode = doc.GetRoot()->GetChildren();
55 while(toplevelnode) {
56 wxString tag = toplevelnode->GetName();
57 if(tag != wxT("object")) {
58 wxMessageBox(_("This doesn't seem to be a valid XRC file. Aborting."), _("CodeLite"), wxICON_ERROR | wxOK,
59 m_Parent);
60 return false;
61 }
62
63 bool alreadyParented(false);
64 wxcWidget* wrapper = ParseNode(toplevelnode, NULL, alreadyParented);
65 if(wrapper) {
66 toplevels.push_back(wrapper);
67 }
68
69 toplevelnode = toplevelnode->GetNext();
70 }
71
72 return true;
73 }
74
75 wxcWidget* ImportFromXrc::ParseNode(wxXmlNode* node, wxcWidget* parentwrapper, bool& alreadyParented) const
76 {
77 wxcWidget* wrapper = NULL;
78
79 // XRC for some reason treats sizeritems as an object! We'll deal with that below, but cache node first
80 // BTW, wxSpacer is an exception: it's called <spacer> but looks like a sizeritem with no contained object
81 // BTW2, the buttons in a wxStdButtonSizer are also an exception: each is wrapped in a <button> node that behaves
82 // like sizeritem, so treat it as such
83 wxXmlNode* sizeritemnode = node;
84 wxXmlNode* buttonnode = NULL;
85
86 // For notebooks, both XRC and JSON store their pages as sibling children of the book
87 // XRC stores them in a <notebookpage> 'wrapper', similar to the sizeritem ones
88 // If you add a notebookpage in e.g. XRCed, it invents a wxPanel and adds it as a notebookpage child object
89 // (If you add a normal control, that takes the place of the wxPanel. wxCrafter doesn't let you do that, at least
90 // atm) For treebook this is still true for level 0 pages, but subpages are different: In XRC each is a sibling of
91 // all the other tbk pages, but with a <depth> of 1 (or 2 or...) JSON stores subpages as children of the last
92 // page-of-less-depth; so the hierarchy might be:
93 // Treebook
94 // Subpage 1 depth 0
95 // Subpage 2 depth 0
96 // Subpage 3 depth 1
97 // Subpage 4 depth 1
98 // Subpage 5 depth 2
99 // Subpage 6 depth 1
100 // Subpage 7 depth 0
101 // SomeOtherControl
102 wxXmlNode* booknode = NULL;
103
104 wxString tag = node->GetName();
105 wxCHECK_MSG(tag == wxT("object"), NULL, wxT("Passed a node that isn't an object"));
106
107 wxString classname = XmlUtils::ReadString(node, wxT("class"));
108 wxCHECK_MSG(!classname.empty(), NULL, wxT("Object node doesn't have a 'class' attribute"));
109
110 if(classname == wxT("sizeritem")) {
111 // See the above comment. Replace node with the contained object node
112 // We'll process sizeritemnode node separately later
113 node = XmlUtils::FindFirstByTagName(node, wxT("object"));
114 if(!node) {
115 // I don't think this can happen, but...
116 return NULL;
117 }
118
119 // Read the class of the new node
120 classname = XmlUtils::ReadString(node, wxT("class"));
121 }
122
123 else if(classname.Contains(wxT("bookpage"))) {
124 // See the above comment. Replace node with the contained object node
125 // We'll process booknode node separately later
126 booknode = node;
127 node = XmlUtils::FindFirstByTagName(node, wxT("object"));
128 if(!node) {
129 // I don't think this can happen, but...
130 return NULL;
131 }
132
133 // Read the class of the new node
134 classname = XmlUtils::ReadString(node, wxT("class"));
135 }
136
137 else if(classname == wxT("button")) {
138 buttonnode = node;
139 node = XmlUtils::FindFirstByTagName(node, wxT("object"));
140 if(!node) {
141 return NULL;
142 }
143
144 classname = XmlUtils::ReadString(node, wxT("class"));
145 if(classname == "wxButton") { // and it certainly should be
146 classname = "stdbutton";
147 }
148 }
149
150 int Id = Allocator::StringToId(classname);
151 if(Id == wxNOT_FOUND) {
152 wxLogWarning(wxString::Format(_("Can't import unknown class %s from XRC"), classname));
153 return NULL;
154 }
155
156 if((Id == ID_WXPANEL) && (parentwrapper == NULL)) {
157 Id = ID_WXPANEL_TOPLEVEL;
158 }
159
160 if(parentwrapper && parentwrapper->GetWidgetType() == TYPE_TOOLBAR) {
161 // In XRC, classname == "separator", which gives and Id of ID_WXMENUITEM, could be a menu or toolbar one
162 // "space" is a stretchspacer
163 if(classname == "separator") {
164 Id = ID_WXTOOLBARITEM_SEPARATOR;
165 } else if(classname == "space") {
166 Id = ID_WXTOOLBARITEM_STRETCHSPACE;
167 }
168 }
169
170 if(parentwrapper && parentwrapper->GetWidgetType() == TYPE_AUITOOLBAR) {
171 // This time it could be separator, space, or label
172 // "space" is a stretchspacer
173 if(classname == "separator") {
174 Id = ID_WXTOOLBARITEM_SEPARATOR; // No need for a specific one for aui
175 } else if(classname == "space") {
176 // There were briefly 2 different words for 'space': space itself, and nonstretchspace
177 // Now the official auitoolbar handler uses 'space' for both, distinguishing by the presence/absence of a
178 // 'width' attribute, so do the same
179 if(XmlUtils::FindFirstByTagName(node, "width")) {
180 Id = ID_WXAUITOOLBARITEM_SPACE;
181 } else {
182 Id = ID_WXAUITOOLBARITEM_STRETCHSPACE;
183 }
184 }
185 }
186
187 if(booknode) {
188 // Correctly label a wxPanel that's really a notebookpage
189 Id = ID_WXPANEL_NOTEBOOK_PAGE;
190 }
191
192 wrapper = Allocator::Instance()->Create(Id);
193 wxCHECK_MSG(wrapper, NULL, wxT("Failed to create a wrapper"));
194
195 wrapper->LoadPropertiesFromXRC(node);
196
197 GetSizeritemContents(sizeritemnode, wrapper); // Now we have a valid wrapper, we can store the sizeritem info in it
198
199 int depth = 0;
200 if(booknode) {
201 // For book pages, extract any info from the <foobookpage> node
202 NotebookPageWrapper* nbwrapper = dynamic_cast<NotebookPageWrapper*>(wrapper);
203 wxCHECK_MSG(nbwrapper, NULL, wxT("A booknode which has no NotebookPageWrapper"));
204 GetBookitemContents(booknode, nbwrapper, depth);
205
206 // If this is a treebook subpage, it needs to be parented by a page, not the book
207 // depth will only be >0 in that situation
208 if(depth) {
209 NotebookBaseWrapper* nb = dynamic_cast<NotebookBaseWrapper*>(parentwrapper);
210 wxCHECK_MSG(nb, NULL, wxT("treebookpage 'parent' not a book"));
211
212 wxcWidget* item = nb->GetChildPageAtDepth(depth - 1);
213 if(item) {
214 item->AddChild(wrapper);
215 alreadyParented = true;
216 } else {
217 // If the XRC held an invalid depth (i.e. there was no suitable parent found)
218 // just mention the fact, and add the page anyway at depth 0
219 wxLogWarning("Trying to add to a wxTreebook a page with invalid depth");
220 }
221 }
222 }
223
224 if(buttonnode) {
225 // For wxStdDialogButtonSizer buttons, extract any info from the <buttons> node
226 ProcessButtonNode(buttonnode, wrapper);
227 }
228
229 wxXmlNode* child = node->GetChildren();
230 while(child) {
231 wxString childname(child->GetName());
232 if(childname == wxT("object")) {
233 bool alreadyParented(false);
234 wxcWidget* childwrapper = ParseNode(child, wrapper, alreadyParented);
235 if(childwrapper && !alreadyParented) {
236 wrapper->AddChild(childwrapper);
237 }
238 }
239
240 child = child->GetNext();
241 }
242
243 return wrapper;
244 }
245
246 void ImportFromXrc::GetSizeritemContents(const wxXmlNode* node, wxcWidget* wrapper) const
247 {
248 wrapper->ClearSizerAll(); // otherwise the default ones will remain
249
250 wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("flag"));
251 if(propertynode) {
252 wxString flags = propertynode->GetNodeContent();
253 // We must cope with wxC's American misspellings...
254 flags.Replace("wxALIGN_CENTRE", "wxALIGN_CENTER");
255
256 // if left|right|top|bottom add wxALL. Duplication does no harm here
257 if(flags.Contains("wxLEFT") && flags.Contains("wxRIGHT") && flags.Contains("wxTOP") &&
258 flags.Contains("wxBOTTOM")) {
259 flags << "|wxALL";
260 }
261
262 wxArrayString flagsarray = wxCrafter::Split(flags, "|");
263
264 // Eran: If flagsarray contains 'wxALL' - make sure all the other four stars are also there...
265 if(flagsarray.Index("wxALL") != wxNOT_FOUND) {
266 flagsarray.Add("wxLEFT");
267 flagsarray.Add("wxRIGHT");
268 flagsarray.Add("wxTOP");
269 flagsarray.Add("wxBOTTOM");
270 flagsarray = wxCrafter::MakeUnique(flagsarray);
271 }
272 for(size_t n = 0; n < flagsarray.GetCount(); ++n) {
273 wrapper->EnableSizerFlag(flagsarray.Item(n), true);
274 }
275 }
276
277 propertynode = XmlUtils::FindFirstByTagName(node, wxT("option")); // 'option' is XRC's term for proportion :/
278 if(propertynode) {
279 wxString proportion = propertynode->GetNodeContent();
280 wrapper->SizerItem().SetProportion(wxCrafter::ToNumber(proportion, 0));
281 }
282
283 propertynode = XmlUtils::FindFirstByTagName(node, wxT("border"));
284 if(propertynode) {
285 wxString border = propertynode->GetNodeContent();
286 wrapper->SizerItem().SetBorder(wxCrafter::ToNumber(border, 0));
287 }
288
289 propertynode = XmlUtils::FindFirstByTagName(node, wxT("cellpos"));
290 if(propertynode) {
291 wxString cellpos = propertynode->GetNodeContent();
292 if(!cellpos.empty()) {
293 wrapper->SetGbPos(cellpos);
294 }
295 }
296
297 propertynode = XmlUtils::FindFirstByTagName(node, wxT("cellspan"));
298 if(propertynode) {
299 wxString cellspan = propertynode->GetNodeContent();
300 if(!cellspan.empty()) {
301 wrapper->SetGbSpan(cellspan);
302 }
303 }
304
305 propertynode = XmlUtils::FindFirstByTagName(node, wxT("minsize"));
306 if(propertynode) {
307 wxString minsize = propertynode->GetNodeContent();
308 PropertyBase* prop = wrapper->GetProperty(PROP_MINSIZE);
309 if(prop) {
310 prop->SetValue(minsize);
311 }
312 }
313 }
314
315 void ImportFromXrc::GetBookitemContents(const wxXmlNode* node, NotebookPageWrapper* wrapper, int& depth) const
316 {
317 wxString classname = XmlUtils::ReadString(node, wxT("class"));
318
319 wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("selected"));
320 if(propertynode) {
321 wxString selected = propertynode->GetNodeContent();
322 if(selected == "1") {
323 wrapper->SetSelected(true);
324 }
325 }
326
327 propertynode = XmlUtils::FindFirstByTagName(node, wxT("label"));
328 if(propertynode) {
329 wxString label = propertynode->GetNodeContent();
330 PropertyBase* prop = wrapper->GetProperty(PROP_LABEL);
331 if(prop) {
332 prop->SetValue(label);
333 }
334 }
335
336 if(classname != "choicebookpage") { // which don't have bitmaps
337 propertynode = XmlUtils::FindFirstByTagName(node, wxT("bitmap"));
338 if(propertynode) {
339 ImportFromXrc::ProcessBitmapProperty(propertynode, wrapper, "PROP_BITMAP_PATH", "wxART_OTHER");
340 }
341 }
342
343 if(classname == "treebookpage") {
344 propertynode = XmlUtils::FindFirstByTagName(node, wxT("depth"));
345 if(propertynode) {
346 depth = wxCrafter::ToNumber(propertynode->GetNodeContent(), 0);
347 }
348 }
349 }
350
351 // static NB This is also used to import from wxSmith
352 void ImportFromXrc::ProcessBitmapProperty(const wxXmlNode* node, wxcWidget* wrapper,
353 const wxString& property /*= PROP_BITMAP_PATH*/,
354 const wxString& client_hint /*=""*/)
355 {
356 // Node may be e.g. <bitmap stock_id="wxART_GO_DOWN" stock_client="wxART_BUTTON">some.png</bitmap>
357 // (the some.png is optional here, but is used as a fallback if stock_id fails. wxC doesn't use this atm, so ignore
358 // it here)
359 wxString artstring = XmlUtils::ReadString(node, "stock_id");
360 if(!artstring.empty()) {
361 wxString clientId = XmlUtils::ReadString(node, "stock_client");
362 if(clientId.empty()) {
363 clientId = client_hint; // Use any hint we were given
364 }
365 if(!clientId.empty()) {
366 artstring << "," << clientId;
367 }
368 wrapper->SetPropertyString(property, artstring);
369 } else {
370 // or e.g. <bitmap>/path/to/some.png</bitmap>
371 wrapper->SetPropertyString(property, node->GetNodeContent());
372 }
373 }
374
375 void ImportFromXrc::ProcessButtonNode(const wxXmlNode* node, wxcWidget* wrapper) const
376 {
377 // First deal with any sizer stuff (flag, option...) in here; wxFB does this, XRCed and wxSmith don't
378 GetSizeritemContents(node, wrapper);
379
380 DoProcessButtonNode(node, wrapper); // Shared with ImportFromwxSmith
381 }
382
383 void ImportFromXrc::DoProcessButtonNode(const wxXmlNode* node, wxcWidget* wrapper)
384 {
385 // Deduce a sensible ID. There isn't an id field in the xrc, so work it out from the name or label
386 wxString name = wrapper->GetName().MakeLower();
387 wxString label = wrapper->Label().MakeLower();
388 if(name.Contains("ok") || label.Contains("ok")) {
389 wrapper->SetId("wxID_OK");
390 } else if(name.Contains("cancel") || label.Contains("cancel")) {
391 wrapper->SetId("wxID_CANCEL");
392 } else if(name.Contains("yes") || label.Contains("yes")) {
393 wrapper->SetId("wxID_YES");
394 } else if(name.Contains("save") || label.Contains("save")) {
395 wrapper->SetId("wxID_SAVE");
396 } else if(name.Contains("apply") || label.Contains("apply")) {
397 wrapper->SetId("wxID_APPLY");
398 } else if(name.Contains("close") || label.Contains("close")) {
399 wrapper->SetId("wxID_CLOSE");
400 } else if(name.Contains("no") || label.Contains("no")) {
401 wrapper->SetId("wxID_NO");
402 } else if(name.Contains("context") || label.Contains("context")) {
403 wrapper->SetId("wxID_CONTEXT_HELP");
404 } else if(name.Contains("help") || label.Contains("help")) {
405 wrapper->SetId("wxID_HELP");
406 }
407 }
408
409 void ImportFromXrc::ProcessNamedNode(wxXmlNode* node, wxcWidget* parentwrapper, const wxString& name) const
410 {
411 // This is for processing the child menu of an auitoolbar dropdownitem (but might be useful elsewhere in the future)
412 // The original node was <dropdown>, not <object>, so normal parsing would ignore any children
413 wxXmlNode* childnode = XmlUtils::FindFirstByTagName(node, "object");
414 if(childnode && (XmlUtils::ReadString(childnode, "class") == name)) {
415 bool alreadyParented(false);
416 wxcWidget* childwrapper = ParseNode(childnode, parentwrapper, alreadyParented);
417 if(childwrapper) {
418 parentwrapper->AddChild(childwrapper);
419 }
420 }
421 }
422
423 bool ImportFromXrc::GetProject() const { return false; }