]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/tools/pywxrc.py
fix building/running of tex2rtf
[wxWidgets.git] / wxPython / wx / tools / pywxrc.py
index a30b8fd79d0cef06d4ca2bc7fd3f2fb23f8441bd..9093b2e12591a64e78414a82f24c2b0a7b3e07e7 100644 (file)
@@ -38,6 +38,7 @@ import wx.xrc
 class PythonTemplates:
     FILE_HEADER = """\
 # This file was automatically generated by pywxrc, do not edit by hand.
 class PythonTemplates:
     FILE_HEADER = """\
 # This file was automatically generated by pywxrc, do not edit by hand.
+# -*- coding: UTF-8 -*-
 
 import wx
 import wx.xrc as xrc
 
 import wx
 import wx.xrc as xrc
@@ -55,7 +56,7 @@ def get_resources():
 
     CLASS_HEADER = """\
 class xrc%(windowName)s(wx.%(windowClass)s):
 
     CLASS_HEADER = """\
 class xrc%(windowName)s(wx.%(windowClass)s):
-    def PreCreate(self):
+    def PreCreate(self, pre):
         \"\"\" This function is called during the class's initialization.
         
         Override it for custom setup before the window is created usually to
         \"\"\" This function is called during the class's initialization.
         
         Override it for custom setup before the window is created usually to
@@ -65,8 +66,8 @@ class xrc%(windowName)s(wx.%(windowClass)s):
     def __init__(self, parent):
         # Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
         pre = wx.Pre%(windowClass)s()
     def __init__(self, parent):
         # Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
         pre = wx.Pre%(windowClass)s()
+        self.PreCreate(pre)
         get_resources().LoadOn%(windowClass)s(pre, parent, "%(windowName)s")
         get_resources().LoadOn%(windowClass)s(pre, parent, "%(windowName)s")
-        self.PreCreate()
         self.PostCreate(pre)
 
         # Define variables for the controls
         self.PostCreate(pre)
 
         # Define variables for the controls
