]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
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 | # | |
bb0054cd | 7 | |
8fa876ca RD |
8 | import wx |
9 | import wxScrolledWindow | |
bb0054cd RD |
10 | |
11 | #---------------------------------------------------------------------- | |
12 | ||
8fa876ca RD |
13 | ID_Setup = wx.NewId() |
14 | ID_Preview = wx.NewId() | |
15 | ID_Print = wx.NewId() | |
16 | ||
17 | class MyPrintout(wx.Printout): | |
bb0054cd | 18 | def __init__(self, canvas, log): |
8fa876ca | 19 | wx.Printout.__init__(self) |
bb0054cd RD |
20 | self.canvas = canvas |
21 | self.log = log | |
22 | ||
23 | def OnBeginDocument(self, start, end): | |
8fa876ca | 24 | self.log.WriteText("wx.Printout.OnBeginDocument\n") |
bb0054cd RD |
25 | return self.base_OnBeginDocument(start, end) |
26 | ||
27 | def OnEndDocument(self): | |
8fa876ca | 28 | self.log.WriteText("wx.Printout.OnEndDocument\n") |
b1cf23dc | 29 | self.base_OnEndDocument() |
bb0054cd RD |
30 | |
31 | def OnBeginPrinting(self): | |
8fa876ca | 32 | self.log.WriteText("wx.Printout.OnBeginPrinting\n") |
b1cf23dc | 33 | self.base_OnBeginPrinting() |
bb0054cd RD |
34 | |
35 | def OnEndPrinting(self): | |
8fa876ca | 36 | self.log.WriteText("wx.Printout.OnEndPrinting\n") |
b1cf23dc | 37 | self.base_OnEndPrinting() |
bb0054cd RD |
38 | |
39 | def OnPreparePrinting(self): | |
8fa876ca | 40 | self.log.WriteText("wx.Printout.OnPreparePrinting\n") |
b1cf23dc | 41 | self.base_OnPreparePrinting() |
bb0054cd RD |
42 | |
43 | def HasPage(self, page): | |
8fa876ca | 44 | self.log.WriteText("wx.Printout.HasPage: %d\n" % page) |
0122b7e3 | 45 | if page <= 2: |
1e4a197e | 46 | return True |
bb0054cd | 47 | else: |
1e4a197e | 48 | return False |
bb0054cd RD |
49 | |
50 | def GetPageInfo(self): | |
8fa876ca | 51 | self.log.WriteText("wx.Printout.GetPageInfo\n") |
0122b7e3 | 52 | return (1, 2, 1, 2) |
bb0054cd RD |
53 | |
54 | def OnPrintPage(self, page): | |
8fa876ca | 55 | self.log.WriteText("wx.Printout.OnPrintPage: %d\n" % page) |
bb0054cd RD |
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 | ||
8b9a4190 | 82 | # Calculate the position on the DC for centering the graphic |
bb0054cd RD |
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 | ||
1fded56b | 92 | self.canvas.DoDrawing(dc, True) |
fd3f2efe | 93 | dc.DrawText("Page: %d" % page, (marginX/2, maxY-marginY)) |
0122b7e3 | 94 | |
1e4a197e | 95 | return True |
bb0054cd RD |
96 | |
97 | ||
98 | #---------------------------------------------------------------------- | |
99 | ||
100 | ||
8fa876ca | 101 | class TestPrintPanel(wx.Panel): |
bb0054cd | 102 | def __init__(self, parent, frame, log): |
8fa876ca | 103 | wx.Panel.__init__(self, parent, -1) |
bb0054cd RD |
104 | self.log = log |
105 | self.frame = frame | |
106 | ||
8fa876ca RD |
107 | self.printData = wx.PrintData() |
108 | self.printData.SetPaperId(wx.PAPER_LETTER) | |
bb0054cd | 109 | |
8fa876ca RD |
110 | self.box = wx.BoxSizer(wx.VERTICAL) |
111 | self.canvas = wxScrolledWindow.MyCanvas(self) | |
112 | self.box.Add(self.canvas, 1, wx.GROW) | |
bb0054cd | 113 | |
8fa876ca RD |
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) | |
bb0054cd | 118 | |
8fa876ca RD |
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) | |
bb0054cd | 122 | |
8fa876ca RD |
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) | |
bb0054cd | 126 | |
8fa876ca | 127 | self.box.Add(subbox, 0, wx.GROW) |
bb0054cd | 128 | |
1e4a197e | 129 | self.SetAutoLayout(True) |
2f90df85 | 130 | self.SetSizer(self.box) |
bb0054cd RD |
131 | |
132 | ||
bb0054cd | 133 | def OnPrintSetup(self, event): |
8fa876ca | 134 | printerDialog = wx.PrintDialog(self) |
bb0054cd | 135 | printerDialog.GetPrintDialogData().SetPrintData(self.printData) |
1e4a197e | 136 | printerDialog.GetPrintDialogData().SetSetupDialog(True) |
bb0054cd RD |
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) | |
8fa876ca RD |
146 | self.preview = wx.PrintPreview(printout, printout2, self.printData) |
147 | ||
bb0054cd RD |
148 | if not self.preview.Ok(): |
149 | self.log.WriteText("Houston, we have a problem...\n") | |
150 | return | |
151 | ||
8fa876ca | 152 | frame = wx.PreviewFrame(self.preview, self.frame, "This is a print preview") |
bb0054cd RD |
153 | |
154 | frame.Initialize() | |
155 | frame.SetPosition(self.frame.GetPosition()) | |
156 | frame.SetSize(self.frame.GetSize()) | |
1e4a197e | 157 | frame.Show(True) |
bb0054cd RD |
158 | |
159 | ||
160 | ||
161 | def OnDoPrint(self, event): | |
8fa876ca | 162 | pdd = wx.PrintDialogData() |
bb0054cd | 163 | pdd.SetPrintData(self.printData) |
8fa876ca | 164 | printer = wx.Printer(pdd) |
bb0054cd | 165 | printout = MyPrintout(self.canvas, self.log) |
8fa876ca | 166 | |
bb0054cd | 167 | if not printer.Print(self.frame, printout): |
8fa876ca | 168 | wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK) |
bb0054cd RD |
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 = """\ | |
8fa876ca RD |
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> | |
1fded56b | 226 | |
8fa876ca | 227 | """ |
1fded56b RD |
228 | |
229 | ||
230 | if __name__ == '__main__': | |
231 | import sys,os | |
232 | import run | |
233 | run.main(['', os.path.basename(sys.argv[0])]) | |
234 |