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 #----------------------------------------------------------------------
101 def runTest(frame
, nb
, log
):
103 ed
= p
= MySTC(nb
, -1, log
)
107 ed
= MySTC(p
, -1, log
)
108 s
= wxBoxSizer(wxHORIZONTAL
)
109 s
.Add(ed
, 1, wxEXPAND
)
111 p
.SetAutoLayout(true
)
117 ed
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "size:%d,face:%s" % (pb
+2, face3
))
118 ed
.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb
+3, face1
))
119 ed
.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2
, pb
+2))
120 ed
.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2
, pb
+3))
121 ed
.StyleSetSpec(4, "face:%s,size:%d" % (face1
, pb
))
124 # now set some text to those styles... Normally this would be
125 # done in an event handler that happens when text needs displayed.
126 ed
.StartStyling(98, 0xff)
127 ed
.SetStyling(6, 1) # set style for 6 characters using style 1
129 ed
.StartStyling(190, 0xff)
132 ed
.StartStyling(310, 0xff)
138 # line numbers in the margin
139 ed
.SetMarginType(0, wxSTC_MARGIN_NUMBER
)
140 ed
.SetMarginWidth(0, 22)
141 ed
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "size:%d,face:%s" % (pb
, face1
))
144 ed
.SetMarginType(1, wxSTC_MARGIN_SYMBOL
)
145 ed
.MarkerDefine(0, wxSTC_MARK_ROUNDRECT
, "#CCFF00", "RED")
146 ed
.MarkerDefine(1, wxSTC_MARK_CIRCLE
, "FOREST GREEN", "SIENNA")
147 ed
.MarkerDefine(2, wxSTC_MARK_SHORTARROW
, "blue", "blue")
148 ed
.MarkerDefine(3, wxSTC_MARK_ARROW
, "#00FF00", "#00FF00")
150 # put some markers on some lines
158 # and finally, an indicator or two
159 ed
.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE
)
160 ed
.IndicatorSetForeground(0, wxRED
)
161 ed
.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL
)
162 ed
.IndicatorSetForeground(1, wxBLUE
)
163 ed
.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE
)
164 ed
.IndicatorSetForeground(2, wxRED
)
166 ed
.StartStyling(836, wxSTC_INDICS_MASK
)
167 ed
.SetStyling(10, wxSTC_INDIC0_MASK
)
168 ed
.SetStyling(10, wxSTC_INDIC1_MASK
)
169 ed
.SetStyling(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK
)
176 #----------------------------------------------------------------------
181 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a>
182 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
189 if __name__
== '__main__':
191 app
= wxPySimpleApp()
192 frame
= wxFrame(None, -1, "Tester...", size
=(640, 480))
193 win
= runTest(frame
, frame
, sys
.stdout
)