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
= 'Drag.h' # The Apple header file
10 MODNAME
= 'Drag' # The name of the module
11 OBJECTNAME
= 'DragObj' # The basic name of the objects used here
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX
= MODNAME
# The prefix for module-wide routines
15 OBJECTTYPE
= 'DragReference' # The C type used to represent them
16 OBJECTPREFIX
= MODPREFIX
+ 'Obj' # The prefix for object methods
17 INPUTFILE
= string
.lower(MODPREFIX
) + 'gen.py' # The file generated by the scanner
18 OUTPUTFILE
= MODNAME
+ "module.c" # The file generated by this program
20 from macsupport
import *
22 # Create the type objects
24 DragReference
= OpaqueByValueType(OBJECTTYPE
, OBJECTPREFIX
)
26 ##CCTabHandle = OpaqueByValueType("CCTabHandle", "ResObj")
27 ##AuxCtlHandle = OpaqueByValueType("AuxCtlHandle", "ResObj")
28 ##ControlPartCode = Type("ControlPartCode", "h")
29 ##DragConstraint = Type("DragConstraint", "h")
30 ##ControlVariant = Type("ControlVariant", "h")
31 ##IconTransformType = Type("IconTransformType", "h")
32 ##ControlButtonGraphicAlignment = Type("ControlButtonGraphicAlignment", "h")
33 ##ControlButtonTextAlignment = Type("ControlButtonTextAlignment", "h")
34 ##ControlButtonTextPlacement = Type("ControlButtonTextPlacement", "h")
35 ##ControlContentType = Type("ControlContentType", "h")
36 ##ControlFocusPart = Type("ControlFocusPart", "h")
38 ##ControlFontStyleRec = OpaqueType('ControlFontStyleRec', 'ControlFontStyle')
39 ##ControlFontStyleRec_ptr = ControlFontStyleRec
40 PixMapHandle
= OpaqueByValueType("PixMapHandle", "ResObj")
41 RgnHandle
= OpaqueByValueType("RgnHandle", "ResObj")
42 AEDesc
= OpaqueType('AEDesc')
44 RGBColor
= OpaqueType("RGBColor", "QdRGB")
46 ItemReference
= Type("ItemReference", "l")
47 FlavorType
= OSTypeType("FlavorType")
48 DragAttributes
= Type("DragAttributes", "l")
49 DragImageFlags
= Type("DragImageFlags", "l")
50 DragImageTranslucency
= Type("DragImageTranslucency", "l")
51 DragRegionMessage
= Type("DragRegionMessage", "h")
52 ZoomAcceleration
= Type("ZoomAcceleration", "h")
53 FlavorFlags
= Type("FlavorFlags", "l")
54 DragTrackingMessage
= Type("DragTrackingMessage", "h")
56 includestuff
= includestuff
+ """
57 #include <%s>""" % MACHEADERFILE
+ """
59 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
60 /* Exported by Qdmodule.c: */
61 extern PyObject *QdRGB_New(RGBColor *);
62 extern int QdRGB_Convert(PyObject *, RGBColor *);
65 /* Exported by AEModule.c: */
66 extern PyObject *AEDesc_New(AppleEvent *);
67 extern int AEDesc_Convert(PyObject *, AppleEvent *);
69 /* Callback glue routines */
70 DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
71 DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
72 DragSendDataUPP dragglue_SendDataUPP;
74 DragInputUPP dragglue_InputUPP;
75 DragDrawingUPP dragglue_DrawingUPP;
79 finalstuff
= finalstuff
+ """
81 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
82 void *handlerRefCon, DragReference theDrag)
87 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
90 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
93 fprintf(stderr, "Drag: Exception in TrackingHandler\\n");
100 PyArg_Parse(rv, "l", &i);
106 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
107 DragReference theDrag)
112 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
115 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
118 fprintf(stderr, "Drag: Exception in ReceiveHandler\\n");
125 PyArg_Parse(rv, "l", &i);
131 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
132 ItemReference theItem, DragReference theDrag)
134 DragObjObject *self = (DragObjObject *)dragSendRefCon;
138 if ( self->sendproc == NULL )
140 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
143 rv = PyEval_CallObject(self->sendproc, args);
146 fprintf(stderr, "Drag: Exception in SendDataHandler\\n");
153 PyArg_Parse(rv, "l", &i);
160 dragglue_Input(Point *mouse, short *modifiers,
161 void *dragSendRefCon, DragReference theDrag)
167 dragglue_Drawing(xxxx
168 void *dragSendRefCon, DragReference theDrag)
177 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerProc(dragglue_TrackingHandler);
178 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerProc(dragglue_ReceiveHandler);
179 dragglue_SendDataUPP = NewDragSendDataProc(dragglue_SendData);
181 dragglue_InputUPP = NewDragInputProc(dragglue_Input);
182 dragglue_DrawingUPP = NewDragDrawingProc(dragglue_Drawing);
186 class MyObjectDefinition(GlobalObjectDefinition
):
187 def outputCheckNewArg(self
):
188 Output("""if (itself == NULL) {
189 PyErr_SetString(Drag_Error,"Cannot create null Drag");
192 def outputFreeIt(self
, itselfname
):
193 ## Output("DisposeDrag(%s);", itselfname)
194 Output("Py_XDECREF(self->sendproc);")
195 ## Output("Py_XDECREF(self->inputproc);")
196 ## Output("Py_XDECREF(self->drawingproc);")
198 def outputStructMembers(self
):
199 GlobalObjectDefinition
.outputStructMembers(self
)
200 Output("PyObject *sendproc;")
201 ## Output("PyObject *inputproc;")
202 ## Output("PyObject *drawingproc;")
204 def outputInitStructMembers(self
):
205 GlobalObjectDefinition
.outputInitStructMembers(self
)
206 Output("it->sendproc = NULL;")
207 ## Output("it->inputproc = NULL;")
208 ## Output("it->drawingproc = NULL;")
211 # Create the generator groups and link them
212 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
, variablestuff
)
213 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
214 module
.addobject(object)
216 # Create the generator classes used to populate the lists
217 Function
= OSErrFunctionGenerator
218 Method
= OSErrMethodGenerator
220 # Create and populate the lists
225 # add the populated lists to the generator groups
226 for f
in functions
: module
.add(f
)
227 for f
in methods
: object.add(f
)
229 # Manual generators for the callbacks
231 installtracking_body
= """
233 WindowPtr theWindow = NULL;
236 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
238 Py_INCREF(callback); /* Cannot decref later, too bad */
239 _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback);
240 if (_err != noErr) return PyMac_Error(_err);
244 installtracking
= ManualGenerator("InstallTrackingHandler", installtracking_body
)
245 module
.add(installtracking
)
247 installreceive_body
= """
249 WindowPtr theWindow = NULL;
252 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
254 Py_INCREF(callback); /* Cannot decref later, too bad */
255 _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback);
256 if (_err != noErr) return PyMac_Error(_err);
260 installreceive
= ManualGenerator("InstallReceiveHandler", installreceive_body
)
261 module
.add(installreceive
)
263 removetracking_body
= """
264 WindowPtr theWindow = NULL;
267 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
269 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
270 if (_err != noErr) return PyMac_Error(_err);
274 removetracking
= ManualGenerator("RemoveTrackingHandler", removetracking_body
)
275 module
.add(removetracking
)
277 removereceive_body
= """
278 WindowPtr theWindow = NULL;
281 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
283 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
284 if (_err != noErr) return PyMac_Error(_err);
288 removereceive
= ManualGenerator("RemoveReceiveHandler", removereceive_body
)
289 module
.add(removereceive
)
291 # generate output (open the output file as late as possible)
292 SetOutputFileName(OUTPUTFILE
)