| 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 = 'Drag.h' # The Apple header file |
|---|
| 10 | n/a | MODNAME = '_Drag' # The name of the module |
|---|
| 11 | n/a | OBJECTNAME = 'DragObj' # The basic name of the objects used here |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | # The following is *usually* unchanged but may still require tuning |
|---|
| 14 | n/a | MODPREFIX = 'Drag' # The prefix for module-wide routines |
|---|
| 15 | n/a | OBJECTTYPE = 'DragRef' # The C type used to represent them |
|---|
| 16 | n/a | OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods |
|---|
| 17 | n/a | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|---|
| 18 | n/a | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | from macsupport import * |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | # Create the type objects |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | DragRef = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
|---|
| 25 | n/a | DragItemRef = Type("ItemReference", "l") |
|---|
| 26 | n/a | # Old names |
|---|
| 27 | n/a | DragReference = DragRef |
|---|
| 28 | n/a | ItemReference = DragItemRef |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj") |
|---|
| 31 | n/a | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
|---|
| 32 | n/a | AEDesc = OpaqueType('AEDesc') |
|---|
| 33 | n/a | AEDesc_ptr = AEDesc |
|---|
| 34 | n/a | RGBColor = OpaqueType("RGBColor", "QdRGB") |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | FlavorType = OSTypeType("FlavorType") |
|---|
| 37 | n/a | DragAttributes = Type("DragAttributes", "l") |
|---|
| 38 | n/a | DragBehaviors = Type("DragBehaviors", "l") |
|---|
| 39 | n/a | DragImageFlags = Type("DragImageFlags", "l") |
|---|
| 40 | n/a | DragImageTranslucency = Type("DragImageTranslucency", "l") |
|---|
| 41 | n/a | DragRegionMessage = Type("DragRegionMessage", "h") |
|---|
| 42 | n/a | ZoomAcceleration = Type("ZoomAcceleration", "h") |
|---|
| 43 | n/a | FlavorFlags = Type("FlavorFlags", "l") |
|---|
| 44 | n/a | DragTrackingMessage = Type("DragTrackingMessage", "h") |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | includestuff = includestuff + """ |
|---|
| 47 | n/a | #include <Carbon/Carbon.h> |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | /* Callback glue routines */ |
|---|
| 50 | n/a | DragTrackingHandlerUPP dragglue_TrackingHandlerUPP; |
|---|
| 51 | n/a | DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP; |
|---|
| 52 | n/a | DragSendDataUPP dragglue_SendDataUPP; |
|---|
| 53 | n/a | #if 0 |
|---|
| 54 | n/a | DragInputUPP dragglue_InputUPP; |
|---|
| 55 | n/a | DragDrawingUPP dragglue_DrawingUPP; |
|---|
| 56 | n/a | #endif |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 59 | n/a | extern PyObject *_DragObj_New(DragRef); |
|---|
| 60 | n/a | extern int _DragObj_Convert(PyObject *, DragRef *); |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | #define DragObj_New _DragObj_New |
|---|
| 63 | n/a | #define DragObj_Convert _DragObj_Convert |
|---|
| 64 | n/a | #endif |
|---|
| 65 | n/a | """ |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | finalstuff = finalstuff + """ |
|---|
| 68 | n/a | static pascal OSErr |
|---|
| 69 | n/a | dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow, |
|---|
| 70 | n/a | void *handlerRefCon, DragReference theDrag) |
|---|
| 71 | n/a | { |
|---|
| 72 | n/a | PyObject *args, *rv; |
|---|
| 73 | n/a | int i; |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow); |
|---|
| 76 | n/a | if ( args == NULL ) |
|---|
| 77 | n/a | return -1; |
|---|
| 78 | n/a | rv = PyEval_CallObject((PyObject *)handlerRefCon, args); |
|---|
| 79 | n/a | Py_DECREF(args); |
|---|
| 80 | n/a | if ( rv == NULL ) { |
|---|
| 81 | n/a | PySys_WriteStderr("Drag: Exception in TrackingHandler\\n"); |
|---|
| 82 | n/a | PyErr_Print(); |
|---|
| 83 | n/a | return -1; |
|---|
| 84 | n/a | } |
|---|
| 85 | n/a | i = -1; |
|---|
| 86 | n/a | if ( rv == Py_None ) |
|---|
| 87 | n/a | i = 0; |
|---|
| 88 | n/a | else |
|---|
| 89 | n/a | PyArg_Parse(rv, "l", &i); |
|---|
| 90 | n/a | Py_DECREF(rv); |
|---|
| 91 | n/a | return i; |
|---|
| 92 | n/a | } |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | static pascal OSErr |
|---|
| 95 | n/a | dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon, |
|---|
| 96 | n/a | DragReference theDrag) |
|---|
| 97 | n/a | { |
|---|
| 98 | n/a | PyObject *args, *rv; |
|---|
| 99 | n/a | int i; |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow); |
|---|
| 102 | n/a | if ( args == NULL ) |
|---|
| 103 | n/a | return -1; |
|---|
| 104 | n/a | rv = PyEval_CallObject((PyObject *)handlerRefCon, args); |
|---|
| 105 | n/a | Py_DECREF(args); |
|---|
| 106 | n/a | if ( rv == NULL ) { |
|---|
| 107 | n/a | PySys_WriteStderr("Drag: Exception in ReceiveHandler\\n"); |
|---|
| 108 | n/a | PyErr_Print(); |
|---|
| 109 | n/a | return -1; |
|---|
| 110 | n/a | } |
|---|
| 111 | n/a | i = -1; |
|---|
| 112 | n/a | if ( rv == Py_None ) |
|---|
| 113 | n/a | i = 0; |
|---|
| 114 | n/a | else |
|---|
| 115 | n/a | PyArg_Parse(rv, "l", &i); |
|---|
| 116 | n/a | Py_DECREF(rv); |
|---|
| 117 | n/a | return i; |
|---|
| 118 | n/a | } |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | static pascal OSErr |
|---|
| 121 | n/a | dragglue_SendData(FlavorType theType, void *dragSendRefCon, |
|---|
| 122 | n/a | ItemReference theItem, DragReference theDrag) |
|---|
| 123 | n/a | { |
|---|
| 124 | n/a | DragObjObject *self = (DragObjObject *)dragSendRefCon; |
|---|
| 125 | n/a | PyObject *args, *rv; |
|---|
| 126 | n/a | int i; |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | if ( self->sendproc == NULL ) |
|---|
| 129 | n/a | return -1; |
|---|
| 130 | n/a | args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem); |
|---|
| 131 | n/a | if ( args == NULL ) |
|---|
| 132 | n/a | return -1; |
|---|
| 133 | n/a | rv = PyEval_CallObject(self->sendproc, args); |
|---|
| 134 | n/a | Py_DECREF(args); |
|---|
| 135 | n/a | if ( rv == NULL ) { |
|---|
| 136 | n/a | PySys_WriteStderr("Drag: Exception in SendDataHandler\\n"); |
|---|
| 137 | n/a | PyErr_Print(); |
|---|
| 138 | n/a | return -1; |
|---|
| 139 | n/a | } |
|---|
| 140 | n/a | i = -1; |
|---|
| 141 | n/a | if ( rv == Py_None ) |
|---|
| 142 | n/a | i = 0; |
|---|
| 143 | n/a | else |
|---|
| 144 | n/a | PyArg_Parse(rv, "l", &i); |
|---|
| 145 | n/a | Py_DECREF(rv); |
|---|
| 146 | n/a | return i; |
|---|
| 147 | n/a | } |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | #if 0 |
|---|
| 150 | n/a | static pascal OSErr |
|---|
| 151 | n/a | dragglue_Input(Point *mouse, short *modifiers, |
|---|
| 152 | n/a | void *dragSendRefCon, DragReference theDrag) |
|---|
| 153 | n/a | { |
|---|
| 154 | n/a | return 0; |
|---|
| 155 | n/a | } |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | static pascal OSErr |
|---|
| 158 | n/a | dragglue_Drawing(xxxx |
|---|
| 159 | n/a | void *dragSendRefCon, DragReference theDrag) |
|---|
| 160 | n/a | { |
|---|
| 161 | n/a | return 0; |
|---|
| 162 | n/a | } |
|---|
| 163 | n/a | #endif |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | """ |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | initstuff = initstuff + """ |
|---|
| 168 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New); |
|---|
| 169 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert); |
|---|
| 170 | n/a | """ |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | variablestuff = """ |
|---|
| 173 | n/a | dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler); |
|---|
| 174 | n/a | dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler); |
|---|
| 175 | n/a | dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData); |
|---|
| 176 | n/a | #if 0 |
|---|
| 177 | n/a | dragglue_InputUPP = NewDragInputUPP(dragglue_Input); |
|---|
| 178 | n/a | dragglue_DrawingUPP = NewDragDrawingUPP(dragglue_Drawing); |
|---|
| 179 | n/a | #endif |
|---|
| 180 | n/a | """ |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 183 | n/a | def outputCheckNewArg(self): |
|---|
| 184 | n/a | Output("""if (itself == NULL) { |
|---|
| 185 | n/a | PyErr_SetString(Drag_Error,"Cannot create null Drag"); |
|---|
| 186 | n/a | return NULL; |
|---|
| 187 | n/a | }""") |
|---|
| 188 | n/a | def outputFreeIt(self, itselfname): |
|---|
| 189 | n/a | ## Output("DisposeDrag(%s);", itselfname) |
|---|
| 190 | n/a | Output("Py_XDECREF(self->sendproc);") |
|---|
| 191 | n/a | ## Output("Py_XDECREF(self->inputproc);") |
|---|
| 192 | n/a | ## Output("Py_XDECREF(self->drawingproc);") |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | def outputStructMembers(self): |
|---|
| 195 | n/a | GlobalObjectDefinition.outputStructMembers(self) |
|---|
| 196 | n/a | Output("PyObject *sendproc;") |
|---|
| 197 | n/a | ## Output("PyObject *inputproc;") |
|---|
| 198 | n/a | ## Output("PyObject *drawingproc;") |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | def outputInitStructMembers(self): |
|---|
| 201 | n/a | GlobalObjectDefinition.outputInitStructMembers(self) |
|---|
| 202 | n/a | Output("it->sendproc = NULL;") |
|---|
| 203 | n/a | ## Output("it->inputproc = NULL;") |
|---|
| 204 | n/a | ## Output("it->drawingproc = NULL;") |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | # Create the generator groups and link them |
|---|
| 208 | n/a | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff) |
|---|
| 209 | n/a | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|---|
| 210 | n/a | module.addobject(object) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | # Create the generator classes used to populate the lists |
|---|
| 213 | n/a | Function = OSErrWeakLinkFunctionGenerator |
|---|
| 214 | n/a | Method = OSErrWeakLinkMethodGenerator |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | # Create and populate the lists |
|---|
| 217 | n/a | functions = [] |
|---|
| 218 | n/a | methods = [] |
|---|
| 219 | n/a | execfile(INPUTFILE) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | # add the populated lists to the generator groups |
|---|
| 222 | n/a | for f in functions: module.add(f) |
|---|
| 223 | n/a | for f in methods: object.add(f) |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | # Manual generators for the callbacks |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | installtracking_body = """ |
|---|
| 228 | n/a | PyObject *callback; |
|---|
| 229 | n/a | WindowPtr theWindow = NULL; |
|---|
| 230 | n/a | OSErr _err; |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) ) |
|---|
| 233 | n/a | return NULL; |
|---|
| 234 | n/a | Py_INCREF(callback); /* Cannot decref later, too bad */ |
|---|
| 235 | n/a | _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback); |
|---|
| 236 | n/a | if (_err != noErr) return PyMac_Error(_err); |
|---|
| 237 | n/a | Py_INCREF(Py_None); |
|---|
| 238 | n/a | _res = Py_None; |
|---|
| 239 | n/a | return _res; |
|---|
| 240 | n/a | """ |
|---|
| 241 | n/a | installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body) |
|---|
| 242 | n/a | module.add(installtracking) |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | installreceive_body = """ |
|---|
| 245 | n/a | PyObject *callback; |
|---|
| 246 | n/a | WindowPtr theWindow = NULL; |
|---|
| 247 | n/a | OSErr _err; |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) ) |
|---|
| 250 | n/a | return NULL; |
|---|
| 251 | n/a | Py_INCREF(callback); /* Cannot decref later, too bad */ |
|---|
| 252 | n/a | _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback); |
|---|
| 253 | n/a | if (_err != noErr) return PyMac_Error(_err); |
|---|
| 254 | n/a | Py_INCREF(Py_None); |
|---|
| 255 | n/a | _res = Py_None; |
|---|
| 256 | n/a | return _res; |
|---|
| 257 | n/a | """ |
|---|
| 258 | n/a | installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body) |
|---|
| 259 | n/a | module.add(installreceive) |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | removetracking_body = """ |
|---|
| 262 | n/a | WindowPtr theWindow = NULL; |
|---|
| 263 | n/a | OSErr _err; |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) ) |
|---|
| 266 | n/a | return NULL; |
|---|
| 267 | n/a | _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow); |
|---|
| 268 | n/a | if (_err != noErr) return PyMac_Error(_err); |
|---|
| 269 | n/a | Py_INCREF(Py_None); |
|---|
| 270 | n/a | _res = Py_None; |
|---|
| 271 | n/a | return _res; |
|---|
| 272 | n/a | """ |
|---|
| 273 | n/a | removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body) |
|---|
| 274 | n/a | module.add(removetracking) |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | removereceive_body = """ |
|---|
| 277 | n/a | WindowPtr theWindow = NULL; |
|---|
| 278 | n/a | OSErr _err; |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) ) |
|---|
| 281 | n/a | return NULL; |
|---|
| 282 | n/a | _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow); |
|---|
| 283 | n/a | if (_err != noErr) return PyMac_Error(_err); |
|---|
| 284 | n/a | Py_INCREF(Py_None); |
|---|
| 285 | n/a | _res = Py_None; |
|---|
| 286 | n/a | return _res; |
|---|
| 287 | n/a | """ |
|---|
| 288 | n/a | removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body) |
|---|
| 289 | n/a | module.add(removereceive) |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | # generate output (open the output file as late as possible) |
|---|
| 292 | n/a | SetOutputFileName(OUTPUTFILE) |
|---|
| 293 | n/a | module.generate() |
|---|