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.)
36 if wxPlatform
== '__WXMSW__':
38 face2
= 'Times New Roman'
48 #----------------------------------------------------------------------
49 # This shows how to catch the Modified event from the wxStyledTextCtrl
51 class MySTC(wxStyledTextCtrl
):
52 def __init__(self
, parent
, ID
, log
):
53 wxStyledTextCtrl
.__init
__(self
, parent
, ID
)
56 EVT_STC_MODIFIED(self
, ID
, self
.OnModified
)
59 def OnModified(self
, evt
):
60 self
.log
.write("""OnModified
65 Text: %s\n""" % ( self
.transModType(evt
.GetModificationType()),
71 def transModType(self
, modType
):
73 table
= [(wxSTC_MOD_INSERTTEXT
, "InsertText"),
74 (wxSTC_MOD_DELETETEXT
, "DeleteText"),
75 (wxSTC_MOD_CHANGESTYLE
, "ChangeStyle"),
76 (wxSTC_MOD_CHANGEFOLD
, "ChangeFold"),
77 (wxSTC_PERFORMED_USER
, "UserFlag"),
78 (wxSTC_PERFORMED_UNDO
, "Undo"),
79 (wxSTC_PERFORMED_REDO
, "Redo"),
80 (wxSTC_LASTSTEPINUNDOREDO
, "Last-Undo/Redo"),
81 (wxSTC_MOD_CHANGEMARKER
, "ChangeMarker"),
82 (wxSTC_MOD_BEFOREINSERT
, "B4-Insert"),
83 (wxSTC_MOD_BEFOREDELETE
, "B4-Delete")
86 for flag
,text
in table
:
97 #----------------------------------------------------------------------
99 def runTest(frame
, nb
, log
):
100 ed
= MySTC(nb
, -1, log
)
106 ed
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "size:%d,face:%s" % (pb
+2, face3
))
107 ed
.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb
+3, face1
))
108 ed
.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2
, pb
+2))
109 ed
.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2
, pb
+3))
110 ed
.StyleSetSpec(4, "face:%s,size:%d" % (face1
, pb
))
113 # now set some text to those styles... Normally this would be
114 # done in an event handler that happens when text needs displayed.
115 ed
.StartStyling(98, 0xff)
116 ed
.SetStyleFor(6, 1) # set style for 6 characters using style 1
118 ed
.StartStyling(190, 0xff)
119 ed
.SetStyleFor(20, 2)
121 ed
.StartStyling(310, 0xff)
124 ed
.SetStyleFor(10, 4)
127 # line numbers in the margin
128 ed
.SetMarginType(0, wxSTC_MARGIN_NUMBER
)
129 ed
.SetMarginWidth(0, 22)
130 ed
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "size:%d,face:%s" % (pb
, face1
))
133 ed
.SetMarginType(1, wxSTC_MARGIN_SYMBOL
)
134 ed
.MarkerDefine(0, wxSTC_MARK_ROUNDRECT
, "#CCFF00", "RED")
135 ed
.MarkerDefine(1, wxSTC_MARK_CIRCLE
, "FOREST GREEN", "SIENNA")
136 ed
.MarkerDefine(2, wxSTC_MARK_SHORTARROW
, "blue", "blue")
137 ed
.MarkerDefine(3, wxSTC_MARK_ARROW
, "#00FF00", "#00FF00")
139 # put some markers on some lines
147 # and finally, an indicator or two
148 ed
.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE
)
149 ed
.IndicatorSetColour(0, wxRED
)
150 ed
.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL
)
151 ed
.IndicatorSetColour(1, wxBLUE
)
152 ed
.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE
)
153 ed
.IndicatorSetColour(2, wxRED
)
155 ed
.StartStyling(836, wxSTC_INDICS_MASK
)
156 ed
.SetStyleFor(10, wxSTC_INDIC0_MASK
)
157 ed
.SetStyleFor(10, wxSTC_INDIC1_MASK
)
158 ed
.SetStyleFor(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK
)
165 #----------------------------------------------------------------------
170 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a>
171 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
178 if __name__
== '__main__':
180 app
= wxPySimpleApp()
181 frame
= wxFrame(None, -1, "Tester...", size
=(640, 480))
182 win
= runTest(frame
, frame
, sys
.stdout
)