Bump version to 0.9.1.
[python/dscho.git] / Mac / Modules / drag / dragsupport.py
blobaf8a25534eb2d18c74f8450d4226dd1eb6ba9f48
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 /* Callback glue routines */
60 DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
61 DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
62 DragSendDataUPP dragglue_SendDataUPP;
63 #if 0
64 DragInputUPP dragglue_InputUPP;
65 DragDrawingUPP dragglue_DrawingUPP;
66 #endif
67 """
69 finalstuff = finalstuff + """
70 static pascal OSErr
71 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
72 void *handlerRefCon, DragReference theDrag)
74 PyObject *args, *rv;
75 int i;
77 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
78 if ( args == NULL )
79 return -1;
80 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
81 Py_DECREF(args);
82 if ( rv == NULL ) {
83 fprintf(stderr, "Drag: Exception in TrackingHandler\\n");
84 return -1;
86 i = -1;
87 if ( rv == Py_None )
88 i = 0;
89 else
90 PyArg_Parse(rv, "l", &i);
91 Py_DECREF(rv);
92 return i;
95 static pascal OSErr
96 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
97 DragReference theDrag)
99 PyObject *args, *rv;
100 int i;
102 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
103 if ( args == NULL )
104 return -1;
105 rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
106 Py_DECREF(args);
107 if ( rv == NULL ) {
108 fprintf(stderr, "Drag: Exception in ReceiveHandler\\n");
109 return -1;
111 i = -1;
112 if ( rv == Py_None )
113 i = 0;
114 else
115 PyArg_Parse(rv, "l", &i);
116 Py_DECREF(rv);
117 return i;
120 static pascal OSErr
121 dragglue_SendData(FlavorType theType, void *dragSendRefCon,
122 ItemReference theItem, DragReference theDrag)
124 DragObjObject *self = (DragObjObject *)dragSendRefCon;
125 PyObject *args, *rv;
126 int i;
128 if ( self->sendproc == NULL )
129 return -1;
130 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem);
131 if ( args == NULL )
132 return -1;
133 rv = PyEval_CallObject(self->sendproc, args);
134 Py_DECREF(args);
135 if ( rv == NULL ) {
136 fprintf(stderr, "Drag: Exception in SendDataHandler\\n");
137 return -1;
139 i = -1;
140 if ( rv == Py_None )
141 i = 0;
142 else
143 PyArg_Parse(rv, "l", &i);
144 Py_DECREF(rv);
145 return i;
148 #if 0
149 static pascal OSErr
150 dragglue_Input(Point *mouse, short *modifiers,
151 void *dragSendRefCon, DragReference theDrag)
153 return 0;
156 static pascal OSErr
157 dragglue_Drawing(xxxx
158 void *dragSendRefCon, DragReference theDrag)
160 return 0;
162 #endif
166 variablestuff = """
167 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerProc(dragglue_TrackingHandler);
168 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerProc(dragglue_ReceiveHandler);
169 dragglue_SendDataUPP = NewDragSendDataProc(dragglue_SendData);
170 #if 0
171 dragglue_InputUPP = NewDragInputProc(dragglue_Input);
172 dragglue_DrawingUPP = NewDragDrawingProc(dragglue_Drawing);
173 #endif
174 """
176 class MyObjectDefinition(GlobalObjectDefinition):
177 def outputCheckNewArg(self):
178 Output("""if (itself == NULL) {
179 PyErr_SetString(Drag_Error,"Cannot create null Drag");
180 return NULL;
181 }""")
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
211 functions = []
212 methods = []
213 execfile(INPUTFILE)
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 = """
222 PyObject *callback;
223 WindowPtr theWindow = NULL;
224 OSErr _err;
226 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
227 return NULL;
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);
231 Py_INCREF(Py_None);
232 return Py_None;
234 installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body)
235 module.add(installtracking)
237 installreceive_body = """
238 PyObject *callback;
239 WindowPtr theWindow = NULL;
240 OSErr _err;
242 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
243 return NULL;
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);
247 Py_INCREF(Py_None);
248 return Py_None;
250 installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body)
251 module.add(installreceive)
253 removetracking_body = """
254 WindowPtr theWindow = NULL;
255 OSErr _err;
257 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
258 return NULL;
259 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
260 if (_err != noErr) return PyMac_Error(_err);
261 Py_INCREF(Py_None);
262 return Py_None;
264 removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body)
265 module.add(removetracking)
267 removereceive_body = """
268 WindowPtr theWindow = NULL;
269 OSErr _err;
271 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
272 return NULL;
273 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
274 if (_err != noErr) return PyMac_Error(_err);
275 Py_INCREF(Py_None);
276 return Py_None;
278 removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body)
279 module.add(removereceive)
281 # generate output (open the output file as late as possible)
282 SetOutputFileName(OUTPUTFILE)
283 module.generate()