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