]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/PrintFramework.py
it is now possible to add custom buttons into wxHtmlHelpFrame's toolbar
[wxWidgets.git] / utils / 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\n")
35 if page == 1:
36 return true
37 else:
38 return false
39
40 def GetPageInfo(self):
41 self.log.WriteText("wxPrintout.GetPageInfo\n")
42 return (1, 1, 1, 1)
43
44 def OnPrintPage(self, page):
45 self.log.WriteText("wxPrintout.OnPrintPage\n")
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 return true
84
85
86 #----------------------------------------------------------------------
87
88
89 class TestPrintPanel(wxPanel):
90 def __init__(self, parent, frame, log):
91 wxPanel.__init__(self, parent, -1)
92 self.log = log
93 self.frame = frame
94
95
96 self.printData = wxPrintData()
97 self.printData.SetPaperId(wxPAPER_LETTER)
98
99 self.box = wxBoxSizer(wxVERTICAL)
100 self.canvas = MyCanvas(self)
101 self.box.Add(self.canvas, 1, wxGROW)
102
103 subbox = wxBoxSizer(wxHORIZONTAL)
104 btn = wxButton(self, 1201, "Print Setup")
105 EVT_BUTTON(self, 1201, self.OnPrintSetup)
106 subbox.Add(btn, 1, wxGROW | wxALL, 2)
107
108 btn = wxButton(self, 1202, "Print Preview")
109 EVT_BUTTON(self, 1202, self.OnPrintPreview)
110 subbox.Add(btn, 1, wxGROW | wxALL, 2)
111
112 btn = wxButton(self, 1203, "Print")
113 EVT_BUTTON(self, 1203, self.OnDoPrint)
114 subbox.Add(btn, 1, wxGROW | wxALL, 2)
115
116 self.box.Add(subbox, 0, wxGROW)
117
118 self.SetAutoLayout(true)
119 self.SetSizer(self.box)
120
121
122 def OnPrintSetup(self, event):
123 printerDialog = wxPrintDialog(self)
124 printerDialog.GetPrintDialogData().SetPrintData(self.printData)
125 printerDialog.GetPrintDialogData().SetSetupDialog(true)
126 printerDialog.ShowModal();
127 self.printData = printerDialog.GetPrintDialogData().GetPrintData()
128 printerDialog.Destroy()
129
130
131 def OnPrintPreview(self, event):
132 self.log.WriteText("OnPrintPreview\n")
133 printout = MyPrintout(self.canvas, self.log)
134 printout2 = MyPrintout(self.canvas, self.log)
135 self.preview = wxPrintPreview(printout, printout2, self.printData)
136 if not self.preview.Ok():
137 self.log.WriteText("Houston, we have a problem...\n")
138 return
139
140 frame = wxPreviewFrame(self.preview, self.frame, "This is a print preview")
141
142 frame.Initialize()
143 frame.SetPosition(self.frame.GetPosition())
144 frame.SetSize(self.frame.GetSize())
145 frame.Show(true)
146
147
148
149 def OnDoPrint(self, event):
150 pdd = wxPrintDialogData()
151 pdd.SetPrintData(self.printData)
152 printer = wxPrinter(pdd)
153 printout = MyPrintout(self.canvas, self.log)
154 if not printer.Print(self.frame, printout):
155 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
156 else:
157 self.printData = printer.GetPrintDialogData().GetPrintData()
158 printout.Destroy()
159
160
161 #----------------------------------------------------------------------
162
163 def runTest(frame, nb, log):
164 win = TestPrintPanel(nb, frame, log)
165 return win
166
167
168 #----------------------------------------------------------------------
169
170
171
172
173
174 overview = """\
175 """
176