Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Modules / list / listsupport.py
blobab7cc3a3083258f6c7418824c6eccf7859c670b1
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 """
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);
93 """
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]
100 t0, n0, m0 = a0
101 if m0 != InMode:
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
109 getsetlist = [(
110 'listFlags',
111 'return Py_BuildValue("l", (long)GetListFlags(self->ob_itself) & 0xff);',
112 'if (!PyArg_Parse(v, "B", &(*self->ob_itself)->listFlags)) return -1;',
113 None,
114 ), (
115 'selFlags',
116 'return Py_BuildValue("l", (long)GetListSelectionFlags(self->ob_itself) & 0xff);',
117 'if (!PyArg_Parse(v, "B", &(*self->ob_itself)->selFlags)) return -1;',
118 None,
119 ), (
120 'cellSize',
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;',
123 None
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");
134 return NULL;
135 }""")
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,
153 Boolean selected,
154 Rect *cellRect,
155 Cell theCell,
156 SInt16 dataOffset,
157 SInt16 dataLen,
158 ListHandle theList)
160 PyObject *listDefFunc, *args, *rv=NULL;
161 ListObject *self;
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,
170 selected,
171 PyMac_BuildRect, cellRect,
172 PyMac_BuildPoint, theCell,
173 dataOffset,
174 dataLen,
175 self);
176 if (args != NULL) {
177 rv = PyEval_CallObject(listDefFunc, args);
178 Py_DECREF(args);
180 if (rv == NULL) {
181 PySys_WriteStderr("error in list definition callback:\\n");
182 PyErr_Print();
183 } else {
184 Py_DECREF(rv);
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
199 functions = []
200 methods = []
201 execfile(INPUTFILE)
203 # Function to convert any handle to a list and vv.
204 ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
205 as_List_body = """
206 Handle h;
207 ListObject *l;
208 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
209 return NULL;
210 l = (ListObject *)ListObj_New(as_List(h));
211 l->ob_must_be_disposed = 0;
212 _res = Py_BuildValue("O", l);
213 return _res;
215 f = ManualGenerator("as_List", as_List_body)
216 f.docstring = lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)"
217 functions.append(f)
219 f = Method(Handle, 'as_Resource', (ListHandle, 'lh', InMode))
220 methods.append(f)
222 # Manual generator for CreateCustomList, due to callback ideosyncracies
223 CreateCustomList_body = """\
224 Rect rView;
225 Rect dataBounds;
226 Point cellSize;
228 PyObject *listDefFunc;
229 ListDefSpec theSpec;
230 WindowPtr theWindow;
231 Boolean drawIt;
232 Boolean hasGrow;
233 Boolean scrollHoriz;
234 Boolean scrollVert;
235 ListHandle outList;
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,
243 &drawIt,
244 &hasGrow,
245 &scrollHoriz,
246 &scrollVert))
247 return NULL;
250 /* Carbon applications use the CreateCustomList API */
251 theSpec.u.userProc = myListDefFunctionUPP;
252 CreateCustomList(&rView,
253 &dataBounds,
254 cellSize,
255 &theSpec,
256 theWindow,
257 drawIt,
258 hasGrow,
259 scrollHoriz,
260 scrollVert,
261 &outList);
264 _res = ListObj_New(outList);
265 if (_res == NULL)
266 return NULL;
267 Py_INCREF(listDefFunc);
268 ((ListObject*)_res)->ob_ldef_func = listDefFunc;
269 return _res;\
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)"
274 module.add(f)
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)
284 module.generate()