The 0.5 release happened on 2/15, not on 2/14. :-)
[python/dscho.git] / Mac / Modules / drag / dragsupport.py
blob64274a6fdf776c30488fe1c8c981ad782b102f94
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).
6 import string
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')
43 AEDesc_ptr = 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;
73 #if 0
74 DragInputUPP dragglue_InputUPP;
75 DragDrawingUPP dragglue_DrawingUPP;
76 #endif
77 """
79 finalstuff = finalstuff + """
80 static pascal OSErr
81 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
82 void *handlerRefCon, DragReference theDrag)
84 PyObject *args, *rv;
85 int i;
87 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
88 if ( args == NULL )
89 return -1;
90 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
91 Py_DECREF(args);
92 if ( rv == NULL ) {
93 fprintf(stderr, "Drag: Exception in TrackingHandler\\n");
94 return -1;
96 i = -1;
97 if ( rv == Py_None )
98 i = 0;
99 else
100 PyArg_Parse(rv, "l", &i);
101 Py_DECREF(rv);
102 return i;
105 static pascal OSErr
106 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
107 DragReference theDrag)
109 PyObject *args, *rv;
110 int i;
112 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
113 if ( args == NULL )
114 return -1;
115 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
116 Py_DECREF(args);
117 if ( rv == NULL ) {
118 fprintf(stderr, "Drag: Exception in ReceiveHandler\\n");
119 return -1;
121 i = -1;
122 if ( rv == Py_None )
123 i = 0;
124 else
125 PyArg_Parse(rv, "l", &i);
126 Py_DECREF(rv);
127 return i;
130 static pascal OSErr
131 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
132 ItemReference theItem, DragReference theDrag)
134 DragObjObject *self = (DragObjObject *)dragSendRefCon;
135 PyObject *args, *rv;
136 int i;
138 if ( self->sendproc == NULL )
139 return -1;
140 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
141 if ( args == NULL )
142 return -1;
143 rv = PyEval_CallObject(self->sendproc, args);
144 Py_DECREF(args);
145 if ( rv == NULL ) {
146 fprintf(stderr, "Drag: Exception in SendDataHandler\\n");
147 return -1;
149 i = -1;
150 if ( rv == Py_None )
151 i = 0;
152 else
153 PyArg_Parse(rv, "l", &i);
154 Py_DECREF(rv);
155 return i;
158 #if 0
159 static pascal OSErr
160 dragglue_Input(Point *mouse, short *modifiers,
161 void *dragSendRefCon, DragReference theDrag)
163 return 0;
166 static pascal OSErr
167 dragglue_Drawing(xxxx
168 void *dragSendRefCon, DragReference theDrag)
170 return 0;
172 #endif
176 variablestuff = """
177 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerProc(dragglue_TrackingHandler);
178 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerProc(dragglue_ReceiveHandler);
179 dragglue_SendDataUPP = NewDragSendDataProc(dragglue_SendData);
180 #if 0
181 dragglue_InputUPP = NewDragInputProc(dragglue_Input);
182 dragglue_DrawingUPP = NewDragDrawingProc(dragglue_Drawing);
183 #endif
184 """
186 class MyObjectDefinition(GlobalObjectDefinition):
187 def outputCheckNewArg(self):
188 Output("""if (itself == NULL) {
189 PyErr_SetString(Drag_Error,"Cannot create null Drag");
190 return NULL;
191 }""")
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
221 functions = []
222 methods = []
223 execfile(INPUTFILE)
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 = """
232 PyObject *callback;
233 WindowPtr theWindow = NULL;
234 OSErr _err;
236 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
237 return NULL;
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);
241 Py_INCREF(Py_None);
242 return Py_None;
244 installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body)
245 module.add(installtracking)
247 installreceive_body = """
248 PyObject *callback;
249 WindowPtr theWindow = NULL;
250 OSErr _err;
252 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
253 return NULL;
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);
257 Py_INCREF(Py_None);
258 return Py_None;
260 installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body)
261 module.add(installreceive)
263 removetracking_body = """
264 WindowPtr theWindow = NULL;
265 OSErr _err;
267 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
268 return NULL;
269 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
270 if (_err != noErr) return PyMac_Error(_err);
271 Py_INCREF(Py_None);
272 return Py_None;
274 removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body)
275 module.add(removetracking)
277 removereceive_body = """
278 WindowPtr theWindow = NULL;
279 OSErr _err;
281 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
282 return NULL;
283 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
284 if (_err != noErr) return PyMac_Error(_err);
285 Py_INCREF(Py_None);
286 return Py_None;
288 removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body)
289 module.add(removereceive)
291 # generate output (open the output file as late as possible)
292 SetOutputFileName(OUTPUTFILE)
293 module.generate()