]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PrintFramework.py
   5 #---------------------------------------------------------------------- 
   8 class MyPrintout(wx
.Printout
): 
   9     def __init__(self
, canvas
, log
): 
  10         wx
.Printout
.__init
__(self
) 
  14     def OnBeginDocument(self
, start
, end
): 
  15         self
.log
.WriteText("MyPrintout.OnBeginDocument\n") 
  16         return super(MyPrintout
, self
).OnBeginDocument(start
, end
) 
  18     def OnEndDocument(self
): 
  19         self
.log
.WriteText("MyPrintout.OnEndDocument\n") 
  20         super(MyPrintout
, self
).OnEndDocument() 
  22     def OnBeginPrinting(self
): 
  23         self
.log
.WriteText("MyPrintout.OnBeginPrinting\n") 
  24         super(MyPrintout
, self
).OnBeginPrinting() 
  26     def OnEndPrinting(self
): 
  27         self
.log
.WriteText("MyPrintout.OnEndPrinting\n") 
  28         super(MyPrintout
, self
).OnEndPrinting() 
  30     def OnPreparePrinting(self
): 
  31         self
.log
.WriteText("MyPrintout.OnPreparePrinting\n") 
  32         super(MyPrintout
, self
).OnPreparePrinting() 
  34     def HasPage(self
, page
): 
  35         self
.log
.WriteText("MyPrintout.HasPage: %d\n" % page
) 
  41     def GetPageInfo(self
): 
  42         self
.log
.WriteText("MyPrintout.GetPageInfo\n") 
  45     def OnPrintPage(self
, page
): 
  46         self
.log
.WriteText("MyPrintout.OnPrintPage: %d\n" % page
) 
  49         #------------------------------------------- 
  50         # One possible method of setting scaling factors... 
  52         maxX 
= self
.canvas
.getWidth() 
  53         maxY 
= self
.canvas
.getHeight() 
  55         # Let's have at least 50 device units margin 
  59         # Add the margin to the graphic size 
  60         maxX 
= maxX 
+ (2 * marginX
) 
  61         maxY 
= maxY 
+ (2 * marginY
) 
  63         # Get the size of the DC in pixels 
  64         (w
, h
) = dc
.GetSizeTuple() 
  66         # Calculate a suitable scaling factor 
  67         scaleX 
= float(w
) / maxX
 
  68         scaleY 
= float(h
) / maxY
 
  70         # Use x or y scaling factor, whichever fits on the DC 
  71         actualScale 
= min(scaleX
, scaleY
) 
  73         # Calculate the position on the DC for centering the graphic 
  74         posX 
= (w 
- (self
.canvas
.getWidth() * actualScale
)) / 2.0 
  75         posY 
= (h 
- (self
.canvas
.getHeight() * actualScale
)) / 2.0 
  77         # Set the scale and origin 
  78         dc
.SetUserScale(actualScale
, actualScale
) 
  79         dc
.SetDeviceOrigin(int(posX
), int(posY
)) 
  81         #------------------------------------------- 
  83         self
.canvas
.DoDrawing(dc
, True) 
  84         dc
.DrawText("Page: %d" % page
, marginX
/2, maxY
-marginY
) 
  89 #---------------------------------------------------------------------- 
  92 class TestPrintPanel(wx
.Panel
): 
  93     def __init__(self
, parent
, frame
, log
): 
  94         wx
.Panel
.__init
__(self
, parent
, -1) 
  98         self
.printData 
= wx
.PrintData() 
  99         self
.printData
.SetPaperId(wx
.PAPER_LETTER
) 
 100         self
.printData
.SetPrintMode(wx
.PRINT_MODE_PRINTER
) 
 102         self
.box 
= wx
.BoxSizer(wx
.VERTICAL
) 
 103         self
.canvas 
= ScrolledWindow
.MyCanvas(self
) 
 104         self
.box
.Add(self
.canvas
, 1, wx
.GROW
) 
 106         subbox 
= wx
.BoxSizer(wx
.HORIZONTAL
) 
 107         btn 
= wx
.Button(self
, -1, "Page Setup") 
 108         self
.Bind(wx
.EVT_BUTTON
, self
.OnPageSetup
, btn
) 
 109         subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2) 
 111         btn 
= wx
.Button(self
, -1, "Print Preview") 
 112         self
