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
= '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 fprintf(stderr, "Drag: Exception in TrackingHandler\\n");
92 PyArg_Parse(rv, "l", &i);
98 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
99 DragReference theDrag)
104 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
107 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
110 fprintf(stderr, "Drag: Exception in ReceiveHandler\\n");
117 PyArg_Parse(rv, "l", &i);
123 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
124 ItemReference theItem, DragReference theDrag)
126 DragObjObject *self = (DragObjObject *)dragSendRefCon;
130 if ( self->sendproc == NULL )
132 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
135 rv = PyEval_CallObject(self->sendproc, args);
138 fprintf(stderr, "Drag: Exception in SendDataHandler\\n");
145 PyArg_Parse(rv, "l", &i);
152 dragglue_Input(Point *mouse, short *modifiers,
153 void *dragSendRefCon, DragReference theDrag)
159 dragglue_Drawing(xxxx
160 void *dragSendRefCon, DragReference theDrag)
168 initstuff
= initstuff
+ """
169 PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New);
170 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert);
174 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler);
175 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler);
176 dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData);
178 dragglue_InputUPP = NewDragInputUPP(dragglue_Input);
179 dragglue_DrawingUPP = NewDragDrawingUPP(dragglue_Drawing);
183 class MyObjectDefinition(GlobalObjectDefinition
):
184 def outputCheckNewArg(self
):
185 Output("""if (itself == NULL) {
186 PyErr_SetString(Drag_Error,"Cannot create null Drag");
189 def outputFreeIt(self
, itselfname
):
190 ## Output("DisposeDrag(%s);", itselfname)
191 Output("Py_XDECREF(self->sendproc);")
192 ## Output("Py_XDECREF(self->inputproc);")
193 ## Output("Py_XDECREF(self->drawingproc);")
195 def outputStructMembers(self
):
196 GlobalObjectDefinition
.outputStructMembers(self
)
197 Output("PyObject *sendproc;")
198 ## Output("PyObject *inputproc;")
199 ## Output("PyObject *drawingproc;")
201 def outputInitStructMembers(self
):
202 GlobalObjectDefinition
.outputInitStructMembers(self
)
203 Output("it->sendproc = NULL;")
204 ## Output("it->inputproc = NULL;")
205 ## Output("it->drawingproc = NULL;")
208 # Create the generator groups and link them
209 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
, variablestuff
)
210 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
211 module
.addobject(object)
213 # Create the generator classes used to populate the lists
214 Function
= OSErrFunctionGenerator
215 Method
= OSErrMethodGenerator
217 # Create and populate the lists
222 # add the populated lists to the generator groups
223 for f
in functions
: module
.add(f
)
224 for f
in methods
: object.add(f
)
226 # Manual generators for the callbacks
228 installtracking_body
= """
230 WindowPtr theWindow = NULL;
233 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
235 Py_INCREF(callback); /* Cannot decref later, too bad */
236 _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback);
237 if (_err != noErr) return PyMac_Error(_err);
241 installtracking
= ManualGenerator("InstallTrackingHandler", installtracking_body
)
242 module
.add(installtracking
)
244 installreceive_body
= """
246 WindowPtr theWindow = NULL;
249 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
251 Py_INCREF(callback); /* Cannot decref later, too bad */
252 _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback);
253 if (_err != noErr) return PyMac_Error(_err);
257 installreceive
= ManualGenerator("InstallReceiveHandler", installreceive_body
)
258 module
.add(installreceive
)
260 removetracking_body
= """
261 WindowPtr theWindow = NULL;
264 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
266 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
267 if (_err != noErr) return PyMac_Error(_err);
271 removetracking
= ManualGenerator("RemoveTrackingHandler", removetracking_body
)
272 module
.add(removetracking
)
274 removereceive_body
= """
275 WindowPtr theWindow = NULL;
278 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
280 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
281 if (_err != noErr) return PyMac_Error(_err);
285 removereceive
= ManualGenerator("RemoveReceiveHandler", removereceive_body
)
286 module
.add(removereceive
)
288 # generate output (open the output file as late as possible)
289 SetOutputFileName(OUTPUTFILE
)