]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PrintFramework.py
better __docfilter__ and other epydoc tweaks
[wxWidgets.git] / wxPython / demo / PrintFramework.py
1
2 import wx
3 import ScrolledWindow
4
5 #----------------------------------------------------------------------
6
7 ID_Setup = wx.NewId()
8 ID_Preview = wx.NewId()
9 ID_Print = wx.NewId()
10
11 class MyPrintout(wx.Printout):
12 def __init__(self, canvas, log):
13 wx.Printout.__init__(self)
14 self.canvas = canvas
15 self.log = log
16
17 def OnBeginDocument(self, start, end):
18 self.log.WriteText("wx.Printout.OnBeginDocument\n")
19 return self.base_OnBeginDocument(start, end)
20
21 def OnEndDocument(self):
22 self.log.WriteText("wx.Printout.OnEndDocument\n")
23 self.base_OnEndDocument()
24
25 def OnBeginPrinting(self):
26 self.log.WriteText("wx.Printout.OnBeginPrinting\n")
27 self.base_OnBeginPrinting()
28
29 def OnEndPrinting(self):
30 self.log.WriteText("wx.Printout.OnEndPrinting\n")
31 self.base_OnEndPrinting()
32
33 def OnPreparePrinting(self):
34 self.log.WriteText("wx.Printout.OnPreparePrinting\n")
35 self.base_OnPreparePrinting()
36
37 def HasPage(self, page):
38 self.log.WriteText("wx.Printout.HasPage: %d\n" % page)
39 if page <= 2:
40 return True
41 else:
42 return False
43
44 def GetPageInfo(self):
45 self.log.WriteText("wx.Printout.GetPageInfo\n")
46 return (1, 2, 1, 2)
47
48 def OnPrintPage(self, page):
49 self.log.WriteText("wx.Printout.OnPrintPage: %d\n" % page)
50 dc = self.GetDC()
51
52 #-------------------------------------------
53 # One possible method of setting scaling factors...
54
55 maxX = self.canvas.getWidth()
56 maxY = self.canvas.getHeight()
57
58 # Let's have at least 50 device units margin
59 marginX = 50
60 marginY = 50
61
62 # Add the margin to the graphic size
63 maxX = maxX + (2 * marginX)
64 maxY = maxY + (2 * marginY)
65
66 # Get the size of the DC in pixels
67 (w, h) = dc.GetSizeTuple()
68
69 # Calculate a suitable scaling factor
70 scaleX = float(w) / maxX
71 scaleY = float(h) / maxY
72
73 # Use x or y scaling factor, whichever fits on the DC
74 actualScale = min(scaleX, scaleY)
75
76 # Calculate the position on the DC for centering the graphic
77 posX = (w - (self.canvas.getWidth() * actualScale)) / 2.0
78 posY = (h - (self.canvas.getHeight() * actualScale)) / 2.0
79
80 # Set the scale and origin
81 dc.SetUserScale(actualScale, actualScale)
82 dc.SetDeviceOrigin(int(posX), int(posY))
83
84 #-------------------------------------------
85
86 self.canvas.DoDrawing(dc, True)
87 dc.DrawText("Page: %d" % page, (marginX/2, maxY-marginY))
88
89 return True
90
91
92 #----------------------------------------------------------------------
93
94
95 class TestPrintPanel(wx.Panel):
96 def __init__(self, parent, frame, log):
97 wx.Panel.__init__(self, parent, -1)
98 self.log = log
99 self.frame = frame
100
101 self.printData = wx.PrintData()
102 self.printData.SetPaperId(wx.PAPER_LETTER)
103 self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
104
105 self.box = wx.BoxSizer(wx.VERTICAL)
106 self.canvas = ScrolledWindow.MyCanvas(self)
107 self.box.Add(self.canvas, 1, wx.GROW)
108
109 subbox = wx.BoxSizer(wx.HORIZONTAL)
110 btn = wx.Button(self, ID_Setup, "Print Setup")
111 self.Bind(wx.EVT_BUTTON, self.OnPrintSetup, btn)
112 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
113
114 btn = wx.Button(self, ID_Preview, "Print Preview")
115 self.Bind(wx.EVT_BUTTON, self.OnPrintPreview, btn)
116 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
117
118 btn = wx.Button(self, ID_Print, "Print")
119 self.Bind(wx.EVT_BUTTON, self.OnDoPrint, btn)
120 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
121
122 self.box.Add(subbox, 0, wx.GROW)
123
124 self.SetAutoLayout(True)
125 self.SetSizer(self.box)
126
127
128 def OnPrintSetup(self, event):
129 data = wx.PrintDialogData(self.printData)
130 printerDialog = wx.PrintDialog(self, data)
131 printerDialog.GetPrintDialogData().SetSetupDialog(True)
132 printerDialog.ShowModal();
133 self.printData = printerDialog.GetPrintDialogData().GetPrintData()
134 printerDialog.Destroy()
135
136
137 def OnPrintPreview(self, event):
138 self.log.WriteText("OnPrintPreview\n")
139 data = wx.PrintDialogData(self.printData)
140 printout = MyPrintout(self.canvas, self.log)
141 printout2 = MyPrintout(self.canvas, self.log)
142 self.preview = wx.PrintPreview(printout, printout2, data)
143
144 if not self.preview.Ok():
145 self.log.WriteText("Houston, we have a problem...\n")
146 return
147
148 frame = wx.PreviewFrame(self.preview, self.frame, "This is a print preview")
149
150 frame.Initialize()
151 frame.SetPosition(self.frame.GetPosition())
152 frame.SetSize(self.frame.GetSize())
153 frame.Show(True)
154
155
156
157 def OnDoPrint(self, event):
158 pdd = wx.PrintDialogData(self.printData)
159 pdd.SetToPage(2)
160 printer = wx.Printer(pdd)
161 printout = MyPrintout(self.canvas, self.log)
162
163 if not printer.Print(self.frame, printout, True):
164 wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
165 else:
166 self.printData = printer.GetPrintDialogData().GetPrintData()
167 printout.Destroy()
168
169
170 #----------------------------------------------------------------------
171
172 def runTest(frame, nb, log):
173 win = TestPrintPanel(nb, frame, log)
174 return win
175
176
177 #----------------------------------------------------------------------
178
179
180
181
182
183 overview = """\
184 <html>
185 <body>
186 <h1>PrintFramework</h1>
187
188 This is an overview of the classes and methods used to print documents.
189 It also demonstrates how to do print previews and invoke the printer
190 setup dialog.
191
192 <p>Classes demonstrated here:<P>
193 <ul>
194 <li><b>wx.Printout()</b> - This class encapsulates the functionality of printing out
195 an application document. A new class must be derived and members overridden
196 to respond to calls such as OnPrintPage and HasPage. Instances of this class
197 are passed to wx.Printer.Print() or a wx.PrintPreview object to initiate
198 printing or previewing.<P><p>
199
200 <li><b>wx.PrintData()</b> - This class holds a variety of information related to
201 printers and printer device contexts. This class is used to create a
202 wx.PrinterDC and a wx.PostScriptDC. It is also used as a data member of
203 wx.PrintDialogData and wx.PageSetupDialogData, as part of the mechanism for
204 transferring data between the print dialogs and the application.<p><p>
205
206 <li><b>wx.PrintDialog()</b> - This class represents the print and print setup
207 common dialogs. You may obtain a wx.PrinterDC device context from a
208 successfully dismissed print dialog.<p><p>
209
210 <li><b>wx.PrintPreview()</b> - Objects of this class manage the print preview
211 process. The object is passed a wx.Printout object, and the wx.PrintPreview
212 object itself is passed to a wx.PreviewFrame object. Previewing is started by
213 initializing and showing the preview frame. Unlike wxPrinter.Print, flow of
214 control returns to the application immediately after the frame is shown.<p><p>
215 </ul>
216
217 <p>Other classes are also demonstrated, but this is the gist of the printer interface
218 framework in wxPython.
219
220 </body>
221 </html>
222
223 """
224
225
226 if __name__ == '__main__':
227 import sys,os
228 import run
229 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
230