.Bind(wx
.EVT_BUTTON
, self
.OnPrintPreview
, btn
) 
 113         subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2) 
 115         btn 
= wx
.Button(self
, -1, "Print") 
 116         self
.Bind(wx
.EVT_BUTTON
, self
.OnDoPrint
, btn
) 
 117         subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2) 
 119         self
.box
.Add(subbox
, 0, wx
.GROW
) 
 121         self
.SetAutoLayout(True) 
 122         self
.SetSizer(self
.box
) 
 126     def OnPageSetup(self
, evt
): 
 127         psdd 
= wx
.PageSetupDialogData(self
.printData
) 
 128         psdd
.CalculatePaperSizeFromId() 
 129         dlg 
= wx
.PageSetupDialog(self
, psdd
) 
 132         # this makes a copy of the wx.PrintData instead of just saving 
 133         # a reference to the one inside the PrintDialogData that will 
 134         # be destroyed when the dialog is destroyed 
 135         self
.printData 
= wx
.PrintData( dlg
.GetPageSetupData().GetPrintData() ) 
 139     def OnPrintPreview(self
, event
): 
 140         data 
= wx
.PrintDialogData(self
.printData
) 
 141         printout 
= MyPrintout(self
.canvas
, self
.log
) 
 142         printout2 
= MyPrintout(self
.canvas
, self
.log
) 
 143         self
.preview 
= wx
.PrintPreview(printout
, printout2
, data
) 
 145         if not self
.preview
.Ok(): 
 146             self
.log
.WriteText("Houston, we have a problem...\n") 
 149         pfrm 
= wx
.PreviewFrame(self
.preview
, self
.frame
, "This is a print preview") 
 152         pfrm
.SetPosition(self
.frame
.GetPosition()) 
 153         pfrm
.SetSize(self
.frame
.GetSize()) 
 158     def OnDoPrint(self
, event
): 
 159         pdd 
= wx
.PrintDialogData(self
.printData
) 
 161         printer 
= wx
.Printer(pdd
) 
 162         printout 
= MyPrintout(self
.canvas
, self
.log
) 
 164         if not printer
.Print(self
.frame
, printout
, True): 
 165             wx
.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx
.OK
) 
 167             self
.printData 
= wx
.PrintData( printer
.GetPrintDialogData().GetPrintData() ) 
 171 #---------------------------------------------------------------------- 
 173 def runTest(frame
, nb
, log
): 
 174     win 
= TestPrintPanel(nb
, frame
, log
) 
 178 #---------------------------------------------------------------------- 
 187 <h1>PrintFramework</h1> 
 189 This is an overview of the classes and methods used to print documents. 
 190 It also demonstrates how to do print previews and invoke the printer 
 193 <p>Classes demonstrated here:<P> 
 195     <li><b>wx.Printout()</b> - This class encapsulates the functionality of printing out  
 196         an application document. A new class must be derived and members overridden  
 197         to respond to calls such as OnPrintPage and HasPage. Instances of this class  
 198         are passed to wx.Printer.Print() or a wx.PrintPreview object to initiate  
 199         printing or previewing.<P><p> 
 201     <li><b>wx.PrintData()</b> - This class holds a variety of information related to  
 202         printers and printer device contexts. This class is used to create a  
 203         wx.PrinterDC and a wx.PostScriptDC. It is also used as a data member of  
 204         wx.PrintDialogData and wx.PageSetupDialogData, as part of the mechanism for  
 205         transferring data between the print dialogs and the application.<p><p> 
 207     <li><b>wx.PrintDialog()</b> - This class represents the print and print setup  
 208         common dialogs. You may obtain a wx.PrinterDC device context from a  
 209         successfully dismissed print dialog.<p><p> 
 211     <li><b>wx.PrintPreview()</b> - Objects of this class manage the print preview  
 212         process. The object is passed a wx.Printout object, and the wx.PrintPreview  
 213         object itself is passed to a wx.PreviewFrame object. Previewing is started by  
 214         initializing and showing the preview frame. Unlike wxPrinter.Print, flow of  
 215         control returns to the application immediately after the frame is shown.<p><p> 
 218 <p>Other classes are also demonstrated, but this is the gist of the printer interface 
 219 framework in wxPython. 
 227 if __name__ 
== '__main__': 
 230     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])