]> git.saurik.com Git - wxWidgets.git/blob - utils/wxrc/wxrc.cpp
Get the stock label when stock ID is used with empty label in Create()
[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("#ifndef WX_PRECOMP\n")
566 _T(" #include <wx/wx.h>\n")
567 _T("#endif\n")
568 _T("")
569 _T("#include <wx/filesys.h>\n")
570 _T("#include <wx/fs_mem.h>\n")
571 _T("#include <wx/xrc/xmlres.h>\n")
572 _T("#include <wx/xrc/xh_all.h>\n")
573 _T("\n"));
574
575 for (i = 0; i < flist.Count(); i++)
576 file.Write(
577 FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
578
579 file.Write(_T("")
580 _T("void ") + parFuncname + wxT("()\n")
581 _T("{\n")
582 _T("\n")
583 _T(" // Check for memory FS. If not present, load the handler:\n")
584 _T(" {\n")
585 _T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n")
586 _T(" wxFileSystem fsys;\n")
587 _T(" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n")
588 _T(" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n")
589 _T(" if (f) delete f;\n")
590 _T(" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n")
591 _T(" }\n")
592 _T("\n"));
593
594 for (i = 0; i < flist.Count(); i++)
595 {
596 wxString s;
597 s.Printf(_T(" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/") + flist[i] +
598 _T("\"), xml_res_file_%i, xml_res_size_%i);\n"), i, i);
599 file.Write(s);
600 }
601
602 for (i = 0; i < parFiles.Count(); i++)
603 {
604 file.Write(_T(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/") +
605 GetInternalFileName(parFiles[i], flist) + _T("\"));\n"));
606 }
607
608 file.Write(_T("}\n"));
609
610
611 }
612
613 void XmlResApp::GenCPPHeader()
614 {
615 wxString fileSpec = ((parOutput.BeforeLast('.')).AfterLast('/')).AfterLast('\\');
616 wxString heaFileName = fileSpec + _T(".h");
617
618 wxFFile file(heaFileName, wxT("wt"));
619 file.Write(
620 _T("//\n")
621 _T("// This file was automatically generated by wxrc, do not edit by hand.\n")
622 _T("//\n\n")
623 _T("#ifndef __") + fileSpec + _T("_h__\n")
624 _T("#define __") + fileSpec + _T("_h__\n")
625 );
626 for(size_t i=0;i<aXRCWndClassData.Count();++i){
627 aXRCWndClassData.Item(i).GenerateHeaderCode(file);
628 }
629 file.Write(
630 _T("\nvoid \n")
631 + parFuncname
632 + _T("();\n#endif\n"));
633 }
634
635 static wxString FileToPythonArray(wxString filename, int num)
636 {
637 wxString output;
638 wxString tmp;
639 wxString snum;
640 wxFFile file(filename, wxT("rb"));
641 size_t lng = file.Length();
642
643 snum.Printf(_T("%i"), num);
644 output = _T(" xml_res_file_") + snum + _T(" = '''\\\n");
645
646 unsigned char *buffer = new unsigned char[lng];
647 file.Read(buffer, lng);
648
649 for (size_t i = 0, linelng = 0; i < lng; i++)
650 {
651 unsigned char c = buffer[i];
652 if (c == '\n')
653 {
654 tmp = (wxChar)c;
655 linelng = 0;
656 }
657 else if (c < 32 || c > 127 || c == '\'')
658 tmp.Printf(_T("\\x%02x"), c);
659 else if (c == '\\')
660 tmp = _T("\\\\");
661 else
662 tmp = (wxChar)c;
663 if (linelng > 70)
664 {
665 linelng = 0;
666 output << _T("\\\n");
667 }
668 output << tmp;
669 linelng += tmp.Length();
670 }
671
672 delete[] buffer;
673
674 output += _T("'''\n\n");
675
676 return output;
677 }
678
679
680 void XmlResApp::MakePackagePython(const wxArrayString& flist)
681 {
682 wxFFile file(parOutput, wxT("wt"));
683 size_t i;
684
685 if (flagVerbose)
686 wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n"));
687
688 file.Write(
689 _T("#\n")
690 _T("# This file was automatically generated by wxrc, do not edit by hand.\n")
691 _T("#\n\n")
692 _T("import wx\n")
693 _T("import wx.xrc\n\n")
694 );
695
696
697 file.Write(_T("def ") + parFuncname + _T("():\n"));
698
699 for (i = 0; i < flist.Count(); i++)
700 file.Write(
701 FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
702
703 file.Write(
704 _T(" # check if the memory filesystem handler has been loaded yet, and load it if not\n")
705 _T(" wx.MemoryFSHandler.AddFile('XRC_resource/dummy_file', 'dummy value')\n")
706 _T(" fsys = wx.FileSystem()\n")
707 _T(" f = fsys.OpenFile('memory:XRC_resource/dummy_file')\n")
708 _T(" wx.MemoryFSHandler.RemoveFile('XRC_resource/dummy_file')\n")
709 _T(" if f is not None:\n")
710 _T(" f.Destroy()\n")
711 _T(" else:\n")
712 _T(" wx.FileSystem.AddHandler(wx.MemoryFSHandler())\n")
713 _T("\n")
714 _T(" # load all the strings as memory files and load into XmlRes\n")
715 );
716
717
718 for (i = 0; i < flist.Count(); i++)
719 {
720 wxString s;
721 s.Printf(_T(" wx.MemoryFSHandler.AddFile('XRC_resource/") + flist[i] +
722 _T("', xml_res_file_%i)\n"), i);
723 file.Write(s);
724 }
725 for (i = 0; i < parFiles.Count(); i++)
726 {
727 file.Write(_T(" wx.xrc.XmlResource.Get().Load('memory:XRC_resource/") +
728 GetInternalFileName(parFiles[i], flist) + _T("')\n"));
729 }
730
731 file.Write(_T("\n"));
732 }
733
734
735
736 void XmlResApp::OutputGettext()
737 {
738 wxArrayString str = FindStrings();
739
740 wxFFile fout;
741 if (parOutput.empty())
742 fout.Attach(stdout);
743 else
744 fout.Open(parOutput, wxT("wt"));
745
746 for (size_t i = 0; i < str.GetCount(); i++)
747 fout.Write(_T("_(\"") + str[i] + _T("\");\n"));
748
749 if (!parOutput) fout.Detach();
750 }
751
752
753
754 wxArrayString XmlResApp::FindStrings()
755 {
756 wxArrayString arr, a2;
757
758 for (size_t i = 0; i < parFiles.Count(); i++)
759 {
760 if (flagVerbose)
761 wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
762
763 wxXmlDocument doc;
764 if (!doc.Load(parFiles[i]))
765 {
766 wxLogError(_T("Error parsing file ") + parFiles[i]);
767 retCode = 1;
768 continue;
769 }
770 a2 = FindStrings(doc.GetRoot());
771 WX_APPEND_ARRAY(arr, a2);
772 }
773
774 return arr;
775 }
776
777
778
779 static wxString ConvertText(const wxString& str)
780 {
781 wxString str2;
782 const wxChar *dt;
783
784 for (dt = str.c_str(); *dt; dt++)
785 {
786 if (*dt == wxT('_'))
787 {
788 if ( *(++dt) == wxT('_') )
789 str2 << wxT('_');
790 else
791 str2 << wxT('&') << *dt;
792 }
793 else
794 {
795 switch (*dt)
796 {
797 case wxT('\n') : str2 << wxT("\\n"); break;
798 case wxT('\t') : str2 << wxT("\\t"); break;
799 case wxT('\r') : str2 << wxT("\\r"); break;
800 case wxT('\\') : if ((*(dt+1) != 'n') &&
801 (*(dt+1) != 't') &&
802 (*(dt+1) != 'r'))
803 str2 << wxT("\\\\");
804 else
805 str2 << wxT("\\");
806 break;
807 case wxT('"') : str2 << wxT("\\\""); break;
808 default : str2 << *dt; break;
809 }
810 }
811 }
812
813 return str2;
814 }
815
816
817 wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
818 {
819 wxArrayString arr;
820
821 wxXmlNode *n = node;
822 if (n == NULL) return arr;
823 n = n->GetChildren();
824
825 while (n)
826 {
827 if ((node->GetType() == wxXML_ELEMENT_NODE) &&
828 // parent is an element, i.e. has subnodes...
829 (n->GetType() == wxXML_TEXT_NODE ||
830 n->GetType() == wxXML_CDATA_SECTION_NODE) &&
831 // ...it is textnode...
832 (
833 node/*not n!*/->GetName() == _T("label") ||
834 (node/*not n!*/->GetName() == _T("value") &&
835 !n->GetContent().IsNumber()) ||
836 node/*not n!*/->GetName() == _T("help") ||
837 node/*not n!*/->GetName() == _T("longhelp") ||
838 node/*not n!*/->GetName() == _T("tooltip") ||
839 node/*not n!*/->GetName() == _T("htmlcode") ||
840 node/*not n!*/->GetName() == _T("title") ||
841 node/*not n!*/->GetName() == _T("item")
842 ))
843 // ...and known to contain translatable string
844 {
845 if (!flagGettext ||
846 node->GetPropVal(_T("translate"), _T("1")) != _T("0"))
847 {
848 arr.Add(ConvertText(n->GetContent()));
849 }
850 }
851
852 // subnodes:
853 if (n->GetType() == wxXML_ELEMENT_NODE)
854 {
855 wxArrayString a2 = FindStrings(n);
856 WX_APPEND_ARRAY(arr, a2);
857 }
858
859 n = n->GetNext();
860 }
861 return arr;
862 }