• 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
6#error missing SetActionFilter
7
8import string
9
10# Declarations that change for each manager
11MODNAME = '_CG'                         # The name of the module
12
13# The following is *usually* unchanged but may still require tuning
14MODPREFIX = 'CG'                        # The prefix for module-wide routines
15INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
16OUTPUTFILE = MODNAME + "module.c"       # The file generated by this program
17
18from macsupport import *
19
20CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
21RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
22
23# Create the type objects
24
25includestuff = includestuff + """
26#include <ApplicationServices/ApplicationServices.h>
27
28extern int GrafObj_Convert(PyObject *, GrafPtr *);
29
30/*
31** Manual converters
32*/
33
34PyObject *CGPoint_New(CGPoint *itself)
35{
36
37        return Py_BuildValue("(ff)",
38                        itself->x,
39                        itself->y);
40}
41
42int
43CGPoint_Convert(PyObject *v, CGPoint *p_itself)
44{
45        if( !PyArg_Parse(v, "(ff)",
46                        &p_itself->x,
47                        &p_itself->y) )
48                return 0;
49        return 1;
50}
51
52PyObject *CGRect_New(CGRect *itself)
53{
54
55        return Py_BuildValue("(ffff)",
56                        itself->origin.x,
57                        itself->origin.y,
58                        itself->size.width,
59                        itself->size.height);
60}
61
62int
63CGRect_Convert(PyObject *v, CGRect *p_itself)
64{
65        if( !PyArg_Parse(v, "(ffff)",
66                        &p_itself->origin.x,
67                        &p_itself->origin.y,
68                        &p_itself->size.width,
69                        &p_itself->size.height) )
70                return 0;
71        return 1;
72}
73
74PyObject *CGAffineTransform_New(CGAffineTransform *itself)
75{
76
77        return Py_BuildValue("(ffffff)",
78                        itself->a,
79                        itself->b,
80                        itself->c,
81                        itself->d,
82                        itself->tx,
83                        itself->ty);
84}
85
86int
87CGAffineTransform_Convert(PyObject *v, CGAffineTransform *p_itself)
88{
89        if( !PyArg_Parse(v, "(ffffff)",
90                        &p_itself->a,
91                        &p_itself->b,
92                        &p_itself->c,
93                        &p_itself->d,
94                        &p_itself->tx,
95                        &p_itself->ty) )
96                return 0;
97        return 1;
98}
99"""
100
101class MyOpaqueByValueType(OpaqueByValueType):
102    """Sort of a mix between OpaqueByValueType and OpaqueType."""
103    def mkvalueArgs(self, name):
104        return "%s, &%s" % (self.new, name)
105
106CGPoint = MyOpaqueByValueType('CGPoint', 'CGPoint')
107CGRect = MyOpaqueByValueType('CGRect', 'CGRect')
108CGAffineTransform = MyOpaqueByValueType('CGAffineTransform', 'CGAffineTransform')
109
110char_ptr = Type("char *", "s")
111
112CGTextEncoding = int
113CGLineCap = int
114CGLineJoin = int
115CGTextDrawingMode = int
116CGPathDrawingMode = int
117CGInterpolationQuality = int
118
119# The real objects
120CGContextRef = OpaqueByValueType("CGContextRef", "CGContextRefObj")
121
122
123class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
124    def outputStructMembers(self):
125        ObjectDefinition.outputStructMembers(self)
126    def outputCleanupStructMembers(self):
127        Output("CGContextRelease(self->ob_itself);")
128
129
130# Create the generator groups and link them
131module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
132
133CGContextRef_object = MyObjectDefinition('CGContextRef', 'CGContextRefObj', 'CGContextRef')
134
135
136# ADD object here
137
138module.addobject(CGContextRef_object)
139
140
141
142Function = FunctionGenerator
143Method = MethodGenerator
144
145CGContextRef_methods = []
146
147# ADD _methods initializer here
148execfile(INPUTFILE)
149
150# manual method, lives in Quickdraw.h
151f = Method(void, 'SyncCGContextOriginWithPort',
152    (CGContextRef, 'ctx', InMode),
153    (CGrafPtr, 'port', InMode),
154)
155CGContextRef_methods.append(f)
156
157# manual method, lives in Quickdraw.h
158f = Method(void, 'ClipCGContextToRegion',
159    (CGContextRef, 'ctx', InMode),
160    (Rect, 'portRect', InMode),
161    (RgnHandle, 'region', InMode),
162)
163CGContextRef_methods.append(f)
164
165
166CreateCGContextForPort_body = """\
167GrafPtr port;
168CGContextRef ctx;
169OSStatus _err;
170
171if (!PyArg_ParseTuple(_args, "O&", GrafObj_Convert, &port))
172        return NULL;
173
174_err = CreateCGContextForPort(port, &ctx);
175if (_err != noErr)
176        if (_err != noErr) return PyMac_Error(_err);
177_res = Py_BuildValue("O&", CGContextRefObj_New, ctx);
178return _res;
179"""
180
181f = ManualGenerator("CreateCGContextForPort", CreateCGContextForPort_body);
182f.docstring = lambda: "(CGrafPtr) -> CGContextRef"
183module.add(f)
184
185
186# ADD add forloop here
187for f in CGContextRef_methods:
188    CGContextRef_object.add(f)
189
190# generate output (open the output file as late as possible)
191SetOutputFileName(OUTPUTFILE)
192module.generate()
193