| 1 | n/a | # This script generates a Python interface for an Apple Macintosh Manager. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # The function specifications are generated by scanning the mamager's header file, |
|---|
| 4 | n/a | # using the "scantools" package (customized for this particular manager). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import string |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Declarations that change for each manager |
|---|
| 9 | n/a | MACHEADERFILE = 'TextEdit.h' # The Apple header file |
|---|
| 10 | n/a | MODNAME = '_TE' # The name of the module |
|---|
| 11 | n/a | OBJECTNAME = 'TE' # The basic name of the objects used here |
|---|
| 12 | n/a | KIND = 'Handle' # Usually 'Ptr' or 'Handle' |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # The following is *usually* unchanged but may still require tuning |
|---|
| 15 | n/a | MODPREFIX = 'TE' # The prefix for module-wide routines |
|---|
| 16 | n/a | OBJECTTYPE = "TEHandle" # The C type used to represent them |
|---|
| 17 | n/a | OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods |
|---|
| 18 | n/a | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|---|
| 19 | n/a | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | from macsupport import * |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | # Create the type objects |
|---|
| 24 | n/a | TEHandle = OpaqueByValueType("TEHandle", "TEObj") |
|---|
| 25 | n/a | CharsHandle = OpaqueByValueType("CharsHandle", "ResObj") |
|---|
| 26 | n/a | Handle = OpaqueByValueType("Handle", "ResObj") |
|---|
| 27 | n/a | StScrpHandle = OpaqueByValueType("StScrpHandle", "ResObj") |
|---|
| 28 | n/a | TEStyleHandle = OpaqueByValueType("TEStyleHandle", "ResObj") |
|---|
| 29 | n/a | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | TextStyle = OpaqueType("TextStyle", "TextStyle") |
|---|
| 32 | n/a | TextStyle_ptr = TextStyle |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | includestuff = includestuff + """ |
|---|
| 35 | n/a | #include <Carbon/Carbon.h> |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 38 | n/a | extern PyObject *_TEObj_New(TEHandle); |
|---|
| 39 | n/a | extern int _TEObj_Convert(PyObject *, TEHandle *); |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | #define TEObj_New _TEObj_New |
|---|
| 42 | n/a | #define TEObj_Convert _TEObj_Convert |
|---|
| 43 | n/a | #endif |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | #define as_TE(h) ((TEHandle)h) |
|---|
| 46 | n/a | #define as_Resource(teh) ((Handle)teh) |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | /* |
|---|
| 49 | n/a | ** Parse/generate TextStyle records |
|---|
| 50 | n/a | */ |
|---|
| 51 | n/a | static PyObject * |
|---|
| 52 | n/a | TextStyle_New(TextStylePtr itself) |
|---|
| 53 | n/a | { |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | return Py_BuildValue("lllO&", (long)itself->tsFont, (long)itself->tsFace, (long)itself->tsSize, QdRGB_New, |
|---|
| 56 | n/a | &itself->tsColor); |
|---|
| 57 | n/a | } |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | static int |
|---|
| 60 | n/a | TextStyle_Convert(PyObject *v, TextStylePtr p_itself) |
|---|
| 61 | n/a | { |
|---|
| 62 | n/a | long font, face, size; |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | if( !PyArg_ParseTuple(v, "lllO&", &font, &face, &size, QdRGB_Convert, &p_itself->tsColor) ) |
|---|
| 65 | n/a | return 0; |
|---|
| 66 | n/a | p_itself->tsFont = (short)font; |
|---|
| 67 | n/a | p_itself->tsFace = (Style)face; |
|---|
| 68 | n/a | p_itself->tsSize = (short)size; |
|---|
| 69 | n/a | return 1; |
|---|
| 70 | n/a | } |
|---|
| 71 | n/a | """ |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | initstuff = initstuff + """ |
|---|
| 74 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(TEHandle, TEObj_New); |
|---|
| 75 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(TEHandle, TEObj_Convert); |
|---|
| 76 | n/a | """ |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | class TEMethodGenerator(OSErrWeakLinkMethodGenerator): |
|---|
| 79 | n/a | """Similar to MethodGenerator, but has self as last argument""" |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def parseArgumentList(self, args): |
|---|
| 82 | n/a | args, a0 = args[:-1], args[-1] |
|---|
| 83 | n/a | t0, n0, m0 = a0 |
|---|
| 84 | n/a | if m0 != InMode: |
|---|
| 85 | n/a | raise ValueError, "method's 'self' must be 'InMode'" |
|---|
| 86 | n/a | self.itself = Variable(t0, "_self->ob_itself", SelfMode) |
|---|
| 87 | n/a | FunctionGenerator.parseArgumentList(self, args) |
|---|
| 88 | n/a | self.argumentList.append(self.itself) |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 93 | n/a | # XXXX Could be subtype of Resource |
|---|
| 94 | n/a | # Attributes that can be set. |
|---|
| 95 | n/a | getsetlist = [ |
|---|
| 96 | n/a | ( |
|---|
| 97 | n/a | 'destRect', |
|---|
| 98 | n/a | 'return Py_BuildValue("O&", PyMac_BuildRect, &(*self->ob_itself)->destRect);', |
|---|
| 99 | n/a | None, |
|---|
| 100 | n/a | 'Destination rectangle' |
|---|
| 101 | n/a | ), ( |
|---|
| 102 | n/a | 'viewRect', |
|---|
| 103 | n/a | 'return Py_BuildValue("O&", PyMac_BuildRect, &(*self->ob_itself)->viewRect);', |
|---|
| 104 | n/a | None, |
|---|
| 105 | n/a | 'Viewing rectangle' |
|---|
| 106 | n/a | ), ( |
|---|
| 107 | n/a | 'selRect', |
|---|
| 108 | n/a | 'return Py_BuildValue("O&", PyMac_BuildRect, &(*self->ob_itself)->selRect);', |
|---|
| 109 | n/a | None, |
|---|
| 110 | n/a | 'Selection rectangle' |
|---|
| 111 | n/a | ), ( |
|---|
| 112 | n/a | 'lineHeight', |
|---|
| 113 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->lineHeight);', |
|---|
| 114 | n/a | None, |
|---|
| 115 | n/a | 'Height of a line' |
|---|
| 116 | n/a | ), ( |
|---|
| 117 | n/a | 'fontAscent', |
|---|
| 118 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->fontAscent);', |
|---|
| 119 | n/a | None, |
|---|
| 120 | n/a | 'Ascent of a line' |
|---|
| 121 | n/a | ), ( |
|---|
| 122 | n/a | "selPoint", |
|---|
| 123 | n/a | 'return Py_BuildValue("O&", PyMac_BuildPoint, (*self->ob_itself)->selPoint);', |
|---|
| 124 | n/a | None, |
|---|
| 125 | n/a | 'Selection Point' |
|---|
| 126 | n/a | ), ( |
|---|
| 127 | n/a | 'selStart', |
|---|
| 128 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->selStart);', |
|---|
| 129 | n/a | None, |
|---|
| 130 | n/a | 'Start of selection' |
|---|
| 131 | n/a | ), ( |
|---|
| 132 | n/a | 'selEnd', |
|---|
| 133 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->selEnd);', |
|---|
| 134 | n/a | None, |
|---|
| 135 | n/a | 'End of selection' |
|---|
| 136 | n/a | ), ( |
|---|
| 137 | n/a | 'active', |
|---|
| 138 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->active);', |
|---|
| 139 | n/a | None, |
|---|
| 140 | n/a | 'TBD' |
|---|
| 141 | n/a | ), ( |
|---|
| 142 | n/a | 'just', |
|---|
| 143 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->just);', |
|---|
| 144 | n/a | None, |
|---|
| 145 | n/a | 'Justification' |
|---|
| 146 | n/a | ), ( |
|---|
| 147 | n/a | 'teLength', |
|---|
| 148 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->teLength);', |
|---|
| 149 | n/a | None, |
|---|
| 150 | n/a | 'TBD' |
|---|
| 151 | n/a | ), ( |
|---|
| 152 | n/a | 'txFont', |
|---|
| 153 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->txFont);', |
|---|
| 154 | n/a | None, |
|---|
| 155 | n/a | 'Current font' |
|---|
| 156 | n/a | ), ( |
|---|
| 157 | n/a | 'txFace', |
|---|
| 158 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->txFace);', |
|---|
| 159 | n/a | None, |
|---|
| 160 | n/a | 'Current font variant' |
|---|
| 161 | n/a | ), ( |
|---|
| 162 | n/a | 'txMode', |
|---|
| 163 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->txMode);', |
|---|
| 164 | n/a | None, |
|---|
| 165 | n/a | 'Current text-drawing mode' |
|---|
| 166 | n/a | ), ( |
|---|
| 167 | n/a | 'txSize', |
|---|
| 168 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->txSize);', |
|---|
| 169 | n/a | None, |
|---|
| 170 | n/a | 'Current font size' |
|---|
| 171 | n/a | ), ( |
|---|
| 172 | n/a | 'nLines', |
|---|
| 173 | n/a | 'return Py_BuildValue("h", (*self->ob_itself)->nLines);', |
|---|
| 174 | n/a | None, |
|---|
| 175 | n/a | 'TBD' |
|---|
| 176 | n/a | )] |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | def outputCheckNewArg(self): |
|---|
| 179 | n/a | Output("""if (itself == NULL) { |
|---|
| 180 | n/a | PyErr_SetString(TE_Error,"Cannot create null TE"); |
|---|
| 181 | n/a | return NULL; |
|---|
| 182 | n/a | }""") |
|---|
| 183 | n/a | def outputFreeIt(self, itselfname): |
|---|
| 184 | n/a | Output("TEDispose(%s);", itselfname) |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | # From here on it's basically all boiler plate... |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | # Create the generator groups and link them |
|---|
| 190 | n/a | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
|---|
| 191 | n/a | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|---|
| 192 | n/a | module.addobject(object) |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | # Create the generator classes used to populate the lists |
|---|
| 195 | n/a | Function = OSErrWeakLinkFunctionGenerator |
|---|
| 196 | n/a | Method = TEMethodGenerator |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | # Create and populate the lists |
|---|
| 199 | n/a | functions = [] |
|---|
| 200 | n/a | methods = [] |
|---|
| 201 | n/a | execfile(INPUTFILE) |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | # Converter from/to handle |
|---|
| 204 | n/a | f = Function(TEHandle, 'as_TE', (Handle, 'h', InMode)) |
|---|
| 205 | n/a | functions.append(f) |
|---|
| 206 | n/a | f = Method(Handle, 'as_Resource', (TEHandle, 'teh', InMode)) |
|---|
| 207 | n/a | methods.append(f) |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | # add the populated lists to the generator groups |
|---|
| 210 | n/a | # (in a different wordl the scan program would generate this) |
|---|
| 211 | n/a | for f in functions: module.add(f) |
|---|
| 212 | n/a | for f in methods: object.add(f) |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | # generate output (open the output file as late as possible) |
|---|
| 215 | n/a | SetOutputFileName(OUTPUTFILE) |
|---|
| 216 | n/a | module.generate() |
|---|