2 from wxPython
.wx
import *
3 from wxPython
.stc
import *
5 #----------------------------------------------------------------------
9 This editor is provided by a class named wxStyledTextCtrl. As
10 the name suggests, you can define styles that can be applied to
11 sections of text. This will typically be used for things like
12 syntax highlighting code editors, but I'm sure that there are other
13 applications as well. A style is a combination of font, point size,
14 forground and background colours. The editor can handle
15 proportional fonts just as easily as monospaced fonts, and various
16 styles can use different sized fonts.
18 There are a few canned language lexers and colourizers included,
19 (see the next demo) or you can handle the colourization yourself.
20 If you do you can simply register an event handler and the editor
21 will let you know when the visible portion of the text needs
24 wxStyledTextEditor also supports setting markers in the margin...
29 ...and indicators within the text. You can use these for whatever
30 you want in your application. Cut, Copy, Paste, Drag and Drop of
31 text works, as well as virtually unlimited Undo and Redo
32 capabilities, (right click to try it out.)
35 if wxPlatform
== '__WXMSW__':
37 face2
= 'Times New Roman'
47 #----------------------------------------------------------------------
48 # This shows how to catch the Modified event from the wxStyledTextCtrl
50 class MySTC(wxStyledTextCtrl
):
51 def __init__(self
, parent
, ID
, log
):
52 wxStyledTextCtrl
.__init
__(self
, parent
, ID
)
55 EVT_STC_MODIFIED(self
, ID
, self
.OnModified
)
58 def OnModified(self
, evt
):
59 self
.log
.write("""OnModified
64 Text: %s\n""" % ( self
.transModType(evt
.GetModificationType()),
70 def transModType(self
, modType
):
72 table
= [(wxSTC_MOD_INSERTTEXT
, "InsertText"),
73 (wxSTC_MOD_DELETETEXT
, "DeleteText"),
74 (wxSTC_MOD_CHANGESTYLE
, "ChangeStyle"),
75 (wxSTC_MOD_CHANGEFOLD
, "ChangeFold"),
76 (wxSTC_PERFORMED_USER
, "UserFlag"),
77 (wxSTC_PERFORMED_UNDO
, "Undo"),
78 (wxSTC_PERFORMED_REDO
, "Redo"),
79 (wxSTC_LASTSTEPINUNDOREDO
, "Last-Undo/Redo"),
80 (wxSTC_MOD_CHANGEMARKER
, "ChangeMarker"),
81 (wxSTC_MOD_BEFOREINSERT
, "B4-Insert"),
82 (wxSTC_MOD_BEFOREDELETE
, "B4-Delete")
85 for flag
,text
in table
:
96 #----------------------------------------------------------------------
100 def runTest(frame
, nb
, log
):
102 ed
= p
= MySTC(nb
, -1, log
)
106 ed
= MySTC(p
, -1, log
)
107 s
= wxBoxSizer(wxHORIZONTAL
)
108 s
.Add(ed
, 1, wxEXPAND
)
110 p
.SetAutoLayout(true
)
116 ed
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "size:%d,face:%s" % (pb
+2, face3
))
117 ed
.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb
+3, face1
))
118 ed
.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2
, pb
+2))
119 ed
.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2
, pb
+3))
120 ed
.StyleSetSpec(4, "face:%s,size:%d" % (face1
, pb
))
123 # now set some text to those styles... Normally this would be
124 # done in an event handler that happens when text needs displayed.
125 ed
.StartStyling(98, 0xff)
126 ed
.SetStyling(6, 1) # set style for 6 characters using style 1
128 ed
.StartStyling(190, 0xff)
131 ed
.StartStyling(310, 0xff)
137 # line numbers in the margin
138 ed
.SetMarginType(0, wxSTC_MARGIN_NUMBER
)
139 ed
.SetMarginWidth(0, 22)
140 ed
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "size:%d,face:%s" % (pb
, face1
))
143 ed
.SetMarginType(1, wxSTC_MARGIN_SYMBOL
)
144 ed
.MarkerDefine(0, wxSTC_MARK_ROUNDRECT
, "#CCFF00", "RED")
145 ed
.MarkerDefine(1, wxSTC_MARK_CIRCLE
, "FOREST GREEN", "SIENNA")
146 ed
.MarkerDefine(2, wxSTC_MARK_SHORTARROW
, "blue", "blue")
147 ed
.MarkerDefine(3, wxSTC_MARK_ARROW
, "#00FF00", "#00FF00")
149 # put some markers on some lines
157 # and finally, an indicator or two
158 ed
.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE
)
159 ed
.IndicatorSetForeground(0, wxRED
)
160 ed
.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL
)
161 ed
.IndicatorSetForeground(1, wxBLUE
)
162 ed
.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE
)
163 ed
.IndicatorSetForeground(2, wxRED
)
165 ed
.StartStyling(836, wxSTC_INDICS_MASK
)
166 ed
.SetStyling(10, wxSTC_INDIC0_MASK
)
167 ed
.SetStyling(10, wxSTC_INDIC1_MASK
)
168 ed
.SetStyling(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK
)
172 print "GetTextLength(): ", ed
.GetTextLength(), len(ed
.GetText())
173 print "GetText(): ", repr(ed
.GetText())
175 print "GetStyledText(98, 104): ", repr(ed
.GetStyledText(98, 104)), len(ed
.GetStyledText(98, 104))
177 print "GetCurLine(): ", repr(ed
.GetCurLine())
179 print "GetLine(1): ", repr(ed
.GetLine(1))
181 ed
.SetSelection(25, 35)
182 print "GetSelectedText(): ", repr(ed
.GetSelectedText())
183 print "GetTextRange(25, 35): ", repr(ed
.GetTextRange(25, 35))
192 #----------------------------------------------------------------------
197 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
198 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
205 if __name__
== '__main__':
207 app
= wxPySimpleApp()
208 frame
= wxFrame(None, -1, "Tester...", size
=(640, 480))
209 win
= runTest(frame
, frame
, sys
.stdout
)