// wxr2xml.cpp: implementation of the wxr2xml class. // 8/30/00 Brian Gavin // only tested on wxMSW so far //License: wxWindows Liscense // //////////////////////////////////////////////////////////////////// /* How to use class: #include "wxr2xml.h" ... wxr2xml trans; trans->Convert("Myfile.wxr","Myfile.xml"); */ // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "wxr2xml.h" // //////////////////////////////////////////////////////////////////// // Construction/Destruction // //////////////////////////////////////////////////////////////////// wxr2xml::wxr2xml() { } wxr2xml::~wxr2xml() { } bool wxr2xml::Convert(wxString wxrfile, wxString xmlfile) { bool result; result = m_xmlfile.Open(xmlfile.c_str(), _T("w+t")); wxASSERT_MSG(result, _T("Couldn't create XML file")); if (!result) return false; result = m_table.ParseResourceFile(wxrfile); wxASSERT_MSG(result, _T("Couldn't Load WXR file")); if (!result) return false; // Write basic xml header m_xmlfile.Write(_T("\n")); m_xmlfile.Write(_T("\n")); result = ParseResources(); m_xmlfile.Write(_T("\n")); m_xmlfile.Close(); return result; } bool wxr2xml::ParseResources() { m_table.BeginFind(); wxHashTable::Node *node; node = m_table.Next(); while (node) { wxItemResource *res = (wxItemResource *) node->GetData(); wxString resType(res->GetType()); if (resType == _T("wxDialog")) ParseDialog(res); else if (resType == _T("wxPanel")) ParsePanel(res); else if (resType == _T("wxPanel")) ParsePanel(res); else if (resType == _T("wxMenu")) ParseMenuBar(res); else if (resType == _T("wxBitmap")) ParseBitmap(res); else wxLogError(_T("Found unsupported resource ") + resType); node = m_table.Next(); } return true; } void wxr2xml::ParsePanel(wxItemResource * res) { m_xmlfile.Write(_T("\t\n\n")); } void wxr2xml::ParseDialog(wxItemResource * res) { PanelStuff(res); m_xmlfile.Write(_T("\t\n\n")); } void wxr2xml::ParseControls(wxItemResource * res) { wxNode *node = res->GetChildren().GetFirst(); while (node) { wxItemResource *res = (wxItemResource *) node->GetData(); wxString resType(res->GetType()); if (resType == _T("wxButton")) ParseButton(res); else if ((resType == _T("wxTextCtrl")) | (resType == _T("wxText")) | (resType == _T("wxMultiText"))) ParseTextCtrl(res); else if (resType == _T("wxCheckBox")) ParseCheckBox(res); else if (resType == _T("wxRadioBox")) ParseRadioBox(res); else if (resType == _T("wxListBox")) ParseListBox(res); else if ((resType == _T("wxStaticText")) | (resType == _T("wxMessage"))) ParseStaticText(res); else if (resType == _T("wxChoice")) ParseChoice(res); else if (resType == _T("wxGauge")) ParseGauge(res); else if (resType == _T("wxSlider")) ParseSlider(res); else if (resType == _T("wxComboBox")) ParseComboBox(res); else if (resType == _T("wxRadioButton")) ParseRadioButton(res); else if (resType == _T("wxStaticBitmap")) ParseStaticBitmap(res); else if (resType == _T("wxScrollBar")) ParseScrollBar(res); else if ((resType == _T("wxStaticBox")) | (resType == _T("wxGroupBox"))) ParseStaticBox(res); else if (resType == _T("wxBitmapButton")) ParseBitmapButton(res); else wxLogError(_T("Found unsupported resource ") + resType); node = node->GetNext(); } } // Write out basic stuff every control has // name,position,size,bg,fg void wxr2xml::WriteControlInfo(wxItemResource * res) { m_xmlfile.Write(GenerateName(res)); m_xmlfile.Write(_T(">\n")); m_xmlfile.Write(GetPosition(res)); m_xmlfile.Write(GetSize(res)); m_xmlfile.Write(GetStyles(res)); WriteFontInfo(res); } wxString wxr2xml::GetSize(wxItemResource * res) { wxString msg; if (m_dlgunits) msg << _T("\t\t\t\t") << res->GetWidth() << _T(",") << res->GetHeight() << _T("d\n"); else msg << _T("\t\t\t\t") << res->GetWidth() << _T(",") << res->GetHeight() << _T("\n"); return msg; } wxString wxr2xml::GetPosition(wxItemResource * res) { wxString msg; if (m_dlgunits) msg << _T("\t\t\t\t") << res->GetX() << _T(",") << res->GetY() << _T("d\n"); else msg << _T("\t\t\t\t") << res->GetX() << _T(",") << res->GetY() << _T("\n"); return msg; } void wxr2xml::ParseButton(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::ParseTextCtrl(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } wxString wxr2xml::GetTitle(wxItemResource * res) { wxString msg; msg = _T("\t\t\t\t") + res->GetTitle() + _T(""); return msg; } wxString wxr2xml::GetValue4(wxItemResource * res) { wxString msg; msg = _T("\t\t\t\t") + res->GetValue4() + _T("\n"); return msg; } void wxr2xml::ParseCheckBox(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } wxString wxr2xml::GetLabel(wxItemResource * res) { return _T("\t\t\t\t\n"); } void wxr2xml::ParseRadioBox(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::ParseListBox(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::ParseStaticText(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::ParseStaticBox(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::WriteStringList(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\t\n")); for (wxStringListNode * node = res->GetStringValues().GetFirst(); node;node = node->GetNext()) { const wxString s1 = node->GetData(); m_xmlfile.Write(_T("\t\t\t\t\t") + s1 + _T("\n")); } m_xmlfile.Write(_T("\t\t\t\t\n")); } void wxr2xml::ParseChoice(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::ParseGauge(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } wxString wxr2xml::GetValue1(wxItemResource * res) { wxString msg; msg << _T("\t\t\t\t") << res->GetValue1() << _T("\n"); return msg; } wxString wxr2xml::GetRange(wxItemResource * res) { wxString msg; msg << _T("\t\t\t\t") << res->GetValue2() << _T(""); return msg; } void wxr2xml::ParseSlider(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } wxString wxr2xml::GetMax(wxItemResource * res) { wxString msg; msg << _T("\t\t\t\t") << res->GetValue3() << _T("\n"); return msg; } wxString wxr2xml::GetMin(wxItemResource * res) { wxString msg; msg << _T("\t\t\t\t") << res->GetValue2() << _T(""); return msg; } void wxr2xml::ParseComboBox(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::ParseRadioButton(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::ParseScrollBar(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\t")+GetValue2(res)+_T("\n")); m_xmlfile.Write(_T("\t\t\t\t")+GetValue3(res)+_T("\n")); m_xmlfile.Write(_T("\t\t\t\t")+GetValue5(res)+_T("\n")); m_xmlfile.Write(_T("\t\t\t\n")); } wxString wxr2xml::GetCheckStatus(wxItemResource * res) { wxString msg; msg << _T("\t\t\t\t") << res->GetValue1() << _T("\n"); return msg; } wxString wxr2xml::GetStyles(wxItemResource * res) { // Very crude way to get styles long style; wxString s, restype; restype = res->GetType(); style = res->GetStyle(); s = _T("\t\t\t\t\n"); return s; } wxString wxr2xml::GetDimension(wxItemResource * res) { wxString msg; msg << _T("\t\t\t\t") << res->GetValue1() << _T("\n"); return msg; } wxString wxr2xml::GenerateName(wxItemResource * res) { wxString name; name = _T(" name=\""); switch (res->GetId()) { case wxID_OK: name += _T("wxID_OK"); break; case wxID_CANCEL: name += _T("wxID_CANCEL"); break; default: name += res->GetName(); } name += _T("\""); return name; } void wxr2xml::ParseMenuBar(wxItemResource * res) { wxItemResource *child; wxNode *node = res->GetChildren().GetFirst(); // Get Menu Bar Name m_xmlfile.Write(_T("\t\n")); while (node) { child = (wxItemResource *) node->GetData(); ParseMenu(child); node = node->GetNext(); } m_xmlfile.Write(_T("\t \n\n")); } void wxr2xml::ParseMenu(wxItemResource * res) { wxItemResource *child; wxNode *node = res->GetChildren().GetFirst(); // Get Menu m_xmlfile.Write(_T("\t\t\tGetValue1() << _T("\""); m_xmlfile.Write(menuname); m_xmlfile.Write(_T(">\n")); m_xmlfile.Write(_T("\t\t\t\t\n")); if (res->GetValue4() != _T("")) m_xmlfile.Write(_T("\t\t\t\t") + res->GetValue4() + _T("\n")); // Read in menu items and additional menus while (node) { child = (wxItemResource *) node->GetData(); if (!child->GetChildren().GetFirst()) ParseMenuItem(child); else ParseMenu(child); node = node->GetNext(); } m_xmlfile.Write(_T("\t\t\t \n")); } void wxr2xml::ParseMenuItem(wxItemResource * res) { // Get Menu Item or Separator if (res->GetTitle() == _T("")) { m_xmlfile.Write(_T("\t\t\t\n")); } else { m_xmlfile.Write(_T("\t\t\t\tGetValue1() << _T("\""); m_xmlfile.Write(menuname); m_xmlfile.Write(_T(">\n")); m_xmlfile.Write(_T("\t\t\t\n")); if (res->GetValue4() != _T("")) m_xmlfile.Write(_T("\t\t\t") + res->GetValue4() + _T("\n")); if (res->GetValue2()) m_xmlfile.Write(_T("\t\t\t\t1\n")); m_xmlfile.Write(_T("\t\t\t \n")); } } wxString wxr2xml::FixMenuString(wxString phrase) { phrase.Replace(_T("&"), _T("$")); return phrase; } void wxr2xml::ParseStaticBitmap(wxItemResource * res) { m_xmlfile.Write(_T("\t\t\tGetValue4(); wxBitmap bitmap; bitmap = wxResourceCreateBitmap(bitmapname, &m_table); bitmapname += _T(".bmp"); bitmap.SaveFile(bitmapname, wxBITMAP_TYPE_BMP); m_xmlfile.Write(_T("\n\t\t\t\t") + bitmapname + _T("")); m_xmlfile.Write(_T("\t\t\t\n")); // bitmap5 } //Parse a bitmap resource void wxr2xml::ParseBitmap(wxItemResource * res) { m_xmlfile.Write(_T("\t")); wxString bitmapname; bitmapname = res->GetName(); wxBitmap bitmap; bitmap = wxResourceCreateBitmap(bitmapname, &m_table); bitmapname += _T(".bmp"); bitmap.SaveFile(bitmapname, wxBITMAP_TYPE_BMP); m_xmlfile.Write(bitmapname); m_xmlfile.Write(_T("\n\n")); } void wxr2xml::PanelStuff(wxItemResource * res) { if ((res->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0) m_dlgunits = true; else m_dlgunits = false; // If this is true ignore fonts, background color and use system // defaults instead if ((res->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS) != 0) m_systemdefaults = true; else m_systemdefaults = false; } wxString wxr2xml::GetValue2(wxItemResource *res) { wxString msg; msg << res->GetValue2(); return msg; } wxString wxr2xml::GetValue3(wxItemResource *res) { wxString msg; msg << res->GetValue3(); return msg; } wxString wxr2xml::GetValue5(wxItemResource *res) { wxString msg; msg << res->GetValue5(); return msg; } void wxr2xml::ParseBitmapButton(wxItemResource *res) { m_xmlfile.Write(_T("\t\t\tGetValue4(); wxBitmap bitmap; bitmap = wxResourceCreateBitmap(bitmapname, &m_table); bitmapname += _T(".bmp"); bitmap.SaveFile(bitmapname, wxBITMAP_TYPE_BMP); m_xmlfile.Write(_T("\t\t\t\t") + bitmapname + _T("\n")); m_xmlfile.Write(_T("\t\t\t\n")); } void wxr2xml::WriteFontInfo(wxItemResource *res) { //if systems_defaults true just ignore the fonts if (m_systemdefaults) return; wxFont font; font=res->GetFont(); if (!font.GetRefData()) return; m_xmlfile.Write(_T("\t\t\t\n")); //Get font point size,font family,weight,font style,underline int pt; wxString msg; pt=font.GetPointSize(); msg<<_T("\t\t\t\t")<\n"); m_xmlfile.Write(msg); GetFontFace(font); GetFontStyle(font); GetFontWeight(font); if (font.GetUnderlined()) m_xmlfile.Write(_T("\t\t\t\t1\n")); m_xmlfile.Write(_T("\t\t\t\n")); } //WARNING possible make here //I wasn't really sure the right way to do this. void wxr2xml::GetFontFace(wxFont font) { int family=font.GetFamily(); switch (family) { case wxDEFAULT: break; case wxDECORATIVE: m_xmlfile.Write(_T("\t\t\t\tdecorative\n")); break; case wxROMAN: m_xmlfile.Write(_T("\t\t\t\troman\n")); break; case wxSCRIPT: m_xmlfile.Write(_T("\t\t\t\tscript\n")); break; case wxSWISS: m_xmlfile.Write(_T("\t\t\t\tswiss\n")); break; case wxMODERN: m_xmlfile.Write(_T("\t\t\t\tmodern\n")); break; default: wxLogError(_T("Unknown font face")); } } void wxr2xml::GetFontStyle(wxFont font) { int style=font.GetStyle(); switch (style) { //since this is default no point in making file any larger case wxNORMAL: break; case wxITALIC: m_xmlfile.Write(_T("\n")); break; case wxSLANT: m_xmlfile.Write(_T("\n")); break; default: wxLogError(_T("Unknown font style")); } } void wxr2xml::GetFontWeight(wxFont font) { int weight=font.GetWeight(); switch (weight) { //since this is default no point in making file any larger case wxNORMAL: break; case wxLIGHT: m_xmlfile.Write(_T("\t\t\t\tlight\n")); break; case wxBOLD: m_xmlfile.Write(_T("\t\t\t\tbold\n")); break; default: wxLogError(_T("Unknown font weight")); } }