]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/PrintFramework.py
docstring update
[wxWidgets.git] / wxPython / demo / PrintFramework.py
CommitLineData
bb0054cd 1
8fa876ca 2import wx
299647ac 3import ScrolledWindow
bb0054cd
RD
4
5#----------------------------------------------------------------------
6
8fa876ca
RD
7
8class MyPrintout(wx.Printout):
bb0054cd 9 def __init__(self, canvas, log):
8fa876ca 10 wx.Printout.__init__(self)
bb0054cd
RD
11 self.canvas = canvas
12 self.log = log
13
14 def OnBeginDocument(self, start, end):
a7a01418
RD
15 self.log.WriteText("MyPrintout.OnBeginDocument\n")
16 return super(MyPrintout, self).OnBeginDocument(start, end)
bb0054cd
RD
17
18 def OnEndDocument(self):
a7a01418
RD
19 self.log.WriteText("MyPrintout.OnEndDocument\n")
20 super(MyPrintout, self).OnEndDocument()
bb0054cd
RD
21
22 def OnBeginPrinting(self):
a7a01418
RD
23 self.log.WriteText("MyPrintout.OnBeginPrinting\n")
24 super(MyPrintout, self).OnBeginPrinting()
bb0054cd
RD
25
26 def OnEndPrinting(self):
a7a01418
RD
27 self.log.WriteText("MyPrintout.OnEndPrinting\n")
28 super(MyPrintout, self).OnEndPrinting()
bb0054cd
RD
29
30 def OnPreparePrinting(self):
a7a01418
RD
31 self.log.WriteText("MyPrintout.OnPreparePrinting\n")
32 super(MyPrintout, self).OnPreparePrinting()
bb0054cd
RD
33
34 def HasPage(self, page):
a7a01418 35 self.log.WriteText("MyPrintout.HasPage: %d\n" % page)
0122b7e3 36 if page <= 2:
1e4a197e 37 return True
bb0054cd 38 else:
1e4a197e 39 return False
bb0054cd
RD
40
41 def GetPageInfo(self):
a7a01418 42 self.log.WriteText("MyPrintout.GetPageInfo\n")
0122b7e3 43 return (1, 2, 1, 2)
bb0054cd
RD
44
45 def OnPrintPage(self, page):
a7a01418 46 self.log.WriteText("MyPrintout.OnPrintPage: %d\n" % page)
bb0054cd
RD
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
8b9a4190 73 # Calculate the position on the DC for centering the graphic
bb0054cd
RD
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
1fded56b 83 self.canvas.DoDrawing(dc, True)
d7403ad2 84 dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
0122b7e3 85
1e4a197e 86 return True
bb0054cd
RD
87
88
89#----------------------------------------------------------------------
90
91
8fa876ca 92class TestPrintPanel(wx.Panel):
bb0054cd 93 def __init__(self, parent, frame, log):
8fa876ca 94 wx.Panel.__init__(self, parent, -1)
bb0054cd
RD
95 self.log = log
96 self.frame = frame
97
8fa876ca
RD
98 self.printData = wx.PrintData()
99 self.printData.SetPaperId(wx.PAPER_LETTER)
d88d4683
RD
100 self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
101
8fa876ca 102 self.box = wx.BoxSizer(wx.VERTICAL)
299647ac 103 self.canvas = ScrolledWindow.MyCanvas(self)
8fa876ca 104 self.box.Add(self.canvas, 1, wx.GROW)
bb0054cd 105
8fa876ca 106 subbox = wx.BoxSizer(wx.HORIZONTAL)
13bf44b0
RD
107 btn = wx.Button(self, -1, "Page Setup")
108 self.Bind(wx.EVT_BUTTON, self.OnPageSetup, btn)
8fa876ca 109 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
bb0054cd 110
13bf44b0 111 btn = wx.Button(self, -1, "Print Preview")
d88d4683 112 self.Bind(wx.EVT_BUTTON, self.OnPrintPreview, btn)
8fa876ca 113 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
bb0054cd 114
13bf44b0 115 btn = wx.Button(self, -1, "Print")
d88d4683 116 self.Bind(wx.EVT_BUTTON, self.OnDoPrint, btn)
8fa876ca 117 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
bb0054cd 118
8fa876ca 119 self.box.Add(subbox, 0, wx.GROW)
bb0054cd 120
1e4a197e 121 self.SetAutoLayout(True)
2f90df85 122 self.SetSizer(self.box)
bb0054cd
RD
123
124
13bf44b0
RD
125
126 def OnPageSetup(self, evt):
127 psdd = wx.PageSetupDialogData(self.printData)
128 psdd.CalculatePaperSizeFromId()
129 dlg = wx.PageSetupDialog(self, psdd)
130 dlg.ShowModal()
356f3c65
RD
131
132 # this makes a copy of the wx.PrintData instead of just saving
54563c57 133 # a reference to the one inside the PrintDialogData that will
6b9f434e 134 # be destroyed when the dialog is destroyed
13bf44b0 135 self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
bb0054cd 136
13bf44b0 137 dlg.Destroy()
bb0054cd
RD
138
139 def OnPrintPreview(self, event):
d88d4683 140 data = wx.PrintDialogData(self.printData)
bb0054cd
RD
141 printout = MyPrintout(self.canvas, self.log)
142 printout2 = MyPrintout(self.canvas, self.log)
d88d4683 143 self.preview = wx.PrintPreview(printout, printout2, data)
8fa876ca 144
bb0054cd
RD
145 if not self.preview.Ok():
146 self.log.WriteText("Houston, we have a problem...\n")
147 return
148
13bf44b0 149 pfrm = wx.PreviewFrame(self.preview, self.frame, "This is a print preview")
bb0054cd 150
13bf44b0
RD
151 pfrm.Initialize()
152 pfrm.SetPosition(self.frame.GetPosition())
153 pfrm.SetSize(self.frame.GetSize())
154 pfrm.Show(True)
bb0054cd
RD
155
156
157
158 def OnDoPrint(self, event):
d88d4683
RD
159 pdd = wx.PrintDialogData(self.printData)
160 pdd.SetToPage(2)
8fa876ca 161 printer = wx.Printer(pdd)
bb0054cd 162 printout = MyPrintout(self.canvas, self.log)
8fa876ca 163
d88d4683 164 if not printer.Print(self.frame, printout, True):
8fa876ca 165 wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
bb0054cd 166 else:
356f3c65 167 self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
bb0054cd
RD
168 printout.Destroy()
169
170
171#----------------------------------------------------------------------
172
173def runTest(frame, nb, log):
174 win = TestPrintPanel(nb, frame, log)
175 return win
176
177
178#----------------------------------------------------------------------
179
180
181
182
183
184overview = """\
8fa876ca
RD
185<html>
186<body>
187<h1>PrintFramework</h1>
188
189This is an overview of the classes and methods used to print documents.
190It also demonstrates how to do print previews and invoke the printer
191setup 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
219framework in wxPython.
220
221</body>
222</html>
1fded56b 223
8fa876ca 224"""
1fded56b
RD
225
226
227if __name__ == '__main__':
228 import sys,os
229 import run
8eca4fef 230 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1fded56b 231