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