PageRenderTime 53ms CodeModel.GetById 20ms app.highlight 13ms RepoModel.GetById 17ms app.codeStats 0ms

/Mac/Modules/te/tesupport.py

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