This commit was manufactured by cvs2svn to create tag 'r223c1'.
[python/dscho.git] / Mac / Modules / drag / dragsupport.py
blob1eaa28e0a29332eaf69d8900c631649f006ababe
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 = '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")
26 # Old names
27 DragReference = DragRef
28 ItemReference = DragItemRef
30 PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj")
31 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
32 AEDesc = OpaqueType('AEDesc')
33 AEDesc_ptr = 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
48 #include <Drag.h>
49 #else
50 #include <Carbon/Carbon.h>
51 #endif
53 /* Callback glue routines */
54 DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
55 DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
56 DragSendDataUPP dragglue_SendDataUPP;
57 #if 0
58 DragInputUPP dragglue_InputUPP;
59 DragDrawingUPP dragglue_DrawingUPP;
60 #endif
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
68 #endif
69 """
71 finalstuff = finalstuff + """
72 static pascal OSErr
73 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
74 void *handlerRefCon, DragReference theDrag)
76 PyObject *args, *rv;
77 int i;
79 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
80 if ( args == NULL )
81 return -1;
82 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
83 Py_DECREF(args);
84 if ( rv == NULL ) {
85 PySys_WriteStderr("Drag: Exception in TrackingHandler\\n");
86 PyErr_Print();
87 return -1;
89 i = -1;
90 if ( rv == Py_None )
91 i = 0;
92 else
93 PyArg_Parse(rv, "l", &i);
94 Py_DECREF(rv);
95 return i;
98 static pascal OSErr
99 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
100 DragReference theDrag)
102 PyObject *args, *rv;
103 int i;
105 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
106 if ( args == NULL )
107 return -1;
108 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
109 Py_DECREF(args);
110 if ( rv == NULL ) {
111 PySys_WriteStderr("Drag: Exception in ReceiveHandler\\n");
112 PyErr_Print();
113 return -1;
115 i = -1;
116 if ( rv == Py_None )
117 i = 0;
118 else
119 PyArg_Parse(rv, "l", &i);
120 Py_DECREF(rv);
121 return i;
124 static pascal OSErr
125 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
126 ItemReference theItem, DragReference theDrag)
128 DragObjObject *self = (DragObjObject *)dragSendRefCon;
129 PyObject *args, *rv;
130 int i;
132 if ( self->sendproc == NULL )
133 return -1;
134 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
135 if ( args == NULL )
136 return -1;
137 rv = PyEval_CallObject(self->sendproc, args);
138 Py_DECREF(args);
139 if ( rv == NULL ) {
140 PySys_WriteStderr("Drag: Exception in SendDataHandler\\n");
141 PyErr_Print();
142 return -1;
144 i = -1;
145 if ( rv == Py_None )
146 i = 0;
147 else
148 PyArg_Parse(rv, "l", &i);
149 Py_DECREF(rv);
150 return i;
153 #if 0
154 static pascal OSErr
155 dragglue_Input(Point *mouse, short *modifiers,
156 void *dragSendRefCon, DragReference theDrag)
158 return 0;
161 static pascal OSErr
162 dragglue_Drawing(xxxx
163 void *dragSendRefCon, DragReference theDrag)
165 return 0;
167 #endif
171 initstuff = initstuff + """
172 PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New);
173 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert);
176 variablestuff = """
177 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler);
178 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler);
179 dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData);
180 #if 0
181 dragglue_InputUPP = NewDragInputUPP(dragglue_Input);
182 dragglue_DrawingUPP = NewDragDrawingUPP(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 = OSErrWeakLinkFunctionGenerator
218 Method = OSErrWeakLinkMethodGenerator
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 _res = Py_None;
243 return _res;
245 installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body)
246 module.add(installtracking)
248 installreceive_body = """
249 PyObject *callback;
250 WindowPtr theWindow = NULL;
251 OSErr _err;
253 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
254 return NULL;
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);
258 Py_INCREF(Py_None);
259 _res = Py_None;
260 return _res;
262 installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body)
263 module.add(installreceive)
265 removetracking_body = """
266 WindowPtr theWindow = NULL;
267 OSErr _err;
269 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
270 return NULL;
271 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
272 if (_err != noErr) return PyMac_Error(_err);
273 Py_INCREF(Py_None);
274 _res = Py_None;
275 return _res;
277 removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body)
278 module.add(removetracking)
280 removereceive_body = """
281 WindowPtr theWindow = NULL;
282 OSErr _err;
284 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
285 return NULL;
286 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
287 if (_err != noErr) return PyMac_Error(_err);
288 Py_INCREF(Py_None);
289 _res = Py_None;
290 return _res;
292 removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body)
293 module.add(removereceive)
295 # generate output (open the output file as late as possible)
296 SetOutputFileName(OUTPUTFILE)
297 module.generate()