]>
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: %d\n" % page
)
40 def GetPageInfo(self
):
41 self
.log
.WriteText("wxPrintout.GetPageInfo\n")
44 def OnPrintPage(self
, page
):
45 self
.log
.WriteText("wxPrintout.OnPrintPage: %d\n" % page
)
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
, True)
83 dc
.DrawText("Page: %d" % page
, marginX
/2, maxY
-marginY
)
88 #----------------------------------------------------------------------
91 class TestPrintPanel(wxPanel
):
92 def __init__(self
, parent
, frame
, log
):
93 wxPanel
.__init
__(self
, parent
, -1)
98 self
.printData
= wxPrintData()
99 self
.printData
.SetPaperId(wxPAPER_LETTER
)
101 self
.box
= wxBoxSizer(wxVERTICAL
)
102 self
.canvas
= MyCanvas(self
)
103 self
.box
.Add(self
.canvas
, 1, wxGROW
)
105 subbox
= wxBoxSizer(wxHORIZONTAL
)
106 btn
= wxButton(self
, 1201, "Print Setup")
107 EVT_BUTTON(self
, 1201, self
.OnPrintSetup
)
108 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
110 btn
= wxButton(self
, 1202, "Print Preview")
111 EVT_BUTTON(self
, 1202, self
.OnPrintPreview
)
112 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
114 btn
= wxButton(self
, 1203, "Print")
115 EVT_BUTTON(self
, 1203, self
.OnDoPrint
)
116 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
118 self
.box
.Add(subbox
, 0, wxGROW
)
120 self
.SetAutoLayout(True)
121 self
.SetSizer(self
.box
)
124 def OnPrintSetup(self
, event
):
125 printerDialog
= wxPrintDialog(self
)
126 printerDialog
.GetPrintDialogData().SetPrintData(self
.printData
)
127 printerDialog
.GetPrintDialogData().SetSetupDialog(True)
128 printerDialog
.ShowModal();
129 self
.printData
= printerDialog
.GetPrintDialogData().GetPrintData()
130 printerDialog
.Destroy()
133 def OnPrintPreview(self
, event
):
134 self
.log
.WriteText("OnPrintPreview\n")
135 printout
= MyPrintout(self
.canvas
, self
.log
)
136 printout2
= MyPrintout(self
.canvas
, self
.log
)
137 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
138 if not self
.preview
.Ok():
139 self
.log
.WriteText("Houston, we have a problem...\n")
142 frame
= wxPreviewFrame(self
.preview
, self
.frame
, "This is a print preview")
145 frame
.SetPosition(self
.frame
.GetPosition())
146 frame
.SetSize(self
.frame
.GetSize())
151 def OnDoPrint(self
, event
):
152 pdd
= wxPrintDialogData()
153 pdd
.SetPrintData(self
.printData
)
154 printer
= wxPrinter(pdd
)
155 printout
= MyPrintout(self
.canvas
, self
.log
)
156 if not printer
.Print(self
.frame
, printout
):
157 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
159 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
163 #----------------------------------------------------------------------
165 def runTest(frame
, nb
, log
):
166 win
= TestPrintPanel(nb
, frame
, log
)
170 #----------------------------------------------------------------------
183 if __name__
== '__main__':
186 run
.main(['', os
.path
.basename(sys
.argv
[0])])