]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/convertrc/rc2wxr.cpp
Unicode markup.
[wxWidgets.git] / contrib / utils / convertrc / rc2wxr.cpp
1 // rc2wxr.cpp: implementation of the rc2wxr class.
2
3 //
4
5 //////////////////////////////////////////////////////////////////////
6
7 //Author: Brian Gavin 9/24/00
8
9 //License: wxWindows License
10
11 /*
12
13 WARNING- I know this code has some bugs to work out but
14
15 I don't plan to fix them since I feel that wxr files will
16
17 not be used much longer.
18
19 This code was used as a starting point for my rc2xml converter
20
21 */
22
23 #ifdef __GNUG__
24
25 #pragma implementation "rc2wxr.cpp"
26
27 #pragma interface "rc2wxr.cpp"
28
29 #endif
30
31
32
33 // For compilers that support precompilation, includes "wx/wx.h".
34
35 #include "wx/wxprec.h"
36
37
38
39 #ifdef __BORLANDC__
40
41 #pragma hdrstop
42
43 #endif
44
45
46
47 // for all others, include the necessary headers (this file is usually all you
48
49 // need because it includes almost all "standard" wxWidgets headers
50
51 #ifndef WX_PRECOMP
52
53 #include <wx/wx.h>
54
55 #endif
56
57
58
59
60
61 #include "rc2wxr.h"
62
63 #include "wx/image.h"
64
65 #include "wx/deprecated/setup.h"
66 #include "wx/deprecated/resource.h"
67
68 //////////////////////////////////////////////////////////////////////
69
70 // Construction/Destruction
71
72 //////////////////////////////////////////////////////////////////////
73
74
75
76 rc2wxr::rc2wxr()
77
78 {
79
80 m_done=false;
81
82 m_controlid=6000;
83
84 }
85
86
87
88 rc2wxr::~rc2wxr()
89
90 {
91
92
93
94 }
95
96
97
98 void rc2wxr::Convert(wxString wxrfile, wxString rcfile)
99 {
100 m_rc.Open(rcfile);
101 m_filesize=m_rc.Length();
102 if( (m_wxr = wxFopen( wxrfile, _T("wt") )) == NULL )
103 {
104 return;
105 }
106
107 wxString tok,prevtok;
108
109 while (!m_done)
110 {
111 tok=GetToken();
112
113 if (tok==_T("DIALOG"))
114 {
115 ParseDialog(prevtok);
116 }
117
118 if (tok==_T("MENU"))
119 {
120 ParseMenu(prevtok);
121 }
122
123 prevtok=tok;
124 }
125
126 fclose(m_wxr);
127
128 m_rc.Close();
129 }
130
131
132
133
134
135 /*
136
137 Example .rc
138
139 Microsoft style as of v5.0
140
141 IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 217, 55
142
143 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
144
145 CAPTION "About Funimator"
146
147 FONT 8, "MS Sans Serif"
148
149
150
151 Borland 4.5 style rc
152
153 IDD_DIBATTR DIALOG 7, 16, 172, 119
154
155 STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
156
157 CAPTION "DIB Attributes"
158
159 FONT 8, "MS Sans Serif"
160
161 {
162
163 DEFPUSHBUTTON "Ok", IDOK, 114, 8, 50, 14
164
165 PUSHBUTTON "Cancel", IDCANCEL, 114, 28, 50, 14
166
167
168
169
170
171
172
173 */
174
175 void rc2wxr::ParseDialog(wxString dlgname)
176
177 {
178
179 wxString tok;
180
181 static int dlgid=999;
182
183 dlgid++;
184
185 /* Make sure that this really is a dialog
186
187 microsoft reuses the keyword DIALOG for other things
188
189 */
190
191 tok=PeekToken();
192
193 //Microsoft notation?
194
195 if (tok==_T("DISCARDABLE"))
196
197 {
198
199 tok=GetToken();
200
201 tok=PeekToken();
202
203 }
204
205 //This isn't a Dialog resource eject eject
206
207 if (!tok.IsNumber())
208
209 return;
210
211 //Generate Dialog text
212
213 wxFprintf(m_wxr,_T("static char *dialog%i = \"dialog(name = '%s',\\\n"),dlgid,dlgname.c_str());
214
215 //be lazy about style for now. add it later
216
217 wxFprintf(m_wxr,_T("style = 'wxRAISED_BORDER | wxCAPTION | wxTHICK_FRAME | wxSYSTEM_MENU',\\\n"));
218
219
220
221 wxFprintf(m_wxr,_T("id = %i,\\\n"),dlgid);
222
223
224
225 //Record x,y,width,height
226
227 int x,y,width,height;
228
229 ReadRect(x,y,width,height);
230
231 wxFprintf(m_wxr,_T("x = %i, y = %i, width = %i, height = %i,\\\n"),x,y,width,height);
232
233
234
235
236
237 //CAPTION "About Funimator"
238
239 //Get Title
240
241 tok=GetToken();
242
243 wxString title;
244
245
246
247 while ((tok!=_T("BEGIN"))&(tok!=_T("{")))
248
249 {
250
251 if (tok==_T("CAPTION"))
252
253 {
254
255 title=GetQuoteField();
256
257 wxFprintf(m_wxr,_T("title = '%s',\\\n"),title.c_str());
258
259 }
260
261 tok=GetToken();
262
263 }
264
265 wxFprintf(m_wxr,_T("use_dialog_units = 1,\\\n"));
266
267 wxFprintf(m_wxr,_T("use_system_defaults = 0,\\\n"));
268
269
270
271 wxFprintf(m_wxr,_T("font = [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif'],\\\n"));
272
273 ParseControls();
274
275 wxFprintf(m_wxr,_T(").\";\n\n"));
276
277 }
278
279
280
281 /*
282
283 BEGIN
284
285
286
287
288
289
290
291 EDITTEXT IDC_BANDS,36,83,22,14,ES_AUTOHSCROLL | ES_NUMBER | NOT
292
293 WS_TABSTOP
294
295 LTEXT "Bands",IDC_STATIC,11,86,21,8
296
297 EDITTEXT IDC_NAME,10,3,75,14,ES_AUTOHSCROLL
298
299 END
300
301 */
302
303 void rc2wxr::ParseControls()
304
305 {
306
307 wxString tok;
308
309
310
311 tok=GetToken();
312
313 while ((tok!=_T("END"))&(tok!=_T("}")))
314
315 {
316
317 if (tok==_T("LTEXT"))
318
319 ParseStaticText();
320
321 if (tok==_T("EDITTEXT"))
322
323 ParseTextCtrl();
324
325 if (tok==_T("PUSHBUTTON"))
326
327 ParsePushButton();
328
329 if (tok==_T("DEFPUSHBUTTON"))
330
331 ParsePushButton();
332
333 if (tok==_T("GROUPBOX"))
334
335 ParseGroupBox();
336
337 if (tok==_T("COMBOBOX"))
338
339 ParseComboBox();
340
341 if (tok==_T("CONTROL"))
342
343 ParseControlMS();
344
345
346
347 tok=GetToken();
348
349 }
350
351
352
353 }
354
355 //LTEXT "Radius",IDC_STATIC,9,67,23,8
356
357 void rc2wxr::ParseStaticText()
358
359 {
360
361 wxString tok;
362
363 wxString phrase,varname;
364
365 phrase=GetQuoteField();
366
367 varname=GetToken();
368
369 m_controlid++;
370
371 int x,y,width,height;
372
373 ReadRect(x,y,width,height);
374
375 wxFprintf(m_wxr,_T(" control = [%i,wxStaticText,'%s','0','%s',"),m_controlid,phrase.c_str(),varname.c_str());
376
377 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
378
379 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
380
381 }
382
383 //EDITTEXT IDC_RADIUS,36,65,40,14,ES_AUTOHSCROLL
384
385 void rc2wxr::ParseTextCtrl()
386
387 {
388
389 wxString tok;
390
391 wxString varname;
392
393 varname=GetToken();
394
395 m_controlid++;
396
397 int x,y,width,height;
398
399 ReadRect(x,y,width,height);
400
401 wxFprintf(m_wxr,_T(" control = [%i,wxTextCtrl,'','0','%s',"),m_controlid,varname.c_str());
402
403 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
404
405 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
406
407
408
409 }
410
411 //PUSHBUTTON "Create/Update",IDC_CREATE,15,25,53,13,NOT WS_TABSTOP
412
413 void rc2wxr::ParsePushButton()
414
415 {
416
417 wxString tok;
418
419 wxString phrase,varname;
420
421 phrase=GetQuoteField();
422
423 varname=GetToken();
424
425 int c;
426
427 m_controlid++;
428
429 c=m_controlid;
430
431 if (varname==_T("IDOK"))
432
433 c=wxID_OK;
434
435
436
437 if (varname==_T("IDCANCEL"))
438
439 c=wxID_CANCEL;
440
441
442
443 if (varname==_T("IDAPPLY"))
444
445 c=wxID_APPLY;
446
447
448
449 int x,y,width,height;
450
451 ReadRect(x,y,width,height);
452
453 wxFprintf(m_wxr,_T(" control = [%i,wxButton,'%s','0','%s',"),c,phrase.c_str(),varname.c_str());
454
455 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
456
457 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
458
459
460
461 }
462
463
464
465
466
467 bool rc2wxr::Seperator(int ch)
468
469 {
470
471 if ((ch==' ')|(ch==',')|(ch==13)|(ch==10)|(ch=='|'))
472
473 return true;
474
475
476
477 if (ch==EOF)
478
479 {
480
481 m_done=true;
482
483 return true;
484
485 }
486
487 return false;
488
489 }
490
491
492
493 void rc2wxr::ParseGroupBox()
494
495 {
496
497 // GROUPBOX "Rotate",IDC_STATIC,1,1,71,79
498
499 wxString tok;
500
501 wxString phrase,varname;
502
503 phrase=GetQuoteField();
504
505 varname=GetToken();
506
507 m_controlid++;
508
509 int x,y,width,height;
510
511 ReadRect(x,y,width,height);
512
513 wxFprintf(m_wxr,_T(" control = [%i,wxStaticBox,'%s','0','%s',"),m_controlid,phrase.c_str(),varname.c_str());
514
515 wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
516
517 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
518
519
520
521
522
523 }
524
525
526
527 void rc2wxr::ReadRect(int & x, int & y, int & width, int & height)
528
529 {
530
531 x=wxAtoi(GetToken());
532
533 y=wxAtoi(GetToken());
534
535 width=wxAtoi(GetToken());
536
537 height=wxAtoi(GetToken());
538
539
540
541 }
542
543
544
545 wxString rc2wxr::GetToken()
546
547 {
548
549 wxString tok=wxEmptyString;
550
551
552
553 if (m_rc.Eof())
554
555 {
556
557 m_done=true;
558
559 return tok;
560
561 }
562
563
564
565 int ch=0;
566
567 ReadChar(ch);
568
569 if (ch==EOF)
570
571 {
572
573 m_done=true;
574
575 return tok;
576
577 }
578
579
580
581 while (Seperator(ch))
582
583 {
584
585 ReadChar(ch);
586
587 if (m_done)
588
589 return tok;
590
591 }
592
593
594
595 if (ch==EOF)
596
597 {
598
599 m_done=true;
600
601
602
603 }
604
605
606
607
608
609 while (!Seperator(ch))
610
611 {
612
613 tok+=(char)ch;
614
615 ReadChar(ch);
616
617
618
619 }
620
621
622
623 if (ch==EOF)
624
625 m_done=true;
626
627
628
629
630
631 return tok;
632
633 }
634
635
636
637 wxString rc2wxr::GetQuoteField()
638
639 {
640
641 wxString phrase;
642
643 //ASCII code 34 "
644
645 int ch=0;
646
647 ReadChar(ch);
648
649
650
651 while (ch!=34)
652
653 ReadChar(ch);
654
655
656
657 ReadChar(ch);
658
659
660
661 while (ch!=34)
662
663 {
664
665 phrase+=(char)ch;
666
667 ReadChar(ch);
668
669 }
670
671 return phrase;
672
673 }
674
675
676
677 void rc2wxr::ReadChar(int &ch)
678
679 {
680
681 int result;
682
683 result=m_rc.Tell();
684
685
686
687 if((result>=m_filesize))
688
689 m_done=true;
690
691
692
693 result=m_rc.Read(&ch,1);
694
695
696
697 if((result==-1))
698
699 m_done=true;
700
701
702
703 if(ch==EOF)
704
705 m_done=true;
706
707 }
708
709
710
711 void rc2wxr::ParseComboBox()
712
713 {
714
715 /* COMBOBOX IDC_SCALECOMBO,10,110,48,52,CBS_DROPDOWNLIST | CBS_SORT |
716
717 WS_VSCROLL | WS_TABSTOP */
718
719 wxString tok;
720
721 wxString varname;
722
723 varname=GetToken();
724
725 m_controlid++;
726
727 int x,y,width,height;
728
729 ReadRect(x,y,width,height);
730
731
732
733 wxFprintf(m_wxr,_T(" control = [%i,wxChoice,'','0','%s',"),m_controlid,varname.c_str());
734
735 wxFprintf(m_wxr,_T("%i,%i,%i,%i,[],\\\n"),x,y,width,height);
736
737 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
738
739
740
741
742
743 }
744
745
746
747 void rc2wxr::ParseMenu(wxString name)
748
749 {
750
751 wxString tok=wxEmptyString;
752
753 static int menuid=0;
754
755 menuid++;
756
757 wxFprintf(m_wxr,_T("static char *MenuBar%i = \"menu(name = '%s',\\\n"),menuid,name.c_str());
758
759 wxFprintf(m_wxr,_T("menu = \\\n"));
760
761 wxFprintf(m_wxr,_T("[\\\n"));
762
763
764
765 while ((tok!=_T("BEGIN"))&(tok!=_T("{")))
766
767 tok=GetToken();
768
769
770
771 while ((tok!=_T("END"))&(tok!=_T("}")))
772
773 {
774
775 tok=GetToken();
776
777 if (tok==_T("POPUP"))
778
779 {
780
781 ParsePopupMenu();
782
783 wxFprintf(m_wxr,_T(" ],\\\n"));
784
785 }
786
787 }
788
789
790
791 wxFprintf(m_wxr,_T("]).\";\n\n"));
792
793 }
794
795
796
797 void rc2wxr::ParsePopupMenu()
798
799 {
800
801 static int menuitem=99;
802
803 menuitem++;
804
805
806
807 wxString tok;
808
809 tok=GetQuoteField();
810
811 int spot;
812
813 //Remove /t because it causes problems
814
815 spot=tok.First(_T("\\t"));
816
817 tok=tok.Left(spot);
818
819 wxFprintf(m_wxr,_T(" ['%s',%i,'',\\\n"),tok.c_str(),menuitem);
820
821 while ((tok!=_T("BEGIN"))&(tok!=_T("{")))
822
823 tok=GetToken();
824
825
826
827 while ((tok!=_T("END"))&(tok!=_T("}")))
828
829 {
830
831 tok=GetToken();
832
833 if (tok==_T("MENUITEM"))
834
835 {
836
837 if (PeekToken()==_T("SEPARATOR"))
838
839 wxFprintf(m_wxr,_T(" [],\\\n"));
840
841 else
842
843 {
844
845 tok=GetQuoteField();
846
847 //Remove /t because it causes problems
848
849 spot=tok.First(_T("\\t"));
850
851 tok=tok.Left(spot);
852
853 menuitem++;
854
855 wxFprintf(m_wxr,_T(" ['%s',%i,''],\\\n"),tok.c_str(),menuitem);
856
857 }
858
859 }
860
861
862
863 }
864
865
866
867
868
869 }
870
871
872
873 wxString rc2wxr::PeekToken()
874
875 {
876
877 wxString tok;
878
879 int p;
880
881 p=m_rc.Tell();
882
883 tok=GetToken();
884
885
886
887 m_rc.Seek(p);
888
889 return tok;
890
891 }
892
893 //Windows pain in the butt CONTROL
894
895 void rc2wxr::ParseControlMS()
896
897 {
898
899 wxString label,varname,kindctrl,tok;
900
901 label=GetQuoteField();
902
903 varname=GetToken();
904
905 kindctrl=GetQuoteField();
906
907 kindctrl.MakeUpper();
908
909
910
911
912
913 if (kindctrl==_T("MSCTLS_TRACKBAR32"))
914
915 ParseSlider(label,varname);
916
917 if (kindctrl==_T("MSCTLS_PROGRESS32"))
918
919 ParseProgressBar(label,varname);
920
921 if (kindctrl==_T("BUTTON"))
922
923 ParseCtrlButton(label,varname);
924
925 }
926
927 /* CONTROL "Slider1",IDC_SLIDER1,"msctls_trackbar32",TBS_BOTH |
928
929 TBS_NOTICKS | WS_TABSTOP,52,73,100,15
930
931 */
932
933
934
935 void rc2wxr::ParseSlider(wxString WXUNUSED(label), wxString varname)
936
937 {
938
939 wxString tok;
940
941 while (ReadOrs(tok))
942 ;
943
944 wxFprintf(m_wxr,_T(" control = [%i,wxSlider,'','wxSL_HORIZONTAL','%s',"),m_controlid,varname.c_str());
945
946 int x,y,width,height;
947
948 ReadRect(x,y,width,height);
949
950 wxFprintf(m_wxr,_T("%i,%i,%i,%i,"),x,y,width,height);
951
952 wxFprintf(m_wxr,_T(" 1, 1, 10,\\\n"));
953
954 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
955
956 }
957
958 /*
959
960 CONTROL "Progress1",CG_IDC_PROGDLG_PROGRESS,"msctls_progress32",
961
962 WS_BORDER,15,52,154,13
963
964 */
965
966 void rc2wxr::ParseProgressBar(wxString WXUNUSED(label), wxString varname)
967
968 {
969
970 wxString tok;
971
972 while (ReadOrs(tok))
973 ;
974
975 wxFprintf(m_wxr,_T(" control = [%i,wxGauge,'','wxGA_HORIZONTAL','%s',"),m_controlid,varname.c_str());
976
977 int x,y,width,height;
978
979 ReadRect(x,y,width,height);
980
981 wxFprintf(m_wxr,_T("%i,%i,%i,%i,"),x,y,width,height);
982
983 wxFprintf(m_wxr,_T(" 0, 10,\\\n"));
984
985 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
986
987 }
988
989
990
991 bool rc2wxr::ReadOrs(wxString & w)
992
993 {
994
995 wxString tok;
996
997 tok=PeekToken();
998
999 if (tok.IsNumber())
1000
1001 return false;
1002
1003 w=GetToken();
1004
1005 return true;
1006
1007 }
1008
1009
1010
1011 //Is it a check button or a radio button
1012
1013 void rc2wxr::ParseCtrlButton(wxString label, wxString varname)
1014
1015 {
1016
1017 wxString tok;
1018
1019 tok=GetToken();
1020
1021
1022
1023 m_controlid++;
1024
1025 int x,y,width,height;
1026
1027
1028
1029 if (tok==_T("BS_AUTOCHECKBOX"))
1030
1031 {
1032
1033 wxFprintf(m_wxr,_T(" control = [%i,wxCheckBox,'%s','0','%s',"),m_controlid,label.c_str(),varname.c_str());
1034
1035 while (ReadOrs(tok))
1036 ;
1037
1038 ReadRect(x,y,width,height);
1039
1040 wxFprintf(m_wxr,_T("%i,%i,%i,%i,0,\\\n"),x,y,width,height);
1041
1042 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
1043
1044 }
1045
1046
1047
1048 if (tok==_T("BS_AUTORADIOBUTTON"))
1049
1050 {
1051
1052 wxFprintf(m_wxr,_T(" control = [%i,wxRadioButton,'%s','0','%s',"),m_controlid,label.c_str(),varname.c_str());
1053
1054 while(ReadOrs(tok))
1055 ;
1056
1057 ReadRect(x,y,width,height);
1058
1059 wxFprintf(m_wxr,_T("%i,%i,%i,%i,0,\\\n"),x,y,width,height);
1060
1061 wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
1062
1063 }
1064
1065
1066
1067
1068
1069
1070
1071 }
1072
1073
1074