Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Modules / cg / cgsupport.py
blob7456b55d9d578118ecc279d037459e05cef0b1be
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 #ifdef WITHOUT_FRAMEWORKS
27 #include <Quickdraw.h>
28 #include <CGContext.h>
29 #else
30 #include <ApplicationServices/ApplicationServices.h>
31 #endif
33 #if !TARGET_API_MAC_OSX
34 /* This code is adapted from the CallMachOFramework demo at:
35 http://developer.apple.com/samplecode/Sample_Code/Runtime_Architecture/CallMachOFramework.htm
36 It allows us to call Mach-O functions from CFM apps. */
38 #include <Folders.h>
39 #include "CFMLateImport.h"
41 static OSStatus LoadFrameworkBundle(CFStringRef framework, CFBundleRef *bundlePtr)
42 // This routine finds a the named framework and creates a CFBundle
43 // object for it. It looks for the framework in the frameworks folder,
44 // as defined by the Folder Manager. Currently this is
45 // "/System/Library/Frameworks", but we recommend that you avoid hard coded
46 // paths to ensure future compatibility.
48 // You might think that you could use CFBundleGetBundleWithIdentifier but
49 // that only finds bundles that are already loaded into your context.
50 // That would work in the case of the System framework but it wouldn't
51 // work if you're using some other, less-obvious, framework.
53 OSStatus err;
54 FSRef frameworksFolderRef;
55 CFURLRef baseURL;
56 CFURLRef bundleURL;
58 *bundlePtr = nil;
60 baseURL = nil;
61 bundleURL = nil;
63 // Find the frameworks folder and create a URL for it.
65 err = FSFindFolder(kOnAppropriateDisk, kFrameworksFolderType, true, &frameworksFolderRef);
66 if (err == noErr) {
67 baseURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &frameworksFolderRef);
68 if (baseURL == nil) {
69 err = coreFoundationUnknownErr;
73 // Append the name of the framework to the URL.
75 if (err == noErr) {
76 bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, baseURL, framework, false);
77 if (bundleURL == nil) {
78 err = coreFoundationUnknownErr;
82 // Create a bundle based on that URL and load the bundle into memory.
83 // We never unload the bundle, which is reasonable in this case because
84 // the sample assumes that you'll be calling functions from this
85 // framework throughout the life of your application.
87 if (err == noErr) {
88 *bundlePtr = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL);
89 if (*bundlePtr == nil) {
90 err = coreFoundationUnknownErr;
93 if (err == noErr) {
94 if ( ! CFBundleLoadExecutable( *bundlePtr ) ) {
95 err = coreFoundationUnknownErr;
99 // Clean up.
101 if (err != noErr && *bundlePtr != nil) {
102 CFRelease(*bundlePtr);
103 *bundlePtr = nil;
105 if (bundleURL != nil) {
106 CFRelease(bundleURL);
108 if (baseURL != nil) {
109 CFRelease(baseURL);
112 return err;
117 // The CFMLateImport approach requires that you define a fragment
118 // initialisation routine that latches the fragment's connection
119 // ID and locator. If your code already has a fragment initialiser
120 // you will have to integrate the following into it.
122 static CFragConnectionID gFragToFixConnID;
123 static FSSpec gFragToFixFile;
124 static CFragSystem7DiskFlatLocator gFragToFixLocator;
126 extern OSErr FragmentInit(const CFragInitBlock *initBlock);
127 extern OSErr FragmentInit(const CFragInitBlock *initBlock)
129 __initialize(initBlock); /* call the "original" initializer */
130 gFragToFixConnID = (CFragConnectionID) initBlock->closureID;
131 gFragToFixFile = *(initBlock->fragLocator.u.onDisk.fileSpec);
132 gFragToFixLocator = initBlock->fragLocator.u.onDisk;
133 gFragToFixLocator.fileSpec = &gFragToFixFile;
135 return noErr;
138 #endif
140 extern int GrafObj_Convert(PyObject *, GrafPtr *);
143 ** Manual converters
146 PyObject *CGPoint_New(CGPoint *itself)
149 return Py_BuildValue("(ff)",
150 itself->x,
151 itself->y);
155 CGPoint_Convert(PyObject *v, CGPoint *p_itself)
157 if( !PyArg_Parse(v, "(ff)",
158 &p_itself->x,
159 &p_itself->y) )
160 return 0;
161 return 1;
164 PyObject *CGRect_New(CGRect *itself)
167 return Py_BuildValue("(ffff)",
168 itself->origin.x,
169 itself->origin.y,
170 itself->size.width,
171 itself->size.height);
175 CGRect_Convert(PyObject *v, CGRect *p_itself)
177 if( !PyArg_Parse(v, "(ffff)",
178 &p_itself->origin.x,
179 &p_itself->origin.y,
180 &p_itself->size.width,
181 &p_itself->size.height) )
182 return 0;
183 return 1;
186 PyObject *CGAffineTransform_New(CGAffineTransform *itself)
189 return Py_BuildValue("(ffffff)",
190 itself->a,
191 itself->b,
192 itself->c,
193 itself->d,
194 itself->tx,
195 itself->ty);
199 CGAffineTransform_Convert(PyObject *v, CGAffineTransform *p_itself)
201 if( !PyArg_Parse(v, "(ffffff)",
202 &p_itself->a,
203 &p_itself->b,
204 &p_itself->c,
205 &p_itself->d,
206 &p_itself->tx,
207 &p_itself->ty) )
208 return 0;
209 return 1;
213 initstuff = initstuff + """
214 #if !TARGET_API_MAC_OSX
215 CFBundleRef sysBundle;
216 OSStatus err;
218 if (&LoadFrameworkBundle == NULL) {
219 PyErr_SetString(PyExc_ImportError, "CoreCraphics not supported");
220 return;
222 err = LoadFrameworkBundle(CFSTR("ApplicationServices.framework"), &sysBundle);
223 if (err == noErr)
224 err = CFMLateImportBundle(&gFragToFixLocator, gFragToFixConnID, FragmentInit, "\pCGStubLib", sysBundle);
225 if (err != noErr) {
226 PyErr_SetString(PyExc_ImportError, "CoreCraphics not supported");
227 return;
229 #endif /* !TARGET_API_MAC_OSX */
232 class MyOpaqueByValueType(OpaqueByValueType):
233 """Sort of a mix between OpaqueByValueType and OpaqueType."""
234 def mkvalueArgs(self, name):
235 return "%s, &%s" % (self.new, name)
237 CGPoint = MyOpaqueByValueType('CGPoint', 'CGPoint')
238 CGRect = MyOpaqueByValueType('CGRect', 'CGRect')
239 CGAffineTransform = MyOpaqueByValueType('CGAffineTransform', 'CGAffineTransform')
241 char_ptr = Type("char *", "s")
243 CGTextEncoding = int
244 CGLineCap = int
245 CGLineJoin = int
246 CGTextDrawingMode = int
247 CGPathDrawingMode = int
249 # The real objects
250 CGContextRef = OpaqueByValueType("CGContextRef", "CGContextRefObj")
253 class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
254 def outputStructMembers(self):
255 ObjectDefinition.outputStructMembers(self)
256 def outputCleanupStructMembers(self):
257 Output("CGContextRelease(self->ob_itself);")
260 # Create the generator groups and link them
261 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
263 CGContextRef_object = MyObjectDefinition('CGContextRef', 'CGContextRefObj', 'CGContextRef')
266 # ADD object here
268 module.addobject(CGContextRef_object)
272 Function = FunctionGenerator
273 Method = MethodGenerator
275 CGContextRef_methods = []
277 # ADD _methods initializer here
278 execfile(INPUTFILE)
280 # manual method, lives in Quickdraw.h
281 f = Method(void, 'SyncCGContextOriginWithPort',
282 (CGContextRef, 'ctx', InMode),
283 (CGrafPtr, 'port', InMode),
285 CGContextRef_methods.append(f)
287 # manual method, lives in Quickdraw.h
288 f = Method(void, 'ClipCGContextToRegion',
289 (CGContextRef, 'ctx', InMode),
290 (Rect, 'portRect', InMode),
291 (RgnHandle, 'region', InMode),
293 CGContextRef_methods.append(f)
296 CreateCGContextForPort_body = """\
297 GrafPtr port;
298 CGContextRef ctx;
299 OSStatus _err;
301 if (!PyArg_ParseTuple(_args, "O&", GrafObj_Convert, &port))
302 return NULL;
304 _err = CreateCGContextForPort(port, &ctx);
305 if (_err != noErr)
306 if (_err != noErr) return PyMac_Error(_err);
307 _res = Py_BuildValue("O&", CGContextRefObj_New, ctx);
308 return _res;
311 f = ManualGenerator("CreateCGContextForPort", CreateCGContextForPort_body);
312 f.docstring = lambda: "(CGrafPtr) -> CGContextRef"
313 module.add(f)
316 # ADD add forloop here
317 for f in CGContextRef_methods:
318 CGContextRef_object.add(f)
320 # generate output (open the output file as late as possible)
321 SetOutputFileName(OUTPUTFILE)
322 module.generate()