]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/PrintFramework.py
I think I found out how GTK+ want popup windows
[wxWidgets.git] / wxPython / demo / PrintFramework.py
CommitLineData
bb0054cd 1
8fa876ca 2import wx
299647ac 3import ScrolledWindow
bb0054cd
RD
4
5#----------------------------------------------------------------------
6
8fa876ca
RD
7ID_Setup = wx.NewId()
8ID_Preview = wx.NewId()
9ID_Print = wx.NewId()
10
11class MyPrintout(wx.Printout):
bb0054cd 12 def __init__(self, canvas, log):
8fa876ca 13 wx.Printout.__init__(self)
bb0054cd
RD
14 self.canvas = canvas
15 self.log = log
16
17 def OnBeginDocument(self, start, end):
8fa876ca 18 self.log.WriteText("wx.Printout.OnBeginDocument\n")
bb0054cd
RD
19 return self.base_OnBeginDocument(start, end)
20
21 def OnEndDocument(self):
8fa876ca 22 self.log.WriteText("wx.Printout.OnEndDocument\n")
b1cf23dc 23 self.base_OnEndDocument()
bb0054cd
RD
24
25 def OnBeginPrinting(self):
8fa876ca 26 self.log.WriteText("wx.Printout.OnBeginPrinting\n")
b1cf23dc 27 self.base_OnBeginPrinting()
bb0054cd
RD
28
29 def OnEndPrinting(self):
8fa876ca 30 self.log.WriteText("wx.Printout.OnEndPrinting\n")
b1cf23dc 31 self.base_OnEndPrinting()
bb0054cd
RD
32
33 def OnPreparePrinting(self):
8fa876ca 34 self.log.WriteText("wx.Printout.OnPreparePrinting\n")
b1cf23dc 35 self.base_OnPreparePrinting()
bb0054cd
RD
36
37 def HasPage(self, page):
8fa876ca 38 self.log.WriteText("wx.Printout.HasPage: %d\n" % page)
0122b7e3 39 if page <= 2:
1e4a197e 40 return True
bb0054cd 41 else:
1e4a197e 42 return False
bb0054cd
RD
43
44 def GetPageInfo(self):
8fa876ca 45 self.log.WriteText("wx.Printout.GetPageInfo\n")
0122b7e3 46 return (1, 2, 1, 2)
bb0054cd
RD
47
48 def OnPrintPage(self, page):
8fa876ca 49 self.log.WriteText("wx.Printout.OnPrintPage: %d\n" % page)
bb0054cd
RD
50 dc = self.GetDC()
51
52 #-------------------------------------------
53 # One possible method of setting scaling factors...
54
55 maxX = self.canvas.getWidth()
56 maxY = self.canvas.getHeight()
57
58 # Let's have at least 50 device units margin
59 marginX = 50
60 marginY = 50
61
62 # Add the margin to the graphic size
63 maxX = maxX + (2 * marginX)
64 maxY = maxY + (2 * marginY)
65
66 # Get the size of the DC in pixels
67 (w, h) = dc.GetSizeTuple()
68
69 # Calculate a suitable scaling factor
70 scaleX = float(w) / maxX
71 scaleY = float(h) / maxY
72
73 # Use x or y scaling factor, whichever fits on the DC
74 actualScale = min(scaleX, scaleY)
75
8b9a4190 76 # Calculate the position on the DC for centering the graphic
bb0054cd
RD
77 posX = (w - (self.canvas.getWidth() * actualScale)) / 2.0
78 posY = (h - (self.canvas.getHeight() * actualScale)) / 2.0
79
80 # Set the scale and origin
81 dc.SetUserScale(actualScale, actualScale)
82 dc.SetDeviceOrigin(int(posX), int(posY))
83
84 #-------------------------------------------
85
1fded56b 86 self.canvas.DoDrawing(dc, True)
d7403ad2 87 dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
0122b7e3 88
1e4a197e 89 return True
bb0054cd
RD
90
91
92#----------------------------------------------------------------------
93
94
8fa876ca 95class TestPrintPanel(wx.Panel):
bb0054cd 96 def __init__(self, parent, frame, log):
8fa876ca 97 wx.Panel.__init__(self, parent, -1)
bb0054cd
RD
98 self.log = log
99 self.frame = frame
100
8fa876ca
RD
101 self.printData = wx.PrintData()
102 self.printData.SetPaperId(wx.PAPER_LETTER)
d88d4683
RD
103 self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
104
8fa876ca 105 self.box = wx.BoxSizer(wx.VERTICAL)
299647ac 106 self.canvas = ScrolledWindow.MyCanvas(self)
8fa876ca 107 self.box.Add(self.canvas, 1, wx.GROW)
bb0054cd 108
8fa876ca
RD
109 subbox = wx.BoxSizer(wx.HORIZONTAL)
110 btn = wx.Button(self, ID_Setup, "Print Setup")
d88d4683 111 self.Bind(wx.EVT_BUTTON, self.OnPrintSetup, btn)
8fa876ca 112 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
bb0054cd 113
8fa876ca 114 btn = wx.Button(self, ID_Preview, "Print Preview")
d88d4683 115 self.Bind(wx.EVT_BUTTON, self.OnPrintPreview, btn)
8fa876ca 116 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
bb0054cd 117
8fa876ca 118 btn = wx.Button(self, ID_Print, "Print")
d88d4683 119 self.Bind(wx.EVT_BUTTON, self.OnDoPrint, btn)
8fa876ca 120 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
bb0054cd 121
8fa876ca 122 self.box.Add(subbox, 0, wx.GROW)
bb0054cd 123
1e4a197e 124 self.SetAutoLayout(True)
2f90df85 125 self.SetSizer(self.box)
bb0054cd
RD
126
127
bb0054cd 128 def OnPrintSetup(self, event):
d88d4683
RD
129 data = wx.PrintDialogData(self.printData)
130 printerDialog = wx.PrintDialog(self, data)
1e4a197e 131 printerDialog.GetPrintDialogData().SetSetupDialog(True)
bb0054cd 132 printerDialog.ShowModal();
356f3c65
RD
133
134 # this makes a copy of the wx.PrintData instead of just saving
54563c57 135 # a reference to the one inside the PrintDialogData that will
6b9f434e 136 # be destroyed when the dialog is destroyed
356f3c65
RD
137 self.printData = wx.PrintData( printerDialog.GetPrintDialogData().GetPrintData() )
138
bb0054cd
RD
139 printerDialog.Destroy()
140
141
142 def OnPrintPreview(self, event):
143 self.log.WriteText("OnPrintPreview\n")
d88d4683 144 data = wx.PrintDialogData(self.printData)
bb0054cd
RD
145 printout = MyPrintout(self.canvas, self.log)
146 printout2 = MyPrintout(self.canvas, self.log)
d88d4683 147 self.preview = wx.PrintPreview(printout, printout2, data)
8fa876ca 148
bb0054cd
RD
149 if not self.preview.Ok():
150 self.log.WriteText("Houston, we have a problem...\n")
151 return
152
8fa876ca 153 frame = wx.PreviewFrame(self.preview, self.frame, "This is a print preview")
bb0054cd
RD
154
155 frame.Initialize()
156 frame.SetPosition(self.frame.GetPosition())
157 frame.SetSize(self.frame.GetSize())
1e4a197e 158 frame.Show(True)
bb0054cd
RD
159
160
161
162 def OnDoPrint(self, event):
d88d4683
RD
163 pdd = wx.PrintDialogData(self.printData)
164 pdd.SetToPage(2)
8fa876ca 165 printer = wx.Printer(pdd)
bb0054cd 166 printout = MyPrintout(self.canvas, self.log)
8fa876ca 167
d88d4683 168 if not printer.Print(self.frame, printout, True):
8fa876ca 169 wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
bb0054cd 170 else:
356f3c65 171 self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
bb0054cd
RD
172 printout.Destroy()
173
174
175#----------------------------------------------------------------------
176
177def runTest(frame, nb, log):
178 win = TestPrintPanel(nb, frame, log)
179 return win
180
181
182#----------------------------------------------------------------------
183
184
185
186
187
188overview = """\
8fa876ca
RD
189<html>
190<body>
191<h1>PrintFramework</h1>
192
193This is an overview of the classes and methods used to print documents.
194It also demonstrates how to do print previews and invoke the printer
195setup dialog.
196
197<p>Classes demonstrated here:<P>
198<ul>
199 <li><b>wx.Printout()</b> - This class encapsulates the functionality of printing out
200 an application document. A new class must be derived and members overridden
201 to respond to calls such as OnPrintPage and HasPage. Instances of this class
202 are passed to wx.Printer.Print() or a wx.PrintPreview object to initiate
203 printing or previewing.<P><p>
204
205 <li><b>wx.PrintData()</b> - This class holds a variety of information related to
206 printers and printer device contexts. This class is used to create a
207 wx.PrinterDC and a wx.PostScriptDC. It is also used as a data member of
208 wx.PrintDialogData and wx.PageSetupDialogData, as part of the mechanism for
209 transferring data between the print dialogs and the application.<p><p>
210
211 <li><b>wx.PrintDialog()</b> - This class represents the print and print setup
212 common dialogs. You may obtain a wx.PrinterDC device context from a
213 successfully dismissed print dialog.<p><p>
214
215 <li><b>wx.PrintPreview()</b> - Objects of this class manage the print preview
216 process. The object is passed a wx.Printout object, and the wx.PrintPreview
217 object itself is passed to a wx.PreviewFrame object. Previewing is started by
218 initializing and showing the preview frame. Unlike wxPrinter.Print, flow of
219 control returns to the application immediately after the frame is shown.<p><p>
220</ul>
221
222<p>Other classes are also demonstrated, but this is the gist of the printer interface
223framework in wxPython.
224
225</body>
226</html>
1fded56b 227
8fa876ca 228"""
1fded56b
RD
229
230
231if __name__ == '__main__':
232 import sys,os
233 import run
8eca4fef 234 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1fded56b 235