]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PrintFramework.py
1 # 11/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
4 # o Got rid of static buton IDs
5 # o Took at a stab at a lucid overview string.
11 #----------------------------------------------------------------------
14 ID_Preview
= wx
.NewId()
17 class MyPrintout(wx
.Printout
):
18 def __init__(self
, canvas
, log
):
19 wx
.Printout
.__init
__(self
)
23 def OnBeginDocument(self
, start
, end
):
24 self
.log
.WriteText("wx.Printout.OnBeginDocument\n")
25 return self
.base_OnBeginDocument(start
, end
)
27 def OnEndDocument(self
):
28 self
.log
.WriteText("wx.Printout.OnEndDocument\n")
29 self
.base_OnEndDocument()
31 def OnBeginPrinting(self
):
32 self
.log
.WriteText("wx.Printout.OnBeginPrinting\n")
33 self
.base_OnBeginPrinting()
35 def OnEndPrinting(self
):
36 self
.log
.WriteText("wx.Printout.OnEndPrinting\n")
37 self
.base_OnEndPrinting()
39 def OnPreparePrinting(self
):
40 self
.log
.WriteText("wx.Printout.OnPreparePrinting\n")
41 self
.base_OnPreparePrinting()
43 def HasPage(self
, page
):
44 self
.log
.WriteText("wx.Printout.HasPage: %d\n" % page
)
50 def GetPageInfo(self
):
51 self
.log
.WriteText("wx.Printout.GetPageInfo\n")
54 def OnPrintPage(self
, page
):
55 self
.log
.WriteText("wx.Printout.OnPrintPage: %d\n" % page
)
58 #-------------------------------------------
59 # One possible method of setting scaling factors...
61 maxX
= self
.canvas
.getWidth()
62 maxY
= self
.canvas
.getHeight()
64 # Let's have at least 50 device units margin
68 # Add the margin to the graphic size
69 maxX
= maxX
+ (2 * marginX
)
70 maxY
= maxY
+ (2 * marginY
)
72 # Get the size of the DC in pixels
73 (w
, h
) = dc
.GetSizeTuple()
75 # Calculate a suitable scaling factor
76 scaleX
= float(w
) / maxX
77 scaleY
= float(h
) / maxY
79 # Use x or y scaling factor, whichever fits on the DC
80 actualScale
= min(scaleX
, scaleY
)
82 # Calculate the position on the DC for centering the graphic
83 posX
= (w
- (self
.canvas
.getWidth() * actualScale
)) / 2.0
84 posY
= (h
- (self
.canvas
.getHeight() * actualScale
)) / 2.0
86 # Set the scale and origin
87 dc
.SetUserScale(actualScale
, actualScale
)
88 dc
.SetDeviceOrigin(int(posX
), int(posY
))
90 #-------------------------------------------
92 self
.canvas
.DoDrawing(dc
, True)
93 dc
.DrawText("Page: %d" % page
, (marginX
/2, maxY
-marginY
))
98 #----------------------------------------------------------------------
101 class TestPrintPanel(wx
.Panel
):
102 def __init__(self
, parent
, frame
, log
):
103 wx
.Panel
.__init
__(self
, parent
, -1)
107 self
.printData
= wx
.PrintData()
108 self
.printData
.SetPaperId(wx
.PAPER_LETTER
)
110 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
111 self
.canvas
= ScrolledWindow
.MyCanvas(self
)
112 self
.box
.Add(self
.canvas
, 1, wx
.GROW
)
114 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
115 btn
= wx
.Button(self
, ID_Setup
, "Print Setup")
116 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrintSetup
, id=ID_Setup
)
117 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
119 btn
= wx
.Button(self
, ID_Preview
, "Print Preview")
120 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrintPreview
, id=ID_Preview
)
121 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
123 btn
= wx
.Button(self
, ID_Print
, "Print")
124 self
.Bind(wx
.EVT_BUTTON
, self
.OnDoPrint
, id=ID_Print
)
125 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
127 self
.box
.Add(subbox
, 0, wx
.GROW
)
129 self
.SetAutoLayout(True)
130 self
.SetSizer(self
.box
)
133 def OnPrintSetup(self
, event
):
134 printerDialog
= wx
.PrintDialog(self
)
135 printerDialog
.GetPrintDialogData().SetPrintData(self
.printData
)
136 printerDialog
.GetPrintDialogData().SetSetupDialog(True)
137 printerDialog
.ShowModal();
138 self
.printData
= printerDialog
.GetPrintDialogData().GetPrintData()
139 printerDialog
.Destroy()
142 def OnPrintPreview(self
, event
):
143 self
.log
.WriteText("OnPrintPreview\n")
144 printout
= MyPrintout(self
.canvas
, self
.log
)
145 printout2
= MyPrintout(self
.canvas
, self
.log
)
146 self
.preview
= wx
.PrintPreview(printout
, printout2
, self
.printData
)
148 if not self
.preview
.Ok():
149 self
.log
.WriteText("Houston, we have a problem...\n")
152 frame
= wx
.PreviewFrame(self
.preview
, self
.frame
, "This is a print preview")
155 frame
.SetPosition(self
.frame
.GetPosition())
156 frame
.SetSize(self
.frame
.GetSize())
161 def OnDoPrint(self
, event
):
162 pdd
= wx
.PrintDialogData()
163 pdd
.SetPrintData(self
.printData
)
164 printer
= wx
.Printer(pdd
)
165 printout
= MyPrintout(self
.canvas
, self
.log
)
167 if not printer
.Print(self
.frame
, printout
):
168 wx
.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx
.OK
)
170 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
174 #----------------------------------------------------------------------
176 def runTest(frame
, nb
, log
):
177 win
= TestPrintPanel(nb
, frame
, log
)
181 #----------------------------------------------------------------------
190 <h1>PrintFramework</h1>
192 This is an overview of the classes and methods used to print documents.
193 It also demonstrates how to do print previews and invoke the printer
196 <p>Classes demonstrated here:<P>
198 <li><b>wx.Printout()</b> - This class encapsulates the functionality of printing out
199 an application document. A new class must be derived and members overridden
200 to respond to calls such as OnPrintPage and HasPage. Instances of this class
201 are passed to wx.Printer.Print() or a wx.PrintPreview object to initiate
202 printing or previewing.<P><p>
204 <li><b>wx.PrintData()</b> - This class holds a variety of information related to
205 printers and printer device contexts. This class is used to create a
206 wx.PrinterDC and a wx.PostScriptDC. It is also used as a data member of
207 wx.PrintDialogData and wx.PageSetupDialogData, as part of the mechanism for
208 transferring data between the print dialogs and the application.<p><p>
210 <li><b>wx.PrintDialog()</b> - This class represents the print and print setup
211 common dialogs. You may obtain a wx.PrinterDC device context from a
212 successfully dismissed print dialog.<p><p>
214 <li><b>wx.PrintPreview()</b> - Objects of this class manage the print preview
215 process. The object is passed a wx.Printout object, and the wx.PrintPreview
216 object itself is passed to a wx.PreviewFrame object. Previewing is started by
217 initializing and showing the preview frame. Unlike wxPrinter.Print, flow of
218 control returns to the application immediately after the frame is shown.<p><p>
221 <p>Other classes are also demonstrated, but this is the gist of the printer interface
222 framework in wxPython.
230 if __name__
== '__main__':
233 run
.main(['', os
.path
.basename(sys
.argv
[0])])