move sections
[python/dscho.git] / Mac / Modules / cg / cgsupport.py
blob6eedfbef3f762682c09dbdb35bc358bf4e7eef5b
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).
6 #error missing SetActionFilter
8 import string
10 # Declarations that change for each manager
11 MODNAME = '_CG' # The name of the module
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX = 'CG' # The prefix for module-wide routines
15 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
16 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
18 from macsupport import *
20 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
21 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
23 # Create the type objects
25 includestuff = includestuff + """
26 #include <ApplicationServices/ApplicationServices.h>
28 extern int GrafObj_Convert(PyObject *, GrafPtr *);
31 ** Manual converters
34 PyObject *CGPoint_New(CGPoint *itself)
37 return Py_BuildValue("(ff)",
38 itself->x,
39 itself->y);
42 int
43 CGPoint_Convert(PyObject *v, CGPoint *p_itself)
45 if( !PyArg_Parse(v, "(ff)",
46 &p_itself->x,
47 &p_itself->y) )
48 return 0;
49 return 1;
52 PyObject *CGRect_New(CGRect *itself)
55 return Py_BuildValue("(ffff)",
56 itself->origin.x,
57 itself->origin.y,
58 itself->size.width,
59 itself->size.height);
62 int
63 CGRect_Convert(PyObject *v, CGRect *p_itself)
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;
74 PyObject *CGAffineTransform_New(CGAffineTransform *itself)
77 return Py_BuildValue("(ffffff)",
78 itself->a,
79 itself->b,
80 itself->c,
81 itself->d,
82 itself->tx,
83 itself->ty);
86 int
87 CGAffineTransform_Convert(PyObject *v, CGAffineTransform *p_itself)
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;
99 """
101 class MyOpaqueByValueType(OpaqueByValueType):
102 """Sort of a mix between OpaqueByValueType and OpaqueType."""
103 def mkvalueArgs(self, name):
104 return "%s, &%s" % (self.new, name)
106 CGPoint = MyOpaqueByValueType('CGPoint', 'CGPoint')
107 CGRect = MyOpaqueByValueType('CGRect', 'CGRect')
108 CGAffineTransform = MyOpaqueByValueType('CGAffineTransform', 'CGAffineTransform')
110 char_ptr = Type("char *", "s")
112 CGTextEncoding = int
113 CGLineCap = int
114 CGLineJoin = int
115 CGTextDrawingMode = int
116 CGPathDrawingMode = int
117 CGInterpolationQuality = int
119 # The real objects
120 CGContextRef = OpaqueByValueType("CGContextRef", "CGContextRefObj")
123 class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
124 def outputStructMembers(self):
125 ObjectDefinition.outputStructMembers(self)
126 def outputCleanupStructMembers(self):
127 Output("CGContextRelease(self->ob_itself);")
130 # Create the generator groups and link them
131 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
133 CGContextRef_object = MyObjectDefinition('CGContextRef', 'CGContextRefObj', 'CGContextRef')
136 # ADD object here
138 module.addobject(CGContextRef_object)
142 Function = FunctionGenerator
143 Method = MethodGenerator
145 CGContextRef_methods = []
147 # ADD _methods initializer here
148 execfile(INPUTFILE)
150 # manual method, lives in Quickdraw.h
151 f = Method(void, 'SyncCGContextOriginWithPort',
152 (CGContextRef, 'ctx', InMode),
153 (CGrafPtr, 'port', InMode),
155 CGContextRef_methods.append(f)
157 # manual method, lives in Quickdraw.h
158 f = Method(void, 'ClipCGContextToRegion',
159 (CGContextRef, 'ctx', InMode),
160 (Rect, 'portRect', InMode),
161 (RgnHandle, 'region', InMode),
163 CGContextRef_methods.append(f)
166 CreateCGContextForPort_body = """\
167 GrafPtr port;
168 CGContextRef ctx;
169 OSStatus _err;
171 if (!PyArg_ParseTuple(_args, "O&", GrafObj_Convert, &port))
172 return NULL;
174 _err = CreateCGContextForPort(port, &ctx);
175 if (_err != noErr)
176 if (_err != noErr) return PyMac_Error(_err);
177 _res = Py_BuildValue("O&", CGContextRefObj_New, ctx);
178 return _res;
181 f = ManualGenerator("CreateCGContextForPort", CreateCGContextForPort_body);
182 f.docstring = lambda: "(CGrafPtr) -> CGContextRef"
183 module.add(f)
186 # ADD add forloop here
187 for f in CGContextRef_methods:
188 CGContextRef_object.add(f)
190 # generate output (open the output file as late as possible)
191 SetOutputFileName(OUTPUTFILE)
192 module.generate()