Cocoa’s printing architecture is composed of a half-dozen interconnected classes. Separate classes represent the Page Setup panel, the Print panel, the print settings, the print operation, the printer, and the object to be printed. Each of these classes is described here.
NSView generates the data to be printed. Because Cocoa drawing is device independent, a view generates print commands simply by drawing itself with regular Application Kit and Core Graphics drawing commands after the display device has been changed to a printer. A view can be told to print itself by invoking its print:
method.
NSPrintInfo stores the options that control how a print job is performed. This information includes the paper size, number of copies, print margins, and so on. A shared instance holding default settings is used by the other objects of the print system if you do not provide your own instance for a particular print job.
NSPageLayout displays the Page Setup panel where the user selects the paper type and orientation. The settings are stored in an NSPrintInfo object. You can add a custom accessory view to the Page Setup panel to display application-specific options.
NSPrintPanel displays the Print panel where the user selects the output options for a print job, such as the number of copies and the pages to print. The settings are stored in an NSPrintInfo object. You can add a custom accessory view to the Print panel to display application-specific options.
NSPrintOperation manages a print job. It displays the Print panel, optionally spawns a new thread to process the print job, sets up the print environment, and tells the NSView to print itself. It can also generate Encapsulated PostScript (EPS) or Portable Document Format (PDF) data instead of sending the results to a printer.
NSPrinter describes a printer’s capabilities as defined in its PostScript Printer Description (PPD) file. (Despite its name, non-PostScript printers can have PPDs, too.) Printers can be specified either by name or by type (make and model). You can also use several class methods to obtain a list of all the printers, organized either by name or by type, that the user has added to his or her printer list.
Printing is generally initiated by the user choosing the Print menu command, which usually sends either a print:
or printDocument:
message, depending on whether the application is NSDocument-based or not, up the responder chain. You receive the message either in a custom NSView object (if it has the keyboard focus), a window delegate, or an NSDocument object. Upon receipt, you create an NSPrintOperation object to manage the print job, tell it which NSView to print, add an accessory view to its print panel, if desired, and then run the operation. The NSView class defines several methods that you can override to control how the view is divided between multiple pages. From there, the view’s drawRect:
method draws the view’s contents.
Subsequent sections of this document describe in greater detail how each part of Cocoa’s printing architecture works.
© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-06-28)