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).
9 addpack
.addpack(':Tools:bgen:bgen')
11 # Declarations that change for each manager
12 MACHEADERFILE
= 'Windows.h' # The Apple header file
13 MODNAME
= 'Win' # The name of the module
14 OBJECTNAME
= 'Window' # The basic name of the objects used here
16 # The following is *usually* unchanged but may still require tuning
17 MODPREFIX
= MODNAME
# The prefix for module-wide routines
18 OBJECTTYPE
= OBJECTNAME
+ 'Ptr' # The C type used to represent them
19 OBJECTPREFIX
= MODPREFIX
+ 'Obj' # The prefix for object methods
20 INPUTFILE
= string
.lower(MODPREFIX
) + 'gen.py' # The file generated by the scanner
21 OUTPUTFILE
= MODNAME
+ "module.c" # The file generated by this program
23 from macsupport
import *
25 # Create the type objects
27 WindowPtr
= OpaqueByValueType(OBJECTTYPE
, OBJECTPREFIX
)
29 WindowPeek
= OpaqueByValueType("WindowPeek", OBJECTPREFIX
)
30 WindowPeek
.passInput
= lambda name
: "(WindowPeek)(%s)" % name
32 #RgnHandle = FakeType("theWindow->updtRgn") # XXX
34 includestuff
= includestuff
+ """
35 #include <%s>""" % MACHEADERFILE
+ """
37 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
39 #ifdef HAVE_UNIVERSAL_HEADERS
40 #define WindowPeek WindowPtr
44 finalstuff
= finalstuff
+ """
45 /* Return the object corresponding to the window, or NULL */
53 /* XXX What if we find a stdwin window or a window belonging
54 to some other package? */
58 it = (PyObject *) GetWRefCon(w);
59 if (it == NULL || ((WindowObject *)it)->ob_itself != w)
66 class MyObjectDefinition(GlobalObjectDefinition
):
67 def outputCheckNewArg(self
):
68 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
69 def outputInitStructMembers(self
):
70 GlobalObjectDefinition
.outputInitStructMembers(self
)
71 Output("SetWRefCon(itself, (long)it);")
72 def outputCheckConvertArg(self
):
73 OutLbrace("if (DlgObj_Check(v))")
74 Output("*p_itself = ((WindowObject *)v)->ob_itself;")
78 if (v == Py_None) { *p_itself = NULL; return 1; }
79 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
81 def outputFreeIt(self
, itselfname
):
82 Output("DisposeWindow(%s);", itselfname
)
84 # From here on it's basically all boiler plate...
86 # Create the generator groups and link them
87 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
88 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
89 module
.addobject(object)
91 # Create the generator classes used to populate the lists
92 Function
= OSErrFunctionGenerator
93 Method
= OSErrMethodGenerator
95 # Create and populate the lists
100 # Add a manual routine for converting integer WindowPtr's (as returned by
101 # various event routines) to a WindowObject.
105 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
107 return WinObj_WhichWindow((WindowPtr)ptr);
110 f
= ManualGenerator("WhichWindow", whichwin_body
)
111 f
.docstring
= lambda : "Resolve an integer WindowPtr address to a Window object"
115 # add the populated lists to the generator groups
116 # (in a different wordl the scan program would generate this)
117 for f
in functions
: module
.add(f
)
118 for f
in methods
: object.add(f
)
122 # generate output (open the output file as late as possible)
123 SetOutputFileName(OUTPUTFILE
)