NSView drawRect Issue

Discussion in 'Mac Programming' started by renzil, Aug 7, 2009.

  1. renzil macrumors newbie

    Joined:
    Aug 5, 2009
    #1
    Hi,

    I'm using the following code to print the contents of a view.

    Code:
    NSPrintInfo* printInfo = [[NSPrintInfo sharedPrintInfo] copyWithZone:nil]; // should I release this?
    
        NSSize paperSize = [printInfo paperSize];
        NSRect printableRect = [printInfo imageablePageBounds];
    
        // calculate page margins
        float marginL = printableRect.origin.x;
        float marginR = paperSize.width - (printableRect.origin.x + printableRect.size.width);
        float marginB = printableRect.origin.y;
        float marginT = paperSize.height - (printableRect.origin.y + printableRect.size.height);
    
        // Make sure margins are symetric and positive
        float marginLR = MAX(0,MAX(marginL,marginR));
        float marginTB = MAX(0,MAX(marginT,marginB));
        
        // Tell printInfo what the nice new margins are
        [printInfo setLeftMargin:   marginLR];
        [printInfo setRightMargin:  marginLR];
        [printInfo setTopMargin:    marginTB];
        [printInfo setBottomMargin: marginTB];
    
        NSRect printViewFrame = {};
        printViewFrame.size.width = paperSize.width - marginLR*2;
        printViewFrame.size.height = paperSize.height - marginTB*2;
    
        NSString *nsImageFileName = [[NSString alloc] initWithCString:inImageFileName];
        NSURL *absURL = [[NSURL alloc] initFileURLWithPath:nsImageFileName];
        PrintView *view = [[PrintView alloc] initWithFrame:printViewFrame];	
        [view readFromURL:absURL error:nil];
        [view setNeedsDisplay:YES];
        [view print:nil];
    
        [view release];	
        [printInfo release];
        [nsImageFileName release];
        [absURL release];
    I have overridden 'drawRect' of the PrintView class, where I draw an image. The issue is, the 'rect' passed as argument to 'drawRect' is not the same as 'printViewFrame', so the contents of my view are clipped in the print panel's preview. Any clue why is this happening and how to rectify it?

    Also, my print preview hangs on 'Processing File' when I try to preview as PDF in Mac's 'Preview'. The same image file, when opened in 'Preview', works fine. Is there something more to be done to get basic printing right?

    Thanks for your time.
     
  2. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #2
    I see that you have not got any answer yet. I am a new in cocoa but I have an idea which may help, or will start discussion.

    Your calculations seems OK but:
    Maybe you need to use convert method in order to convert coordinates if they are not in the same user space.

    /petron
     
  3. renzil thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #3
    Thanks petron. It had nothing to do with the co-ordinate system. I managed to make it work using an NSPrintOperation, and passing it the same 'view' and 'printInfo'. I'm still not sure why the first case gave me an incorrect rect though.

    Also, I forgot to mention the app. I'm working on is a Carbon application. My "Open PDF in Preview" hangs in this app., so I made a Cocoa test app. to try it out and it worked just fine. But the 'rect' problem persists in the Cocoa app. as well.

    Any clue why 'Open PDF in Preview' would hang in a Carbon app. ?
     

Share This Page