Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[cocoa] Is it possible to draw text to an offscreen view that has no window?

2 views
Skip to first unread message

Michael Szuchy

unread,
Dec 31, 2004, 12:01:14 AM12/31/04
to
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

unread,
Dec 31, 2004, 12:12:15 AM12/31/04
to
Michael Szuchy <vast...@REMOVETHIScomcast.net> wrote:

> 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".

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.

--
matt neuburg, phd = ma...@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

unread,
Dec 31, 2004, 12:45:32 AM12/31/04
to
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.

Michael Szuchy

unread,
Dec 31, 2004, 12:56:20 AM12/31/04
to
It worked! Thank you. Now on to learning how the whole printable area
rectangle thing is done.
0 new messages