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;
88 initstuff
= initstuff
+ """
89 myListDefFunctionUPP = NewListDefUPP((ListDefProcPtr)myListDefFunction);
91 PyMac_INIT_TOOLBOX_OBJECT_NEW(ListHandle, ListObj_New);
92 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ListHandle, ListObj_Convert);
95 class ListMethodGenerator(MethodGenerator
):
96 """Similar to MethodGenerator, but has self as last argument"""
98 def parseArgumentList(self
, args
):
99 args
, a0
= args
[:-1], args
[-1]
102 raise ValueError, "method's 'self' must be 'InMode'"
103 self
.itself
= Variable(t0
, "_self->ob_itself", SelfMode
)
104 FunctionGenerator
.parseArgumentList(self
, args
)
105 self
.argumentList
.append(self
.itself
)
107 class MyObjectDefinition(PEP253Mixin
, GlobalObjectDefinition
):
108 # XXXX Should inherit from Resource
111 'return Py_BuildValue("l", (long)GetListFlags(self->ob_itself) & 0xff);',
112 'if (!PyArg_Parse(v, "B", &(*self->ob_itself)->listFlags)) return -1;',
116 'return Py_BuildValue("l", (long)GetListSelectionFlags(self->ob_itself) & 0xff);',
117 'if (!PyArg_Parse(v, "B", &(*self->ob_itself)->selFlags)) return -1;',
121 'return Py_BuildValue("O&", PyMac_BuildPoint, (*self->ob_itself)->cellSize);',
122 'if (!PyArg_Parse(v, "O&", PyMac_GetPoint, &(*self->ob_itself)->cellSize)) return -1;',
126 def outputStructMembers(self
):
127 ObjectDefinition
.outputStructMembers(self
)
128 Output("PyObject *ob_ldef_func;")
129 Output("int ob_must_be_disposed;")
131 def outputCheckNewArg(self
):
132 Output("""if (itself == NULL) {
133 PyErr_SetString(List_Error,"Cannot create null List");
137 def outputInitStructMembers(self
):
138 ObjectDefinition
.outputInitStructMembers(self
)
139 Output("it->ob_ldef_func = NULL;")
140 Output("it->ob_must_be_disposed = 1;")
141 Output("SetListRefCon(itself, (long)it);")
143 def outputFreeIt(self
, itselfname
):
144 Output("Py_XDECREF(self->ob_ldef_func);")
145 Output("self->ob_ldef_func = NULL;")
146 Output("SetListRefCon(self->ob_itself, (long)0);")
147 Output("if (self->ob_must_be_disposed && %s) LDispose(%s);", itselfname
, itselfname
)
149 # From here on it's basically all boiler plate...
151 finalstuff
= finalstuff
+ """
152 static void myListDefFunction(SInt16 message,
160 PyObject *listDefFunc, *args, *rv=NULL;
163 self = (ListObject*)GetListRefCon(theList);
164 if (self == NULL || self->ob_itself != theList)
165 return; /* nothing we can do */
166 listDefFunc = self->ob_ldef_func;
167 if (listDefFunc == NULL)
168 return; /* nothing we can do */
169 args = Py_BuildValue("hbO&O&hhO", message,
171 PyMac_BuildRect, cellRect,
172 PyMac_BuildPoint, theCell,
177 rv = PyEval_CallObject(listDefFunc, args);
181 PySys_WriteStderr("error in list definition callback:\\n");
189 # Create the generator groups and link them
190 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
191 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
192 module
.addobject(object)
194 # Create the generator classes used to populate the lists
195 Function
= FunctionGenerator
196 Method
= ListMethodGenerator
198 # Create and populate the lists
203 # Function to convert any handle to a list and vv.
204 ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
208 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
210 l = (ListObject *)ListObj_New(as_List(h));
211 l->ob_must_be_disposed = 0;
212 _res = Py_BuildValue("O", l);
215 f
= ManualGenerator("as_List", as_List_body
)
216 f
.docstring
= lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)"
219 f
= Method(Handle
, 'as_Resource', (ListHandle
, 'lh', InMode
))
222 # Manual generator for CreateCustomList, due to callback ideosyncracies
223 CreateCustomList_body
= """\
228 PyObject *listDefFunc;
237 if (!PyArg_ParseTuple(_args, "O&O&O&(iO)O&bbbb",
238 PyMac_GetRect, &rView,
239 PyMac_GetRect, &dataBounds,
240 PyMac_GetPoint, &cellSize,
241 &theSpec.defType, &listDefFunc,
242 WinObj_Convert, &theWindow,
250 /* Carbon applications use the CreateCustomList API */
251 theSpec.u.userProc = myListDefFunctionUPP;
252 CreateCustomList(&rView,
264 _res = ListObj_New(outList);
267 Py_INCREF(listDefFunc);
268 ((ListObject*)_res)->ob_ldef_func = listDefFunc;
272 f
= ManualGenerator("CreateCustomList", CreateCustomList_body
);
273 f
.docstring
= lambda: "(Rect rView, Rect dataBounds, Point cellSize, ListDefSpec theSpec, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle outList)"
276 # add the populated lists to the generator groups
277 # (in a different wordl the scan program would generate this)
278 for f
in functions
: module
.add(f
)
279 for f
in methods
: object.add(f
)
282 # generate output (open the output file as late as possible)
283 SetOutputFileName(OUTPUTFILE
)