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