blob: 6b67db1fc1929403f3e828b99214b99e773748e2 [file] [log] [blame]
Guido van Rossum17448e21995-01-30 11:53:55 +00001# 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
6import string
7
8# Declarations that change for each manager
9MACHEADERFILE = 'Windows.h' # The Apple header file
10MODNAME = 'Win' # The name of the module
11OBJECTNAME = 'Window' # The basic name of the objects used here
12
13# The following is *usually* unchanged but may still require tuning
14MODPREFIX = MODNAME # The prefix for module-wide routines
15OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them
16OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
17INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
Jack Jansen330f5761995-11-14 10:48:54 +000018EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions
Guido van Rossum17448e21995-01-30 11:53:55 +000019OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
20
21from macsupport import *
22
23# Create the type objects
24
25WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
Jack Jansenb81cf9d1995-06-06 13:08:40 +000026WindowRef = WindowPtr
Guido van Rossum17448e21995-01-30 11:53:55 +000027WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
28WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name
Jack Jansenb7abb181995-11-15 15:18:47 +000029CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
30GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
Guido van Rossum17448e21995-01-30 11:53:55 +000031
Jack Jansen723ad8a2000-12-12 22:10:21 +000032DragReference = OpaqueByValueType("DragReference", "DragObj")
33
Jack Jansenb7abb181995-11-15 15:18:47 +000034RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
Jack Jansen330f5761995-11-14 10:48:54 +000035PicHandle = OpaqueByValueType("PicHandle", "ResObj")
Jack Jansen8f0fab71997-08-15 14:38:05 +000036WCTabHandle = OpaqueByValueType("WCTabHandle", "ResObj")
37AuxWinHandle = OpaqueByValueType("AuxWinHandle", "ResObj")
38PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
Jack Jansena05ac601999-12-12 21:41:51 +000039AliasHandle = OpaqueByValueType("AliasHandle", "ResObj")
40IconRef = OpaqueByValueType("IconRef", "ResObj")
Guido van Rossum17448e21995-01-30 11:53:55 +000041
Jack Jansen0b13e7c2000-07-07 13:09:35 +000042WindowRegionCode = Type("WindowRegionCode", "H")
Jack Jansena05ac601999-12-12 21:41:51 +000043WindowClass = Type("WindowClass", "l")
44WindowAttributes = Type("WindowAttributes", "l")
45WindowPositionMethod = Type("WindowPositionMethod", "l")
46WindowTransitionEffect = Type("WindowTransitionEffect", "l")
47WindowTransitionAction = Type("WindowTransitionAction", "l")
Jack Jansena05ac601999-12-12 21:41:51 +000048RGBColor = OpaqueType("RGBColor", "QdRGB")
Jack Jansen723ad8a2000-12-12 22:10:21 +000049RGBColor_ptr = RGBColor
50ScrollWindowOptions = Type("ScrollWindowOptions", "l")
51WindowPartCode = Type("WindowPartCode", "h")
52
Jack Jansena05ac601999-12-12 21:41:51 +000053PropertyCreator = OSTypeType("PropertyCreator")
54PropertyTag = OSTypeType("PropertyTag")
Jack Jansen21f96871998-02-20 16:02:09 +000055
Guido van Rossum17448e21995-01-30 11:53:55 +000056includestuff = includestuff + """
57#include <%s>""" % MACHEADERFILE + """
Jack Jansen723ad8a2000-12-12 22:10:21 +000058
59#if !ACCESSOR_CALLS_ARE_FUNCTIONS
Jack Jansen32248652000-12-19 22:23:06 +000060/* Carbon calls that we emulate in classic mode */
Jack Jansen723ad8a2000-12-12 22:10:21 +000061#define GetWindowSpareFlag(win) (((CWindowPeek)(win))->spareFlag)
62#define GetWindowFromPort(port) ((WindowRef)(port))
63#define GetWindowPortBounds(win, rectp) (*(rectp) = ((CWindowPeek)(win))->port.portRect)
64#endif
Jack Jansen32248652000-12-19 22:23:06 +000065#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))
70#endif
Jack Jansen723ad8a2000-12-12 22:10:21 +000071
Jack Jansen0aee0e62000-08-25 22:17:51 +000072/* Function to dispose a window, with a "normal" calling sequence */
73static void
74PyMac_AutoDisposeWindow(WindowPtr w)
75{
76 DisposeWindow(w);
77}
Guido van Rossum17448e21995-01-30 11:53:55 +000078"""
79
80finalstuff = finalstuff + """
81/* Return the object corresponding to the window, or NULL */
82
83PyObject *
84WinObj_WhichWindow(w)
85 WindowPtr w;
86{
87 PyObject *it;
88
Jack Jansen0aee0e62000-08-25 22:17:51 +000089 if (w == NULL) {
Guido van Rossum17448e21995-01-30 11:53:55 +000090 it = Py_None;
Jack Jansen0aee0e62000-08-25 22:17:51 +000091 Py_INCREF(it);
92 } else {
93 it = (PyObject *) GetWRefCon(w);
Jack Jansen80716f02000-12-14 22:29:00 +000094 if (it == NULL || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) {
Jack Jansen0aee0e62000-08-25 22:17:51 +000095 it = WinObj_New(w);
96 ((WindowObject *)it)->ob_freeit = NULL;
97 } else {
98 Py_INCREF(it);
99 }
100 }
Guido van Rossum17448e21995-01-30 11:53:55 +0000101 return it;
102}
103"""
104
105class MyObjectDefinition(GlobalObjectDefinition):
106 def outputCheckNewArg(self):
107 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
Jack Jansen0aee0e62000-08-25 22:17:51 +0000108 def outputStructMembers(self):
109 GlobalObjectDefinition.outputStructMembers(self)
110 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
Guido van Rossum17448e21995-01-30 11:53:55 +0000111 def outputInitStructMembers(self):
112 GlobalObjectDefinition.outputInitStructMembers(self)
Jack Jansen80716f02000-12-14 22:29:00 +0000113 Output("it->ob_freeit = NULL;")
114 Output("if (GetWRefCon(itself) == 0)")
115 OutLbrace()
Guido van Rossum17448e21995-01-30 11:53:55 +0000116 Output("SetWRefCon(itself, (long)it);")
Jack Jansen0aee0e62000-08-25 22:17:51 +0000117 Output("it->ob_freeit = PyMac_AutoDisposeWindow;")
Jack Jansen80716f02000-12-14 22:29:00 +0000118 OutRbrace()
Guido van Rossum17448e21995-01-30 11:53:55 +0000119 def outputCheckConvertArg(self):
120 OutLbrace("if (DlgObj_Check(v))")
Jack Jansen0aee0e62000-08-25 22:17:51 +0000121 Output("*p_itself = DlgObj_ConvertToWindow(v);")
Guido van Rossum17448e21995-01-30 11:53:55 +0000122 Output("return 1;")
123 OutRbrace()
124 Out("""
125 if (v == Py_None) { *p_itself = NULL; return 1; }
126 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
127 """)
Jack Jansen0aee0e62000-08-25 22:17:51 +0000128 def outputCleanupStructMembers(self):
Jack Jansen0aee0e62000-08-25 22:17:51 +0000129 Output("if (self->ob_freeit && self->ob_itself)")
130 OutLbrace()
Jack Jansen80716f02000-12-14 22:29:00 +0000131 Output("SetWRefCon(self->ob_itself, 0);")
Jack Jansen0aee0e62000-08-25 22:17:51 +0000132 Output("self->ob_freeit(self->ob_itself);")
133 OutRbrace()
134 Output("self->ob_itself = NULL;")
Jack Jansen80716f02000-12-14 22:29:00 +0000135 Output("self->ob_freeit = NULL;")
Jack Jansen87a30922000-12-19 21:34:55 +0000136
137 def outputCompare(self):
138 Output()
139 Output("static int %s_compare(self, other)", self.prefix)
140 IndentLevel()
141 Output("%s *self, *other;", self.objecttype)
142 DedentLevel()
143 OutLbrace()
144 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
145 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
146 Output("return 0;")
147 OutRbrace()
148
149 def outputHash(self):
150 Output()
151 Output("static int %s_hash(self)", self.prefix)
152 IndentLevel()
153 Output("%s *self;", self.objecttype)
154 DedentLevel()
155 OutLbrace()
156 Output("return (int)self->ob_itself;")
157 OutRbrace()
158
Jack Jansen0aee0e62000-08-25 22:17:51 +0000159## def outputFreeIt(self, itselfname):
160## Output("DisposeWindow(%s);", itselfname)
Guido van Rossum17448e21995-01-30 11:53:55 +0000161# From here on it's basically all boiler plate...
162
163# Create the generator groups and link them
164module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
165object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
166module.addobject(object)
167
168# Create the generator classes used to populate the lists
169Function = OSErrFunctionGenerator
170Method = OSErrMethodGenerator
171
172# Create and populate the lists
173functions = []
174methods = []
175execfile(INPUTFILE)
176
Jack Jansen8f0fab71997-08-15 14:38:05 +0000177# Add manual routines for converting integer WindowPtr's (as returned by
178# various event routines) and Dialog objects to a WindowObject.
Jack Jansencaf75051995-08-17 14:28:27 +0000179whichwin_body = """
180long ptr;
181
182if ( !PyArg_ParseTuple(_args, "i", &ptr) )
183 return NULL;
184return WinObj_WhichWindow((WindowPtr)ptr);
185"""
186
187f = ManualGenerator("WhichWindow", whichwin_body)
188f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"
189
190functions.append(f)
191
Jack Jansen330f5761995-11-14 10:48:54 +0000192# And add the routines that access the internal bits of a window struct. They
193# are currently #defined in Windows.h, they will be real routines in Copland
194# (at which time this execfile can go)
195execfile(EDITFILE)
196
Guido van Rossum17448e21995-01-30 11:53:55 +0000197# add the populated lists to the generator groups
198# (in a different wordl the scan program would generate this)
199for f in functions: module.add(f)
200for f in methods: object.add(f)
201
Jack Jansencaf75051995-08-17 14:28:27 +0000202
203
Guido van Rossum17448e21995-01-30 11:53:55 +0000204# generate output (open the output file as late as possible)
205SetOutputFileName(OUTPUTFILE)
206module.generate()