| 1 |
ninoborges |
8 |
##Printer2
|
| 2 |
|
|
|
| 3 |
|
|
from wxPython.wx import *
|
| 4 |
|
|
|
| 5 |
|
|
class MyPrintout(wxPrintout):
|
| 6 |
|
|
def __init__(self, canvas):
|
| 7 |
|
|
wxPrintout.__init__(self)
|
| 8 |
|
|
self.canvas = canvas
|
| 9 |
|
|
|
| 10 |
|
|
def OnBeginDocument(self, start, end):
|
| 11 |
|
|
#self.log.WriteText("wxPrintout.OnBeginDocument\n")
|
| 12 |
|
|
return self.base_OnBeginDocument(start, end)
|
| 13 |
|
|
|
| 14 |
|
|
def OnEndDocument(self):
|
| 15 |
|
|
#self.log.WriteText("wxPrintout.OnEndDocument\n")
|
| 16 |
|
|
self.base_OnEndDocument()
|
| 17 |
|
|
|
| 18 |
|
|
def OnBeginPrinting(self):
|
| 19 |
|
|
#self.log.WriteText("wxPrintout.OnBeginPrinting\n")
|
| 20 |
|
|
self.base_OnBeginPrinting()
|
| 21 |
|
|
|
| 22 |
|
|
def OnEndPrinting(self):
|
| 23 |
|
|
#self.log.WriteText("wxPrintout.OnEndPrinting\n")
|
| 24 |
|
|
self.base_OnEndPrinting()
|
| 25 |
|
|
|
| 26 |
|
|
def OnPreparePrinting(self):
|
| 27 |
|
|
#self.log.WriteText("wxPrintout.OnPreparePrinting\n")
|
| 28 |
|
|
self.base_OnPreparePrinting()
|
| 29 |
|
|
|
| 30 |
|
|
def HasPage(self, page):
|
| 31 |
|
|
#self.log.WriteText("wxPrintout.HasPage: %d\n" % page)
|
| 32 |
|
|
if page <= 2:
|
| 33 |
|
|
return True
|
| 34 |
|
|
else:
|
| 35 |
|
|
return False
|
| 36 |
|
|
|
| 37 |
|
|
def GetPageInfo(self):
|
| 38 |
|
|
#self.log.WriteText("wxPrintout.GetPageInfo\n")
|
| 39 |
|
|
return (1, 2, 1, 2)
|
| 40 |
|
|
|
| 41 |
|
|
def OnPrintPage(self, page):
|
| 42 |
|
|
#self.log.WriteText("wxPrintout.OnPrintPage: %d\n" % page)
|
| 43 |
|
|
dc = self.GetDC()
|
| 44 |
|
|
|
| 45 |
|
|
#-------------------------------------------
|
| 46 |
|
|
# One possible method of setting scaling factors...
|
| 47 |
|
|
|
| 48 |
|
|
maxX = self.canvas.GetWidth()
|
| 49 |
|
|
maxY = self.canvas.GetHeight()
|
| 50 |
|
|
|
| 51 |
|
|
# Let's have at least 50 device units margin
|
| 52 |
|
|
marginX = 50
|
| 53 |
|
|
marginY = 50
|
| 54 |
|
|
|
| 55 |
|
|
# Add the margin to the graphic size
|
| 56 |
|
|
maxX = maxX + (2 * marginX)
|
| 57 |
|
|
maxY = maxY + (2 * marginY)
|
| 58 |
|
|
|
| 59 |
|
|
# Get the size of the DC in pixels
|
| 60 |
|
|
(w, h) = dc.GetSizeTuple()
|
| 61 |
|
|
|
| 62 |
|
|
# Calculate a suitable scaling factor
|
| 63 |
|
|
scaleX = float(w) / maxX
|
| 64 |
|
|
scaleY = float(h) / maxY
|
| 65 |
|
|
|
| 66 |
|
|
# Use x or y scaling factor, whichever fits on the DC
|
| 67 |
|
|
actualScale = min(scaleX, scaleY)
|
| 68 |
|
|
|
| 69 |
|
|
# Calculate the position on the DC for centring the graphic
|
| 70 |
|
|
posX = (w - (self.canvas.getWidth() * actualScale)) / 2.0
|
| 71 |
|
|
posY = (h - (self.canvas.getHeight() * actualScale)) / 2.0
|
| 72 |
|
|
|
| 73 |
|
|
# Set the scale and origin
|
| 74 |
|
|
dc.SetUserScale(actualScale, actualScale)
|
| 75 |
|
|
dc.SetDeviceOrigin(int(posX), int(posY))
|
| 76 |
|
|
|
| 77 |
|
|
#-------------------------------------------
|
| 78 |
|
|
|
| 79 |
|
|
self.canvas.DoDrawing(dc)
|
| 80 |
|
|
dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
|
| 81 |
|
|
|
| 82 |
|
|
return True |