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 /* Callback glue routines */
60 DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
61 DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
62 DragSendDataUPP dragglue_SendDataUPP;
64 DragInputUPP dragglue_InputUPP;
65 DragDrawingUPP dragglue_DrawingUPP;
69 finalstuff
= finalstuff
+ """
71 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
72 void *handlerRefCon, DragReference theDrag)
77 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
80 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
83 fprintf(stderr, "Drag: Exception in TrackingHandler\\n");
90 PyArg_Parse(rv, "l", &i);
96 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
97 DragReference theDrag)
102 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
105 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
108 fprintf(stderr, "Drag: Exception in ReceiveHandler\\n");
115 PyArg_Parse(rv, "l", &i);
121 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
122 ItemReference theItem, DragReference theDrag)
124 DragObjObject *self = (DragObjObject *)dragSendRefCon;
128 if ( self->sendproc == NULL )
130 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
133 rv = PyEval_CallObject(self->sendproc, args);
136 fprintf(stderr, "Drag: Exception in SendDataHandler\\n");
143 PyArg_Parse(rv, "l", &i);
150 dragglue_Input(Point *mouse, short *modifiers,
151 void *dragSendRefCon, DragReference theDrag)
157 dragglue_Drawing(xxxx
158 void *dragSendRefCon, DragReference theDrag)
167 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerProc(dragglue_TrackingHandler);
168 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerProc(dragglue_ReceiveHandler);
169 dragglue_SendDataUPP = NewDragSendDataProc(dragglue_SendData);
171 dragglue_InputUPP = NewDragInputProc(dragglue_Input);
172 dragglue_DrawingUPP = NewDragDrawingProc(dragglue_Drawing);
176 class MyObjectDefinition(GlobalObjectDefinition
):
177 def outputCheckNewArg(self
):
178 Output("""if (itself == NULL) {
179 PyErr_SetString(Drag_Error,"Cannot create null Drag");
182 def outputFreeIt(self
, itselfname
):
183 ## Output("DisposeDrag(%s);", itselfname)
184 Output("Py_XDECREF(self->sendproc);")
185 ## Output("Py_XDECREF(self->inputproc);")
186 ## Output("Py_XDECREF(self->drawingproc);")
188 def outputStructMembers(self
):
189 GlobalObjectDefinition
.outputStructMembers(self
)
190 Output("PyObject *sendproc;")
191 ## Output("PyObject *inputproc;")
192 ## Output("PyObject *drawingproc;")
194 def outputInitStructMembers(self
):
195 GlobalObjectDefinition
.outputInitStructMembers(self
)
196 Output("it->sendproc = NULL;")
197 ## Output("it->inputproc = NULL;")
198 ## Output("it->drawingproc = NULL;")
201 # Create the generator groups and link them
202 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
, variablestuff
)
203 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
204 module
.addobject(object)
206 # Create the generator classes used to populate the lists
207 Function
= OSErrFunctionGenerator
208 Method
= OSErrMethodGenerator
210 # Create and populate the lists
215 # add the populated lists to the generator groups
216 for f
in functions
: module
.add(f
)
217 for f
in methods
: object.add(f
)
219 # Manual generators for the callbacks
221 installtracking_body
= """
223 WindowPtr theWindow = NULL;
226 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
228 Py_INCREF(callback); /* Cannot decref later, too bad */
229 _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback);
230 if (_err != noErr) return PyMac_Error(_err);
234 installtracking
= ManualGenerator("InstallTrackingHandler", installtracking_body
)
235 module
.add(installtracking
)
237 installreceive_body
= """
239 WindowPtr theWindow = NULL;
242 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
244 Py_INCREF(callback); /* Cannot decref later, too bad */
245 _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback);
246 if (_err != noErr) return PyMac_Error(_err);
250 installreceive
= ManualGenerator("InstallReceiveHandler", installreceive_body
)
251 module
.add(installreceive
)
253 removetracking_body
= """
254 WindowPtr theWindow = NULL;
257 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
259 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
260 if (_err != noErr) return PyMac_Error(_err);
264 removetracking
= ManualGenerator("RemoveTrackingHandler", removetracking_body
)
265 module
.add(removetracking
)
267 removereceive_body
= """
268 WindowPtr theWindow = NULL;
271 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
273 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
274 if (_err != noErr) return PyMac_Error(_err);
278 removereceive
= ManualGenerator("RemoveReceiveHandler", removereceive_body
)
279 module
.add(removereceive
)
281 # generate output (open the output file as late as possible)
282 SetOutputFileName(OUTPUTFILE
)