]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/convertrc/rc2wxr.cpp
minor change: buf[(size_t)pos] instead of buf.c_str()[pos]
[wxWidgets.git] / contrib / utils / convertrc / rc2wxr.cpp
1 // rc2wxr.cpp: implementation of the rc2wxr class.
2 //
3 //////////////////////////////////////////////////////////////////////
4 //Author: Brian Gavin 9/24/00
5 //License: wxWindows License
6 /*
7 WARNING- I know this code has some bugs to work out but
8 I don't plan to fix them since I feel that wxr files will
9 not be used much longer.
10 This code was used as a starting point for my rc2xml converter
11 */
12 #ifdef __GNUG__
13 #pragma implementation "rc2wxr.cpp"
14 #pragma interface "rc2wxr.cpp"
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include <wx/wxprec.h>
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 // for all others, include the necessary headers (this file is usually all you
25 // need because it includes almost all "standard" wxWindows headers
26 #ifndef WX_PRECOMP
27 #include <wx/wx.h>
28 #endif
29
30
31 #include "rc2wxr.h"
32 #include "wx/image.h"
33 #include "wx/resource.h"
34 //////////////////////////////////////////////////////////////////////
35 // Construction/Destruction
36 //////////////////////////////////////////////////////////////////////
37
38 rc2wxr::rc2wxr()
39 {
40 m_done=FALSE;
41 m_controlid=6000;
42 }
43
44 rc2wxr::~rc2wxr()
45 {
46
47 }
48
49 void rc2wxr::Convert(wxString wxrfile, wxString rcfile)
50 {
51 m_rc.Open(rcfile);
52 m_filesize=m_rc.Length();
53 if( (m_wxr = fopen( wxrfile, "wt" )) == NULL )
54 {
55 return;
56 }
57
58
59 wxString tok,prevtok;
60
61
62 while (!m_done)
63 {
64
65 tok=GetToken();
66
67 if (tok=="DIALOG")
68 {
69 ParseDialog(prevtok);
70 }
71
72
73 if (tok=="MENU")
74 {
75 ParseMenu(prevtok);
76 }
77
78 prevtok=tok;
79 }
80
81 fclose(m_wxr);
82 //fclose(m_rc);
83 m_rc.Close();
84
85 }
86
87
88 /*
89 Example .rc
90 Microsoft style as of v5.0
91 IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 217, 55
92 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
93 CAPTION "About Funimator"
94 FONT 8, "MS Sans Serif"
95
96 Borland 4.5 style rc
97 IDD_DIBATTR DIALOG 7, 16, 172, 119
98 STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
99 CAPTION "DIB Attributes"
100 FONT 8, "MS Sans Serif"
101 {
102 DEFPUSHBUTTON "Ok", IDOK, 114, 8, 50, 14
103 PUSHBUTTON "Cancel", IDCANCEL, 114, 28, 50, 14
104
105
106
107 */
108 void rc2wxr::ParseDialog(wxString dlgname)
109 {
110 wxString tok;
111 static int dlgid=999;
112 dlgid++;
113 /* Make sure that this really is a dialog
114 microsoft reuses the keyword DIALOG for other things
115 */
116 tok=PeekToken();
117 //Microsoft notation?
118 if (tok=="DISCARDABLE")
119 {
120 tok=GetToken();
121 tok=PeekToken();
122 }
123 //This isn't a Dialog resource eject eject
124 if (!tok.IsNumber())
125 return;
126 //Generate Dialog text
127 fprintf(m_wxr,"static char *dialog%i = \"dialog(name = '%s',\\\n",dlgid,dlgname);
128 //be lazy about style for now. add it later
129 fprintf(m_wxr,"style = 'wxRAISED_BORDER | wxCAPTION | wxTHICK_FRAME | wxSYSTEM_MENU',\\\n");
130
131 fprintf(m_wxr,"id = %i,\\\n",dlgid);
132
133 //Record x,y,width,height
134 int x,y,width,height;
135 ReadRect(x,y,width,height);
136 fprintf(m_wxr,"x = %i, y = %i, width = %i, height = %i,\\\n",x,y,width,height);
137
138
139 //CAPTION "About Funimator"
140 //Get Title
141 tok=GetToken();
142 wxString title;
143
144 while ((tok!="BEGIN")&(tok!="{"))
145 {
146 if (tok=="CAPTION")
147 {
148 title=GetQuoteField();
149 fprintf(m_wxr,"title = '%s',\\\n",title);
150 }
151 tok=GetToken();
152 }
153 fprintf(m_wxr,"use_dialog_units = 1,\\\n");
154 fprintf(m_wxr,"use_system_defaults = 0,\\\n");
155
156 fprintf(m_wxr,"font = [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif'],\\\n");
157 ParseControls();
158 fprintf(m_wxr,").\";\n\n");
159 }
160
161 /*
162 BEGIN
163
164
165
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 rc2wxr::ParseControls()
173 {
174 wxString tok;
175
176 tok=GetToken();
177 while ((tok!="END")&(tok!="}"))
178 {
179 if (tok=="LTEXT")
180 ParseStaticText();
181 if (tok=="EDITTEXT")
182 ParseTextCtrl();
183 if (tok=="PUSHBUTTON")
184 ParsePushButton();
185 if (tok=="DEFPUSHBUTTON")
186 ParsePushButton();
187 if (tok=="GROUPBOX")
188 ParseGroupBox();
189 if (tok=="COMBOBOX")
190 ParseComboBox();
191 if (tok=="CONTROL")
192 ParseControlMS();
193
194 tok=GetToken();
195 }
196
197 }
198 //LTEXT "Radius",IDC_STATIC,9,67,23,8
199 void rc2wxr::ParseStaticText()
200 {
201 wxString tok;
202 wxString phrase,varname;
203 phrase=GetQuoteField();
204 varname=GetToken();
205 m_controlid++;
206 int x,y,width,height;
207 ReadRect(x,y,width,height);
208 fprintf(m_wxr," control = [%i,wxStaticText,'%s','0','%s',",m_controlid,phrase,varname);
209 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
210 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
211 }
212 //EDITTEXT IDC_RADIUS,36,65,40,14,ES_AUTOHSCROLL
213 void rc2wxr::ParseTextCtrl()
214 {
215 wxString tok;
216 wxString varname;
217 varname=GetToken();
218 m_controlid++;
219 int x,y,width,height;
220 ReadRect(x,y,width,height);
221 fprintf(m_wxr," control = [%i,wxTextCtrl,'','0','%s',",m_controlid,varname);
222 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
223 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
224
225 }
226 //PUSHBUTTON "Create/Update",IDC_CREATE,15,25,53,13,NOT WS_TABSTOP
227 void rc2wxr::ParsePushButton()
228 {
229 wxString tok;
230 wxString phrase,varname;
231 phrase=GetQuoteField();
232 varname=GetToken();
233 int c;
234 m_controlid++;
235 c=m_controlid;
236 if (varname=="IDOK")
237 c=wxID_OK;
238
239 if (varname=="IDCANCEL")
240 c=wxID_CANCEL;
241
242 if (varname=="IDAPPLY")
243 c=wxID_APPLY;
244
245 int x,y,width,height;
246 ReadRect(x,y,width,height);
247 fprintf(m_wxr," control = [%i,wxButton,'%s','0','%s',",c,phrase,varname);
248 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
249 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
250
251 }
252
253
254 bool rc2wxr::Seperator(int ch)
255 {
256 if ((ch==' ')|(ch==',')|(ch==13)|(ch==10)|(ch=='|'))
257 return TRUE;
258
259 if (ch==EOF)
260 {
261 m_done=TRUE;
262 return TRUE;
263 }
264 return FALSE;
265 }
266
267 void rc2wxr::ParseGroupBox()
268 {
269 // GROUPBOX "Rotate",IDC_STATIC,1,1,71,79
270 wxString tok;
271 wxString phrase,varname;
272 phrase=GetQuoteField();
273 varname=GetToken();
274 m_controlid++;
275 int x,y,width,height;
276 ReadRect(x,y,width,height);
277 fprintf(m_wxr," control = [%i,wxStaticBox,'%s','0','%s',",m_controlid,phrase,varname);
278 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
279 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
280
281
282 }
283
284 void rc2wxr::ReadRect(int & x, int & y, int & width, int & height)
285 {
286 x=atoi(GetToken());
287 y=atoi(GetToken());
288 width=atoi(GetToken());
289 height=atoi(GetToken());
290
291 }
292
293 wxString rc2wxr::GetToken()
294 {
295 wxString tok="";
296
297 if (m_rc.Eof())
298 {
299 m_done=TRUE;
300 return tok;
301 }
302
303 int ch=0;
304 ReadChar(ch);
305 if (ch==EOF)
306 {
307 m_done=TRUE;
308 return tok;
309 }
310
311 while (Seperator(ch))
312 {
313 ReadChar(ch);
314 if (m_done)
315 return tok;
316 }
317
318 if (ch==EOF)
319 {
320 m_done=TRUE;
321
322 }
323
324
325 while (!Seperator(ch))
326 {
327 tok+=(char)ch;
328 ReadChar(ch);
329
330 }
331
332 if (ch==EOF)
333 m_done=TRUE;
334
335
336 return tok;
337 }
338
339 wxString rc2wxr::GetQuoteField()
340 {
341 wxString phrase;
342 //ASCII code 34 "
343 int ch=0;
344 ReadChar(ch);
345
346 while (ch!=34)
347 ReadChar(ch);
348
349 ReadChar(ch);
350
351 while (ch!=34)
352 {
353 phrase+=(char)ch;
354 ReadChar(ch);
355 }
356 return phrase;
357 }
358
359 void rc2wxr::ReadChar(int &ch)
360 {
361 int result;
362 result=m_rc.Tell();
363
364 if((result>=m_filesize))
365 m_done=TRUE;
366
367 result=m_rc.Read(&ch,1);
368
369 if((result==-1))
370 m_done=TRUE;
371
372 if(ch==EOF)
373 m_done=TRUE;
374 }
375
376 void rc2wxr::ParseComboBox()
377 {
378 /* COMBOBOX IDC_SCALECOMBO,10,110,48,52,CBS_DROPDOWNLIST | CBS_SORT |
379 WS_VSCROLL | WS_TABSTOP */
380 wxString tok;
381 wxString varname;
382 varname=GetToken();
383 m_controlid++;
384 int x,y,width,height;
385 ReadRect(x,y,width,height);
386
387 fprintf(m_wxr," control = [%i,wxChoice,'','0','%s',",m_controlid,varname);
388 fprintf(m_wxr,"%i,%i,%i,%i,[],\\\n",x,y,width,height);
389 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
390
391
392 }
393
394 void rc2wxr::ParseMenu(wxString name)
395 {
396 wxString tok="";
397 static int menuid=0;
398 menuid++;
399 fprintf(m_wxr,"static char *MenuBar%i = \"menu(name = '%s',\\\n",menuid,name);
400 fprintf(m_wxr,"menu = \\\n");
401 fprintf(m_wxr,"[\\\n");
402
403 while ((tok!="BEGIN")&(tok!="{"))
404 tok=GetToken();
405
406 while ((tok!="END")&(tok!="}"))
407 {
408 tok=GetToken();
409 if (tok=="POPUP")
410 {
411 ParsePopupMenu();
412 fprintf(m_wxr," ],\\\n");
413 }
414 }
415
416 fprintf(m_wxr,"]).\";\n\n");
417 }
418
419 void rc2wxr::ParsePopupMenu()
420 {
421 static int menuitem=99;
422 menuitem++;
423
424 wxString tok;
425 tok=GetQuoteField();
426 int spot;
427 //Remove /t because it causes problems
428 spot=tok.First("\\t");
429 tok=tok.Left(spot);
430 fprintf(m_wxr," ['%s',%i,'',\\\n",tok,menuitem);
431 while ((tok!="BEGIN")&(tok!="{"))
432 tok=GetToken();
433
434 while ((tok!="END")&(tok!="}"))
435 {
436 tok=GetToken();
437 if (tok=="MENUITEM")
438 {
439 if (PeekToken()=="SEPARATOR")
440 fprintf(m_wxr," [],\\\n");
441 else
442 {
443 tok=GetQuoteField();
444 //Remove /t because it causes problems
445 spot=tok.First("\\t");
446 tok=tok.Left(spot);
447 menuitem++;
448 fprintf(m_wxr," ['%s',%i,''],\\\n",tok,menuitem);
449 }
450 }
451
452 }
453
454
455 }
456
457 wxString rc2wxr::PeekToken()
458 {
459 wxString tok;
460 int p;
461 p=m_rc.Tell();
462 tok=GetToken();
463
464 m_rc.Seek(p);
465 return tok;
466 }
467 //Windows pain in the butt CONTROL
468 void rc2wxr::ParseControlMS()
469 {
470 wxString label,varname,kindctrl,tok;
471 label=GetQuoteField();
472 varname=GetToken();
473 kindctrl=GetQuoteField();
474 kindctrl.MakeUpper();
475
476
477 if (kindctrl=="MSCTLS_TRACKBAR32")
478 ParseSlider(label,varname);
479 if (kindctrl=="MSCTLS_PROGRESS32")
480 ParseProgressBar(label,varname);
481 if (kindctrl=="BUTTON")
482 ParseCtrlButton(label,varname);
483 }
484 /* CONTROL "Slider1",IDC_SLIDER1,"msctls_trackbar32",TBS_BOTH |
485 TBS_NOTICKS | WS_TABSTOP,52,73,100,15
486 */
487
488 void rc2wxr::ParseSlider(wxString label, wxString varname)
489 {
490 wxString tok;
491 while (ReadOrs(tok));
492 fprintf(m_wxr," control = [%i,wxSlider,'','wxSL_HORIZONTAL','%s',",m_controlid,varname);
493 int x,y,width,height;
494 ReadRect(x,y,width,height);
495 fprintf(m_wxr,"%i,%i,%i,%i,",x,y,width,height);
496 fprintf(m_wxr," 1, 1, 10,\\\n");
497 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
498 }
499 /*
500 CONTROL "Progress1",CG_IDC_PROGDLG_PROGRESS,"msctls_progress32",
501 WS_BORDER,15,52,154,13
502 */
503 void rc2wxr::ParseProgressBar(wxString label, wxString varname)
504 {
505 wxString tok;
506 while (ReadOrs(tok));
507 fprintf(m_wxr," control = [%i,wxGauge,'','wxGA_HORIZONTAL','%s',",m_controlid,varname);
508 int x,y,width,height;
509 ReadRect(x,y,width,height);
510 fprintf(m_wxr,"%i,%i,%i,%i,",x,y,width,height);
511 fprintf(m_wxr," 0, 10,\\\n");
512 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
513 }
514
515 bool rc2wxr::ReadOrs(wxString & w)
516 {
517 wxString tok;
518 tok=PeekToken();
519 if (tok.IsNumber())
520 return false;
521 w=GetToken();
522 return TRUE;
523 }
524
525 //Is it a check button or a radio button
526 void rc2wxr::ParseCtrlButton(wxString label, wxString varname)
527 {
528 wxString tok;
529 tok=GetToken();
530
531 m_controlid++;
532 int x,y,width,height;
533
534 if (tok=="BS_AUTOCHECKBOX")
535 {
536 fprintf(m_wxr," control = [%i,wxCheckBox,'%s','0','%s',",m_controlid,label,varname);
537 while (ReadOrs(tok));
538 ReadRect(x,y,width,height);
539 fprintf(m_wxr,"%i,%i,%i,%i,0,\\\n",x,y,width,height);
540 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
541 }
542
543 if (tok=="BS_AUTORADIOBUTTON")
544 {
545 fprintf(m_wxr," control = [%i,wxRadioButton,'%s','0','%s',",m_controlid,label,varname);
546 while(ReadOrs(tok));
547 ReadRect(x,y,width,height);
548 fprintf(m_wxr,"%i,%i,%i,%i,0,\\\n",x,y,width,height);
549 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
550 }
551
552
553
554 }
555