1 // wxr2xml.cpp: implementation of the wxWxr2Xml class.
3 // only tested on wxMSW so far
4 //////////////////////////////////////////////////////////////////////
6 port to wxGTK should be very easy
8 add unsupported controls when XML format adds them
11 #pragma implementation "wxr2xml.h"
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
28 //////////////////////////////////////////////////////////////////////
29 // Construction/Destruction
30 //////////////////////////////////////////////////////////////////////
32 wxWxr2Xml::wxWxr2Xml()
37 wxWxr2Xml::~wxWxr2Xml()
42 bool wxWxr2Xml::Convert(wxString wxrfile
, wxString xmlfile
)
45 result
=m_xmlfile
.Open(xmlfile
.c_str(),"w+t");
46 wxASSERT_MSG(result
,"Couldn't create XML file");
50 result
=m_table
.ParseResourceFile(wxrfile
);
51 wxASSERT_MSG(result
,"Couldn't Load WXR file");
54 //Write basic xml header
55 m_xmlfile
.Write("<?xml version=\"1.0\" ?>\n");
56 m_xmlfile
.Write("<resource>\n");
57 result
=ParseResources();
58 m_xmlfile
.Write("</resource>\n");
65 bool wxWxr2Xml::ParseResources()
70 while ((node
= m_table
.Next()))
72 wxItemResource
*res
= (wxItemResource
*)node
->Data();
73 wxString
resType(res
->GetType());
74 if (resType
=="wxDialog")
76 else if (resType
=="wxPanel")
78 else if (resType
=="wxPanel")
80 else if (resType
=="wxMenu")
82 else if (resType
=="wxBitmap")
85 wxLogError("Found unsupported resource "+resType
);
91 void wxWxr2Xml::ParsePanel(wxItemResource
*res
)
94 m_xmlfile
.Write(" <panel");
96 WriteControlInfo(res
);
97 m_xmlfile
.Write("\n");
98 m_xmlfile
.Write(" <children>\n");
100 m_xmlfile
.Write(" </children>\n");
102 m_xmlfile
.Write(" </panel>\n");
106 void wxWxr2Xml::ParseDialog(wxItemResource
*res
)
109 m_xmlfile
.Write(" <dialog");
110 WriteControlInfo(res
);
111 m_xmlfile
.Write(GetTitle(res
));
114 m_xmlfile
.Write("\n");
115 m_xmlfile
.Write(" <children>\n");
117 m_xmlfile
.Write(" </children>\n");
118 m_xmlfile
.Write(" </dialog>\n");
121 void wxWxr2Xml::ParseControls(wxItemResource
*res
)
123 wxNode
*node
= res
->GetChildren().First();
126 wxItemResource
*res
= (wxItemResource
*)node
->Data();
127 wxString
resType(res
->GetType());
128 if (resType
=="wxButton")
130 else if ((resType
=="wxTextCtrl")|(resType
=="wxText")
131 |(resType
=="wxMultiText"))
133 else if (resType
=="wxCheckBox")
135 else if (resType
=="wxRadioBox")
137 else if (resType
=="wxListBox")
139 else if ((resType
=="wxStaticText")|(resType
=="wxMessage"))
140 ParseStaticText(res
);
141 else if (resType
=="wxChoice")
143 else if (resType
=="wxGauge")
145 else if (resType
=="wxSlider")
147 else if (resType
=="wxComboBox")
149 else if (resType
=="wxRadioButton")
150 ParseRadioButton(res
);
151 else if (resType
=="wxStaticBitmap")
152 ParseStaticBitmap(res
);
153 else if (resType
=="wxScrollBar")
154 wxLogError("wxScrollBar unsupported");
155 else if ((resType
=="wxStaticBox")|(resType
=="wxGroupBox"))
156 wxLogError("wxStaticBox unsupported");
157 else if (resType
=="wxBitmapButton")
158 wxLogError("wxBitmapButton unsupported");
160 wxLogError("Found unsupported resource "+resType
);
165 //Write out basic stuff every control has
166 // name,position,size,bg,fg
167 void wxWxr2Xml::WriteControlInfo(wxItemResource
*res
)
169 m_xmlfile
.Write(GenerateName(res
));
170 m_xmlfile
.Write(">\n");
171 m_xmlfile
.Write(GetPosition(res
));
172 m_xmlfile
.Write(GetSize(res
));
173 m_xmlfile
.Write(GetStyles(res
));
176 wxString
wxWxr2Xml::GetSize(wxItemResource
*res
)
180 msg
<<" <size>"<<res
->GetWidth()<<","<<res
->GetHeight()<<"d</size>";
182 msg
<<" <size>"<<res
->GetWidth()<<","<<res
->GetHeight()<<"</size>";
186 wxString
wxWxr2Xml::GetPosition(wxItemResource
*res
)
190 msg
<<" <pos>"<<res
->GetX()<<","<<res
->GetY()<<"d</pos>";
192 msg
<<" <pos>"<<res
->GetX()<<","<<res
->GetY()<<"</pos>";
196 void wxWxr2Xml::ParseButton(wxItemResource
*res
)
198 m_xmlfile
.Write(" <button");
199 WriteControlInfo(res
);
200 m_xmlfile
.Write(GetLabel(res
));
201 m_xmlfile
.Write("</button>\n");
204 void wxWxr2Xml::ParseTextCtrl(wxItemResource
*res
)
206 m_xmlfile
.Write(" <textctrl");
207 WriteControlInfo(res
);
208 m_xmlfile
.Write(GetValue4(res
));
209 m_xmlfile
.Write("</textctrl>\n");
213 wxString
wxWxr2Xml::GetTitle(wxItemResource
*res
)
216 msg
=_T(" <title>"+res
->GetTitle()+"</title>");
220 wxString
wxWxr2Xml::GetValue4(wxItemResource
*res
)
223 msg
=_T(" <value>"+res
->GetValue4()+"</value>");
227 void wxWxr2Xml::ParseCheckBox(wxItemResource
*res
)
229 m_xmlfile
.Write(" <checkbox");
230 WriteControlInfo(res
);
231 m_xmlfile
.Write(GetLabel(res
));
232 m_xmlfile
.Write(GetCheckStatus(res
));
233 m_xmlfile
.Write("</checkbox>\n");
236 wxString
wxWxr2Xml::GetLabel(wxItemResource
*res
)
238 return _T(" <label>"+res
->GetTitle()+"</label>");
241 void wxWxr2Xml::ParseRadioBox(wxItemResource
*res
)
243 m_xmlfile
.Write(" <radiobox");
244 WriteControlInfo(res
);
245 m_xmlfile
.Write(GetLabel(res
));
246 //Add radio box items
247 WriteStringList(res
);
249 m_xmlfile
.Write(GetDimension(res
));
250 m_xmlfile
.Write("\n </radiobox>\n");
253 void wxWxr2Xml::ParseListBox(wxItemResource
*res
)
255 m_xmlfile
.Write(" <listbox");
256 WriteControlInfo(res
);
257 WriteStringList(res
);
258 m_xmlfile
.Write("</listbox>\n");
261 void wxWxr2Xml::ParseStaticText(wxItemResource
*res
)
263 m_xmlfile
.Write(" <statictext");
264 WriteControlInfo(res
);
265 m_xmlfile
.Write(GetLabel(res
));
266 m_xmlfile
.Write("</statictext>\n");
269 void wxWxr2Xml::ParseStaticBox(wxItemResource
*res
)
271 m_xmlfile
.Write(" <staticbox");
272 WriteControlInfo(res
);
273 m_xmlfile
.Write(GetLabel(res
));
274 m_xmlfile
.Write("</staticbox>\n");
277 void wxWxr2Xml::WriteStringList(wxItemResource
*res
)
279 m_xmlfile
.Write("\n <content>");
280 for ( wxStringListNode
*node
= res
->GetStringValues().GetFirst(); node
;
281 node
= node
->GetNext() )
283 const wxString s1
= node
->GetData();
284 m_xmlfile
.Write("\n <item>"+s1
+"</item>");
286 m_xmlfile
.Write("\n </content>");
289 void wxWxr2Xml::ParseChoice(wxItemResource
*res
)
291 m_xmlfile
.Write(" <choice");
292 WriteControlInfo(res
);
294 WriteStringList(res
);
295 m_xmlfile
.Write("\n </choice>\n");
298 void wxWxr2Xml::ParseGauge(wxItemResource
*res
)
300 m_xmlfile
.Write(" <gauge");
301 WriteControlInfo(res
);
302 m_xmlfile
.Write(GetValue1(res
));
303 m_xmlfile
.Write(GetRange(res
));
304 m_xmlfile
.Write("\n </gauge>\n");
308 wxString
wxWxr2Xml::GetValue1(wxItemResource
*res
)
311 msg
<<" <value>"<<res
->GetValue1()<<"</value>";
315 wxString
wxWxr2Xml::GetRange(wxItemResource
*res
)
318 msg
<<" <range>"<<res
->GetValue2()<<"</range>";
322 void wxWxr2Xml::ParseSlider(wxItemResource
*res
)
324 m_xmlfile
.Write(" <slider");
325 WriteControlInfo(res
);
326 m_xmlfile
.Write(GetValue1(res
));
327 m_xmlfile
.Write(GetMax(res
));
328 m_xmlfile
.Write(GetMin(res
));
329 m_xmlfile
.Write("\n </slider>\n");
332 wxString
wxWxr2Xml::GetMax(wxItemResource
*res
)
335 msg
<<" <max>"<<res
->GetValue3()<<"</max>";
339 wxString
wxWxr2Xml::GetMin(wxItemResource
*res
)
342 msg
<<" <min>"<<res
->GetValue2()<<"</min>";
346 void wxWxr2Xml::ParseComboBox(wxItemResource
*res
)
348 m_xmlfile
.Write(" <combobox");
349 WriteControlInfo(res
);
351 WriteStringList(res
);
352 m_xmlfile
.Write("\n </combobox>\n");
355 void wxWxr2Xml::ParseRadioButton(wxItemResource
*res
)
357 m_xmlfile
.Write(" <radiobutton");
358 WriteControlInfo(res
);
359 m_xmlfile
.Write(GetLabel(res
));
362 m_xmlfile
.Write(GetValue1(res
));
363 m_xmlfile
.Write(GetCheckStatus(res
));
364 m_xmlfile
.Write("\n </radiobutton>\n");
367 void wxWxr2Xml::ParseScrollBar(wxItemResource
*res
)
369 m_xmlfile
.Write(" <scrollbar");
370 WriteControlInfo(res
);
371 //TODO learn more about XML scrollbar format
372 m_xmlfile
.Write("\n </scrollbar>\n");
375 wxString
wxWxr2Xml::GetCheckStatus(wxItemResource
*res
)
378 msg
<<" <checked>"<<res
->GetValue1()<<"</checked>";
382 wxString
wxWxr2Xml::GetStyles(wxItemResource
*res
)
384 //Very crude way to get styles
387 restype
=res
->GetType();
388 style
=res
->GetStyle();
392 //Common styles for all controls
393 if (style
&wxSIMPLE_BORDER
)
394 s
+="wxSIMPLE_BORDER|";
395 if (style
&wxSUNKEN_BORDER
)
396 s
+="wxSUNKEN_BORDER|";
397 if (style
&wxSIMPLE_BORDER
)
398 s
+="wxSIMPLE_BORDER|";
399 if (style
&wxDOUBLE_BORDER
)
400 s
+="wxDOUBLE_BORDER|";
401 if (style
&wxRAISED_BORDER
)
402 s
+="wxRAISED_BORDER|";
403 if (style
&wxTRANSPARENT_WINDOW
)
404 s
+="wxTRANSPARENT_WINDOW|";
405 if (style
&wxWANTS_CHARS
)
407 if (style
&wxNO_FULL_REPAINT_ON_RESIZE
)
408 s
+="wxNO_FULL_REPAINT_ON_RESIZE|";
410 if (restype
=="wxDialog")
412 if (style
&wxDIALOG_MODAL
)
413 s
+="wxDIALOG_MODAL|";
414 if (style
&wxDEFAULT_DIALOG_STYLE
)
415 s
+="wxDEFAULT_DIALOG_STYLE|";
416 if (style
&wxDIALOG_MODELESS
)
417 s
+="wxDIALOG_MODELESS|";
420 if (style
&wxTAB_TRAVERSAL
)
421 s
+="wxTAB_TRAVERSAL|";
422 if (style
&wxWS_EX_VALIDATE_RECURSIVELY
)
423 s
+="wxWS_EX_VALIDATE_RECURSIVELY|";
424 if (style
&wxSTAY_ON_TOP
)
428 if (style
&wxTHICK_FRAME
)
430 if (style
&wxRESIZE_BOX
)
432 if (style
&wxRESIZE_BORDER
)
433 s
+="wxRESIZE_BORDER|";
434 if (style
&wxSYSTEM_MENU
)
436 if (style
&wxCLIP_CHILDREN
)
437 s
+="wxCLIP_CHILDREN|";
444 if (restype
=="wxPanel")
446 if (style
&wxCLIP_CHILDREN
)
447 s
+="wxCLIP_CHILDREN|";
450 if (style
&wxTAB_TRAVERSAL
)
451 s
+="wxTAB_TRAVERSAL|";
452 if (style
&wxWS_EX_VALIDATE_RECURSIVELY
)
453 s
+="wxWS_EX_VALIDATE_RECURSIVELY|";
456 if (restype
=="wxComboBox")
460 if (style
&wxCB_SIMPLE
)
462 if (style
&wxCB_READONLY
)
464 if (style
&wxCB_DROPDOWN
)
468 if (restype
=="wxGauge")
470 if (style
&wxGA_HORIZONTAL
)
471 s
+="wxGA_HORIZONTAL|";
472 if (style
&wxGA_VERTICAL
)
474 if (style
&wxGA_PROGRESSBAR
)
475 s
+="wxGA_PROGRESSBAR|";
477 if (style
&wxGA_SMOOTH
)
481 if (restype
=="wxRadioButton")
483 if (style
&wxRB_GROUP
)
487 if (restype
=="wxStaticText")
489 if (style
&wxST_NO_AUTORESIZE
)
490 s
+="wxST_NO_AUTORESIZEL|";
493 if (restype
=="wxRadioBox")
495 if (style
&wxRA_HORIZONTAL
)
496 s
+="wxRA_HORIZONTAL|";
497 if (style
&wxRA_SPECIFY_COLS
)
498 s
+="wxRA_SPECIFY_COLS|";
499 if (style
&wxRA_SPECIFY_ROWS
)
500 s
+="wxRA_SPECIFY_ROWS|";
501 if (style
&wxRA_VERTICAL
)
505 if (restype
=="wxListBox")
507 if (style
&wxLB_SINGLE
)
509 if (style
&wxLB_MULTIPLE
)
511 if (style
&wxLB_EXTENDED
)
513 if (style
&wxLB_HSCROLL
)
515 if (style
&wxLB_ALWAYS_SB
)
516 s
+="wxLB_ALWAYS_SB|";
517 if (style
&wxLB_NEEDED_SB
)
518 s
+="wxLB_NEEDED_SB|";
523 if (restype
=="wxTextCtrl")
525 if (style
&wxTE_PROCESS_ENTER
)
526 s
+="wxTE_PROCESS_ENTER|";
527 if (style
&wxTE_PROCESS_TAB
)
528 s
+="wxTE_PROCESS_TAB|";
529 if (style
&wxTE_MULTILINE
)
530 s
+="wxTE_MULTILINE|";
531 if (style
&wxTE_PASSWORD
)
533 if (style
&wxTE_READONLY
)
551 wxString
wxWxr2Xml::GetDimension(wxItemResource
*res
)
554 msg
<<" <dimension>"<<res
->GetValue1()<<"</dimension>";
558 wxString
wxWxr2Xml::GenerateName(wxItemResource
*res
)
562 switch (res
->GetId())
568 name
+=_T("wxID_CANCEL");
571 name
+=res
->GetName();
578 void wxWxr2Xml::ParseMenuBar(wxItemResource
*res
)
580 wxItemResource
*child
;
581 wxNode
*node
= res
->GetChildren().First();
583 m_xmlfile
.Write("<menubar ");
584 m_xmlfile
.Write(GenerateName(res
));
585 m_xmlfile
.Write(">\n");
586 m_xmlfile
.Write(" <children>\n");
589 child
= (wxItemResource
*)node
->Data();
594 m_xmlfile
.Write(" </children>\n");
595 m_xmlfile
.Write("</menubar> \n");
599 void wxWxr2Xml::ParseMenu(wxItemResource
*res
)
601 wxItemResource
*child
;
602 wxNode
*node
= res
->GetChildren().First();
604 m_xmlfile
.Write(" <menu ");
606 menuname
<<"name = \"menu_"<<res
->GetValue1()<<"\"";
607 m_xmlfile
.Write(menuname
);
608 m_xmlfile
.Write(">\n");
609 m_xmlfile
.Write(" <label>"
610 +FixMenuString(res
->GetTitle())+"</label>\n");
611 if (res
->GetValue4()!="")
612 m_xmlfile
.Write(" <help>"+res
->GetValue4()+"</help>\n");
613 m_xmlfile
.Write(" <children>\n");
614 //Read in menu items and additional menus
617 child
= (wxItemResource
*)node
->Data();
618 if (!child
->GetChildren().First())
619 ParseMenuItem(child
);
625 m_xmlfile
.Write(" </children>\n");
626 m_xmlfile
.Write(" </menu> \n");
629 void wxWxr2Xml::ParseMenuItem(wxItemResource
*res
)
631 //Get Menu Item or Separator
632 if (res
->GetTitle()=="")
634 m_xmlfile
.Write(" <separator/>\n");
638 m_xmlfile
.Write(" <menuitem ");
640 menuname
<<"name = \"menuitem_"<<res
->GetValue1()<<"\"";
641 m_xmlfile
.Write(menuname
);
642 m_xmlfile
.Write(">\n");
643 m_xmlfile
.Write(" <label>"
644 +FixMenuString(res
->GetTitle())+"</label>\n");
645 if (res
->GetValue4()!="")
646 m_xmlfile
.Write(" <help>"+res
->GetValue4()+"</help>\n");
647 if (res
->GetValue2())
648 m_xmlfile
.Write(" <checkable>1</checkable>\n");
649 m_xmlfile
.Write(" </menuitem> \n");
653 wxString
wxWxr2Xml::FixMenuString(wxString phrase
)
655 phrase
.Replace("&","$");
659 void wxWxr2Xml::ParseStaticBitmap(wxItemResource
*res
)
661 m_xmlfile
.Write(" <staticbitmap");
662 WriteControlInfo(res
);
663 //value4 holds bitmap name
665 bitmapname
=res
->GetValue4();
667 bitmap
= wxResourceCreateBitmap(bitmapname
,&m_table
);
668 bitmapname
+=_T(".bmp");
669 bitmap
.SaveFile(bitmapname
,wxBITMAP_TYPE_BMP
);
670 m_xmlfile
.Write("\n <bitmap>"+bitmapname
+"</bitmap>");
671 m_xmlfile
.Write("</staticbitmap>\n");
675 void wxWxr2Xml::ParseBitmap(wxItemResource
*res
)
680 void wxWxr2Xml::PanelStuff(wxItemResource
*res
)
682 if ((res
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) != 0)
687 //If this is true ignore fonts, background color and use system
689 if ((res
->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS
) != 0)
690 m_systemdefaults
=TRUE
;
692 m_systemdefaults
=FALSE
;