/* Copyright (c) 2006-2007 Christopher J. W. Lloyd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #import #import #import #import #import #import #import #import "NSPrintProgressPanelController.h" enum { NSPrintOperationPDFInRect, NSPrintOperationEPSInRect, NSPrintOperationPrinter, NSPrintOperationFile, }; @implementation NSPrintOperation static NSPrintOperation *_currentOperation=nil; +(NSPrintOperation *)currentOperation { return _currentOperation; } -initWithView:(NSView *)view printInfo:(NSPrintInfo *)printInfo insideRect:(NSRect)rect toData:(NSMutableData *)data type:(int)type { _view=[view retain]; _printInfo=[printInfo copy]; if(type==NSPrintOperationPDFInRect || type==NSPrintOperationEPSInRect || type==NSPrintOperationFile) [_printInfo setPaperSize:rect.size]; _printPanel=[[NSPrintPanel printPanel] retain]; _showsPrintPanel=YES; _currentPage=0; _insideRect=rect; _mutableData=[data retain]; _type=type; return self; } -initWithView:(NSView *)view printInfo:(NSPrintInfo *)printInfo { int type = NSPrintOperationPrinter; if ([[printInfo jobDisposition] isEqualToString: NSPrintSaveJob]) { type = NSPrintOperationFile; } return [self initWithView:view printInfo:printInfo insideRect:[view bounds] toData:nil type:type]; } -(void)dealloc { [_view release]; [_printInfo release]; [_printPanel release]; [_context release]; [_mutableData release]; [super dealloc]; } +(NSPrintOperation *)printOperationWithView:(NSView *)view { return [[[self alloc] initWithView:view printInfo:[NSPrintInfo sharedPrintInfo]] autorelease]; } +(NSPrintOperation *)printOperationWithView:(NSView *)view printInfo:(NSPrintInfo *)printInfo { return [[[self alloc] initWithView:view printInfo:printInfo] autorelease]; } +(NSPrintOperation *)PDFOperationWithView:(NSView *)view insideRect:(NSRect)rect toData:(NSMutableData *)data { return [self PDFOperationWithView:view insideRect:rect toData:data printInfo:[NSPrintInfo sharedPrintInfo]]; } +(NSPrintOperation *)PDFOperationWithView:(NSView *)view insideRect:(NSRect)rect toData:(NSMutableData *)data printInfo:(NSPrintInfo *)printInfo { return [[[self alloc] initWithView:view printInfo:printInfo insideRect:rect toData:data type:NSPrintOperationPDFInRect] autorelease]; } +(NSPrintOperation *)EPSOperationWithView:(NSView *)view insideRect:(NSRect)rect toData:(NSMutableData *)data { return [self EPSOperationWithView:view insideRect:rect toData:data printInfo:[NSPrintInfo sharedPrintInfo]]; } +(NSPrintOperation *)EPSOperationWithView:(NSView *)view insideRect:(NSRect)rect toData:(NSMutableData *)data printInfo:(NSPrintInfo *)printInfo { return [[[self alloc] initWithView:view printInfo:printInfo insideRect:rect toData:data type:NSPrintOperationEPSInRect] autorelease]; } -(BOOL)isCopyingOperation { return NO; } -(void)setAccessoryView:(NSView *)view { view=[view retain]; [_accessoryView release]; _accessoryView=view; } -(NSView *)view { return _view; } -(NSPrintInfo *)printInfo { return _printInfo; } -(NSPrintPanel *)printPanel { return _printPanel; } -(BOOL)showsPrintPanel { return _showsPrintPanel; } -(void)setShowsPrintPanel:(BOOL)flag { _showsPrintPanel=flag; } -(int)currentPage { return _currentPage; } -(BOOL)showsProgressPanel { return _showsPrintProgressPanel; } -(void)setShowsProgressPanel:(BOOL)flag { _showsPrintProgressPanel = flag; } -(void)_autopaginatePageRange:(NSRange)pageRange actualPageRange:(NSRange *)rangep context:(CGContextRef)context { NSRange result=NSMakeRange(1,0); NSRect bounds=[_view bounds]; NSRect imageableRect=[_printInfo imageablePageBounds]; BOOL isFlipped=[_view isFlipped]; float top=isFlipped?NSMinY(bounds):NSMaxY(bounds); NSPrintingOrientation orientation=[_printInfo orientation]; while(YES) { float heightAdjustLimit=[_view heightAdjustLimit]; float widthAdjustLimit=[_view widthAdjustLimit]; float left=NSMinX(bounds); float right=left+imageableRect.size.width; float rightLimit=left+imageableRect.size.width*widthAdjustLimit; float bottom=isFlipped?top+imageableRect.size.height:top-imageableRect.size.height; float bottomLimit=isFlipped?top+imageableRect.size.height*heightAdjustLimit:top-imageableRect.size.height*heightAdjustLimit; if(orientation==NSLandscapeOrientation){ // FIX } if(isFlipped){ if(bottom>NSMaxY(bounds)) bottom=NSMaxY(bounds); } else { if(bottom=NSMaxY(bounds)) break; } else { if(top<=NSMinY(bounds)) break; } } *rangep=result; } -(void)_paginateWithPageRange:(NSRange)pageRange context:(CGContextRef)context { int i; NSPrintProgressPanelController *printProgressPanelController = nil; if ([self showsProgressPanel]) { printProgressPanelController = [NSPrintProgressPanelController printProgressPanelController]; if ([[[self printInfo] jobDisposition] isEqualToString: NSPrintSaveJob]) { [printProgressPanelController setTitle: NSLocalizedString(@"Save", @"Save a print job")]; } [printProgressPanelController setMaxPages: pageRange.length]; [printProgressPanelController showPanel]; } for(i=0,_currentPage=pageRange.location;i