Changeset 3232

Show
Ignore:
Timestamp:
07/09/07 17:39:39 (2 years ago)
Author:
ed
Message:

Added some experimental printing code. It seems to work OK, but there needs to be a lot more customization exposed.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/ui/uiwx/dEditor.py

    r3199 r3232  
    7171            if self.Parent: 
    7272                self.Parent.SetLexer(stc.STC_LEX_CONTAINER) 
     73 
     74 
     75class STCPrintout(wx.Printout): 
     76    """Printout class for styled text controls. Taken from the following 
     77    program by Riaan Booysen: 
     78        ----------------------------------------------------------------------------- 
     79         Name:         STCPrinting.py 
     80         Purpose: 
     81         
     82         Author:       Riaan Booysen 
     83         
     84         Created:      2003/05/21 
     85         RCS-ID:       $Id: STCPrinting.py,v 1.8 2006/10/12 12:19:17 riaan Exp $ 
     86         Copyright:   (c) 2003 - 2006 
     87         Licence:      wxWidgets 
     88        ----------------------------------------------------------------------------- 
     89        Boa:Dialog:STCPrintDlg 
     90    """ 
     91    margin = 0.1 
     92    linesPerPage = 80 
     93 
     94    def __init__(self, stc, colourMode=0, filename='', doPageNums=1): 
     95        wx.Printout.__init__(self) 
     96        self.stc = stc 
     97        self.colourMode = colourMode 
     98        self.filename = filename 
     99        self.doPageNums = doPageNums 
     100 
     101        self.pageTotal, m = divmod(stc.GetLineCount(), self.linesPerPage) 
     102        if m: self.pageTotal += 1 
     103 
     104 
     105    def HasPage(self, page): 
     106        return (page <= self.pageTotal) 
     107 
     108 
     109    def GetPageInfo(self): 
     110        return (1, self.pageTotal, 1, 32000) 
     111 
     112 
     113    def OnPrintPage(self, page): 
     114        stc = self.stc 
     115        self.stcLineHeight = stc.TextHeight(0) 
     116 
     117        # calculate sizes including margin and scale 
     118        dc = self.GetDC() 
     119        dw, dh = dc.GetSizeTuple() 
     120        mw = self.margin*dw 
     121        mh = self.margin*dh 
     122        textAreaHeight = dh - mh*2 
     123        textAreaWidth = dw - mw*2 
     124        scale = float(textAreaHeight)/(self.stcLineHeight*self.linesPerPage) 
     125        dc.SetUserScale(scale, scale) 
     126 
     127        # render page titles and numbers 
     128        f = dc.GetFont() 
     129        f.SetFamily(wx.ROMAN) 
     130        f.SetFaceName('Times New Roman') 
     131        f.SetPointSize(f.GetPointSize()+3) 
     132        dc.SetFont(f) 
     133 
     134        if self.filename: 
     135            tlw, tlh = dc.GetTextExtent(self.filename) 
     136            dc.DrawText(self.filename, 
     137                  int(dw/scale/2-tlw/2), int(mh/scale-tlh*3)) 
     138 
     139        if self.doPageNums: 
     140            pageLabel = _('Page: %d') % page 
     141            plw, plh = dc.GetTextExtent(pageLabel) 
     142            dc.DrawText(pageLabel, 
     143                  int(dw/scale/2-plw/2), int((textAreaHeight+mh)/scale+plh*2)) 
     144 
     145        # render stc into dc 
     146        stcStartPos = stc.PositionFromLine((page-1)*self.linesPerPage) 
     147        stcEndPos = stc.GetLineEndPosition(page*self.linesPerPage-1) 
     148 
     149        maxWidth = 32000 
     150        stc.SetPrintColourMode(self.colourMode) 
     151        ep = stc.FormatRange(1, stcStartPos, stcEndPos, dc, dc, 
     152                        wx.Rect(int(mw/scale), int(mh/scale), 
     153                               maxWidth, int(textAreaHeight/scale)), 
     154                        wx.Rect(0, (page-1)*self.linesPerPage*self.stcLineHeight, 
     155                            maxWidth, self.stcLineHeight*self.linesPerPage)) 
     156        # warn when fewer characters than expected are rendered by the stc when 
     157        # printing 
     158        if not self.IsPreview(): 
     159            if ep < stcEndPos: 
     160                print _('warning: on page %s: not enough chars rendered, diff: %s')%(page, stcEndPos-ep) 
     161        return True 
    73162 
    74163 
     
    119208        self._afterInit() 
    120209         
     210        self._printData = wx.PrintData() 
     211        self._printout = STCPrintout(self) 
    121212        self._newFileName = _("< New File >") 
    122213        self._curdir = os.getcwd() 
     
    192283        super(dEditor, self).__del__() 
    193284     
     285     
     286    def onPrintSetup(self): 
     287        dlgData = wx.PageSetupDialogData(self._printData) 
     288        printDlg = wx.PageSetupDialog(self, dlgData) 
     289        printDlg.ShowModal() 
     290        self._printData = wx.PrintData(dlgData.GetPrintData()) 
     291        printDlg.Destroy() 
     292         
     293     
     294    def onPrintPreview(self): 
     295        po1 = STCPrintout(self, stc.STC_PRINT_COLOURONWHITEDEFAULTBG, 
     296                self._fileName, False) 
     297        po2 = STCPrintout(self, stc.STC_PRINT_COLOURONWHITEDEFAULTBG, 
     298                self._fileName, False) 
     299        self._printPreview = wx.PrintPreview(po1, po2, self._printData) 
     300        if not self._printPreview.Ok(): 
     301            dabo.errorLog.write(_("An error occured while preparing preview.")) 
     302            return 
     303        frame = wx.PreviewFrame(self._printPreview, self.Form, _("Print Preview")) 
     304        frame.Initialize() 
     305        frame.SetSize(self.Form.Size) 
     306        frame.CenterOnScreen() 
     307        frame.Show(True) 
     308 
     309 
     310    def onPrint(self, evt=None): 
     311        pdd = wx.PrintDialogData() 
     312        pdd.SetPrintData(self._printData) 
     313        printer = wx.Printer(pdd) 
     314        printout = STCPrintout(self, stc.STC_PRINT_COLOURONWHITEDEFAULTBG, 
     315                self._fileName, False) 
     316 
     317        if not printer.Print(self.Form, printout): 
     318            dabo.errorLog.write(_("An error occured while printing.")) 
     319        else: 
     320            self.printData = wx.PrintData(printer.GetPrintDialogData().GetPrintData()) 
     321        printout.Destroy() 
     322         
    194323     
    195324    def setBookmark(self, nm, line=None):