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
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>
30 #include <ApplicationServices/ApplicationServices.h>
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. */
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.
54 FSRef frameworksFolderRef;
63 // Find the frameworks folder and create a URL for it.
65 err = FSFindFolder(kOnAppropriateDisk, kFrameworksFolderType, true, &frameworksFolderRef);
67 baseURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &frameworksFolderRef);
69 err = coreFoundationUnknownErr;
73 // Append the name of the framework to the URL.
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.
88 *bundlePtr = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL);
89 if (*bundlePtr == nil) {
90 err = coreFoundationUnknownErr;
94 if ( ! CFBundleLoadExecutable( *bundlePtr ) ) {
95 err = coreFoundationUnknownErr;
101 if (err != noErr && *bundlePtr != nil) {
102 CFRelease(*bundlePtr);
105 if (bundleURL != nil) {
106 CFRelease(bundleURL);
108 if (baseURL != nil) {
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;
140 extern int GrafObj_Convert(PyObject *, GrafPtr *);
146 PyObject *CGPoint_New(CGPoint *itself)
149 return Py_BuildValue("(ff)",
155 CGPoint_Convert(PyObject *v, CGPoint *p_itself)
157 if( !PyArg_Parse(v, "(ff)",
164 PyObject *CGRect_New(CGRect *itself)
167 return Py_BuildValue("(ffff)",
171 itself->size.height);
175 CGRect_Convert(PyObject *v, CGRect *p_itself)
177 if( !PyArg_Parse(v, "(ffff)",
180 &p_itself->size.width,
181 &p_itself->size.height) )
186 PyObject *CGAffineTransform_New(CGAffineTransform *itself)
189 return Py_BuildValue("(ffffff)",
199 CGAffineTransform_Convert(PyObject *v, CGAffineTransform *p_itself)
201 if( !PyArg_Parse(v, "(ffffff)",
213 initstuff
= initstuff
+ """
214 #if !TARGET_API_MAC_OSX
215 CFBundleRef sysBundle;
218 if (&LoadFrameworkBundle == NULL) {
219 PyErr_SetString(PyExc_ImportError, "CoreCraphics not supported");
222 err = LoadFrameworkBundle(CFSTR("ApplicationServices.framework"), &sysBundle);
224 err = CFMLateImportBundle(&gFragToFixLocator, gFragToFixConnID, FragmentInit, "\pCGStubLib", sysBundle);
226 PyErr_SetString(PyExc_ImportError, "CoreCraphics not supported");
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")
246 CGTextDrawingMode
= int
247 CGPathDrawingMode
= int
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')
268 module
.addobject(CGContextRef_object
)
272 Function
= FunctionGenerator
273 Method
= MethodGenerator
275 CGContextRef_methods
= []
277 # ADD _methods initializer here
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
= """\
301 if (!PyArg_ParseTuple(_args, "O&", GrafObj_Convert, &port))
304 _err = CreateCGContextForPort(port, &ctx);
306 if (_err != noErr) return PyMac_Error(_err);
307 _res = Py_BuildValue("O&", CGContextRefObj_New, ctx);
311 f
= ManualGenerator("CreateCGContextForPort", CreateCGContextForPort_body
);
312 f
.docstring
= lambda: "(CGrafPtr) -> CGContextRef"
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
)