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
= 'Drag' # The prefix for module-wide routines
15 OBJECTTYPE
= 'DragRef' # 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 DragRef
= OpaqueByValueType(OBJECTTYPE
, OBJECTPREFIX
)
25 DragItemRef
= Type("ItemReference", "l")
27 DragReference
= DragRef
28 ItemReference
= DragItemRef
30 PixMapHandle
= OpaqueByValueType("PixMapHandle", "ResObj")
31 RgnHandle
= OpaqueByValueType("RgnHandle", "ResObj")
32 AEDesc
= OpaqueType('AEDesc')
34 RGBColor
= OpaqueType("RGBColor", "QdRGB")
36 FlavorType
= OSTypeType("FlavorType")
37 DragAttributes
= Type("DragAttributes", "l")
38 DragBehaviors
= Type("DragBehaviors", "l")
39 DragImageFlags
= Type("DragImageFlags", "l")
40 DragImageTranslucency
= Type("DragImageTranslucency", "l")
41 DragRegionMessage
= Type("DragRegionMessage", "h")
42 ZoomAcceleration
= Type("ZoomAcceleration", "h")
43 FlavorFlags
= Type("FlavorFlags", "l")
44 DragTrackingMessage
= Type("DragTrackingMessage", "h")
46 includestuff
= includestuff
+ """
47 #ifdef WITHOUT_FRAMEWORKS
50 #include <Carbon/Carbon.h>
53 /* Callback glue routines */
54 DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
55 DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
56 DragSendDataUPP dragglue_SendDataUPP;
58 DragInputUPP dragglue_InputUPP;
59 DragDrawingUPP dragglue_DrawingUPP;
62 #ifdef USE_TOOLBOX_OBJECT_GLUE
63 extern PyObject *_DragObj_New(DragRef);
64 extern int _DragObj_Convert(PyObject *, DragRef *);
66 #define DragObj_New _DragObj_New
67 #define DragObj_Convert _DragObj_Convert
71 finalstuff
= finalstuff
+ """
73 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
74 void *handlerRefCon, DragReference theDrag)
79 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
82 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
85 PySys_WriteStderr("Drag: Exception in TrackingHandler\\n");
93 PyArg_Parse(rv, "l", &i);
99 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
100 DragReference theDrag)
105 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
108 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
111 PySys_WriteStderr("Drag: Exception in ReceiveHandler\\n");
119 PyArg_Parse(rv, "l", &i);
125 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
126 ItemReference theItem, DragReference theDrag)
128 DragObjObject *self = (DragObjObject *)dragSendRefCon;
132 if ( self->sendproc == NULL )
134 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
137 rv = PyEval_CallObject(self->sendproc, args);
140 PySys_WriteStderr("Drag: Exception in SendDataHandler\\n");
148 PyArg_Parse(rv, "l", &i);
155 dragglue_Input(Point *mouse, short *modifiers,
156 void *dragSendRefCon, DragReference theDrag)
162 dragglue_Drawing(xxxx
163 void *dragSendRefCon, DragReference theDrag)
171 initstuff
= initstuff
+ """
172 PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New);
173 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert);
177 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler);
178 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler);
179 dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData);
181 dragglue_InputUPP = NewDragInputUPP(dragglue_Input);
182 dragglue_DrawingUPP = NewDragDrawingUPP(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
= OSErrWeakLinkFunctionGenerator
218 Method
= OSErrWeakLinkMethodGenerator
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);
245 installtracking
= ManualGenerator("InstallTrackingHandler", installtracking_body
)
246 module
.add(installtracking
)
248 installreceive_body
= """
250 WindowPtr theWindow = NULL;
253 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
255 Py_INCREF(callback); /* Cannot decref later, too bad */
256 _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback);
257 if (_err != noErr) return PyMac_Error(_err);
262 installreceive
= ManualGenerator("InstallReceiveHandler", installreceive_body
)
263 module
.add(installreceive
)
265 removetracking_body
= """
266 WindowPtr theWindow = NULL;
269 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
271 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
272 if (_err != noErr) return PyMac_Error(_err);
277 removetracking
= ManualGenerator("RemoveTrackingHandler", removetracking_body
)
278 module
.add(removetracking
)
280 removereceive_body
= """
281 WindowPtr theWindow = NULL;
284 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
286 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
287 if (_err != noErr) return PyMac_Error(_err);
292 removereceive
= ManualGenerator("RemoveReceiveHandler", removereceive_body
)
293 module
.add(removereceive
)
295 # generate output (open the output file as late as possible)
296 SetOutputFileName(OUTPUTFILE
)