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).
8 # Declarations that change for each manager
9 MACHEADERFILE
= 'Windows.h' # The Apple header file
10 MODNAME
= 'Win' # The name of the module
11 OBJECTNAME
= 'Window' # The basic name of the objects used here
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX
= MODNAME
# The prefix for module-wide routines
15 OBJECTTYPE
= OBJECTNAME
+ 'Ptr' # The C type used to represent them
16 OBJECTPREFIX
= MODPREFIX
+ '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
21 from macsupport
import *
23 # Create the type objects
25 WindowPtr
= OpaqueByValueType(OBJECTTYPE
, OBJECTPREFIX
)
27 WindowPeek
= OpaqueByValueType("WindowPeek", OBJECTPREFIX
)
28 WindowPeek
.passInput
= lambda name
: "(WindowPeek)(%s)" % name
29 CGrafPtr
= OpaqueByValueType("CGrafPtr", "GrafObj")
30 GrafPtr
= OpaqueByValueType("GrafPtr", "GrafObj")
32 DragReference
= OpaqueByValueType("DragReference", "DragObj")
34 RgnHandle
= OpaqueByValueType("RgnHandle", "ResObj")
35 PicHandle
= OpaqueByValueType("PicHandle", "ResObj")
36 WCTabHandle
= OpaqueByValueType("WCTabHandle", "ResObj")
37 AuxWinHandle
= OpaqueByValueType("AuxWinHandle", "ResObj")
38 PixPatHandle
= OpaqueByValueType("PixPatHandle", "ResObj")
39 AliasHandle
= OpaqueByValueType("AliasHandle", "ResObj")
40 IconRef
= OpaqueByValueType("IconRef", "ResObj")
42 WindowRegionCode
= Type("WindowRegionCode", "H")
43 WindowClass
= Type("WindowClass", "l")
44 WindowAttributes
= Type("WindowAttributes", "l")
45 WindowPositionMethod
= Type("WindowPositionMethod", "l")
46 WindowTransitionEffect
= Type("WindowTransitionEffect", "l")
47 WindowTransitionAction
= Type("WindowTransitionAction", "l")
48 RGBColor
= OpaqueType("RGBColor", "QdRGB")
49 RGBColor_ptr
= RGBColor
50 ScrollWindowOptions
= Type("ScrollWindowOptions", "l")
51 WindowPartCode
= Type("WindowPartCode", "h")
53 PropertyCreator
= OSTypeType("PropertyCreator")
54 PropertyTag
= OSTypeType("PropertyTag")
56 includestuff
= includestuff
+ """
57 #include <%s>""" % MACHEADERFILE
+ """
59 #if !ACCESSOR_CALLS_ARE_FUNCTIONS
60 /* Carbon calls that we emulate in classic mode */
61 #define GetWindowSpareFlag(win) (((CWindowPeek)(win))->spareFlag)
62 #define GetWindowFromPort(port) ((WindowRef)(port))
63 #define GetWindowPortBounds(win, rectp) (*(rectp) = ((CWindowPeek)(win))->port.portRect)
65 #if ACCESSOR_CALLS_ARE_FUNCTIONS
66 /* Classic calls that we emulate in carbon mode */
67 #define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn))
68 #define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn))
69 #define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn))
72 /* Function to dispose a window, with a "normal" calling sequence */
74 PyMac_AutoDisposeWindow(WindowPtr w)
80 finalstuff
= finalstuff
+ """
81 /* Return the object corresponding to the window, or NULL */
93 it = (PyObject *) GetWRefCon(w);
94 if (it == NULL || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) {
96 ((WindowObject *)it)->ob_freeit = NULL;
105 class MyObjectDefinition(GlobalObjectDefinition
):
106 def outputCheckNewArg(self
):
107 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
108 def outputStructMembers(self
):
109 GlobalObjectDefinition
.outputStructMembers(self
)
110 Output("void (*ob_freeit)(%s ptr);", self
.itselftype
)
111 def outputInitStructMembers(self
):
112 GlobalObjectDefinition
.outputInitStructMembers(self
)
113 Output("it->ob_freeit = NULL;")
114 Output("if (GetWRefCon(itself) == 0)")
116 Output("SetWRefCon(itself, (long)it);")
117 Output("it->ob_freeit = PyMac_AutoDisposeWindow;")
119 def outputCheckConvertArg(self
):
120 OutLbrace("if (DlgObj_Check(v))")
121 Output("*p_itself = DlgObj_ConvertToWindow(v);")
125 if (v == Py_None) { *p_itself = NULL; return 1; }
126 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
128 def outputCleanupStructMembers(self
):
129 Output("if (self->ob_freeit && self->ob_itself)")
131 Output("SetWRefCon(self->ob_itself, 0);")
132 Output("self->ob_freeit(self->ob_itself);")
134 Output("self->ob_itself = NULL;")
135 Output("self->ob_freeit = NULL;")
137 def outputCompare(self
):
139 Output("static int %s_compare(self, other)", self
.prefix
)
141 Output("%s *self, *other;", self
.objecttype
)
144 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
145 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
149 def outputHash(self
):
151 Output("static int %s_hash(self)", self
.prefix
)
153 Output("%s *self;", self
.objecttype
)
156 Output("return (int)self->ob_itself;")
159 def outputRepr(self
):
161 Output("static PyObject * %s_repr(self)", self
.prefix
)
163 Output("%s *self;", self
.objecttype
)
166 Output("char buf[100];")
167 Output("""sprintf(buf, "<Window object at 0x%%08.8x for 0x%%08.8x>", self, self->ob_itself);""")
168 Output("return PyString_FromString(buf);")
171 ## def outputFreeIt(self, itselfname):
172 ## Output("DisposeWindow(%s);", itselfname)
173 # From here on it's basically all boiler plate...
175 # Create the generator groups and link them
176 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
177 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
178 module
.addobject(object)
180 # Create the generator classes used to populate the lists
181 Function
= OSErrFunctionGenerator
182 Method
= OSErrMethodGenerator
184 # Create and populate the lists
189 # Add manual routines for converting integer WindowPtr's (as returned by
190 # various event routines) and Dialog objects to a WindowObject.
194 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
196 return WinObj_WhichWindow((WindowPtr)ptr);
199 f
= ManualGenerator("WhichWindow", whichwin_body
)
200 f
.docstring
= lambda : "Resolve an integer WindowPtr address to a Window object"
204 # And add the routines that access the internal bits of a window struct. They
205 # are currently #defined in Windows.h, they will be real routines in Copland
206 # (at which time this execfile can go)
209 # add the populated lists to the generator groups
210 # (in a different wordl the scan program would generate this)
211 for f
in functions
: module
.add(f
)
212 for f
in methods
: object.add(f
)
216 # generate output (open the output file as late as possible)
217 SetOutputFileName(OUTPUTFILE
)