Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 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 | |
| 6 | import string |
| 7 | |
| 8 | # Declarations that change for each manager |
| 9 | MACHEADERFILE = 'Controls.h' # The Apple header file |
| 10 | MODNAME = 'Ctl' # The name of the module |
| 11 | OBJECTNAME = 'Control' # The basic name of the objects used here |
| 12 | |
| 13 | # The following is *usually* unchanged but may still require tuning |
| 14 | MODPREFIX = MODNAME # The prefix for module-wide routines |
| 15 | OBJECTTYPE = OBJECTNAME + 'Handle' # 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 |
| 19 | |
| 20 | from macsupport import * |
| 21 | |
| 22 | # Create the type objects |
| 23 | |
| 24 | ControlHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
Jack Jansen | ae8a68f | 1995-06-06 12:55:40 +0000 | [diff] [blame] | 25 | ControlRef = ControlHandle |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 26 | ExistingControlHandle = OpaqueByValueType(OBJECTTYPE, "CtlObj_WhichControl", "BUG") |
| 27 | |
Jack Jansen | 2b72417 | 1996-04-10 14:48:19 +0000 | [diff] [blame] | 28 | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
Jack Jansen | c7fefed | 1997-08-15 14:32:18 +0000 | [diff] [blame] | 29 | CCTabHandle = OpaqueByValueType("CCTabHandle", "ResObj") |
| 30 | AuxCtlHandle = OpaqueByValueType("AuxCtlHandle", "ResObj") |
Jack Jansen | 7d0bc83 | 1995-06-09 20:56:31 +0000 | [diff] [blame] | 31 | ControlPartCode = Type("ControlPartCode", "h") |
| 32 | DragConstraint = Type("DragConstraint", "h") |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 33 | ControlVariant = Type("ControlVariant", "h") |
| 34 | IconTransformType = Type("IconTransformType", "h") |
| 35 | ControlButtonGraphicAlignment = Type("ControlButtonGraphicAlignment", "h") |
| 36 | ControlButtonTextAlignment = Type("ControlButtonTextAlignment", "h") |
| 37 | ControlButtonTextPlacement = Type("ControlButtonTextPlacement", "h") |
| 38 | ControlContentType = Type("ControlContentType", "h") |
| 39 | ControlFocusPart = Type("ControlFocusPart", "h") |
| 40 | |
| 41 | ControlFontStyleRec = OpaqueType('ControlFontStyleRec', 'ControlFontStyle') |
| 42 | ControlFontStyleRec_ptr = ControlFontStyleRec |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 43 | |
| 44 | includestuff = includestuff + """ |
| 45 | #include <%s>""" % MACHEADERFILE + """ |
| 46 | |
| 47 | #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */ |
| 48 | |
| 49 | extern PyObject *CtlObj_WhichControl(ControlHandle); /* Forward */ |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 50 | extern PyObject *QdRGB_New(RGBColorPtr); |
| 51 | extern QdRGB_Convert(PyObject *, RGBColorPtr); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef THINK_C |
| 54 | #define ControlActionUPP ProcPtr |
| 55 | #endif |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | ** Parse/generate ControlFontStyleRec records |
| 59 | */ |
| 60 | #if 0 /* Not needed */ |
| 61 | PyObject *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 | |
| 71 | ControlFontStyle_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 Jansen | 848250c | 1998-05-28 14:20:09 +0000 | [diff] [blame] | 80 | |
| 81 | /* TrackControl callback support */ |
| 82 | static PyObject *tracker; |
| 83 | static ControlActionUPP mytracker_upp; |
| 84 | |
| 85 | extern int settrackfunc(PyObject *); /* forward */ |
| 86 | extern void clrtrackfunc(void); /* forward */ |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 87 | """ |
| 88 | |
| 89 | finalstuff = finalstuff + """ |
| 90 | PyObject * |
| 91 | CtlObj_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 Jansen | 85ae4a8 | 1997-04-08 15:26:03 +0000 | [diff] [blame] | 99 | it = (PyObject *) GetControlReference(c); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 100 | if (it == NULL || ((ControlObject *)it)->ob_itself != c) |
| 101 | it = Py_None; |
| 102 | Py_INCREF(it); |
| 103 | return it; |
| 104 | } |
Jack Jansen | 848250c | 1998-05-28 14:20:09 +0000 | [diff] [blame] | 105 | |
| 106 | static int |
| 107 | settrackfunc(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 | |
| 118 | static void |
| 119 | clrtrackfunc() |
| 120 | { |
| 121 | Py_XDECREF(tracker); |
| 122 | tracker = 0; |
| 123 | } |
| 124 | |
| 125 | static pascal void |
| 126 | mytracker(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 Jansen | deff89c | 1998-10-12 20:53:15 +0000 | [diff] [blame^] | 140 | PySys_WriteStderr("TrackControl: exception in tracker function\\n"); |
Jack Jansen | 848250c | 1998-05-28 14:20:09 +0000 | [diff] [blame] | 141 | } |
| 142 | """ |
| 143 | |
| 144 | initstuff = initstuff + """ |
| 145 | mytracker_upp = NewControlActionProc(mytracker); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 146 | """ |
| 147 | |
| 148 | class MyObjectDefinition(GlobalObjectDefinition): |
| 149 | def outputCheckNewArg(self): |
| 150 | Output("if (itself == NULL) return PyMac_Error(resNotFound);") |
| 151 | def outputInitStructMembers(self): |
| 152 | GlobalObjectDefinition.outputInitStructMembers(self) |
Jack Jansen | 85ae4a8 | 1997-04-08 15:26:03 +0000 | [diff] [blame] | 153 | Output("SetControlReference(itself, (long)it);") |
Jack Jansen | c574b43 | 1996-04-12 16:26:59 +0000 | [diff] [blame] | 154 | def outputCleanupStructMembers(self): |
Jack Jansen | 85ae4a8 | 1997-04-08 15:26:03 +0000 | [diff] [blame] | 155 | Output("if (self->ob_itself) SetControlReference(self->ob_itself, (long)0); /* Make it forget about us */") |
Jack Jansen | c574b43 | 1996-04-12 16:26:59 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 157 | # Create the generator groups and link them |
| 158 | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
| 159 | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
| 160 | module.addobject(object) |
| 161 | |
| 162 | # Create the generator classes used to populate the lists |
| 163 | Function = OSErrFunctionGenerator |
| 164 | Method = OSErrMethodGenerator |
| 165 | |
| 166 | # Create and populate the lists |
| 167 | functions = [] |
| 168 | methods = [] |
| 169 | execfile(INPUTFILE) |
Jack Jansen | 5d56f4b | 1995-06-18 20:16:33 +0000 | [diff] [blame] | 170 | execfile('ctledit.py') |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 171 | |
| 172 | # add the populated lists to the generator groups |
| 173 | for f in functions: module.add(f) |
| 174 | for f in methods: object.add(f) |
| 175 | |
Jack Jansen | 848250c | 1998-05-28 14:20:09 +0000 | [diff] [blame] | 176 | # Manual generator for TrackControl, due to callback ideosyncracies |
| 177 | trackcontrol_body = """ |
| 178 | ControlPartCode _rv; |
| 179 | Point startPoint; |
| 180 | ControlActionUPP upp = 0; |
| 181 | PyObject *callback = 0; |
| 182 | |
| 183 | if (!PyArg_ParseTuple(_args, "O&|O", |
| 184 | PyMac_GetPoint, &startPoint, &callback)) |
| 185 | return NULL; |
| 186 | if (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); |
| 197 | clrtrackfunc(); |
| 198 | _res = Py_BuildValue("h", |
| 199 | _rv); |
| 200 | return _res; |
| 201 | """ |
| 202 | |
| 203 | f = ManualGenerator("TrackControl", trackcontrol_body); |
| 204 | #f.docstring = "(Point startPoint [,trackercallback]) -> (ControlPartCode _rv)" |
| 205 | object.add(f) |
| 206 | |
Jack Jansen | 4c70413 | 1998-06-19 13:35:14 +0000 | [diff] [blame] | 207 | # And manual generators to get/set popup menu information |
| 208 | getpopupdata_body = """ |
| 209 | PopupPrivateDataHandle hdl; |
| 210 | |
| 211 | if ( (*_self->ob_itself)->contrlData == NULL ) { |
| 212 | PyErr_SetString(Ctl_Error, "No contrlData handle in control"); |
| 213 | return 0; |
| 214 | } |
| 215 | hdl = (PopupPrivateDataHandle)(*_self->ob_itself)->contrlData; |
| 216 | HLock((Handle)hdl); |
| 217 | _res = Py_BuildValue("O&i", MenuObj_New, (*hdl)->mHandle, (int)(*hdl)->mID); |
| 218 | HUnlock((Handle)hdl); |
| 219 | return _res; |
| 220 | """ |
| 221 | f = ManualGenerator("GetPopupData", getpopupdata_body) |
| 222 | object.add(f) |
| 223 | |
| 224 | setpopupdata_body = """ |
| 225 | PopupPrivateDataHandle hdl; |
| 226 | MenuHandle mHandle; |
| 227 | short mID; |
| 228 | |
| 229 | if (!PyArg_ParseTuple(_args, "O&h", MenuObj_Convert, &mHandle, &mID) ) |
| 230 | return 0; |
| 231 | if ( (*_self->ob_itself)->contrlData == NULL ) { |
| 232 | PyErr_SetString(Ctl_Error, "No contrlData handle in control"); |
| 233 | return 0; |
| 234 | } |
| 235 | hdl = (PopupPrivateDataHandle)(*_self->ob_itself)->contrlData; |
| 236 | (*hdl)->mHandle = mHandle; |
| 237 | (*hdl)->mID = mID; |
| 238 | Py_INCREF(Py_None); |
| 239 | return Py_None; |
| 240 | """ |
| 241 | f = ManualGenerator("SetPopupData", setpopupdata_body) |
| 242 | object.add(f) |
| 243 | |
Jack Jansen | 848250c | 1998-05-28 14:20:09 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 245 | # generate output (open the output file as late as possible) |
| 246 | SetOutputFileName(OUTPUTFILE) |
| 247 | module.generate() |