]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/convertrc/wxr2xml.cpp
a6e9d0d9735917304b679ddab9ceb846da2865a3
[wxWidgets.git] / contrib / utils / convertrc / wxr2xml.cpp
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
32 wxWxr2Xml::wxWxr2Xml()
33 {
34
35 }
36
37 wxWxr2Xml::~wxWxr2Xml()
38 {
39
40 }
41
42 bool wxWxr2Xml::Convert(wxString wxrfile, wxString xmlfile)
43 {
44 bool result;
45 result=m_xmlfile.Open(xmlfile.c_str(),"w+t");
46 wxASSERT_MSG(result,"Couldn't create XML file");
47 if (!result)
48 return FALSE;
49
50 result=m_table.ParseResourceFile(wxrfile);
51 wxASSERT_MSG(result,"Couldn't Load WXR file");
52 if (!result)
53 return FALSE;
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");
59
60 m_xmlfile.Close();
61
62 return result;
63 }
64
65 bool wxWxr2Xml::ParseResources()
66 {
67 m_table.BeginFind();
68 wxNode *node;
69
70 while ((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
88 return TRUE;
89 }
90
91 void wxWxr2Xml::ParsePanel(wxItemResource *res)
92 {
93
94 m_xmlfile.Write(" <panel");
95 PanelStuff(res);
96 WriteControlInfo(res);
97 m_xmlfile.Write("\n");
98 m_xmlfile.Write(" <children>\n");
99 ParseControls(res);
100 m_xmlfile.Write(" </children>\n");
101
102 m_xmlfile.Write(" </panel>\n");
103 }
104
105
106 void wxWxr2Xml::ParseDialog(wxItemResource *res)
107 {
108 PanelStuff(res);
109 m_xmlfile.Write(" <dialog");
110 WriteControlInfo(res);
111 m_xmlfile.Write(GetTitle(res));
112
113
114 m_xmlfile.Write("\n");
115 m_xmlfile.Write(" <children>\n");
116 ParseControls(res);
117 m_xmlfile.Write(" </children>\n");
118 m_xmlfile.Write(" </dialog>\n");
119 }
120
121 void wxWxr2Xml::ParseControls(wxItemResource *res)
122 {
123 wxNode *node = res->GetChildren().First();
124 while (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
167 void wxWxr2Xml::WriteControlInfo(wxItemResource *res)
168 {
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));
174 }
175
176 wxString wxWxr2Xml::GetSize(wxItemResource *res)
177 {
178 wxString msg;
179 if (m_dlgunits)
180 msg<<" <size>"<<res->GetWidth()<<","<<res->GetHeight()<<"d</size>";
181 else
182 msg<<" <size>"<<res->GetWidth()<<","<<res->GetHeight()<<"</size>";
183 return msg;
184 }
185
186 wxString wxWxr2Xml::GetPosition(wxItemResource *res)
187 {
188 wxString msg;
189 if (m_dlgunits)
190 msg<<" <pos>"<<res->GetX()<<","<<res->GetY()<<"d</pos>";
191 else
192 msg<<" <pos>"<<res->GetX()<<","<<res->GetY()<<"</pos>";
193 return msg;
194 }
195
196 void wxWxr2Xml::ParseButton(wxItemResource *res)
197 {
198 m_xmlfile.Write(" <button");
199 WriteControlInfo(res);
200 m_xmlfile.Write(GetLabel(res));
201 m_xmlfile.Write("</button>\n");
202 }
203
204 void wxWxr2Xml::ParseTextCtrl(wxItemResource *res)
205 {
206 m_xmlfile.Write(" <textctrl");
207 WriteControlInfo(res);
208 m_xmlfile.Write(GetValue4(res));
209 m_xmlfile.Write("</textctrl>\n");
210
211 }
212
213 wxString wxWxr2Xml::GetTitle(wxItemResource *res)
214 {
215 wxString msg;
216 msg=_T(" <title>"+res->GetTitle()+"</title>");
217 return msg;
218 }
219
220 wxString wxWxr2Xml::GetValue4(wxItemResource *res)
221 {
222 wxString msg;
223 msg=_T(" <value>"+res->GetValue4()+"</value>");
224 return msg;
225 }
226
227 void wxWxr2Xml::ParseCheckBox(wxItemResource *res)
228 {
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");
234 }
235
236 wxString wxWxr2Xml::GetLabel(wxItemResource *res)
237 {
238 return _T(" <label>"+res->GetTitle()+"</label>");
239 }
240
241 void wxWxr2Xml::ParseRadioBox(wxItemResource *res)
242 {
243 m_xmlfile.Write(" <radiobox");
244 WriteControlInfo(res);
245 m_xmlfile.Write(GetLabel(res));
246 //Add radio box items
247 WriteStringList(res);
248 //Value1
249 m_xmlfile.Write(GetDimension(res));
250 m_xmlfile.Write("\n </radiobox>\n");
251 }
252
253 void wxWxr2Xml::ParseListBox(wxItemResource *res)
254 {
255 m_xmlfile.Write(" <listbox");
256 WriteControlInfo(res);
257 WriteStringList(res);
258 m_xmlfile.Write("</listbox>\n");
259 }
260
261 void wxWxr2Xml::ParseStaticText(wxItemResource *res)
262 {
263 m_xmlfile.Write(" <statictext");
264 WriteControlInfo(res);
265 m_xmlfile.Write(GetLabel(res));
266 m_xmlfile.Write("</statictext>\n");
267 }
268
269 void wxWxr2Xml::ParseStaticBox(wxItemResource *res)
270 {
271 m_xmlfile.Write(" <staticbox");
272 WriteControlInfo(res);
273 m_xmlfile.Write(GetLabel(res));
274 m_xmlfile.Write("</staticbox>\n");
275 }
276
277 void wxWxr2Xml::WriteStringList(wxItemResource *res)
278 {
279 m_xmlfile.Write("\n <content>");
280 for ( 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
289 void wxWxr2Xml::ParseChoice(wxItemResource *res)
290 {
291 m_xmlfile.Write(" <choice");
292 WriteControlInfo(res);
293 //Add choice items
294 WriteStringList(res);
295 m_xmlfile.Write("\n </choice>\n");
296 }
297
298 void wxWxr2Xml::ParseGauge(wxItemResource *res)
299 {
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");
305 }
306
307
308 wxString wxWxr2Xml::GetValue1(wxItemResource *res)
309 {
310 wxString msg;
311 msg<<" <value>"<<res->GetValue1()<<"</value>";
312 return msg;
313 }
314
315 wxString wxWxr2Xml::GetRange(wxItemResource *res)
316 {
317 wxString msg;
318 msg<<" <range>"<<res->GetValue2()<<"</range>";
319 return msg;
320 }
321
322 void wxWxr2Xml::ParseSlider(wxItemResource *res)
323 {
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");
330 }
331
332 wxString wxWxr2Xml::GetMax(wxItemResource *res)
333 {
334 wxString msg;
335 msg<<" <max>"<<res->GetValue3()<<"</max>";
336 return msg;
337 }
338
339 wxString wxWxr2Xml::GetMin(wxItemResource *res)
340 {
341 wxString msg;
342 msg<<" <min>"<<res->GetValue2()<<"</min>";
343 return msg;
344 }
345
346 void wxWxr2Xml::ParseComboBox(wxItemResource *res)
347 {
348 m_xmlfile.Write(" <combobox");
349 WriteControlInfo(res);
350 //Add combo items
351 WriteStringList(res);
352 m_xmlfile.Write("\n </combobox>\n");
353 }
354
355 void wxWxr2Xml::ParseRadioButton(wxItemResource *res)
356 {
357 m_xmlfile.Write(" <radiobutton");
358 WriteControlInfo(res);
359 m_xmlfile.Write(GetLabel(res));
360
361 wxString msg;
362 m_xmlfile.Write(GetValue1(res));
363 m_xmlfile.Write(GetCheckStatus(res));
364 m_xmlfile.Write("\n </radiobutton>\n");
365 }
366
367 void wxWxr2Xml::ParseScrollBar(wxItemResource *res)
368 {
369 m_xmlfile.Write(" <scrollbar");
370 WriteControlInfo(res);
371 //TODO learn more about XML scrollbar format
372 m_xmlfile.Write("\n </scrollbar>\n");
373 }
374
375 wxString wxWxr2Xml::GetCheckStatus(wxItemResource *res)
376 {
377 wxString msg;
378 msg<<" <checked>"<<res->GetValue1()<<"</checked>";
379 return msg;
380 }
381
382 wxString wxWxr2Xml::GetStyles(wxItemResource *res)
383 {
384 //Very crude way to get styles
385 long style;
386 wxString s,restype;
387 restype=res->GetType();
388 style=res->GetStyle();
389
390 s="<style>";
391
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)
406 s+="wxWANTS_CHARS|";
407 if (style&wxNO_FULL_REPAINT_ON_RESIZE)
408 s+="wxNO_FULL_REPAINT_ON_RESIZE|";
409
410 if (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
444 if (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
456 if (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
468 if (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
481 if (restype=="wxRadioButton")
482 {
483 if (style&wxRB_GROUP)
484 s+="wxRB_GROUP|";
485 }
486
487 if (restype=="wxStaticText")
488 {
489 if (style&wxST_NO_AUTORESIZE)
490 s+="wxST_NO_AUTORESIZEL|";
491 }
492
493 if (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
505 if (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
523 if (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
539 int l;
540 l=s.Length();
541 //No styles defined
542 if (l==7)
543 return _T("");
544 //Trim off last |
545 s=s.Truncate(l-1);
546
547 s+="</style>";
548 return s;
549 }
550
551 wxString wxWxr2Xml::GetDimension(wxItemResource *res)
552 {
553 wxString msg;
554 msg<<" <dimension>"<<res->GetValue1()<<"</dimension>";
555 return msg;
556 }
557
558 wxString wxWxr2Xml::GenerateName(wxItemResource *res)
559 {
560 wxString name;
561 name=_T(" name=\"");
562 switch (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
574 name+="\"";
575 return name;
576 }
577
578 void 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
599 void 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 }
625 m_xmlfile.Write(" </children>\n");
626 m_xmlfile.Write(" </menu> \n");
627 }
628
629 void wxWxr2Xml::ParseMenuItem(wxItemResource *res)
630 {
631 //Get Menu Item or Separator
632 if (res->GetTitle()=="")
633 {
634 m_xmlfile.Write(" <separator/>\n");
635 }
636 else
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
653 wxString wxWxr2Xml::FixMenuString(wxString phrase)
654 {
655 phrase.Replace("&","$");
656 return phrase;
657 }
658
659 void wxWxr2Xml::ParseStaticBitmap(wxItemResource *res)
660 {
661 m_xmlfile.Write(" <staticbitmap");
662 WriteControlInfo(res);
663 //value4 holds bitmap name
664 wxString bitmapname;
665 bitmapname=res->GetValue4();
666 wxBitmap bitmap;
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");
672 //bitmap5
673 }
674
675 void wxWxr2Xml::ParseBitmap(wxItemResource *res)
676 {
677
678 }
679
680 void wxWxr2Xml::PanelStuff(wxItemResource *res)
681 {
682 if ((res->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0)
683 m_dlgunits=TRUE;
684 else
685 m_dlgunits=FALSE;
686
687 //If this is true ignore fonts, background color and use system
688 //defaults instead
689 if ((res->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS) != 0)
690 m_systemdefaults=TRUE;
691 else
692 m_systemdefaults=FALSE;
693
694 }