]> git.saurik.com Git - wxWidgets.git/blame - contrib/utils/convertrc/wxr2xml.cpp
fixed bug in wxHtmlWindow: HistoryBack/Forward now correctly preserves last entry...
[wxWidgets.git] / contrib / utils / convertrc / wxr2xml.cpp
CommitLineData
88d42654
VS
1// wxr2xml.cpp: implementation of the wxWxr2Xml class.
2// 8/30/00 Brian Gavin
3// only tested on wxMSW so far
4//////////////////////////////////////////////////////////////////////
5/* TODO
6 port to wxGTK should be very easy
7 support fonts
8 add unsupported controls when XML format adds them
9*/
10#ifdef __GNUG__
11#pragma implementation "wxr2xml.h"
12#endif
13
14// For compilers that support precompilation, includes "wx/wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18#pragma hdrstop
19#endif
20
21#ifndef WX_PRECOMP
22#include "wx/wx.h"
23#endif
24
25#include "wxr2xml.h"
26
27
28//////////////////////////////////////////////////////////////////////
29// Construction/Destruction
30//////////////////////////////////////////////////////////////////////
31
32wxWxr2Xml::wxWxr2Xml()
33{
34
35}
36
37wxWxr2Xml::~wxWxr2Xml()
38{
39
40}
41
42bool wxWxr2Xml::Convert(wxString wxrfile, wxString xmlfile)
43{
44bool result;
45result=m_xmlfile.Open(xmlfile.c_str(),"w+t");
46wxASSERT_MSG(result,"Couldn't create XML file");
47if (!result)
48 return FALSE;
49
50result=m_table.ParseResourceFile(wxrfile);
51wxASSERT_MSG(result,"Couldn't Load WXR file");
52if (!result)
53 return FALSE;
54//Write basic xml header
55m_xmlfile.Write("<?xml version=\"1.0\" ?>\n");
56m_xmlfile.Write("<resource>\n");
57result=ParseResources();
58m_xmlfile.Write("</resource>\n");
59
60m_xmlfile.Close();
61
62return result;
63}
64
65bool wxWxr2Xml::ParseResources()
66{
67m_table.BeginFind();
68wxNode *node;
69
70while ((node = m_table.Next()))
71 {
72 wxItemResource *res = (wxItemResource *)node->Data();
73 wxString resType(res->GetType());
74 if (resType=="wxDialog")
75 ParseDialog(res);
76 else if (resType=="wxPanel")
77 ParsePanel(res);
78 else if (resType=="wxPanel")
79 ParsePanel(res);
80 else if (resType=="wxMenu")
81 ParseMenuBar(res);
82 else if (resType=="wxBitmap")
83 ParseBitmap(res);
84 else
85 wxLogError("Found unsupported resource "+resType);
86 }
87
88return TRUE;
89}
90
91void wxWxr2Xml::ParsePanel(wxItemResource *res)
92{
93
94m_xmlfile.Write(" <panel");
95PanelStuff(res);
96WriteControlInfo(res);
97m_xmlfile.Write("\n");
98m_xmlfile.Write(" <children>\n");
99ParseControls(res);
100m_xmlfile.Write(" </children>\n");
101
102m_xmlfile.Write(" </panel>\n");
103}
104
105
106void wxWxr2Xml::ParseDialog(wxItemResource *res)
107{
108PanelStuff(res);
109m_xmlfile.Write(" <dialog");
110WriteControlInfo(res);
111m_xmlfile.Write(GetTitle(res));
112
113
114m_xmlfile.Write("\n");
115m_xmlfile.Write(" <children>\n");
116ParseControls(res);
117m_xmlfile.Write(" </children>\n");
118m_xmlfile.Write(" </dialog>\n");
119}
120
121void wxWxr2Xml::ParseControls(wxItemResource *res)
122{
123wxNode *node = res->GetChildren().First();
124while (node)
125 {
126 wxItemResource *res = (wxItemResource *)node->Data();
127 wxString resType(res->GetType());
128 if (resType=="wxButton")
129 ParseButton(res);
130 else if ((resType=="wxTextCtrl")|(resType=="wxText")
131 |(resType=="wxMultiText"))
132 ParseTextCtrl(res);
133 else if (resType=="wxCheckBox")
134 ParseCheckBox(res);
135 else if (resType=="wxRadioBox")
136 ParseRadioBox(res);
137 else if (resType=="wxListBox")
138 ParseListBox(res);
139 else if ((resType=="wxStaticText")|(resType=="wxMessage"))
140 ParseStaticText(res);
141 else if (resType=="wxChoice")
142 ParseChoice(res);
143 else if (resType=="wxGauge")
144 ParseGauge(res);
145 else if (resType=="wxSlider")
146 ParseSlider(res);
147 else if (resType=="wxComboBox")
148 ParseComboBox(res);
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");
159 else
160 wxLogError("Found unsupported resource "+resType);
161 node = node->Next();
162 }
163}
164
165//Write out basic stuff every control has
166// name,position,size,bg,fg
167void wxWxr2Xml::WriteControlInfo(wxItemResource *res)
168{
169m_xmlfile.Write(GenerateName(res));
170m_xmlfile.Write(">\n");
171m_xmlfile.Write(GetPosition(res));
172m_xmlfile.Write(GetSize(res));
173m_xmlfile.Write(GetStyles(res));
174}
175
176wxString wxWxr2Xml::GetSize(wxItemResource *res)
177{
178wxString msg;
179if (m_dlgunits)
180 msg<<" <size>"<<res->GetWidth()<<","<<res->GetHeight()<<"d</size>";
181else
182 msg<<" <size>"<<res->GetWidth()<<","<<res->GetHeight()<<"</size>";
183return msg;
184}
185
186wxString wxWxr2Xml::GetPosition(wxItemResource *res)
187{
188wxString msg;
189if (m_dlgunits)
190 msg<<" <pos>"<<res->GetX()<<","<<res->GetY()<<"d</pos>";
191else
192 msg<<" <pos>"<<res->GetX()<<","<<res->GetY()<<"</pos>";
193return msg;
194}
195
196void wxWxr2Xml::ParseButton(wxItemResource *res)
197{
198m_xmlfile.Write(" <button");
199WriteControlInfo(res);
200m_xmlfile.Write(GetLabel(res));
201m_xmlfile.Write("</button>\n");
202}
203
204void wxWxr2Xml::ParseTextCtrl(wxItemResource *res)
205{
206m_xmlfile.Write(" <textctrl");
207WriteControlInfo(res);
208m_xmlfile.Write(GetValue4(res));
209m_xmlfile.Write("</textctrl>\n");
210
211}
212
213wxString wxWxr2Xml::GetTitle(wxItemResource *res)
214{
215wxString msg;
216msg=_T(" <title>"+res->GetTitle()+"</title>");
217return msg;
218}
219
220wxString wxWxr2Xml::GetValue4(wxItemResource *res)
221{
222wxString msg;
223msg=_T(" <value>"+res->GetValue4()+"</value>");
224return msg;
225}
226
227void wxWxr2Xml::ParseCheckBox(wxItemResource *res)
228{
229m_xmlfile.Write(" <checkbox");
230WriteControlInfo(res);
231m_xmlfile.Write(GetLabel(res));
232m_xmlfile.Write(GetCheckStatus(res));
233m_xmlfile.Write("</checkbox>\n");
234}
235
236wxString wxWxr2Xml::GetLabel(wxItemResource *res)
237{
238return _T(" <label>"+res->GetTitle()+"</label>");
239}
240
241void wxWxr2Xml::ParseRadioBox(wxItemResource *res)
242{
243m_xmlfile.Write(" <radiobox");
244WriteControlInfo(res);
245m_xmlfile.Write(GetLabel(res));
246//Add radio box items
247WriteStringList(res);
248//Value1
249m_xmlfile.Write(GetDimension(res));
250m_xmlfile.Write("\n </radiobox>\n");
251}
252
253void wxWxr2Xml::ParseListBox(wxItemResource *res)
254{
255m_xmlfile.Write(" <listbox");
256WriteControlInfo(res);
257WriteStringList(res);
258m_xmlfile.Write("</listbox>\n");
259}
260
261void wxWxr2Xml::ParseStaticText(wxItemResource *res)
262{
263m_xmlfile.Write(" <statictext");
264WriteControlInfo(res);
265m_xmlfile.Write(GetLabel(res));
266m_xmlfile.Write("</statictext>\n");
267}
268
269void wxWxr2Xml::ParseStaticBox(wxItemResource *res)
270{
271m_xmlfile.Write(" <staticbox");
272WriteControlInfo(res);
273m_xmlfile.Write(GetLabel(res));
274m_xmlfile.Write("</staticbox>\n");
275}
276
277void wxWxr2Xml::WriteStringList(wxItemResource *res)
278{
279m_xmlfile.Write("\n <content>");
280for ( wxStringListNode *node = res->GetStringValues().GetFirst(); node;
281 node = node->GetNext() )
282 {
283 const wxString s1 = node->GetData();
284 m_xmlfile.Write("\n <item>"+s1+"</item>");
285 }
286 m_xmlfile.Write("\n </content>");
287}
288
289void wxWxr2Xml::ParseChoice(wxItemResource *res)
290{
291m_xmlfile.Write(" <choice");
292WriteControlInfo(res);
293//Add choice items
294WriteStringList(res);
295m_xmlfile.Write("\n </choice>\n");
296}
297
298void wxWxr2Xml::ParseGauge(wxItemResource *res)
299{
300m_xmlfile.Write(" <gauge");
301WriteControlInfo(res);
302m_xmlfile.Write(GetValue1(res));
303m_xmlfile.Write(GetRange(res));
304m_xmlfile.Write("\n </gauge>\n");
305}
306
307
308wxString wxWxr2Xml::GetValue1(wxItemResource *res)
309{
310wxString msg;
311msg<<" <value>"<<res->GetValue1()<<"</value>";
312return msg;
313}
314
315wxString wxWxr2Xml::GetRange(wxItemResource *res)
316{
317wxString msg;
318msg<<" <range>"<<res->GetValue2()<<"</range>";
319return msg;
320}
321
322void wxWxr2Xml::ParseSlider(wxItemResource *res)
323{
324m_xmlfile.Write(" <slider");
325WriteControlInfo(res);
326m_xmlfile.Write(GetValue1(res));
327m_xmlfile.Write(GetMax(res));
328m_xmlfile.Write(GetMin(res));
329m_xmlfile.Write("\n </slider>\n");
330}
331
332wxString wxWxr2Xml::GetMax(wxItemResource *res)
333{
334wxString msg;
335msg<<" <max>"<<res->GetValue3()<<"</max>";
336return msg;
337}
338
339wxString wxWxr2Xml::GetMin(wxItemResource *res)
340{
341wxString msg;
342msg<<" <min>"<<res->GetValue2()<<"</min>";
343return msg;
344}
345
346void wxWxr2Xml::ParseComboBox(wxItemResource *res)
347{
348m_xmlfile.Write(" <combobox");
349WriteControlInfo(res);
350//Add combo items
351WriteStringList(res);
352m_xmlfile.Write("\n </combobox>\n");
353}
354
355void wxWxr2Xml::ParseRadioButton(wxItemResource *res)
356{
357m_xmlfile.Write(" <radiobutton");
358WriteControlInfo(res);
359m_xmlfile.Write(GetLabel(res));
360
361wxString msg;
362m_xmlfile.Write(GetValue1(res));
363m_xmlfile.Write(GetCheckStatus(res));
364m_xmlfile.Write("\n </radiobutton>\n");
365}
366
367void wxWxr2Xml::ParseScrollBar(wxItemResource *res)
368{
369m_xmlfile.Write(" <scrollbar");
370WriteControlInfo(res);
371//TODO learn more about XML scrollbar format
372m_xmlfile.Write("\n </scrollbar>\n");
373}
374
375wxString wxWxr2Xml::GetCheckStatus(wxItemResource *res)
376{
377wxString msg;
378msg<<" <checked>"<<res->GetValue1()<<"</checked>";
379return msg;
380}
381
382wxString wxWxr2Xml::GetStyles(wxItemResource *res)
383{
384//Very crude way to get styles
385long style;
386wxString s,restype;
387restype=res->GetType();
388style=res->GetStyle();
389
390s="<style>";
391
392//Common styles for all controls
393if (style&wxSIMPLE_BORDER)
394 s+="wxSIMPLE_BORDER|";
395if (style&wxSUNKEN_BORDER)
396 s+="wxSUNKEN_BORDER|";
397if (style&wxSIMPLE_BORDER)
398 s+="wxSIMPLE_BORDER|";
399if (style&wxDOUBLE_BORDER)
400 s+="wxDOUBLE_BORDER|";
401if (style&wxRAISED_BORDER)
402 s+="wxRAISED_BORDER|";
403if (style&wxTRANSPARENT_WINDOW)
404 s+="wxTRANSPARENT_WINDOW|";
405if (style&wxWANTS_CHARS)
406 s+="wxWANTS_CHARS|";
407if (style&wxNO_FULL_REPAINT_ON_RESIZE)
408 s+="wxNO_FULL_REPAINT_ON_RESIZE|";
409
410if (restype=="wxDialog")
411{
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|";
418 if (style&wxNO_3D)
419 s+="wxNO_3D|";
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)
425 s+="wxSTAY_ON_TOP|";
426 if (style&wxCAPTION)
427 s+="wxCAPTION|";
428 if (style&wxTHICK_FRAME)
429 s+="wxTHICK_FRAME|";
430 if (style&wxRESIZE_BOX)
431 s+="wxRESIZE_BOX|";
432 if (style&wxRESIZE_BORDER)
433 s+="wxRESIZE_BORDER|";
434 if (style&wxSYSTEM_MENU)
435 s+="wxSYSTEM_MENU|";
436 if (style&wxCLIP_CHILDREN)
437 s+="wxCLIP_CHILDREN|";
438
439
440}
441
442
443
444if (restype=="wxPanel")
445{
446 if (style&wxCLIP_CHILDREN)
447 s+="wxCLIP_CHILDREN|";
448 if (style&wxNO_3D)
449 s+="wxNO_3D|";
450 if (style&wxTAB_TRAVERSAL)
451 s+="wxTAB_TRAVERSAL|";
452 if (style&wxWS_EX_VALIDATE_RECURSIVELY)
453 s+="wxWS_EX_VALIDATE_RECURSIVELY|";
454}
455
456if (restype=="wxComboBox")
457{
458 if (style&wxCB_SORT)
459 s+="wxCB_SORT|";
460 if (style&wxCB_SIMPLE)
461 s+="wxCB_SIMPLE|";
462 if (style&wxCB_READONLY)
463 s+="wxCB_READONLY|";
464 if (style&wxCB_DROPDOWN)
465 s+="wxCB_DROPDOWN|";
466}
467
468if (restype=="wxGauge")
469{
470 if (style&wxGA_HORIZONTAL)
471 s+="wxGA_HORIZONTAL|";
472 if (style&wxGA_VERTICAL)
473 s+="wxGA_VERTICAL|";
474 if (style&wxGA_PROGRESSBAR)
475 s+="wxGA_PROGRESSBAR|";
476 // windows only
477 if (style&wxGA_SMOOTH)
478 s+="wxGA_SMOOTH|";
479}
480
481if (restype=="wxRadioButton")
482{
483 if (style&wxRB_GROUP)
484 s+="wxRB_GROUP|";
485}
486
487if (restype=="wxStaticText")
488{
489 if (style&wxST_NO_AUTORESIZE)
490 s+="wxST_NO_AUTORESIZEL|";
491}
492
493if (restype=="wxRadioBox")
494 {
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)
502 s+="wxRA_VERTICAL|";
503 }
504
505if (restype=="wxListBox")
506{
507 if (style&wxLB_SINGLE)
508 s+="wxLB_SINGLE|";
509 if (style&wxLB_MULTIPLE)
510 s+="wxLB_MULTIPLE|";
511 if (style&wxLB_EXTENDED)
512 s+="wxLB_EXTENDED|";
513 if (style&wxLB_HSCROLL)
514 s+="wxLB_HSCROLL|";
515 if (style&wxLB_ALWAYS_SB)
516 s+="wxLB_ALWAYS_SB|";
517 if (style&wxLB_NEEDED_SB)
518 s+="wxLB_NEEDED_SB|";
519 if (style&wxLB_SORT)
520 s+="wxLB_SORT|";
521}
522
523if (restype=="wxTextCtrl")
524{
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)
532 s+="wxTE_PASSWORD|";
533 if (style&wxTE_READONLY)
534 s+="wxTE_READONLY|";
535 if (style&wxHSCROLL)
536 s+="wxHSCROLL|";
537}
538
539int l;
540l=s.Length();
541//No styles defined
542if (l==7)
543 return _T("");
544//Trim off last |
545s=s.Truncate(l-1);
546
547s+="</style>";
548return s;
549}
550
551wxString wxWxr2Xml::GetDimension(wxItemResource *res)
552{
553wxString msg;
554msg<<" <dimension>"<<res->GetValue1()<<"</dimension>";
555return msg;
556}
557
558wxString wxWxr2Xml::GenerateName(wxItemResource *res)
559{
560wxString name;
561name=_T(" name=\"");
562switch (res->GetId())
563 {
564 case wxID_OK:
565 name+=_T("wxID_OK");
566 break;
567 case wxID_CANCEL:
568 name+=_T("wxID_CANCEL");
569 break;
570 default:
571 name+=res->GetName();
572 }
573
574name+="\"";
575return name;
576}
577
578void wxWxr2Xml::ParseMenuBar(wxItemResource *res)
579{
580 wxItemResource *child;
581 wxNode *node = res->GetChildren().First();
582 //Get Menu Bar Name
583 m_xmlfile.Write("<menubar ");
584 m_xmlfile.Write(GenerateName(res));
585 m_xmlfile.Write(">\n");
586 m_xmlfile.Write(" <children>\n");
587 while (node)
588 {
589 child= (wxItemResource *)node->Data();
590 ParseMenu(child);
591 node = node->Next();
592 }
593
594 m_xmlfile.Write(" </children>\n");
595 m_xmlfile.Write("</menubar> \n");
596}
597
598
599void wxWxr2Xml::ParseMenu(wxItemResource *res)
600{
601 wxItemResource *child;
602 wxNode *node = res->GetChildren().First();
603 //Get Menu
604 m_xmlfile.Write(" <menu ");
605 wxString menuname;
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
615 while (node)
616 {
617 child= (wxItemResource *)node->Data();
618 if (!child->GetChildren().First())
619 ParseMenuItem(child);
620 else
621 ParseMenu(child);
622
623 node = node->Next();
624 }
625m_xmlfile.Write(" </children>\n");
626m_xmlfile.Write(" </menu> \n");
627}
628
629void wxWxr2Xml::ParseMenuItem(wxItemResource *res)
630{
631 //Get Menu Item or Separator
632if (res->GetTitle()=="")
633 {
634 m_xmlfile.Write(" <separator/>\n");
635 }
636else
637 {
638 m_xmlfile.Write(" <menuitem ");
639 wxString menuname;
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");
650 }
651}
652
653wxString wxWxr2Xml::FixMenuString(wxString phrase)
654{
655phrase.Replace("&","$");
656return phrase;
657}
658
659void wxWxr2Xml::ParseStaticBitmap(wxItemResource *res)
660{
661m_xmlfile.Write(" <staticbitmap");
662WriteControlInfo(res);
663//value4 holds bitmap name
664wxString bitmapname;
665bitmapname=res->GetValue4();
666wxBitmap bitmap;
667bitmap= wxResourceCreateBitmap(bitmapname,&m_table);
668bitmapname+=_T(".bmp");
669bitmap.SaveFile(bitmapname,wxBITMAP_TYPE_BMP);
670m_xmlfile.Write("\n <bitmap>"+bitmapname+"</bitmap>");
671m_xmlfile.Write("</staticbitmap>\n");
672//bitmap5
673}
674
675void wxWxr2Xml::ParseBitmap(wxItemResource *res)
676{
677
678}
679
680void wxWxr2Xml::PanelStuff(wxItemResource *res)
681{
682if ((res->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0)
683 m_dlgunits=TRUE;
684else
685 m_dlgunits=FALSE;
686
687//If this is true ignore fonts, background color and use system
688//defaults instead
689if ((res->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS) != 0)
690 m_systemdefaults=TRUE;
691else
692 m_systemdefaults=FALSE;
693
694}