| 1 | n/a | # This script generates a Python interface for an Apple Macintosh Manager. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # The function specifications are generated by scanning the mamager's header file, |
|---|
| 4 | n/a | # using the "scantools" package (customized for this particular manager). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import string |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Declarations that change for each manager |
|---|
| 9 | n/a | MACHEADERFILE = 'Lists.h' # The Apple header file |
|---|
| 10 | n/a | MODNAME = '_List' # The name of the module |
|---|
| 11 | n/a | OBJECTNAME = 'List' # The basic name of the objects used here |
|---|
| 12 | n/a | KIND = 'Handle' # Usually 'Ptr' or 'Handle' |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # The following is *usually* unchanged but may still require tuning |
|---|
| 15 | n/a | MODPREFIX = 'List' # The prefix for module-wide routines |
|---|
| 16 | n/a | OBJECTTYPE = "ListHandle" # The C type used to represent them |
|---|
| 17 | n/a | OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods |
|---|
| 18 | n/a | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|---|
| 19 | n/a | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | from macsupport import * |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | # Create the type objects |
|---|
| 24 | n/a | ListHandle = OpaqueByValueType("ListHandle", "ListObj") |
|---|
| 25 | n/a | ListRef = ListHandle # Obsolete, but used in Lists.h |
|---|
| 26 | n/a | Cell = Point |
|---|
| 27 | n/a | ListBounds = Rect |
|---|
| 28 | n/a | ListBounds_ptr = Rect_ptr |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | ListDefSpec = ListDefSpec_ptr = OpaqueType("ListDefSpec", "PyMac_BuildListDefSpec", "PyMac_GetListDefSpec") |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | VarOutBufferShortsize = VarHeapOutputBufferType('char', 'short', 's') # (buf, &len) |
|---|
| 33 | n/a | InBufferShortsize = VarInputBufferType('char', 'short', 's') # (buf, len) |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
|---|
| 36 | n/a | DataHandle = OpaqueByValueType("DataHandle", "ResObj") |
|---|
| 37 | n/a | Handle = OpaqueByValueType("Handle", "ResObj") |
|---|
| 38 | n/a | CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") |
|---|
| 39 | n/a | EventModifiers = Type("EventModifiers", "H") |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | includestuff = includestuff + """ |
|---|
| 42 | n/a | #include <Carbon/Carbon.h> |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 45 | n/a | extern PyObject *_ListObj_New(ListHandle); |
|---|
| 46 | n/a | extern int _ListObj_Convert(PyObject *, ListHandle *); |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | #define ListObj_New _ListObj_New |
|---|
| 49 | n/a | #define ListObj_Convert _ListObj_Convert |
|---|
| 50 | n/a | #endif |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | #define as_List(x) ((ListHandle)x) |
|---|
| 53 | n/a | #define as_Resource(lh) ((Handle)lh) |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | static ListDefUPP myListDefFunctionUPP; |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | """ |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | initstuff = initstuff + """ |
|---|
| 60 | n/a | myListDefFunctionUPP = NewListDefUPP((ListDefProcPtr)myListDefFunction); |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(ListHandle, ListObj_New); |
|---|
| 63 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ListHandle, ListObj_Convert); |
|---|
| 64 | n/a | """ |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | class ListMethodGenerator(MethodGenerator): |
|---|
| 67 | n/a | """Similar to MethodGenerator, but has self as last argument""" |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def parseArgumentList(self, args): |
|---|
| 70 | n/a | args, a0 = args[:-1], args[-1] |
|---|
| 71 | n/a | t0, n0, m0 = a0 |
|---|
| 72 | n/a | if m0 != InMode: |
|---|
| 73 | n/a | raise ValueError, "method's 'self' must be 'InMode'" |
|---|
| 74 | n/a | self.itself = Variable(t0, "_self->ob_itself", SelfMode) |
|---|
| 75 | n/a | FunctionGenerator.parseArgumentList(self, args) |
|---|
| 76 | n/a | self.argumentList.append(self.itself) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 79 | n/a | # XXXX Should inherit from Resource |
|---|
| 80 | n/a | getsetlist = [( |
|---|
| 81 | n/a | 'listFlags', |
|---|
| 82 | n/a | 'return Py_BuildValue("l", (long)GetListFlags(self->ob_itself) & 0xff);', |
|---|
| 83 | n/a | 'if (!PyArg_Parse(v, "B", &(*self->ob_itself)->listFlags)) return -1;', |
|---|
| 84 | n/a | None, |
|---|
| 85 | n/a | ), ( |
|---|
| 86 | n/a | 'selFlags', |
|---|
| 87 | n/a | 'return Py_BuildValue("l", (long)GetListSelectionFlags(self->ob_itself) & 0xff);', |
|---|
| 88 | n/a | 'if (!PyArg_Parse(v, "B", &(*self->ob_itself)->selFlags)) return -1;', |
|---|
| 89 | n/a | None, |
|---|
| 90 | n/a | ), ( |
|---|
| 91 | n/a | 'cellSize', |
|---|
| 92 | n/a | 'return Py_BuildValue("O&", PyMac_BuildPoint, (*self->ob_itself)->cellSize);', |
|---|
| 93 | n/a | 'if (!PyArg_Parse(v, "O&", PyMac_GetPoint, &(*self->ob_itself)->cellSize)) return -1;', |
|---|
| 94 | n/a | None |
|---|
| 95 | n/a | )] |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | def outputStructMembers(self): |
|---|
| 98 | n/a | ObjectDefinition.outputStructMembers(self) |
|---|
| 99 | n/a | Output("PyObject *ob_ldef_func;") |
|---|
| 100 | n/a | Output("int ob_must_be_disposed;") |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | def outputCheckNewArg(self): |
|---|
| 103 | n/a | Output("""if (itself == NULL) { |
|---|
| 104 | n/a | PyErr_SetString(List_Error,"Cannot create null List"); |
|---|
| 105 | n/a | return NULL; |
|---|
| 106 | n/a | }""") |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | def outputInitStructMembers(self): |
|---|
| 109 | n/a | ObjectDefinition.outputInitStructMembers(self) |
|---|
| 110 | n/a | Output("it->ob_ldef_func = NULL;") |
|---|
| 111 | n/a | Output("it->ob_must_be_disposed = 1;") |
|---|
| 112 | n/a | Output("SetListRefCon(itself, (long)it);") |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | def outputFreeIt(self, itselfname): |
|---|
| 115 | n/a | Output("Py_XDECREF(self->ob_ldef_func);") |
|---|
| 116 | n/a | Output("self->ob_ldef_func = NULL;") |
|---|
| 117 | n/a | Output("SetListRefCon(self->ob_itself, (long)0);") |
|---|
| 118 | n/a | Output("if (self->ob_must_be_disposed && %s) LDispose(%s);", itselfname, itselfname) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | # From here on it's basically all boiler plate... |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | finalstuff = finalstuff + """ |
|---|
| 123 | n/a | static void myListDefFunction(SInt16 message, |
|---|
| 124 | n/a | Boolean selected, |
|---|
| 125 | n/a | Rect *cellRect, |
|---|
| 126 | n/a | Cell theCell, |
|---|
| 127 | n/a | SInt16 dataOffset, |
|---|
| 128 | n/a | SInt16 dataLen, |
|---|
| 129 | n/a | ListHandle theList) |
|---|
| 130 | n/a | { |
|---|
| 131 | n/a | PyObject *listDefFunc, *args, *rv=NULL; |
|---|
| 132 | n/a | ListObject *self; |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | self = (ListObject*)GetListRefCon(theList); |
|---|
| 135 | n/a | if (self == NULL || self->ob_itself != theList) |
|---|
| 136 | n/a | return; /* nothing we can do */ |
|---|
| 137 | n/a | listDefFunc = self->ob_ldef_func; |
|---|
| 138 | n/a | if (listDefFunc == NULL) |
|---|
| 139 | n/a | return; /* nothing we can do */ |
|---|
| 140 | n/a | args = Py_BuildValue("hbO&O&hhO", message, |
|---|
| 141 | n/a | selected, |
|---|
| 142 | n/a | PyMac_BuildRect, cellRect, |
|---|
| 143 | n/a | PyMac_BuildPoint, theCell, |
|---|
| 144 | n/a | dataOffset, |
|---|
| 145 | n/a | dataLen, |
|---|
| 146 | n/a | self); |
|---|
| 147 | n/a | if (args != NULL) { |
|---|
| 148 | n/a | rv = PyEval_CallObject(listDefFunc, args); |
|---|
| 149 | n/a | Py_DECREF(args); |
|---|
| 150 | n/a | } |
|---|
| 151 | n/a | if (rv == NULL) { |
|---|
| 152 | n/a | PySys_WriteStderr("error in list definition callback:\\n"); |
|---|
| 153 | n/a | PyErr_Print(); |
|---|
| 154 | n/a | } else { |
|---|
| 155 | n/a | Py_DECREF(rv); |
|---|
| 156 | n/a | } |
|---|
| 157 | n/a | } |
|---|
| 158 | n/a | """ |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | # Create the generator groups and link them |
|---|
| 161 | n/a | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
|---|
| 162 | n/a | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|---|
| 163 | n/a | module.addobject(object) |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | # Create the generator classes used to populate the lists |
|---|
| 166 | n/a | Function = FunctionGenerator |
|---|
| 167 | n/a | Method = ListMethodGenerator |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | # Create and populate the lists |
|---|
| 170 | n/a | functions = [] |
|---|
| 171 | n/a | methods = [] |
|---|
| 172 | n/a | execfile(INPUTFILE) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | # Function to convert any handle to a list and vv. |
|---|
| 175 | n/a | ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode)) |
|---|
| 176 | n/a | as_List_body = """ |
|---|
| 177 | n/a | Handle h; |
|---|
| 178 | n/a | ListObject *l; |
|---|
| 179 | n/a | if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h)) |
|---|
| 180 | n/a | return NULL; |
|---|
| 181 | n/a | l = (ListObject *)ListObj_New(as_List(h)); |
|---|
| 182 | n/a | l->ob_must_be_disposed = 0; |
|---|
| 183 | n/a | _res = Py_BuildValue("O", l); |
|---|
| 184 | n/a | return _res; |
|---|
| 185 | n/a | """ |
|---|
| 186 | n/a | f = ManualGenerator("as_List", as_List_body) |
|---|
| 187 | n/a | f.docstring = lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)" |
|---|
| 188 | n/a | functions.append(f) |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | f = Method(Handle, 'as_Resource', (ListHandle, 'lh', InMode)) |
|---|
| 191 | n/a | methods.append(f) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | # Manual generator for CreateCustomList, due to callback ideosyncracies |
|---|
| 194 | n/a | CreateCustomList_body = """\ |
|---|
| 195 | n/a | Rect rView; |
|---|
| 196 | n/a | Rect dataBounds; |
|---|
| 197 | n/a | Point cellSize; |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | PyObject *listDefFunc; |
|---|
| 200 | n/a | ListDefSpec theSpec; |
|---|
| 201 | n/a | WindowPtr theWindow; |
|---|
| 202 | n/a | Boolean drawIt; |
|---|
| 203 | n/a | Boolean hasGrow; |
|---|
| 204 | n/a | Boolean scrollHoriz; |
|---|
| 205 | n/a | Boolean scrollVert; |
|---|
| 206 | n/a | ListHandle outList; |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | if (!PyArg_ParseTuple(_args, "O&O&O&(iO)O&bbbb", |
|---|
| 209 | n/a | PyMac_GetRect, &rView, |
|---|
| 210 | n/a | PyMac_GetRect, &dataBounds, |
|---|
| 211 | n/a | PyMac_GetPoint, &cellSize, |
|---|
| 212 | n/a | &theSpec.defType, &listDefFunc, |
|---|
| 213 | n/a | WinObj_Convert, &theWindow, |
|---|
| 214 | n/a | &drawIt, |
|---|
| 215 | n/a | &hasGrow, |
|---|
| 216 | n/a | &scrollHoriz, |
|---|
| 217 | n/a | &scrollVert)) |
|---|
| 218 | n/a | return NULL; |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | /* Carbon applications use the CreateCustomList API */ |
|---|
| 222 | n/a | theSpec.u.userProc = myListDefFunctionUPP; |
|---|
| 223 | n/a | CreateCustomList(&rView, |
|---|
| 224 | n/a | &dataBounds, |
|---|
| 225 | n/a | cellSize, |
|---|
| 226 | n/a | &theSpec, |
|---|
| 227 | n/a | theWindow, |
|---|
| 228 | n/a | drawIt, |
|---|
| 229 | n/a | hasGrow, |
|---|
| 230 | n/a | scrollHoriz, |
|---|
| 231 | n/a | scrollVert, |
|---|
| 232 | n/a | &outList); |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | _res = ListObj_New(outList); |
|---|
| 236 | n/a | if (_res == NULL) |
|---|
| 237 | n/a | return NULL; |
|---|
| 238 | n/a | Py_INCREF(listDefFunc); |
|---|
| 239 | n/a | ((ListObject*)_res)->ob_ldef_func = listDefFunc; |
|---|
| 240 | n/a | return _res;\ |
|---|
| 241 | n/a | """ |
|---|
| 242 | n/a | |
|---|
| 243 | n/a | f = ManualGenerator("CreateCustomList", CreateCustomList_body); |
|---|
| 244 | n/a | f.docstring = lambda: "(Rect rView, Rect dataBounds, Point cellSize, ListDefSpec theSpec, WindowPtr theWindow, Boolean drawIt, Boolean hasGrow, Boolean scrollHoriz, Boolean scrollVert) -> (ListHandle outList)" |
|---|
| 245 | n/a | module.add(f) |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | # add the populated lists to the generator groups |
|---|
| 248 | n/a | # (in a different wordl the scan program would generate this) |
|---|
| 249 | n/a | for f in functions: module.add(f) |
|---|
| 250 | n/a | for f in methods: object.add(f) |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | # generate output (open the output file as late as possible) |
|---|
| 254 | n/a | SetOutputFileName(OUTPUTFILE) |
|---|
| 255 | n/a | module.generate() |
|---|