1from Cocoa import * 2 3import UIHandling 4import AppDrawing 5import FrameworkTextDrawing 6 7import objc 8 9# XXX: Why are these global? 10_drawingCommand = UIHandling.kHICommandSimpleRect 11_pdfDocument = None 12 13class MyView (NSView): 14 currentMenuItem = objc.IBOutlet() 15 16 def initWithFrame_(self, frameRect): 17 self = super(MyView, self).initWithFrame_(frameRect) 18 if self is None: 19 return None 20 21 global _pdfDocument 22 _pdfDocument = None 23 return self 24 25 26 if False: 27 def isFlipped(self): 28 return True 29 30 31 def drawRect_(self, rect): 32 context = NSGraphicsContext.currentContext().graphicsPort() 33 34 if _pdfDocument is None: 35 if _drawingCommand in (UIHandling.kHICommandDrawNSString, 36 UIHandling.kHICommandDrawNSLayoutMgr, UIHandling.kHICommandDrawCustomNSLayoutMgr): 37 38 if _drawingCommand == UIHandling.kHICommandDrawNSString: 39 FrameworkTextDrawing.drawNSStringWithAttributes() 40 41 elif _drawingCommand == UIHandling.kHICommandDrawNSLayoutMgr: 42 FrameworkTextDrawing.drawWithNSLayout() 43 44 else: 45 FrameworkTextDrawing.drawWithCustomNSLayout() 46 else: 47 AppDrawing.DispatchDrawing(context, _drawingCommand) 48 49 else: 50 mediaRect = CGPDFDocumentGetMediaBox(_pdfDocument, 1) 51 mediaRect.origin.x = mediaRect.origin.y = 0 52 CGContextDrawPDFDocument(context, mediaRect, _pdfDocument, 1) 53 54 @objc.IBAction 55 def setDrawCommand_(self, sender): 56 global _drawingCommand, _pdfDocument 57 58 newCommand = sender.tag() 59 if _drawingCommand != newCommand: 60 _drawingCommand = newCommand 61 # The view needs to be redisplayed since there is a new drawing command. 62 self.setNeedsDisplay_(True) 63 64 # Disable previous menu item. 65 if self.currentMenuItem is not None: 66 self.currentMenuItem.setState_(NSOffState) 67 68 # Update the current item. 69 self.currentMenuItem = sender 70 71 # Enable new menu item. 72 self.currentMenuItem.setState_(NSOnState) 73 74 # If we were showing a pasted document, let's get rid of it. 75 if _pdfDocument: 76 _pdfDocument = None 77 78 def currentPrintableCommand(self): 79 # The best representation for printing or exporting 80 # when the current command caches using a bitmap context 81 # or a layer is to not do any caching. 82 if _drawingCommand in (UIHandling.kHICommandDrawOffScreenImage, 83 UIHandling.kHICommandDrawWithLayer): 84 return UIHandling.kHICommandDrawNoOffScreenImage 85 86 return _drawingCommand 87 88 89 def print_(self, sender): 90 global _drawingCommand 91 92 savedDrawingCommand = _drawingCommand 93 # Set the drawing command to be one that is printable. 94 _drawingCommand = self.currentPrintableCommand() 95 # Do the printing operation on the view. 96 NSPrintOperation.printOperationWithView_(self).runOperation() 97 # Restore that before the printing operation. 98 _drawingCommand = savedDrawingCommand 99 100 def acceptsFirstResponder(self): 101 return True 102 103 104 @objc.IBAction 105 def copy_(self, sender): 106 addPDFDataToPasteBoard(_drawingCommand) 107 108 @objc.IBAction 109 def paste_(self, sender): 110 global _pdfDocument 111 112 newPDFDocument = createNewPDFRefFromPasteBoard() 113 if newPDFDocument is not None: 114 _pdfDocument = newPDFDocument 115 # The view needs to be redisplayed since there is 116 # a new PDF document. 117 self.setNeedsDisplay_(True) 118 119 120 # Return the number of pages available for printing. For this 121 # application it is always 1. 122 def knowsPageRange_(self): 123 return True, NSRange(1, 1) 124 125 # Return the drawing rectangle for a particular page number. 126 # For this application it is always the page width and height. 127 def rectForPage_(self, page): 128 pi = NSPrintOperation.currentOperation().printInfo() 129 130 # Calculate the page height in points. 131 paperSize = pi.paperSize() 132 return NSMakeRect(0, 0, paperSize.width, paperSize.height) 133 134 def validateMenuItem_(self, menuItem): 135 if menuItem.tag() == _drawingCommand: 136 self.currentMenuItem = menuItem 137 menuItem.setState_(True) 138 else: 139 menuItem.setState_(False) 140 141 return True 142