X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/df65cc5a4be1f49e8bd240b78f5978d729367cb8..739555e3218220043efa230cca8e44a91ae82d30:/utils/wxrc/wxrc.cpp diff --git a/utils/wxrc/wxrc.cpp b/utils/wxrc/wxrc.cpp index 94ea9473c0..65a606db6e 100644 --- a/utils/wxrc/wxrc.cpp +++ b/utils/wxrc/wxrc.cpp @@ -62,12 +62,13 @@ private: void DeleteTempFiles(const wxArrayString& flist); void MakePackageZIP(const wxArrayString& flist); void MakePackageCPP(const wxArrayString& flist); + void MakePackagePython(const wxArrayString& flist); void OutputGettext(); wxArrayString FindStrings(); wxArrayString FindStrings(wxXmlNode *node); - bool flagVerbose, flagCPP, flagGettext; + bool flagVerbose, flagCPP, flagPython, flagGettext; wxString parOutput, parFuncname, parOutputPath; wxArrayString parFiles; int retCode; @@ -87,8 +88,9 @@ int XmlResApp::OnRun() wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" }, { wxCMD_LINE_SWITCH, "c", "cpp-code", "output C++ source rather than .rsc file" }, + { wxCMD_LINE_SWITCH, "p", "python-code", "output wxPython source rather than .rsc file" }, { wxCMD_LINE_SWITCH, "g", "gettext", "output list of translatable strings (to stdout or file if -o used)" }, - { wxCMD_LINE_OPTION, "n", "function", "C++ function name (with -c) [InitXmlResource]" }, + { wxCMD_LINE_OPTION, "n", "function", "C++/Python function name (with -c or -p) [InitXmlResource]" }, { wxCMD_LINE_OPTION, "o", "output", "output file [resource.xrs/cpp]" }, #if 0 // not yet implemented { wxCMD_LINE_OPTION, "l", "list-of-handlers", "output list of neccessary handlers to this file" }, @@ -140,13 +142,21 @@ void XmlResApp::ParseParams(const wxCmdLineParser& cmdline) flagGettext = cmdline.Found("g"); flagVerbose = cmdline.Found("v"); flagCPP = cmdline.Found("c"); + flagPython = cmdline.Found("p"); if (!cmdline.Found("o", &parOutput)) { if (flagGettext) parOutput = wxEmptyString; else - parOutput = flagCPP ? "resource.cpp" : "resource.xrs"; + { + if (flagCPP) + parOutput = "resource.cpp"; + else if (flagPython) + parOutput = "resource.py"; + else + parOutput = "resource.xrs"; + } } parOutputPath = wxPathOnly(parOutput); if (!parOutputPath) parOutputPath = "."; @@ -171,6 +181,8 @@ void XmlResApp::CompileRes() { if (flagCPP) MakePackageCPP(files); + else if (flagPython) + MakePackagePython(files); else MakePackageZIP(files); } @@ -360,6 +372,9 @@ void XmlResApp::MakePackageCPP(const wxArrayString& flist) wxPrintf("creating C++ source file " + parOutput + "...\n"); file.Write("\ +//\n\ +// This file was automatically generated by wxrc, do not edit by hand.\n\ +//\n\n\ #include \n\ \n\ #ifdef __BORLANDC__\n\ @@ -413,6 +428,81 @@ void " + parFuncname + "()\n\ } +static wxString FileToPythonArray(wxString filename, int num) +{ + wxString output; + wxString tmp; + wxString snum; + wxFFile file(filename, "rb"); + size_t lng = file.Length(); + + snum.Printf("%i", num); + output = " xml_res_file_" + snum + " = \"\"\"\\\n"; + + unsigned char *buffer = new unsigned char[lng]; + file.Read(buffer, lng); + + for (size_t i = 0, linelng = 0; i < lng; i++) + { + unsigned char c = buffer[i]; + if (c == '\n') + { + tmp = (wxChar)c; + linelng = 0; + } + else if (c < 32 || c > 127) + tmp.Printf("\\x%02x", c); + else if (c == '\\') + tmp = "\\\\"; + else + tmp = (wxChar)c; + if (linelng > 70) + { + linelng = 0; + output << "\\\n"; + } + output << tmp; + linelng += tmp.Length(); + } + + delete[] buffer; + + output += "\"\"\"\n\n"; + + return output; +} + + +void XmlResApp::MakePackagePython(const wxArrayString& flist) +{ + wxFFile file(parOutput, "wt"); + size_t i; + + if (flagVerbose) + wxPrintf("creating Python source file " + parOutput + "...\n"); + + file.Write( + "#\n" + "# This file was automatically generated by wxrc, do not edit by hand.\n" + "#\n\n" + "from wxPython.wx import *\n" + "from wxPython.xrc import *\n\n" + ); + + + file.Write("def " + parFuncname + "():\n"); + + for (i = 0; i < flist.Count(); i++) + file.Write(FileToPythonArray(flist[i], i)); + + for (i = 0; i < flist.Count(); i++) + { + wxString s; + s.Printf(" wxXmlResource_Get().LoadFromString(xml_res_file_%i)\n", i); + file.Write(s); + } +} + void XmlResApp::OutputGettext() @@ -424,7 +514,7 @@ void XmlResApp::OutputGettext() else fout.Open(parOutput, _T("wt")); for (size_t i = 0; i < str.GetCount(); i++) - fout.Write(_T("_(\"") + str[i] + _T("\")\n")); + fout.Write(_T("_(\"") + str[i] + _T("\");\n")); if (!parOutput) fout.Detach(); } @@ -477,6 +567,8 @@ static wxString ConvertText(const wxString& str) case wxT('\n') : str2 << wxT("\\n"); break; case wxT('\t') : str2 << wxT("\\t"); break; case wxT('\r') : str2 << wxT("\\r"); break; + case wxT('\\') : str2 << wxT("\\\\"); break; + case wxT('"') : str2 << wxT("\\\""); break; default : str2 << *dt; break; } } @@ -509,7 +601,8 @@ wxArrayString XmlResApp::FindStrings(wxXmlNode *node) node/*not n!*/->GetName() == _T("longhelp") || node/*not n!*/->GetName() == _T("tooltip") || node/*not n!*/->GetName() == _T("htmlcode") || - node/*not n!*/->GetName() == _T("title") + node/*not n!*/->GetName() == _T("title") || + node/*not n!*/->GetName() == _T("item") )) // ...and known to contain translatable string {