1999-06-22 03:03:29 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import wx
|
2004-01-02 20:14:17 -05:00
|
|
|
import ScrolledWindow
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
ID_Setup = wx.NewId()
|
|
|
|
ID_Preview = wx.NewId()
|
|
|
|
ID_Print = wx.NewId()
|
|
|
|
|
|
|
|
class MyPrintout(wx.Printout):
|
1999-06-22 03:03:29 -04:00
|
|
|
def __init__(self, canvas, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Printout.__init__(self)
|
1999-06-22 03:03:29 -04:00
|
|
|
self.canvas = canvas
|
|
|
|
self.log = log
|
|
|
|
|
|
|
|
def OnBeginDocument(self, start, end):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.OnBeginDocument\n")
|
1999-06-22 03:03:29 -04:00
|
|
|
return self.base_OnBeginDocument(start, end)
|
|
|
|
|
|
|
|
def OnEndDocument(self):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.OnEndDocument\n")
|
1999-06-24 16:02:25 -04:00
|
|
|
self.base_OnEndDocument()
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def OnBeginPrinting(self):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.OnBeginPrinting\n")
|
1999-06-24 16:02:25 -04:00
|
|
|
self.base_OnBeginPrinting()
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def OnEndPrinting(self):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.OnEndPrinting\n")
|
1999-06-24 16:02:25 -04:00
|
|
|
self.base_OnEndPrinting()
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
def OnPreparePrinting(self):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.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):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.HasPage: %d\n" % page)
|
2001-10-12 19:26:38 -04:00
|
|
|
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):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.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):
|
2003-12-08 20:23:28 -05:00
|
|
|
self.log.WriteText("wx.Printout.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)
|
|
|
|
|
2003-10-01 20:58:06 -04:00
|
|
|
# Calculate the position on the DC for centering the graphic
|
1999-06-22 03:03:29 -04:00
|
|
|
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)
|
2003-11-22 17:57:49 -05:00
|
|
|
dc.DrawText("Page: %d" % page, (marginX/2, maxY-marginY))
|
2001-10-12 19:26:38 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
return True
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestPrintPanel(wx.Panel):
|
1999-06-22 03:03:29 -04:00
|
|
|
def __init__(self, parent, frame, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
1999-06-22 03:03:29 -04:00
|
|
|
self.log = log
|
|
|
|
self.frame = frame
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.printData = wx.PrintData()
|
|
|
|
self.printData.SetPaperId(wx.PAPER_LETTER)
|
2004-03-09 20:19:08 -05:00
|
|
|
self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.box = wx.BoxSizer(wx.VERTICAL)
|
2004-01-02 20:14:17 -05:00
|
|
|
self.canvas = ScrolledWindow.MyCanvas(self)
|
2003-12-08 20:23:28 -05:00
|
|
|
self.box.Add(self.canvas, 1, wx.GROW)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
subbox = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
btn = wx.Button(self, ID_Setup, "Print Setup")
|
2004-03-09 20:19:08 -05:00
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnPrintSetup, btn)
|
2003-12-08 20:23:28 -05:00
|
|
|
subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
btn = wx.Button(self, ID_Preview, "Print Preview")
|
2004-03-09 20:19:08 -05:00
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnPrintPreview, btn)
|
2003-12-08 20:23:28 -05:00
|
|
|
subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
btn = wx.Button(self, ID_Print, "Print")
|
2004-03-09 20:19:08 -05:00
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnDoPrint, btn)
|
2003-12-08 20:23:28 -05:00
|
|
|
subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
|
1999-06-22 03:03:29 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.box.Add(subbox, 0, wx.GROW)
|
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):
|
2004-03-09 20:19:08 -05:00
|
|
|
data = wx.PrintDialogData(self.printData)
|
|
|
|
printerDialog = wx.PrintDialog(self, data)
|
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")
|
2004-03-09 20:19:08 -05:00
|
|
|
data = wx.PrintDialogData(self.printData)
|
1999-06-22 03:03:29 -04:00
|
|
|
printout = MyPrintout(self.canvas, self.log)
|
|
|
|
printout2 = MyPrintout(self.canvas, self.log)
|
2004-03-09 20:19:08 -05:00
|
|
|
self.preview = wx.PrintPreview(printout, printout2, data)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-06-22 03:03:29 -04:00
|
|
|
if not self.preview.Ok():
|
|
|
|
self.log.WriteText("Houston, we have a problem...\n")
|
|
|
|
return
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
frame = wx.PreviewFrame(self.preview, self.frame, "This is a print preview")
|
1999-06-22 03:03:29 -04:00
|
|
|
|
|
|
|
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):
|
2004-03-09 20:19:08 -05:00
|
|
|
pdd = wx.PrintDialogData(self.printData)
|
|
|
|
pdd.SetToPage(2)
|
2003-12-08 20:23:28 -05:00
|
|
|
printer = wx.Printer(pdd)
|
1999-06-22 03:03:29 -04:00
|
|
|
printout = MyPrintout(self.canvas, self.log)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2004-03-09 20:19:08 -05:00
|
|
|
if not printer.Print(self.frame, printout, True):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
|
1999-06-22 03:03:29 -04:00
|
|
|
else:
|
|
|
|
self.printData = printer.GetPrintDialogData().GetPrintData()
|
|
|
|
printout.Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestPrintPanel(nb, frame, log)
|
|
|
|
return win
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
2003-12-08 20:23:28 -05:00
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<h1>PrintFramework</h1>
|
|
|
|
|
|
|
|
This is an overview of the classes and methods used to print documents.
|
|
|
|
It also demonstrates how to do print previews and invoke the printer
|
|
|
|
setup dialog.
|
|
|
|
|
|
|
|
<p>Classes demonstrated here:<P>
|
|
|
|
<ul>
|
|
|
|
<li><b>wx.Printout()</b> - This class encapsulates the functionality of printing out
|
|
|
|
an application document. A new class must be derived and members overridden
|
|
|
|
to respond to calls such as OnPrintPage and HasPage. Instances of this class
|
|
|
|
are passed to wx.Printer.Print() or a wx.PrintPreview object to initiate
|
|
|
|
printing or previewing.<P><p>
|
|
|
|
|
|
|
|
<li><b>wx.PrintData()</b> - This class holds a variety of information related to
|
|
|
|
printers and printer device contexts. This class is used to create a
|
|
|
|
wx.PrinterDC and a wx.PostScriptDC. It is also used as a data member of
|
|
|
|
wx.PrintDialogData and wx.PageSetupDialogData, as part of the mechanism for
|
|
|
|
transferring data between the print dialogs and the application.<p><p>
|
|
|
|
|
|
|
|
<li><b>wx.PrintDialog()</b> - This class represents the print and print setup
|
|
|
|
common dialogs. You may obtain a wx.PrinterDC device context from a
|
|
|
|
successfully dismissed print dialog.<p><p>
|
|
|
|
|
|
|
|
<li><b>wx.PrintPreview()</b> - Objects of this class manage the print preview
|
|
|
|
process. The object is passed a wx.Printout object, and the wx.PrintPreview
|
|
|
|
object itself is passed to a wx.PreviewFrame object. Previewing is started by
|
|
|
|
initializing and showing the preview frame. Unlike wxPrinter.Print, flow of
|
|
|
|
control returns to the application immediately after the frame is shown.<p><p>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p>Other classes are also demonstrated, but this is the gist of the printer interface
|
|
|
|
framework in wxPython.
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
2003-07-02 19:13:10 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
"""
|
2003-07-02 19:13:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2003-07-02 19:13:10 -04:00
|
|
|
|