As I understand it, NS graphics objects draw to the current context… so for example in the drawRect method of an NSView, the current context is the on-screen graphics of the NSView.
You can draw into a CGContext by creating an NSContext with graphicsContextWithGraphicsPort and setting it to be the current context with setCurrentContext… you can also draw to an NSBitmapImageRep with graphicsContextWithBitmapImageRep.
The CG methods draw to an explicit CGContext. In Xojo, the graphics.handle is a CGContextRef. So you can easily use CG methods with picture.graphics.handle or from a paint event using g.handle.
I find the simplest method for use in Xojo is to either use the CG methods directly on picture.graphics or in a paint event.
Here’s that CGGradient method-
[code]Private Sub ApplyGradient(byref p As Picture,colors() As color,pnts() As Double)
//draws a gradient in p based on colors and points (colors are regular Xojo colors and points are 0.0 to 1.0)
soft declare sub CGContextDrawLinearGradient lib “Cocoa” _
( context as ptr, gradient as ptr, startPoint as CGPoint, endPoint as CGPoint, options as UInt32)
soft declare function CGGradientCreateWithColorComponents lib “Cocoa” _
(space as ptr,components as ptr, locations as ptr,count as integer) as ptr
soft declare function CGColorSpaceCreateWithName lib “Cocoa” (name as CFStringRef) as ptr
soft declare sub CGColorSpaceRelease lib “Cocoa”(colorspace as ptr)
soft declare sub CGGradientRelease lib “Cocoa”(gradient as ptr)
const kCGColorSpaceGenericRGB=“kCGColorSpaceGenericRGB”
dim colorspace As Ptr=CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)
dim colorblock As new MemoryBlock(16*(colors.Ubound+1))
//colorblock is a memoryblock ala r g b a r g b a r g b a
for i as integer=0 to colors.Ubound
colorblock.SingleValue(i16)=colors(i).red/255
colorblock.SingleValue(i16+4)=colors(i).Green/255
colorblock.SingleValue(i16+8)=colors(i).Blue/255
colorblock.SingleValue(i16+12)=1-colors(i).Alpha/255
next
dim gradient As ptr
if pnts=nil then //if pnts is empty the gradient color will be evenly distributed
gradient=CGGradientCreateWithColorComponents(colorspace,colorblock,nil,colors.Ubound+1)
else
//locs is just a list of CGFloats representing points for the gradient stops.
dim locs As new MemoryBlock(4*(pnts.Ubound+1))
for i as integer=0 to pnts.Ubound
locs.SingleValue(i*4)=pnts(i)
next
gradient=CGGradientCreateWithColorComponents(colorspace,colorblock,locs,colors.Ubound+1)
end if
dim gstart As CGPoint
dim gend as CGPoint
gstart.y=p.Height
CGContextDrawLinearGradient(ptr(p.Graphics.handle(Graphics.handletypecgcontextref)),gradient,gstart,gend,0)
//clean up things we own
CGColorSpaceRelease(colorspace)
CGGradientRelease(gradient)
End Sub
[/code]