]>
Commit | Line | Data |
---|---|---|
bb0054cd RD |
1 | |
2 | from wxPython.wx import * | |
bb0054cd RD |
3 | from wxScrolledWindow import MyCanvas |
4 | ||
5 | #---------------------------------------------------------------------- | |
6 | ||
7 | class MyPrintout(wxPrintout): | |
8 | def __init__(self, canvas, log): | |
9 | wxPrintout.__init__(self) | |
10 | self.canvas = canvas | |
11 | self.log = log | |
12 | ||
13 | def OnBeginDocument(self, start, end): | |
14 | self.log.WriteText("wxPrintout.OnBeginDocument\n") | |
15 | return self.base_OnBeginDocument(start, end) | |
16 | ||
17 | def OnEndDocument(self): | |
18 | self.log.WriteText("wxPrintout.OnEndDocument\n") | |
b1cf23dc | 19 | self.base_OnEndDocument() |
bb0054cd RD |
20 | |
21 | def OnBeginPrinting(self): | |
22 | self.log.WriteText("wxPrintout.OnBeginPrinting\n") | |
b1cf23dc | 23 | self.base_OnBeginPrinting() |
bb0054cd RD |
24 | |
25 | def OnEndPrinting(self): | |
26 | self.log.WriteText("wxPrintout.OnEndPrinting\n") | |
b1cf23dc | 27 | self.base_OnEndPrinting() |
bb0054cd RD |
28 | |
29 | def OnPreparePrinting(self): | |
30 | self.log.WriteText("wxPrintout.OnPreparePrinting\n") | |
b1cf23dc | 31 | self.base_OnPreparePrinting() |
bb0054cd RD |
32 | |
33 | def HasPage(self, page): | |
0122b7e3 RD |
34 | self.log.WriteText("wxPrintout.HasPage: %d\n" % page) |
35 | if page <= 2: | |
1e4a197e | 36 | return True |
bb0054cd | 37 | else: |
1e4a197e | 38 | return False |
bb0054cd RD |
39 | |
40 | def GetPageInfo(self): | |
41 | self.log.WriteText("wxPrintout.GetPageInfo\n") | |
0122b7e3 | 42 | return (1, 2, 1, 2) |
bb0054cd RD |
43 | |
44 | def OnPrintPage(self, page): | |
0122b7e3 | 45 | self.log.WriteText("wxPrintout.OnPrintPage: %d\n" % page) |
bb0054cd RD |
46 | dc = self.GetDC() |
47 | ||
48 | #------------------------------------------- | |
49 | # One possible method of setting scaling factors... | |
50 | ||
51 | maxX = self.canvas.getWidth() | |
52 | maxY = self.canvas.getHeight() | |
53 | ||
54 | # Let's have at least 50 device units margin | |
55 | marginX = 50 | |
56 | marginY = 50 | |
57 | ||
58 | # Add the margin to the graphic size | |
59 | maxX = maxX + (2 * marginX) | |
60 | maxY = maxY + (2 * marginY) | |
61 | ||
62 | # Get the size of the DC in pixels | |
63 | (w, h) = dc.GetSizeTuple() | |
64 | ||
65 | # Calculate a suitable scaling factor | |
66 | scaleX = float(w) / maxX | |
67 | scaleY = float(h) / maxY | |
68 | ||
69 | # Use x or y scaling factor, whichever fits on the DC | |
70 | actualScale = min(scaleX, scaleY) | |
71 | ||
72 | # Calculate the position on the DC for centring the graphic | |
73 | posX = (w - (self.canvas.getWidth() * actualScale)) / 2.0 | |
74 | posY = (h - (self.canvas.getHeight() * actualScale)) / 2.0 | |
75 | ||
76 | # Set the scale and origin | |
77 | dc.SetUserScale(actualScale, actualScale) | |
78 | dc.SetDeviceOrigin(int(posX), int(posY)) | |
79 | ||
80 | #------------------------------------------- | |
81 | ||
82 | self.canvas.DoDrawing(dc) | |
0122b7e3 RD |
83 | dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY) |
84 | ||
1e4a197e | 85 | return True |
bb0054cd RD |
86 | |
87 | ||
88 | #---------------------------------------------------------------------- | |
89 | ||
90 | ||
91 | class TestPrintPanel(wxPanel): | |
92 | def __init__(self, parent, frame, log): | |
93 | wxPanel.__init__(self, parent, -1) | |
94 | self.log = log | |
95 | self.frame = frame | |
96 | ||
97 | ||
98 | self.printData = wxPrintData() | |
99 | self.printData.SetPaperId(wxPAPER_LETTER) | |
100 | ||
2f90df85 | 101 | self.box = wxBoxSizer(wxVERTICAL) |
bb0054cd | 102 | self.canvas = MyCanvas(self) |
2f90df85 | 103 | self.box.Add(self.canvas, 1, wxGROW) |
bb0054cd RD |
104 | |
105 | subbox = wxBoxSizer(wxHORIZONTAL) | |
106 | btn = wxButton(self, 1201, "Print Setup") | |
107 | EVT_BUTTON(self, 1201, self.OnPrintSetup) | |
2f90df85 | 108 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
bb0054cd RD |
109 | |
110 | btn = wxButton(self, 1202, "Print Preview") | |
111 | EVT_BUTTON(self, 1202, self.OnPrintPreview) | |
2f90df85 | 112 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
bb0054cd RD |
113 | |
114 | btn = wxButton(self, 1203, "Print") | |
115 | EVT_BUTTON(self, 1203, self.OnDoPrint) | |
2f90df85 | 116 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
bb0054cd | 117 | |
2f90df85 | 118 | self.box.Add(subbox, 0, wxGROW) |
bb0054cd | 119 | |
1e4a197e | 120 | self.SetAutoLayout(True) |
2f90df85 | 121 | self.SetSizer(self.box) |
bb0054cd RD |
122 | |
123 | ||
bb0054cd RD |
124 | def OnPrintSetup(self, event): |
125 | printerDialog = wxPrintDialog(self) | |
126 | printerDialog.GetPrintDialogData().SetPrintData(self.printData) | |
1e4a197e | 127 | printerDialog.GetPrintDialogData().SetSetupDialog(True) |
bb0054cd RD |
128 | printerDialog.ShowModal(); |
129 | self.printData = printerDialog.GetPrintDialogData().GetPrintData() | |
130 | printerDialog.Destroy() | |
131 | ||
132 | ||
133 | def OnPrintPreview(self, event): | |
134 | self.log.WriteText("OnPrintPreview\n") | |
135 | printout = MyPrintout(self.canvas, self.log) | |
136 | printout2 = MyPrintout(self.canvas, self.log) | |
137 | self.preview = wxPrintPreview(printout, printout2, self.printData) | |
138 | if not self.preview.Ok(): | |
139 | self.log.WriteText("Houston, we have a problem...\n") | |
140 | return | |
141 | ||
142 | frame = wxPreviewFrame(self.preview, self.frame, "This is a print preview") | |
143 | ||
144 | frame.Initialize() | |
145 | frame.SetPosition(self.frame.GetPosition()) | |
146 | frame.SetSize(self.frame.GetSize()) | |
1e4a197e | 147 | frame.Show(True) |
bb0054cd RD |
148 | |
149 | ||
150 | ||
151 | def OnDoPrint(self, event): | |
152 | pdd = wxPrintDialogData() | |
153 | pdd.SetPrintData(self.printData) | |
154 | printer = wxPrinter(pdd) | |
155 | printout = MyPrintout(self.canvas, self.log) | |
156 | if not printer.Print(self.frame, printout): | |
157 | wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK) | |
158 | else: | |
159 | self.printData = printer.GetPrintDialogData().GetPrintData() | |
160 | printout.Destroy() | |
161 | ||
162 | ||
163 | #---------------------------------------------------------------------- | |
164 | ||
165 | def runTest(frame, nb, log): | |
166 | win = TestPrintPanel(nb, frame, log) | |
167 | return win | |
168 | ||
169 | ||
170 | #---------------------------------------------------------------------- | |
171 | ||
172 | ||
173 | ||
174 | ||
175 | ||
176 | overview = """\ | |
177 | """ | |
178 |