]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/convertrc/rc2wxr.cpp
Applied patch [ 1174270 ] small fixes to wxGenericDirCtrl
[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
13 #ifdef __GNUG__
14 #pragma implementation "rc2wxr.cpp"
15 #pragma interface "rc2wxr.cpp"
16 #endif
17
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 // for all others, include the necessary headers (this file is usually all you
26
27 // need because it includes almost all "standard" wxWidgets headers
28
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/image.h"
34 #include "wx/deprecated/setup.h"
35 #include "wx/deprecated/resource.h"
36
37 #include "rc2wxr.h"
38
39 //////////////////////////////////////////////////////////////////////
40 // Construction/Destruction
41 //////////////////////////////////////////////////////////////////////
42
43 rc2wxr::rc2wxr()
44 {
45 m_done=false;
46 m_controlid=6000;
47 }
48
49 rc2wxr::~rc2wxr()
50 {
51 }
52
53 void rc2wxr::Convert(wxString wxrfile, wxString rcfile)
54 {
55 m_rc.Open(rcfile);
56 m_filesize=m_rc.Length();
57 if( (m_wxr = wxFopen( wxrfile, _T("wt") )) == NULL )
58 {
59 return;
60 }
61
62 wxString tok,prevtok;
63
64 while (!m_done)
65 {
66 tok=GetToken();
67
68 if (tok==_T("DIALOG"))
69 {
70 ParseDialog(prevtok);
71 }
72
73 if (tok==_T("MENU"))
74 {
75 ParseMenu(prevtok);
76 }
77
78 prevtok=tok;
79 }
80
81 fclose(m_wxr);
82
83 m_rc.Close();
84 }
85
86
87 /*
88
89 Example .rc
90
91 Microsoft style as of v5.0
92
93 IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 217, 55
94
95 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
96
97 CAPTION "About Funimator"
98
99 FONT 8, "MS Sans Serif"
100
101 Borland 4.5 style rc
102
103 IDD_DIBATTR DIALOG 7, 16, 172, 119
104
105 STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
106
107 CAPTION "DIB Attributes"
108
109 FONT 8, "MS Sans Serif"
110
111 {
112
113 DEFPUSHBUTTON "Ok", IDOK, 114, 8, 50, 14
114
115 PUSHBUTTON "Cancel", IDCANCEL, 114, 28, 50, 14
116
117 */
118
119 void rc2wxr::ParseDialog(wxString dlgname)
120
121 {
122
123 wxString tok;
124
125 static int dlgid=999;
126
127 dlgid++;
128
129 /* Make sure that this really is a dialog
130
131 microsoft reuses the keyword DIALOG for other things
132
133 */
134
135 tok=PeekToken();
136
137 //Microsoft notation?
138
139 if (tok==_T("DISCARDABLE"))
140
141 {
142
143 tok=GetToken();
144
145 tok=PeekToken();
146
147 }
148
149 //This isn't a Dialog resource eject eject
150
151 if (!tok.IsNumber())
152
153 return;
154
155 //Generate Dialog text
156
157 wxFprintf(m_wxr,_T("static char *dialog%i = \"dialog(name = '%s',\\\n"),dlgid,dlgname.c_str());
158
159 //be lazy about style for now. add it later
160
161 wxFprintf(m_wxr,_T("style = 'wxRAISED_BORDER | wxCAPTION | wxTHICK_FRAME | wxSYSTEM_MENU',\\\n"));
162 wxFprintf(m_wxr,_T("id = %i,\\\n"),dlgid);
163
164 //Record x,y,width,height
165
166 int x,y,width,height;
167
168 ReadRect(x,y,width,height);
169
170 wxFprintf(m_wxr,_T("x = %i, y = %i, width = %i, height = %i,\\\n"),x,y,width,height);
171
172
173 //CAPTION "About Funimator"
174
175 //Get Title
176
177 tok=GetToken();
178
179 wxString title;
180
181
182
183 while ((tok!=_T("BEGIN"))&(tok!=_T("{")))
184
185 {
186
187 if (tok==_T("CAPTION"))
188
189 {
190
191 title=GetQuoteField();
192
193 wxFprintf(m_wxr,_T("title = '%s',\\\n"),title.c_str());
194
195 }
196
197 tok=GetToken();
198
199 }
200
201 wxFprintf(m_wxr,_T("use_dialog_units = 1,\\\n"));
202
203 wxFprintf(m_wxr,_T("use_system_defaults = 0,\\\n"));
204
205
206
207 wxFprintf(m_wxr,_T("font = [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif'],\\\n"));
208
209 ParseControls();
210
211 wxFprintf(m_wxr,_T(").\";\n\n"));
212
213 }
214
215
216
217 /*
218
219 BEGIN
220
221
222
223
224
225
226
227 EDITTEXT IDC_BANDS,36,83,22,14,ES_AUTOHSCROLL | ES_NUMBER | NOT
228
229 WS_TABSTOP
230
231 LTEXT "Bands",IDC_STATIC,11,86,21,8
232
233 EDITTEXT IDC_NAME,10,3,75,14,ES_AUTOHSCROLL
234
235 END
236
237 */
238
239 void rc2wxr::ParseControls()
240
241 {
242
243 wxString tok;
244
245
246
247 tok=GetToken();
248
249 while ((tok!=_T("END"))&(tok!=_T("}")))
250
251 {
252
253 if (tok==_T("LTEXT"))
254
255 ParseStaticText();
256
257 if (tok==_T("EDITTEXT"))
258
259 ParseTextCtrl();
260
261 if (tok==_T("PUSHBUTTON"))
262
263 ParsePushButton();
264
265 if (tok==_T("DEFPUSHBUTTON"))
266
267 ParsePushButton();
268
269 if (tok==_T("GROUPBOX"))
270
271 ParseGroupBox();
272
273 if (tok==_T("COMBOBOX"))
274
275 ParseComboBox();
276
277 if (tok==_T("CONTROL"))
278
279 ParseControlMS();
280
281
282
283 tok=GetToken();
284
285 }
286
287
288
289 }
290
291 //LTEXT "Radius",IDC_STATIC,9,67,23,8
292
293 void rc2wxr::ParseStaticText()
294
295 {
296
297 wxString tok;
298
299 wxString phrase,varname;
300
301 phrase=GetQuoteField();
302
303 varname=GetToken();
304
305 m_controlid++;
306
307 int x,y,width,height;
308
309 ReadRect(x,y,width,height);
310
311 wxFprintf(m_wxr,_T(" control = [%i,wxStaticText,'%s','0','%s',"),m_controlid,phrase.c_str(),varname.c_str());
312
313 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
314
315 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
316
317 }
318
319 //EDITTEXT IDC_RADIUS,36,65,40,14,ES_AUTOHSCROLL
320
321 void rc2wxr::ParseTextCtrl()
322
323 {
324
325 wxString tok;
326
327 wxString varname;
328
329 varname=GetToken();
330
331 m_controlid++;
332
333 int x,y,width,height;
334
335 ReadRect(x,y,width,height);
336
337 wxFprintf(m_wxr,_T(" control = [%i,wxTextCtrl,'','0','%s',"),m_controlid,varname.c_str());
338
339 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
340
341 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
342
343
344
345 }
346
347 //PUSHBUTTON "Create/Update",IDC_CREATE,15,25,53,13,NOT WS_TABSTOP
348
349 void rc2wxr::ParsePushButton()
350
351 {
352
353 wxString tok;
354
355 wxString phrase,varname;
356
357 phrase=GetQuoteField();
358
359 varname=GetToken();
360
361 int c;
362
363 m_controlid++;
364
365 c=m_controlid;
366
367 if (varname==_T("IDOK"))
368
369 c=wxID_OK;
370
371
372
373 if (varname==_T("IDCANCEL"))
374
375 c=wxID_CANCEL;
376
377
378
379 if (varname==_T("IDAPPLY"))
380
381 c=wxID_APPLY;
382
383
384
385 int x,y,width,height;
386
387 ReadRect(x,y,width,height);
388
389 wxFprintf(m_wxr,_T(" control = [%i,wxButton,'%s','0','%s',"),c,phrase.c_str(),varname.c_str());
390
391 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
392
393 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
394
395
396
397 }
398
399
400
401
402
403 bool rc2wxr::Seperator(int ch)
404
405 {
406
407 if ((ch==' ')|(ch==',')|(ch==13)|(ch==10)|(ch=='|'))
408
409 return true;
410
411
412
413 if (ch==EOF)
414
415 {
416
417 m_done=true;
418
419 return true;
420
421 }
422
423 return false;
424
425 }
426
427
428
429 void rc2wxr::ParseGroupBox()
430
431 {
432
433 // GROUPBOX "Rotate",IDC_STATIC,1,1,71,79
434
435 wxString tok;
436
437 wxString phrase,varname;
438
439 phrase=GetQuoteField();
440
441 varname=GetToken();
442
443 m_controlid++;
444
445 int x,y,width,height;
446
447 ReadRect(x,y,width,height);
448
449 wxFprintf(m_wxr,_T(" control = [%i,wxStaticBox,'%s','0','%s',"),m_controlid,phrase.c_str(),varname.c_str());
450
451 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
452
453 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
454
455
456
457
458
459 }
460
461
462
463 void rc2wxr::ReadRect(int & x, int & y, int & width, int & height)
464
465 {
466
467 x=wxAtoi(GetToken());
468
469 y=wxAtoi(GetToken());
470
471 width=wxAtoi(GetToken());
472
473 height=wxAtoi(GetToken());
474
475
476
477 }
478
479
480
481 wxString rc2wxr::GetToken()
482
483 {
484
485 wxString tok=wxEmptyString;
486
487
488
489 if (m_rc.Eof())
490
491 {
492
493 m_done=true;
494
495 return tok;
496
497 }
498
499
500
501 int ch=0;
502
503 ReadChar(ch);
504
505 if (ch==EOF)
506
507 {
508
509 m_done=true;
510
511 return tok;
512
513 }
514
515
516
517 while (Seperator(ch))
518
519 {
520
521 ReadChar(ch);
522
523 if (m_done)
524
525 return tok;
526
527 }
528
529
530
531 if (ch==EOF)
532
533 {
534
535 m_done=true;
536
537
538
539 }
540
541
542
543
544
545 while (!Seperator(ch))
546
547 {
548
549 tok+=(char)ch;
550
551 ReadChar(ch);
552
553
554
555 }
556
557
558
559 if (ch==EOF)
560
561 m_done=true;
562
563
564
565
566
567 return tok;
568
569 }
570
571
572
573 wxString rc2wxr::GetQuoteField()
574 {
575 wxString phrase;
576
577 //ASCII code 34 "
578 int ch=0;
579 ReadChar(ch);
580
581 while (ch!=34)
582 ReadChar(ch);
583
584 ReadChar(ch);
585
586 while (ch!=34)
587 {
588 phrase+=(char)ch;
589 ReadChar(ch);
590 }
591
592 return phrase;
593 }
594
595
596
597 void rc2wxr::ReadChar(int &ch)
598 {
599 wxFileOffset result = m_rc.Tell();
600
601 if ( result >= m_filesize )
602 m_done=true;
603
604 result = m_rc.Read(&ch,1);
605
606 if ( result==wxInvalidOffset )
607 m_done=true;
608
609 if(ch==EOF)
610 m_done=true;
611 }
612
613
614 /* COMBOBOX IDC_SCALECOMBO,10,110,48,52,CBS_DROPDOWNLIST | CBS_SORT |
615 WS_VSCROLL | WS_TABSTOP */
616 void rc2wxr::ParseComboBox()
617 {
618 int x,y,width,height;
619 wxString tok;
620 wxString varname = GetToken();
621
622 m_controlid++;
623
624 ReadRect(x,y,width,height);
625 wxFprintf(m_wxr,_T(" control = [%i,wxChoice,'','0','%s',"),m_controlid,varname.c_str());
626 wxFprintf(m_wxr,_T("%i,%i,%i,%i,[],\\\n"),x,y,width,height);
627 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
628 }
629
630
631 void rc2wxr::ParseMenu(wxString name)
632 {
633 wxString tok;
634 static int menuid=0;
635 menuid++;
636 wxFprintf(m_wxr,_T("static char *MenuBar%i = \"menu(name = '%s',\\\n"),menuid,name.c_str());
637 wxFprintf(m_wxr,_T("menu = \\\n"));
638 wxFprintf(m_wxr,_T("[\\\n"));
639
640 while ((tok!=_T("BEGIN"))&(tok!=_T("{")))
641 tok=GetToken();
642
643 while ((tok!=_T("END"))&(tok!=_T("}")))
644 {
645 tok=GetToken();
646
647 if (tok==_T("POPUP"))
648 {
649 ParsePopupMenu();
650 wxFprintf(m_wxr,_T(" ],\\\n"));
651 }
652 }
653
654 wxFprintf(m_wxr,_T("]).\";\n\n"));
655 }
656
657
658 void rc2wxr::ParsePopupMenu()
659 {
660 static int menuitem=99;
661
662 menuitem++;
663
664 wxString tok = GetQuoteField();
665 int spot;
666
667 //Remove /t because it causes problems
668 spot=tok.First(_T("\\t"));
669 tok=tok.Left(spot);
670
671 wxFprintf(m_wxr,_T(" ['%s',%i,'',\\\n"),tok.c_str(),menuitem);
672
673 while ((tok!=_T("BEGIN"))&(tok!=_T("{")))
674 tok=GetToken();
675
676 while ((tok!=_T("END"))&(tok!=_T("}")))
677 {
678 tok=GetToken();
679
680 if (tok==_T("MENUITEM"))
681 {
682 if (PeekToken()==_T("SEPARATOR"))
683 {
684 wxFprintf(m_wxr,_T(" [],\\\n"));
685 }
686 else
687 {
688 tok=GetQuoteField();
689 //Remove /t because it causes problems
690 spot=tok.First(_T("\\t"));
691 tok=tok.Left(spot);
692 menuitem++;
693 wxFprintf(m_wxr,_T(" ['%s',%i,''],\\\n"),tok.c_str(),menuitem);
694 }
695 }
696 }
697 }
698
699
700
701 wxString rc2wxr::PeekToken()
702 {
703 wxFileOffset p = m_rc.Tell();
704 wxString tok = GetToken();
705 m_rc.Seek(p);
706 return tok;
707 }
708
709 //Windows pain in the butt CONTROL
710 void rc2wxr::ParseControlMS()
711 {
712 wxString tok;
713 wxString label=GetQuoteField();
714 wxString varname=GetToken();
715 wxString kindctrl=GetQuoteField();
716
717 kindctrl.MakeUpper();
718 if (kindctrl==_T("MSCTLS_TRACKBAR32"))
719 ParseSlider(label,varname);
720 if (kindctrl==_T("MSCTLS_PROGRESS32"))
721 ParseProgressBar(label,varname);
722 if (kindctrl==_T("BUTTON"))
723 ParseCtrlButton(label,varname);
724 }
725
726 /* CONTROL "Slider1",IDC_SLIDER1,"msctls_trackbar32",TBS_BOTH |
727 TBS_NOTICKS | WS_TABSTOP,52,73,100,15
728 */
729 void rc2wxr::ParseSlider(wxString WXUNUSED(label), wxString varname)
730 {
731 int x,y,width,height;
732 wxString tok;
733
734 while (ReadOrs(tok))
735 ;
736 wxFprintf(m_wxr,_T(" control = [%i,wxSlider,'','wxSL_HORIZONTAL','%s',"),m_controlid,varname.c_str());
737 ReadRect(x,y,width,height);
738 wxFprintf(m_wxr,_T("%i,%i,%i,%i,"),x,y,width,height);
739 wxFprintf(m_wxr,_T(" 1, 1, 10,\\\n"));
740 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
741 }
742
743 /*
744 CONTROL "Progress1",CG_IDC_PROGDLG_PROGRESS,"msctls_progress32",
745 WS_BORDER,15,52,154,13
746 */
747 void rc2wxr::ParseProgressBar(wxString WXUNUSED(label), wxString varname)
748 {
749 int x,y,width,height;
750 wxString tok;
751
752 while (ReadOrs(tok))
753 ;
754
755 wxFprintf(m_wxr,_T(" control = [%i,wxGauge,'','wxGA_HORIZONTAL','%s',"),m_controlid,varname.c_str());
756 ReadRect(x,y,width,height);
757 wxFprintf(m_wxr,_T("%i,%i,%i,%i,"),x,y,width,height);
758 wxFprintf(m_wxr,_T(" 0, 10,\\\n"));
759 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
760 }
761
762
763 bool rc2wxr::ReadOrs(wxString & w)
764 {
765 wxString tok = PeekToken();
766 if (tok.IsNumber())
767 return false;
768 w=GetToken();
769 return true;
770 }
771
772
773 //Is it a check button or a radio button
774 void rc2wxr::ParseCtrlButton(wxString label, wxString varname)
775 {
776 int x,y,width,height;
777 wxString tok = GetToken();
778
779 m_controlid++;
780
781 if (tok==_T("BS_AUTOCHECKBOX"))
782 {
783 wxFprintf(m_wxr,_T(" control = [%i,wxCheckBox,'%s','0','%s',"),m_controlid,label.c_str(),varname.c_str());
784 while (ReadOrs(tok))
785 ;
786
787 ReadRect(x,y,width,height);
788 wxFprintf(m_wxr,_T("%i,%i,%i,%i,0,\\\n"),x,y,width,height);
789 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
790 }
791
792 if (tok==_T("BS_AUTORADIOBUTTON"))
793 {
794 wxFprintf(m_wxr,_T(" control = [%i,wxRadioButton,'%s','0','%s',"),m_controlid,label.c_str(),varname.c_str());
795 while(ReadOrs(tok))
796 ;
797
798 ReadRect(x,y,width,height);
799 wxFprintf(m_wxr,_T("%i,%i,%i,%i,0,\\\n"),x,y,width,height);
800 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
801 }
802 }
803