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