]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/wxrc/wxrc.cpp
SetSelection() for generic wxSpinCtrl (forgot to commit before)
[wxWidgets.git] / utils / wxrc / wxrc.cpp
index be9b7b75b5af5e0900c533990fad60800507c6e0..65a606db6e4135b4a0aa5ecdc266f278bc9a3efe 100644 (file)
@@ -58,15 +58,17 @@ private:
     wxArrayString PrepareTempFiles();
     void FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxString& inputPath);
 
+    wxString GetInternalFileName(const wxString& name, const wxArrayString& flist);
     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;
@@ -82,26 +84,24 @@ int XmlResApp::OnRun()
 {
     static const wxCmdLineEntryDesc cmdLineDesc[] =
     {
-        { wxCMD_LINE_SWITCH, "h", "help",  "show help message" },
+        { wxCMD_LINE_SWITCH, "h", "help",  "show help message", 
+              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" },
-
+#endif
         { wxCMD_LINE_PARAM,  NULL, NULL, "input file(s)",
-            wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
+              wxCMD_LINE_VAL_STRING, 
+              wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_OPTION_MANDATORY },
 
         { wxCMD_LINE_NONE }
     };
 
-#if wxUSE_GUI
-    // VS: I need reasonable output to console from wxCmdLineParser
-    wxLog::SetTimestamp(NULL);
-    delete wxLog::SetActiveTarget(new wxLogStderr);
-#endif
-
     wxCmdLineParser parser(cmdLineDesc, argc, argv);
 
     switch (parser.Parse())
@@ -142,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 = ".";
@@ -173,6 +181,8 @@ void XmlResApp::CompileRes()
     {        
         if (flagCPP)
             MakePackageCPP(files);
+        else if (flagPython)
+            MakePackagePython(files);
         else
             MakePackageZIP(files);
     }
@@ -181,6 +191,28 @@ void XmlResApp::CompileRes()
 }
 
 
+wxString XmlResApp::GetInternalFileName(const wxString& name, const wxArrayString& flist)
+{
+    wxString name2 = name;
+    name2.Replace(":", "_");
+    name2.Replace("/", "_");
+    name2.Replace("\\", "_");
+    name2.Replace("*", "_");
+    name2.Replace("?", "_");
+    
+    wxString s = wxFileNameFromPath(parOutput) + "$" + name2;
+
+    if (wxFileExists(s) && flist.Index(s) == wxNOT_FOUND)
+    {        
+        for (int i = 0;; i++)
+        {
+            s.Printf(wxFileNameFromPath(parOutput) + "$%03i-" + name2, i);
+            if (!wxFileExists(s) || flist.Index(s) != wxNOT_FOUND)
+                break;
+        }
+    }
+    return s;
+}
 
 wxArrayString XmlResApp::PrepareTempFiles()
 {
@@ -205,8 +237,10 @@ wxArrayString XmlResApp::PrepareTempFiles()
 
         FindFilesInXML(doc.GetRoot(), flist, path);
 
-        doc.Save(parOutputPath + "/" + name + ".xrc");
-        flist.Add(name + ".xrc");
+        wxString internalName = GetInternalFileName(parFiles[i], flist);
+        
+        doc.Save(parOutputPath + "/" + internalName);
+        flist.Add(internalName);
     }
     
     return flist;
@@ -228,22 +262,19 @@ void XmlResApp::FindFilesInXML(wxXmlNode *node, wxArrayString& flist, const wxSt
             (n->GetType() == wxXML_TEXT_NODE || 
             n->GetType() == wxXML_CDATA_SECTION_NODE) &&
             // ...it is textnode...
-            (node/*not n!*/->GetName() == "bitmap"))
+            ((node/*not n!*/->GetName() == "bitmap") ||
+             (node/*not n!*/->GetName() == "url")))
             // ...and known to contain filename
         {
             wxString fullname;
-            wxString filename = n->GetContent();
-            if (wxIsAbsolutePath(n->GetContent())) fullname = n->GetContent();
+            if (wxIsAbsolutePath(n->GetContent()) || inputPath == "") fullname = n->GetContent();
             else fullname = inputPath + "/" + n->GetContent();
+
+            if (flagVerbose) 
+                wxPrintf("adding     " + fullname +  "...\n");
             
-            filename.Replace("/", "_");
-            filename.Replace("\\", "_");
-            filename.Replace("*", "_");
-            filename.Replace("?", "_");
+            wxString filename = GetInternalFileName(n->GetContent(), flist);
             n->SetContent(filename);
-            
-            if (flagVerbose) 
-                wxPrintf("adding " + filename +  "...\n");
 
             flist.Add(filename);
 
@@ -341,20 +372,23 @@ void XmlResApp::MakePackageCPP(const wxArrayString& flist)
         wxPrintf("creating C++ source file " + parOutput +  "...\n");
     
     file.Write("\
-#include \"wx/wxprec.h\"\n\
+//\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\
 #ifndef WX_PRECOMP\n\
-    #include \"wx/wx.h\"\n\
+    #include <wx/wx.h>\n\
 #endif\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\
+#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");
 
     for (i = 0; i < flist.Count(); i++)
@@ -366,10 +400,10 @@ void " + parFuncname + "()\n\
 \n\
     // Check for memory FS. If not present, load the handler:\n\
     {\n\
-        wxMemoryFSHandler::AddFile(\"xml_resource/dummy_file\", \"dummy one\");\n\
+        wxMemoryFSHandler::AddFile(\"XRC_resource/dummy_file\", \"dummy one\");\n\
         wxFileSystem fsys;\n\
-        wxFSFile *f = fsys.OpenFile(\"memory:xml_resource/dummy_file\");\n\
-        wxMemoryFSHandler::RemoveFile(\"xml_resource/dummy_file\");\n\
+        wxFSFile *f = fsys.OpenFile(\"memory:XRC_resource/dummy_file\");\n\
+        wxMemoryFSHandler::RemoveFile(\"XRC_resource/dummy_file\");\n\
         if (f) delete f;\n\
         else wxFileSystem::AddHandler(new wxMemoryFSHandler);\n\
     }\n\
@@ -378,17 +412,15 @@ void " + parFuncname + "()\n\
     for (i = 0; i < flist.Count(); i++)
     {
         wxString s;
-        s.Printf("    wxMemoryFSHandler::AddFile(\"xml_resource/" + flist[i] +
+        s.Printf("    wxMemoryFSHandler::AddFile(\"XRC_resource/" + flist[i] +
                  "\", xml_res_file_%i, xml_res_size_%i);\n", i, i);
         file.Write(s);
     }
 
     for (i = 0; i < parFiles.Count(); i++)
     {
-        wxString name, ext, path;
-        wxSplitPath(parFiles[i], &path, &name, &ext);
-        file.Write("    wxXmlResource::Get()->Load(\"memory:xml_resource/" + 
-                   name + ".xrc" + "\");\n");
+        file.Write("    wxXmlResource::Get()->Load(\"memory:XRC_resource/" + 
+                   GetInternalFileName(parFiles[i], flist) + "\");\n");
     }
     
     file.Write("}\n");
@@ -396,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()
@@ -407,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();
 }
@@ -439,6 +546,38 @@ wxArrayString XmlResApp::FindStrings()
 
 
 
+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;
+        }
+        else 
+        {
+            switch (*dt)
+            {
+                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;
+            }
+        }
+    }
+
+    return str2;
+}
+
+
 wxArrayString XmlResApp::FindStrings(wxXmlNode *node)
 {
     wxArrayString arr;
@@ -462,11 +601,12 @@ 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 filename
+            // ...and known to contain translatable string
         {
-            arr.Add(n->GetContent());
+            arr.Add(ConvertText(n->GetContent()));
         }
         
         // subnodes: