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