X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6bcfd974ef26faf6f91a62cac09827e09463fd1..52f52ebc4e0be6a9899d328b08db9eb14629d219:/wxPython/demo/PrintFramework.py?ds=inline diff --git a/wxPython/demo/PrintFramework.py b/wxPython/demo/PrintFramework.py index 6b77eb1f2b..ea3881e68f 100644 --- a/wxPython/demo/PrintFramework.py +++ b/wxPython/demo/PrintFramework.py @@ -1,48 +1,52 @@ -from wxPython.wx import * -from wxScrolledWindow import MyCanvas +import wx +import ScrolledWindow #---------------------------------------------------------------------- -class MyPrintout(wxPrintout): +ID_Setup = wx.NewId() +ID_Preview = wx.NewId() +ID_Print = wx.NewId() + +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") + self.log.WriteText("wx.Printout.OnBeginDocument\n") return self.base_OnBeginDocument(start, end) def OnEndDocument(self): - self.log.WriteText("wxPrintout.OnEndDocument\n") + self.log.WriteText("wx.Printout.OnEndDocument\n") self.base_OnEndDocument() def OnBeginPrinting(self): - self.log.WriteText("wxPrintout.OnBeginPrinting\n") + self.log.WriteText("wx.Printout.OnBeginPrinting\n") self.base_OnBeginPrinting() def OnEndPrinting(self): - self.log.WriteText("wxPrintout.OnEndPrinting\n") + self.log.WriteText("wx.Printout.OnEndPrinting\n") self.base_OnEndPrinting() def OnPreparePrinting(self): - self.log.WriteText("wxPrintout.OnPreparePrinting\n") + self.log.WriteText("wx.Printout.OnPreparePrinting\n") self.base_OnPreparePrinting() def HasPage(self, page): - self.log.WriteText("wxPrintout.HasPage\n") - if page == 1: - return true + self.log.WriteText("wx.Printout.HasPage: %d\n" % page) + if page <= 2: + return True else: - return false + return False def GetPageInfo(self): - self.log.WriteText("wxPrintout.GetPageInfo\n") - return (1, 1, 1, 1) + self.log.WriteText("wx.Printout.GetPageInfo\n") + return (1, 2, 1, 2) def OnPrintPage(self, page): - self.log.WriteText("wxPrintout.OnPrintPage\n") + self.log.WriteText("wx.Printout.OnPrintPage: %d\n" % page) dc = self.GetDC() #------------------------------------------- @@ -69,7 +73,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,50 +83,51 @@ class MyPrintout(wxPrintout): #------------------------------------------- - self.canvas.DoDrawing(dc) - return true + self.canvas.DoDrawing(dc, True) + dc.DrawText("Page: %d" % page, (marginX/2, maxY-marginY)) + + 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 = wxPrintData() - self.printData.SetPaperId(wxPAPER_LETTER) + self.box = wx.BoxSizer(wx.VERTICAL) + self.canvas = ScrolledWindow.MyCanvas(self) + self.box.Add(self.canvas, 1, wx.GROW) - self.box = wxBoxSizer(wxVERTICAL) - self.canvas = MyCanvas(self) - self.box.Add(self.canvas, 1, wxGROW) + subbox = wx.BoxSizer(wx.HORIZONTAL) + btn = wx.Button(self, ID_Setup, "Print Setup") + self.Bind(wx.EVT_BUTTON, self.OnPrintSetup, id=ID_Setup) + 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, ID_Preview, "Print Preview") + self.Bind(wx.EVT_BUTTON, self.OnPrintPreview, id=ID_Preview) + 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) + btn = wx.Button(self, ID_Print, "Print") + self.Bind(wx.EVT_BUTTON, self.OnDoPrint, id=ID_Print) + subbox.Add(btn, 1, wx.GROW | wx.ALL, 2) - btn = wxButton(self, 1203, "Print") - EVT_BUTTON(self, 1203, self.OnDoPrint) - subbox.Add(btn, 1, wxGROW | wxALL, 2) + self.box.Add(subbox, 0, wx.GROW) - self.box.Add(subbox, 0, wxGROW) - - self.SetAutoLayout(true) + self.SetAutoLayout(True) self.SetSizer(self.box) def OnPrintSetup(self, event): - printerDialog = wxPrintDialog(self) + printerDialog = wx.PrintDialog(self) printerDialog.GetPrintDialogData().SetPrintData(self.printData) - printerDialog.GetPrintDialogData().SetSetupDialog(true) + printerDialog.GetPrintDialogData().SetSetupDialog(True) printerDialog.ShowModal(); self.printData = printerDialog.GetPrintDialogData().GetPrintData() printerDialog.Destroy() @@ -132,27 +137,29 @@ class TestPrintPanel(wxPanel): self.log.WriteText("OnPrintPreview\n") 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, self.printData) + 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") + frame = 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) + frame.Show(True) def OnDoPrint(self, event): - pdd = wxPrintDialogData() + pdd = wx.PrintDialogData() pdd.SetPrintData(self.printData) - printer = wxPrinter(pdd) + 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) + wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK) else: self.printData = printer.GetPrintDialogData().GetPrintData() printout.Destroy() @@ -172,5 +179,50 @@ def runTest(frame, nb, log): overview = """\ + + +

PrintFramework

+ +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. + +

Classes demonstrated here:

+

+ +

Other classes are also demonstrated, but this is the gist of the printer interface +framework in wxPython. + + + + """ + +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])]) +