Advertisement
Guest User

Untitled

a guest
Feb 9th, 2011
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.75 KB | None | 0 0
  1. import wx
  2. import os
  3.  
  4. FONTSIZE = 10
  5.  
  6. class TextDocPrintout(wx.Printout):
  7.     def __init__(self, text, title, margins):
  8.         wx.Printout.__init__(self, title)
  9.         self.lines = text.split('\n')
  10.         self.margins = margins
  11.  
  12.     def HasPage(self, page):
  13.         return page <= self.numPages
  14.  
  15.     def GetPageInfo(self):
  16.         return (1, self.numPages, 1, self.numPages)
  17.  
  18.     def CalculateScale(self, dc):
  19.         ppiPrinterX, ppiPrinterY = self.GetPPIPrinter()
  20.         ppiScreenX, ppiScreenY = self.GetPPIScreen()
  21.         logScale = float(ppiPrinterX)/float(ppiScreenX)
  22.  
  23.         pw, ph = self.GetPageSizePixels()
  24.         dw, dh = dc.GetSize()
  25.         scale = logScale * float(dw)/float(pw)
  26.         dc.SetUserScale(scale, scale)
  27.         self.logUnitsMM = float(ppiPrinterX)/(logScale*25.4)
  28.  
  29.     def CalculateLayout(self, dc):
  30.         topLeft, bottomRight = self.margins
  31.         dw, dh = dc.GetSize()
  32.         self.x1 = topLeft.x * self.logUnitsMM
  33.         self.y1 = topLeft.y * self.logUnitsMM
  34.         self.x2 = (dc.DeviceToLogicalXRel(dw) -
  35.                    bottomRight.x * self.logUnitsMM)
  36.         self.y2 = (dc.DeviceToLogicalYRel(dh) -
  37.                    bottomRight.y * self.logUnitsMM)
  38.         self.pageHeight = self.y2 - self.y1 - 2*self.logUnitsMM
  39.         font = wx.Font(FONTSIZE, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
  40.         dc.SetFont(font)
  41.         self.lineHeight = dc.GetCharHeight()
  42.         self.linesPerPage = int(self.pageHeight/self.lineHeight)
  43.        
  44.     def OnPreparePrinting(self):
  45.         dc = self.GetDC()
  46.         self.CalculateScale(dc)
  47.         self.CalculateLayout(dc)
  48.         self.numPages = len(self.lines) / self.linesPerPage
  49.         if len(self.lines) % self.linesPerPage != 0:
  50.             self.numPages += 1
  51.  
  52.     def OnPrintPage(self, page):
  53.         dc = self.GetDC()
  54.         self.CalculateScale(dc)
  55.         self.CalculateLayout(dc)
  56.         dc.SetPen(wx.Pen("black", 0))
  57.         dc.SetBrush(wx.TRANSPARENT_BRUSH)
  58.         r = wx.RectPP((self.x1, self.y1),
  59.                       (self.x2, self.y2))
  60.         dc.DrawRectangleRect(r)
  61.         dc.SetClippingRect(r)
  62.        
  63.         line = (page-1) * self.linesPerPage
  64.         x = self.x1 + self.logUnitsMM
  65.         y = self.y1 + self.logUnitsMM
  66.         while line < (page * self.linesPerPage):
  67.             dc.DrawText(self.lines[line], x, y)
  68.             y += self.lineHeight
  69.             line += 1
  70.             if line >= len(self.lines):
  71.                 break
  72.         return True
  73.    
  74. class PrintFrameworkSample(wx.Frame):
  75.     def __init__(self):
  76.         wx.Frame.__init__(self, None, size=(640, 480),
  77.                           title="Print Framework Sample")
  78.         self.CreateStatusBar()
  79.         self.tc = wx.TextCtrl(self, -1, "",
  80.                               style=wx.TE_MULTILINE|wx.TE_DONTWRAP)
  81.         self.tc.SetFont(wx.Font(FONTSIZE, wx.TELETYPE, wx.NORMAL, wx.NORMAL))
  82.         filename = os.path.join(os.path.dirname(__file__), "temp.txt")
  83.         self.tc.SetValue(open(filename).read())
  84.         self.tc.Bind(wx.EVT_SET_FOCUS, self.OnClearSelection)
  85.         wx.CallAfter(self.tc.SetInsertionPoint, 0)
  86.         menu = wx.Menu()
  87.         item = menu.Append(-1, "Page Setup...\tF5",
  88.                            "Set up page margins and etc.")
  89.         self.Bind(wx.EVT_MENU, self.OnPageSetup, item)
  90.         item = menu.Append(-1, "Print Setup...\tF6",
  91.                            "Set up the printer options, etc.")
  92.         self.Bind(wx.EVT_MENU, self.OnPrintSetup, item)
  93.         item = menu.Append(-1, "Print Preview...\tF7",
  94.                            "View the printout on-screen")
  95.         self.Bind(wx.EVT_MENU, self.OnPrintPreview, item)
  96.         item = menu.Append(-1, "Print...\tF8", "Print the document")
  97.         self.Bind(wx.EVT_MENU, self.OnPrint, item)
  98.         menu.AppendSeparator()
  99.         item = menu.Append(-1, "E&xit", "Close this application")
  100.         self.Bind(wx.EVT_MENU, self.OnExit, item)
  101.         menubar = wx.MenuBar()
  102.         menubar.Append(menu, "&File")
  103.         self.SetMenuBar(menubar)
  104.         self.pdata = wx.PrintData()
  105.         self.pdata.SetPaperId(wx.PAPER_LETTER)
  106.         self.pdata.SetOrientation(wx.PORTRAIT)
  107.         self.margins = (wx.Point(15,15), wx.Point(15,15))
  108.  
  109.     def OnExit(self, evt):
  110.         self.Close()
  111.  
  112.     def OnClearSelection(self, evt):
  113.         evt.Skip()
  114.         wx.CallAfter(self.tc.SetInsertionPoint,
  115.                      self.tc.GetInsertionPoint())
  116.  
  117.     def OnPageSetup(self, evt):
  118.         data = wx.PageSetupDialogData()
  119.         data.SetPrintData(self.pdata)
  120.         data.SetDefaultMinMargins(True)
  121.         data.SetMarginTopLeft(self.margins[0])
  122.         data.SetMarginBottomRight(self.margins[1])
  123.         dlg = wx.PageSetupDialog(self, data)
  124.         if dlg.ShowModal() == wx.ID_OK:
  125.             data = dlg.GetPageSetupData()
  126.             self.pdata = wx.PrintData(data.GetPrintData())
  127.             self.pdata.SetPaperId(data.GetPaperId())
  128.             self.margins = (data.GetMarginTopLeft(),
  129.                             data.GetMarginBottomRight())
  130.         dlg.Destroy()
  131.            
  132.     def OnPrintSetup(self, evt):
  133.         data = wx.PrintDialogData(self.pdata)
  134.         dlg = wx.PrintDialog(self, data)
  135.         dlg.GetPrintDialogData().SetSetupDialog(True)
  136.         dlg.ShowModal();
  137.         data = dlg.GetPrintDialogData()
  138.         self.pdata = wx.PrintData(data.GetPrintData())
  139.         dlg.Destroy()
  140.  
  141.     def OnPrintPreview(self, evt):
  142.         data = wx.PrintDialogData(self.pdata)
  143.         text = self.tc.GetValue()
  144.         printout1 = TextDocPrintout(text, "title", self.margins)
  145.         printout2 = TextDocPrintout(text, "title", self.margins)
  146.         preview = wx.PrintPreview(printout1, printout2, data)
  147.         if not preview.Ok():
  148.             wx.MessageBox("Unable to create PrintPreview!", "Error")
  149.         else:
  150.             frame = wx.PreviewFrame(preview, self, "Print Preview",
  151.             pos=self.GetPosition(),
  152.             size=self.GetSize())
  153.             frame.Initialize()
  154.             frame.Show()
  155.  
  156.     def OnPrint(self, evt):
  157.         data = wx.PrintDialogData(self.pdata)
  158.         printer = wx.Printer(data)
  159.         text = self.tc.GetValue()
  160.         printout = TextDocPrintout(text, "title", self.margins)
  161.         useSetupDialog = True
  162.         if not printer.Print(self, printout, useSetupDialog) \
  163.             and printer.GetLastError() == wx.PRINTER_ERROR:
  164.             wx.MessageBox(
  165.                 "There was a problem printing.\n"
  166.                 "Perhaps your current printer is not set correctly?",
  167.                 "Printing Error", wx.OK)
  168.         else:
  169.             data = printer.GetPrintDialogData()
  170.             self.pdata = wx.PrintData(data.GetPrintData()) # force a copy
  171.         printout.Destroy()
  172.  
  173. app = wx.PySimpleApp()
  174. frm = PrintFrameworkSample()
  175. frm.Show()
  176. app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement