symbian-qemu-0.9.1-12/python-2.6.1/Mac/Modules/qdoffs/qdoffssupport.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 # This script generates a Python interface for an Apple Macintosh Manager.
       
     2 # It uses the "bgen" package to generate C code.
       
     3 # The function specifications are generated by scanning the mamager's header file,
       
     4 # using the "scantools" package (customized for this particular manager).
       
     5 
       
     6 import string
       
     7 
       
     8 # Declarations that change for each manager
       
     9 MACHEADERFILE = 'QDOffscreen.h'         # The Apple header file
       
    10 MODNAME = '_Qdoffs'                             # The name of the module
       
    11 OBJECTNAME = 'GWorld'                   # The basic name of the objects used here
       
    12 
       
    13 # The following is *usually* unchanged but may still require tuning
       
    14 MODPREFIX = 'Qdoffs'                    # The prefix for module-wide routines
       
    15 OBJECTTYPE = OBJECTNAME + 'Ptr'         # The C type used to represent them
       
    16 OBJECTPREFIX = OBJECTNAME + 'Obj'       # The prefix for object methods
       
    17 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
       
    18 #EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions
       
    19 OUTPUTFILE = MODNAME + "module.c"       # The file generated by this program
       
    20 
       
    21 from macsupport import *
       
    22 
       
    23 # Create the type objects
       
    24 
       
    25 GWorldPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
       
    26 GWorldFlags = Type("GWorldFlags", "l")
       
    27 GDHandle = OpaqueByValueType("GDHandle", "ResObj")
       
    28 OptGDHandle = OpaqueByValueType("GDHandle", "OptResObj")
       
    29 CTabHandle = OpaqueByValueType("CTabHandle", "OptResObj")
       
    30 PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
       
    31 PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj")
       
    32 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
       
    33 GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
       
    34 QDErr = OSErrType("QDErr", 'h')
       
    35 
       
    36 includestuff = includestuff + """
       
    37 #include <Carbon/Carbon.h>
       
    38 
       
    39 #ifdef USE_TOOLBOX_OBJECT_GLUE
       
    40 extern PyObject *_GWorldObj_New(GWorldPtr);
       
    41 extern int _GWorldObj_Convert(PyObject *, GWorldPtr *);
       
    42 
       
    43 #define GWorldObj_New _GWorldObj_New
       
    44 #define GWorldObj_Convert _GWorldObj_Convert
       
    45 #endif
       
    46 
       
    47 #define as_GrafPtr(gworld) ((GrafPtr)(gworld))
       
    48 
       
    49 """
       
    50 
       
    51 initstuff = initstuff + """
       
    52         PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New);
       
    53         PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert);
       
    54 """
       
    55 
       
    56 class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
       
    57     # XXXX Should inherit from GrafPtr?
       
    58     def outputCheckNewArg(self):
       
    59         Output("if (itself == NULL) return PyMac_Error(resNotFound);")
       
    60 ##      def outputInitStructMembers(self):
       
    61 ##              GlobalObjectDefinition.outputInitStructMembers(self)
       
    62 ##              Output("SetWRefCon(itself, (long)it);")
       
    63 ##      def outputCheckConvertArg(self):
       
    64 ##              OutLbrace("if (DlgObj_Check(v))")
       
    65 ##              Output("*p_itself = ((WindowObject *)v)->ob_itself;")
       
    66 ##              Output("return 1;")
       
    67 ##              OutRbrace()
       
    68 ##              Out("""
       
    69 ##              if (v == Py_None) { *p_itself = NULL; return 1; }
       
    70 ##              if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
       
    71 ##              """)
       
    72     def outputFreeIt(self, itselfname):
       
    73         Output("DisposeGWorld(%s);", itselfname)
       
    74 # From here on it's basically all boiler plate...
       
    75 
       
    76 # Create the generator groups and link them
       
    77 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
       
    78 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
       
    79 module.addobject(object)
       
    80 
       
    81 
       
    82 # Create the generator classes used to populate the lists
       
    83 Function = OSErrWeakLinkFunctionGenerator
       
    84 Method = OSErrWeakLinkMethodGenerator
       
    85 
       
    86 # Create and populate the lists
       
    87 functions = []
       
    88 methods = []
       
    89 execfile(INPUTFILE)
       
    90 
       
    91 # A method to convert a GWorldPtr to a GrafPtr
       
    92 f = Method(GrafPtr, 'as_GrafPtr', (GWorldPtr, 'p', InMode))
       
    93 methods.append(f)
       
    94 
       
    95 #
       
    96 # Manual generator: get data out of a pixmap
       
    97 pixmapgetbytes_body = """
       
    98 PixMapHandle pm;
       
    99 int from, length;
       
   100 char *cp;
       
   101 
       
   102 if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) )
       
   103         return NULL;
       
   104 cp = GetPixBaseAddr(pm)+from;
       
   105 _res = PyString_FromStringAndSize(cp, length);
       
   106 return _res;
       
   107 """
       
   108 f = ManualGenerator("GetPixMapBytes", pixmapgetbytes_body)
       
   109 f.docstring = lambda: """(pixmap, int start, int size) -> string. Return bytes from the pixmap"""
       
   110 functions.append(f)
       
   111 
       
   112 # Manual generator: store data in a pixmap
       
   113 pixmapputbytes_body = """
       
   114 PixMapHandle pm;
       
   115 int from, length;
       
   116 char *cp, *icp;
       
   117 
       
   118 if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) )
       
   119         return NULL;
       
   120 cp = GetPixBaseAddr(pm)+from;
       
   121 memcpy(cp, icp, length);
       
   122 Py_INCREF(Py_None);
       
   123 _res = Py_None;
       
   124 return _res;
       
   125 """
       
   126 f = ManualGenerator("PutPixMapBytes", pixmapputbytes_body)
       
   127 f.docstring = lambda: """(pixmap, int start, string data). Store bytes into the pixmap"""
       
   128 functions.append(f)
       
   129 
       
   130 # add the populated lists to the generator groups
       
   131 # (in a different wordl the scan program would generate this)
       
   132 for f in functions: module.add(f)
       
   133 for f in methods: object.add(f)
       
   134 
       
   135 
       
   136 
       
   137 # generate output (open the output file as late as possible)
       
   138 SetOutputFileName(OUTPUTFILE)
       
   139 module.generate()