Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 18, 2006, 05:06 PM   #1
cblackburn
macrumors regular
 
Join Date: Jul 2005
Location: London, UK
Printing NSView subclass

Hey all,

I have a custom class that inherits off NSView and I want to be able to print it. I want it to cover the whole of one page when it prints, so I overrode the print: function as so:-

Code:
// Make sure we use all of the page when printing
- (void)print:(id)sender
{
        NSPrintOperation *op = [NSPrintOperation currentOperation];
        NSPrintInfo *pInfo = [op printInfo];
       
        [pInfo setHorizontalPagination:NSFitPagination];
        [pInfo setVerticalPagination:NSFitPagination];
       
        [op setPrintInfo:pInfo];
       
        // Continue printing
        [super print:sender];
}
However it does not cover the whole page. What am I doing wrong?

Chris
cblackburn is offline   0 Reply With Quote
Old Dec 18, 2006, 05:09 PM   #2
cblackburn
Thread Starter
macrumors regular
 
Join Date: Jul 2005
Location: London, UK
I have also tried this to no avail

Code:
- (void)awakeFromNib
{
	[[NSPrintInfo sharedPrintInfo] setVerticalPagination:NSFitPagination];
	[[NSPrintInfo sharedPrintInfo] setHorizontalPagination:NSFitPagination];
}
cblackburn is offline   0 Reply With Quote
Old Dec 18, 2006, 05:39 PM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
print: is the action method sent down the responder chain. You should probably not override this within the view.

All drawing for printing gets done in the normal drawRect: method. You can use the NSGraphics context isDrawingToScreen to discover if you are printing.

If you want/need to alter the print info like that the view is totally the wrong place to do it. That's controller code and belongs in the controller. If you are using a document based app put it in NSDocument, otherwise the NSApplication delegate might be a good place...
robbieduncan is offline   0 Reply With Quote
Old Dec 18, 2006, 05:47 PM   #4
cblackburn
Thread Starter
macrumors regular
 
Join Date: Jul 2005
Location: London, UK
Quote:
Originally Posted by robbieduncan View Post
otherwise the NSApplication delegate might be a good place...
How would I do that, do I need to do something in Interface builder?

Chris
cblackburn is offline   0 Reply With Quote
Old Dec 18, 2006, 06:15 PM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
You'd create a new object, instansiate it in Interface Builder and connect it to the delegate outlet of the File's Owner in MainMenu.nib.
robbieduncan is offline   0 Reply With Quote
Old Dec 18, 2006, 06:20 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Scratch that: it doesn't seem to work as something higher in the responder chain is getting the print: message, probably the window.
robbieduncan is offline   0 Reply With Quote
Old Dec 18, 2006, 06:29 PM   #7
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
OK I've read the documentation. You can have print: in the view if you want but the print: operation will only get routed to that view if it's got focus. I don't think your print: is ever getting called. You could subclass NSWindow and override it there...

I'd drop some NSLogs into see what's getting called where...
robbieduncan is offline   0 Reply With Quote
Old Dec 18, 2006, 06:29 PM   #8
cblackburn
Thread Starter
macrumors regular
 
Join Date: Jul 2005
Location: London, UK
Quote:
Originally Posted by robbieduncan View Post
Scratch that: it doesn't seem to work as something higher in the responder chain is getting the print: message, probably the window.
I tried connecting the delagate connection of Files Owner and the window containing the control to my main controller class, then placing the afformentioned code in awakeFromNib, but neither has worked
cblackburn is offline   0 Reply With Quote
Old Dec 18, 2006, 06:38 PM   #9
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
I think you are trying to use pagination wrong. Pagination breaks the image up onto one or more pages. So what you are doing is say fit the contents on a single page. This will scale the content to fit on that page. It will only scale the content down though. As far as I am aware if you want to scale the content up to fill the page you have to do it yourself in drawRect:
robbieduncan is offline   0 Reply With Quote
Old Dec 18, 2006, 06:51 PM   #10
cblackburn
Thread Starter
macrumors regular
 
Join Date: Jul 2005
Location: London, UK
Quote:
Originally Posted by robbieduncan View Post
I think you are trying to use pagination wrong. Pagination breaks the image up onto one or more pages. So what you are doing is say fit the contents on a single page. This will scale the content to fit on that page. It will only scale the content down though. As far as I am aware if you want to scale the content up to fill the page you have to do it yourself in drawRect:
Hmm, well, my drawRect is written such that it will fill whatever rect it is given. It seems that when it prints it only gets a rect for a small portion of the piece of paper.
cblackburn is offline   0 Reply With Quote
Old Dec 18, 2006, 06:54 PM   #11
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
I don't believe the rect gets changed when you print: it's the one in the view. To do what you want to do you probably need to resize the view or copy the view to the correct size when you print.

Or ask the print info what size the paper is and try drawing to that size...
robbieduncan is offline   0 Reply With Quote
Old Dec 18, 2006, 07:16 PM   #12
cblackburn
Thread Starter
macrumors regular
 
Join Date: Jul 2005
Location: London, UK
Quote:
Originally Posted by robbieduncan View Post
OK I've read the documentation. You can have print: in the view if you want but the print: operation will only get routed to that view if it's got focus. I don't think your print: is ever getting called. You could subclass NSWindow and override it there...

I'd drop some NSLogs into see what's getting called where...
Eugh, that sounds wonderfully horrific. *sigh* it's gonna be a long night.
cblackburn is offline   0 Reply With Quote
Old Dec 18, 2006, 07:29 PM   #13
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Here's an example project to get you started. It manages to do what you want, although the state of the view after the print operation is messed up so you might want to sort that.

I'm not saying this is a good way to do this: I've never looked at the printing system too much before.

Now I've really got to get some sleep!
Attached Files
File Type: zip Test.zip (34.2 KB, 207 views)
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: Trying to subclass NSView Hans Kamp Mac Programming 10 Aug 31, 2014 10:39 AM
Add elements to View from subclass Pades iPhone/iPad Programming 1 May 9, 2014 07:51 AM
Subclassing a subclass - question regarding IBOutlets and IBActions moonman239 iPhone/iPad Programming 1 Sep 27, 2013 09:15 PM
Custom NSView with subview of NSSplitView ArtOfWarfare Mac Programming 3 Aug 20, 2013 06:16 AM
Trying to subclass UIView Hans Kamp iPhone/iPad Programming 3 Mar 25, 2013 01:15 AM

Forum Jump

All times are GMT -5. The time now is 07:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC