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