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