blob: 84dd6cec8d7ccceaea49367b1298ee3c2dec6202 [file] [log] [blame]
Guido van Rossum17448e21995-01-30 11:53:55 +00001# 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 = 'Controls.h' # The Apple header file
10MODNAME = 'Ctl' # The name of the module
11OBJECTNAME = 'Control' # The basic name of the objects used here
12
13# The following is *usually* unchanged but may still require tuning
14MODPREFIX = MODNAME # The prefix for module-wide routines
15OBJECTTYPE = OBJECTNAME + 'Handle' # 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
24ControlHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
Jack Jansenae8a68f1995-06-06 12:55:40 +000025ControlRef = ControlHandle
Guido van Rossum17448e21995-01-30 11:53:55 +000026ExistingControlHandle = OpaqueByValueType(OBJECTTYPE, "CtlObj_WhichControl", "BUG")
27
Jack Jansen2b724171996-04-10 14:48:19 +000028RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
Jack Jansenc7fefed1997-08-15 14:32:18 +000029CCTabHandle = OpaqueByValueType("CCTabHandle", "ResObj")
30AuxCtlHandle = OpaqueByValueType("AuxCtlHandle", "ResObj")
Jack Jansen7d0bc831995-06-09 20:56:31 +000031ControlPartCode = Type("ControlPartCode", "h")
32DragConstraint = Type("DragConstraint", "h")
Jack Jansen21f96871998-02-20 16:02:09 +000033ControlVariant = Type("ControlVariant", "h")
34IconTransformType = Type("IconTransformType", "h")
35ControlButtonGraphicAlignment = Type("ControlButtonGraphicAlignment", "h")
36ControlButtonTextAlignment = Type("ControlButtonTextAlignment", "h")
37ControlButtonTextPlacement = Type("ControlButtonTextPlacement", "h")
38ControlContentType = Type("ControlContentType", "h")
39ControlFocusPart = Type("ControlFocusPart", "h")
40
41ControlFontStyleRec = OpaqueType('ControlFontStyleRec', 'ControlFontStyle')
42ControlFontStyleRec_ptr = ControlFontStyleRec
Guido van Rossum17448e21995-01-30 11:53:55 +000043
44includestuff = includestuff + """
45#include <%s>""" % MACHEADERFILE + """
46
47#define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
48
49extern PyObject *CtlObj_WhichControl(ControlHandle); /* Forward */
Jack Jansen21f96871998-02-20 16:02:09 +000050extern PyObject *QdRGB_New(RGBColorPtr);
51extern QdRGB_Convert(PyObject *, RGBColorPtr);
Guido van Rossum17448e21995-01-30 11:53:55 +000052
53#ifdef THINK_C
54#define ControlActionUPP ProcPtr
55#endif
Jack Jansen21f96871998-02-20 16:02:09 +000056
57/*
58** Parse/generate ControlFontStyleRec records
59*/
60#if 0 /* Not needed */
61PyObject *ControlFontStyle_New(itself)
62 ControlFontStyleRec *itself;
63{
64
65 return Py_BuildValue("hhhhhhO&O&", itself->flags, itself->font,
66 itself->size, itself->style, itself->mode, itself->just,
67 QdRGB_New, &itself->foreColor, QdRGB_New, &itself->backColor);
68}
69#endif
70
71ControlFontStyle_Convert(v, itself)
72 PyObject *v;
73 ControlFontStyleRec *itself;
74{
75 return PyArg_ParseTuple(v, "hhhhhhO&O&", &itself->flags,
76 &itself->font, &itself->size, &itself->style, &itself->mode,
77 &itself->just, QdRGB_Convert, &itself->foreColor,
78 QdRGB_Convert, &itself->backColor);
79}
Jack Jansen848250c1998-05-28 14:20:09 +000080
81/* TrackControl callback support */
82static PyObject *tracker;
83static ControlActionUPP mytracker_upp;
84
85extern int settrackfunc(PyObject *); /* forward */
86extern void clrtrackfunc(void); /* forward */
Guido van Rossum17448e21995-01-30 11:53:55 +000087"""
88
89finalstuff = finalstuff + """
90PyObject *
91CtlObj_WhichControl(ControlHandle c)
92{
93 PyObject *it;
94
95 /* XXX What if we find a control belonging to some other package? */
96 if (c == NULL)
97 it = NULL;
98 else
Jack Jansen85ae4a81997-04-08 15:26:03 +000099 it = (PyObject *) GetControlReference(c);
Guido van Rossum17448e21995-01-30 11:53:55 +0000100 if (it == NULL || ((ControlObject *)it)->ob_itself != c)
101 it = Py_None;
102 Py_INCREF(it);
103 return it;
104}
Jack Jansen848250c1998-05-28 14:20:09 +0000105
106static int
107settrackfunc(obj)
108 PyObject *obj;
109{
110 if (tracker) {
111 PyErr_SetString(Ctl_Error, "Tracker function in use");
112 return 0;
113 }
114 tracker = obj;
115 Py_INCREF(tracker);
116}
117
118static void
119clrtrackfunc()
120{
121 Py_XDECREF(tracker);
122 tracker = 0;
123}
124
125static pascal void
126mytracker(ctl, part)
127 ControlHandle ctl;
128 short part;
129{
130 PyObject *args, *rv=0;
131
132 args = Py_BuildValue("(O&i)", CtlObj_WhichControl, ctl, (int)part);
133 if (args && tracker) {
134 rv = PyEval_CallObject(tracker, args);
135 Py_DECREF(args);
136 }
137 if (rv)
138 Py_DECREF(rv);
139 else
Jack Jansendeff89c1998-10-12 20:53:15 +0000140 PySys_WriteStderr("TrackControl: exception in tracker function\\n");
Jack Jansen848250c1998-05-28 14:20:09 +0000141}
142"""
143
144initstuff = initstuff + """
145mytracker_upp = NewControlActionProc(mytracker);
Guido van Rossum17448e21995-01-30 11:53:55 +0000146"""
147
148class MyObjectDefinition(GlobalObjectDefinition):
149 def outputCheckNewArg(self):
150 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
151 def outputInitStructMembers(self):
152 GlobalObjectDefinition.outputInitStructMembers(self)
Jack Jansen85ae4a81997-04-08 15:26:03 +0000153 Output("SetControlReference(itself, (long)it);")
Jack Jansenc574b431996-04-12 16:26:59 +0000154 def outputCleanupStructMembers(self):
Jack Jansen85ae4a81997-04-08 15:26:03 +0000155 Output("if (self->ob_itself) SetControlReference(self->ob_itself, (long)0); /* Make it forget about us */")
Jack Jansenc574b431996-04-12 16:26:59 +0000156
Guido van Rossum17448e21995-01-30 11:53:55 +0000157# Create the generator groups and link them
158module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
159object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
160module.addobject(object)
161
162# Create the generator classes used to populate the lists
163Function = OSErrFunctionGenerator
164Method = OSErrMethodGenerator
165
166# Create and populate the lists
167functions = []
168methods = []
169execfile(INPUTFILE)
Jack Jansen5d56f4b1995-06-18 20:16:33 +0000170execfile('ctledit.py')
Guido van Rossum17448e21995-01-30 11:53:55 +0000171
172# add the populated lists to the generator groups
173for f in functions: module.add(f)
174for f in methods: object.add(f)
175
Jack Jansen848250c1998-05-28 14:20:09 +0000176# Manual generator for TrackControl, due to callback ideosyncracies
177trackcontrol_body = """
178ControlPartCode _rv;
179Point startPoint;
180ControlActionUPP upp = 0;
181PyObject *callback = 0;
182
183if (!PyArg_ParseTuple(_args, "O&|O",
184 PyMac_GetPoint, &startPoint, &callback))
185 return NULL;
186if (callback && callback != Py_None) {
187 if (PyInt_Check(callback) && PyInt_AS_LONG(callback) == -1)
188 upp = (ControlActionUPP)-1;
189 else {
190 settrackfunc(callback);
191 upp = mytracker_upp;
192 }
193}
194_rv = TrackControl(_self->ob_itself,
195 startPoint,
196 upp);
197clrtrackfunc();
198_res = Py_BuildValue("h",
199 _rv);
200return _res;
201"""
202
203f = ManualGenerator("TrackControl", trackcontrol_body);
204#f.docstring = "(Point startPoint [,trackercallback]) -> (ControlPartCode _rv)"
205object.add(f)
206
Jack Jansen4c704131998-06-19 13:35:14 +0000207# And manual generators to get/set popup menu information
208getpopupdata_body = """
209PopupPrivateDataHandle hdl;
210
211if ( (*_self->ob_itself)->contrlData == NULL ) {
212 PyErr_SetString(Ctl_Error, "No contrlData handle in control");
213 return 0;
214}
215hdl = (PopupPrivateDataHandle)(*_self->ob_itself)->contrlData;
216HLock((Handle)hdl);
217_res = Py_BuildValue("O&i", MenuObj_New, (*hdl)->mHandle, (int)(*hdl)->mID);
218HUnlock((Handle)hdl);
219return _res;
220"""
221f = ManualGenerator("GetPopupData", getpopupdata_body)
222object.add(f)
223
224setpopupdata_body = """
225PopupPrivateDataHandle hdl;
226MenuHandle mHandle;
227short mID;
228
229if (!PyArg_ParseTuple(_args, "O&h", MenuObj_Convert, &mHandle, &mID) )
230 return 0;
231if ( (*_self->ob_itself)->contrlData == NULL ) {
232 PyErr_SetString(Ctl_Error, "No contrlData handle in control");
233 return 0;
234}
235hdl = (PopupPrivateDataHandle)(*_self->ob_itself)->contrlData;
236(*hdl)->mHandle = mHandle;
237(*hdl)->mID = mID;
238Py_INCREF(Py_None);
239return Py_None;
240"""
241f = ManualGenerator("SetPopupData", setpopupdata_body)
242object.add(f)
243
Jack Jansen848250c1998-05-28 14:20:09 +0000244
Guido van Rossum17448e21995-01-30 11:53:55 +0000245# generate output (open the output file as late as possible)
246SetOutputFileName(OUTPUTFILE)
247module.generate()