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
= '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
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
45 #include <Carbon/Carbon.h>
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
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))
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
92 short jmpabs; /* 4EF9 */
93 ListDefUPP theUPP; /* 00000000 */
94 } LDEFStub, **LDEFStubHandle;
95 #pragma options align=reset
97 static OSErr installLDEFStub(ListHandle list) {
100 stubH = (LDEFStubHandle)NewHandleClear(sizeof(LDEFStub));
104 (*stubH)->jmpabs = kJumpAbs;
105 (*stubH)->theUPP = myListDefFunctionUPP;
106 HLock((Handle) stubH);
108 (*list)->listDefProc = (Handle)stubH;
112 static void removeLDEFStub(ListHandle list) {
113 if ((*list)->listDefProc)
114 DisposeHandle((Handle)(*list)->listDefProc);
115 (*list)->listDefProc = NULL;
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]
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);
151 ListObj_setattr(ListObject *self, char *name, PyObject *value)
156 if ( value == NULL ) {
157 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
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);
167 PyErr_SetString(PyExc_AttributeError, "No such attribute");
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");
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);");
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
):
210 # From here on it's basically all boiler plate...
212 finalstuff
= finalstuff
+ """
213 static void myListDefFunction(SInt16 message,
221 PyObject *listDefFunc, *args, *rv=NULL;
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,
232 PyMac_BuildRect, cellRect,
233 PyMac_BuildPoint, theCell,
238 rv = PyEval_CallObject(listDefFunc, args);
242 PySys_WriteStderr("error in list definition callback:\\n");
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
264 # Function to convert any handle to a list and vv.
265 ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
269 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
271 l = (ListObject *)ListObj_New(as_List(h));
272 l->ob_must_be_disposed = 0;
273 _res = Py_BuildValue("O", l);
276 f
= ManualGenerator("as_List", as_List_body
)
277 f
.docstring
= lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)"
280 f
= Method(Handle
, 'as_Resource', (ListHandle
, 'lh', InMode
))
283 # Manual generator for CreateCustomList, due to callback ideosyncracies
284 CreateCustomList_body
= """\
289 PyObject *listDefFunc;
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,
311 #if TARGET_API_MAC_CARBON
312 /* Carbon applications use the CreateCustomList API */
313 theSpec.u.userProc = myListDefFunctionUPP;
314 CreateCustomList(&rView,
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,
334 drawIt, /* XXX must be false */
338 if (installLDEFStub(outList) != noErr) {
339 PyErr_SetString(PyExc_MemoryError, "can't create LDEF stub");
344 _res = ListObj_New(outList);
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;
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)"
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
)