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