Use full package paths in imports.
[python/dscho.git] / Mac / Modules / list / listsupport.py
blob23a2a840b1d022e26565d184bd24897dde163a80
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).
6 import string
8 # Declarations that change for each manager
9 MACHEADERFILE = 'Lists.h' # The Apple header file
10 MODNAME = '_List' # The name of the module
11 OBJECTNAME = 'List' # The basic name of the objects used here
12 KIND = 'Handle' # Usually 'Ptr' or 'Handle'
14 # The following is *usually* unchanged but may still require tuning
15 MODPREFIX = 'List' # The prefix for module-wide routines
16 OBJECTTYPE = "ListHandle" # The C type used to represent them
17 OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
18 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
19 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
21 from macsupport import *
23 # Create the type objects
24 ListHandle = OpaqueByValueType("ListHandle", "ListObj")
25 ListRef = ListHandle # Obsolete, but used in Lists.h
26 Cell = Point
27 ListBounds = Rect
28 ListBounds_ptr = Rect_ptr
30 ListDefSpec = ListDefSpec_ptr = OpaqueType("ListDefSpec", "PyMac_BuildListDefSpec", "PyMac_GetListDefSpec")
32 VarOutBufferShortsize = VarHeapOutputBufferType('char', 'short', 's') # (buf, &len)
33 InBufferShortsize = VarInputBufferType('char', 'short', 's') # (buf, len)
35 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
36 DataHandle = OpaqueByValueType("DataHandle", "ResObj")
37 Handle = OpaqueByValueType("Handle", "ResObj")
38 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
39 EventModifiers = Type("EventModifiers", "H")
41 includestuff = includestuff + """
42 #ifdef WITHOUT_FRAMEWORKS
43 #include <Lists.h>
44 #else
45 #include <Carbon/Carbon.h>
46 #endif
48 #ifdef USE_TOOLBOX_OBJECT_GLUE
49 extern PyObject *_ListObj_New(ListHandle);
50 extern int _ListObj_Convert(PyObject *, ListHandle *);
52 #define ListObj_New _ListObj_New
53 #define ListObj_Convert _ListObj_Convert
54 #endif
56 #if !ACCESSOR_CALLS_ARE_FUNCTIONS
57 #define GetListPort(list) ((CGrafPtr)(*(list))->port)
58 #define GetListVerticalScrollBar(list) ((*(list))->vScroll)
59 #define GetListHorizontalScrollBar(list) ((*(list))->hScroll)
60 #define GetListActive(list) ((*(list))->lActive)
61 #define GetListClickTime(list) ((*(list))->clikTime)
62 #define GetListRefCon(list) ((*(list))->refCon)
63 #define GetListDefinition(list) ((*(list))->listDefProc) /* XXX Is this indeed the same? */
64 #define GetListUserHandle(list) ((*(list))->userHandle)
65 #define GetListDataHandle(list) ((*(list))->cells)
66 #define GetListFlags(list) ((*(list))->listFlags)
67 #define GetListSelectionFlags(list) ((*(list))->selFlags)
68 #define SetListViewBounds(list, bounds) (((*(list))->rView) = *(bounds))
70 #define SetListPort(list, port) (((*(list))->port) = (GrafPtr)(port))
71 #define SetListCellIndent(list, ind) (((*(list))->indent) = *(ind))
72 #define SetListClickTime(list, time) (((*(list))->clikTime) = (time))
73 #define SetListLastClick(list, click) (((*(list)->lastClick) = *(click))
74 #define SetListRefCon(list, refcon) (((*(list))->refCon) = (refcon))
75 #define SetListUserHandle(list, handle) (((*(list))->userHandle) = (handle))
76 #define SetListFlags(list, flags) (((*(list))->listFlags) = (flags))
77 #define SetListSelectionFlags(list, flags) (((*(list))->selFlags) = (flags))
79 #endif
81 #define as_List(x) ((ListHandle)x)
82 #define as_Resource(lh) ((Handle)lh)
84 static ListDefUPP myListDefFunctionUPP;
86 #if !TARGET_API_MAC_CARBON
88 #define kJumpAbs 0x4EF9
90 #pragma options align=mac68k
91 typedef struct {
92 short jmpabs; /* 4EF9 */
93 ListDefUPP theUPP; /* 00000000 */
94 } LDEFStub, **LDEFStubHandle;
95 #pragma options align=reset
97 static OSErr installLDEFStub(ListHandle list) {
98 LDEFStubHandle stubH;
100 stubH = (LDEFStubHandle)NewHandleClear(sizeof(LDEFStub));
101 if (stubH == NULL)
102 return MemError();
104 (*stubH)->jmpabs = kJumpAbs;
105 (*stubH)->theUPP = myListDefFunctionUPP;
106 HLock((Handle) stubH);
108 (*list)->listDefProc = (Handle)stubH;
109 return noErr;
112 static void removeLDEFStub(ListHandle list) {
113 if ((*list)->listDefProc)
114 DisposeHandle((Handle)(*list)->listDefProc);
115 (*list)->listDefProc = NULL;
118 #endif
121 initstuff = initstuff + """
122 myListDefFunctionUPP = NewListDefUPP((ListDefProcPtr)myListDefFunction);
124 PyMac_INIT_TOOLBOX_OBJECT_NEW(ListHandle, ListObj_New);
125 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ListHandle, ListObj_Convert);
128 class ListMethodGenerator(MethodGenerator):
129 """Similar to MethodGenerator, but has self as last argument"""
131 def parseArgumentList(self, args):
132 args, a0 = args[:-1], args[-1]
133 t0, n0, m0 = a0
134 if m0 != InMode:
135 raise ValueError, "method's 'self' must be 'InMode'"
136 self.itself = Variable(t0, "_self->ob_itself", SelfMode)
137 FunctionGenerator.parseArgumentList(self, args)
138 self.argumentList.append(self.itself)
140 getattrHookCode = """{
141 if ( strcmp(name, "listFlags") == 0 )
142 return Py_BuildValue("l", (long)GetListFlags(self->ob_itself) & 0xff);
143 if ( strcmp(name, "selFlags") == 0 )
144 return Py_BuildValue("l", (long)GetListSelectionFlags(self->ob_itself) & 0xff);
145 if ( strcmp(name, "cellSize") == 0 )
146 return Py_BuildValue("O&", PyMac_BuildPoint, (*self->ob_itself)->cellSize);
147 }"""
149 setattrCode = """
150 static int
151 ListObj_setattr(ListObject *self, char *name, PyObject *value)
153 long intval;
154 int err = 0;
156 if ( value == NULL ) {
157 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
158 return -1;
160 if (strcmp(name, "listFlags") == 0 )
161 err = PyArg_Parse(value, "B", &(*self->ob_itself)->listFlags);
162 else if (strcmp(name, "selFlags") == 0 )
163 err = PyArg_Parse(value, "B", &(*self->ob_itself)->selFlags);
164 else if (strcmp(name, "cellSize") == 0 )
165 err = PyArg_Parse(value, "O&", PyMac_GetPoint, &(*self->ob_itself)->cellSize);
166 else
167 PyErr_SetString(PyExc_AttributeError, "No such attribute");
168 if (err) return 0;
169 else return -1;
174 class MyObjectDefinition(GlobalObjectDefinition):
176 def outputStructMembers(self):
177 ObjectDefinition.outputStructMembers(self)
178 Output("PyObject *ob_ldef_func;")
179 Output("int ob_have_ldef_stub;")
180 Output("int ob_must_be_disposed;")
182 def outputCheckNewArg(self):
183 Output("""if (itself == NULL) {
184 PyErr_SetString(List_Error,"Cannot create null List");
185 return NULL;
186 }""")
188 def outputInitStructMembers(self):
189 ObjectDefinition.outputInitStructMembers(self)
190 Output("it->ob_ldef_func = NULL;")
191 Output("it->ob_have_ldef_stub = 0;")
192 Output("it->ob_must_be_disposed = 1;")
193 Output("SetListRefCon(itself, (long)it);")
195 def outputFreeIt(self, itselfname):
196 Output("Py_XDECREF(self->ob_ldef_func);")
197 Output("self->ob_ldef_func = NULL;")
198 Output("#if !TARGET_API_MAC_CARBON")
199 Output("if (self->ob_have_ldef_stub) removeLDEFStub(self->ob_itself);");
200 Output("#endif");
201 Output("SetListRefCon(self->ob_itself, (long)0);")
202 Output("if (self->ob_must_be_disposed && %s) LDispose(%s);", itselfname, itselfname)
204 def outputGetattrHook(self):
205 Output(getattrHookCode)
207 def outputSetattr(self):
208 Output(setattrCode)
210 # From here on it's basically all boiler plate...
212 finalstuff = finalstuff + """
213 static void myListDefFunction(SInt16 message,
214 Boolean selected,
215 Rect *cellRect,
216 Cell theCell,
217 SInt16 dataOffset,
218 SInt16 dataLen,
219 ListHandle theList)
221 PyObject *listDefFunc, *args, *rv=NULL;
222 ListObject *self;
224 self = (ListObject*)GetListRefCon(theList);
225 if (self == NULL || self->ob_itself != theList)
226 return; /* nothing we can do */
227 listDefFunc = self->ob_ldef_func;
228 if (listDefFunc == NULL)
229 return; /* nothing we can do */
230 args = Py_BuildValue("hbO&O&hhO", message,
231 selected,
232 PyMac_BuildRect, cellRect,
233 PyMac_BuildPoint, theCell,
234 dataOffset,
235 dataLen,
236 self);
237 if (args != NULL) {
238 rv = PyEval_CallObject(listDefFunc, args);
239 Py_DECREF(args);
241 if (rv == NULL) {
242 PySys_WriteStderr("error in list definition callback:\\n");
243 PyErr_Print();
244 } else {
245 Py_DECREF(rv);
250 # Create the generator groups and link them
251 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
252 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
253 module.addobject(object)
255 # Create the generator classes used to populate the lists
256 Function = FunctionGenerator
257 Method = ListMethodGenerator
259 # Create and populate the lists
260 functions = []
261 methods = []
262 execfile(INPUTFILE)
264 # Function to convert any handle to a list and vv.
265 ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
266 as_List_body = """
267 Handle h;
268 ListObject *l;
269 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
270 return NULL;
271 l = (ListObject *)ListObj_New(as_List(h));
272 l->ob_must_be_disposed = 0;
273 _res = Py_BuildValue("O", l);
274 return _res;
276 f = ManualGenerator("as_List", as_List_body)
277 f.docstring = lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)"
278 functions.append(f)
280 f = Method(Handle, 'as_Resource', (ListHandle, 'lh', InMode))
281 methods.append(f)
283 # Manual generator for CreateCustomList, due to callback ideosyncracies
284 CreateCustomList_body = """\
285 Rect rView;
286 Rect dataBounds;
287 Point cellSize;
289 PyObject *listDefFunc;
290 ListDefSpec theSpec;
291 WindowPtr theWindow;
292 Boolean drawIt;
293 Boolean hasGrow;
294 Boolean scrollHoriz;
295 Boolean scrollVert;
296 ListHandle outList;
298 if (!PyArg_ParseTuple(_args, "O&O&O&(iO)O&bbbb",
299 PyMac_GetRect, &rView,
300 PyMac_GetRect, &dataBounds,
301 PyMac_GetPoint, &cellSize,
302 &theSpec.defType, &listDefFunc,
303 WinObj_Convert, &theWindow,
304 &drawIt,
305 &hasGrow,
306 &scrollHoriz,
307 &scrollVert))
308 return NULL;
311 #if TARGET_API_MAC_CARBON
312 /* Carbon applications use the CreateCustomList API */
313 theSpec.u.userProc = myListDefFunctionUPP;
314 CreateCustomList(&rView,
315 &dataBounds,
316 cellSize,
317 &theSpec,
318 theWindow,
319 drawIt,
320 hasGrow,
321 scrollHoriz,
322 scrollVert,
323 &outList);
325 #else
326 /* pre-Carbon applications set the address in the LDEF
327 to a routine descriptor referring to their list
328 definition routine. */
329 outList = LNew(&rView,
330 &dataBounds,
331 cellSize,
333 theWindow,
334 drawIt, /* XXX must be false */
335 hasGrow,
336 scrollHoriz,
337 scrollVert);
338 if (installLDEFStub(outList) != noErr) {
339 PyErr_SetString(PyExc_MemoryError, "can't create LDEF stub");
340 return NULL;
342 #endif
344 _res = ListObj_New(outList);
345 if (_res == NULL)
346 return NULL;
347 Py_INCREF(listDefFunc);
348 ((ListObject*)_res)->ob_ldef_func = listDefFunc;
349 #if !TARGET_API_MAC_CARBON
350 ((ListObject*)_res)->ob_have_ldef_stub = 1;
351 #endif
352 return _res;\
355 f = ManualGenerator("CreateCustomList", CreateCustomList_body);
356 f.docstring = lambda: "(Rect rView, Rect dataBounds, Point cellSize, ListDefSpec theSpec, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle outList)"
357 module.add(f)
359 # add the populated lists to the generator groups
360 # (in a different wordl the scan program would generate this)
361 for f in functions: module.add(f)
362 for f in methods: object.add(f)
365 # generate output (open the output file as late as possible)
366 SetOutputFileName(OUTPUTFILE)
367 module.generate()