File: STCPrinting.py

package info (click to toggle)
boa-constructor 0.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,188 kB
  • ctags: 8,857
  • sloc: python: 54,163; sh: 66; makefile: 36
file content (268 lines) | stat: -rw-r--r-- 9,542 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#-----------------------------------------------------------------------------
# Name:        STCPrinting.py
# Purpose:     
#
# Author:      Riaan Booysen
#
# Created:     2003/05/21
# RCS-ID:      $Id: STCPrinting.py,v 1.2 2004/08/16 13:35:57 riaan Exp $
# Copyright:   (c) 2003 - 2004
# Licence:     wxWidgets
#-----------------------------------------------------------------------------
#Boa:Dialog:STCPrintDlg

"""
"""

# XXX Currently forces stc rendering not to wrap lines, currently the page
# XXX total calculations depend on exact number of lines

# XXX

import os

from wxPython.wx import *
from wxPython.stc import *

def create(parent):
    return STCPrintDlg(parent)

[wxID_STCPRINTDLG, wxID_STCPRINTDLGBTNCANCEL, wxID_STCPRINTDLGBTNPRINT, 
 wxID_STCPRINTDLGBTNPRINTPREVIEW, wxID_STCPRINTDLGBTNPRINTSETUP, 
 wxID_STCPRINTDLGCKBFILENAME, wxID_STCPRINTDLGCKBPAGENUMBERS, 
 wxID_STCPRINTDLGRDBCOLOURMODE, 
] = map(lambda _init_ctrls: wxNewId(), range(8))

stcPrintColourModes = [0, wxSTC_PRINT_BLACKONWHITE, wxSTC_PRINT_COLOURONWHITE, 
                       wxSTC_PRINT_COLOURONWHITEDEFAULTBG]

class STCPrintDlg(wxDialog):
    def _init_utils(self):
        # generated method, don't edit
        pass

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wxDialog.__init__(self, id=wxID_STCPRINTDLG, name='STCPrintDlg',
              parent=prnt, pos=wxPoint(352, 204), size=wxSize(402, 189),
              style=wxDEFAULT_DIALOG_STYLE, title='Print Source')
        self._init_utils()
        self.SetClientSize(wxSize(394, 162))

        self.rdbColourMode = wxRadioBox(choices=['Normal', 'Black on white',
              'Colour on white', 'Colour on white default background'],
              id=wxID_STCPRINTDLGRDBCOLOURMODE, label='Colour Mode',
              majorDimension=1, name='rdbColourMode', parent=self,
              point=wxPoint(8, 8), size=wxSize(216, 112),
              style=wxRA_SPECIFY_COLS, validator=wxDefaultValidator)

        self.ckbFilename = wxCheckBox(id=wxID_STCPRINTDLGCKBFILENAME,
              label='Filename at top', name='ckbFilename', parent=self,
              pos=wxPoint(240, 16), size=wxSize(144, 13), style=0)
        self.ckbFilename.SetValue(True)

        self.ckbPageNumbers = wxCheckBox(id=wxID_STCPRINTDLGCKBPAGENUMBERS,
              label='Page numbers', name='ckbPageNumbers', parent=self,
              pos=wxPoint(240, 40), size=wxSize(136, 13), style=0)
        self.ckbPageNumbers.SetValue(True)

        self.btnPrintSetup = wxButton(id=wxID_STCPRINTDLGBTNPRINTSETUP,
              label='Print Setup', name='btnPrintSetup', parent=self,
              pos=wxPoint(8, 128), size=wxSize(88, 23), style=0)
        EVT_BUTTON(self.btnPrintSetup, wxID_STCPRINTDLGBTNPRINTSETUP,
              self.OnPrintSetup)

        self.btnPrintPreview = wxButton(id=wxID_STCPRINTDLGBTNPRINTPREVIEW,
              label='Print Preview', name='btnPrintPreview', parent=self,
              pos=wxPoint(104, 128), size=wxSize(88, 23), style=0)
        EVT_BUTTON(self.btnPrintPreview, wxID_STCPRINTDLGBTNPRINTPREVIEW,
              self.OnPrintPreview)

        self.btnPrint = wxButton(id=wxID_STCPRINTDLGBTNPRINT, label='Print',
              name='btnPrint', parent=self, pos=wxPoint(200, 128),
              size=wxSize(88, 23), style=0)
        EVT_BUTTON(self.btnPrint, wxID_STCPRINTDLGBTNPRINT, self.OnDoPrint)

        self.btnCancel = wxButton(id=wxID_CANCEL, label='Cancel',
              name='btnCancel', parent=self, pos=wxPoint(296, 128),
              size=wxSize(88, 23), style=0)

    colourMode = 0
    

    def __init__(self, parent, stc, filename=''):
        self._init_ctrls(parent)

        self.stc = stc
        self.filename = filename
        self.parentFrame = parent
        self.preview = None

        self.printData = wxPrintData()
        self.printData.SetPaperId(wxPAPER_LETTER)


    def OnPrintSetup(self, event):
        printerDlg = wxPrintDialog(self)
        pdd = printerDlg.GetPrintDialogData()
        pdd.SetPrintData(self.printData)
        pdd.SetSetupDialog(true)
        printerDlg.ShowModal()
        self.printData = pdd.GetPrintData()
        printerDlg.Destroy()

    def createSTCPrintout(self):
        colourMode = stcPrintColourModes[self.rdbColourMode.GetSelection()]
        doPageNums = self.ckbPageNumbers.GetValue()
        if self.ckbFilename.GetValue():
            filename = self.filename
        else:
            filename = ''

        return STCPrintout(self.stc, colourMode, filename, doPageNums)
    
    def OnPrintPreview(self, event):
        printout1 = self.createSTCPrintout()
        printout2 = self.createSTCPrintout()
        self.preview = wxPrintPreview(printout1, printout2, self.printData)
        if not self.preview.Ok():
            wxLogError('An error occured while preparing preview.')
            return

        frame = wxPreviewFrame(self.preview, self.parentFrame, 'Print Preview')
        frame.Initialize()
        frame.Show(true)


    def OnDoPrint(self, event):
        pdd = wxPrintDialogData()
        pdd.SetPrintData(self.printData)
        printer = wxPrinter(pdd)
        printout = self.createSTCPrintout()
        
        if not printer.Print(self.parentFrame, printout):
            wxLogError('An error occured while printing.')
        else:
            self.printData = printer.GetPrintDialogData().GetPrintData()
        printout.Destroy()
        
        self.EndModal(wxOK)


