]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/demo/PrintFramework.py
Significantly improved thanks to vi.
[wxWidgets.git] / utils / wxPython / demo / PrintFramework.py
CommitLineData
bb0054cd
RD
1
2from wxPython.wx import *
3from wxPython.lib.sizers import *
4from wxScrolledWindow import MyCanvas
5
6#----------------------------------------------------------------------
7
8class MyPrintout(wxPrintout):
9 def __init__(self, canvas, log):
10 wxPrintout.__init__(self)
11 self.canvas = canvas
12 self.log = log
13
14 def OnBeginDocument(self, start, end):
15 self.log.WriteText("wxPrintout.OnBeginDocument\n")
16 return self.base_OnBeginDocument(start, end)
17
18 def OnEndDocument(self):
19 self.log.WriteText("wxPrintout.OnEndDocument\n")
20
21 def OnBeginPrinting(self):
22 self.log.WriteText("wxPrintout.OnBeginPrinting\n")
23
24 def OnEndPrinting(self):
25 self.log.WriteText("wxPrintout.OnEndPrinting\n")
26
27 def OnPreparePrinting(self):
28 self.log.WriteText("wxPrintout.OnPreparePrinting\n")
29
30 def HasPage(self, page):
31 self.log.WriteText("wxPrintout.HasPage\n")
32 if page == 1:
33 return true
34 else:
35 return false
36
37 def GetPageInfo(self):
38 self.log.WriteText("wxPrintout.GetPageInfo\n")
39 return (1, 1, 1, 1)
40
41 def OnPrintPage(self, page):
42 self.log.WriteText("wxPrintout.OnPrintPage\n")
43 dc = self.GetDC()
44
45 #-------------------------------------------
46 # One possible method of setting scaling factors...
47
48 maxX = self.canvas.getWidth()
49 maxY = self.canvas.getHeight()
50
51 # Let's have at least 50 device units margin
52 marginX = 50
53 marginY = 50
54
55 # Add the margin to the graphic size
56 maxX = maxX + (2 * marginX)
57 maxY = maxY + (2 * marginY)
58
59 # Get the size of the DC in pixels
60 (w, h) = dc.GetSizeTuple()
61
62 # Calculate a suitable scaling factor
63 scaleX = float(w) / maxX
64 scaleY = float(h) / maxY
65
66 # Use x or y scaling factor, whichever fits on the DC
67 actualScale = min(scaleX, scaleY)
68
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
72
73 # Set the scale and origin
74 dc.SetUserScale(actualScale, actualScale)
75 dc.SetDeviceOrigin(int(posX), int(posY))
76
77 #-------------------------------------------
78
79 self.canvas.DoDrawing(dc)
80 return true
81
82
83#----------------------------------------------------------------------
84
85
86class TestPrintPanel(wxPanel):
87 def __init__(self, parent, frame, log):
88 wxPanel.__init__(self, parent, -1)
89 self.log = log
90 self.frame = frame
91
92
93 self.printData = wxPrintData()
94 self.printData.SetPaperId(wxPAPER_LETTER)
95
96 self.box = box.wxBoxSizer(wxVERTICAL)
97 self.canvas = MyCanvas(self)
98 self.box.Add(self.canvas, 1)
99
100 subbox = wxBoxSizer(wxHORIZONTAL)
101 btn = wxButton(self, 1201, "Print Setup")
102 EVT_BUTTON(self, 1201, self.OnPrintSetup)
103 subbox.Add(btn, 1)
104
105 btn = wxButton(self, 1202, "Print Preview")
106 EVT_BUTTON(self, 1202, self.OnPrintPreview)
107 subbox.Add(btn, 1)
108
109 btn = wxButton(self, 1203, "Print")
110 EVT_BUTTON(self, 1203, self.OnDoPrint)
111 subbox.Add(btn, 1)
112
113 self.box.Add(subbox)
114
115
116
117 def OnSize(self, event):
118 size = self.GetClientSize()
119 self.box.Layout(size)
120
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()
128
129
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")
137 return
138
139 frame = wxPreviewFrame(self.preview, self.frame, "This is a print preview")
140
141 frame.Initialize()
142 frame.SetPosition(self.frame.GetPosition())
143 frame.SetSize(self.frame.GetSize())
144 frame.Show(true)
145
146
147
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)
155 else:
156 self.printData = printer.GetPrintDialogData().GetPrintData()
157 printout.Destroy()
158
159
160#----------------------------------------------------------------------
161
162def runTest(frame, nb, log):
163 win = TestPrintPanel(nb, frame, log)
164 return win
165
166
167#----------------------------------------------------------------------
168
169
170
171
172
173overview = """\
174"""
175