generated resources code doesn't have to include <wx/wx.h>
[wxWidgets.git] / utils / wxrc / wxrc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxrc.cpp
3 // Purpose: XML resource compiler
4 // Author: Vaclav Slavik, Eduardo Marques <edrdo@netcabo.pt>
5 // Created: 2000/03/05
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #if defined(__GNUG__) && !defined(__APPLE__)
12 #pragma implementation
13 #pragma interface
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 // for all others, include the necessary headers
24 #ifndef WX_PRECOMP
25 #include "wx/app.h"
26 #include "wx/log.h"
27 #endif
28
29 #include "wx/cmdline.h"
30 #include "wx/xml/xml.h"
31 #include "wx/ffile.h"
32 #include "wx/filename.h"
33 #include "wx/wfstream.h"
34
35
36 class XRCWidgetData
37 {
38 public:
39 XRCWidgetData(const wxString& vname,const wxString& vclass)
40 : m_class(vclass), m_name(vname) {}
41 const wxString& GetName() const { return m_name; }
42 const wxString& GetClass() const { return m_class; }
43 private:
44 wxString m_class;
45 wxString m_name;
46 };
47 #include "wx/arrimpl.cpp"
48 WX_DECLARE_OBJARRAY(XRCWidgetData,ArrayOfXRCWidgetData);
49 WX_DEFINE_OBJARRAY(ArrayOfXRCWidgetData);
50
51 class XRCWndClassData
52 {
53 private:
54 wxString m_className;
55 wxString m_parentClassName;
56 ArrayOfXRCWidgetData m_wdata;
57
58 void BrowseXmlNode(wxXmlNode* node)
59 {
60 wxString classValue;
61 wxString nameValue;
62 wxXmlNode* children;
63 while (node)
64 {
65 if (node->GetName() == _T("object")
66 && node->GetPropVal(_T("class"),&classValue)
67 && node->GetPropVal(_T("name"),&nameValue))
68 {
69 m_wdata.Add(XRCWidgetData(nameValue,classValue));
70 }
71 children = node->GetChildren();
72 if (children)
73 BrowseXmlNode(children);
74 node = node->GetNext();
75 }
76 }
77
78 public:
79 XRCWndClassData(const wxString& className,const wxString& parentClassName, const wxXmlNode* node) :
80 m_className(className) , m_parentClassName(parentClassName) {
81
82 BrowseXmlNode(node->GetChildren());
83
84 }
85
86 const ArrayOfXRCWidgetData& GetWidgetData(){
87 return m_wdata;
88 }
89
90 bool IsRealClass(const wxString& name)
91 {
92 if (name == _T("tool") || name == _T("unknown") ||
93 name == _T("notebookpage") || name == _T("separator") ||
94 name == _T("sizeritem") ||
95 name == _T("wxMenuItem"))
96 {
97 return false;
98 }
99 return true;
100 }
101
102 void GenerateHeaderCode(wxFFile& file)
103 {
104
105 file.Write(_T("class ") + m_className + _T(" : public ") + m_parentClassName
106 + _T(" {\nprotected:\n"));
107 size_t i;
108 for(i=0;i<m_wdata.Count();++i)
109 {
110 const XRCWidgetData& w = m_wdata.Item(i);
111 if( !IsRealClass(w.GetClass()) ) continue;
112 if( w.GetName().Length() == 0 ) continue;
113 file.Write(
114 _T(" ") + w.GetClass() + _T("* ") + w.GetName()
115 + _T(";\n"));
116 }
117 file.Write(_T("\nprivate:\n void InitWidgetsFromXRC(){\n")
118 _T(" wxXmlResource::Get()->LoadObject(this,NULL,\"")
119 + m_className
120 + _T("\",\"")
121 + m_parentClassName
122 + _T("\");\n"));
123 for(i=0;i<m_wdata.Count();++i)
124 {
125 const XRCWidgetData& w = m_wdata.Item(i);
126 if( !IsRealClass(w.GetClass()) ) continue;
127 if( w.GetName().Length() == 0 ) continue;
128 file.Write( _T(" ")
129 + w.GetName()
130 + _T(" = XRCCTRL(*this,\"")
131 + w.GetName()
132 + _T("\",")
133 + w.GetClass()
134 + _T(");\n")
135 );
136 }
137 file.Write(_T(" }\n"));
138
139 file.Write(
140 _T("public:\n")
141 + m_className
142 + _T("::")
143 + m_className
144 + _T("(){\n")
145 + _T(" InitWidgetsFromXRC();\n")
146 _T(" }\n")
147 _T("};\n"));
148 };
149 };
150 WX_DECLARE_OBJARRAY(XRCWndClassData,ArrayOfXRCWndClassData);
151 WX_DEFINE_OBJARRAY(ArrayOfXRCWndClassData);
152
153
154 class XmlResApp : public wxAppConsole
155 {
156 public:
157 // don't use builtin cmd line parsing:
158 virtual bool OnInit() { return true; }
159 virtual int OnRun();
160
161 private:
162 void ParseParams(const wxCmdLineParser& cmdline);
163 void CompileRes();
164 wxArrayString PrepareTempFiles();
165 void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
166
167 wxString GetInternalFileName(const wxString& name, const wxArrayString& flist);
168 void DeleteTempFiles(const wxArrayString& flist);
169 void MakePackageZIP(const wxArrayString& flist);
170 void MakePackageCPP(const wxArrayString& flist);
171 void MakePackagePython(const wxArrayString& flist);
172
173 void OutputGettext();
174 wxArrayString FindStrings();
175 wxArrayString FindStrings(wxXmlNode *node);
176
177 bool flagVerbose, flagCPP, flagPython, flagGettext;
178 wxString parOutput, parFuncname, parOutputPath;
179 wxArrayString parFiles;
180 int retCode;
181
182 ArrayOfXRCWndClassData aXRCWndClassData;
183 bool flagH;
184 void GenCPPHeader();
185 };
186
187 IMPLEMENT_APP_CONSOLE(XmlResApp)
188
189 int XmlResApp::OnRun()
190 {
191 static const wxCmdLineEntryDesc cmdLineDesc[] =
192 {
193 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show help message"),
194 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
195 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") },
196 { wxCMD_LINE_SWITCH, _T("e"), _T("extra-cpp-code"), _T("output C++ header file with XRC derived classes") },
197 { wxCMD_LINE_SWITCH, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file") },
198 { wxCMD_LINE_SWITCH, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file") },
199 { wxCMD_LINE_SWITCH, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)") },
200 { wxCMD_LINE_OPTION, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]") },
201 { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]") },
202 #if 0 // not yet implemented
203 { wxCMD_LINE_OPTION, _T("l"), _T("list-of-handlers", _T("output list of neccessary handlers to this file" },
204 #endif
205 { wxCMD_LINE_PARAM, NULL, NULL, _T("input file(s)"),
206 wxCMD_LINE_VAL_STRING,
207 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY },
208
209 { wxCMD_LINE_NONE }
210 };
211
212 wxCmdLineParser parser(cmdLineDesc, argc, argv);
213
214 switch (parser.Parse())
215 {
216 case -1:
217 return 0;
218 #if 0
219 // break is unreachable because of return earlier
220 break;
221 #endif
222
223 case 0:
224 retCode = 0;
225 ParseParams(parser);
226 if (flagGettext)
227 OutputGettext();
228 else
229 CompileRes();
230 return retCode;
231 #if 0
232 // break is unreachable because of return earlier
233 break;
234 #endif
235
236 #if 0
237 // default return moved outside of switch to avoid warning about lack of return in function
238 default:
239 return 1;
240 break;
241 #endif
242 }
243 return 1;
244 }
245
246
247
248
249 void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
250 {
251 flagGettext = cmdline.Found(_T("g"));
252 flagVerbose = cmdline.Found(_T("v"));
253 flagCPP = cmdline.Found(_T("c"));
254 flagPython = cmdline.Found(_T("p"));
255 flagH = flagCPP && cmdline.Found(_T("e"));
256
257
258 if (!cmdline.Found(_T("o"), &parOutput))
259 {
260 if (flagGettext)
261 parOutput = wxEmptyString;
262 else
263 {
264 if (flagCPP)
265 parOutput = _T("resource.cpp");
266 else if (flagPython)
267 parOutput = _T("resource.py");
268 else
269 parOutput = _T("resource.xrs");
270 }
271 }
272 if (!parOutput.empty())
273 {
274 wxFileName fn(parOutput);
275 fn.Normalize();
276 parOutput = fn.GetFullPath();
277 parOutputPath = wxPathOnly(parOutput);
278 }
279 if (!parOutputPath) parOutputPath = _T(".");
280
281 if (!cmdline.Found(_T("n"), &parFuncname))
282 parFuncname = _T("InitXmlResource");
283
284 for (size_t i = 0; i < cmdline.GetParamCount(); i++)
285 {
286 #ifdef __WINDOWS__
287 wxString fn=wxFindFirstFile(cmdline.GetParam(i), wxFILE);
288 while (!fn.IsEmpty())
289 {
290 parFiles.Add(fn);
291 fn=wxFindNextFile();
292 }
293 #else
294 parFiles.Add(cmdline.GetParam(i));
295 #endif
296 }
297 }
298
299
300
301
302 void XmlResApp::CompileRes()
303 {
304 wxArrayString files = PrepareTempFiles();
305
306 wxRemoveFile(parOutput);
307
308 if (!retCode)
309 {
310 if (flagCPP){
311 MakePackageCPP(files);
312 if (flagH)
313 GenCPPHeader();
314 }
315 else if (flagPython)
316 MakePackagePython(files);
317 else
318 MakePackageZIP(files);
319 }
320
321 DeleteTempFiles(files);
322 }
323
324
325 wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
326 {
327 wxString name2 = name;
328 name2.Replace(_T(":"), _T("_"));
329 name2.Replace(_T("/"), _T("_"));
330 name2.Replace(_T("\\"), _T("_"));
331 name2.Replace(_T("*"), _T("_"));
332 name2.Replace(_T("?"), _T("_"));
333
334 wxString s = wxFileNameFromPath(parOutput) + _T("$") + name2;
335
336 if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
337 {
338 for (int i = 0;; i++)
339 {
340 s.Printf(wxFileNameFromPath(parOutput) + _T("$%03i-") + name2, i);
341 if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
342 break;
343 }
344 }
345 return s;
346 }
347
348 wxArrayString XmlResApp::PrepareTempFiles()
349 {
350 wxArrayString flist;
351
352 for (size_t i = 0; i < parFiles.Count(); i++)
353 {
354 if (flagVerbose)
355 wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
356
357 wxXmlDocument doc;
358
359 if (!doc.Load(parFiles[i]))
360 {
361 wxLogError(_T("Error parsing file ") + parFiles[i]);
362 retCode = 1;
363 continue;
364 }
365
366 wxString name, ext, path;
367 wxSplitPath(parFiles[i], &path, &name, &ext);
368
369 FindFilesInXML(doc.GetRoot(), flist, path);
370 if (flagH)
371 {
372 wxXmlNode* node = (doc.GetRoot())->GetChildren();
373 wxString classValue,nameValue;
374 while(node){
375 if(node->GetName() == _T("object")
376 && node->GetPropVal(_T("class"),&classValue)
377 && node->GetPropVal(_T("name"),&nameValue)){
378
379 aXRCWndClassData.Add(
380 XRCWndClassData(nameValue,classValue,node)
381 );
382 }
383 node = node -> GetNext();
384 }
385 }
386 wxString internalName = GetInternalFileName(parFiles[i], flist);
387
388 doc.Save(parOutputPath + wxFILE_SEP_PATH + internalName);
389 flist.Add(internalName);
390 }
391
392 return flist;
393 }
394
395
396 // Does 'node' contain filename information at all?
397 static bool NodeContainsFilename(wxXmlNode *node)
398 {
399 // Any bitmaps:
400 if (node->GetName() == _T("bitmap"))
401 return true;
402
403 // URLs in wxHtmlWindow:
404 if (node->GetName() == _T("url"))
405 return true;
406
407 // wxBitmapButton:
408 wxXmlNode *parent = node->GetParent();
409 if (parent != NULL &&
410 parent->GetPropVal(_T("class"), _T("")) == _T("wxBitmapButton") &&
411 (node->GetName() == _T("focus") ||
412 node->GetName() == _T("disabled") ||
413 node->GetName() == _T("selected")))
414 return true;
415
416 // wxBitmap or wxIcon toplevel resources:
417 if (node->GetName() == _T("object"))
418 {
419 wxString klass = node->GetPropVal(_T("class"), wxEmptyString);
420 if (klass == _T("wxBitmap") || klass == _T("wxIcon"))
421 return true;
422 }
423
424 return false;
425 }
426
427 // find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
428 void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
429 {
430 // Is 'node' XML node element?
431 if (node == NULL) return;
432 if (node->GetType() != wxXML_ELEMENT_NODE) return;
433
434 bool containsFilename = NodeContainsFilename(node);
435
436 wxXmlNode *n = node->GetChildren();
437 while (n)
438 {
439 if (containsFilename &&
440 (n->GetType() == wxXML_TEXT_NODE ||
441 n->GetType() == wxXML_CDATA_SECTION_NODE))
442 {
443 wxString fullname;
444 if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty())
445 fullname = n->GetContent();
446 else
447 fullname = inputPath + wxFILE_SEP_PATH + n->GetContent();
448
449 if (flagVerbose)
450 wxPrintf(_T("adding ") + fullname + _T("...\n"));
451
452 wxString filename = GetInternalFileName(n->GetContent(), flist);
453 n->SetContent(filename);
454
455 if (flist.Index(filename) == wxNOT_FOUND)
456 flist.Add(filename);
457
458 wxFileInputStream sin(fullname);
459 wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename);
460 sin.Read(sout); // copy the stream
461 }
462
463 // subnodes:
464 if (n->GetType() == wxXML_ELEMENT_NODE)
465 FindFilesInXML(n, flist, inputPath);
466
467 n = n->GetNext();
468 }
469 }
470
471
472
473 void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
474 {
475 for (size_t i = 0; i < flist.Count(); i++)
476 wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]);
477 }
478
479
480
481 void XmlResApp::MakePackageZIP(const wxArrayString& flist)
482 {
483 wxString files;
484
485 for (size_t i = 0; i < flist.Count(); i++)
486 files += flist[i] + _T(" ");
487 files.RemoveLast();
488
489 if (flagVerbose)
490 wxPrintf(_T("compressing ") + parOutput + _T("...\n"));
491
492 wxString cwd = wxGetCwd();
493 wxSetWorkingDirectory(parOutputPath);
494 int execres = wxExecute(_T("zip -9 -j ") +
495 wxString(flagVerbose ? _T("") : _T("-q ")) +
496 parOutput + _T(" ") + files, true);
497 wxSetWorkingDirectory(cwd);
498 if (execres == -1)
499 {
500 wxLogError(_T("Unable to execute zip program. Make sure it is in the path."));
501 wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/"));
502 retCode = 1;
503 return;
504 }
505 }
506
507
508
509 static wxString FileToCppArray(wxString filename, int num)
510 {
511 wxString output;
512 wxString tmp;
513 wxString snum;
514 wxFFile file(filename, wxT("rb"));
515 size_t lng = file.Length();
516
517 snum.Printf(_T("%i"), num);
518 output.Printf(_T("static size_t xml_res_size_") + snum + _T(" = %i;\n"), lng);
519 output += _T("static unsigned char xml_res_file_") + snum + _T("[] = {\n");
520 // we cannot use string literals because MSVC is dumb wannabe compiler
521 // with arbitrary limitation to 2048 strings :(
522
523 unsigned char *buffer = new unsigned char[lng];
524 file.Read(buffer, lng);
525
526 for (size_t i = 0, linelng = 0; i < lng; i++)
527 {
528 tmp.Printf(_T("%i"), buffer[i]);
529 if (i != 0) output << _T(',');
530 if (linelng > 70)
531 {
532 linelng = 0;
533 output << _T("\n");
534 }
535 output << tmp;
536 linelng += tmp.Length()+1;
537 }
538
539 delete[] buffer;
540
541 output += _T("};\n\n");
542
543 return output;
544 }
545
546
547 void XmlResApp::MakePackageCPP(const wxArrayString& flist)
548 {
549 wxFFile file(parOutput, wxT("wt"));
550 size_t i;
551
552 if (flagVerbose)
553 wxPrintf(_T("creating C++ source file ") + parOutput + _T("...\n"));
554
555 file.Write(_T("")
556 _T("//\n")
557 _T("// This file was automatically generated by wxrc, do not edit by hand.\n")
558 _T("//\n\n")
559 _T("#include <wx/wxprec.h>\n")
560 _T("\n")
561 _T("#ifdef __BORLANDC__\n")
562 _T(" #pragma hdrstop\n")
563 _T("#endif\n")
564 _T("\n")
565 _T("")
566 _T("#include <wx/filesys.h>\n")
567 _T("#include <wx/fs_mem.h>\n")
568 _T("#include <wx/xrc/xmlres.h>\n")
569 _T("#include <wx/xrc/xh_all.h>\n")
570 _T("\n"));
571
572 for (i = 0; i < flist.Count(); i++)
573 file.Write(
574 FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
575
576 file.Write(_T("")
577 _T("void ") + parFuncname + wxT("()\n")
578 _T("{\n")
579 _T("\n")
580 _T(" // Check for memory FS. If not present, load the handler:\n")
581 _T(" {\n")
582 _T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n")
583 _T(" wxFileSystem fsys;\n")
584 _T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n")
585 _T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n")
586 _T(" if (f) delete f;\n")
587 _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
588 _T(" }\n")
589 _T("\n"));
590
591 for (i = 0; i < flist.Count(); i++)
592 {
593 wxString s;
594 s.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist[i] +
595 _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i, i);
596 file.Write(s);
597 }
598
599 for (i = 0; i < parFiles.Count(); i++)
600 {
601 file.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") +
602 GetInternalFileName(parFiles[i], flist) + _T("\"));\n"));
603 }
604
605 file.Write(_T("}\n"));
606
607
608 }
609
610 void XmlResApp::GenCPPHeader()
611 {
612 wxString fileSpec = ((parOutput.BeforeLast('.')).AfterLast('/')).AfterLast('\\');
613 wxString heaFileName = fileSpec + _T(".h");
614
615 wxFFile file(heaFileName, wxT("wt"));
616 file.Write(
617 _T("//\n")
618 _T("// This file was automatically generated by wxrc, do not edit by hand.\n")
619 _T("//\n\n")
620 _T("#ifndef __") + fileSpec + _T("_h__\n")
621 _T("#define __") + fileSpec + _T("_h__\n")
622 );
623 for(size_t i=0;i<aXRCWndClassData.Count();++i){
624 aXRCWndClassData.Item(i).GenerateHeaderCode(file);
625 }
626 file.Write(
627 _T("\nvoid \n")
628 + parFuncname
629 + _T("();\n#endif\n"));
630 }
631
632 static wxString FileToPythonArray(wxString filename, int num)
633 {
634 wxString output;
635 wxString tmp;
636 wxString snum;
637 wxFFile file(filename, wxT("rb"));
638 size_t lng = file.Length();
639
640 snum.Printf(_T("%i"), num);
641 output = _T(" xml_res_file_") + snum + _T(" = '''\\\n");
642
643 unsigned char *buffer = new unsigned char[lng];
644 file.Read(buffer, lng);
645
646 for (size_t i = 0, linelng = 0; i < lng; i++)
647 {
648 unsigned char c = buffer[i];
649 if (c == '\n')
650 {
651 tmp = (wxChar)c;
652 linelng = 0;
653 }
654 else if (c < 32 || c > 127 || c == '\'')
655 tmp.Printf(_T("\\x%02x"), c);
656 else if (c == '\\')
657 tmp = _T("\\\\");
658 else
659 tmp = (wxChar)c;
660 if (linelng > 70)
661 {
662 linelng = 0;
663 output << _T("\\\n");
664 }
665 output << tmp;
666 linelng += tmp.Length();
667 }
668
669 delete[] buffer;
670
671 output += _T("'''\n\n");
672
673 return output;
674 }
675
676
677 void XmlResApp::MakePackagePython(const wxArrayString& flist)
678 {
679 wxFFile file(parOutput, wxT("wt"));
680 size_t i;
681
682 if (flagVerbose)
683 wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n"));
684
685 file.Write(
686 _T("#\n")
687 _T("# This file was automatically generated by wxrc, do not edit by hand.\n")
688 _T("#\n\n")
689 _T("import wx\n")
690 _T("import wx.xrc\n\n")
691 );
692
693
694 file.Write(_T("def ") + parFuncname + _T("():\n"));
695
696 for (i = 0; i < flist.Count(); i++)
697 file.Write(
698 FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
699
700 file.Write(
701 _T(" # check if the memory filesystem handler has been loaded yet, and load it if not\n")
702 _T(" wx.MemoryFSHandler.AddFile('XRC_resource/dummy_file', 'dummy value')\n")
703 _T(" fsys = wx.FileSystem()\n")
704 _T(" f = fsys.OpenFile('memory:XRC_resource/dummy_file')\n")
705 _T(" wx.MemoryFSHandler.RemoveFile('XRC_resource/dummy_file')\n")
706 _T(" if f is not None:\n")
707 _T(" f.Destroy()\n")
708 _T(" else:\n")
709 _T(" wx.FileSystem.AddHandler(wx.MemoryFSHandler())\n")
710 _T("\n")
711 _T(" # load all the strings as memory files and load into XmlRes\n")
712 );
713
714
715 for (i = 0; i < flist.Count(); i++)
716 {
717 wxString s;
718 s.Printf(_T(" wx.MemoryFSHandler.AddFile('XRC_resource/") + flist[i] +
719 _T("', xml_res_file_%i)\n"), i);
720 file.Write(s);
721 }
722 for (i = 0; i < parFiles.Count(); i++)
723 {
724 file.Write(_T(" wx.xrc.XmlResource.Get().Load('memory:XRC_resource/") +
725 GetInternalFileName(parFiles[i], flist) + _T("')\n"));
726 }
727
728 file.Write(_T("\n"));
729 }
730
731
732
733 void XmlResApp::OutputGettext()
734 {
735 wxArrayString str = FindStrings();
736
737 wxFFile fout;
738 if (parOutput.empty())
739 fout.Attach(stdout);
740 else
741 fout.Open(parOutput, wxT("wt"));
742
743 for (size_t i = 0; i < str.GetCount(); i++)
744 fout.Write(_T("_(\"") + str[i] + _T("\");\n"));
745
746 if (!parOutput) fout.Detach();
747 }
748
749
750
751 wxArrayString XmlResApp::FindStrings()
752 {
753 wxArrayString arr, a2;
754
755 for (size_t i = 0; i < parFiles.Count(); i++)
756 {
757 if (flagVerbose)
758 wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
759
760 wxXmlDocument doc;
761 if (!doc.Load(parFiles[i]))
762 {
763 wxLogError(_T("Error parsing file ") + parFiles[i]);
764 retCode = 1;
765 continue;
766 }
767 a2 = FindStrings(doc.GetRoot());
768 WX_APPEND_ARRAY(arr, a2);
769 }
770
771 return arr;
772 }
773
774
775
776 static wxString ConvertText(const wxString& str)
777 {
778 wxString str2;
779 const wxChar *dt;
780
781 for (dt = str.c_str(); *dt; dt++)
782 {
783 if (*dt == wxT('_'))
784 {
785 if ( *(++dt) == wxT('_') )
786 str2 << wxT('_');
787 else
788 str2 << wxT('&') << *dt;
789 }
790 else
791 {
792 switch (*dt)
793 {
794 case wxT('\n') : str2 << wxT("\\n"); break;
795 case wxT('\t') : str2 << wxT("\\t"); break;
796 case wxT('\r') : str2 << wxT("\\r"); break;
797 case wxT('\\') : if ((*(dt+1) != 'n') &&
798 (*(dt+1) != 't') &&
799 (*(dt+1) != 'r'))
800 str2 << wxT("\\\\");
801 else
802 str2 << wxT("\\");
803 break;
804 case wxT('"') : str2 << wxT("\\\""); break;
805 default : str2 << *dt; break;
806 }
807 }
808 }
809
810 return str2;
811 }
812
813
814 wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
815 {
816 wxArrayString arr;
817
818 wxXmlNode *n = node;
819 if (n == NULL) return arr;
820 n = n->GetChildren();
821
822 while (n)
823 {
824 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
825 // parent is an element, i.e. has subnodes...
826 (n->GetType() == wxXML_TEXT_NODE ||
827 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
828 // ...it is textnode...
829 (
830 node/*not n!*/->GetName() == _T("label") ||
831 (node/*not n!*/->GetName() == _T("value") &&
832 !n->GetContent().IsNumber()) ||
833 node/*not n!*/->GetName() == _T("help") ||
834 node/*not n!*/->GetName() == _T("longhelp") ||
835 node/*not n!*/->GetName() == _T("tooltip") ||
836 node/*not n!*/->GetName() == _T("htmlcode") ||
837 node/*not n!*/->GetName() == _T("title") ||
838 node/*not n!*/->GetName() == _T("item")
839 ))
840 // ...and known to contain translatable string
841 {
842 if (!flagGettext ||
843 node->GetPropVal(_T("translate"), _T("1")) != _T("0"))
844 {
845 arr.Add(ConvertText(n->GetContent()));
846 }
847 }
848
849 // subnodes:
850 if (n->GetType() == wxXML_ELEMENT_NODE)
851 {
852 wxArrayString a2 = FindStrings(n);
853 WX_APPEND_ARRAY(arr, a2);
854 }
855
856 n = n->GetNext();
857 }
858 return arr;
859 }