• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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).
5
6import string
7
8# Declarations that change for each manager
9MACHEADERFILE = 'Drag.h'                # The Apple header file
10MODNAME = '_Drag'                               # The name of the module
11OBJECTNAME = 'DragObj'                  # The basic name of the objects used here
12
13# The following is *usually* unchanged but may still require tuning
14MODPREFIX = 'Drag'                      # The prefix for module-wide routines
15OBJECTTYPE = 'DragRef'  # The C type used to represent them
16OBJECTPREFIX = MODPREFIX + 'Obj'        # The prefix for object methods
17INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
18OUTPUTFILE = MODNAME + "module.c"       # The file generated by this program
19
20from macsupport import *
21
22# Create the type objects
23
24DragRef = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
25DragItemRef = Type("ItemReference", "l")
26# Old names
27DragReference = DragRef
28ItemReference = DragItemRef
29
30PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj")
31RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
32AEDesc = OpaqueType('AEDesc')
33AEDesc_ptr = AEDesc
34RGBColor = OpaqueType("RGBColor", "QdRGB")
35
36FlavorType = OSTypeType("FlavorType")
37DragAttributes = Type("DragAttributes", "l")
38DragBehaviors = Type("DragBehaviors", "l")
39DragImageFlags = Type("DragImageFlags", "l")
40DragImageTranslucency = Type("DragImageTranslucency", "l")
41DragRegionMessage = Type("DragRegionMessage", "h")
42ZoomAcceleration = Type("ZoomAcceleration", "h")
43FlavorFlags = Type("FlavorFlags", "l")
44DragTrackingMessage = Type("DragTrackingMessage", "h")
45
46includestuff = includestuff + """
47#include <Carbon/Carbon.h>
48
49/* Callback glue routines */
50DragTrackingHandlerUPP dragglue_TrackingHandlerUPP;
51DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP;
52DragSendDataUPP dragglue_SendDataUPP;
53#if 0
54DragInputUPP dragglue_InputUPP;
55DragDrawingUPP dragglue_DrawingUPP;
56#endif
57
58#ifdef USE_TOOLBOX_OBJECT_GLUE
59extern PyObject *_DragObj_New(DragRef);
60extern int _DragObj_Convert(PyObject *, DragRef *);
61
62#define DragObj_New _DragObj_New
63#define DragObj_Convert _DragObj_Convert
64#endif
65"""
66
67finalstuff = finalstuff + """
68static pascal OSErr
69dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
70                         void *handlerRefCon, DragReference theDrag)
71{
72        PyObject *args, *rv;
73        int i;
74
75        args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
76        if ( args == NULL )
77                return -1;
78        rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
79        Py_DECREF(args);
80        if ( rv == NULL ) {
81                PySys_WriteStderr("Drag: Exception in TrackingHandler\\n");
82                PyErr_Print();
83                return -1;
84        }
85        i = -1;
86        if ( rv == Py_None )
87                i = 0;
88        else
89                PyArg_Parse(rv, "l", &i);
90        Py_DECREF(rv);
91        return i;
92}
93
94static pascal OSErr
95dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
96                        DragReference theDrag)
97{
98        PyObject *args, *rv;
99        int i;
100
101        args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow);
102        if ( args == NULL )
103                return -1;
104        rv = PyEval_CallObject((PyObject *)handlerRefCon, args);
105        Py_DECREF(args);
106        if ( rv == NULL ) {
107                PySys_WriteStderr("Drag: Exception in ReceiveHandler\\n");
108                PyErr_Print();
109                return -1;
110        }
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;
118}
119
120static pascal OSErr
121dragglue_SendData(FlavorType theType, void *dragSendRefCon,
122                      ItemReference theItem, DragReference theDrag)
123{
124        DragObjObject *self = (DragObjObject *)dragSendRefCon;
125        PyObject *args, *rv;
126        int i;
127
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                PySys_WriteStderr("Drag: Exception in SendDataHandler\\n");
137                PyErr_Print();
138                return -1;
139        }
140        i = -1;
141        if ( rv == Py_None )
142                i = 0;
143        else
144                PyArg_Parse(rv, "l", &i);
145        Py_DECREF(rv);
146        return i;
147}
148
149#if 0
150static pascal OSErr
151dragglue_Input(Point *mouse, short *modifiers,
152                   void *dragSendRefCon, DragReference theDrag)
153{
154    return 0;
155}
156
157static pascal OSErr
158dragglue_Drawing(xxxx
159                   void *dragSendRefCon, DragReference theDrag)
160{
161    return 0;
162}
163#endif
164
165"""
166
167initstuff = initstuff + """
168        PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New);
169        PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert);
170"""
171
172variablestuff = """
173dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler);
174dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler);
175dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData);
176#if 0
177dragglue_InputUPP = NewDragInputUPP(dragglue_Input);
178dragglue_DrawingUPP = NewDragDrawingUPP(dragglue_Drawing);
179#endif
180"""
181
182class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
183    def outputCheckNewArg(self):
184        Output("""if (itself == NULL) {
185                                PyErr_SetString(Drag_Error,"Cannot create null Drag");
186                                return NULL;
187                        }""")
188    def outputFreeIt(self, itselfname):
189        ## Output("DisposeDrag(%s);", itselfname)
190        Output("Py_XDECREF(self->sendproc);")
191        ## Output("Py_XDECREF(self->inputproc);")
192        ## Output("Py_XDECREF(self->drawingproc);")
193
194    def outputStructMembers(self):
195        GlobalObjectDefinition.outputStructMembers(self)
196        Output("PyObject *sendproc;")
197        ## Output("PyObject *inputproc;")
198        ## Output("PyObject *drawingproc;")
199
200    def outputInitStructMembers(self):
201        GlobalObjectDefinition.outputInitStructMembers(self)
202        Output("it->sendproc = NULL;")
203        ## Output("it->inputproc = NULL;")
204        ## Output("it->drawingproc = NULL;")
205
206
207# Create the generator groups and link them
208module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
209object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
210module.addobject(object)
211
212# Create the generator classes used to populate the lists
213Function = OSErrWeakLinkFunctionGenerator
214Method = OSErrWeakLinkMethodGenerator
215
216# Create and populate the lists
217functions = []
218methods = []
219execfile(INPUTFILE)
220
221# add the populated lists to the generator groups
222for f in functions: module.add(f)
223for f in methods: object.add(f)
224
225# Manual generators for the callbacks
226
227installtracking_body = """
228    PyObject *callback;
229    WindowPtr theWindow = NULL;
230    OSErr _err;
231
232    if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) )
233        return NULL;
234    Py_INCREF(callback);        /* Cannot decref later, too bad */
235    _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback);
236        if (_err != noErr) return PyMac_Error(_err);
237        Py_INCREF(Py_None);
238        _res = Py_None;
239        return _res;
240"""
241installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body)
242module.add(installtracking)
243
244installreceive_body = """
245    PyObject *callback;
246    WindowPtr theWindow = NULL;
247    OSErr _err;
248
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        _res = Py_None;
256        return _res;
257"""
258installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body)
259module.add(installreceive)
260
261removetracking_body = """
262    WindowPtr theWindow = NULL;
263    OSErr _err;
264
265    if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
266        return NULL;
267    _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow);
268        if (_err != noErr) return PyMac_Error(_err);
269        Py_INCREF(Py_None);
270        _res = Py_None;
271        return _res;
272"""
273removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body)
274module.add(removetracking)
275
276removereceive_body = """
277    WindowPtr theWindow = NULL;
278    OSErr _err;
279
280    if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) )
281        return NULL;
282    _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow);
283        if (_err != noErr) return PyMac_Error(_err);
284        Py_INCREF(Py_None);
285        _res = Py_None;
286        return _res;
287"""
288removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body)
289module.add(removereceive)
290
291# generate output (open the output file as late as possible)
292SetOutputFileName(OUTPUTFILE)
293module.generate()
294