views:

166

answers:

2

Initially I created a simple program with a custom NSView. I drew a picture (certificate) and printed it! beautiful! Everything worked great!

I them moved my custom NSView to an existing application. My hope was that when a user hit print it would print this certificate. Simple enough. I figured a could have a NSView pointer in my controller code. Then at initialization I would populate the pointer. Then when someone wanted to print the certificate it would print. The problem is that all of my drawing code is in the "drawRect" method. This method doesn't get called because this view is never displayed in a window.

I have heart that others use non-visible NSView objects just for printing. What do I need to do. I really don't want to show this view to the screen.

Rodger

+1  A: 

You don't have to create the view in advance, you can create it when needed.

If you have Document Based application and a view that you want to dump to printer, then in our MyDocument (or whatever you call it) that extends NSDocument you would implement:

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)ps
                                           error:(NSError **)e

The view then uses standard drawRect: for drawing.

Example, here PeopleView just draws a table of people details, it takes a NSDictonary of people employees here:

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)ps
                                           error:(NSError **)e
{
    PeopleView * view = [[PeopleView alloc] initWithPeople:employees];
    NSPrintInfo * printInfo = [self printInfo];
    NSPrintOperation * printOp
        = [NSPrintOperation printOperationWithView:view
                                         printInfo:printInfo];
    [view release];
    return printOp;
}

You can look for more details in chapter 27, Printing, in Hillegass' Cocoa Programming For Mac OS X.

stefanB
Don't you have to set the frame of the view? Or does the print op do that for you?
Peter Hosey
No, this is all you have to do if you have view that can draw itself. I created the example and it works. I'm using the data in different ways and when I want to print them I use the `PeopleView`.
stefanB
A: 

This is my code...

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)ps error:(NSError **)e
{
    NSLog(@"MyDoc printOp");
    StretchView * view = [[StretchView alloc] init];
    NSPrintInfo * printInfo = [self printInfo];
    NSPrintOperation * printOp
    = [NSPrintOperation printOperationWithView:view
                                     printInfo:printInfo];
    [view release];
    return printOp;
}

This is my log...

unning…
2010-03-15 10:10:42.251 spelling_tutor[6772:a0b] MyDoc printOp

I have a nslog entry in the draw routine. As you can see it is never called. What gives.

RW
I figured out my bug. It was with the initialization. I only had one initialization routine which required dems. If you look above I didn't give any. I changed my code above to initialize an NSRec then give it to StretchView. After that my draw code was called.
RW