Hijacking NSGraphicsContext

tigakub
Unregistered
 
Post: #1
Hi sorry if this is noobish.

Is there a way to "redirect" the drawing of an NSView from the windows graphics context to my own bitmap context?

The reason I want to do this is I want to capture all drawing done by a standard NSTextView into a bitmap without letting any of it go to the screen.

I've tried subclassing NSGraphicsContext and using NSView's allocateGState and overriding its setupGState. I'm not entirely sure when I should make my custom NSGraphicsContext the current context. Should I be overriding [NSView lockFocus] and [NSView unlockFocus] to call [NSGraphicsContext setCurrentContext: myContext] and [NSGraphicsContext setCurrentContext: prevContext]?

Does anyone have any insight into how the AppKit works at this level? There doesn't seem to be much documentation out there on this. Or should I be looking at capturing the backstore while somehow preventing it from being flushed to the screen?

What is the best/correct way of doing this?
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #2
You can -lockFocus on an NSImage or NSBitmapImageRep directly. Try searching cocoabuilder for "bitmap lockFocus".

However, if you want to do any kind of animation this way (i.e. render to bitmap, manipulate bitmap, draw bitmap to screen, repeat) forget about using NSImage. This is one of the really dumb parts of Cocoa, NSBitmapImageReps are changed into NSCachedImageReps as soon as you draw them and you can no longer get your modified bytes back out. You can use CoreGraphics bitmap contexts instead, it just makes for uglier code.
Quote this message in a reply
tigakub
Unregistered
 
Post: #3
Thanks! This might just work! I'll give it a try.

I don't really intend to use NSImage to draw the image to the screen, but to use glTexSubImage2D to copy it to an OpenGL texture. So I don't think I'll be touching the AppKit blting routines at all.

I'd actually prefer to use Core Graphics because I get the feeling that the AppKit is just an extra layer over it anyway. But I don't want to reproduce the functionality of say NSTextView, or the other nifty widgets.

I'm already using CG bitmap contexts to draw Quartz graphics into OpenGL textures, but I can't figure out how to marry CG bitmap contexts with NSGraphicsContexts in the NSView heirarchy.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #4
Since there is now a public blurb about it, I can tell you that Tiger has new support for redirecting NSView drawing like you originally asked about.
Quote this message in a reply
tigakub
Unregistered
 
Post: #5
arekkusu Wrote:Since there is now a public blurb about it, I can tell you that Tiger has new support for redirecting NSView drawing like you originally asked about.

Excellent. Looks like Apple thought of everything ... again.
Quote this message in a reply
Post Reply