ed_print.py
5.87 KB
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
###############################################################################
# Name: ed_print.py #
# Purpose: Editra's printer class #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2007 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
Printer class for creating and managing printouts from a StyledTextCtrl.
Classes:
- L{EdPrinter}: Class for managing printing and providing print dialogs
- L{EdPrintout}: Scales and renders the given document to a printer.
@summary: Printer Classes for printing text from an STC
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__cvsid__ = "$Id: ed_print.py 67499 2011-04-15 20:33:40Z CJP $"
__revision__ = "$Revision: 67499 $"
#--------------------------------------------------------------------------#
# Imports
import wx
import wx.stc
# Editra Imports
import ed_glob
import util
import extern.stcprint as stcprint
_ = wx.GetTranslation
#--------------------------------------------------------------------------#
# Globals
COLOURMODES = { ed_glob.PRINT_BLACK_WHITE : wx.stc.STC_PRINT_BLACKONWHITE,
ed_glob.PRINT_COLOR_WHITE : wx.stc.STC_PRINT_COLOURONWHITE,
ed_glob.PRINT_COLOR_DEF : wx.stc.STC_PRINT_COLOURONWHITEDEFAULTBG,
ed_glob.PRINT_INVERT : wx.stc.STC_PRINT_INVERTLIGHT,
ed_glob.PRINT_NORMAL : wx.stc.STC_PRINT_NORMAL }
#--------------------------------------------------------------------------#
class EdPrinter(object):
"""Printer Class for the editor
@note: current font size is fixed at 12 point for printing
"""
def __init__(self, parent, mode=ed_glob.PRINT_NORMAL):
"""Initializes the Printer
@param parent: parent window
@keyword mode: printer mode
"""
super(EdPrinter, self).__init__()
# Attributes
self.stc = None
self.title = wx.EmptyString
self.parent = parent
self.print_mode = mode
self.print_data = wx.PrintData()
self.margins = (wx.Point(15,15), wx.Point(15,15))
def CreatePrintout(self):
"""Creates a printout of the current stc window
@return: a printout object
"""
colour = COLOURMODES[self.print_mode]
dlg_data = wx.PageSetupDialogData(self.print_data)
dlg_data.SetPrintData(self.print_data)
dlg_data.SetMarginTopLeft(self.margins[0])
dlg_data.SetMarginBottomRight(self.margins[1])
fname = self.stc.GetFileName()
printout = stcprint.STCPrintout(self.stc, page_setup_data=dlg_data,
print_mode=colour, title=self.title,
job_title=fname)
return printout
def PageSetup(self):
"""Opens a print setup dialog and save print settings.
@return: None
"""
dlg_data = wx.PageSetupDialogData(self.print_data)
dlg_data.SetPrintData(self.print_data)
dlg_data.SetDefaultMinMargins(True)
dlg_data.SetMarginTopLeft(self.margins[0])
dlg_data.SetMarginBottomRight(self.margins[1])
print_dlg = wx.PageSetupDialog(self.parent, dlg_data)
if print_dlg.ShowModal() == wx.ID_OK:
self.print_data = wx.PrintData(dlg_data.GetPrintData())
self.print_data.SetPaperId(dlg_data.GetPaperId())
self.margins = (dlg_data.GetMarginTopLeft(),
dlg_data.GetMarginBottomRight())
print_dlg.Destroy()
def Preview(self):
"""Preview the Print
@return: None
"""
printout = self.CreatePrintout()
printout2 = self.CreatePrintout()
preview = wx.PrintPreview(printout, printout2, self.print_data)
preview.SetZoom(150)
if preview.IsOk():
pre_frame = wx.PreviewFrame(preview, self.parent,
_("Print Preview"))
dsize = wx.GetDisplaySize()
pre_frame.SetInitialSize((self.stc.GetSize()[0],
dsize.GetHeight() - 100))
pre_frame.Initialize()
pre_frame.Show()
else:
wx.MessageBox(_("Failed to create print preview"),
_("Print Error"),
style=wx.ICON_ERROR|wx.OK)
def Print(self):
"""Prints the document
@postcondition: the current document is printed
"""
pdd = wx.PrintDialogData(self.print_data)
printer = wx.Printer(pdd)
printout = self.CreatePrintout()
result = printer.Print(self.parent, printout)
if result:
dlg_data = printer.GetPrintDialogData()
self.print_data = wx.PrintData(dlg_data.GetPrintData())
elif printer.GetLastError() == wx.PRINTER_ERROR:
wx.MessageBox(_("There was an error when printing.\n"
"Check that your printer is properly connected."),
_("Printer Error"),
style=wx.ICON_ERROR|wx.OK)
printout.Destroy()
def SetColourMode(self, mode):
"""Sets the color mode that the text is to be rendered with
@param mode: mode to set the printer to use
@return: whether mode was set or not
@rtype: boolean
"""
if mode in COLOURMODES:
self.print_mode = mode
ret = True
else:
ret = False
return ret
def SetStc(self, stc):
"""Set the stc we are printing for
@param stc: instance of wx.stc.StyledTextCtrl
@note: MUST be called prior to any other print operations
"""
self.stc = stc