Re: Text Editor
Re: Text Editor
- Subject: Re: Text Editor
- From: "Erik M. Buck" <email@hidden>
- Date: Wed, 21 Nov 2001 11:48:34 -0600
See:
NSSpecialPageOrder
NSPrintInfo
NSView
See the TextEdit.app example: look at MultiplePageView and particularly
at -rectForPage:
To print only alterante pages, I would implement -knowsPagesFirst:last: to
return the correct first page (1 or 2) and half the number of pages. Then
implement -rectForPage: to return the rect for (pageNumber -
firstPageNumber) * 2 + pageNumber.
Pagination
When an NSView is printed onto pages smaller than itself, it tiles itself
out onto separate logical pages so that its entire visible region is
printed. A subclass of NSView can alter the way pagination is performed by
overriding two small sets of methods. The first set affects automatic
pagination; the second replaces automatic pagination completely. One extra
method allows the NSView to adjust the location of the printed image on the
page. Finally, after pagination has actually been performed, the NSView is
given the chance to draw additional marks on the page.
NSView's automatic pagination tries to fit as much of the view being printed
onto a logical page, slicing the view into the largest possible chunks. This
is sufficient for many views, but if a view's image must be divided only at
certain places-between lines of text or cells in a table, for example, the
view can adjust the automatic mechanism to accommodate this by reducing the
height or width of each page. It does so by overriding up to four methods.
adjustPageHeightNew:top:bottom:limit: provides an out parameter for the new
bottom coordinate of the page, followed by the proposed top and bottom. An
additional parameter limits the height of the page; the bottom can't be
moved above it. adjustPageWidthNew:left:right:limit: works in the same way
to allow the view to adjust the width of a page. The limits are calculated
as a percentage of the proposed page's height or width. Your view subclass
can also customize this percentage by overriding the methods
heightAdjustLimit and widthAdjustLimit to return the reducible fraction of
the page.
More complex views, such as those that display separate pages over a
background, need to direct their own pagination. An NSView subclass that
needs to do so overrides the knowsPageRange: method to return YES, which
signals that it will be calculating each page's dimensions, and returns by
reference its first and last page numbers. The pagination machinery then
uses these numbers, sending rectForPage: to the NSView, which uses the page
number and the current printing information to calculate an appropriate
rectangle in its coordinate system. The adjustPage... methods aren't used in
this case.
The last stage of pagination involves placing the image to be printed on the
logical page. NSView's locationOfPrintRect: places it according to the
NSPrintInfo's status. By default it places the image in the upper left
corner of the page, but if NSPrintInfo's isHorizontallyCentered or
isVerticallyCentered methods return YES, it centers a single-page image
along the appropriate axis. A multiple-page document, however, is always
placed so that the divided pieces can be assembled at their edges.
After the NSView has sliced out a rectangle and positioned it on a page,
it's given two chances to add extra marks to the page, such as crop marks or
fold lines. drawPageBorderWithSize: is used for logical pages, and is
invoked for each paginated portion of the view. drawSheetBorderWithSize: is
used for actual physical pages, or sheets, on which one or more logical
pages may be laid out. In a 2-up printing, for example, the former method is
invoked twice for each sheet, while the latter is invoked once for each
sheet.
Communicating with the Window Server During Printing
While an NSView is printing, its connection to the Window Server is replaced
by a connection to the print job output. Sometimes the NSView needs to
communicate briefly with the Window Server while printing; for example, it
may need to read some data stored only on the Window Server, or open an
attention panel to alert the user of a problem. In these cases, it can
temporarily swap in the NSApplication object's display context to restore
access to the application's Window Server state and to the screen. When
finished, the view object restores the print operation's context to continue
generating its image.