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