Graphics Handle always null

Hello,
I need to get a Graphics Handle of either HandleTypeCGrafPtr or HandleTypeCGContextRef Type.
In both cases, I get a null Handle
I need it, for I want to draw a part of a NSTextView into a Graphics (Canvas or a Graphics to be printed)

Dim Pic as New Picture (300, 300, 24) Dim g as Graphics = Pic.Graphics Dim iHandle as integer = g.Handle (Graphics.HandleTypeCGrafPtr)

Same for

Dim iHandle as integer = g.Handle (Graphics.HandleTypeCGContextRef)

Is it a bug of Xojo ?
Is there another way to get a Mac Graphics Handle ?
TIA

AFAIK there is no Graphics.Handle for a picture. Graphis.Handle is only non-zero within the Paint event of controls and windows, most notably of course is the Canvas’s Paint event:

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Dim handle As Integer = g.Handle(Graphics.HandleTypeCGContextRef) ... End Sub

You can always create a fresh image context in the size you need, draw the entire view hierarchy (or a rectangle of it) of your control into it and then draw the context into an image, which in turn you can convert into a Xojo picture.

Creating the context and drawing the view hierachy takes some time; I would recommend to do this conversion outside your canvas draw event and rather attach the result as an image as described to avoid flicker. But maybe drawing the control’s view hierarchy into the canvas paint graphics object is fast enough though as the event provides you with the current context already.

Do you know which Core Graphics methods to call?

EDIT: To be a bit more verbose: No, it’s no Xojo error. A graphics context doesn’t exist outside of a paint event, unless you create one. In case you can read German, I have a short description on my blog: http://xojoblog.me/2015/07/10/ios-grafiken-fuer-fortgeschrittene-teil-i-iosgraphics-aka-cgcontext/

Works for me in an older Xojo on 10.10

HandleTypeCGContextRef works for Cocoa
HandleTypeCGrafPtr works for Carbon

[quote=238143:@Eli Ott]AFAIK there is no Graphics.Handle for a picture. Graphis.Handle is only non-zero within the Paint event of controls and windows, most notably of course is the Canvas’s Paint event:

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Dim handle As Integer = g.Handle(Graphics.HandleTypeCGContextRef) ... End Sub[/quote]

A Picture that has a non-Nil Graphics object should have a valid CGContext handle. The only place you definitely won’t get a handle back is using a control’s Graphics object outside of the Paint event (which is deprecated anyways).

Well, it seems that it is possible, the following code works :

[code]Dim Pic as New Picture (300, 300)

Dim iHandle as integer = Pic.Graphics.Handle(Pic.Graphics.HandleTypeCGContextRef)
Dim gPtr as Ptr = Ptr (iHandle)[/code]

I can’t use a paint event here because my NStextView instance is not intended to be displayed :

' Instance a new NSTextView, given a NSRect : aRect dim pNewTextView as Ptr = initWithFrame (allocFromString ("NSTextView"), aRect) ' Load RTF in it Dim data as Ptr = dataWithBytes (ClassFromString("NSData"), RtfString, LenB(RtfString)) dim range as NSRange replaceCharactersInRange (pNSTextView, range, data) ' Get a bitmap and draw in it dim pNSBitmapImageRep as Ptr = bitmapImageRepForCachingDisplayInRect (pNewTextView, aRect) CacheDisplayInRectToBitmapImageRep (pNewTextView, aRect, pNSBitmapImageRep) ' Conversion into a CGImage dim pCGImage As Ptr = CGImage (pNSBitmapImageRep) ' We draw the CGImage into Pic Dim Pic as New Picture (aRect.Width, aRect.Height ) Dim iHandle as integer = Pic.Graphics.Handle(Pic.Graphics.HandleTypeCGContextRef) Dim gPtr as Ptr = Ptr (iHandle) Dim cgr as CGRect ' ==> Convert here aRect (= NSRect) to cgr (= CGRect) CGContextDrawImage (gPtr, cgr, pCGImage) ReleasePtr (pNewTextView)

I can then scale and draw Pic anywhere, in a Canvas or in some clipped region on a printer graphics …