Summary:
How can I draw some text to an offscreen view and then print it? Looking for
a simple way to do this.
Details:
I have a bunch of NSTextFields in a window, the contents of which must be
printed. There is some other stuff in the window however, which I do not
want print with the text. Also the text must be repositioned for printing.
So, I decided the easiest way would be to copy the text from all of the
NSTextFields into an offscreen view, and then print that. But, I am running
into problems. Here is what I did:
1.. Created a very basic subclass of NSView.
2.. Instantiate this subclass from code using alloc/initwithframe.
3.. Attempt to lockfocus on this NSView, so that I can draw text to it.
This is where I run into problems. Upon calling lockfocus I am given the
message in Run Log: "lockfocus sent to a view whose window is deferred and
does not yet have a corresponding platform window".
Do I have to put the view in a window if I want to draw offscreen? This
means I will have to instantiate the window as well as hide it, which is
more complicated than I had intended. It looks like my other option is the
unpublished defaultGlyphForChar method, using NSBezierPath to draw the text.
Again, this is not as simple as I was looking for. Is there an easy way to
draw text offscreen?
matt neuburg - 31 Dec 2004 05:12 GMT
> How can I draw some text to an offscreen view and then print it? Looking for
> a simple way to do this.
[quoted text clipped - 18 lines]
> message in Run Log: "lockfocus sent to a view whose window is deferred and
> does not yet have a corresponding platform window".
Sounds like there's something screwy with your architecture. Why are you
calling lockfocus at all? Just have the NSView's drawRect method
describe what you want printed. m.

Signature
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
AppleScript: The Definitive Guide
http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
Read TidBITS! It's free and smart. http://www.tidbits.com
Michael Szuchy - 31 Dec 2004 05:45 GMT
Yes... I think you are right. I must admit I am unfamiliar with how
subclassing NSView works, printing is the first time that I have had to deal
with it. I will try using drawRect and post my results.
> > How can I draw some text to an offscreen view and then print it? Looking for
> > a simple way to do this.
[quoted text clipped - 22 lines]
> calling lockfocus at all? Just have the NSView's drawRect method
> describe what you want printed. m.
Michael Szuchy - 31 Dec 2004 05:56 GMT
It worked! Thank you. Now on to learning how the whole printable area
rectangle thing is done.
> Yes... I think you are right. I must admit I am unfamiliar with how
> subclassing NSView works, printing is the first time that I have had to deal
[quoted text clipped - 33 lines]
> > calling lockfocus at all? Just have the NSView's drawRect method
> > describe what you want printed. m.