]> git.saurik.com Git - wxWidgets.git/blame_incremental - contrib/utils/convertrc/rc2wxr.cpp
Applied patch [ 1174270 ] small fixes to wxGenericDirCtrl
[wxWidgets.git] / contrib / utils / convertrc / rc2wxr.cpp
... / ...
CommitLineData
1// rc2wxr.cpp: implementation of the rc2wxr class.
2//
3//////////////////////////////////////////////////////////////////////
4//Author: Brian Gavin 9/24/00
5//License: wxWindows License
6/*
7WARNING- I know this code has some bugs to work out but
8I don't plan to fix them since I feel that wxr files will
9not be used much longer.
10This 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
43rc2wxr::rc2wxr()
44{
45 m_done=false;
46 m_controlid=6000;
47}
48
49rc2wxr::~rc2wxr()
50{
51}
52
53void 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
89Example .rc
90
91Microsoft style as of v5.0
92
93IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 217, 55
94
95STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
96
97CAPTION "About Funimator"
98
99FONT 8, "MS Sans Serif"
100
101 Borland 4.5 style rc
102
103IDD_DIBATTR DIALOG 7, 16, 172, 119
104
105STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
106
107CAPTION "DIB Attributes"
108
109FONT 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
119void rc2wxr::ParseDialog(wxString dlgname)
120
121{
122
123wxString tok;
124
125static int dlgid=999;
126
127dlgid++;
128
129/* Make sure that this really is a dialog
130
131microsoft reuses the keyword DIALOG for other things
132
133*/
134
135tok=PeekToken();
136
137//Microsoft notation?
138
139if (tok==_T("DISCARDABLE"))
140
141{
142
143tok=GetToken();
144
145tok=PeekToken();
146
147}
148
149//This isn't a Dialog resource eject eject
150
151if (!tok.IsNumber())
152
153 return;
154
155//Generate Dialog text
156
157wxFprintf(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
161wxFprintf(m_wxr,_T("style = 'wxRAISED_BORDER | wxCAPTION | wxTHICK_FRAME | wxSYSTEM_MENU',\\\n"));
162wxFprintf(m_wxr,_T("id = %i,\\\n"),dlgid);
163
164//Record x,y,width,height
165
166int x,y,width,height;
167
168ReadRect(x,y,width,height);
169
170wxFprintf(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
177tok=GetToken();
178
179wxString title;
180
181
182
183while ((tok!=_T("BEGIN"))&(tok!=_T("{")))
184
185{
186
187if (tok==_T("CAPTION"))
188
189{
190
191title=GetQuoteField();
192
193wxFprintf(m_wxr,_T("title = '%s',\\\n"),title.c_str());
194
195}
196
197tok=GetToken();
198
199}
200
201wxFprintf(m_wxr,_T("use_dialog_units = 1,\\\n"));
202
203wxFprintf(m_wxr,_T("use_system_defaults = 0,\\\n"));
204
205
206
207wxFprintf(m_wxr,_T("font = [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif'],\\\n"));
208
209ParseControls();
210
211wxFprintf(m_wxr,_T(").\";\n\n"));
212
213}
214
215
216
217/*
218
219BEGIN
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
235END
236
237*/
238
239void rc2wxr::ParseControls()
240
241{
242
243wxString tok;
244
245
246
247tok=GetToken();
248
249while ((tok!=_T("END"))&(tok!=_T("}")))
250
251{
252
253if (tok==_T("LTEXT"))
254
255 ParseStaticText();
256
257if (tok==_T("EDITTEXT"))
258
259 ParseTextCtrl();
260
261if (tok==_T("PUSHBUTTON"))
262
263 ParsePushButton();
264
265if (tok==_T("DEFPUSHBUTTON"))
266
267 ParsePushButton();
268
269if (tok==_T("GROUPBOX"))
270
271 ParseGroupBox();
272
273if (tok==_T("COMBOBOX"))
274
275 ParseComboBox();
276
277if (tok==_T("CONTROL"))
278
279 ParseControlMS();
280
281
282
283tok=GetToken();
284
285}
286
287
288
289}
290
291//LTEXT "Radius",IDC_STATIC,9,67,23,8
292
293void rc2wxr::ParseStaticText()
294
295{
296
297wxString tok;
298
299wxString phrase,varname;
300
301phrase=GetQuoteField();
302
303varname=GetToken();
304
305m_controlid++;
306
307int x,y,width,height;
308
309ReadRect(x,y,width,height);
310
311wxFprintf(m_wxr,_T(" control = [%i,wxStaticText,'%s','0','%s',"),m_controlid,phrase.c_str(),varname.c_str());
312
313wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
314
315wxFprintf(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
321void rc2wxr::ParseTextCtrl()
322
323{
324
325wxString tok;
326
327wxString varname;
328
329varname=GetToken();
330
331m_controlid++;
332
333int x,y,width,height;
334
335ReadRect(x,y,width,height);
336
337wxFprintf(m_wxr,_T(" control = [%i,wxTextCtrl,'','0','%s',"),m_controlid,varname.c_str());
338
339wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
340
341wxFprintf(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
349void rc2wxr::ParsePushButton()
350
351{
352
353wxString tok;
354
355wxString phrase,varname;
356
357phrase=GetQuoteField();
358
359varname=GetToken();
360
361int c;
362
363m_controlid++;
364
365c=m_controlid;
366
367if (varname==_T("IDOK"))
368
369c=wxID_OK;
370
371
372
373if (varname==_T("IDCANCEL"))
374
375c=wxID_CANCEL;
376
377
378
379if (varname==_T("IDAPPLY"))
380
381c=wxID_APPLY;
382
383
384
385int x,y,width,height;
386
387ReadRect(x,y,width,height);
388
389wxFprintf(m_wxr,_T(" control = [%i,wxButton,'%s','0','%s',"),c,phrase.c_str(),varname.c_str());
390
391wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
392
393wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
394
395
396
397}
398
399
400
401
402
403bool rc2wxr::Seperator(int ch)
404
405{
406
407if ((ch==' ')|(ch==',')|(ch==13)|(ch==10)|(ch=='|'))
408
409 return true;
410
411
412
413if (ch==EOF)
414
415{
416
417m_done=true;
418
419return true;
420
421}
422
423return false;
424
425}
426
427
428
429void rc2wxr::ParseGroupBox()
430
431{
432
433// GROUPBOX "Rotate",IDC_STATIC,1,1,71,79
434
435wxString tok;
436
437wxString phrase,varname;
438
439phrase=GetQuoteField();
440
441varname=GetToken();
442
443m_controlid++;
444
445int x,y,width,height;
446
447ReadRect(x,y,width,height);
448
449wxFprintf(m_wxr,_T(" control = [%i,wxStaticBox,'%s','0','%s',"),m_controlid,phrase.c_str(),varname.c_str());
450
451wxFprintf(m_wxr,_T("%i,%i,%i,%i,'',\\\n"),x,y,width,height);
452
453wxFprintf(m_wxr,_T("[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n"));
454
455
456
457
458
459}
460
461
462
463void rc2wxr::ReadRect(int & x, int & y, int & width, int & height)
464
465{
466
467x=wxAtoi(GetToken());
468
469y=wxAtoi(GetToken());
470
471width=wxAtoi(GetToken());
472
473height=wxAtoi(GetToken());
474
475
476
477}
478
479
480
481wxString rc2wxr::GetToken()
482
483{
484
485wxString tok=wxEmptyString;
486
487
488
489if (m_rc.Eof())
490
491{
492
493m_done=true;
494
495return tok;
496
497}
498
499
500
501int ch=0;
502
503ReadChar(ch);
504
505if (ch==EOF)
506
507{
508
509m_done=true;
510
511return tok;
512
513}
514
515
516
517while (Seperator(ch))
518
519{
520
521 ReadChar(ch);
522
523 if (m_done)
524
525 return tok;
526
527}
528
529
530
531if (ch==EOF)
532
533{
534
535m_done=true;
536
537
538
539}
540
541
542
543
544
545while (!Seperator(ch))
546
547{
548
549tok+=(char)ch;
550
551ReadChar(ch);
552
553
554
555}
556
557
558
559if (ch==EOF)
560
561 m_done=true;
562
563
564
565
566
567return tok;
568
569}
570
571
572
573wxString 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
597void 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 */
616void 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
631void 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
658void 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
701wxString 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
710void 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*/
729void 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/*
744CONTROL "Progress1",CG_IDC_PROGDLG_PROGRESS,"msctls_progress32",
745 WS_BORDER,15,52,154,13
746*/
747void 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
763bool 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
774void 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