PDA

View Full Version : Another puzzle with NSViews




phjo
Apr 4, 2008, 11:15 AM
Hi,

I have a problem when printing or exporting to pdf a view :

Suppose you have a window containing just a view, let's say a myView which inherits of course from NSView.

Suppose the drawRect: method is this :

- (void)drawRect:(NSRect)rect {
CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(context,0,0,1,1);
CGContextSelectFont(context,"Helvetica",2,kCGEncodingMacRoman);
CGContextShowTextAtPoint(context,20,20,"test",4);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 20,18);
CGContextAddLineToPoint(context, 65,18);
CGContextStrokePath(context);
CGContextFlush(context);
}

Then you get a simple window which, when you select Print from the Edit menu and Preview will give you a representation of your window, with the window border and title (and weird titlebar corners in my experience), and the view inside it.

So far so good...

Suppose you now add this to the myView method :

-(BOOL)acceptsFirstResponder
{
return YES;
}

then the Print command won't print the border of your window, nor its title, but only the content of the view inside the window which sounds reasonable enough.

But, it appears that although the text is still there, precisely where it should be, you have too look hard to see it, as it appears in a microscopic size... The lines, rect or whatever objects you drew on the view do appear in the same size as previously...

I tested on my macbook (10.4.11, xcode 2.4) and mac pro (10.5.2, xcode 3.0), both exhibited the same behavior...

As usual, I wonder... Am I doing something wrong ? and whatever the answer to this first question, how do I fix this ?

phjo



Eraserhead
Apr 4, 2008, 11:46 AM
I believe according to Apple subviews should be inside an empty NSTabView.

phjo
Apr 4, 2008, 12:28 PM
That sounds weird, as I couldn't see anything in NSView class documentation about that... Nothing in AAron Hillegass book as well. Do you have a source about it ?

Anyway, I redid the UI with embedding my view inside an empty NSTabView, and the behavior I described still applies so it isn't that...

phjo

Eraserhead
Apr 4, 2008, 12:45 PM
Hmm, I don't seem to be able to find any obvious references. I have always used NSTabView's though, and it wasn't for no reason. <shrug> it may not be the best way to do it.

EDIT: I think this (http://www.cocoadev.com/index.pl?NSViewQuestions) is where I got my information.

phjo
Apr 4, 2008, 11:26 PM
If I am not misstaken, you would use a tabless NSTabView when you want to be able (not by explicit user interaction) to switch views inside your window...

It makes no sense if you only have one view instance, which is my case... And obviously it does not fix the problem I wrote about.

phjo

Eraserhead
Apr 5, 2008, 04:32 AM
^^ Ah OK, that is true for me sometimes, and others I'm already using a tab view :o.

Sorry for derailing your thread.

Back to the question.


But, it appears that although the text is still there, precisely where it should be, you have too look hard to see it, as it appears in a microscopic size... The lines, rect or whatever objects you drew on the view do appear in the same size as previously...


That is the weirdest thing I've ever seen, it sounds like a bug if I'm honest.

I do agree that printing in Cocoa is pretty horrible, and doesn't work very well, though it doesn't involve writing much code, maybe its worth writing the print code manually and see what happens?

phjo
Apr 6, 2008, 12:09 AM
I'm still interested in an explanation if you have one, but for the record, I filed a bug report, and I replaced the Quartz text drawing with the AppKit directives of NSString which work fine, for screen and print...

phjo