2000-07-25 14:47:21 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wxrc.cpp
|
|
|
|
// Purpose: XML resource compiler
|
2004-02-05 16:58:29 -05:00
|
|
|
// Author: Vaclav Slavik, Eduardo Marques <edrdo@netcabo.pt>
|
2000-07-25 14:47:21 -04:00
|
|
|
// Created: 2000/03/05
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2000 Vaclav Slavik
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
2003-09-19 11:44:28 -04:00
|
|
|
// for all others, include the necessary headers
|
2000-07-25 14:47:21 -04:00
|
|
|
#ifndef WX_PRECOMP
|
2003-09-19 11:44:28 -04:00
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/log.h"
|
2007-04-27 17:03:46 -04:00
|
|
|
#include "wx/wxcrtvararg.h"
|
2000-07-25 14:47:21 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/cmdline.h"
|
2003-07-20 18:15:32 -04:00
|
|
|
#include "wx/xml/xml.h"
|
2000-07-25 14:47:21 -04:00
|
|
|
#include "wx/ffile.h"
|
2002-12-25 10:23:44 -05:00
|
|
|
#include "wx/filename.h"
|
2000-08-11 15:07:43 -04:00
|
|
|
#include "wx/wfstream.h"
|
2005-01-21 07:40:19 -05:00
|
|
|
#include "wx/utils.h"
|
2006-05-06 18:53:06 -04:00
|
|
|
#include "wx/hashset.h"
|
2007-06-18 14:43:34 -04:00
|
|
|
#include "wx/mimetype.h"
|
2000-08-11 15:07:43 -04:00
|
|
|
|
2006-05-06 18:53:06 -04:00
|
|
|
WX_DECLARE_HASH_SET(wxString, wxStringHash, wxStringEqual, StringSet);
|
2000-08-11 15:07:43 -04:00
|
|
|
|
2004-02-05 16:58:29 -05:00
|
|
|
class XRCWidgetData
|
|
|
|
{
|
|
|
|
public:
|
2004-06-11 09:14:23 -04:00
|
|
|
XRCWidgetData(const wxString& vname,const wxString& vclass)
|
2004-02-05 16:58:29 -05:00
|
|
|
: m_class(vclass), m_name(vname) {}
|
|
|
|
const wxString& GetName() const { return m_name; }
|
|
|
|
const wxString& GetClass() const { return m_class; }
|
|
|
|
private:
|
|
|
|
wxString m_class;
|
|
|
|
wxString m_name;
|
|
|
|
};
|
2005-01-13 14:57:24 -05:00
|
|
|
|
2004-02-05 16:58:29 -05:00
|
|
|
#include "wx/arrimpl.cpp"
|
|
|
|
WX_DECLARE_OBJARRAY(XRCWidgetData,ArrayOfXRCWidgetData);
|
2005-09-25 15:59:19 -04:00
|
|
|
WX_DEFINE_OBJARRAY(ArrayOfXRCWidgetData)
|
2004-02-05 16:58:29 -05:00
|
|
|
|
|
|
|
class XRCWndClassData
|
|
|
|
{
|
2004-06-11 09:14:23 -04:00
|
|
|
private:
|
2004-02-05 16:58:29 -05:00
|
|
|
wxString m_className;
|
|
|
|
wxString m_parentClassName;
|
2006-05-06 18:53:06 -04:00
|
|
|
StringSet m_ancestorClassNames;
|
2004-02-05 16:58:29 -05:00
|
|
|
ArrayOfXRCWidgetData m_wdata;
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2004-02-05 16:58:29 -05:00
|
|
|
void BrowseXmlNode(wxXmlNode* node)
|
|
|
|
{
|
|
|
|
wxString classValue;
|
|
|
|
wxString nameValue;
|
2004-06-11 09:14:23 -04:00
|
|
|
wxXmlNode* children;
|
2004-02-05 16:58:29 -05:00
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
if (node->GetName() == _T("object")
|
2007-07-14 06:07:41 -04:00
|
|
|
&& node->GetAttribute(_T("class"),&classValue)
|
|
|
|
&& node->GetAttribute(_T("name"),&nameValue))
|
2004-02-05 16:58:29 -05:00
|
|
|
{
|
|
|
|
m_wdata.Add(XRCWidgetData(nameValue,classValue));
|
|
|
|
}
|
|
|
|
children = node->GetChildren();
|
|
|
|
if (children)
|
2004-06-11 09:14:23 -04:00
|
|
|
BrowseXmlNode(children);
|
2004-02-05 16:58:29 -05:00
|
|
|
node = node->GetNext();
|
|
|
|
}
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2004-02-05 16:58:29 -05:00
|
|
|
public:
|
2006-05-06 18:53:06 -04:00
|
|
|
XRCWndClassData(const wxString& className,
|
|
|
|
const wxString& parentClassName,
|
|
|
|
const wxXmlNode* node) :
|
2005-01-13 14:57:24 -05:00
|
|
|
m_className(className) , m_parentClassName(parentClassName)
|
|
|
|
{
|
2006-05-06 18:53:06 -04:00
|
|
|
if ( className == _T("wxMenu") )
|
|
|
|
{
|
|
|
|
m_ancestorClassNames.insert(_T("wxMenu"));
|
|
|
|
m_ancestorClassNames.insert(_T("wxMenuBar"));
|
|
|
|
}
|
|
|
|
else if ( className == _T("wxMDIChildFrame") )
|
|
|
|
{
|
|
|
|
m_ancestorClassNames.insert(_T("wxMDIParentFrame"));
|
|
|
|
}
|
|
|
|
else if( className == _T("wxMenuBar") ||
|
|
|
|
className == _T("wxStatusBar") ||
|
|
|
|
className == _T("wxToolBar") )
|
|
|
|
{
|
|
|
|
m_ancestorClassNames.insert(_T("wxFrame"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_ancestorClassNames.insert(_T("wxWindow"));
|
|
|
|
}
|
|
|
|
|
2004-03-18 12:23:31 -05:00
|
|
|
BrowseXmlNode(node->GetChildren());
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2005-01-13 14:57:24 -05:00
|
|
|
const ArrayOfXRCWidgetData& GetWidgetData()
|
|
|
|
{
|
2004-03-18 12:23:31 -05:00
|
|
|
return m_wdata;
|
|
|
|
}
|
2004-08-23 08:55:16 -04:00
|
|
|
|
2007-03-17 12:48:11 -04:00
|
|
|
bool CanBeUsedWithXRCCTRL(const wxString& name)
|
2004-08-23 08:55:16 -04:00
|
|
|
{
|
2005-01-13 14:57:24 -05:00
|
|
|
if (name == _T("tool") ||
|
2007-04-10 20:19:20 -04:00
|
|
|
name == _T("data") ||
|
2005-01-13 14:57:24 -05:00
|
|
|
name == _T("unknown") ||
|
|
|
|
name == _T("notebookpage") ||
|
|
|
|
name == _T("separator") ||
|
2004-08-23 08:55:16 -04:00
|
|
|
name == _T("sizeritem") ||
|
2007-03-17 12:46:23 -04:00
|
|
|
name == _T("wxMenuBar") ||
|
|
|
|
name == _T("wxMenuItem") ||
|
|
|
|
name == _T("wxStaticBoxSizer") )
|
2004-08-23 08:55:16 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2005-01-13 14:57:24 -05:00
|
|
|
|
2004-08-23 08:55:16 -04:00
|
|
|
void GenerateHeaderCode(wxFFile& file)
|
|
|
|
{
|
2004-03-18 12:23:31 -05:00
|
|
|
|
|
|
|
file.Write(_T("class ") + m_className + _T(" : public ") + m_parentClassName
|
|
|
|
+ _T(" {\nprotected:\n"));
|
|
|
|
size_t i;
|
2007-04-10 20:23:19 -04:00
|
|
|
for(i=0;i<m_wdata.GetCount();++i)
|
2004-08-23 08:55:16 -04:00
|
|
|
{
|
2004-03-18 12:23:31 -05:00
|
|
|
const XRCWidgetData& w = m_wdata.Item(i);
|
2007-03-17 12:48:11 -04:00
|
|
|
if( !CanBeUsedWithXRCCTRL(w.GetClass()) ) continue;
|
2005-01-13 14:57:24 -05:00
|
|
|
if( w.GetName().Length() == 0 ) continue;
|
2004-03-18 12:23:31 -05:00
|
|
|
file.Write(
|
|
|
|
_T(" ") + w.GetClass() + _T("* ") + w.GetName()
|
|
|
|
+ _T(";\n"));
|
|
|
|
}
|
2006-05-06 18:53:06 -04:00
|
|
|
file.Write(_T("\nprivate:\n void InitWidgetsFromXRC(wxWindow *parent){\n")
|
|
|
|
_T(" wxXmlResource::Get()->LoadObject(this,parent,_T(\"")
|
2004-06-11 09:14:23 -04:00
|
|
|
+ m_className
|
2004-11-16 15:49:58 -05:00
|
|
|
+ _T("\"), _T(\"")
|
2004-06-11 09:14:23 -04:00
|
|
|
+ m_parentClassName
|
2004-11-16 15:49:58 -05:00
|
|
|
+ _T("\"));\n"));
|
2007-04-10 20:23:19 -04:00
|
|
|
for(i=0;i<m_wdata.GetCount();++i)
|
2004-08-23 08:55:16 -04:00
|
|
|
{
|
2004-03-18 12:23:31 -05:00
|
|
|
const XRCWidgetData& w = m_wdata.Item(i);
|
2007-03-17 12:48:11 -04:00
|
|
|
if( !CanBeUsedWithXRCCTRL(w.GetClass()) ) continue;
|
2005-01-13 14:57:24 -05:00
|
|
|
if( w.GetName().Length() == 0 ) continue;
|
2004-06-11 09:14:23 -04:00
|
|
|
file.Write( _T(" ")
|
|
|
|
+ w.GetName()
|
2004-03-18 12:23:31 -05:00
|
|
|
+ _T(" = XRCCTRL(*this,\"")
|
2004-06-11 09:14:23 -04:00
|
|
|
+ w.GetName()
|
2004-03-18 12:23:31 -05:00
|
|
|
+ _T("\",")
|
|
|
|
+ w.GetClass()
|
2006-05-06 18:53:06 -04:00
|
|
|
+ _T(");\n"));
|
2004-03-18 12:23:31 -05:00
|
|
|
}
|
2005-01-13 14:57:24 -05:00
|
|
|
file.Write(_T(" }\n"));
|
|
|
|
|
2006-05-06 18:53:06 -04:00
|
|
|
file.Write( _T("public:\n"));
|
|
|
|
|
|
|
|
if ( m_ancestorClassNames.size() == 1 )
|
|
|
|
{
|
|
|
|
file.Write
|
|
|
|
(
|
|
|
|
m_className +
|
|
|
|
_T("(") +
|
|
|
|
*m_ancestorClassNames.begin() +
|
|
|
|
_T(" *parent=NULL){\n") +
|
|
|
|
_T(" InitWidgetsFromXRC((wxWindow *)parent);\n")
|
|
|
|
_T(" }\n")
|
|
|
|
_T("};\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file.Write(m_className + _T("(){\n") +
|
|
|
|
_T(" InitWidgetsFromXRC(NULL);\n")
|
|
|
|
_T(" }\n")
|
|
|
|
_T("};\n"));
|
|
|
|
|
|
|
|
for ( StringSet::const_iterator it = m_ancestorClassNames.begin();
|
|
|
|
it != m_ancestorClassNames.end();
|
|
|
|
++it )
|
|
|
|
{
|
|
|
|
file.Write(m_className + _T("(") + *it + _T(" *parent){\n") +
|
|
|
|
_T(" InitWidgetsFromXRC((wxWindow *)parent);\n")
|
|
|
|
_T(" }\n")
|
|
|
|
_T("};\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-05 16:58:29 -05:00
|
|
|
};
|
|
|
|
WX_DECLARE_OBJARRAY(XRCWndClassData,ArrayOfXRCWndClassData);
|
2005-09-25 15:59:19 -04:00
|
|
|
WX_DEFINE_OBJARRAY(ArrayOfXRCWndClassData)
|
2004-02-05 16:58:29 -05:00
|
|
|
|
|
|
|
|
2003-07-20 18:15:32 -04:00
|
|
|
class XmlResApp : public wxAppConsole
|
2000-07-25 14:47:21 -04:00
|
|
|
{
|
|
|
|
public:
|
2003-08-09 16:09:02 -04:00
|
|
|
// don't use builtin cmd line parsing:
|
2004-06-11 09:14:23 -04:00
|
|
|
virtual bool OnInit() { return true; }
|
2000-07-25 14:47:21 -04:00
|
|
|
virtual int OnRun();
|
2004-06-11 09:14:23 -04:00
|
|
|
|
|
|
|
private:
|
2000-07-25 14:47:21 -04:00
|
|
|
void ParseParams(const wxCmdLineParser& cmdline);
|
|
|
|
void CompileRes();
|
|
|
|
wxArrayString PrepareTempFiles();
|
2000-08-11 15:07:43 -04:00
|
|
|
void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
|
|
|
|
|
2002-06-22 19:33:12 -04:00
|
|
|
wxString GetInternalFileName(const wxString& name, const wxArrayString& flist);
|
2000-07-25 14:47:21 -04:00
|
|
|
void DeleteTempFiles(const wxArrayString& flist);
|
|
|
|
void MakePackageZIP(const wxArrayString& flist);
|
|
|
|
void MakePackageCPP(const wxArrayString& flist);
|
2002-08-14 07:02:39 -04:00
|
|
|
void MakePackagePython(const wxArrayString& flist);
|
2000-11-24 12:20:20 -05:00
|
|
|
|
|
|
|
void OutputGettext();
|
|
|
|
wxArrayString FindStrings();
|
|
|
|
wxArrayString FindStrings(wxXmlNode *node);
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-08-14 07:02:39 -04:00
|
|
|
bool flagVerbose, flagCPP, flagPython, flagGettext;
|
2000-07-25 14:47:21 -04:00
|
|
|
wxString parOutput, parFuncname, parOutputPath;
|
|
|
|
wxArrayString parFiles;
|
|
|
|
int retCode;
|
2004-02-05 16:58:29 -05:00
|
|
|
|
|
|
|
ArrayOfXRCWndClassData aXRCWndClassData;
|
2004-03-18 12:23:31 -05:00
|
|
|
bool flagH;
|
|
|
|
void GenCPPHeader();
|
2000-07-25 14:47:21 -04:00
|
|
|
};
|
|
|
|
|
2003-08-25 16:48:47 -04:00
|
|
|
IMPLEMENT_APP_CONSOLE(XmlResApp)
|
2000-07-25 14:47:21 -04:00
|
|
|
|
|
|
|
int XmlResApp::OnRun()
|
|
|
|
{
|
|
|
|
static const wxCmdLineEntryDesc cmdLineDesc[] =
|
|
|
|
{
|
2004-06-11 09:14:23 -04:00
|
|
|
{ wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show help message"),
|
2002-07-02 18:06:00 -04:00
|
|
|
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
|
2005-02-01 15:34:39 -05:00
|
|
|
{ wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose"), (wxCmdLineParamType)0, 0 },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("e"), _T("extra-cpp-code"), _T("output C++ header file with XRC derived classes"), (wxCmdLineParamType)0, 0 },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("c"), _T("cpp-code"), _T("output C++ source rather than .rsc file"), (wxCmdLineParamType)0, 0 },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("p"), _T("python-code"), _T("output wxPython source rather than .rsc file"), (wxCmdLineParamType)0, 0 },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("g"), _T("gettext"), _T("output list of translatable strings (to stdout or file if -o used)"), (wxCmdLineParamType)0, 0 },
|
|
|
|
{ wxCMD_LINE_OPTION, _T("n"), _T("function"), _T("C++/Python function name (with -c or -p) [InitXmlResource]"), (wxCmdLineParamType)0, 0 },
|
|
|
|
{ wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file [resource.xrs/cpp]"), (wxCmdLineParamType)0, 0 },
|
2002-07-02 18:06:00 -04:00
|
|
|
#if 0 // not yet implemented
|
2005-05-31 05:29:22 -04:00
|
|
|
{ wxCMD_LINE_OPTION, _T("l"), _T("list-of-handlers"), _T("output list of necessary handlers to this file"), (wxCmdLineParamType)0, 0 },
|
2002-07-02 18:06:00 -04:00
|
|
|
#endif
|
2002-12-04 09:11:26 -05:00
|
|
|
{ wxCMD_LINE_PARAM, NULL, NULL, _T("input file(s)"),
|
2004-06-11 09:14:23 -04:00
|
|
|
wxCMD_LINE_VAL_STRING,
|
2002-07-02 18:06:00 -04:00
|
|
|
wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY },
|
2000-07-25 14:47:21 -04:00
|
|
|
|
2005-02-01 15:34:39 -05:00
|
|
|
{ wxCMD_LINE_NONE, NULL, NULL, NULL, (wxCmdLineParamType)0, 0 }
|
2000-07-25 14:47:21 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
wxCmdLineParser parser(cmdLineDesc, argc, argv);
|
|
|
|
|
|
|
|
switch (parser.Parse())
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
retCode = 0;
|
|
|
|
ParseParams(parser);
|
2000-11-24 12:20:20 -05:00
|
|
|
if (flagGettext)
|
|
|
|
OutputGettext();
|
|
|
|
else
|
|
|
|
CompileRes();
|
2000-07-25 14:47:21 -04:00
|
|
|
return retCode;
|
|
|
|
}
|
2003-09-11 05:30:59 -04:00
|
|
|
return 1;
|
2000-07-25 14:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
|
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
flagGettext = cmdline.Found(_T("g"));
|
|
|
|
flagVerbose = cmdline.Found(_T("v"));
|
|
|
|
flagCPP = cmdline.Found(_T("c"));
|
|
|
|
flagPython = cmdline.Found(_T("p"));
|
2004-02-05 16:58:29 -05:00
|
|
|
flagH = flagCPP && cmdline.Found(_T("e"));
|
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
if (!cmdline.Found(_T("o"), &parOutput))
|
2000-11-24 12:20:20 -05:00
|
|
|
{
|
|
|
|
if (flagGettext)
|
|
|
|
parOutput = wxEmptyString;
|
|
|
|
else
|
2002-08-14 07:02:39 -04:00
|
|
|
{
|
|
|
|
if (flagCPP)
|
2002-12-04 09:11:26 -05:00
|
|
|
parOutput = _T("resource.cpp");
|
2002-08-14 07:02:39 -04:00
|
|
|
else if (flagPython)
|
2002-12-04 09:11:26 -05:00
|
|
|
parOutput = _T("resource.py");
|
2002-08-14 07:02:39 -04:00
|
|
|
else
|
2002-12-04 09:11:26 -05:00
|
|
|
parOutput = _T("resource.xrs");
|
2002-08-14 07:02:39 -04:00
|
|
|
}
|
2000-11-24 12:20:20 -05:00
|
|
|
}
|
2004-02-05 16:58:29 -05:00
|
|
|
if (!parOutput.empty())
|
|
|
|
{
|
|
|
|
wxFileName fn(parOutput);
|
|
|
|
fn.Normalize();
|
|
|
|
parOutput = fn.GetFullPath();
|
|
|
|
parOutputPath = wxPathOnly(parOutput);
|
|
|
|
}
|
2002-12-04 09:11:26 -05:00
|
|
|
if (!parOutputPath) parOutputPath = _T(".");
|
2000-07-25 14:47:21 -04:00
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
if (!cmdline.Found(_T("n"), &parFuncname))
|
2002-12-04 09:11:26 -05:00
|
|
|
parFuncname = _T("InitXmlResource");
|
2000-07-25 14:47:21 -04:00
|
|
|
|
|
|
|
for (size_t i = 0; i < cmdline.GetParamCount(); i++)
|
2003-07-21 18:23:09 -04:00
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
wxString fn=wxFindFirstFile(cmdline.GetParam(i), wxFILE);
|
2005-01-13 14:57:24 -05:00
|
|
|
while (!fn.empty())
|
2003-07-21 18:23:09 -04:00
|
|
|
{
|
|
|
|
parFiles.Add(fn);
|
|
|
|
fn=wxFindNextFile();
|
|
|
|
}
|
|
|
|
#else
|
2000-07-25 14:47:21 -04:00
|
|
|
parFiles.Add(cmdline.GetParam(i));
|
2003-07-21 18:23:09 -04:00
|
|
|
#endif
|
|
|
|
}
|
2000-07-25 14:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::CompileRes()
|
|
|
|
{
|
|
|
|
wxArrayString files = PrepareTempFiles();
|
|
|
|
|
|
|
|
wxRemoveFile(parOutput);
|
|
|
|
|
|
|
|
if (!retCode)
|
2004-06-11 09:14:23 -04:00
|
|
|
{
|
2004-02-05 16:58:29 -05:00
|
|
|
if (flagCPP){
|
2000-07-25 14:47:21 -04:00
|
|
|
MakePackageCPP(files);
|
2004-02-05 16:58:29 -05:00
|
|
|
if (flagH)
|
|
|
|
GenCPPHeader();
|
|
|
|
}
|
2002-08-14 07:02:39 -04:00
|
|
|
else if (flagPython)
|
|
|
|
MakePackagePython(files);
|
2000-07-25 14:47:21 -04:00
|
|
|
else
|
|
|
|
MakePackageZIP(files);
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
DeleteTempFiles(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-22 19:33:12 -04:00
|
|
|
wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
|
|
|
|
{
|
|
|
|
wxString name2 = name;
|
2002-12-04 09:11:26 -05:00
|
|
|
name2.Replace(_T(":"), _T("_"));
|
|
|
|
name2.Replace(_T("/"), _T("_"));
|
|
|
|
name2.Replace(_T("\\"), _T("_"));
|
|
|
|
name2.Replace(_T("*"), _T("_"));
|
|
|
|
name2.Replace(_T("?"), _T("_"));
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
wxString s = wxFileNameFromPath(parOutput) + _T("$") + name2;
|
2002-06-22 19:33:12 -04:00
|
|
|
|
|
|
|
if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
|
2004-06-11 09:14:23 -04:00
|
|
|
{
|
2002-06-22 19:33:12 -04:00
|
|
|
for (int i = 0;; i++)
|
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
s.Printf(wxFileNameFromPath(parOutput) + _T("$%03i-") + name2, i);
|
2002-06-22 19:33:12 -04:00
|
|
|
if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2000-07-25 14:47:21 -04:00
|
|
|
|
|
|
|
wxArrayString XmlResApp::PrepareTempFiles()
|
|
|
|
{
|
|
|
|
wxArrayString flist;
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (size_t i = 0; i < parFiles.GetCount(); i++)
|
2000-07-25 14:47:21 -04:00
|
|
|
{
|
2004-06-11 09:14:23 -04:00
|
|
|
if (flagVerbose)
|
2002-12-04 09:11:26 -05:00
|
|
|
wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
|
2000-07-25 14:47:21 -04:00
|
|
|
|
|
|
|
wxXmlDocument doc;
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
if (!doc.Load(parFiles[i]))
|
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
wxLogError(_T("Error parsing file ") + parFiles[i]);
|
2000-07-25 14:47:21 -04:00
|
|
|
retCode = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-08-11 15:07:43 -04:00
|
|
|
wxString name, ext, path;
|
|
|
|
wxSplitPath(parFiles[i], &path, &name, &ext);
|
|
|
|
|
|
|
|
FindFilesInXML(doc.GetRoot(), flist, path);
|
2004-02-05 16:58:29 -05:00
|
|
|
if (flagH)
|
|
|
|
{
|
|
|
|
wxXmlNode* node = (doc.GetRoot())->GetChildren();
|
2004-03-18 12:23:31 -05:00
|
|
|
wxString classValue,nameValue;
|
|
|
|
while(node){
|
2004-02-05 16:58:29 -05:00
|
|
|
if(node->GetName() == _T("object")
|
2007-07-14 06:07:41 -04:00
|
|
|
&& node->GetAttribute(_T("class"),&classValue)
|
|
|
|
&& node->GetAttribute(_T("name"),&nameValue)){
|
2004-02-05 16:58:29 -05:00
|
|
|
|
|
|
|
aXRCWndClassData.Add(
|
2004-03-18 12:23:31 -05:00
|
|
|
XRCWndClassData(nameValue,classValue,node)
|
2004-02-05 16:58:29 -05:00
|
|
|
);
|
|
|
|
}
|
2004-03-18 12:23:31 -05:00
|
|
|
node = node -> GetNext();
|
2004-02-05 16:58:29 -05:00
|
|
|
}
|
|
|
|
}
|
2002-06-22 19:33:12 -04:00
|
|
|
wxString internalName = GetInternalFileName(parFiles[i], flist);
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-12-25 10:23:44 -05:00
|
|
|
doc.Save(parOutputPath + wxFILE_SEP_PATH + internalName);
|
2002-06-22 19:33:12 -04:00
|
|
|
flist.Add(internalName);
|
2000-07-25 14:47:21 -04:00
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
return flist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-25 10:23:44 -05:00
|
|
|
// Does 'node' contain filename information at all?
|
|
|
|
static bool NodeContainsFilename(wxXmlNode *node)
|
|
|
|
{
|
2006-03-21 10:12:37 -05:00
|
|
|
const wxString name = node->GetName();
|
|
|
|
|
|
|
|
// Any bitmaps (bitmap2 is used for disabled toolbar buttons):
|
|
|
|
if ( name == _T("bitmap") || name == _T("bitmap2") )
|
2004-06-11 09:14:23 -04:00
|
|
|
return true;
|
2005-01-13 14:57:24 -05:00
|
|
|
|
2006-03-21 10:12:37 -05:00
|
|
|
if ( name == _T("icon") )
|
2004-10-31 06:42:27 -05:00
|
|
|
return true;
|
2002-12-25 10:23:44 -05:00
|
|
|
|
|
|
|
// wxBitmapButton:
|
|
|
|
wxXmlNode *parent = node->GetParent();
|
2004-06-11 09:14:23 -04:00
|
|
|
if (parent != NULL &&
|
2007-07-14 06:07:41 -04:00
|
|
|
parent->GetAttribute(_T("class"), _T("")) == _T("wxBitmapButton") &&
|
2006-03-21 10:12:37 -05:00
|
|
|
(name == _T("focus") ||
|
|
|
|
name == _T("disabled") ||
|
|
|
|
name == _T("selected")))
|
2004-06-11 09:14:23 -04:00
|
|
|
return true;
|
|
|
|
|
2002-12-25 10:23:44 -05:00
|
|
|
// wxBitmap or wxIcon toplevel resources:
|
2006-03-21 10:12:37 -05:00
|
|
|
if ( name == _T("object") )
|
2002-12-25 10:23:44 -05:00
|
|
|
{
|
2007-07-14 06:07:41 -04:00
|
|
|
wxString klass = node->GetAttribute(_T("class"), wxEmptyString);
|
2007-04-10 20:19:20 -04:00
|
|
|
if (klass == _T("wxBitmap") ||
|
|
|
|
klass == _T("wxIcon") ||
|
|
|
|
klass == _T("data") )
|
2004-06-11 09:14:23 -04:00
|
|
|
return true;
|
2002-12-25 10:23:44 -05:00
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2007-06-25 08:16:20 -04:00
|
|
|
// URLs in wxHtmlWindow:
|
|
|
|
if ( name == _T("url") &&
|
|
|
|
parent != NULL &&
|
2007-07-14 06:07:41 -04:00
|
|
|
parent->GetAttribute(_T("class"), _T("")) == _T("wxHtmlWindow") )
|
2007-06-25 08:16:20 -04:00
|
|
|
{
|
|
|
|
// FIXME: this is wrong for e.g. http:// URLs
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
return false;
|
2002-12-25 10:23:44 -05:00
|
|
|
}
|
2000-07-25 14:47:21 -04:00
|
|
|
|
2000-08-11 15:07:43 -04:00
|
|
|
// find all files mentioned in structure, e.g. <bitmap>filename</bitmap>
|
|
|
|
void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath)
|
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
// Is 'node' XML node element?
|
|
|
|
if (node == NULL) return;
|
|
|
|
if (node->GetType() != wxXML_ELEMENT_NODE) return;
|
|
|
|
|
2002-12-25 10:23:44 -05:00
|
|
|
bool containsFilename = NodeContainsFilename(node);
|
2002-12-04 09:11:26 -05:00
|
|
|
|
|
|
|
wxXmlNode *n = node->GetChildren();
|
2000-08-11 15:07:43 -04:00
|
|
|
while (n)
|
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
if (containsFilename &&
|
2004-06-11 09:14:23 -04:00
|
|
|
(n->GetType() == wxXML_TEXT_NODE ||
|
2002-12-04 09:11:26 -05:00
|
|
|
n->GetType() == wxXML_CDATA_SECTION_NODE))
|
2000-08-11 15:07:43 -04:00
|
|
|
{
|
|
|
|
wxString fullname;
|
2002-12-04 09:11:26 -05:00
|
|
|
if (wxIsAbsolutePath(n->GetContent()) || inputPath.empty())
|
|
|
|
fullname = n->GetContent();
|
|
|
|
else
|
2002-12-25 10:23:44 -05:00
|
|
|
fullname = inputPath + wxFILE_SEP_PATH + n->GetContent();
|
2002-06-22 19:33:12 -04:00
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
if (flagVerbose)
|
2002-12-04 09:11:26 -05:00
|
|
|
wxPrintf(_T("adding ") + fullname + _T("...\n"));
|
|
|
|
|
2002-06-22 19:33:12 -04:00
|
|
|
wxString filename = GetInternalFileName(n->GetContent(), flist);
|
2000-08-11 15:07:43 -04:00
|
|
|
n->SetContent(filename);
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
if (flist.Index(filename) == wxNOT_FOUND)
|
|
|
|
flist.Add(filename);
|
2000-08-11 15:07:43 -04:00
|
|
|
|
|
|
|
wxFileInputStream sin(fullname);
|
2002-12-25 10:23:44 -05:00
|
|
|
wxFileOutputStream sout(parOutputPath + wxFILE_SEP_PATH + filename);
|
2000-08-11 15:07:43 -04:00
|
|
|
sin.Read(sout); // copy the stream
|
|
|
|
}
|
2002-12-04 09:11:26 -05:00
|
|
|
|
2000-08-11 15:07:43 -04:00
|
|
|
// subnodes:
|
|
|
|
if (n->GetType() == wxXML_ELEMENT_NODE)
|
|
|
|
FindFilesInXML(n, flist, inputPath);
|
2002-12-04 09:11:26 -05:00
|
|
|
|
2000-08-11 15:07:43 -04:00
|
|
|
n = n->GetNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
void XmlResApp::DeleteTempFiles(const wxArrayString& flist)
|
|
|
|
{
|
2007-04-10 20:23:19 -04:00
|
|
|
for (size_t i = 0; i < flist.GetCount(); i++)
|
2002-12-25 10:23:44 -05:00
|
|
|
wxRemoveFile(parOutputPath + wxFILE_SEP_PATH + flist[i]);
|
2000-07-25 14:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::MakePackageZIP(const wxArrayString& flist)
|
|
|
|
{
|
|
|
|
wxString files;
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (size_t i = 0; i < flist.GetCount(); i++)
|
2002-12-04 09:11:26 -05:00
|
|
|
files += flist[i] + _T(" ");
|
2000-07-25 14:47:21 -04:00
|
|
|
files.RemoveLast();
|
2004-06-11 09:14:23 -04:00
|
|
|
|
|
|
|
if (flagVerbose)
|
2002-12-04 09:11:26 -05:00
|
|
|
wxPrintf(_T("compressing ") + parOutput + _T("...\n"));
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-12-25 10:23:44 -05:00
|
|
|
wxString cwd = wxGetCwd();
|
|
|
|
wxSetWorkingDirectory(parOutputPath);
|
2004-06-11 09:14:23 -04:00
|
|
|
int execres = wxExecute(_T("zip -9 -j ") +
|
2005-01-31 17:17:18 -05:00
|
|
|
wxString(flagVerbose ? _T("\"") : _T("-q \"")) +
|
|
|
|
parOutput + _T("\" ") + files, true);
|
2002-12-25 10:23:44 -05:00
|
|
|
wxSetWorkingDirectory(cwd);
|
|
|
|
if (execres == -1)
|
2000-07-25 14:47:21 -04:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
wxLogError(_T("Unable to execute zip program. Make sure it is in the path."));
|
|
|
|
wxLogError(_T("You can download it at http://www.cdrom.com/pub/infozip/"));
|
2000-07-25 14:47:21 -04:00
|
|
|
retCode = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static wxString FileToCppArray(wxString filename, int num)
|
|
|
|
{
|
|
|
|
wxString output;
|
|
|
|
wxString tmp;
|
2000-08-11 15:07:43 -04:00
|
|
|
wxString snum;
|
2003-03-16 15:27:15 -05:00
|
|
|
wxFFile file(filename, wxT("rb"));
|
2005-01-13 14:57:24 -05:00
|
|
|
wxFileOffset offset = file.Length();
|
|
|
|
wxASSERT_MSG( offset >= 0 , wxT("Invalid file length") );
|
2005-09-25 15:59:19 -04:00
|
|
|
|
|
|
|
const size_t lng = wx_truncate_cast(size_t, offset);
|
|
|
|
wxASSERT_MSG( lng == offset, wxT("Huge file not supported") );
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
snum.Printf(_T("%i"), num);
|
|
|
|
output.Printf(_T("static size_t xml_res_size_") + snum + _T(" = %i;\n"), lng);
|
|
|
|
output += _T("static unsigned char xml_res_file_") + snum + _T("[] = {\n");
|
2000-10-07 17:56:37 -04:00
|
|
|
// we cannot use string literals because MSVC is dumb wannabe compiler
|
|
|
|
// with arbitrary limitation to 2048 strings :(
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
unsigned char *buffer = new unsigned char[lng];
|
|
|
|
file.Read(buffer, lng);
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-08-11 15:07:43 -04:00
|
|
|
for (size_t i = 0, linelng = 0; i < lng; i++)
|
2000-07-25 14:47:21 -04:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
tmp.Printf(_T("%i"), buffer[i]);
|
|
|
|
if (i != 0) output << _T(',');
|
2000-10-07 17:56:37 -04:00
|
|
|
if (linelng > 70)
|
2000-08-11 15:07:43 -04:00
|
|
|
{
|
|
|
|
linelng = 0;
|
2002-12-04 09:11:26 -05:00
|
|
|
output << _T("\n");
|
2000-08-11 15:07:43 -04:00
|
|
|
}
|
2000-10-07 17:56:37 -04:00
|
|
|
output << tmp;
|
|
|
|
linelng += tmp.Length()+1;
|
2000-07-25 14:47:21 -04:00
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
delete[] buffer;
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
output += _T("};\n\n");
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::MakePackageCPP(const wxArrayString& flist)
|
|
|
|
{
|
2003-03-16 15:27:15 -05:00
|
|
|
wxFFile file(parOutput, wxT("wt"));
|
2000-07-25 14:47:21 -04:00
|
|
|
size_t i;
|
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
if (flagVerbose)
|
2002-12-04 09:11:26 -05:00
|
|
|
wxPrintf(_T("creating C++ source file ") + parOutput + _T("...\n"));
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2007-06-18 16:22:08 -04:00
|
|
|
file.Write(""
|
|
|
|
"//\n"
|
|
|
|
"// This file was automatically generated by wxrc, do not edit by hand.\n"
|
|
|
|
"//\n\n"
|
|
|
|
"#include <wx/wxprec.h>\n"
|
|
|
|
"\n"
|
|
|
|
"#ifdef __BORLANDC__\n"
|
|
|
|
" #pragma hdrstop\n"
|
|
|
|
"#endif\n"
|
|
|
|
"\n"
|
|
|
|
""
|
|
|
|
"#include <wx/filesys.h>\n"
|
|
|
|
"#include <wx/fs_mem.h>\n"
|
|
|
|
"#include <wx/xrc/xmlres.h>\n"
|
|
|
|
"#include <wx/xrc/xh_all.h>\n"
|
|
|
|
"\n"
|
|
|
|
"#if wxCHECK_VERSION(2,8,5) && wxABI_VERSION >= 20805\n"
|
|
|
|
" #define XRC_ADD_FILE(name, data, size, mime) \\\n"
|
|
|
|
" wxMemoryFSHandler::AddFileWithMimeType(name, data, size, mime)\n"
|
|
|
|
"#else\n"
|
|
|
|
" #define XRC_ADD_FILE(name, data, size, mime) \\\n"
|
|
|
|
" wxMemoryFSHandler::AddFile(name, data, size)\n"
|
|
|
|
"#endif\n"
|
|
|
|
"\n");
|
2000-07-25 14:47:21 -04:00
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (i = 0; i < flist.GetCount(); i++)
|
2002-12-25 10:23:44 -05:00
|
|
|
file.Write(
|
|
|
|
FileToCppArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2007-06-18 16:22:08 -04:00
|
|
|
file.Write(""
|
2007-06-18 16:30:20 -04:00
|
|
|
"void " + parFuncname + "()\n"
|
2007-06-18 16:22:08 -04:00
|
|
|
"{\n"
|
|
|
|
"\n"
|
|
|
|
" // Check for memory FS. If not present, load the handler:\n"
|
|
|
|
" {\n"
|
|
|
|
" wxMemoryFSHandler::AddFile(wxT(\"XRC_resource/dummy_file\"), wxT(\"dummy one\"));\n"
|
|
|
|
" wxFileSystem fsys;\n"
|
|
|
|
" wxFSFile *f = fsys.OpenFile(wxT(\"memory:XRC_resource/dummy_file\"));\n"
|
|
|
|
" wxMemoryFSHandler::RemoveFile(wxT(\"XRC_resource/dummy_file\"));\n"
|
|
|
|
" if (f) delete f;\n"
|
|
|
|
" else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n"
|
|
|
|
" }\n"
|
|
|
|
"\n");
|
2000-07-25 14:47:21 -04:00
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (i = 0; i < flist.GetCount(); i++)
|
2000-07-25 14:47:21 -04:00
|
|
|
{
|
|
|
|
wxString s;
|
2007-06-18 14:43:34 -04:00
|
|
|
|
|
|
|
wxString mime;
|
|
|
|
wxString ext = wxFileName(flist[i]).GetExt();
|
|
|
|
if ( ext.Lower() == _T("xrc") )
|
|
|
|
mime = _T("text/xml");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
|
|
|
|
if ( ft )
|
|
|
|
ft->GetMimeType(&mime);
|
|
|
|
}
|
|
|
|
|
2007-06-18 16:22:08 -04:00
|
|
|
s.Printf(" XRC_ADD_FILE(wxT(\"XRC_resource/" + flist[i] +
|
|
|
|
"\"), xml_res_file_%i, xml_res_size_%i, _T(\"%s\"));\n",
|
2007-06-18 14:43:34 -04:00
|
|
|
i, i, mime.c_str());
|
2000-07-25 14:47:21 -04:00
|
|
|
file.Write(s);
|
|
|
|
}
|
2000-08-11 15:07:43 -04:00
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (i = 0; i < parFiles.GetCount(); i++)
|
2000-08-11 15:07:43 -04:00
|
|
|
{
|
2007-06-18 16:22:08 -04:00
|
|
|
file.Write(" wxXmlResource::Get()->Load(wxT(\"memory:XRC_resource/" +
|
|
|
|
GetInternalFileName(parFiles[i], flist) + "\"));\n");
|
2000-08-11 15:07:43 -04:00
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2007-06-18 16:22:08 -04:00
|
|
|
file.Write("}\n");
|
2000-07-25 14:47:21 -04:00
|
|
|
|
2000-08-11 15:07:43 -04:00
|
|
|
|
2000-07-25 14:47:21 -04:00
|
|
|
}
|
2000-11-24 12:20:20 -05:00
|
|
|
|
2004-02-05 16:58:29 -05:00
|
|
|
void XmlResApp::GenCPPHeader()
|
|
|
|
{
|
2004-08-23 08:55:16 -04:00
|
|
|
wxString fileSpec = ((parOutput.BeforeLast('.')).AfterLast('/')).AfterLast('\\');
|
2004-02-05 16:58:29 -05:00
|
|
|
wxString heaFileName = fileSpec + _T(".h");
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2004-02-05 16:58:29 -05:00
|
|
|
wxFFile file(heaFileName, wxT("wt"));
|
|
|
|
file.Write(
|
2007-06-18 16:22:08 -04:00
|
|
|
"//\n"
|
|
|
|
"// This file was automatically generated by wxrc, do not edit by hand.\n"
|
|
|
|
"//\n\n"
|
2007-06-18 16:30:20 -04:00
|
|
|
"#ifndef __" + fileSpec + "_h__\n"
|
|
|
|
"#define __" + fileSpec + "_h__\n"
|
2004-06-11 09:14:23 -04:00
|
|
|
);
|
2007-04-10 20:23:19 -04:00
|
|
|
for(size_t i=0;i<aXRCWndClassData.GetCount();++i){
|
2004-06-11 09:14:23 -04:00
|
|
|
aXRCWndClassData.Item(i).GenerateHeaderCode(file);
|
|
|
|
}
|
2004-02-05 16:58:29 -05:00
|
|
|
file.Write(
|
2007-06-18 16:22:08 -04:00
|
|
|
"\nvoid \n"
|
2004-03-18 12:23:31 -05:00
|
|
|
+ parFuncname
|
2007-06-18 16:22:08 -04:00
|
|
|
+ "();\n#endif\n");
|
2004-02-05 16:58:29 -05:00
|
|
|
}
|
|
|
|
|
2002-08-14 07:02:39 -04:00
|
|
|
static wxString FileToPythonArray(wxString filename, int num)
|
|
|
|
{
|
|
|
|
wxString output;
|
|
|
|
wxString tmp;
|
|
|
|
wxString snum;
|
2003-03-16 15:27:15 -05:00
|
|
|
wxFFile file(filename, wxT("rb"));
|
2005-01-13 14:57:24 -05:00
|
|
|
wxFileOffset offset = file.Length();
|
|
|
|
wxASSERT_MSG( offset >= 0 , wxT("Invalid file length") );
|
2005-09-25 15:59:19 -04:00
|
|
|
|
|
|
|
const size_t lng = wx_truncate_cast(size_t, offset);
|
|
|
|
wxASSERT_MSG( offset == lng, wxT("Huge file not supported") );
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
snum.Printf(_T("%i"), num);
|
2007-06-18 16:30:20 -04:00
|
|
|
output = " xml_res_file_" + snum + " = '''\\\n";
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-08-14 07:02:39 -04:00
|
|
|
unsigned char *buffer = new unsigned char[lng];
|
|
|
|
file.Read(buffer, lng);
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-08-14 07:02:39 -04:00
|
|
|
for (size_t i = 0, linelng = 0; i < lng; i++)
|
|
|
|
{
|
|
|
|
unsigned char c = buffer[i];
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
tmp = (wxChar)c;
|
|
|
|
linelng = 0;
|
|
|
|
}
|
2004-09-28 20:20:27 -04:00
|
|
|
else if (c < 32 || c > 127 || c == '\'')
|
2002-12-04 09:11:26 -05:00
|
|
|
tmp.Printf(_T("\\x%02x"), c);
|
2002-08-14 07:02:39 -04:00
|
|
|
else if (c == '\\')
|
2002-12-04 09:11:26 -05:00
|
|
|
tmp = _T("\\\\");
|
2002-08-14 07:02:39 -04:00
|
|
|
else
|
|
|
|
tmp = (wxChar)c;
|
|
|
|
if (linelng > 70)
|
|
|
|
{
|
|
|
|
linelng = 0;
|
2002-12-04 09:11:26 -05:00
|
|
|
output << _T("\\\n");
|
2002-08-14 07:02:39 -04:00
|
|
|
}
|
|
|
|
output << tmp;
|
|
|
|
linelng += tmp.Length();
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-08-14 07:02:39 -04:00
|
|
|
delete[] buffer;
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2004-09-28 20:20:27 -04:00
|
|
|
output += _T("'''\n\n");
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-08-14 07:02:39 -04:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::MakePackagePython(const wxArrayString& flist)
|
|
|
|
{
|
2003-03-16 15:27:15 -05:00
|
|
|
wxFFile file(parOutput, wxT("wt"));
|
2002-08-14 07:02:39 -04:00
|
|
|
size_t i;
|
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
if (flagVerbose)
|
2002-12-04 09:11:26 -05:00
|
|
|
wxPrintf(_T("creating Python source file ") + parOutput + _T("...\n"));
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2002-08-14 07:02:39 -04:00
|
|
|
file.Write(
|
2007-06-18 16:22:08 -04:00
|
|
|
"#\n"
|
|
|
|
"# This file was automatically generated by wxrc, do not edit by hand.\n"
|
|
|
|
"#\n\n"
|
|
|
|
"import wx\n"
|
|
|
|
"import wx.xrc\n\n"
|
2002-08-14 07:02:39 -04:00
|
|
|
);
|
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2007-06-18 16:30:20 -04:00
|
|
|
file.Write("def " + parFuncname + "():\n");
|
2002-08-14 07:02:39 -04:00
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (i = 0; i < flist.GetCount(); i++)
|
2002-12-25 10:23:44 -05:00
|
|
|
file.Write(
|
|
|
|
FileToPythonArray(parOutputPath + wxFILE_SEP_PATH + flist[i], i));
|
2002-08-14 07:02:39 -04:00
|
|
|
|
2005-01-13 14:57:24 -05:00
|
|
|
file.Write(
|
2007-06-18 16:22:08 -04:00
|
|
|
" # check if the memory filesystem handler has been loaded yet, and load it if not\n"
|
|
|
|
" wx.MemoryFSHandler.AddFile('XRC_resource/dummy_file', 'dummy value')\n"
|
|
|
|
" fsys = wx.FileSystem()\n"
|
|
|
|
" f = fsys.OpenFile('memory:XRC_resource/dummy_file')\n"
|
|
|
|
" wx.MemoryFSHandler.RemoveFile('XRC_resource/dummy_file')\n"
|
|
|
|
" if f is not None:\n"
|
|
|
|
" f.Destroy()\n"
|
|
|
|
" else:\n"
|
|
|
|
" wx.FileSystem.AddHandler(wx.MemoryFSHandler())\n"
|
|
|
|
"\n"
|
|
|
|
" # load all the strings as memory files and load into XmlRes\n"
|
2004-09-28 20:15:21 -04:00
|
|
|
);
|
|
|
|
|
2005-01-13 14:57:24 -05:00
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (i = 0; i < flist.GetCount(); i++)
|
2002-08-14 07:02:39 -04:00
|
|
|
{
|
|
|
|
wxString s;
|
2007-06-18 16:22:08 -04:00
|
|
|
s.Printf(" wx.MemoryFSHandler.AddFile('XRC_resource/" + flist[i] +
|
|
|
|
"', xml_res_file_%i)\n", i);
|
2002-08-14 07:02:39 -04:00
|
|
|
file.Write(s);
|
|
|
|
}
|
2007-04-10 20:23:19 -04:00
|
|
|
for (i = 0; i < parFiles.GetCount(); i++)
|
2004-09-28 20:15:21 -04:00
|
|
|
{
|
2007-06-18 16:22:08 -04:00
|
|
|
file.Write(" wx.xrc.XmlResource.Get().Load('memory:XRC_resource/" +
|
|
|
|
GetInternalFileName(parFiles[i], flist) + "')\n");
|
2004-09-28 20:15:21 -04:00
|
|
|
}
|
|
|
|
|
2007-06-18 16:22:08 -04:00
|
|
|
file.Write("\n");
|
2002-08-14 07:02:39 -04:00
|
|
|
}
|
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
void XmlResApp::OutputGettext()
|
|
|
|
{
|
|
|
|
wxArrayString str = FindStrings();
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
wxFFile fout;
|
2004-02-05 16:58:29 -05:00
|
|
|
if (parOutput.empty())
|
|
|
|
fout.Attach(stdout);
|
|
|
|
else
|
|
|
|
fout.Open(parOutput, wxT("wt"));
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
for (size_t i = 0; i < str.GetCount(); i++)
|
2007-06-18 16:30:20 -04:00
|
|
|
fout.Write("_(\"" + str[i] + "\");\n");
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
if (!parOutput) fout.Detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wxArrayString XmlResApp::FindStrings()
|
|
|
|
{
|
|
|
|
wxArrayString arr, a2;
|
|
|
|
|
2007-04-10 20:23:19 -04:00
|
|
|
for (size_t i = 0; i < parFiles.GetCount(); i++)
|
2000-11-24 12:20:20 -05:00
|
|
|
{
|
2004-06-11 09:14:23 -04:00
|
|
|
if (flagVerbose)
|
2002-12-04 09:11:26 -05:00
|
|
|
wxPrintf(_T("processing ") + parFiles[i] + _T("...\n"));
|
2000-11-24 12:20:20 -05:00
|
|
|
|
2004-06-11 09:14:23 -04:00
|
|
|
wxXmlDocument doc;
|
2000-11-24 12:20:20 -05:00
|
|
|
if (!doc.Load(parFiles[i]))
|
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
wxLogError(_T("Error parsing file ") + parFiles[i]);
|
2000-11-24 12:20:20 -05:00
|
|
|
retCode = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
a2 = FindStrings(doc.GetRoot());
|
|
|
|
WX_APPEND_ARRAY(arr, a2);
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-03-09 11:35:00 -05:00
|
|
|
static wxString ConvertText(const wxString& str)
|
|
|
|
{
|
|
|
|
wxString str2;
|
|
|
|
const wxChar *dt;
|
|
|
|
|
|
|
|
for (dt = str.c_str(); *dt; dt++)
|
|
|
|
{
|
|
|
|
if (*dt == wxT('_'))
|
|
|
|
{
|
|
|
|
if ( *(++dt) == wxT('_') )
|
|
|
|
str2 << wxT('_');
|
|
|
|
else
|
|
|
|
str2 << wxT('&') << *dt;
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
else
|
2002-03-09 11:35:00 -05:00
|
|
|
{
|
|
|
|
switch (*dt)
|
|
|
|
{
|
|
|
|
case wxT('\n') : str2 << wxT("\\n"); break;
|
|
|
|
case wxT('\t') : str2 << wxT("\\t"); break;
|
|
|
|
case wxT('\r') : str2 << wxT("\\r"); break;
|
2002-12-04 09:11:26 -05:00
|
|
|
case wxT('\\') : if ((*(dt+1) != 'n') &&
|
|
|
|
(*(dt+1) != 't') &&
|
|
|
|
(*(dt+1) != 'r'))
|
|
|
|
str2 << wxT("\\\\");
|
|
|
|
else
|
2004-06-11 09:14:23 -04:00
|
|
|
str2 << wxT("\\");
|
2002-12-04 09:11:26 -05:00
|
|
|
break;
|
2002-08-23 14:52:31 -04:00
|
|
|
case wxT('"') : str2 << wxT("\\\""); break;
|
2002-03-09 11:35:00 -05:00
|
|
|
default : str2 << *dt; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
|
|
|
|
{
|
|
|
|
wxArrayString arr;
|
|
|
|
|
|
|
|
wxXmlNode *n = node;
|
|
|
|
if (n == NULL) return arr;
|
|
|
|
n = n->GetChildren();
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
if ((node->GetType() == wxXML_ELEMENT_NODE) &&
|
|
|
|
// parent is an element, i.e. has subnodes...
|
2004-06-11 09:14:23 -04:00
|
|
|
(n->GetType() == wxXML_TEXT_NODE ||
|
2000-11-24 12:20:20 -05:00
|
|
|
n->GetType() == wxXML_CDATA_SECTION_NODE) &&
|
|
|
|
// ...it is textnode...
|
|
|
|
(
|
|
|
|
node/*not n!*/->GetName() == _T("label") ||
|
|
|
|
(node/*not n!*/->GetName() == _T("value") &&
|
|
|
|
!n->GetContent().IsNumber()) ||
|
|
|
|
node/*not n!*/->GetName() == _T("help") ||
|
|
|
|
node/*not n!*/->GetName() == _T("longhelp") ||
|
|
|
|
node/*not n!*/->GetName() == _T("tooltip") ||
|
|
|
|
node/*not n!*/->GetName() == _T("htmlcode") ||
|
2002-08-31 11:37:20 -04:00
|
|
|
node/*not n!*/->GetName() == _T("title") ||
|
|
|
|
node/*not n!*/->GetName() == _T("item")
|
2000-11-24 12:20:20 -05:00
|
|
|
))
|
2002-03-09 11:35:00 -05:00
|
|
|
// ...and known to contain translatable string
|
2000-11-24 12:20:20 -05:00
|
|
|
{
|
2004-08-29 14:27:51 -04:00
|
|
|
if (!flagGettext ||
|
2007-07-14 06:07:41 -04:00
|
|
|
node->GetAttribute(_T("translate"), _T("1")) != _T("0"))
|
2004-08-29 14:27:51 -04:00
|
|
|
{
|
|
|
|
arr.Add(ConvertText(n->GetContent()));
|
|
|
|
}
|
2000-11-24 12:20:20 -05:00
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
// subnodes:
|
|
|
|
if (n->GetType() == wxXML_ELEMENT_NODE)
|
|
|
|
{
|
|
|
|
wxArrayString a2 = FindStrings(n);
|
|
|
|
WX_APPEND_ARRAY(arr, a2);
|
|
|
|
}
|
2004-06-11 09:14:23 -04:00
|
|
|
|
2000-11-24 12:20:20 -05:00
|
|
|
n = n->GetNext();
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|