]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PrintFramework.py
2 from wxPython
.wx
import *
3 from wxScrolledWindow
import MyCanvas
5 #----------------------------------------------------------------------
7 class MyPrintout(wxPrintout
):
8 def __init__(self
, canvas
, log
):
9 wxPrintout
.__init
__(self
)
13 def OnBeginDocument(self
, start
, end
):
14 self
.log
.WriteText("wxPrintout.OnBeginDocument\n")
15 return self
.base_OnBeginDocument(start
, end
)
17 def OnEndDocument(self
):
18 self
.log
.WriteText("wxPrintout.OnEndDocument\n")
19 self
.base_OnEndDocument()
21 def OnBeginPrinting(self
):
22 self
.log
.WriteText("wxPrintout.OnBeginPrinting\n")
23 self
.base_OnBeginPrinting()
25 def OnEndPrinting(self
):
26 self
.log
.WriteText("wxPrintout.OnEndPrinting\n")
27 self
.base_OnEndPrinting()
29 def OnPreparePrinting(self
):
30 self
.log
.WriteText("wxPrintout.OnPreparePrinting\n")
31 self
.base_OnPreparePrinting()
33 def HasPage(self
, page
):
34 self
.log
.WriteText("wxPrintout.HasPage\n")
40 def GetPageInfo(self
):
41 self
.log
.WriteText("wxPrintout.GetPageInfo\n")
44 def OnPrintPage(self
, page
):
45 self
.log
.WriteText("wxPrintout.OnPrintPage\n")
48 #-------------------------------------------
49 # One possible method of setting scaling factors...
51 maxX
= self
.canvas
.getWidth()
52 maxY
= self
.canvas
.getHeight()
54 # Let's have at least 50 device units margin
58 # Add the margin to the graphic size
59 maxX
= maxX
+ (2 * marginX
)
60 maxY
= maxY
+ (2 * marginY
)
62 # Get the size of the DC in pixels
63 (w
, h
) = dc
.GetSizeTuple()
65 # Calculate a suitable scaling factor
66 scaleX
= float(w
) / maxX
67 scaleY
= float(h
) / maxY
69 # Use x or y scaling factor, whichever fits on the DC
70 actualScale
= min(scaleX
, scaleY
)
72 # Calculate the position on the DC for centring the graphic
73 posX
= (w
- (self
.canvas
.getWidth() * actualScale
)) / 2.0
74 posY
= (h
- (self
.canvas
.getHeight() * actualScale
)) / 2.0
76 # Set the scale and origin
77 dc
.SetUserScale(actualScale
, actualScale
)
78 dc
.SetDeviceOrigin(int(posX
), int(posY
))
80 #-------------------------------------------
82 self
.canvas
.DoDrawing(dc
)
86 #----------------------------------------------------------------------
89 class TestPrintPanel(wxPanel
):
90 def __init__(self
, parent
, frame
, log
):
91 wxPanel
.__init
__(self
, parent
, -1)
96 self
.printData
= wxPrintData()
97 self
.printData
.SetPaperId(wxPAPER_LETTER
)
99 self
.box
= wxBoxSizer(wxVERTICAL
)
100 self
.canvas
= MyCanvas(self
)
101 self
.box
.Add(self
.canvas
, 1, wxGROW
)
103 subbox
= wxBoxSizer(wxHORIZONTAL
)
104 btn
= wxButton(self
, 1201, "Print Setup")
105 EVT_BUTTON(self
, 1201, self
.OnPrintSetup
)
106 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
108 btn
= wxButton(self
, 1202, "Print Preview")
109 EVT_BUTTON(self
, 1202, self
.OnPrintPreview
)
110 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
112 btn
= wxButton(self
, 1203, "Print")
113 EVT_BUTTON(self
, 1203, self
.OnDoPrint
)
114 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
116 self
.box
.Add(subbox
, 0, wxGROW
)
118 self
.SetAutoLayout(true
)
119 self
.SetSizer(self
.box
)
122 def OnPrintSetup(self
, event
):
123 printerDialog
= wxPrintDialog(self
)
124 printerDialog
.GetPrintDialogData().SetPrintData(self
.printData
)
125 printerDialog
.GetPrintDialogData().SetSetupDialog(true
)
126 printerDialog
.ShowModal();
127 self
.printData
= printerDialog
.GetPrintDialogData().GetPrintData()
128 printerDialog
.Destroy()
131 def OnPrintPreview(self
, event
):
132 self
.log
.WriteText("OnPrintPreview\n")
133 printout
= MyPrintout(self
.canvas
, self
.log
)
134 printout2
= MyPrintout(self
.canvas
, self
.log
)
135 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
136 if not self
.preview
.Ok():
137 self
.log
.WriteText("Houston, we have a problem...\n")
140 frame
= wxPreviewFrame(self
.preview
, self
.frame
, "This is a print preview")
143 frame
.SetPosition(self
.frame
.GetPosition())
144 frame
.SetSize(self
.frame
.GetSize())
149 def OnDoPrint(self
, event
):
150 pdd
= wxPrintDialogData()
151 pdd
.SetPrintData(self
.printData
)
152 printer
= wxPrinter(pdd
)
153 printout
= MyPrintout(self
.canvas
, self
.log
)
154 if not printer
.Print(self
.frame
, printout
):
155 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
157 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
161 #----------------------------------------------------------------------
163 def runTest(frame
, nb
, log
):
164 win
= TestPrintPanel(nb
, frame
, log
)
168 #----------------------------------------------------------------------