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