home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / TextEdit / MultiplePageView.m < prev    next >
Text File  |  1996-08-12  |  5KB  |  181 lines

  1. /*
  2.         MultiplePageView.m
  3.     Copyright (c) 1995-1996, NeXT Software, Inc.
  4.         All rights reserved.
  5.         Author: Ali Ozer
  6.  
  7.     You may freely copy, distribute and reuse the code in this example.
  8.     NeXT disclaims any warranty of any kind, expressed or implied,
  9.     as to its fitness for any particular use.
  10.  
  11.         View which holds all the pages together in the multiple-page case
  12. */
  13.  
  14. #import <AppKit/AppKit.h>
  15. #import "MultiplePageView.h"
  16.  
  17. @implementation MultiplePageView
  18.  
  19. - (id)initWithFrame:(NSRect)rect {
  20.     if (self = [super initWithFrame:rect]) {
  21.         numPages = 0;
  22.         [self setLineColor:[NSColor lightGrayColor]];
  23.         [self setMarginColor:[NSColor whiteColor]];
  24.     /* This will set the frame to be whatever's appropriate... */
  25.         [self setPrintInfo:[NSPrintInfo sharedPrintInfo]];
  26.     }
  27.     return self;
  28. }
  29.  
  30. - (BOOL)isFlipped {
  31.     return YES;
  32. }
  33.  
  34. - (BOOL)isOpaque {
  35.     return YES;
  36. }
  37.  
  38. - (void)updateFrame {
  39.     if ([self superview]) {
  40.         NSRect rect = NSZeroRect;
  41.         rect.size = [printInfo paperSize];
  42.         rect.size.height = rect.size.height * numPages;
  43.         if (numPages > 1) rect.size.height += [self pageSeparatorHeight] * (numPages - 1);
  44.         rect.size = [self convertSize:rect.size toView:[self superview]];
  45.         [self setFrame:rect];
  46.     }
  47. }
  48.  
  49. - (void)setPrintInfo:(NSPrintInfo *)anObject {
  50.     if (printInfo != anObject) {
  51.         [printInfo autorelease];
  52.         printInfo = [anObject copyWithZone:[self zone]];
  53.         [self updateFrame];
  54.         [self setNeedsDisplay:YES];    /* Because the page size might change (could optimize this) */
  55.     }
  56. }
  57.  
  58. - (NSPrintInfo *)printInfo {
  59.     return printInfo;
  60. }
  61.  
  62. - (void)setNumberOfPages:(unsigned)num {
  63.     if (numPages != num) {
  64.     NSRect oldFrame = [self frame];
  65.         NSRect newFrame;
  66.         numPages = num;
  67.         [self updateFrame];
  68.     newFrame = [self frame];
  69.         if (newFrame.size.height > oldFrame.size.height) {
  70.         [self setNeedsDisplayInRect:NSMakeRect(oldFrame.origin.x, NSMaxY(oldFrame), oldFrame.size.width, NSMaxY(newFrame) - NSMaxY(oldFrame))];
  71.         }
  72.     }
  73. }
  74.  
  75. - (unsigned)numberOfPages {
  76.     return numPages;
  77. }
  78.     
  79. - (float)pageSeparatorHeight {
  80.     return 5.0;
  81. }
  82.  
  83. - (void)dealloc {
  84.     [printInfo release];
  85.     [super dealloc];
  86. }
  87.  
  88. - (NSSize)documentSizeInPage {
  89.     NSSize paperSize = [printInfo paperSize];
  90.     paperSize.width -= ([printInfo leftMargin] + [printInfo rightMargin]);
  91.     paperSize.height -= ([printInfo topMargin] + [printInfo bottomMargin]);
  92.     return paperSize;
  93. }
  94.  
  95. - (NSRect)documentRectForPageNumber:(unsigned)pageNumber {    /* First page is page 0, of course! */
  96.     NSRect rect = [self pageRectForPageNumber:pageNumber];
  97.     rect.origin.x += [printInfo leftMargin];
  98.     rect.origin.y += [printInfo topMargin];
  99.     rect.size = [self documentSizeInPage];
  100.     return rect;
  101. }
  102.  
  103. - (NSRect)pageRectForPageNumber:(unsigned)pageNumber {
  104.     NSRect rect;
  105.     rect.size = [printInfo paperSize];
  106.     rect.origin = [self frame].origin;
  107.     rect.origin.y += ((rect.size.height + [self pageSeparatorHeight]) * pageNumber);
  108.     return rect;
  109. }
  110.  
  111. - (void)setLineColor:(NSColor *)color {
  112.     if (color != lineColor) {
  113.         [lineColor autorelease];
  114.         lineColor = [color copyWithZone:[self zone]];
  115.         [self setNeedsDisplay:YES];
  116.     }
  117. }
  118.  
  119. - (NSColor *)lineColor {
  120.     return lineColor;
  121. }
  122.  
  123. - (void)setMarginColor:(NSColor *)color {
  124.     if (color != marginColor) {
  125.         [marginColor autorelease];
  126.         marginColor = [color copyWithZone:[self zone]];
  127.         [self setNeedsDisplay:YES];
  128.     }
  129. }
  130.  
  131. - (NSColor *)marginColor {
  132.     return marginColor;
  133. }
  134.  
  135. - (void)drawRect:(NSRect)rect {
  136.     if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  137.         NSSize paperSize = [printInfo paperSize];
  138.         unsigned firstPage = rect.origin.y / (paperSize.height + [self pageSeparatorHeight]);
  139.         unsigned lastPage = NSMaxY(rect) / (paperSize.height + [self pageSeparatorHeight]);
  140.         unsigned cnt;
  141.         
  142.         [marginColor set];
  143.         NSRectFill(rect);
  144.  
  145.         [lineColor set];
  146.         for (cnt = firstPage; cnt <= lastPage; cnt++) {
  147.             NSRect docRect = NSInsetRect([self documentRectForPageNumber:cnt], -1.0, -1.0);
  148.             NSFrameRectWithWidth(docRect, 0.0);
  149.         }
  150.  
  151.         if ([[self superview] isKindOfClass:[NSClipView class]]) {
  152.         NSColor *backgroundColor = [(NSClipView *)[self superview] backgroundColor];
  153.             [backgroundColor set];
  154.             for (cnt = firstPage; cnt <= lastPage; cnt++) {
  155.                 NSRect pageRect = [self pageRectForPageNumber:cnt];
  156.                 NSRectFill (NSMakeRect(pageRect.origin.x, NSMaxY(pageRect), pageRect.size.width, [self pageSeparatorHeight]));
  157.             }
  158.         }
  159.     }
  160. }
  161.  
  162. /**** Printing support... ****/
  163.  
  164. - (BOOL)knowsPagesFirst:(int *)firstPageNum last:(int *)lastPageNum {
  165.     *lastPageNum = [self numberOfPages];
  166.     return YES;     
  167. }
  168.  
  169. - (NSRect)rectForPage:(int)page {
  170.     return [self documentRectForPageNumber:page-1];  /* Our page numbers start from 0; the kit's from 1 */
  171. }
  172.  
  173. @end
  174.  
  175. /*
  176.  
  177.  2/16/95 aozer    Created for Edit II.
  178.  4/20/95 aozer    Implemented printing
  179.  
  180. */
  181.