| 1 |
#include "import_from_wxSmith.h" |
| 2 |
#include "allocator_mgr.h" |
| 3 |
#include "file_logger.h" |
| 4 |
#include "import_dlg.h" |
| 5 |
#include "import_from_xrc.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 |
#include <wx/log.h> |
| 12 |
#include <wx/msgdlg.h> |
| 13 |
#include <wx/window.h> |
| 14 |
#include <wx/xml/xml.h> |
| 15 |
|
| 16 |
std::map<wxString, wxString> ImportFromwxSmith::sm_eventMap; |
| 17 |
|
| 18 |
ImportFromwxSmith::ImportFromwxSmith(wxWindow* parent) |
| 19 |
: m_Parent(parent) |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
ImportFromwxSmith::~ImportFromwxSmith() {} |
| 24 |
|
| 25 |
bool ImportFromwxSmith::ImportProject(ImportDlg::ImportFileData& data, const wxString& sourceFile) const |
| 26 |
{ |
| 27 |
ImportDlg dlg(ImportDlg::IPD_Smith, m_Parent, sourceFile); |
| 28 |
|
| 29 |
if(dlg.ShowModal() != wxID_OK) { return false; } |
| 30 |
|
| 31 |
wxString filepath = dlg.GetFilepath(); |
| 32 |
if(filepath.empty() || !wxFileExists(filepath)) { return false; } |
| 33 |
|
| 34 |
wxXmlDocument doc(filepath); |
| 35 |
if(!doc.IsOk()) { |
| 36 |
wxMessageBox(_("Failed to load the file to import"), _("CodeLite"), wxICON_ERROR | wxOK, m_Parent); |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
wxcWidget::List_t toplevels; |
| 41 |
if(ParseFile(doc, toplevels) && !toplevels.empty()) { |
| 42 |
wxcProjectMetadata::Get().Serialize(toplevels, wxFileName(dlg.GetOutputFilepath())); |
| 43 |
data = dlg.GetData(); |
| 44 |
return true; |
| 45 |
} |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
bool ImportFromwxSmith::ParseFile(wxXmlDocument& doc, wxcWidget::List_t& toplevels) const |
| 50 |
{ |
| 51 |
// <?xml version="1.0" encoding="utf-8" ?> |
| 52 |
// <wxsmith> |
| 53 |
// <object class="wxFrame" name="Foo"> |
| 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 wxSmith file. Aborting."), _("CodeLite"), |
| 59 |
wxICON_ERROR | wxOK, m_Parent); |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
bool alreadyParented(false); |
| 64 |
wxcWidget* wrapper = ParseNode(toplevelnode, NULL, alreadyParented); |
| 65 |
if(wrapper) { toplevels.push_back(wrapper); } |
| 66 |
|
| 67 |
toplevelnode = toplevelnode->GetNext(); |
| 68 |
} |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
wxcWidget* ImportFromwxSmith::ParseNode(wxXmlNode* node, wxcWidget* parentwrapper, bool& alreadyParented) const |
| 74 |
{ |
| 75 |
wxcWidget* wrapper = NULL; |
| 76 |
wxXmlNode* sizeritemnode = node; |
| 77 |
wxXmlNode* buttonnode = NULL; |
| 78 |
wxXmlNode* booknode = NULL; |
| 79 |
|
| 80 |
wxString tag = node->GetName(); |
| 81 |
wxCHECK_MSG(tag == wxT("object"), NULL, wxT("Passed a node that isn't an object")); |
| 82 |
|
| 83 |
wxString classname = XmlUtils::ReadString(node, wxT("class")); |
| 84 |
wxCHECK_MSG(!classname.empty(), NULL, wxT("Object node doesn't have a 'class' attribute")); |
| 85 |
|
| 86 |
if(classname == wxT("sizeritem")) { |
| 87 |
// See the ImportFromXRC comment. Replace node with the contained object node |
| 88 |
// We'll process sizeritemnode node separately later |
| 89 |
node = XmlUtils::FindFirstByTagName(node, wxT("object")); |
| 90 |
if(!node) { |
| 91 |
// I don't think this can happen, but... |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
// Read the class of the new node |
| 96 |
classname = XmlUtils::ReadString(node, wxT("class")); |
| 97 |
} |
| 98 |
|
| 99 |
else if(classname.Contains(wxT("bookpage"))) { |
| 100 |
// See the above comment. Replace node with the contained object node |
| 101 |
// We'll process booknode node separately later |
| 102 |
booknode = node; |
| 103 |
node = XmlUtils::FindFirstByTagName(node, wxT("object")); |
| 104 |
if(!node) { |
| 105 |
// I don't think this can happen, but... |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
// Read the class of the new node |
| 110 |
classname = XmlUtils::ReadString(node, wxT("class")); |
| 111 |
} |
| 112 |
|
| 113 |
else if(classname == wxT("button")) { |
| 114 |
buttonnode = node; |
| 115 |
node = XmlUtils::FindFirstByTagName(node, wxT("object")); |
| 116 |
if(!node) { |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
classname = XmlUtils::ReadString(node, wxT("class")); |
| 121 |
if(classname == "wxButton") { // and it certainly should be |
| 122 |
classname = "stdbutton"; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
int Id = Allocator::StringToId(classname); |
| 127 |
if(Id == wxNOT_FOUND) { |
| 128 |
wxLogWarning(wxString::Format(_("Can't import unknown class %s in from wxSmith"), classname)); |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
if((Id == ID_WXPANEL) && (parentwrapper == NULL)) { Id = ID_WXPANEL_TOPLEVEL; } |
| 133 |
|
| 134 |
// classname == "separator", which gives and Id of ID_WXMENUITEM, could be a menu or toolbar one |
| 135 |
if((classname == "separator") && (parentwrapper && parentwrapper->GetWidgetType() == TYPE_TOOLBAR)) { |
| 136 |
Id = ID_WXTOOLBARITEM; |
| 137 |
} |
| 138 |
|
| 139 |
if(booknode) { |
| 140 |
// Correctly label a wxPanel that's really a notebookpage |
| 141 |
Id = ID_WXPANEL_NOTEBOOK_PAGE; |
| 142 |
} |
| 143 |
|
| 144 |
wrapper = Allocator::Instance()->Create(Id); |
| 145 |
wxCHECK_MSG(wrapper, NULL, wxT("Failed to create a wrapper")); |
| 146 |
|
| 147 |
wrapper->LoadPropertiesFromwxSmith(node); |
| 148 |
|
| 149 |
GetSizeritemContents(sizeritemnode, wrapper); // Now we have a valid wrapper, we can store the sizeritem info in it |
| 150 |
|
| 151 |
int depth = 0; |
| 152 |
if(booknode) { |
| 153 |
// For book pages, extract any info from the <foobookpage> node |
| 154 |
NotebookPageWrapper* nbwrapper = dynamic_cast<NotebookPageWrapper*>(wrapper); |
| 155 |
wxCHECK_MSG(nbwrapper, NULL, wxT("A booknode which has no NotebookPageWrapper")); |
| 156 |
GetBookitemContents(booknode, nbwrapper, depth); |
| 157 |
|
| 158 |
// If this is a treebook subpage, it needs to be parented by a page, not the book |
| 159 |
// depth will only be >0 in that situation |
| 160 |
// NB I can't find a way to create a subpage in wxSmith, but just maybe... |
| 161 |
if(depth) { |
| 162 |
NotebookBaseWrapper* nb = dynamic_cast<NotebookBaseWrapper*>(parentwrapper); |
| 163 |
wxCHECK_MSG(nb, NULL, wxT("treebookpage 'parent' not a book")); |
| 164 |
|
| 165 |
wxcWidget* item = nb->GetChildPageAtDepth(depth - 1); |
| 166 |
if(item) { |
| 167 |
item->AddChild(wrapper); |
| 168 |
alreadyParented = true; |
| 169 |
} else { |
| 170 |
// If the XRC held an invalid depth (i.e. there was no suitable parent found) |
| 171 |
// just mention the fact, and add the page anyway at depth 0 |
| 172 |
wxLogWarning("Trying to add to a wxTreebook a page with invalid depth"); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if(buttonnode) { |
| 178 |
// For wxStdDialogButtonSizer buttons, extract any info from the <buttons> node |
| 179 |
ImportFromXrc::DoProcessButtonNode(buttonnode, wrapper); |
| 180 |
} |
| 181 |
|
| 182 |
wxXmlNode* child = node->GetChildren(); |
| 183 |
while(child) { |
| 184 |
wxString childname(child->GetName()); |
| 185 |
if(childname == wxT("object")) { |
| 186 |
bool alreadyParented(false); |
| 187 |
wxcWidget* childwrapper = ParseNode(child, wrapper, alreadyParented); |
| 188 |
if(childwrapper && !alreadyParented) { wrapper->AddChild(childwrapper); } |
| 189 |
} |
| 190 |
|
| 191 |
child = child->GetNext(); |
| 192 |
} |
| 193 |
|
| 194 |
return wrapper; |
| 195 |
} |
| 196 |
|
| 197 |
void ImportFromwxSmith::GetSizeritemContents(const wxXmlNode* node, wxcWidget* wrapper) const |
| 198 |
{ |
| 199 |
wrapper->ClearSizerAll(); // otherwise the default ones will remain |
| 200 |
|
| 201 |
wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("flag")); |
| 202 |
if(propertynode) { |
| 203 |
wxString flags = propertynode->GetNodeContent(); |
| 204 |
// We must cope with wxC's American misspellings... |
| 205 |
flags.Replace("wxALIGN_CENTRE", "wxALIGN_CENTER"); |
| 206 |
|
| 207 |
// if left|right|top|bottom add wxALL. Duplication does no harm here |
| 208 |
if(flags.Contains("wxLEFT") && flags.Contains("wxRIGHT") && flags.Contains("wxTOP") && |
| 209 |
flags.Contains("wxBOTTOM")) { |
| 210 |
flags << "|wxALL"; |
| 211 |
} |
| 212 |
|
| 213 |
wxArrayString flagsarray = wxCrafter::Split(flags, "|"); |
| 214 |
|
| 215 |
// Eran: If flagsarray contains 'wxALL' - make sure all the other four stars are also there... |
| 216 |
if(flagsarray.Index("wxALL") != wxNOT_FOUND) { |
| 217 |
flagsarray.Add("wxLEFT"); |
| 218 |
flagsarray.Add("wxRIGHT"); |
| 219 |
flagsarray.Add("wxTOP"); |
| 220 |
flagsarray.Add("wxBOTTOM"); |
| 221 |
flagsarray = wxCrafter::MakeUnique(flagsarray); |
| 222 |
} |
| 223 |
|
| 224 |
for(size_t n = 0; n < flagsarray.GetCount(); ++n) { |
| 225 |
wrapper->EnableSizerFlag(flagsarray.Item(n), true); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("option")); // 'option' is XRC's term for proportion :/ |
| 230 |
if(propertynode) { |
| 231 |
wxString proportion = propertynode->GetNodeContent(); |
| 232 |
wrapper->SizerItem().SetProportion(wxCrafter::ToNumber(proportion, 0)); |
| 233 |
} |
| 234 |
|
| 235 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("border")); |
| 236 |
if(propertynode) { |
| 237 |
wxString border = propertynode->GetNodeContent(); |
| 238 |
wrapper->SizerItem().SetBorder(wxCrafter::ToNumber(border, 0)); |
| 239 |
} |
| 240 |
|
| 241 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("minsize")); |
| 242 |
if(propertynode) { |
| 243 |
wxString minsize = propertynode->GetNodeContent(); |
| 244 |
PropertyBase* prop = wrapper->GetProperty(PROP_MINSIZE); |
| 245 |
if(prop) { prop->SetValue(minsize); } |
| 246 |
} |
| 247 |
|
| 248 |
// wxSmith stores these individually (like wxFB), but not in a separate node like wxFB |
| 249 |
GetGridBagSizerData(node, wrapper); |
| 250 |
} |
| 251 |
|
| 252 |
void ImportFromwxSmith::GetGridBagSizerData(const wxXmlNode* node, wxcWidget* wrapper) const |
| 253 |
{ |
| 254 |
wxString row, column, rowspan("1"), colspan("1"); // wxSmith, like wxFB, holds these individually; wxC needs pairs |
| 255 |
bool hasPos(false), hasSpan(false); |
| 256 |
|
| 257 |
wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("row")); |
| 258 |
if(propertynode) { |
| 259 |
row = propertynode->GetNodeContent(); |
| 260 |
hasPos = true; |
| 261 |
} |
| 262 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("col")); |
| 263 |
if(propertynode) { |
| 264 |
column = propertynode->GetNodeContent(); |
| 265 |
hasPos = true; |
| 266 |
} |
| 267 |
|
| 268 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("rowspan")); |
| 269 |
if(propertynode) { |
| 270 |
rowspan = propertynode->GetNodeContent(); |
| 271 |
hasSpan = true; |
| 272 |
} |
| 273 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("colspan")); |
| 274 |
if(propertynode) { |
| 275 |
colspan = propertynode->GetNodeContent(); |
| 276 |
hasSpan = true; |
| 277 |
} |
| 278 |
|
| 279 |
if(hasPos) { wrapper->SetGbPos(row + ',' + column); } |
| 280 |
if(hasSpan) { wrapper->SetGbSpan(rowspan + ',' + colspan); } |
| 281 |
} |
| 282 |
|
| 283 |
void ImportFromwxSmith::GetBookitemContents(const wxXmlNode* node, NotebookPageWrapper* wrapper, int& depth) const |
| 284 |
{ |
| 285 |
wxString classname = XmlUtils::ReadString(node, wxT("class")); |
| 286 |
|
| 287 |
wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("selected")); |
| 288 |
if(propertynode) { |
| 289 |
wxString selected = propertynode->GetNodeContent(); |
| 290 |
if(selected == "1") { wrapper->SetSelected(true); } |
| 291 |
} |
| 292 |
|
| 293 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("label")); |
| 294 |
if(propertynode) { |
| 295 |
wxString label = propertynode->GetNodeContent(); |
| 296 |
PropertyBase* prop = wrapper->GetProperty(PROP_LABEL); |
| 297 |
if(prop) { prop->SetValue(label); } |
| 298 |
} |
| 299 |
|
| 300 |
if(classname != "choicebookpage") { // which don't have bitmaps |
| 301 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("bitmap")); |
| 302 |
if(propertynode) { |
| 303 |
ImportFromXrc::ProcessBitmapProperty(propertynode, wrapper, "PROP_BITMAP_PATH", "wxART_OTHER"); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
if(classname == "treebookpage") { |
| 308 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("depth")); |
| 309 |
if(propertynode) { depth = wxCrafter::ToNumber(propertynode->GetNodeContent(), 0); } |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
bool ImportFromwxSmith::GetProject() const { return false; } |