]>
Commit | Line | Data |
---|---|---|
bb0054cd RD |
1 | |
2 | from wxPython.wx import * | |
3 | from wxPython.lib.sizers import * | |
4 | from wxScrolledWindow import MyCanvas | |
5 | ||
6 | #---------------------------------------------------------------------- | |
7 | ||
8 | class MyPrintout(wxPrintout): | |
9 | def __init__(self, canvas, log): | |
10 | wxPrintout.__init__(self) | |
11 | self.canvas = canvas | |
12 | self.log = log | |
13 | ||
14 | def OnBeginDocument(self, start, end): | |
15 | self.log.WriteText("wxPrintout.OnBeginDocument\n") | |
16 | return self.base_OnBeginDocument(start, end) | |
17 | ||
18 | def OnEndDocument(self): | |
19 | self.log.WriteText("wxPrintout.OnEndDocument\n") | |
b1cf23dc | 20 | self.base_OnEndDocument() |
bb0054cd RD |
21 | |
22 | def OnBeginPrinting(self): | |
23 | self.log.WriteText("wxPrintout.OnBeginPrinting\n") | |
b1cf23dc | 24 | self.base_OnBeginPrinting() |
bb0054cd RD |
25 | |
26 | def OnEndPrinting(self): | |
27 | self.log.WriteText("wxPrintout.OnEndPrinting\n") | |
b1cf23dc | 28 | self.base_OnEndPrinting() |
bb0054cd RD |
29 | |
30 | def OnPreparePrinting(self): | |
31 | self.log.WriteText("wxPrintout.OnPreparePrinting\n") | |
b1cf23dc | 32 | self.base_OnPreparePrinting() |
bb0054cd RD |
33 | |
34 | def HasPage(self, page): | |
35 | self.log.WriteText("wxPrintout.HasPage\n") | |
36 | if page == 1: | |
37 | return true | |
38 | else: | |
39 | return false | |
40 | ||
41 | def GetPageInfo(self): | |
42 | self.log.WriteText("wxPrintout.GetPageInfo\n") | |
43 | return (1, 1, 1, 1) | |
44 | ||
45 | def OnPrintPage(self, page): | |
46 | self.log.WriteText("wxPrintout.OnPrintPage\n") | |
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 centring 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) | |
84 | return true | |
85 | ||
86 | ||
87 | #---------------------------------------------------------------------- | |
88 | ||
89 | ||
90 | class TestPrintPanel(wxPanel): | |
91 | def __init__(self, parent, frame, log): | |
92 | wxPanel.__init__(self, parent, -1) | |
93 | self.log = log | |
94 | self.frame = frame | |
95 | ||
96 | ||
97 | self.printData = wxPrintData() | |
98 | self.printData.SetPaperId(wxPAPER_LETTER) | |
99 | ||
100 | self.box = box.wxBoxSizer(wxVERTICAL) | |
101 | self.canvas = MyCanvas(self) | |
102 | self.box.Add(self.canvas, 1) | |
103 | ||
104 | subbox = wxBoxSizer(wxHORIZONTAL) | |
105 | btn = wxButton(self, 1201, "Print Setup") | |
106 | EVT_BUTTON(self, 1201, self.OnPrintSetup) | |
107 | subbox.Add(btn, 1) | |
108 | ||
109 | btn = wxButton(self, 1202, "Print Preview") | |
110 | EVT_BUTTON(self, 1202, self.OnPrintPreview) | |
111 | subbox.Add(btn, 1) | |
112 | ||
113 | btn = wxButton(self, 1203, "Print") | |
114 | EVT_BUTTON(self, 1203, self.OnDoPrint) | |
115 | subbox.Add(btn, 1) | |
116 | ||
117 | self.box.Add(subbox) | |
118 | ||
119 | ||
120 | ||
121 | def OnSize(self, event): | |
122 | size = self.GetClientSize() | |
123 | self.box.Layout(size) | |
124 | ||
125 | def OnPrintSetup(self, event): | |
126 | printerDialog = wxPrintDialog(self) | |
127 | printerDialog.GetPrintDialogData().SetPrintData(self.printData) | |
128 | printerDialog.GetPrintDialogData().SetSetupDialog(true) | |
129 | printerDialog.ShowModal(); | |
130 | self.printData = printerDialog.GetPrintDialogData().GetPrintData() | |
131 | printerDialog.Destroy() | |
132 | ||
133 | ||
134 | def OnPrintPreview(self, event): | |
135 | self.log.WriteText("OnPrintPreview\n") | |
136 | printout = MyPrintout(self.canvas, self.log) | |
137 | printout2 = MyPrintout(self.canvas, self.log) | |
138 | self.preview = wxPrintPreview(printout, printout2, self.printData) | |
139 | if not self.preview.Ok(): | |
140 | self.log.WriteText("Houston, we have a problem...\n") | |
141 | return | |
142 | ||
143 | frame = wxPreviewFrame(self.preview, self.frame, "This is a print preview") | |
144 | ||
145 | frame.Initialize() | |
146 | frame.SetPosition(self.frame.GetPosition()) | |
147 | frame.SetSize(self.frame.GetSize()) | |
148 | frame.Show(true) | |
149 | ||
150 | ||
151 | ||
152 | def OnDoPrint(self, event): | |
153 | pdd = wxPrintDialogData() | |
154 | pdd.SetPrintData(self.printData) | |
155 | printer = wxPrinter(pdd) | |
156 | printout = MyPrintout(self.canvas, self.log) | |
157 | if not printer.Print(self.frame, printout): | |
158 | wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK) | |
159 | else: | |
160 | self.printData = printer.GetPrintDialogData().GetPrintData() | |
161 | printout.Destroy() | |
162 | ||
163 | ||
164 | #---------------------------------------------------------------------- | |
165 | ||
166 | def runTest(frame, nb, log): | |
167 | win = TestPrintPanel(nb, frame, log) | |
168 | return win | |
169 | ||
170 | ||
171 | #---------------------------------------------------------------------- | |
172 | ||
173 | ||
174 | ||
175 | ||
176 | ||
177 | overview = """\ | |
178 | """ | |
179 |