This commit was manufactured by cvs2svn to create tag 'r212c1'.
[python/dscho.git] / Mac / Modules / drag / dragsupport.py
blobc35111ef48d46d36517c4b42eda813514ef9718f
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 = '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 fprintf(stderr, "Drag: Exception in TrackingHandler\\n");
86 return -1;
88 i = -1;
89 if ( rv == Py_None )
90 i = 0;
91 else
92 PyArg_Parse(rv, "l", &i);
93 Py_DECREF(rv);
94 return i;
97 static pascal OSErr
98 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
99 DragReference theDrag)
101 PyObject *args, *rv;
102 int i;
104 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
105 if ( args == NULL )
106 return -1;
107 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
108 Py_DECREF(args);
109 if ( rv == NULL ) {
110 fprintf(stderr, "Drag: Exception in ReceiveHandler\\n");
111 return -1;
113 i = -1;
114 if ( rv == Py_None )
115 i = 0;
116 else
117 PyArg_Parse(rv, "l", &i);
118 Py_DECREF(rv);
119 return i;
122 static pascal OSErr
123 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
124 ItemReference theItem, DragReference theDrag)
126 DragObjObject *self = (DragObjObject *)dragSendRefCon;
127 PyObject *args, *rv;
128 int i;
130 if ( self->sendproc == NULL )
131 return -1;
132 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
133 if ( args == NULL )
134 return -1;
135 rv = PyEval_CallObject(self->sendproc, args);
136 Py_DECREF(args);
137 if ( rv == NULL ) {
138 fprintf(stderr, "Drag: Exception in SendDataHandler\\n");
139 return -1;
141 i = -1;
142 if ( rv == Py_None )
143 i = 0;
144 else
145 PyArg_Parse(rv, "l", &i);
146 Py_DECREF(rv);
147 return i;
150 #if 0
151 static pascal OSErr
152 dragglue_Input(Point *mouse, short *modifiers,
153 void *dragSendRefCon, DragReference theDrag)
155 return 0;
158 static pascal OSErr
159 dragglue_Drawing(xxxx
160 void *dragSendRefCon, DragReference theDrag)
162 return 0;
164 #endif
168 initstuff = initstuff + """
169 PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New);
170 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert);
173 variablestuff = """
174 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler);
175 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler);
176 dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData);
177 #if 0
178 dragglue_InputUPP = NewDragInputUPP(dragglue_Input);
179 dragglue_DrawingUPP = NewDragDrawingUPP(dragglue_Drawing);
180 #endif
181 """
183 class MyObjectDefinition(GlobalObjectDefinition):
184 def outputCheckNewArg(self):
185 Output("""if (itself == NULL) {
186 PyErr_SetString(Drag_Error,"Cannot create null Drag");
187 return NULL;
188 }""")
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
218 functions = []
219 methods = []
220 execfile(INPUTFILE)
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 = """
229 PyObject *callback;
230 WindowPtr theWindow = NULL;
231 OSErr _err;
233 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
234 return NULL;
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);
238 Py_INCREF(Py_None);
239 return Py_None;
241 installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body)
242 module.add(installtracking)
244 installreceive_body = """
245 PyObject *callback;
246 WindowPtr theWindow = NULL;
247 OSErr _err;
249 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
250 return NULL;
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);
254 Py_INCREF(Py_None);
255 return Py_None;
257 installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body)
258 module.add(installreceive)
260 removetracking_body = """
261 WindowPtr theWindow = NULL;
262 OSErr _err;
264 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
265 return NULL;
266 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
267 if (_err != noErr) return PyMac_Error(_err);
268 Py_INCREF(Py_None);
269 return Py_None;
271 removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body)
272 module.add(removetracking)
274 removereceive_body = """
275 WindowPtr theWindow = NULL;
276 OSErr _err;
278 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
279 return NULL;
280 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
281 if (_err != noErr) return PyMac_Error(_err);
282 Py_INCREF(Py_None);
283 return Py_None;
285 removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body)
286 module.add(removereceive)
288 # generate output (open the output file as late as possible)
289 SetOutputFileName(OUTPUTFILE)
290 module.generate()