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 #include <%s>""" % MACHEADERFILE
+ """
49 /* Callback glue routines */
50 DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
51 DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
52 DragSendDataUPP dragglue_SendDataUPP;
54 DragInputUPP dragglue_InputUPP;
55 DragDrawingUPP dragglue_DrawingUPP;
59 finalstuff
= finalstuff
+ """
61 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
62 void *handlerRefCon, DragReference theDrag)
67 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
70 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
73 fprintf(stderr, "Drag: Exception in TrackingHandler\\n");
80 PyArg_Parse(rv, "l", &i);
86 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
87 DragReference theDrag)
92 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
95 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
98 fprintf(stderr, "Drag: Exception in ReceiveHandler\\n");
105 PyArg_Parse(rv, "l", &i);
111 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
112 ItemReference theItem, DragReference theDrag)
114 DragObjObject *self = (DragObjObject *)dragSendRefCon;
118 if ( self->sendproc == NULL )
120 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
123 rv = PyEval_CallObject(self->sendproc, args);
126 fprintf(stderr, "Drag: Exception in SendDataHandler\\n");
133 PyArg_Parse(rv, "l", &i);
140 dragglue_Input(Point *mouse, short *modifiers,
141 void *dragSendRefCon, DragReference theDrag)
147 dragglue_Drawing(xxxx
148 void *dragSendRefCon, DragReference theDrag)
157 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerProc(dragglue_TrackingHandler);
158 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerProc(dragglue_ReceiveHandler);
159 dragglue_SendDataUPP = NewDragSendDataProc(dragglue_SendData);
161 dragglue_InputUPP = NewDragInputProc(dragglue_Input);
162 dragglue_DrawingUPP = NewDragDrawingProc(dragglue_Drawing);
166 class MyObjectDefinition(GlobalObjectDefinition
):
167 def outputCheckNewArg(self
):
168 Output("""if (itself == NULL) {
169 PyErr_SetString(Drag_Error,"Cannot create null Drag");
172 def outputFreeIt(self
, itselfname
):
173 ## Output("DisposeDrag(%s);", itselfname)
174 Output("Py_XDECREF(self->sendproc);")
175 ## Output("Py_XDECREF(self->inputproc);")
176 ## Output("Py_XDECREF(self->drawingproc);")
178 def outputStructMembers(self
):
179 GlobalObjectDefinition
.outputStructMembers(self
)
180 Output("PyObject *sendproc;")
181 ## Output("PyObject *inputproc;")
182 ## Output("PyObject *drawingproc;")
184 def outputInitStructMembers(self
):
185 GlobalObjectDefinition
.outputInitStructMembers(self
)
186 Output("it->sendproc = NULL;")
187 ## Output("it->inputproc = NULL;")
188 ## Output("it->drawingproc = NULL;")
191 # Create the generator groups and link them
192 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
, variablestuff
)
193 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
194 module
.addobject(object)
196 # Create the generator classes used to populate the lists
197 Function
= OSErrFunctionGenerator
198 Method
= OSErrMethodGenerator
200 # Create and populate the lists
205 # add the populated lists to the generator groups
206 for f
in functions
: module
.add(f
)
207 for f
in methods
: object.add(f
)
209 # Manual generators for the callbacks
211 installtracking_body
= """
213 WindowPtr theWindow = NULL;
216 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
218 Py_INCREF(callback); /* Cannot decref later, too bad */
219 _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback);
220 if (_err != noErr) return PyMac_Error(_err);
224 installtracking
= ManualGenerator("InstallTrackingHandler", installtracking_body
)
225 module
.add(installtracking
)
227 installreceive_body
= """
229 WindowPtr theWindow = NULL;
232 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
234 Py_INCREF(callback); /* Cannot decref later, too bad */
235 _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback);
236 if (_err != noErr) return PyMac_Error(_err);
240 installreceive
= ManualGenerator("InstallReceiveHandler", installreceive_body
)
241 module
.add(installreceive
)
243 removetracking_body
= """
244 WindowPtr theWindow = NULL;
247 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
249 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
250 if (_err != noErr) return PyMac_Error(_err);
254 removetracking
= ManualGenerator("RemoveTrackingHandler", removetracking_body
)
255 module
.add(removetracking
)
257 removereceive_body
= """
258 WindowPtr theWindow = NULL;
261 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
263 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
264 if (_err != noErr) return PyMac_Error(_err);
268 removereceive
= ManualGenerator("RemoveReceiveHandler", removereceive_body
)
269 module
.add(removereceive
)
271 # generate output (open the output file as late as possible)
272 SetOutputFileName(OUTPUTFILE
)