]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/convertrc/rc2xml.cpp
corrections for Mac OS X compilation
[wxWidgets.git] / contrib / utils / convertrc / rc2xml.cpp
1 // rc2xml.cpp: implementation of the rc2xml class.
2 //Author: Brian Gavin 9/24/00
3 //License: wxWindows License
4 /*
5 How to use:
6 #include "rc2xml.h"
7 ...
8 rc2xml trans;
9 trans->Convert("Myfile.rc","Myfile.xml");
10 */
11 /* TODO
12 1. Figure how to fix memory leaks in all wxLists in this class
13 2. Find a way to rename MS Windows fonts so that they work
14 cross platform (wxGTK,etc)
15 3. Be able to abort incorrectly formated files without crashing
16 */
17
18 #ifdef __GNUG__
19 #pragma implementation "rc2xml.cpp"
20 #pragma interface "rc2xml.cpp"
21 #endif
22
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include <wx/wxprec.h>
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 // for all others, include the necessary headers (this file is usually all you
31 // need because it includes almost all "standard" wxWindows headers
32 #ifndef WX_PRECOMP
33 #include <wx/wx.h>
34 #endif
35
36
37 #include "rc2xml.h"
38 #include "wx/image.h"
39 #include "wx/resource.h"
40 #include <wx/textfile.h>
41 #include <wx/tokenzr.h>
42
43
44
45 //////////////////////////////////////////////////////////////////////
46 // Construction/Destruction
47 //////////////////////////////////////////////////////////////////////
48
49 rc2xml::rc2xml()
50 {
51 m_done=FALSE;
52 m_bitmaplist=new wxList(wxKEY_STRING);
53 m_stringtable=new wxList(wxKEY_STRING);
54 m_iconlist = new wxList(wxKEY_STRING);
55 m_resourcelist =new wxList(wxKEY_INTEGER);
56 }
57
58 rc2xml::~rc2xml()
59 {
60 delete m_bitmaplist;
61 delete m_stringtable;
62 delete m_iconlist;
63 delete m_resourcelist;
64 }
65
66 bool rc2xml::Convert(wxString rcfile, wxString xmlfile)
67 {
68 m_rc.Open(rcfile.c_str());
69 m_filesize=m_rc.Length();
70
71 m_workingpath=wxPathOnly(rcfile);
72 m_targetpath=wxPathOnly(xmlfile)+"\\";
73
74 wxSetWorkingDirectory(m_workingpath);
75
76 bool result;
77 result=m_xmlfile.Open(xmlfile.c_str(),"w+t");
78 wxASSERT_MSG(result,"Couldn't create XML file");
79 if (!result)
80 return FALSE;
81
82
83 /* Write Basic header for XML file */
84 m_xmlfile.Write("<?xml version=\"1.0\" ?>\n");
85 m_xmlfile.Write("<resource>\n");
86
87 //Read resource.h
88 ParseResourceHeader();
89 //Gather all the resource we need for toolbars,menus, and etc
90 FirstPass();
91 m_done=FALSE;
92 m_rc.Seek(0);
93 //Read in dialogs, toolbars,menus
94 SecondPass();
95
96 m_xmlfile.Write("</resource>\n");
97 m_xmlfile.Close();
98 m_rc.Close();
99
100 return TRUE;
101 }
102
103
104 void rc2xml::ParseDialog(wxString dlgname)
105 {
106 wxString token;
107 static int dlgid=999;
108 dlgid++;
109 /* Make sure that this really is a dialog
110 microsoft reuses the keyword DIALOG for other things
111 */
112 token=PeekToken();
113 //Microsoft notation?
114 if (token=="DISCARDABLE")
115 {
116 token=GetToken();
117 token=PeekToken();
118 }
119 //Error isn't a Dialog resource eject eject
120 if (!token.IsNumber())
121 return;
122
123 //Record x,y,width,height
124 int x,y,width,height;
125 ReadRect(x,y,width,height);
126 //Get Title
127 token=GetToken();
128 wxString title;
129 wxString ptsize,face;
130
131 m_xmlfile.Write("\t<object class=\"wxDialog\"");
132 //Avoid duplicate names this way
133 dlgname.Replace("IDD_","DLG_");
134 WriteBasicInfo(x,y,width,height,dlgname);
135 WriteTitle(title);
136
137
138 while ((token!="BEGIN")&(token!="{"))
139 {
140 if (token=="CAPTION")
141 {
142 title=GetQuoteField();
143 }
144
145 //TODO fix face name so that it is cross platform name
146 // FONT 8, "MS Sans Serif"
147 if (token=="FONT")
148 {
149 ptsize=GetToken();
150 face=GetQuoteField();
151 m_xmlfile.Write("\t\t<font>\n");
152 m_xmlfile.Write("\t\t\t<size>"+ptsize+"</size>\n");
153 m_xmlfile.Write("\t\t\t<face>"+face+"</face>\n");
154 m_xmlfile.Write("\t\t</font>\n");
155 }
156
157 token=GetToken();
158 }
159
160 ParseControls();
161 m_xmlfile.Write("\t</object>\n");
162 }
163
164 /*
165 BEGIN
166 EDITTEXT IDC_BANDS,36,83,22,14,ES_AUTOHSCROLL | ES_NUMBER | NOT
167 WS_TABSTOP
168 LTEXT "Bands",IDC_STATIC,11,86,21,8
169 EDITTEXT IDC_NAME,10,3,75,14,ES_AUTOHSCROLL
170 END
171 */
172 void rc2xml::ParseControls()
173 {
174 wxString token;
175
176 token=GetToken();
177 while ((token!="END")&(token!="}"))
178 {
179 if (token=="AUTOCHECKBOX")
180 ParseCheckBox();
181 else
182 if (token=="AUTORADIOBUTTON")
183 ParseRadioButton();
184 else
185 if (token=="LTEXT")
186 ParseStaticText();
187 else if (token=="EDITTEXT")
188 ParseTextCtrl();
189 else if (token=="PUSHBUTTON")
190 ParsePushButton();
191 else if (token=="DEFPUSHBUTTON")
192 ParsePushButton();
193 else if (token=="GROUPBOX")
194 ParseGroupBox();
195 else if (token=="COMBOBOX")
196 ParseComboBox();
197 else if (token=="CONTROL")
198 ParseControlMS();
199 else if (token=="LISTBOX")
200 ParseListBox();
201 else if (token=="ICON")
202 ParseIconStatic();
203 else if (token=="SCROLLBAR")
204 ParseScrollBar();
205 token=GetToken();
206 }
207
208 }
209 //LTEXT "Radius",IDC_STATIC,9,67,23,8
210 void rc2xml::ParseStaticText()
211 {
212 wxString token;
213 wxString phrase,varname;
214 phrase=GetQuoteField();
215 varname=GetToken();
216 int x,y,width,height;
217 ReadRect(x,y,width,height);
218
219 m_xmlfile.Write("\t\t<object class=\"wxStaticText\"");
220 WriteBasicInfo(x,y,width,height,varname);WriteLabel(phrase);
221 m_xmlfile.Write("\t\t</object>\n");
222
223 }
224 //EDITTEXT IDC_RADIUS,36,65,40,14,ES_AUTOHSCROLL
225 void rc2xml::ParseTextCtrl()
226 {
227 wxString token;
228 wxString varname,style;
229 varname=GetToken();
230 int x,y,width,height;
231 ReadRect(x,y,width,height);
232 //TODO
233 //style=GetToken();
234 m_xmlfile.Write("\t\t<object class=\"wxTextCtrl\"");
235 WriteBasicInfo(x,y,width,height,varname);
236 m_xmlfile.Write("\t\t</object>\n");
237
238 }
239 //AUTOCHECKBOX "&log.", ID_XLOG, 25, 24, 21, 12
240 void rc2xml::ParseCheckBox()
241 {
242 wxString token;
243 wxString phrase,varname;
244 phrase=GetQuoteField();
245 varname=GetToken();
246
247 int x,y,width,height;
248 ReadRect(x,y,width,height);
249
250 m_xmlfile.Write("\t\t<object class=\"wxCheckBox\"");
251 WriteBasicInfo(x,y,width,height,varname);
252 WriteLabel(phrase);
253 m_xmlfile.Write("\t\t</object>\n");
254
255 }
256 //AUTORADIOBUTTON "&text", ID_SW10, 13, 12, 68, 10, BS_AUTORADIOBUTTON | WS_GROUP
257 void rc2xml::ParseRadioButton()
258 {
259 wxString token,style;
260 wxString phrase,varname;
261 phrase=GetQuoteField();
262 varname=GetToken();
263
264 int x,y,width,height;
265 if (ReadRect(x,y,width,height))
266 ReadOrs(token);
267 if (token.Find("WS_GROUP") != -1)
268 style += "wxRB_GROUP";
269
270 m_xmlfile.Write("\t\t<object class=\"wxRadioButton\"");
271 WriteBasicInfo(x,y,width,height,varname);
272 WriteLabel(phrase);
273 WriteStyle(style);
274 m_xmlfile.Write("\t\t</object>\n");
275
276 }
277
278 //PUSHBUTTON "Create/Update",IDC_CREATE,15,25,53,13,NOT WS_TABSTOP
279 void rc2xml::ParsePushButton()
280 {
281 wxString token;
282 wxString phrase,varname;
283 phrase=GetQuoteField();
284 varname=GetToken();
285
286 int x,y,width,height;
287 ReadRect(x,y,width,height);
288
289 m_xmlfile.Write("\t\t<object class=\"wxButton\"");
290 WriteBasicInfo(x,y,width,height,varname);
291 WriteLabel(phrase);
292 m_xmlfile.Write("\t\t</object>\n");
293
294 }
295
296
297 bool rc2xml::Seperator(int ch)
298 {
299 //if ((ch==' ')|(ch==',')|(ch==13)|(ch==10)|(ch=='|')|(ch=='\t'))
300 if ((ch==' ')|(ch==',')|(ch==13)|(ch==10)|(ch=='\t'))
301 return TRUE;
302
303 if (ch==EOF)
304 {
305 m_done=TRUE;
306 return TRUE;
307 }
308
309 return FALSE;
310 }
311
312 void rc2xml::ParseGroupBox()
313 {
314 // GROUPBOX "Rotate",IDC_STATIC,1,1,71,79
315 wxString token;
316 wxString phrase,varname;
317 phrase=GetQuoteField();
318 varname=GetToken();
319 int x,y,width,height;
320 ReadRect(x,y,width,height);
321
322 m_xmlfile.Write("\t\t<object class=\"wxStaticBox\"");
323 WriteBasicInfo(x,y,width,height,varname);
324 WriteLabel(phrase);
325 m_xmlfile.Write("\t\t</object>\n");
326 }
327
328 bool rc2xml::ReadRect(int & x, int & y, int & width, int & height)
329 {
330 x=atoi(GetToken());
331 y=atoi(GetToken());
332 width=atoi(GetToken());
333 bool ret;
334 wxString tmp = GetToken(&ret);
335 height=atoi(tmp);
336 return ret; // check for more parameters
337 }
338
339 wxString rc2xml::GetToken(bool *listseperator)
340 {
341 wxString token="";
342
343 if (m_rc.Eof())
344 {
345 m_done=TRUE;
346 return token;
347 }
348
349 int ch=0;
350 ReadChar(ch);
351 if (ch==EOF)
352 {
353 m_done=TRUE;
354 return token;
355 }
356
357 while (Seperator(ch))
358 {
359 ReadChar(ch);
360 if (m_done)
361 return token;
362 }
363
364 if (ch==EOF)
365 {
366 m_done=TRUE;
367 }
368
369
370 while (!Seperator(ch))
371 {
372 token += (char)ch;
373 ReadChar(ch);
374 }
375
376 if (ch == EOF)
377 m_done = TRUE;
378
379 if (listseperator)
380 *listseperator = (ch == ',');
381 return token;
382 }
383
384 wxString rc2xml::GetQuoteField()
385 {
386 wxString phrase;
387 //ASCII code 34 "
388 int ch=0;
389 ReadChar(ch);
390
391 while (ch!=34)
392 ReadChar(ch);
393
394 ReadChar(ch);
395
396 while (ch!=34)
397 {
398 phrase+=(char)ch;
399 ReadChar(ch);
400 }
401 return phrase;
402 }
403
404 void rc2xml::ReadChar(int &ch)
405 {
406 int result;
407 result=m_rc.Tell();
408
409 if((result>=m_filesize))
410 m_done=TRUE;
411
412 result=m_rc.Read(&ch,1);
413
414 if((result==-1))
415 m_done=TRUE;
416
417 if(ch==EOF)
418 m_done=TRUE;
419 }
420
421 void rc2xml::ParseComboBox()
422 {
423 /* COMBOBOX IDC_SCALECOMBO,10,110,48,52,CBS_DROPDOWNLIST | CBS_SORT |
424 WS_VSCROLL | WS_TABSTOP */
425 wxString token,style;
426 wxString varname;
427 varname=GetToken();
428 int x,y,width,height;
429 if (ReadRect(x,y,width,height))
430 ReadOrs(token);
431
432 m_xmlfile.Write("\t\t<object class=\"wxComboBox\"");
433 WriteBasicInfo(x,y,width,height,varname);
434 if (token.Find("CBS_SIMPLE") != -1)
435 WriteStyle("wxCB_SIMPLE");
436 if (token.Find("CBS_SORT") != -1)
437 WriteStyle("wxCB_SORT");
438 if (token.Find("CBS_DISABLENOSCROLL") != -1)
439 WriteStyle("wxLB_ALWAYS_SB");
440 m_xmlfile.Write("\n\t\t</object>\n");
441
442 }
443
444 void rc2xml::ParseMenu(wxString varname)
445 {
446 wxString token="";
447
448 //Write menubar to xml file
449 m_xmlfile.Write("\t<object class=\"wxMenuBar\"");
450 //Avoid duplicate names this way
451 varname.Replace("IDR_","MB_");
452 WriteName(varname);
453 m_xmlfile.Write(">\n");
454
455 while ((token!="BEGIN")&(token!="{"))
456 token=GetToken();
457
458 while ((token!="END")&(token!="}"))
459 {
460 token=GetToken();
461 if (token=="POPUP")
462 {
463 ParsePopupMenu();
464 }
465 }
466 m_xmlfile.Write("\t</object>\n");
467 }
468
469 void rc2xml::ParsePopupMenu()
470 {
471 static int menucount=0;
472 menucount++;
473 wxString token,name,msg,longhelp,tip;
474 token=GetQuoteField();
475
476 //Remove \t because it causes problems
477
478 //spot=token.First("\\t");
479 //token=token.Left(spot);
480
481 //Write Menu item
482 //Generate a fake name since RC menus don't have one
483 name<<"Menu_"<<menucount;
484 m_xmlfile.Write("\t\t<object class=\"wxMenu\"");
485 WriteName(name);
486 m_xmlfile.Write(">\n");
487 WriteLabel(token);
488
489 while ((token!="BEGIN")&(token!="{"))
490 token=GetToken();
491
492 while ((token!="END")&(token!="}"))
493 {
494 token=GetToken();
495 if (token=="POPUP")
496 ParsePopupMenu();
497
498 if (token=="MENUITEM")
499 ParseMenuItem();
500 }
501 m_xmlfile.Write("\t\t\t</object>\n");
502 }
503
504 wxString rc2xml::PeekToken()
505 {
506 wxString token;
507 int p;
508 p=m_rc.Tell();
509 token=GetToken();
510
511 m_rc.Seek(p);
512 return token;
513 }
514 //MS Windows pain in the butt CONTROL
515 void rc2xml::ParseControlMS()
516 {
517 wxString label,varname,kindctrl,token;
518 token=PeekToken();
519
520 if (token.Contains("\""))
521 ParseNormalMSControl();
522 else
523 ParseWeirdMSControl();
524
525 }
526
527 /* CONTROL "Slider1",IDC_SLIDER1,"msctls_trackbar32",TBS_BOTH |
528 TBS_NOTICKS | WS_TABSTOP,52,73,100,15
529 */
530
531 void rc2xml::ParseSlider(wxString label, wxString varname)
532 {
533 wxString token,style;
534 ReadOrs(token);
535 if (token.Find("TBS_VERT")!=-1)
536 style+="wxSL_VERTICAL";
537 //MFC RC Default is horizontal
538 else
539 style+="wxSL_HORIZONTAL";
540
541 int x,y,width,height;
542 ReadRect(x,y,width,height);
543 m_xmlfile.Write("\t\t<object class=\"wxSlider\"");
544 WriteBasicInfo(x,y,width,height,varname);
545 WriteStyle(style);
546 m_xmlfile.Write("\n\t\t</object>\n");
547
548 }
549 /*
550 CONTROL "Progress1",CG_IDC_PROGDLG_PROGRESS,"msctls_progress32",
551 WS_BORDER,15,52,154,13
552 */
553 void rc2xml::ParseProgressBar(wxString label, wxString varname)
554 {
555 wxString token,style;
556 ReadOrs(token);
557
558 int x,y,width,height;
559 ReadRect(x,y,width,height);
560
561 //Always horizontal in MFC
562 m_xmlfile.Write("\t\t<object class=\"wxGauge\"");
563 WriteBasicInfo(x,y,width,height,varname);
564 WriteStyle(style);
565 m_xmlfile.Write("\t\t</object>\n");
566 }
567
568 bool rc2xml::ReadOrs(wxString & orstring)
569 {
570 wxString token;
571
572 token=PeekToken();
573 if (token.IsNumber())
574 return FALSE;
575 orstring=GetToken();
576
577 while(PeekToken()==_T("|"))
578 {
579 //Grab |
580 orstring+=GetToken();
581 //Grab next token
582 orstring+=GetToken();
583 }
584 return TRUE;
585 }
586
587 //Is it a check button or a radio button
588 void rc2xml::ParseCtrlButton(wxString label, wxString varname)
589 {
590 wxString token;
591 ReadOrs(token);
592 int x,y,width,height;
593
594 if (token.Find("BS_AUTOCHECKBOX")!=-1)
595 {
596 ReadRect(x,y,width,height);
597 m_xmlfile.Write("\t\t<object class=\"wxCheckBox\"");
598 WriteBasicInfo(x,y,width,height,varname);
599 WriteLabel(label);
600 m_xmlfile.Write("\t\t</object>\n");
601 }
602
603 if (token.Find("BS_AUTORADIOBUTTON")!=-1)
604 {
605 ReadRect(x,y,width,height);
606 m_xmlfile.Write("\t\t<object class=\"wxRadioButton\"");
607 WriteBasicInfo(x,y,width,height,varname);
608 WriteLabel(label);
609 m_xmlfile.Write("\t\t</object>\n");
610 }
611
612 }
613
614
615 void rc2xml::WriteSize(int width, int height)
616 {
617 wxString msg;
618 msg<<" <size>"<<width<<","<<height<<"d</size>";
619 m_xmlfile.Write(msg);
620 }
621
622 void rc2xml::WritePosition(int x, int y)
623 {
624 wxString msg;
625 msg<<" <pos>"<<x<<","<<y<<"d</pos>";
626 m_xmlfile.Write(msg);
627 }
628
629 void rc2xml::WriteTitle(wxString title)
630 {
631 wxString msg;
632 msg=_T("\t\t<title>"+title+"</title>\n");
633 m_xmlfile.Write(msg);
634 }
635
636 void rc2xml::WriteName(wxString name)
637 {
638
639 //Try to convert any number ids into names
640 name=LookUpId(name);
641 //Replace common MS ids with wxWindows ids
642 //I didn't do everyone of them
643 if (name=="IDOK")
644 name="wxID_OK";
645 else if (name=="IDCANCEL")
646 name="wxID_CANCEL";
647 else if (name=="IDAPPLY")
648 name="wxID_APPLY";
649 else if (name=="ID_FILE_OPEN")
650 name="wxID_OPEN";
651 else if (name=="ID_FILE_CLOSE")
652 name="wxID_CLOSE";
653 else if (name=="ID_FILE_SAVE")
654 name="wxID_SAVE";
655 else if (name=="ID_FILE_SAVE_AS")
656 name="wxID_SAVEAS";
657 else if (name=="ID_APP_EXIT")
658 name="wxID_EXIT";
659 else if (name=="ID_FILE_PRINT")
660 name="wxID_PRINT";
661 else if (name=="ID_FILE_PRINT_PREVIEW")
662 name="wxID_PREVIEW";
663 else if (name=="ID_FILE_PRINT_SETUP")
664 name="wxID_PRINT_SETUP";
665 else if (name=="ID_APP_ABOUT")
666 name="wxID_ABOUT";
667 else if (name=="ID_EDIT_UNDO")
668 name="wxID_UNDO";
669 else if (name=="ID_EDIT_CUT")
670 name="wxID_CUT";
671 else if (name=="ID_EDIT_COPY")
672 name="wxID_COPY";
673 else if (name=="ID_EDIT_PASTE")
674 name="wxID_PASTE";
675 else if (name=="IDYES")
676 name="wxID_YES";
677 else if (name=="IDNO")
678 name="wxID_NO";
679 else if (name=="IDHELP")
680 name="wxID_HELP";
681
682 m_xmlfile.Write(" name= \""+name+"\"");
683 }
684
685 void rc2xml::WriteLabel(wxString label)
686 {
687 label.Replace("&","$");
688 m_xmlfile.Write("\t\t\t<label>"+label+"</label>\n");
689 }
690
691 void rc2xml::WriteBasicInfo(int x, int y, int width, int height, wxString name)
692 {
693 WriteName(name);
694 m_xmlfile.Write(">\n");
695 m_xmlfile.Write("\t\t\t");
696 WritePosition(x,y);
697 WriteSize(width,height);
698 m_xmlfile.Write("\n");
699 }
700
701 void rc2xml::WriteStyle(wxString style)
702 {
703 if (style.Length()==0)
704 return;
705 m_xmlfile.Write("\t\t\t<style>"+style+"</style>\n");
706 }
707 /*
708 LISTBOX IDC_LIST1,16,89,48,40,LBS_SORT | LBS_MULTIPLESEL |
709 LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
710 */
711 void rc2xml::ParseListBox()
712 {
713 wxString token;
714 wxString varname;
715 varname=GetToken();
716 int x,y,width,height;
717 ReadRect(x,y,width,height);
718
719 m_xmlfile.Write("\t\t<object class=\"wxListBox\"");
720 WriteBasicInfo(x,y,width,height,varname);
721 m_xmlfile.Write("\n\t\t</object>\n");
722
723 }
724 /*
725 CONTROL "",IDC_RICHEDIT1,"RICHEDIT",ES_AUTOHSCROLL | WS_BORDER |
726 WS_TABSTOP,103,110,40,14
727 */
728 void rc2xml::ParseRichEdit(wxString label, wxString varname)
729 {
730 wxString token;
731 //while (ReadOrs(token));
732 ReadOrs(token);
733 int x,y,width,height;
734 ReadRect(x,y,width,height);
735 wxString style;
736 //Make it a rich text control
737 style+="wxTE_MULTILINE ";
738 m_xmlfile.Write("\t\t<object class=\"wxTextCtrl\"");
739 WriteBasicInfo(x,y,width,height,varname);
740 WriteStyle(style);
741 m_xmlfile.Write("\t\t</object>\n");
742
743 }
744 /*
745 CONTROL "Spin1",IDC_SPIN1,"msctls_updown32",UDS_ARROWKEYS,209,72,
746 19,26
747 */
748 void rc2xml::ParseSpinCtrl(wxString label, wxString varname)
749 {
750 wxString token,style;
751
752 ReadOrs(token);
753 if (token.Find("UDS_HORZ")!=-1)
754 style="wxSP_HORIZONTAL";
755 //MFC default
756 else
757 style="wxSP_VERTICAL";
758
759 int x,y,width,height;
760 ReadRect(x,y,width,height);
761 m_xmlfile.Write("\t\t<object class=\"wxSpinButton\"");
762 WriteBasicInfo(x,y,width,height,varname);
763 WriteStyle(style);
764 m_xmlfile.Write("\n\t\t</object>\n");
765
766 }
767
768 void rc2xml::FirstPass()
769 {
770 wxString token,prevtok;
771 while (!m_done)
772 {
773 token=GetToken();
774 if (token=="BITMAP")
775 ParseBitmap(prevtok);
776 else if (token=="STRINGTABLE")
777 ParseStringTable(prevtok);
778 else if (token=="ICON")
779 ParseIcon(prevtok);
780
781 prevtok=token;
782 }
783 }
784
785 void rc2xml::ParseBitmap(wxString varname)
786 {
787 wxString token,*bitmapfile;
788
789 token=PeekToken();
790 //Microsoft notation?
791 if (token=="DISCARDABLE")
792 {
793 token=GetToken();
794 token=PeekToken();
795 }
796 bitmapfile=new wxString;
797 *bitmapfile=GetQuoteField();
798 m_bitmaplist->Append(varname,bitmapfile);
799
800 }
801
802
803 void rc2xml::SecondPass()
804 {
805 wxString token,prevtok;
806 while (!m_done)
807 {
808 token=GetToken();
809 if (token=="DIALOG")
810 ParseDialog(prevtok);
811 else if (token=="MENU")
812 ParseMenu(prevtok);
813 else if (token=="TOOLBAR")
814 ParseToolBar(prevtok);
815
816 prevtok=token;
817 }
818
819 }
820
821 void rc2xml::ParseToolBar(wxString varname)
822 {
823 wxString token;
824 token=GetToken();
825 wxASSERT_MSG(token=="DISCARDABLE","Error in toolbar parsing");
826 //Look up bitmap for toolbar and load
827 wxNode *node=m_bitmaplist->Find(LookUpId(varname));
828 wxString *bitmappath;
829 bitmappath=(wxString *)node->Data();
830 wxBitmap bitmap;
831 if (!bitmap.LoadFile(*bitmappath,wxBITMAP_TYPE_BMP ))
832 wxLogError("Unable to load bitmap:"+*bitmappath);
833
834 //Write toolbar to xml file
835 m_xmlfile.Write(" <object class=\"wxToolBar\"");
836 //Avoid duplicate names this way
837 varname.Replace("IDR_","TB_");
838 WriteName(varname);
839 m_xmlfile.Write(">\n");
840 wxString style;
841 style+="wxTB_FLAT";
842 WriteStyle(style);
843
844
845 //Grab width and height
846 int width,height;
847 width=atoi(GetToken());
848 height=atoi(GetToken());
849
850 int c=0;
851 wxString buttonname,msg,tip,longhelp;
852 token=GetToken();
853 while ((token!="BEGIN"))
854 token=GetToken();
855
856 while ((token!="END")&(token!="}"))
857 {
858 if (token=="BUTTON")
859 {
860 buttonname=GetToken();
861 m_xmlfile.Write("\t\t\t<object class=\"tool\"");
862 WriteName(buttonname);
863 m_xmlfile.Write(">\n");
864 //Write tool tip if any
865 if (LookUpString(buttonname,msg))
866 {
867 SplitHelp(msg,tip,longhelp);
868 m_xmlfile.Write("\t\t\t\t<tooltip>"+tip+"</tooltip>\n");
869 m_xmlfile.Write(" <longhelp>"+longhelp+"</longhelp>\n");
870 }
871 //Make a bitmap file name
872 buttonname=CleanName(buttonname);
873 buttonname+=".bmp";
874 m_xmlfile.Write("\t\t\t\t<bitmap>"+buttonname+"</bitmap>\n");
875 WriteToolButton(buttonname,c,width,height,bitmap);
876 m_xmlfile.Write("\t\t\t</object>\n");
877 c++;
878 }
879 else if (token=="SEPARATOR")
880 {
881 m_xmlfile.Write("\t\t\t<object class=\"separator\"/>\n");
882 }
883 token=GetToken();
884 }
885 m_xmlfile.Write("\t</object>\n");
886 }
887
888 //Extract bitmaps from larger toolbar bitmap
889 void rc2xml::WriteToolButton(wxString name,int index, int width, int height, wxBitmap bitmap)
890 {
891 int x;
892 x=index*width;
893 wxRect r(x,0,width,height);
894 wxBitmap little;
895 little=bitmap.GetSubBitmap(r);
896 little.SaveFile(m_targetpath+name,wxBITMAP_TYPE_BMP);
897 }
898
899 void rc2xml::ParseStringTable(wxString varname)
900 {
901 wxString token;
902 token=GetToken();
903 while ((token!="BEGIN"))
904 token=GetToken();
905 token=GetToken();
906 wxString *msg;
907
908 while ((token!="END")&(token!="}"))
909 {
910 msg=new wxString;
911 *msg=GetQuoteField();
912 m_stringtable->Append(token,msg);
913 token=GetToken();
914 }
915
916 }
917
918 bool rc2xml::LookUpString(wxString strid,wxString & st)
919 {
920 wxNode *node=m_stringtable->Find(strid);
921 wxString *s;
922 if (node==NULL)
923 return FALSE;
924
925 s=(wxString *)node->Data();
926 st=*s;
927
928 return TRUE;
929 }
930
931 bool rc2xml::SplitHelp(wxString msg, wxString &shorthelp, wxString &longhelp)
932 {
933 int spot;
934 spot=msg.Find("\\n");
935 if (spot==-1)
936 {
937 shorthelp=msg;
938 longhelp=msg;
939 }
940
941 longhelp=msg.Left(spot);
942 spot=msg.Length()-spot-2;
943 shorthelp=msg.Right(spot);
944 return TRUE;
945 }
946
947 void rc2xml::ParseMenuItem()
948 {
949 wxString token,name,msg,tip,longhelp;
950 //int spot;
951 if (PeekToken()=="SEPARATOR")
952 {
953 m_xmlfile.Write("\t\t\t<object class=\"separator\"/>\n");
954 return;
955 }
956
957 token=GetQuoteField();
958 name=GetToken();
959 //Remove \t because it causes problems
960 //spot=token.First("\\t");
961 //token=token.Left(spot);
962 m_xmlfile.Write("\t\t\t<object class=\"wxMenuItem\"");
963 WriteName(name);
964 m_xmlfile.Write(">\n");
965 WriteLabel(token);
966 //Look up help if any listed in stringtable
967 if (LookUpString(name,msg))
968 {
969 SplitHelp(msg,tip,longhelp);
970 m_xmlfile.Write("\t\t\t<help>"
971 +longhelp+"</help>\n");
972 }
973 //look for extra attributes like checked and break
974 wxString ptoken;
975 ptoken=PeekToken();
976 while ((ptoken!="MENUITEM")&(ptoken!="POPUP")&(ptoken!="END"))
977 {
978 token=GetToken();
979 if (token=="CHECKED")
980 m_xmlfile.Write("\t\t\t<checkable>1</checkable>\n");
981 else if (token=="MENUBREAK");
982 //m_xmlfile.Write("\t\t\t</break>\n");
983 else if (token=="GRAYED");
984 else
985 wxLogError("Unknown Menu Item token:"+token);
986
987 ptoken=PeekToken();
988 }
989 m_xmlfile.Write("\t\t\t</object>\n");
990
991 }
992
993 //ICON IDR_MAINFRAME,IDC_STATIC,11,17,20,20
994 void rc2xml::ParseIconStatic()
995 {
996 wxString token;
997 wxString varname,iconname;
998 iconname=GetToken();
999 //Look up icon
1000 varname=GetToken();
1001
1002 int x,y,width,height;
1003 ReadRect(x,y,width,height);
1004
1005 m_xmlfile.Write("\t\t<object class=\"wxStaticBitmap\"");
1006 WriteBasicInfo(x,y,width,height,varname);
1007 //Save icon as a bitmap
1008 WriteIcon(iconname);
1009 m_xmlfile.Write("\t\t</object>\n");
1010
1011 }
1012 //IDR_MAINFRAME ICON DISCARDABLE "res\\mfcexample.ico"
1013 void rc2xml::ParseIcon(wxString varname)
1014 {
1015 wxString token,*iconfile;
1016 iconfile=new wxString;
1017 token=PeekToken();
1018
1019 *iconfile=GetQuoteField();
1020 m_iconlist->Append(varname,iconfile);
1021
1022
1023 }
1024
1025 wxString rc2xml::CleanName(wxString name)
1026 {
1027 name.MakeLower();
1028 name.Replace("id_","");
1029 name.Replace("idr_","");
1030 name.Replace("idb_","");
1031 name.Replace("idc_","");
1032 name.Replace(".ico","");
1033 name.Replace(".bmp","");
1034 return name;
1035 }
1036 // And the award for most messed up control goes to...
1037 // CONTROL IDB_FACE,IDC_STATIC,"Static",SS_BITMAP,26,62,32,30
1038 void rc2xml::ParseStaticBitmap(wxString bitmapname, wxString varname)
1039 {
1040 wxString token;
1041 //Grab SS_BITMAP
1042 token=GetToken();
1043
1044
1045
1046 int x,y,width,height;
1047 ReadRect(x,y,width,height);
1048
1049 m_xmlfile.Write("\t\t<object class=\"wxStaticBitmap\"");
1050 WriteBasicInfo(x,y,width,height,varname);
1051 WriteBitmap(bitmapname);
1052 m_xmlfile.Write("\t\t</object>\n");
1053
1054 }
1055
1056 void rc2xml::ParseNormalMSControl()
1057 {
1058 wxString label,varname,kindctrl;
1059
1060 label=GetQuoteField();
1061 varname=GetToken();
1062 kindctrl=GetQuoteField();
1063 kindctrl.MakeUpper();
1064
1065 if (kindctrl=="MSCTLS_UPDOWN32")
1066 ParseSpinCtrl(label,varname);
1067 if (kindctrl=="MSCTLS_TRACKBAR32")
1068 ParseSlider(label,varname);
1069 if (kindctrl=="MSCTLS_PROGRESS32")
1070 ParseProgressBar(label,varname);
1071 if (kindctrl=="SYSTREEVIEW32")
1072 ParseTreeCtrl(label,varname);
1073 if (kindctrl=="SYSMONTHCAL32")
1074 ParseCalendar(label,varname);
1075 if (kindctrl=="SYSLISTVIEW32")
1076 ParseListCtrl(label,varname);
1077 if (kindctrl=="BUTTON")
1078 ParseCtrlButton(label,varname);
1079 if (kindctrl=="RICHEDIT")
1080 ParseRichEdit(label,varname);
1081
1082 }
1083
1084 void rc2xml::ParseWeirdMSControl()
1085 {
1086 wxString kindctrl;
1087 wxString varname;
1088 wxString id;
1089 id=GetToken();
1090 varname=GetToken();
1091 kindctrl=GetQuoteField();
1092 kindctrl.MakeUpper();
1093 // CONTROL IDB_FACE,IDC_STATIC,"Static",SS_BITMAP,26,62,32,30
1094 if (kindctrl=="STATIC")
1095 {
1096 if (PeekToken()=="SS_BITMAP")
1097 ParseStaticBitmap(id,varname);
1098 else
1099 wxLogError("Unknown MS Control Static token");
1100 }
1101
1102 }
1103 //SCROLLBAR IDC_SCROLLBAR1,219,56,10,40,SBS_VERT
1104
1105 void rc2xml::ParseScrollBar()
1106 {
1107 wxString token;
1108 wxString varname;
1109
1110 varname=GetToken();
1111 int x,y,width,height;
1112 ReadRect(x,y,width,height);
1113 wxString style;
1114
1115 ReadOrs(token);
1116
1117 if (token.Find("SBS_VERT")!=-1)
1118 style=_T("wxSB_VERTICAL");
1119 //Default MFC style is horizontal
1120 else
1121 style=_T("wxSB_HORIZONTAL");
1122
1123 m_xmlfile.Write("\t\t<object class=\"wxScrollBar\"");
1124 WriteBasicInfo(x,y,width,height,varname);
1125 WriteStyle(style);
1126 m_xmlfile.Write("\n\t\t</object>\n");
1127
1128 }
1129 // CONTROL "Tree1",IDC_TREE1,"SysTreeView32",WS_BORDER | WS_TABSTOP,
1130 // 7,7,66,61
1131
1132 void rc2xml::ParseTreeCtrl(wxString label, wxString varname)
1133 {
1134 wxString token;
1135 //while (ReadOrs(token));
1136 ReadOrs(token);
1137 int x,y,width,height;
1138 ReadRect(x,y,width,height);
1139 m_xmlfile.Write("\t\t<object class=\"wxTreeCtrl\"");
1140 WriteBasicInfo(x,y,width,height,varname);
1141 m_xmlfile.Write("\t\t</object>\n");
1142
1143 }
1144 // CONTROL "MonthCalendar1",IDC_MONTHCALENDAR1,"SysMonthCal32",
1145 //MCS_NOTODAY | WS_TABSTOP,105,71,129,89
1146
1147 void rc2xml::ParseCalendar(wxString label, wxString varname)
1148 {
1149 wxString token;
1150 //while (ReadOrs(token));
1151 ReadOrs(token);
1152 int x,y,width,height;
1153 ReadRect(x,y,width,height);
1154 m_xmlfile.Write("\t\t<object class=\"wxCalendarCtrl\"");
1155 WriteBasicInfo(x,y,width,height,varname);
1156 m_xmlfile.Write("\t\t</object>\n");
1157 }
1158 // CONTROL "List1",IDC_LIST1,"SysListView32",WS_BORDER | WS_TABSTOP,
1159 // 7,89,68,71
1160
1161 void rc2xml::ParseListCtrl(wxString label, wxString varname)
1162 {
1163 wxString token;
1164 //while (ReadOrs(token));
1165 ReadOrs(token);
1166 int x,y,width,height;
1167 ReadRect(x,y,width,height);
1168 m_xmlfile.Write("\t\t<object class=\"wxListCtrl\"");
1169 WriteBasicInfo(x,y,width,height,varname);
1170 m_xmlfile.Write("\t\t</object>\n");
1171
1172 }
1173
1174 void rc2xml::WriteBitmap(wxString bitmapname)
1175 {
1176 //Look up bitmap
1177 wxNode *node=m_bitmaplist->Find(LookUpId(bitmapname));
1178 if (node==NULL)
1179 {
1180 m_xmlfile.Write("\t\t\t<bitmap>missingfile</bitmap>\n");
1181 wxLogError("Unable to find bitmap:"+bitmapname);
1182 return;
1183 }
1184
1185 wxString *bitmappath;
1186 bitmappath=(wxString *)node->Data();
1187 bitmapname=wxFileNameFromPath(*bitmappath);
1188 wxBitmap bitmap;
1189 if (!bitmap.LoadFile(*bitmappath,wxBITMAP_TYPE_BMP ))
1190 wxLogError("Unable to load bitmap:"+*bitmappath);
1191
1192 //Make a bitmap file name
1193 bitmapname=CleanName(bitmapname);
1194 bitmapname+=".bmp";
1195 m_xmlfile.Write("\t\t\t<bitmap>"+bitmapname+"</bitmap>\n");
1196 bitmap.SaveFile(m_targetpath+bitmapname,wxBITMAP_TYPE_BMP);
1197 }
1198
1199 void rc2xml::WriteIcon(wxString iconname)
1200 {
1201 wxNode *node=m_iconlist->Find(iconname);
1202 if (node==NULL)
1203 {
1204 m_xmlfile.Write("\t\t\t<bitmap>missing_file</bitmap>\n");
1205 wxLogError("Unable to find icon:"+iconname);
1206 }
1207 wxString *iconpath;
1208 iconpath=(wxString *)node->Data();
1209 wxIcon icon;
1210 wxBitmap bitmap;
1211 if (!icon.LoadFile(*iconpath,wxBITMAP_TYPE_ICO ))
1212 wxLogError("Unable to load icon:"+*iconpath);
1213 #ifdef __WXMSW__
1214 bitmap.CopyFromIcon(icon);
1215 #else
1216 bitmap = icon;
1217 #endif
1218 iconname=wxFileNameFromPath(*iconpath);
1219 //Make a bitmap file name
1220 iconname=CleanName(iconname);
1221 iconname+=".bmp";
1222 m_xmlfile.Write("\t\t\t<bitmap>"+iconname+"</bitmap>\n");
1223 bitmap.SaveFile(m_targetpath+iconname,wxBITMAP_TYPE_BMP);
1224
1225
1226 }
1227 /*Unfortunately sometimes the great MSVC Resource editor decides
1228 to use numbers instead of the word id. I have no idea why they
1229 do this, but that is the way it is.
1230 */
1231 /* this is a quick and dirty way to parse the resource.h file
1232 it will not recognize #ifdef so it can be easily fooled
1233 */
1234 void rc2xml::ParseResourceHeader()
1235 {
1236 wxTextFile r;
1237 //Attempt to load resource.h in current path
1238 if (!r.Open("resource.h"))
1239 {
1240 wxLogError("Warining Unable to load resource.h file");
1241 return;
1242 }
1243
1244 wxString str;
1245 wxString id,v;
1246 wxStringTokenizer tok;
1247 wxString *varname;
1248
1249
1250 long n;
1251
1252 //Read through entire file
1253 for ( str = r.GetFirstLine(); !r.Eof(); str = r.GetNextLine() )
1254 {
1255 if (str.Find("#define")!=-1)
1256 {
1257 tok.SetString(str);
1258 //Just ignore #define token
1259 tok.GetNextToken();
1260 v=tok.GetNextToken();
1261 id=tok.GetNextToken();
1262 if (id.IsNumber())
1263 {
1264 varname=new wxString;
1265 id.ToLong(&n);
1266 *varname=v;
1267 m_resourcelist->Append(n,varname);
1268 }
1269 }
1270 }
1271
1272
1273
1274 }
1275
1276
1277 wxString rc2xml::LookUpId(wxString id)
1278 {
1279 wxString st;
1280
1281 if (!id.IsNumber())
1282 return id;
1283 long n;
1284 id.ToLong(&n);
1285 wxNode *node=m_resourcelist->Find(n);
1286 wxString *s;
1287 if (node==NULL)
1288 return id;
1289
1290 s=(wxString *)node->Data();
1291 st=*s;
1292 return st;
1293 }