]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/demo/PrintFramework.py
Simplified EVT_PAINT handler
[wxWidgets.git] / wxPython / demo / PrintFramework.py
index faa33469d08e054f26ef269fb810d9f517b759e2..74124550867a76d31a84e26265c45bec5fea8037 100644 (file)
@@ -1,48 +1,49 @@
 
-from wxPython.wx         import *
-from wxScrolledWindow    import MyCanvas
+import  wx
+import  ScrolledWindow
 
 #----------------------------------------------------------------------
 
-class MyPrintout(wxPrintout):
+
+class MyPrintout(wx.Printout):
     def __init__(self, canvas, log):
-        wxPrintout.__init__(self)
+        wx.Printout.__init__(self)
         self.canvas = canvas
         self.log = log
 
     def OnBeginDocument(self, start, end):
-        self.log.WriteText("wxPrintout.OnBeginDocument\n")
-        return self.base_OnBeginDocument(start, end)
+        self.log.WriteText("MyPrintout.OnBeginDocument\n")
+        return super(MyPrintout, self).OnBeginDocument(start, end)
 
     def OnEndDocument(self):
-        self.log.WriteText("wxPrintout.OnEndDocument\n")
-        self.base_OnEndDocument()
+        self.log.WriteText("MyPrintout.OnEndDocument\n")
+        super(MyPrintout, self).OnEndDocument()
 
     def OnBeginPrinting(self):
-        self.log.WriteText("wxPrintout.OnBeginPrinting\n")
-        self.base_OnBeginPrinting()
+        self.log.WriteText("MyPrintout.OnBeginPrinting\n")
+        super(MyPrintout, self).OnBeginPrinting()
 
     def OnEndPrinting(self):
-        self.log.WriteText("wxPrintout.OnEndPrinting\n")
-        self.base_OnEndPrinting()
+        self.log.WriteText("MyPrintout.OnEndPrinting\n")
+        super(MyPrintout, self).OnEndPrinting()
 
     def OnPreparePrinting(self):
-        self.log.WriteText("wxPrintout.OnPreparePrinting\n")
-        self.base_OnPreparePrinting()
+        self.log.WriteText("MyPrintout.OnPreparePrinting\n")
+        super(MyPrintout, self).OnPreparePrinting()
 
     def HasPage(self, page):
-        self.log.WriteText("wxPrintout.HasPage: %d\n" % page)
+        self.log.WriteText("MyPrintout.HasPage: %d\n" % page)
         if page <= 2:
-            return true
+            return True
         else:
-            return false
+            return False
 
     def GetPageInfo(self):
-        self.log.WriteText("wxPrintout.GetPageInfo\n")
+        self.log.WriteText("MyPrintout.GetPageInfo\n")
         return (1, 2, 1, 2)
 
     def OnPrintPage(self, page):
-        self.log.WriteText("wxPrintout.OnPrintPage: %d\n" % page)
+        self.log.WriteText("MyPrintout.OnPrintPage: %d\n" % page)
         dc = self.GetDC()
 
         #-------------------------------------------
@@ -69,7 +70,7 @@ class MyPrintout(wxPrintout):
         # Use x or y scaling factor, whichever fits on the DC
         actualScale = min(scaleX, scaleY)
 
-        # Calculate the position on the DC for centring the graphic
+        # Calculate the position on the DC for centering the graphic
         posX = (w - (self.canvas.getWidth() * actualScale)) / 2.0
         posY = (h - (self.canvas.getHeight() * actualScale)) / 2.0
 
@@ -79,84 +80,91 @@ class MyPrintout(wxPrintout):
 
         #-------------------------------------------
 
-        self.canvas.DoDrawing(dc)
+        self.canvas.DoDrawing(dc, True)
         dc.DrawText("Page: %d" % page, marginX/2, maxY-marginY)
 
-        return true
+        return True
 
 
 #----------------------------------------------------------------------
 
 
-class TestPrintPanel(wxPanel):
+class TestPrintPanel(wx.Panel):
     def __init__(self, parent, frame, log):
-        wxPanel.__init__(self, parent, -1)
+        wx.Panel.__init__(self, parent, -1)
         self.log = log
         self.frame = frame
 
+        self.printData = wx.PrintData()
+        self.printData.SetPaperId(wx.PAPER_LETTER)
+        self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
+        
+        self.box = wx.BoxSizer(wx.VERTICAL)
+        self.canvas = ScrolledWindow.MyCanvas(self)
+        self.box.Add(self.canvas, 1, wx.GROW)
 
-        self.printData = wxPrintData()
-        self.printData.SetPaperId(wxPAPER_LETTER)
+        subbox = wx.BoxSizer(wx.HORIZONTAL)
+        btn = wx.Button(self, -1, "Page Setup")
+        self.Bind(wx.EVT_BUTTON, self.OnPageSetup, btn)
+        subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
 
-        self.box = wxBoxSizer(wxVERTICAL)
-        self.canvas = MyCanvas(self)
-        self.box.Add(self.canvas, 1, wxGROW)
+        btn = wx.Button(self, -1, "Print Preview")
+        self.Bind(wx.EVT_BUTTON, self.OnPrintPreview, btn)
+        subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
 
-        subbox = wxBoxSizer(wxHORIZONTAL)
-        btn = wxButton(self, 1201, "Print Setup")
-        EVT_BUTTON(self, 1201, self.OnPrintSetup)
-        subbox.Add(btn, 1, wxGROW | wxALL, 2)
+        btn = wx.Button(self, -1, "Print")
+        self.Bind(wx.EVT_BUTTON, self.OnDoPrint, btn)
+        subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
 
