|
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 = '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 |
|
12 |
|
13 # The following is *usually* unchanged but may still require tuning |
|
14 MODPREFIX = 'Win' # 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 |
|
20 |
|
21 from macsupport import * |
|
22 |
|
23 # Create the type objects |
|
24 |
|
25 WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
|
26 WindowRef = WindowPtr |
|
27 WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX) |
|
28 WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name |
|
29 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") |
|
30 GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj") |
|
31 |
|
32 DragReference = OpaqueByValueType("DragReference", "DragObj") |
|
33 |
|
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") |
|
41 |
|
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") |
|
52 WindowDefPartCode = Type("WindowDefPartCode", "h") |
|
53 WindowModality = Type("WindowModality", "l") |
|
54 GDHandle = OpaqueByValueType("GDHandle", "ResObj") |
|
55 WindowConstrainOptions = Type("WindowConstrainOptions", "l") |
|
56 |
|
57 PropertyCreator = OSTypeType("PropertyCreator") |
|
58 PropertyTag = OSTypeType("PropertyTag") |
|
59 |
|
60 includestuff = includestuff + """ |
|
61 #include <Carbon/Carbon.h> |
|
62 |
|
63 #ifdef USE_TOOLBOX_OBJECT_GLUE |
|
64 extern PyObject *_WinObj_New(WindowRef); |
|
65 extern PyObject *_WinObj_WhichWindow(WindowRef); |
|
66 extern int _WinObj_Convert(PyObject *, WindowRef *); |
|
67 |
|
68 #define WinObj_New _WinObj_New |
|
69 #define WinObj_WhichWindow _WinObj_WhichWindow |
|
70 #define WinObj_Convert _WinObj_Convert |
|
71 #endif |
|
72 |
|
73 /* Classic calls that we emulate in carbon mode */ |
|
74 #define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn)) |
|
75 #define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn)) |
|
76 #define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn)) |
|
77 |
|
78 /* Function to dispose a window, with a "normal" calling sequence */ |
|
79 static void |
|
80 PyMac_AutoDisposeWindow(WindowPtr w) |
|
81 { |
|
82 DisposeWindow(w); |
|
83 } |
|
84 """ |
|
85 |
|
86 finalstuff = finalstuff + """ |
|
87 /* Return the object corresponding to the window, or NULL */ |
|
88 |
|
89 PyObject * |
|
90 WinObj_WhichWindow(WindowPtr w) |
|
91 { |
|
92 PyObject *it; |
|
93 |
|
94 if (w == NULL) { |
|
95 it = Py_None; |
|
96 Py_INCREF(it); |
|
97 } else { |
|
98 it = (PyObject *) GetWRefCon(w); |
|
99 if (it == NULL || !IsPointerValid((Ptr)it) || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) { |
|
100 it = WinObj_New(w); |
|
101 ((WindowObject *)it)->ob_freeit = NULL; |
|
102 } else { |
|
103 Py_INCREF(it); |
|
104 } |
|
105 } |
|
106 return it; |
|
107 } |
|
108 """ |
|
109 |
|
110 initstuff = initstuff + """ |
|
111 PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_New); |
|
112 PyMac_INIT_TOOLBOX_OBJECT_NEW(WindowPtr, WinObj_WhichWindow); |
|
113 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(WindowPtr, WinObj_Convert); |
|
114 """ |
|
115 |
|
116 class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|
117 def outputCheckNewArg(self): |
|
118 Output("if (itself == NULL) return PyMac_Error(resNotFound);") |
|
119 Output("/* XXXX Or should we use WhichWindow code here? */") |
|
120 def outputStructMembers(self): |
|
121 GlobalObjectDefinition.outputStructMembers(self) |
|
122 Output("void (*ob_freeit)(%s ptr);", self.itselftype) |
|
123 def outputInitStructMembers(self): |
|
124 GlobalObjectDefinition.outputInitStructMembers(self) |
|
125 Output("it->ob_freeit = NULL;") |
|
126 Output("if (GetWRefCon(itself) == 0)") |
|
127 OutLbrace() |
|
128 Output("SetWRefCon(itself, (long)it);") |
|
129 Output("it->ob_freeit = PyMac_AutoDisposeWindow;") |
|
130 OutRbrace() |
|
131 def outputCheckConvertArg(self): |
|
132 Out(""" |
|
133 if (v == Py_None) { *p_itself = NULL; return 1; } |
|
134 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; } |
|
135 """) |
|
136 OutLbrace() |
|
137 Output("DialogRef dlg;") |
|
138 OutLbrace("if (DlgObj_Convert(v, &dlg) && dlg)") |
|
139 Output("*p_itself = GetDialogWindow(dlg);") |
|
140 Output("return 1;") |
|
141 OutRbrace() |
|
142 Output("PyErr_Clear();") |
|
143 OutRbrace() |
|
144 def outputCleanupStructMembers(self): |
|
145 Output("if (self->ob_freeit && self->ob_itself)") |
|
146 OutLbrace() |
|
147 Output("SetWRefCon(self->ob_itself, 0);") |
|
148 Output("self->ob_freeit(self->ob_itself);") |
|
149 OutRbrace() |
|
150 Output("self->ob_itself = NULL;") |
|
151 Output("self->ob_freeit = NULL;") |
|
152 |
|
153 def outputCompare(self): |
|
154 Output() |
|
155 Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype) |
|
156 OutLbrace() |
|
157 Output("if ( self->ob_itself > other->ob_itself ) return 1;") |
|
158 Output("if ( self->ob_itself < other->ob_itself ) return -1;") |
|
159 Output("return 0;") |
|
160 OutRbrace() |
|
161 |
|
162 def outputHash(self): |
|
163 Output() |
|
164 Output("static int %s_hash(%s *self)", self.prefix, self.objecttype) |
|
165 OutLbrace() |
|
166 Output("return (int)self->ob_itself;") |
|
167 OutRbrace() |
|
168 |
|
169 def outputRepr(self): |
|
170 Output() |
|
171 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype) |
|
172 OutLbrace() |
|
173 Output("char buf[100];") |
|
174 Output("""sprintf(buf, "<Window object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""") |
|
175 Output("return PyString_FromString(buf);") |
|
176 OutRbrace() |
|
177 |
|
178 ## def outputFreeIt(self, itselfname): |
|
179 ## Output("DisposeWindow(%s);", itselfname) |
|
180 # From here on it's basically all boiler plate... |
|
181 |
|
182 # Create the generator groups and link them |
|
183 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
|
184 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|
185 module.addobject(object) |
|
186 |
|
187 # Create the generator classes used to populate the lists |
|
188 Function = OSErrWeakLinkFunctionGenerator |
|
189 Method = OSErrWeakLinkMethodGenerator |
|
190 |
|
191 # Create and populate the lists |
|
192 functions = [] |
|
193 methods = [] |
|
194 execfile(INPUTFILE) |
|
195 |
|
196 # Add manual routines for converting integer WindowPtr's (as returned by |
|
197 # various event routines) and Dialog objects to a WindowObject. |
|
198 whichwin_body = """ |
|
199 long ptr; |
|
200 |
|
201 if ( !PyArg_ParseTuple(_args, "i", &ptr) ) |
|
202 return NULL; |
|
203 _res = WinObj_WhichWindow((WindowPtr)ptr); |
|
204 return _res; |
|
205 """ |
|
206 |
|
207 f = ManualGenerator("WhichWindow", whichwin_body) |
|
208 f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object" |
|
209 |
|
210 functions.append(f) |
|
211 |
|
212 # And add the routines that access the internal bits of a window struct. They |
|
213 # are currently #defined in Windows.h, they will be real routines in Copland |
|
214 # (at which time this execfile can go) |
|
215 execfile(EDITFILE) |
|
216 |
|
217 # add the populated lists to the generator groups |
|
218 # (in a different wordl the scan program would generate this) |
|
219 for f in functions: module.add(f) |
|
220 for f in methods: object.add(f) |
|
221 |
|
222 |
|
223 |
|
224 # generate output (open the output file as late as possible) |
|
225 SetOutputFileName(OUTPUTFILE) |
|
226 module.generate() |