#-------------------------------------------------------------------------------

class STCPrintout(wxPrintout):
    margin = 0.1
    linesPerPage = 69
    
    def __init__(self, stc, colourMode=0, filename='', doPageNums=1):
        wxPrintout.__init__(self)
        self.stc = stc
        self.colourMode = colourMode
        self.filename = filename
        self.doPageNums = doPageNums

        self.pageTotal, m = divmod(stc.GetLineCount(), self.linesPerPage)
        if m: self.pageTotal += 1
        
##    def OnBeginDocument(self, start, end):
##        return self.base_OnBeginDocument(start, end)
##
##    def OnEndDocument(self):
##        self.base_OnEndDocument()
##
##    def OnBeginPrinting(self):
##        self.base_OnBeginPrinting()
##
##    def OnEndPrinting(self):
##        self.base_OnEndPrinting()
##
##    def OnPreparePrinting(self):
##        self.base_OnPreparePrinting()

    def HasPage(self, page):
        if page <= self.pageTotal:
            return true
        else:
            return false

    def GetPageInfo(self):
        return (1, self.pageTotal, 1, 32000)

    def OnPrintPage(self, page):
        stc = self.stc
        self.stcLineHeight = stc.TextHeight(0)

        # calculate sizes including margin and scale
        dc = self.GetDC()
        dw, dh = dc.GetSizeTuple()
        
        mw = self.margin*dw
        mh = self.margin*dh
        textAreaHeight = dh - mh*2
        textAreaWidth = dw - mw*2
        
        scale = float(textAreaHeight)/(self.stcLineHeight*self.linesPerPage)
        dc.SetUserScale(scale, scale)
        
        # render page titles and numbers
        f = dc.GetFont()
        f.SetFamily(wxROMAN)
        f.SetFaceName('Times New Roman')
        f.SetPointSize(f.GetPointSize()+3)
        dc.SetFont(f)
        
        if self.filename:
            tlw, tlh = dc.GetTextExtent(self.filename)
            dc.DrawText(self.filename, 
                  int(dw/scale/2-tlw/2), int(mh/scale-tlh*3))

        if self.doPageNums:
            pageLabel = 'Page: %d' % page
            plw, plh = dc.GetTextExtent(pageLabel)
            dc.DrawText(pageLabel, 
                  int(dw/scale/2-plw/2), int((textAreaHeight+mh)/scale+plh*2))

        # render stc into dc
        stcStartPos = stc.PositionFromLine((page-1)*self.linesPerPage)
        stcEndPos = stc.GetLineEndPosition(page*self.linesPerPage-1)

        maxWidth = 32000
        stc.SetPrintColourMode(self.colourMode)
        ep = stc.FormatRange(1, stcStartPos, stcEndPos, dc, dc, 
                        wxRect(int(mw/scale), int(mh/scale), 
                               maxWidth, int(textAreaHeight/scale)), 
                        wxRect(0, (page-1)*self.linesPerPage*self.stcLineHeight, 
                            maxWidth, self.stcLineHeight*self.linesPerPage))

        # warn when less characters than expected is rendered by the stc when 
        # printing
        if not self.IsPreview():
            if ep < stcEndPos:
                print 'warning: on page', page, \
                      ': not enough chars rendered, diff:', stcEndPos-ep

        return true


#----------------------------------------------------------------------

if __name__ == '__main__':
    testFile = 'STCPrinting.py'
    app = wxPySimpleApp()
    wxInitAllImageHandlers()
    frame = wxFrame(None, -1, '')
    # prepare an stc
    frame.stc = wxStyledTextCtrl(frame, -1)
    frame.stc.SetText(open(testFile).read())
    config = os.path.abspath('../Config/stc-styles.rc.cfg')
    from STCStyleEditor import initSTC
    initSTC(frame.stc, config, 'python')
    # print dlg for prepared stc
    dlg = STCPrintDlg(frame, frame.stc, testFile)
    try:
        dlg.ShowModal()
    finally:
        dlg.Destroy()
    wxCallAfter(frame.Destroy)
    app.MainLoop()