1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
from wxPython.wx import *
|
|
|
|
from wxScrolledWindow import MyCanvas
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyPrintout(wxPrintout):
|
|
|
|
def __init__(self, canvas, log):
|
|
|
|
wxPrintout.__init__(self)
|
|
|
|
self.canvas = canvas
|
|
|
|
self.log = log
|
|
|
|
|
|
|
|
def OnBeginDocument(self, start, end):
|
|
|
|
self.log.WriteText("wxPrintout.OnBeginDocument\n")
|
|
|
|
return self.base_OnBeginDocument(start, end)
|
|
|
|
|
|
|
|
def OnEndDocument(self):
|
|
|
|
self.log.WriteText("wxPrintout.OnEndDocument\n")
|
1999-06-24 16:02:25 -04:00
|
|
|
self.base_OnEndDocument()
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def OnBeginPrinting(self):
|
|
|
|
self.log.WriteText("wxPrintout.OnBeginPrinting\n")
|
1999-06-24 16:02:25 -04:00
|
|
|
self.base_OnBeginPrinting()
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def OnEndPrinting(self):
|
|
|
|
self.log.WriteText("wxPrintout.OnEndPrinting\n")
|
1999-06-24 16:02:25 -04:00
|
|
|
self.base_OnEndPrinting()
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def OnPreparePrinting(self):
|
|
|
|
self.log.WriteText("wxPrintout.OnPreparePrinting\n")
|
1999-06-24 16:02:25 -04:00
|
|
|
self.base_OnPreparePrinting()
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def HasPage(self, page):
|
2001-10-12 19:26:38 -04:00
|
|
|
self.log.WriteText("wxPrintout.HasPage: %d\n" % page)
|
|
|
|
if page <= 2:
|
2003-03-25 01:35:27 -05:00
|
|
|
return True
|
1999-06-22 03:03:29 -04:00
|
|
|
else:
|
2003-03-25 01:35:27 -05:00
|
|
|
return False
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def GetPageInfo(self):
|
|
|
|
self.log.WriteText("wxPrintout.GetPageInfo\n")
|
2001-10-12 19:26:38 -04:00
|
|
|
return (1, 2, 1, 2)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def OnPrintPage(self, page):
|
2001-10-12 19:26:38 -04:00
|
|
|
self.log.WriteText("wxPrintout.OnPrintPage: %d\n" % page)
|
1999-06-22 03:03:29 -04:00
|
|
|
dc = self.GetDC()
|
|
|
|
|
|
|
|
#-------------------------------------------
|
|
|
|
# One possible method of setting scaling factors...
|
|
|
|
|
|
|
|
maxX = self.canvas.getWidth()
|
|
|
|
maxY = self.canvas.getHeight()
|
|
|
|
|
|
|
|
# Let's have at least 50 device units margin
|
|
|
|
marginX = 50
|
|
|
|
marginY = 50
|
|
|
|
|
|
|
|
# Add the margin to the graphic size
|
|
|
|
maxX = maxX + (2 * marginX)
|
|
|
|
maxY = maxY + (2 * marginY)
|
|
|
|
|
|
|
|
# Get the size of the DC in pixels
|
|
|
|
(w, h) = dc.GetSizeTuple()
|
|
|
|
|
|
|
|
# Calculate a suitable scaling factor
|
|
|
|
scaleX = float(w) / maxX
|
|
|
|
scaleY = float(h) / maxY
|
|
|
|
|
|
|
|
# Use x or y scaling factor, whichever fits on the DC
|
|
|
|
actualScale = min(scaleX, scaleY)
|
|
|
|
|
|
|
|
# Calculate the position on the DC for centring the graphic
|
|
|
|
posX = (w - (self.canvas.getWidth() * actualScale)) / 2.0
|
|
|
|
posY = (h - (self.canvas.getHeight() * actualScale)) / 2.0
|
|
|
|
|
|
|
|
# Set the scale and origin
|
|
|
|
dc.SetUserScale(actualScale, actualScale)
|
|
|
|
dc.SetDeviceOrigin(int(posX), int(posY))
|
|
|
|
|
|
|
|
#-------------------------------------------
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
self.canvas.DoDrawing(dc, True)
|
2001-10-12 19:26:38 -04:00
|
|
|
dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
return True
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
class TestPrintPanel(wxPanel):
|
|
|
|
def __init__(self, parent, frame, log):
|
|
|
|
wxPanel.__init__(self, parent, -1)
|
|
|
|
self.log = log
|
|
|
|
self.frame = frame
|
|
|
|
|
|
|
|
|
|
|
|
self.printData = wxPrintData()
|
|
|
|
self.printData.SetPaperId(wxPAPER_LETTER)
|
|
|
|
|
1999-09-30 03:11:20 -04:00
|
|
|
self.box = wxBoxSizer(wxVERTICAL)
|
1999-06-22 03:03:29 -04:00
|
|
|
self.canvas = MyCanvas(self)
|
1999-09-30 03:11:20 -04:00
|
|
|
self.box.Add(self.canvas, 1, wxGROW)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
subbox = wxBoxSizer(wxHORIZONTAL)
|
|
|
|
btn = wxButton(self, 1201, "Print Setup")
|
|
|
|
EVT_BUTTON(self, 1201, self.OnPrintSetup)
|
1999-09-30 03:11:20 -04:00
|
|
|
subbox.Add(btn, 1, wxGROW | wxALL, 2)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
btn = wxButton(self, 1202, "Print Preview")
|
|
|
|
EVT_BUTTON(self, 1202, self.OnPrintPreview)
|
1999-09-30 03:11:20 -04:00
|
|
|
subbox.Add(btn, 1, wxGROW | wxALL, 2)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
btn = wxButton(self, 1203, "Print")
|
|
|
|
EVT_BUTTON(self, 1203, self.OnDoPrint)
|
1999-09-30 03:11:20 -04:00
|
|
|
subbox.Add(btn, 1, wxGROW | wxALL, 2)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
1999-09-30 03:11:20 -04:00
|
|
|
self.box.Add(subbox, 0, wxGROW)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
1999-09-30 03:11:20 -04:00
|
|
|
self.SetSizer(self.box)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OnPrintSetup(self, event):
|
|
|
|
printerDialog = wxPrintDialog(self)
|
|
|
|
printerDialog.GetPrintDialogData().SetPrintData(self.printData)
|
2003-03-25 01:35:27 -05:00
|
|
|
printerDialog.GetPrintDialogData().SetSetupDialog(True)
|
1999-06-22 03:03:29 -04:00
|
|
|
printerDialog.ShowModal();
|
|
|
|
self.printData = printerDialog.GetPrintDialogData().GetPrintData()
|
|
|
|
printerDialog.Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def OnPrintPreview(self, event):
|
|
|
|
self.log.WriteText("OnPrintPreview\n")
|
|
|
|
printout = MyPrintout(self.canvas, self.log)
|
|
|
|
printout2 = MyPrintout(self.canvas, self.log)
|
|
|
|
self.preview = wxPrintPreview(printout, printout2, self.printData)
|
|
|
|
if not self.preview.Ok():
|
|
|
|
self.log.WriteText("Houston, we have a problem...\n")
|
|
|
|
return
|
|
|
|
|
|
|
|
frame = wxPreviewFrame(self.preview, self.frame, "This is a print preview")
|
|
|
|
|
|
|
|
frame.Initialize()
|
|
|
|
frame.SetPosition(self.frame.GetPosition())
|
|
|
|
frame.SetSize(self.frame.GetSize())
|
2003-03-25 01:35:27 -05:00
|
|
|
frame.Show(True)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def OnDoPrint(self, event):
|
|
|
|
pdd = wxPrintDialogData()
|
|
|
|
pdd.SetPrintData(self.printData)
|
|
|
|
printer = wxPrinter(pdd)
|
|
|
|
printout = MyPrintout(self.canvas, self.log)
|
|
|
|
if not printer.Print(self.frame, printout):
|
|
|
|
wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
|
|
|
|
else:
|
|
|
|
self.printData = printer.GetPrintDialogData().GetPrintData()
|
|
|
|
printout.Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestPrintPanel(nb, frame, log)
|
|
|
|
return win
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
|
|
|
"""
|
|
|
|
|
2003-07-02 19:13:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
|
|
|
run.main(['', os.path.basename(sys.argv[0])])
|
|
|
|
|