]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PrintFramework.py
1. don't emulate OFN_OVERWRITEPROMPT, use the real thing
[wxWidgets.git] / wxPython / demo / PrintFramework.py
1
2 from wxPython.wx import *
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")
19 self.base_OnEndDocument()
20
21 def OnBeginPrinting(self):
22 self.log.WriteText("wxPrintout.OnBeginPrinting\n")
23 self.base_OnBeginPrinting()
24
25 def OnEndPrinting(self):
26 self.log.WriteText("wxPrintout.OnEndPrinting\n")
27 self.base_OnEndPrinting()
28
29 def OnPreparePrinting(self):
30 self.log.WriteText("wxPrintout.OnPreparePrinting\n")
31 self.base_OnPreparePrinting()
32
33 def HasPage(self, page):
34 self.log.WriteText("wxPrintout.HasPage: %d\n" % page)
35 if page <= 2:
36 return true
37 else:
38 return false
39
40 def GetPageInfo(self):
41 self.log.WriteText("wxPrintout.GetPageInfo\n")
42 return (1, 2, 1, 2)
43
44 def OnPrintPage(self, page):
45 self.log.WriteText("wxPrintout.OnPrintPage: %d\n" % page)
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)
83 dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
84
85 return true
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
101 self.box = wxBoxSizer(wxVERTICAL)
102 self.canvas = MyCanvas(self)
103 self.box.Add(self.canvas, 1, wxGROW)
104
105 subbox = wxBoxSizer(wxHORIZONTAL)
106 btn = wxButton(self, 1201, "Print Setup")
107 EVT_BUTTON(self, 1201, self.OnPrintSetup)
108 subbox.Add(btn, 1, wxGROW | wxALL, 2)
109
110 btn = wxButton(self, 1202, "Print Preview")
111 EVT_BUTTON(self, 1202, self.OnPrintPreview)
112 subbox.Add(btn, 1, wxGROW | wxALL, 2)
113
114 btn = wxButton(self, 1203, "Print")
115 EVT_BUTTON(self, 1203, self.OnDoPrint)
116 subbox.Add(btn, 1, wxGROW | wxALL, 2)
117
118 self.box.Add(subbox, 0, wxGROW)
119
120 self.SetAutoLayout(true)
121 self.SetSizer(self.box)
122
123
124 def OnPrintSetup(self, event):
125 printerDialog = wxPrintDialog(self)
126 printerDialog.GetPrintDialogData().SetPrintData(self.printData)
127 printerDialog.GetPrintDialogData().SetSetupDialog(true)
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())
147 frame.Show(true)
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