Re: [Pyobjc-dev] NSGraphicsContext.graphicsPort
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2005-04-19 16:46:15
|
On 19-apr-2005, at 18:13, Robert Kern wrote: > Ronald Oussoren wrote: > > >> It is not. What's more acceptable is to introduce a callback >> function that is used >> to construct a CGContextPtr proxy. PyObjC would ship with a >> callback function that >> uses the sillySwigThing code, you could replace that by a function >> that created instances >> of your CG-wrapper. >> > > Okay, what would this callback function take? The sillySwigThing > string? A Python long? A python long. However, I've just realised that adding a custom callback is not necessary. PyObjC contains enough generic machinery to reach our goal (see below). > > Will this callback get attached to NSGraphicsContext? or a module? > or just passed in as an argument to the method? The easiest way is using PyObjC's C-API (although the required header file currently not installed). As you're writing an extension module the additional C code shouldn't be a problem. Annoyingly the API that should be used is slightly flawed, but that is easily fixed. 1) change the signature for NSGraphicsContext.graphicsPort (in Lib/ AppKit/_AppKitSignatures.py). The signature currently says that the returntype is 'void*', this should be CGContextRef. 2) write two C/Pyrex functions, one for converting from Python to C and one for converting from C to Python. These functions convert from MacPython wrappers to CF types and back: static PyObject* CF_to_py(void* cfValue) { return PyObjC_IDToCFType((id)cfValue); } static int py_to_CF(PyObject* obj, void* output) { id tmp = PyObjC_CFTypeToID(obj); if (tmp == NULL && PyErr_Occurred()) { return -1; } *(void**)output = tmp; return 0; } 3) call PyObjCPointerWrapper_Register (from C) r = PyObjCPointerWrapper_Register(@encode(CFURLRef), (PyObjCPointerWrapper_ToPythonFunc)CF_to_py, (PyObjCPointerWrapper_FromPythonFunc)py_to_CF); if (r == -1) FAIL 4) Remove the C-wrappers for -graphicsPort from the _AppKit extension module and implement the default wrappers for CGContextRef (e.g. step 2, but for Apple's CoreGraphics wrappers). I'll do this (and step 1) later today. > >> For additional bonus points: harass me or Bob enough to finally >> work on extending >> PyObjC to work with CoreFoundation and use that work to wrap >> CoreGraphics. >> > > Wrap it again? Pass. I was only half serious. PyObjC will contain wrappers for CF and friends in the future, but that's a lot more work than the fix I sketched above. Ronald |