]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PrintFramework.py
   5 #---------------------------------------------------------------------- 
   8 ID_Preview  
=   wx
.NewId() 
  11 class MyPrintout(wx
.Printout
): 
  12     def __init__(self
, canvas
, log
): 
  13         wx
.Printout
.__init
__(self
) 
  17     def OnBeginDocument(self
, start
, end
): 
  18         self
.log
.WriteText("wx.Printout.OnBeginDocument\n") 
  19         return self
.base_OnBeginDocument(start
, end
) 
  21     def OnEndDocument(self
): 
  22         self
.log
.WriteText("wx.Printout.OnEndDocument\n") 
  23         self
.base_OnEndDocument() 
  25     def OnBeginPrinting(self
): 
  26         self
.log
.WriteText("wx.Printout.OnBeginPrinting\n") 
  27         self
.base_OnBeginPrinting() 
  29     def OnEndPrinting(self
): 
  30         self
.log
.WriteText("wx.Printout.OnEndPrinting\n") 
  31         self
.base_OnEndPrinting() 
  33     def OnPreparePrinting(self
): 
  34         self
.log
.WriteText("wx.Printout.OnPreparePrinting\n") 
  35         self
.base_OnPreparePrinting() 
  37     def HasPage(self
, page
): 
  38         self
.log
.WriteText("wx.Printout.HasPage: %d\n" % page
) 
  44     def GetPageInfo(self
): 
  45         self
.log
.WriteText("wx.Printout.GetPageInfo\n") 
  48     def OnPrintPage(self
, page
): 
  49         self
.log
.WriteText("wx.Printout.OnPrintPage: %d\n" % page
) 
  52         #------------------------------------------- 
  53         # One possible method of setting scaling factors... 
  55         maxX 
= self
.canvas
.getWidth() 
  56         maxY 
= self
.canvas
.getHeight() 
  58         # Let's have at least 50 device units margin 
  62         # Add the margin to the graphic size 
  63         maxX 
= maxX 
+ (2 * marginX
) 
  64         maxY 
= maxY 
+ (2 * marginY
) 
  66         # Get the size of the DC in pixels 
  67         (w
, h
) = dc
.GetSizeTuple() 
  69         # Calculate a suitable scaling factor 
  70         scaleX 
= float(w
) / maxX
 
  71         scaleY 
= float(h
) / maxY
 
  73         # Use x or y scaling factor, whichever fits on the DC 
  74         actualScale 
= min(scaleX
, scaleY
) 
  76         # Calculate the position on the DC for centering the graphic 
  77         posX 
= (w 
- (self
.canvas
.getWidth() * actualScale
)) / 2.0 
  78         posY 
= (h 
- (self
.canvas
.getHeight() * actualScale
)) / 2.0 
  80         # Set the scale and origin 
  81         dc
.SetUserScale(actualScale
, actualScale
) 
  82         dc
.SetDeviceOrigin(int(posX
), int(posY
)) 
  84         #------------------------------------------- 
  86         self
.canvas
.DoDrawing(dc
, True) 
  87         dc
.DrawText("Page: %d" % page
, (marginX
/2, maxY
-marginY
)) 
  92 #---------------------------------------------------------------------- 
  95 class TestPrintPanel(wx
.Panel
): 
  96     def __init__(self
, parent
, frame
, log
): 
  97         wx
.Panel
.__init
__(self
, parent
, -1) 
 101         self
.printData 
= wx
.PrintData() 
 102         self
.printData
.SetPaperId(wx
.PAPER_LETTER
) 
 104         self
.box 
= wx
.BoxSizer(wx
.VERTICAL
) 
 105         self
.canvas 
= ScrolledWindow
.MyCanvas(self
) 
 106         self
.box
.Add(self
.canvas
, 1, wx
.GROW
) 
 108         subbox 
= wx
.BoxSizer(wx
.HORIZONTAL
) 
 109         btn 
= wx
.Button(self
, ID_Setup
, "Print Setup") 
 110         self
.Bind(wx
.EVT_BUTTON
, self
.OnPrintSetup
, id=ID_Setup
) 
 111         subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2) 
 113         btn 
= wx
.Button(self
, ID_Preview
, "Print Preview") 
 114         self
