+#include "wx/utils.h"
+#include "wx/hashset.h"
+#include "wx/mimetype.h"
+#include "wx/vector.h"
+
+WX_DECLARE_HASH_SET(wxString, wxStringHash, wxStringEqual, StringSet);
+
+class XRCWidgetData
+{
+public:
+ XRCWidgetData(const wxString& vname,const wxString& vclass)
+ : m_class(vclass), m_name(vname) {}
+ const wxString& GetName() const { return m_name; }
+ const wxString& GetClass() const { return m_class; }
+private:
+ wxString m_class;
+ wxString m_name;
+};
+
+#include "wx/arrimpl.cpp"
+WX_DECLARE_OBJARRAY(XRCWidgetData,ArrayOfXRCWidgetData);
+WX_DEFINE_OBJARRAY(ArrayOfXRCWidgetData)
+
+class XRCWndClassData
+{
+private:
+ wxString m_className;
+ wxString m_parentClassName;
+ StringSet m_ancestorClassNames;
+ ArrayOfXRCWidgetData m_wdata;
+
+ void BrowseXmlNode(wxXmlNode* node)
+ {
+ wxString classValue;
+ wxString nameValue;
+ wxXmlNode* children;
+ while (node)
+ {
+ if (node->GetName() == _T("object")
+ && node->GetAttribute(_T("class"),&classValue)
+ && node->GetAttribute(_T("name"),&nameValue))
+ {
+ m_wdata.Add(XRCWidgetData(nameValue,classValue));
+ }
+ children = node->GetChildren();
+ if (children)
+ BrowseXmlNode(children);
+ node = node->GetNext();
+ }
+ }
+
+public:
+ XRCWndClassData(const wxString& className,
+ const wxString& parentClassName,
+ const wxXmlNode* node) :
+ m_className(className) , m_parentClassName(parentClassName)
+ {
+ if ( className == _T("wxMenu") )
+ {
+ m_ancestorClassNames.insert(_T("wxMenu"));
+ m_ancestorClassNames.insert(_T("wxMenuBar"));
+ }
+ else if ( className == _T("wxMDIChildFrame") )
+ {
+ m_ancestorClassNames.insert(_T("wxMDIParentFrame"));
+ }
+ else if( className == _T("wxMenuBar") ||
+ className == _T("wxStatusBar") ||
+ className == _T("wxToolBar") )
+ {
+ m_ancestorClassNames.insert(_T("wxFrame"));
+ }
+ else
+ {
+ m_ancestorClassNames.insert(_T("wxWindow"));
+ }
+
+ BrowseXmlNode(node->GetChildren());
+ }
+
+ const ArrayOfXRCWidgetData& GetWidgetData()
+ {
+ return m_wdata;
+ }
+
+ bool CanBeUsedWithXRCCTRL(const wxString& name)
+ {
+ if (name == _T("tool") ||
+ name == _T("data") ||
+ name == _T("unknown") ||
+ name == _T("notebookpage") ||
+ name == _T("separator") ||
+ name == _T("sizeritem") ||
+ name == _T("wxMenu") ||
+ name == _T("wxMenuBar") ||
+ name == _T("wxMenuItem") ||
+ name.EndsWith(_T("Sizer")) )
+ {
+ return false;
+ }
+ return true;
+ }
+
+ void GenerateHeaderCode(wxFFile& file)
+ {
+
+ file.Write(_T("class ") + m_className + _T(" : public ") + m_parentClassName
+ + _T(" {\nprotected:\n"));
+ size_t i;
+ for(i=0;i<m_wdata.GetCount();++i)
+ {
+ const XRCWidgetData& w = m_wdata.Item(i);
+ if( !CanBeUsedWithXRCCTRL(w.GetClass()) ) continue;
+ if( w.GetName().Length() == 0 ) continue;
+ file.Write(
+ _T(" ") + w.GetClass() + _T("* ") + w.GetName()
+ + _T(";\n"));
+ }
+ file.Write(_T("\nprivate:\n void InitWidgetsFromXRC(wxWindow *parent){\n")
+ _T(" wxXmlResource::Get()->LoadObject(this,parent,_T(\"")
+ + m_className
+ + _T("\"), _T(\"")
+ + m_parentClassName
+ + _T("\"));\n"));
+ for(i=0;i<m_wdata.GetCount();++i)
+ {
+ const XRCWidgetData& w = m_wdata.Item(i);
+ if( !CanBeUsedWithXRCCTRL(w.GetClass()) ) continue;
+ if( w.GetName().Length() == 0 ) continue;
+ file.Write( _T(" ")
+ + w.GetName()
+ + _T(" = XRCCTRL(*this,\"")
+ + w.GetName()
+ + _T("\",")
+ + w.GetClass()
+ + _T(");\n"));
+ }
+ file.Write(_T(" }\n"));
+
+ file.Write( _T("public:\n"));
+
+ if ( m_ancestorClassNames.size() == 1 )
+ {
+ file.Write
+ (
+ m_className +
+ _T("(") +
+ *m_ancestorClassNames.begin() +
+ _T(" *parent=NULL){\n") +
+ _T(" InitWidgetsFromXRC((wxWindow *)parent);\n")
+ _T(" }\n")
+ _T("};\n")
+ );
+ }
+ else
+ {
+ file.Write(m_className + _T("(){\n") +
+ _T(" InitWidgetsFromXRC(NULL);\n")
+ _T(" }\n")
+ _T("};\n"));
+
+ for ( StringSet::const_iterator it = m_ancestorClassNames.begin();
+ it != m_ancestorClassNames.end();
+ ++it )
+ {
+ file.Write(m_className + _T("(") + *it + _T(" *parent){\n") +
+ _T(" InitWidgetsFromXRC((wxWindow *)parent);\n")
+ _T(" }\n")
+ _T("};\n"));
+ }
+ }
+ }
+};
+WX_DECLARE_OBJARRAY(XRCWndClassData,ArrayOfXRCWndClassData);
+WX_DEFINE_OBJARRAY(ArrayOfXRCWndClassData)
+
+struct ExtractedString
+{
+ ExtractedString() : lineNo(-1) {}
+ ExtractedString(const wxString& str_,
+ const wxString& filename_, int lineNo_)
+ : str(str_), filename(filename_), lineNo(lineNo_)
+ {}
+
+ wxString str;
+
+ wxString filename;
+ int lineNo;
+};
+
+typedef wxVector<ExtractedString> ExtractedStrings;