| 1 |
#ifndef WXRC_H |
| 2 |
#define WXRC_H |
| 3 |
|
| 4 |
#include <wx/cmdline.h> |
| 5 |
#include <wx/ffile.h> |
| 6 |
#include <wx/filename.h> |
| 7 |
#include <wx/hashset.h> |
| 8 |
#include <wx/mimetype.h> |
| 9 |
#include <wx/utils.h> |
| 10 |
#include <wx/vector.h> |
| 11 |
#include <wx/wfstream.h> |
| 12 |
#include <wx/xml/xml.h> |
| 13 |
|
| 14 |
WX_DECLARE_HASH_SET(wxString, ::wxStringHash, ::wxStringEqual, StringSet); |
| 15 |
|
| 16 |
class XRCWidgetData |
| 17 |
{ |
| 18 |
public: |
| 19 |
XRCWidgetData(const wxString& vname, const wxString& vclass) |
| 20 |
: m_class(vclass) |
| 21 |
, m_name(vname) |
| 22 |
{ |
| 23 |
} |
| 24 |
const wxString& GetName() const { return m_name; } |
| 25 |
const wxString& GetClass() const { return m_class; } |
| 26 |
|
| 27 |
private: |
| 28 |
wxString m_class; |
| 29 |
wxString m_name; |
| 30 |
}; |
| 31 |
WX_DECLARE_OBJARRAY(XRCWidgetData, ArrayOfXRCWidgetData); |
| 32 |
|
| 33 |
struct ExtractedString { |
| 34 |
ExtractedString() |
| 35 |
: lineNo(-1) |
| 36 |
{ |
| 37 |
} |
| 38 |
ExtractedString(const wxString& str_, const wxString& filename_, int lineNo_) |
| 39 |
: str(str_) |
| 40 |
, filename(filename_) |
| 41 |
, lineNo(lineNo_) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
wxString str; |
| 46 |
|
| 47 |
wxString filename; |
| 48 |
int lineNo; |
| 49 |
}; |
| 50 |
typedef wxVector<ExtractedString> ExtractedStrings; |
| 51 |
|
| 52 |
class XRCWndClassData |
| 53 |
{ |
| 54 |
private: |
| 55 |
wxString m_className; |
| 56 |
wxString m_parentClassName; |
| 57 |
StringSet m_ancestorClassNames; |
| 58 |
ArrayOfXRCWidgetData m_wdata; |
| 59 |
|
| 60 |
void BrowseXmlNode(wxXmlNode* node) |
| 61 |
{ |
| 62 |
wxString classValue; |
| 63 |
wxString nameValue; |
| 64 |
wxXmlNode* children; |
| 65 |
while(node) { |
| 66 |
if(node->GetName() == wxT("object") && node->GetAttribute(wxT("class"), &classValue) && |
| 67 |
node->GetAttribute(wxT("name"), &nameValue)) { |
| 68 |
m_wdata.Add(XRCWidgetData(nameValue, classValue)); |
| 69 |
} |
| 70 |
children = node->GetChildren(); |
| 71 |
if(children) BrowseXmlNode(children); |
| 72 |
node = node->GetNext(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public: |
| 77 |
XRCWndClassData(const wxString& className, const wxString& parentClassName, const wxXmlNode* node) |
| 78 |
: m_className(className) |
| 79 |
, m_parentClassName(parentClassName) |
| 80 |
{ |
| 81 |
if(className == wxT("wxMenu")) { |
| 82 |
m_ancestorClassNames.insert(wxT("wxMenu")); |
| 83 |
m_ancestorClassNames.insert(wxT("wxMenuBar")); |
| 84 |
} else if(className == wxT("wxMDIChildFrame")) { |
| 85 |
m_ancestorClassNames.insert(wxT("wxMDIParentFrame")); |
| 86 |
} else if(className == wxT("wxMenuBar") || className == wxT("wxStatusBar") || className == wxT("wxToolBar")) { |
| 87 |
m_ancestorClassNames.insert(wxT("wxFrame")); |
| 88 |
} else { |
| 89 |
m_ancestorClassNames.insert(wxT("wxWindow")); |
| 90 |
} |
| 91 |
|
| 92 |
BrowseXmlNode(node->GetChildren()); |
| 93 |
} |
| 94 |
|
| 95 |
const ArrayOfXRCWidgetData& GetWidgetData() { return m_wdata; } |
| 96 |
|
| 97 |
bool CanBeUsedWithXRCCTRL(const wxString& name) |
| 98 |
{ |
| 99 |
if(name == wxT("tool") || name == wxT("data") || name == wxT("unknown") || name == wxT("notebookpage") || |
| 100 |
name == wxT("separator") || name == wxT("sizeritem") || name == wxT("wxMenu") || name == wxT("wxMenuBar") || |
| 101 |
name == wxT("wxMenuItem") || name.EndsWith(wxT("Sizer"))) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
void GenerateHeaderCode(wxFFile& file) |
| 108 |
{ |
| 109 |
|
| 110 |
file.Write(wxT("class ") + m_className + wxT(" : public ") + m_parentClassName + wxT(" {\nprotected:\n")); |
| 111 |
size_t i; |
| 112 |
for(i = 0; i < m_wdata.GetCount(); ++i) { |
| 113 |
const XRCWidgetData& w = m_wdata.Item(i); |
| 114 |
if(!CanBeUsedWithXRCCTRL(w.GetClass())) continue; |
| 115 |
if(w.GetName().empty()) continue; |
| 116 |
file.Write(wxT(" ") + w.GetClass() + wxT("* ") + w.GetName() + wxT(";\n")); |
| 117 |
} |
| 118 |
file.Write(wxT("\nprivate:\n void InitWidgetsFromXRC(wxWindow *parent){\n") |
| 119 |
wxT(" wxXmlResource::Get()->LoadObject(this,parent,wxT(\"") + |
| 120 |
m_className + wxT("\"), wxT(\"") + m_parentClassName + wxT("\"));\n")); |
| 121 |
for(i = 0; i < m_wdata.GetCount(); ++i) { |
| 122 |
const XRCWidgetData& w = m_wdata.Item(i); |
| 123 |
if(!CanBeUsedWithXRCCTRL(w.GetClass())) continue; |
| 124 |
if(w.GetName().empty()) continue; |
| 125 |
file.Write(wxT(" ") + w.GetName() + wxT(" = XRCCTRL(*this,\"") + w.GetName() + wxT("\",") + w.GetClass() + |
| 126 |
wxT(");\n")); |
| 127 |
} |
| 128 |
file.Write(wxT(" }\n")); |
| 129 |
|
| 130 |
file.Write(wxT("public:\n")); |
| 131 |
|
| 132 |
if(m_ancestorClassNames.size() == 1) { |
| 133 |
file.Write(m_className + wxT("(") + *m_ancestorClassNames.begin() + wxT(" *parent=NULL){\n") + |
| 134 |
wxT(" InitWidgetsFromXRC((wxWindow *)parent);\n") wxT(" }\n") wxT("};\n")); |
| 135 |
} else { |
| 136 |
file.Write(m_className + wxT("(){\n") + wxT(" InitWidgetsFromXRC(NULL);\n") wxT(" }\n") wxT("};\n")); |
| 137 |
|
| 138 |
for(StringSet::const_iterator it = m_ancestorClassNames.begin(); it != m_ancestorClassNames.end(); ++it) { |
| 139 |
file.Write(m_className + wxT("(") + *it + wxT(" *parent){\n") + |
| 140 |
wxT(" InitWidgetsFromXRC((wxWindow *)parent);\n") wxT(" }\n") wxT("};\n")); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
WX_DECLARE_OBJARRAY(XRCWndClassData, ArrayOfXRCWndClassData); |
| 147 |
|
| 148 |
class wxcXmlResourceCmp |
| 149 |
{ |
| 150 |
wxString m_outputCppFile; |
| 151 |
wxString m_functionName; |
| 152 |
wxString m_xrcFile; |
| 153 |
int m_retCode; |
| 154 |
wxString m_outputPath; |
| 155 |
|
| 156 |
public: |
| 157 |
// don't use builtin cmd line parsing: |
| 158 |
virtual bool OnInit() { return true; } |
| 159 |
virtual int Run(const wxString& inXrcFile, const wxString& outputCppFile, const wxString& functionName); |
| 160 |
|
| 161 |
private: |
| 162 |
void CompileRes(); |
| 163 |
wxArrayString PrepareTempFiles(); |
| 164 |
void FindFilesInXML(wxXmlNode* node, wxArrayString& flist, const wxString& inputPath); |
| 165 |
|
| 166 |
wxString GetInternalFileName(const wxString& name, const wxArrayString& flist); |
| 167 |
void DeleteTempFiles(const wxArrayString& flist); |
| 168 |
void MakePackageCPP(const wxArrayString& flist); |
| 169 |
void MakePackagePython(const wxArrayString& flist); |
| 170 |
|
| 171 |
void OutputGettext(); |
| 172 |
ExtractedStrings FindStrings(); |
| 173 |
ExtractedStrings FindStrings(const wxString& filename, wxXmlNode* node); |
| 174 |
|
| 175 |
ArrayOfXRCWndClassData aXRCWndClassData; |
| 176 |
void GenCPPHeader(); |
| 177 |
}; |
| 178 |
#endif |