]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/PrintFramework.py
133d6270d8dd0b931b0c76d42cb5e77e2ba8b749
   2 from wxPython
.wx         
import * 
   3 from wxPython
.lib
.sizers 
import * 
   4 from wxScrolledWindow    
import MyCanvas
 
   6 #---------------------------------------------------------------------- 
   8 class MyPrintout(wxPrintout
): 
   9     def __init__(self
, canvas
, log
): 
  10         wxPrintout
.__init
__(self
) 
  14     def OnBeginDocument(self
, start
, end
): 
  15         self
.log
.WriteText("wxPrintout.OnBeginDocument\n") 
  16         return self
.base_OnBeginDocument(start
, end
) 
  18     def OnEndDocument(self
): 
  19         self
.log
.WriteText("wxPrintout.OnEndDocument\n") 
  20         self
.base_OnEndDocument() 
  22     def OnBeginPrinting(self
): 
  23         self
.log
.WriteText("wxPrintout.OnBeginPrinting\n") 
  24         self
.base_OnBeginPrinting() 
  26     def OnEndPrinting(self
): 
  27         self
.log
.WriteText("wxPrintout.OnEndPrinting\n") 
  28         self
.base_OnEndPrinting() 
  30     def OnPreparePrinting(self
): 
  31         self
.log
.WriteText("wxPrintout.OnPreparePrinting\n") 
  32         self
.base_OnPreparePrinting() 
  34     def HasPage(self
, page
): 
  35         self
.log
.WriteText("wxPrintout.HasPage\n") 
  41     def GetPageInfo(self
): 
  42         self
.log
.WriteText("wxPrintout.GetPageInfo\n") 
  45     def OnPrintPage(self
, page
): 
  46         self
.log
.WriteText("wxPrintout.OnPrintPage\n") 
  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 centring 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
) 
  87 #---------------------------------------------------------------------- 
  90 class TestPrintPanel(wxPanel
): 
  91     def __init__(self
, parent
, frame
, log
): 
  92         wxPanel
.__init
__(self
, parent
, -1) 
  97         self
.printData 
= wxPrintData() 
  98         self
.printData
.SetPaperId(wxPAPER_LETTER
) 
 100         self
.box 
= box
.wxBoxSizer(wxVERTICAL
) 
 101         self
.canvas 
= MyCanvas(self
) 
 102         self
.box
.Add(self
.canvas
, 1) 
 104         subbox 
= wxBoxSizer(wxHORIZONTAL
) 
 105         btn 
= wxButton(self
, 1201, "Print Setup") 
 106         EVT_BUTTON(self
, 1201, self
.OnPrintSetup
) 
 109         btn 
= wxButton(self
, 1202, "Print Preview") 
 110         EVT_BUTTON(self
, 1202, self
.OnPrintPreview
) 
 113         btn 
= wxButton(self
, 1203, "Print") 
 114         EVT_BUTTON(self
, 1203, self
.OnDoPrint
) 
 121     def OnSize(self
, event
): 
 122         size 
= self
.GetClientSize() 
 123         self
.box
.Layout(size
) 
 125     def OnPrintSetup(self
, event
): 
 126         printerDialog 
= wxPrintDialog(self
) 
 127         printerDialog
.GetPrintDialogData().SetPrintData(self
.printData
) 
 128         printerDialog
.GetPrintDialogData().SetSetupDialog(true
) 
 129         printerDialog
.ShowModal(); 
 130         self
.printData 
= printerDialog
.GetPrintDialogData().GetPrintData() 
 131         printerDialog
.Destroy() 
 134     def OnPrintPreview(self
, event
): 
 135         self
.log
.WriteText("OnPrintPreview\n") 
 136         printout 
= MyPrintout(self
.canvas
, self
.log
) 
 137         printout2 
= MyPrintout(self
.canvas
, self
.log
) 
 138         self
.preview 
= wxPrintPreview(printout
, printout2
, self
.printData
) 
 139         if not self
.preview
.Ok(): 
 140             self
.log
.WriteText("Houston, we have a problem...\n") 
 143         frame 
= wxPreviewFrame(self
.preview
, self
.frame
, "This is a print preview") 
 146         frame
.SetPosition(self
.frame
.GetPosition()) 
 147         frame
.SetSize(self
.frame
.GetSize()) 
 152     def OnDoPrint(self
, event
): 
 153         pdd 
= wxPrintDialogData() 
 154         pdd
.SetPrintData(self
.printData
) 
 155         printer 
= wxPrinter(pdd
) 
 156         printout 
= MyPrintout(self
.canvas
, self
.log
) 
 157         if not printer
.Print(self
.frame
, printout
): 
 158             wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
) 
 160             self
.printData 
= printer
.GetPrintDialogData().GetPrintData() 
 164 #---------------------------------------------------------------------- 
 166 def runTest(frame
, nb
, log
): 
 167     win 
= TestPrintPanel(nb
, frame
, log
) 
 171 #----------------------------------------------------------------------