@@ -76,6 +77,28 @@ class xrc%(windowName)s(wx.%(windowClass)s):
         self.%(widgetName)s = xrc.XRCCTRL(self, \"%(widgetName)s\")
 """
 
         self.%(widgetName)s = xrc.XRCCTRL(self, \"%(widgetName)s\")
 """
 
+    MENU_CLASS_HEADER = """\
+class xrc%(windowName)s(wx.%(windowClass)s):
+    def __init__(self):
+        pre = get_resources().LoadMenu("%(windowName)s")
+
+        # This is a copy of Robin's PostCreate voodoo magic in wx.Window that
+        # relinks the self object with the menu object.
+        self.this = pre.this
+        self.thisown = pre.thisown
+        pre.thisown = 0
+        if hasattr(self, '_setOORInfo'):
+            self._setOORInfo(self)
+        if hasattr(self, '_setCallbackInfo'):
+            self._setCallbackInfo(self, self.__class__)
+
+        # Define variables for the menu items
+"""
+
+    CREATE_MENUITEM_VAR = """\
+        self.%(widgetName)s = self.FindItemById(xrc.XRCID(\"%(widgetName)s\"))
+"""
+
     INIT_RESOURE_HEADER = """\
 # ------------------------ Resource data ----------------------
 
     INIT_RESOURE_HEADER = """\
 # ------------------------ Resource data ----------------------
 
@@ -153,14 +176,23 @@ class XmlResourceCompiler:
                 
         # now write it all out
         print >>outputFile, self.templates.FILE_HEADER
                 
         # now write it all out
         print >>outputFile, self.templates.FILE_HEADER
-        print >>outputFile, "\n".join(classes)
+
+        # Note: Technically it is not legal to have anything other
+        # than ascii for class and variable names, but since the user
+        # can create the XML with non-ascii names we'll go ahead and
+        # allow for it here, and then let Python complain about it
+        # later when they try to run the program.
+        classes = u"\n".join(classes)
+        print >>outputFile, classes.encode("UTF-8")
 
         print >>outputFile, self.templates.INIT_RESOURE_HEADER
         if embedResources:
             print >>outputFile, self.templates.PREPARE_MEMFS
 
         print >>outputFile, self.templates.INIT_RESOURE_HEADER
         if embedResources:
             print >>outputFile, self.templates.PREPARE_MEMFS
-        print >>outputFile, "\n".join(resources)
+        resources = u"\n".join(resources)
+        print >>outputFile, resources.encode("UTF-8")
 
         if generateGetText:
 
         if generateGetText:
+            # These have already been converted to utf-8...
             gettextStrings = ['    _("%s")' % s for s in gettextStrings]
             gettextStrings = "\n".join(gettextStrings)
             print >>outputFile, self.templates.GETTEXT_DUMMY_FUNC % gettextStrings
             gettextStrings = ['    _("%s")' % s for s in gettextStrings]
             gettextStrings = "\n".join(gettextStrings)
             print >>outputFile, self.templates.GETTEXT_DUMMY_FUNC % gettextStrings
@@ -194,7 +226,11 @@ class XmlResourceCompiler:
             windowClass = topWindow.getAttribute("class")
             windowClass = re.sub("^wx", "", windowClass)
             windowName = topWindow.getAttribute("name")
             windowClass = topWindow.getAttribute("class")
             windowClass = re.sub("^wx", "", windowClass)
             windowName = topWindow.getAttribute("name")
-            outputList.append(self.templates.CLASS_HEADER % locals())
+            
+            if windowClass in ["Menu", "MenuItem"]:
+                outputList.append(self.templates.MENU_CLASS_HEADER % locals())
+            else:
+                outputList.append(self.templates.CLASS_HEADER % locals())
             
             # Generate a variable for each control, and standard event handlers
             # for standard controls.
             
             # Generate a variable for each control, and standard event handlers
             # for standard controls.
@@ -202,11 +238,12 @@ class XmlResourceCompiler:
                 widgetClass = widget.getAttribute("class")
                 widgetClass = re.sub("^wx", "", widgetClass)
                 widgetName = widget.getAttribute("name")
                 widgetClass = widget.getAttribute("class")
                 widgetClass = re.sub("^wx", "", widgetClass)
                 widgetName = widget.getAttribute("name")
-                if (widgetName != "" and widgetClass != "" and 
-                    widgetClass not in 
-                        ['tool', 'unknown', 'notebookpage', 
-                         'separator', 'sizeritem', 'MenuItem']):
-                    outputList.append(self.templates.CREATE_WIDGET_VAR % locals())
+                if (widgetName != "" and widgetClass != ""):
+                    if widgetClass == "MenuItem":
+                        outputList.append(self.templates.CREATE_MENUITEM_VAR % locals())
+                    elif widgetClass not in \
+                        ['tool', 'unknown', 'notebookpage', 'separator', 'sizeritem']:
+                        outputList.append(self.templates.CREATE_WIDGET_VAR % locals())
             outputList.append('\n\n')
                     
         return "".join(outputList)
             outputList.append('\n\n')
                     
         return "".join(outputList)
@@ -224,7 +261,7 @@ class XmlResourceCompiler:
         self.ReplaceFilenamesInXRC(resourceDocument.firstChild, files, resourcePath)
         
         filename = resourceFilename
         self.ReplaceFilenamesInXRC(resourceDocument.firstChild, files, resourcePath)
         
         filename = resourceFilename
-        fileData = resourceDocument.toxml()
+        fileData = resourceDocument.toxml() # what about this? encoding=resourceDocument.encoding)
         outputList.append(self.templates.FILE_AS_STRING % locals())
 
         for f in files:
         outputList.append(self.templates.FILE_AS_STRING % locals())
 
         for f in files:
@@ -424,7 +461,7 @@ class XmlResourceCompiler:
             else:            
                 st2 += dt[i]
 
             else:            
                 st2 += dt[i]
 
-        return st2                
+        return st2.encode("UTF-8")                
 
 
     #-------------------------------------------------------------------
 
 
     #-------------------------------------------------------------------