.Bind(wx
.EVT_BUTTON
, self
.OnPrintPreview
, id=ID_Preview
) 
 115         subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2) 
 117         btn 
= wx
.Button(self
, ID_Print
, "Print") 
 118         self
.Bind(wx
.EVT_BUTTON
, self
.OnDoPrint
, id=ID_Print
) 
 119         subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2) 
 121         self
.box
.Add(subbox
, 0, wx
.GROW
) 
 123         self
.SetAutoLayout(True) 
 124         self
.SetSizer(self
.box
) 
 127     def OnPrintSetup(self
, event
): 
 128         printerDialog 
= wx
.PrintDialog(self
) 
 129         printerDialog
.GetPrintDialogData().SetPrintData(self
.printData
) 
 130         printerDialog
.GetPrintDialogData().SetSetupDialog(True) 
 131         printerDialog
.ShowModal(); 
 132         self
.printData 
= printerDialog
.GetPrintDialogData().GetPrintData() 
 133         printerDialog
.Destroy() 
 136     def OnPrintPreview(self
, event
): 
 137         self
.log
.WriteText("OnPrintPreview\n") 
 138         printout 
= MyPrintout(self
.canvas
, self
.log
) 
 139         printout2 
= MyPrintout(self
.canvas
, self
.log
) 
 140         self
.preview 
= wx
.PrintPreview(printout
, printout2
, self
.printData
) 
 142         if not self
.preview
.Ok(): 
 143             self
.log
.WriteText("Houston, we have a problem...\n") 
 146         frame 
= wx
.PreviewFrame(self
.preview
, self
.frame
, "This is a print preview") 
 149         frame
.SetPosition(self
.frame
.GetPosition()) 
 150         frame
.SetSize(self
.frame
.GetSize()) 
 155     def OnDoPrint(self
, event
): 
 156         pdd 
= wx
.PrintDialogData() 
 157         pdd
.SetPrintData(self
.printData
) 
 158         printer 
= wx
.Printer(pdd
) 
 159         printout 
= MyPrintout(self
.canvas
, self
.log
) 
 161         if not printer
.Print(self
.frame
, printout
): 
 162             wx
.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx
.OK
) 
 164             self
.printData 
= printer
.GetPrintDialogData().GetPrintData() 
 168 #---------------------------------------------------------------------- 
 170 def runTest(frame
, nb
, log
): 
 171     win 
= TestPrintPanel(nb
, frame
, log
) 
 175 #---------------------------------------------------------------------- 
 184 <h1>PrintFramework</h1> 
 186 This is an overview of the classes and methods used to print documents. 
 187 It also demonstrates how to do print previews and invoke the printer 
 190 <p>Classes demonstrated here:<P> 
 192     <li><b>wx.Printout()</b> - This class encapsulates the functionality of printing out  
 193         an application document. A new class must be derived and members overridden  
 194         to respond to calls such as OnPrintPage and HasPage. Instances of this class  
 195         are passed to wx.Printer.Print() or a wx.PrintPreview object to initiate  
 196         printing or previewing.<P><p> 
 198     <li><b>wx.PrintData()</b> - This class holds a variety of information related to  
 199         printers and printer device contexts. This class is used to create a  
 200         wx.PrinterDC and a wx.PostScriptDC. It is also used as a data member of  
 201         wx.PrintDialogData and wx.PageSetupDialogData, as part of the mechanism for  
 202         transferring data between the print dialogs and the application.<p><p> 
 204     <li><b>wx.PrintDialog()</b> - This class represents the print and print setup  
 205         common dialogs. You may obtain a wx.PrinterDC device context from a  
 206         successfully dismissed print dialog.<p><p> 
 208     <li><b>wx.PrintPreview()</b> - Objects of this class manage the print preview  
 209         process. The object is passed a wx.Printout object, and the wx.PrintPreview  
 210         object itself is passed to a wx.PreviewFrame object. Previewing is started by  
 211         initializing and showing the preview frame. Unlike wxPrinter.Print, flow of  
 212         control returns to the application immediately after the frame is shown.<p><p> 
 215 <p>Other classes are also demonstrated, but this is the gist of the printer interface 
 216 framework in wxPython. 
 224 if __name__ 
== '__main__': 
 227     run
.main(['', os
.path
.basename(sys
.argv
[0])])