]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/convertrc/rc2wxr.cpp
3c91b14a617a572b2bf9c6358b252d61be30e14a
[wxWidgets.git] / contrib / utils / convertrc / rc2wxr.cpp
1 // RC2WXR.cpp: implementation of the wxRC2WXR class.
2 //
3 //////////////////////////////////////////////////////////////////////
4 // For compilers that support precompilation, includes "wx/wx.h".
5 #include "wx/wxprec.h"
6
7 #include "rc2wxr.h"
8 #include "wx/image.h"
9 #include "wx/resource.h"
10 //////////////////////////////////////////////////////////////////////
11 // Construction/Destruction
12 //////////////////////////////////////////////////////////////////////
13
14 wxRC2WXR::wxRC2WXR()
15 {
16 m_done=FALSE;
17 m_controlid=6000;
18 }
19
20 wxRC2WXR::~wxRC2WXR()
21 {
22
23 }
24
25 void wxRC2WXR::Open(wxString wxrfile, wxString rcfile)
26 {
27 wxFileProgressDlg fileprog;
28
29
30 m_rc.Open(rcfile);
31 m_filesize=m_rc.Length();
32 if( (m_wxr = fopen( wxrfile, "wt" )) == NULL )
33 {
34 return;
35 }
36
37 fileprog.Show(TRUE);
38
39 wxString tok,prevtok;
40
41
42 while (!m_done)
43 {
44
45 tok=GetToken();
46
47 if (tok=="DIALOG")
48 {
49 ParseDialog(prevtok);
50 fileprog.UpdateProgress(&m_rc);
51 }
52
53
54 if (tok=="MENU")
55 {
56 ParseMenu(prevtok);
57 fileprog.UpdateProgress(&m_rc);
58 }
59
60 prevtok=tok;
61 }
62 fileprog.UpdateProgress(&m_rc);
63 fclose(m_wxr);
64 //fclose(m_rc);
65 m_rc.Close();
66
67 fileprog.Show(FALSE);
68 }
69
70
71 /*
72 Example .rc
73 Microsoft style as of v5.0
74 IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 217, 55
75 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
76 CAPTION "About Funimator"
77 FONT 8, "MS Sans Serif"
78
79 Borland 4.5 style rc
80 IDD_DIBATTR DIALOG 7, 16, 172, 119
81 STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
82 CAPTION "DIB Attributes"
83 FONT 8, "MS Sans Serif"
84 {
85 DEFPUSHBUTTON "Ok", IDOK, 114, 8, 50, 14
86 PUSHBUTTON "Cancel", IDCANCEL, 114, 28, 50, 14
87
88
89
90 */
91 void wxRC2WXR::ParseDialog(wxString dlgname)
92 {
93 wxString tok;
94 static int dlgid=999;
95 dlgid++;
96 /* Make sure that this really is a dialog
97 microsoft reuses the keyword DIALOG for other things
98 */
99 tok=PeekToken();
100 //Microsoft notation?
101 if (tok=="DISCARDABLE")
102 {
103 tok=GetToken();
104 tok=PeekToken();
105 }
106 //This isn't a Dialog resource eject eject
107 if (!tok.IsNumber())
108 return;
109 //Generate Dialog text
110 fprintf(m_wxr,"static char *dialog%i = \"dialog(name = '%s',\\\n",dlgid,dlgname);
111 //be lazy about style for now. add it later
112 fprintf(m_wxr,"style = 'wxRAISED_BORDER | wxCAPTION | wxTHICK_FRAME | wxSYSTEM_MENU',\\\n");
113
114 fprintf(m_wxr,"id = %i,\\\n",dlgid);
115
116 //Record x,y,width,height
117 int x,y,width,height;
118 ReadRect(x,y,width,height);
119 fprintf(m_wxr,"x = %i, y = %i, width = %i, height = %i,\\\n",x,y,width,height);
120
121
122 //CAPTION "About Funimator"
123 //Get Title
124 tok=GetToken();
125 wxString title;
126
127 while ((tok!="BEGIN")&(tok!="{"))
128 {
129 if (tok=="CAPTION")
130 {
131 title=GetQuoteField();
132 fprintf(m_wxr,"title = '%s',\\\n",title);
133 }
134 tok=GetToken();
135 }
136 fprintf(m_wxr,"use_dialog_units = 1,\\\n");
137 fprintf(m_wxr,"use_system_defaults = 0,\\\n");
138
139 fprintf(m_wxr,"font = [8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif'],\\\n");
140 ParseControls();
141 fprintf(m_wxr,").\";\n\n");
142 }
143
144 /*
145 BEGIN
146
147
148
149 EDITTEXT IDC_BANDS,36,83,22,14,ES_AUTOHSCROLL | ES_NUMBER | NOT
150 WS_TABSTOP
151 LTEXT "Bands",IDC_STATIC,11,86,21,8
152 EDITTEXT IDC_NAME,10,3,75,14,ES_AUTOHSCROLL
153 END
154 */
155 void wxRC2WXR::ParseControls()
156 {
157 wxString tok;
158
159 tok=GetToken();
160 while ((tok!="END")&(tok!="}"))
161 {
162 if (tok=="LTEXT")
163 ParseStaticText();
164 if (tok=="EDITTEXT")
165 ParseTextCtrl();
166 if (tok=="PUSHBUTTON")
167 ParsePushButton();
168 if (tok=="DEFPUSHBUTTON")
169 ParsePushButton();
170 if (tok=="GROUPBOX")
171 ParseGroupBox();
172 if (tok=="COMBOBOX")
173 ParseComboBox();
174 if (tok=="CONTROL")
175 ParseControlMS();
176
177 tok=GetToken();
178 }
179
180 }
181 //LTEXT "Radius",IDC_STATIC,9,67,23,8
182 void wxRC2WXR::ParseStaticText()
183 {
184 wxString tok;
185 wxString phrase,varname;
186 phrase=GetQuoteField();
187 varname=GetToken();
188 m_controlid++;
189 int x,y,width,height;
190 ReadRect(x,y,width,height);
191 fprintf(m_wxr," control = [%i,wxStaticText,'%s','0','%s',",m_controlid,phrase,varname);
192 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
193 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
194 }
195 //EDITTEXT IDC_RADIUS,36,65,40,14,ES_AUTOHSCROLL
196 void wxRC2WXR::ParseTextCtrl()
197 {
198 wxString tok;
199 wxString varname;
200 varname=GetToken();
201 m_controlid++;
202 int x,y,width,height;
203 ReadRect(x,y,width,height);
204 fprintf(m_wxr," control = [%i,wxTextCtrl,'','0','%s',",m_controlid,varname);
205 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
206 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
207
208 }
209 //PUSHBUTTON "Create/Update",IDC_CREATE,15,25,53,13,NOT WS_TABSTOP
210 void wxRC2WXR::ParsePushButton()
211 {
212 wxString tok;
213 wxString phrase,varname;
214 phrase=GetQuoteField();
215 varname=GetToken();
216 int c;
217 m_controlid++;
218 c=m_controlid;
219 if (varname=="IDOK")
220 c=wxID_OK;
221
222 if (varname=="IDCANCEL")
223 c=wxID_CANCEL;
224
225 if (varname=="IDAPPLY")
226 c=wxID_APPLY;
227
228 int x,y,width,height;
229 ReadRect(x,y,width,height);
230 fprintf(m_wxr," control = [%i,wxButton,'%s','0','%s',",c,phrase,varname);
231 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
232 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
233
234 }
235
236
237 bool wxRC2WXR::Seperator(int ch)
238 {
239 if ((ch==' ')|(ch==',')|(ch==13)|(ch==10)|(ch=='|'))
240 return TRUE;
241
242 if (ch==EOF)
243 {
244 m_done=TRUE;
245 return TRUE;
246 }
247 return FALSE;
248 }
249
250 void wxRC2WXR::ParseGroupBox()
251 {
252 // GROUPBOX "Rotate",IDC_STATIC,1,1,71,79
253 wxString tok;
254 wxString phrase,varname;
255 phrase=GetQuoteField();
256 varname=GetToken();
257 m_controlid++;
258 int x,y,width,height;
259 ReadRect(x,y,width,height);
260 fprintf(m_wxr," control = [%i,wxStaticBox,'%s','0','%s',",m_controlid,phrase,varname);
261 fprintf(m_wxr,"%i,%i,%i,%i,'',\\\n",x,y,width,height);
262 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
263
264
265 }
266
267 void wxRC2WXR::ReadRect(int & x, int & y, int & width, int & height)
268 {
269 x=atoi(GetToken());
270 y=atoi(GetToken());
271 width=atoi(GetToken());
272 height=atoi(GetToken());
273
274 }
275
276 wxString wxRC2WXR::GetToken()
277 {
278 wxString tok="";
279
280 if (m_rc.Eof())
281 {
282 m_done=TRUE;
283 return tok;
284 }
285
286 int ch=0;
287 ReadChar(ch);
288 if (ch==EOF)
289 {
290 m_done=TRUE;
291 return tok;
292 }
293
294 while (Seperator(ch))
295 {
296 ReadChar(ch);
297 if (m_done)
298 return tok;
299 }
300
301 if (ch==EOF)
302 {
303 m_done=TRUE;
304
305 }
306
307
308 while (!Seperator(ch))
309 {
310 tok+=(char)ch;
311 ReadChar(ch);
312
313 }
314
315 if (ch==EOF)
316 m_done=TRUE;
317
318
319 return tok;
320 }
321
322 wxString wxRC2WXR::GetQuoteField()
323 {
324 wxString phrase;
325 //ASCII code 34 "
326 int ch=0;
327 ReadChar(ch);
328
329 while (ch!=34)
330 ReadChar(ch);
331
332 ReadChar(ch);
333
334 while (ch!=34)
335 {
336 phrase+=(char)ch;
337 ReadChar(ch);
338 }
339 return phrase;
340 }
341
342 void wxRC2WXR::ReadChar(int &ch)
343 {
344 int result;
345 result=m_rc.Tell();
346
347 if((result>=m_filesize))
348 m_done=TRUE;
349
350 result=m_rc.Read(&ch,1);
351
352 if((result==-1))
353 m_done=TRUE;
354
355 if(ch==EOF)
356 m_done=TRUE;
357 }
358
359 void wxRC2WXR::ParseComboBox()
360 {
361 /* COMBOBOX IDC_SCALECOMBO,10,110,48,52,CBS_DROPDOWNLIST | CBS_SORT |
362 WS_VSCROLL | WS_TABSTOP */
363 wxString tok;
364 wxString varname;
365 varname=GetToken();
366 m_controlid++;
367 int x,y,width,height;
368 ReadRect(x,y,width,height);
369
370 fprintf(m_wxr," control = [%i,wxChoice,'','0','%s',",m_controlid,varname);
371 fprintf(m_wxr,"%i,%i,%i,%i,[],\\\n",x,y,width,height);
372 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
373
374
375 }
376
377 void wxRC2WXR::ParseMenu(wxString name)
378 {
379 wxString tok="";
380 static int menuid=0;
381 menuid++;
382 fprintf(m_wxr,"static char *MenuBar%i = \"menu(name = '%s',\\\n",menuid,name);
383 fprintf(m_wxr,"menu = \\\n");
384 fprintf(m_wxr,"[\\\n");
385
386 while ((tok!="BEGIN")&(tok!="{"))
387 tok=GetToken();
388
389 while ((tok!="END")&(tok!="}"))
390 {
391 tok=GetToken();
392 if (tok=="POPUP")
393 {
394 ParsePopupMenu();
395 fprintf(m_wxr," ],\\\n");
396 }
397 }
398
399 fprintf(m_wxr,"]).\";\n\n");
400 }
401
402 void wxRC2WXR::ParsePopupMenu()
403 {
404 static int menuitem=99;
405 menuitem++;
406
407 wxString tok;
408 tok=GetQuoteField();
409 int spot;
410 //Remove /t because it causes problems
411 spot=tok.First("\\t");
412 tok=tok.Left(spot);
413 fprintf(m_wxr," ['%s',%i,'',\\\n",tok,menuitem);
414 while ((tok!="BEGIN")&(tok!="{"))
415 tok=GetToken();
416
417 while ((tok!="END")&(tok!="}"))
418 {
419 tok=GetToken();
420 if (tok=="MENUITEM")
421 {
422 if (PeekToken()=="SEPARATOR")
423 fprintf(m_wxr," [],\\\n");
424 else
425 {
426 tok=GetQuoteField();
427 //Remove /t because it causes problems
428 spot=tok.First("\\t");
429 tok=tok.Left(spot);
430 menuitem++;
431 fprintf(m_wxr," ['%s',%i,''],\\\n",tok,menuitem);
432 }
433 }
434
435 }
436
437
438 }
439
440 wxString wxRC2WXR::PeekToken()
441 {
442 wxString tok;
443 int p;
444 p=m_rc.Tell();
445 tok=GetToken();
446
447 m_rc.Seek(p);
448 return tok;
449 }
450 //Windows pain in the butt CONTROL
451 void wxRC2WXR::ParseControlMS()
452 {
453 wxString label,varname,kindctrl,tok;
454 label=GetQuoteField();
455 varname=GetToken();
456 kindctrl=GetQuoteField();
457 kindctrl.MakeUpper();
458
459
460 if (kindctrl=="MSCTLS_TRACKBAR32")
461 ParseSlider(label,varname);
462 if (kindctrl=="MSCTLS_PROGRESS32")
463 ParseProgressBar(label,varname);
464 if (kindctrl=="BUTTON")
465 ParseCtrlButton(label,varname);
466 }
467 /* CONTROL "Slider1",IDC_SLIDER1,"msctls_trackbar32",TBS_BOTH |
468 TBS_NOTICKS | WS_TABSTOP,52,73,100,15
469 */
470
471 void wxRC2WXR::ParseSlider(wxString label, wxString varname)
472 {
473 wxString tok;
474 while (ReadOrs(tok));
475 fprintf(m_wxr," control = [%i,wxSlider,'','wxSL_HORIZONTAL','%s',",m_controlid,varname);
476 int x,y,width,height;
477 ReadRect(x,y,width,height);
478 fprintf(m_wxr,"%i,%i,%i,%i,",x,y,width,height);
479 fprintf(m_wxr," 1, 1, 10,\\\n");
480 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
481 }
482 /*
483 CONTROL "Progress1",CG_IDC_PROGDLG_PROGRESS,"msctls_progress32",
484 WS_BORDER,15,52,154,13
485 */
486 void wxRC2WXR::ParseProgressBar(wxString label, wxString varname)
487 {
488 wxString tok;
489 while (ReadOrs(tok));
490 fprintf(m_wxr," control = [%i,wxGauge,'','wxGA_HORIZONTAL','%s',",m_controlid,varname);
491 int x,y,width,height;
492 ReadRect(x,y,width,height);
493 fprintf(m_wxr,"%i,%i,%i,%i,",x,y,width,height);
494 fprintf(m_wxr," 0, 10,\\\n");
495 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
496 }
497
498 bool wxRC2WXR::ReadOrs(wxString & w)
499 {
500 wxString tok;
501 tok=PeekToken();
502 if (tok.IsNumber())
503 return false;
504 w=GetToken();
505 return TRUE;
506 }
507
508 //Is it a check button or a radio button
509 void wxRC2WXR::ParseCtrlButton(wxString label, wxString varname)
510 {
511 wxString tok;
512 tok=GetToken();
513
514 m_controlid++;
515 int x,y,width,height;
516
517 if (tok=="BS_AUTOCHECKBOX")
518 {
519 fprintf(m_wxr," control = [%i,wxCheckBox,'%s','0','%s',",m_controlid,label,varname);
520 while (ReadOrs(tok));
521 ReadRect(x,y,width,height);
522 fprintf(m_wxr,"%i,%i,%i,%i,0,\\\n",x,y,width,height);
523 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
524 }
525
526 if (tok=="BS_AUTORADIOBUTTON")
527 {
528 fprintf(m_wxr," control = [%i,wxRadioButton,'%s','0','%s',",m_controlid,label,varname);
529 while(ReadOrs(tok));
530 ReadRect(x,y,width,height);
531 fprintf(m_wxr,"%i,%i,%i,%i,0,\\\n",x,y,width,height);
532 fprintf(m_wxr,"[8, 'wxSWISS', 'wxNORMAL', 'wxNORMAL', 0, 'MS Sans Serif']],\\\n");
533 }
534
535
536
537 }
538
539 BEGIN_EVENT_TABLE(wxFileProgressDlg,wxDialog)
540 END_EVENT_TABLE()
541
542 wxFileProgressDlg::wxFileProgressDlg()
543 {
544 wxPoint pos;
545 wxSize size;
546 pos = ConvertDialogToPixels(wxPoint(10,10));
547 size = ConvertDialogToPixels(wxSize(170,31));
548 Create(GetParent(),100,"Parsing RC File",pos,size,603985920);
549 SetClientSize(size);
550 Move(pos);
551 //wxGauge Control
552 pos = ConvertDialogToPixels(wxPoint(16,16));
553 size = ConvertDialogToPixels(wxSize(136,6));
554 m_pProgress = new wxGauge(this,101,100,pos,size);
555 //wxStaticText Control
556 pos = ConvertDialogToPixels(wxPoint(72,4));
557 size = ConvertDialogToPixels(wxSize(18,6));
558 m_pCompleteLabel= new wxStaticText(this,102,"0",pos,size,0);
559 }
560 wxFileProgressDlg::~wxFileProgressDlg()
561 {
562
563 }
564
565 void wxFileProgressDlg::UpdateProgress(wxFile * f)
566 {
567 int p;
568 p=(int)((float)f->Tell()/(float)f->Length()*100.0);
569 m_pProgress->SetValue(p);
570 wxString t;
571 t.sprintf("%i%%",p);
572 m_pCompleteLabel->SetLabel(t);
573 Refresh();
574 }
575
576
577
578 //////////////////////////////////////////////////////////////////////
579 // GenerateBitmapSrc Class
580 //////////////////////////////////////////////////////////////////////
581
582 //////////////////////////////////////////////////////////////////////
583 // Construction/Destruction
584 //////////////////////////////////////////////////////////////////////
585
586 GenerateBitmapSrc::GenerateBitmapSrc()
587 {
588
589 }
590
591 GenerateBitmapSrc::~GenerateBitmapSrc()
592 {
593
594 }
595
596 bool GenerateBitmapSrc::Create(wxString imfile, wxString srcfile,wxString varname)
597 {
598
599 wxImage img;
600 FILE *src;
601
602 int h,w;
603
604 img.LoadFile(imfile,wxBITMAP_TYPE_ANY);
605 h=img.GetHeight();
606 w=img.GetWidth();
607
608 if( (src = fopen( srcfile, "at" )) == NULL )
609 return FALSE;
610 fprintf(src,"#if !defined(IMG_%s)\n",varname);
611 fprintf(src,"#define IMG_%s\n",varname);
612
613 fprintf(src,"//Data from bitmap file %s \n",imfile);
614 fprintf(src,"//Image Height=%i,Width=%i RGB format\n",h,w);
615 fprintf(src,"static unsigned char %s[][3]={\n",varname);
616
617
618 for (int y=0;y<h;y++)
619 {
620 for (int x=0;x<w;x++)
621 {
622 //fprintf(src,"{%i,%i,%i},",img.GetRed(x,y),img.GetGreen(x,y),img.GetBlue(x,y));
623
624 }
625 fprintf(src,"\n");
626 }
627
628 fprintf(src,"};\n\n");
629
630 fprintf(src,"wxBitmap Load%s()\n{\n",varname);
631 fprintf(src,"wxImage myimg(%i,%i);\n",w,h);
632 int size=w*h*3;
633 fprintf(src,"memcpy(myimg.GetData(),&%s[0][0],%i);\n",varname,size);
634 fprintf(src,"return myimg.ConvertToBitmap();\n");
635 fprintf(src,"}\n");
636 fprintf(src,"#endif\n");
637 fclose(src);
638
639 return TRUE;
640 }
641