-        btn = wxButton(self, 1202, "Print Preview")
-        EVT_BUTTON(self, 1202, self.OnPrintPreview)
-        subbox.Add(btn, 1, wxGROW | wxALL, 2)
+        self.box.Add(subbox, 0, wx.GROW)
 
-        btn = wxButton(self, 1203, "Print")
-        EVT_BUTTON(self, 1203, self.OnDoPrint)
-        subbox.Add(btn, 1, wxGROW | wxALL, 2)
+        self.SetAutoLayout(True)
+        self.SetSizer(self.box)
 
-        self.box.Add(subbox, 0, wxGROW)
 
-        self.SetAutoLayout(true)
-        self.SetSizer(self.box)
 
+    def OnPageSetup(self, evt):
+        psdd = wx.PageSetupDialogData(self.printData)
+        psdd.CalculatePaperSizeFromId()
+        dlg = wx.PageSetupDialog(self, psdd)
+        dlg.ShowModal()
 
-    def OnPrintSetup(self, event):
-        printerDialog = wxPrintDialog(self)
-        printerDialog.GetPrintDialogData().SetPrintData(self.printData)
-        printerDialog.GetPrintDialogData().SetSetupDialog(true)
-        printerDialog.ShowModal();
-        self.printData = printerDialog.GetPrintDialogData().GetPrintData()
-        printerDialog.Destroy()
+        # this makes a copy of the wx.PrintData instead of just saving
+        # a reference to the one inside the PrintDialogData that will
+        # be destroyed when the dialog is destroyed
+        self.printData = wx.PrintData( dlg.GetPageSetupData().GetPrintData() )
 
+        dlg.Destroy()
 
     def OnPrintPreview(self, event):
-        self.log.WriteText("OnPrintPreview\n")
+        data = wx.PrintDialogData(self.printData)
         printout = MyPrintout(self.canvas, self.log)
         printout2 = MyPrintout(self.canvas, self.log)
-        self.preview = wxPrintPreview(printout, printout2, self.printData)
+        self.preview = wx.PrintPreview(printout, printout2, data)
+
         if not self.preview.Ok():
             self.log.WriteText("Houston, we have a problem...\n")
             return
 
-        frame = wxPreviewFrame(self.preview, self.frame, "This is a print preview")
+        pfrm = wx.PreviewFrame(self.preview, self.frame, "This is a print preview")
 
-        frame.Initialize()
-        frame.SetPosition(self.frame.GetPosition())
-        frame.SetSize(self.frame.GetSize())
-        frame.Show(true)
+        pfrm.Initialize()
+        pfrm.SetPosition(self.frame.GetPosition())
+        pfrm.SetSize(self.frame.GetSize())
+        pfrm.Show(True)
 
 
 
     def OnDoPrint(self, event):
-        pdd = wxPrintDialogData()
-        pdd.SetPrintData(self.printData)
-        printer = wxPrinter(pdd)
+        pdd = wx.PrintDialogData(self.printData)
+        pdd.SetToPage(2)
+        printer = wx.Printer(pdd)
         printout = MyPrintout(self.canvas, self.log)
-        if not printer.Print(self.frame, printout):
-            wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
+
+        if not printer.Print(self.frame, printout, True):
+            wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
         else:
-            self.printData = printer.GetPrintDialogData().GetPrintData()
+            self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
         printout.Destroy()
 
 
@@ -174,5 +182,50 @@ def runTest(frame, nb, log):
 
 
 overview = """\
+<html>
+<body>
+<h1>PrintFramework</h1>
+
+This is an overview of the classes and methods used to print documents.
+It also demonstrates how to do print previews and invoke the printer
+setup dialog.
+
+<p>Classes demonstrated here:<P>
+<ul>
+    <li><b>wx.Printout()</b> - This class encapsulates the functionality of printing out 
+        an application document. A new class must be derived and members overridden 
+        to respond to calls such as OnPrintPage and HasPage. Instances of this class 
+        are passed to wx.Printer.Print() or a wx.PrintPreview object to initiate 
+        printing or previewing.<P><p>
+        
+    <li><b>wx.PrintData()</b> - This class holds a variety of information related to 
+        printers and printer device contexts. This class is used to create a 
+        wx.PrinterDC and a wx.PostScriptDC. It is also used as a data member of 
+        wx.PrintDialogData and wx.PageSetupDialogData, as part of the mechanism for 
+        transferring data between the print dialogs and the application.<p><p>
+
+    <li><b>wx.PrintDialog()</b> - This class represents the print and print setup 
+        common dialogs. You may obtain a wx.PrinterDC device context from a 
+        successfully dismissed print dialog.<p><p>
+        
+    <li><b>wx.PrintPreview()</b> - Objects of this class manage the print preview 
+        process. The object is passed a wx.Printout object, and the wx.PrintPreview 
+        object itself is passed to a wx.PreviewFrame object. Previewing is started by 
+        initializing and showing the preview frame. Unlike wxPrinter.Print, flow of 
+        control returns to the application immediately after the frame is shown.<p><p>
+</ul>
+
+<p>Other classes are also demonstrated, but this is the gist of the printer interface
+framework in wxPython.
+
+</body>
+</html>
+
 """
 
+
+if __name__ == '__main__':
+    import sys,os
+    import run
+    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
+