]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/PrintFramework.py
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")
21 def OnBeginPrinting(self
):
22 self
.log
.WriteText("wxPrintout.OnBeginPrinting\n")
24 def OnEndPrinting(self
):
25 self
.log
.WriteText("wxPrintout.OnEndPrinting\n")
27 def OnPreparePrinting(self
):
28 self
.log
.WriteText("wxPrintout.OnPreparePrinting\n")
30 def HasPage(self
, page
):
31 self
.log
.WriteText("wxPrintout.HasPage\n")
37 def GetPageInfo(self
):
38 self
.log
.WriteText("wxPrintout.GetPageInfo\n")
41 def OnPrintPage(self
, page
):
42 self
.log
.WriteText("wxPrintout.OnPrintPage\n")
45 #-------------------------------------------
46 # One possible method of setting scaling factors...
48 maxX
= self
.canvas
.getWidth()
49 maxY
= self
.canvas
.getHeight()
51 # Let's have at least 50 device units margin
55 # Add the margin to the graphic size
56 maxX
= maxX
+ (2 * marginX
)
57 maxY
= maxY
+ (2 * marginY
)
59 # Get the size of the DC in pixels
60 (w
, h
) = dc
.GetSizeTuple()
62 # Calculate a suitable scaling factor
63 scaleX
= float(w
) / maxX
64 scaleY
= float(h
) / maxY
66 # Use x or y scaling factor, whichever fits on the DC
67 actualScale
= min(scaleX
, scaleY
)
69 # Calculate the position on the DC for centring the graphic
70 posX
= (w
- (self
.canvas
.getWidth() * actualScale
)) / 2.0
71 posY
= (h
- (self
.canvas
.getHeight() * actualScale
)) / 2.0
73 # Set the scale and origin
74 dc
.SetUserScale(actualScale
, actualScale
)
75 dc
.SetDeviceOrigin(int(posX
), int(posY
))
77 #-------------------------------------------
79 self
.canvas
.DoDrawing(dc
)
83 #----------------------------------------------------------------------
86 class TestPrintPanel(wxPanel
):
87 def __init__(self
, parent
, frame
, log
):
88 wxPanel
.__init
__(self
, parent
, -1)
93 self
.printData
= wxPrintData()
94 self
.printData
.SetPaperId(wxPAPER_LETTER
)
96 self
.box
= box
.wxBoxSizer(wxVERTICAL
)
97 self
.canvas
= MyCanvas(self
)
98 self
.box
.Add(self
.canvas
, 1)
100 subbox
= wxBoxSizer(wxHORIZONTAL
)
101 btn
= wxButton(self
, 1201, "Print Setup")
102 EVT_BUTTON(self
, 1201, self
.OnPrintSetup
)
105 btn
= wxButton(self
, 1202, "Print Preview")
106 EVT_BUTTON(self
, 1202, self
.OnPrintPreview
)
109 btn
= wxButton(self
, 1203, "Print")
110 EVT_BUTTON(self
, 1203, self
.OnDoPrint
)
117 def OnSize(self
, event
):
118 size
= self
.GetClientSize()
119 self
.box
.Layout(size
)
121 def OnPrintSetup(self
, event
):
122 printerDialog
= wxPrintDialog(self
)
123 printerDialog
.GetPrintDialogData().SetPrintData(self
.printData
)
124 printerDialog
.GetPrintDialogData().SetSetupDialog(true
)
125 printerDialog
.ShowModal();
126 self
.printData
= printerDialog
.GetPrintDialogData().GetPrintData()
127 printerDialog
.Destroy()
130 def OnPrintPreview(self
, event
):
131 self
.log
.WriteText("OnPrintPreview\n")
132 printout
= MyPrintout(self
.canvas
, self
.log
)
133 printout2
= MyPrintout(self
.canvas
, self
.log
)
134 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
135 if not self
.preview
.Ok():
136 self
.log
.WriteText("Houston, we have a problem...\n")
139 frame
= wxPreviewFrame(self
.preview
, self
.frame
, "This is a print preview")
142 frame
.SetPosition(self
.frame
.GetPosition())
143 frame
.SetSize(self
.frame
.GetSize())
148 def OnDoPrint(self
, event
):
149 pdd
= wxPrintDialogData()
150 pdd
.SetPrintData(self
.printData
)
151 printer
= wxPrinter(pdd
)
152 printout
= MyPrintout(self
.canvas
, self
.log
)
153 if not printer
.Print(self
.frame
, printout
):
154 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
156 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
160 #----------------------------------------------------------------------
162 def runTest(frame
, nb
, log
):
163 win
= TestPrintPanel(nb
, frame
, log
)
167 #----------------------------------------------------------------------