]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/StyledTextCtrl_1.py
use the MSVC 7.1 DLLs for python versions >= 2.4, not ==
[wxWidgets.git] / wxPython / demo / StyledTextCtrl_1.py
CommitLineData
8fa876ca
RD
1#
2# 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3#
95bfd958 4# o wx.TheClipboard.Flush() generates a warning on program exit.
8fa876ca 5#
f6bcfd97 6
8fa876ca
RD
7import wx
8import wx.stc as stc
f6bcfd97 9
8fa876ca 10import images
1fded56b 11
f6bcfd97
BP
12#----------------------------------------------------------------------
13
a29a241f
RD
14debug = 1
15
16
f6bcfd97 17demoText = """\
95bfd958 18This editor is provided by a class named wx.StyledTextCtrl. As
f6bcfd97
BP
19the name suggests, you can define styles that can be applied to
20sections of text. This will typically be used for things like
21syntax highlighting code editors, but I'm sure that there are other
22applications as well. A style is a combination of font, point size,
8b9a4190 23foreground and background colours. The editor can handle
f6bcfd97
BP
24proportional fonts just as easily as monospaced fonts, and various
25styles can use different sized fonts.
26
27There are a few canned language lexers and colourizers included,
28(see the next demo) or you can handle the colourization yourself.
29If you do you can simply register an event handler and the editor
30will let you know when the visible portion of the text needs
31styling.
32
95bfd958 33wx.StyledTextEditor also supports setting markers in the margin...
f6bcfd97
BP
34
35
36
37
38...and indicators within the text. You can use these for whatever
39you want in your application. Cut, Copy, Paste, Drag and Drop of
40text works, as well as virtually unlimited Undo and Redo
41capabilities, (right click to try it out.)
f6bcfd97
BP
42"""
43
8fa876ca 44if wx.Platform == '__WXMSW__':
f6bcfd97
BP
45 face1 = 'Arial'
46 face2 = 'Times New Roman'
47 face3 = 'Courier New'
9968ba85 48 pb = 10
f6bcfd97
BP
49else:
50 face1 = 'Helvetica'
51 face2 = 'Times'
52 face3 = 'Courier'
7343610e 53 pb = 12
f6bcfd97
BP
54
55
56#----------------------------------------------------------------------
95bfd958 57# This shows how to catch the Modified event from the wx.StyledTextCtrl
f6bcfd97 58
8fa876ca 59class MySTC(stc.StyledTextCtrl):
f6bcfd97 60 def __init__(self, parent, ID, log):
8fa876ca 61 stc.StyledTextCtrl.__init__(self, parent, ID)
f6bcfd97
BP
62 self.log = log
63
8fa876ca
RD
64 self.Bind(stc.EVT_STC_DO_DROP, self.OnDoDrop)
65 self.Bind(stc.EVT_STC_DRAG_OVER, self.OnDragOver)
66 self.Bind(stc.EVT_STC_START_DRAG, self.OnStartDrag)
67 self.Bind(stc.EVT_STC_MODIFIED, self.OnModified)
f6bcfd97 68
8fa876ca 69 self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
63b6646e
RD
70
71 def OnDestroy(self, evt):
72 # This is how the clipboard contents can be preserved after
73 # the app has exited.
8fa876ca 74 wx.TheClipboard.Flush()
63b6646e 75 evt.Skip()
44e50dc1 76
f6bcfd97 77
a29a241f
RD
78 def OnStartDrag(self, evt):
79 self.log.write("OnStartDrag: %d, %s\n"
80 % (evt.GetDragAllowMove(), evt.GetDragText()))
81
82 if debug and evt.GetPosition() < 250:
1e4a197e 83 evt.SetDragAllowMove(False) # you can prevent moving of text (only copy)
a29a241f
RD
84 evt.SetDragText("DRAGGED TEXT") # you can change what is dragged
85 #evt.SetDragText("") # or prevent the drag with empty text
86
87
88 def OnDragOver(self, evt):
8fa876ca
RD
89 self.log.write(
90 "OnDragOver: x,y=(%d, %d) pos: %d DragResult: %d\n"
91 % (evt.GetX(), evt.GetY(), evt.GetPosition(), evt.GetDragResult())
92 )
a29a241f
RD
93
94 if debug and evt.GetPosition() < 250:
8fa876ca 95 evt.SetDragResult(wx.DragNone) # prevent dropping at the beginning of the buffer
a29a241f
RD
96
97
98 def OnDoDrop(self, evt):
99 self.log.write("OnDoDrop: x,y=(%d, %d) pos: %d DragResult: %d\n"
100 "\ttext: %s\n"
101 % (evt.GetX(), evt.GetY(), evt.GetPosition(), evt.GetDragResult(),
102 evt.GetDragText()))
103
104 if debug and evt.GetPosition() < 500:
105 evt.SetDragText("DROPPED TEXT") # Can change text if needed
8fa876ca 106 #evt.SetDragResult(wx.DragNone) # Can also change the drag operation, but it
a29a241f
RD
107 # is probably better to do it in OnDragOver so
108 # there is visual feedback
109
8fa876ca 110 #evt.SetPosition(25) # Can also change position, but I'm not sure why
a29a241f
RD
111 # you would want to...
112
113
114
115
f6bcfd97
BP
116 def OnModified(self, evt):
117 self.log.write("""OnModified
118 Mod type: %s
119 At position: %d
120 Lines added: %d
121 Text Length: %d
122 Text: %s\n""" % ( self.transModType(evt.GetModificationType()),
123 evt.GetPosition(),
124 evt.GetLinesAdded(),
125 evt.GetLength(),
10ef30eb 126 repr(evt.GetText()) ))
f6bcfd97 127
a29a241f 128
f6bcfd97
BP
129 def transModType(self, modType):
130 st = ""
8fa876ca
RD
131 table = [(stc.STC_MOD_INSERTTEXT, "InsertText"),
132 (stc.STC_MOD_DELETETEXT, "DeleteText"),
133 (stc.STC_MOD_CHANGESTYLE, "ChangeStyle"),
134 (stc.STC_MOD_CHANGEFOLD, "ChangeFold"),
135 (stc.STC_PERFORMED_USER, "UserFlag"),
136 (stc.STC_PERFORMED_UNDO, "Undo"),
137 (stc.STC_PERFORMED_REDO, "Redo"),
138 (stc.STC_LASTSTEPINUNDOREDO, "Last-Undo/Redo"),
139 (stc.STC_MOD_CHANGEMARKER, "ChangeMarker"),
140 (stc.STC_MOD_BEFOREINSERT, "B4-Insert"),
141 (stc.STC_MOD_BEFOREDELETE, "B4-Delete")
f6bcfd97
BP
142 ]
143
144 for flag,text in table:
145 if flag & modType:
146 st = st + text + " "
147
148 if not st:
149 st = 'UNKNOWN'
150
151 return st
152
153
154
a29a241f 155
f6bcfd97
BP
156#----------------------------------------------------------------------
157
cf273c67
RD
158_USE_PANEL = 1
159
f6bcfd97 160def runTest(frame, nb, log):
cf273c67
RD
161 if not _USE_PANEL:
162 ed = p = MySTC(nb, -1, log)
163
164 else:
8fa876ca 165 p = wx.Panel(nb, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE)
cf273c67 166 ed = MySTC(p, -1, log)
8fa876ca
RD
167 s = wx.BoxSizer(wx.HORIZONTAL)
168 s.Add(ed, 1, wx.EXPAND)
cf273c67 169 p.SetSizer(s)
1e4a197e 170 p.SetAutoLayout(True)
f6bcfd97 171
10ef30eb 172
1e4a197e 173 #ed.SetBufferedDraw(False)
a834585d
RD
174 #ed.StyleClearAll()
175 #ed.SetScrollWidth(800)
1e4a197e 176 #ed.SetWrapMode(True)
9513c5b6 177 #ed.SetUseAntiAliasing(False)
4b8e2a34 178 #ed.SetViewEOL(True)
a834585d 179
7343610e
RD
180 #ed.CmdKeyClear(stc.STC_KEY_BACK,
181 # stc.STC_SCMOD_CTRL)
182 #ed.CmdKeyAssign(stc.STC_KEY_BACK,
183 # stc.STC_SCMOD_CTRL,
184 # stc.STC_CMD_DELWORDLEFT)
185
f6bcfd97 186 ed.SetText(demoText)
8fa876ca
RD
187
188 if wx.USE_UNICODE:
10ef30eb
RD
189 import codecs
190 decode = codecs.lookup("utf-8")[1]
191
192 ed.GotoPos(ed.GetLength())
95bfd958 193 ed.AddText("\n\nwx.StyledTextCtrl can also do Unicode:\n")
498fbcb4 194 uniline = ed.GetCurrentLine()
10ef30eb
RD
195 unitext, l = decode('\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd - '
196 '\xd0\xbb\xd1\x83\xd1\x87\xd1\x88\xd0\xb8\xd0\xb9 '
197 '\xd1\x8f\xd0\xb7\xd1\x8b\xd0\xba \xd0\xbf\xd1\x80\xd0\xbe\xd0\xb3\xd1\x80\xd0\xb0\xd0\xbc\xd0\xbc\xd0\xb8\xd1\x80\xd0\xbe\xd0\xb2\xd0\xb0\xd0\xbd\xd0\xb8\xd1\x8f!\n\n')
198 ed.AddText('\tRussian: ')
199 ed.AddText(unitext)
200 ed.GotoPos(0)
3727c043
RD
201 #else:
202 # #ed.StyleSetFontEncoding(stc.STC_STYLE_DEFAULT, wx.FONTENCODING_KOI8)
203 # #text = u'\u041f\u0438\u0442\u043e\u043d - \u043b\u0443\u0447\u0448\u0438\u0439 \u044f\u0437\u044b\u043a \n\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f!'
204 # #text = text.encode('koi8-r')
205 # #ed.StyleSetFontEncoding(stc.STC_STYLE_DEFAULT, wx.FONTENCODING_BIG5)
206 # #text = u'Python \u662f\u6700\u597d\u7684\u7de8\u7a0b\u8a9e\u8a00\uff01'
207 # #text = text.encode('big5')
208 # ed.GotoPos(ed.GetLength())
209 # ed.AddText('\n\n' + text)
210
f6bcfd97
BP
211 ed.EmptyUndoBuffer()
212
213 # make some styles
8fa876ca 214 ed.StyleSetSpec(stc.STC_STYLE_DEFAULT, "size:%d,face:%s" % (pb, face3))
7db509e2 215 ed.StyleClearAll()
329a56f7 216 ed.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb, face1))
9968ba85 217 ed.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2, pb))
329a56f7 218 ed.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2, pb))
9968ba85 219 ed.StyleSetSpec(4, "face:%s,size:%d" % (face1, pb-1))
f6bcfd97 220
10ef30eb 221 # Now set some text to those styles... Normally this would be
f6bcfd97
BP
222 # done in an event handler that happens when text needs displayed.
223 ed.StartStyling(98, 0xff)
c368d904 224 ed.SetStyling(6, 1) # set style for 6 characters using style 1
f6bcfd97
BP
225
226 ed.StartStyling(190, 0xff)
c368d904 227 ed.SetStyling(20, 2)
f6bcfd97
BP
228
229 ed.StartStyling(310, 0xff)
c368d904
RD
230 ed.SetStyling(4, 3)
231 ed.SetStyling(2, 0)
232 ed.SetStyling(10, 4)
f6bcfd97
BP
233
234
235 # line numbers in the margin
8fa876ca 236 ed.SetMarginType(0, stc.STC_MARGIN_NUMBER)
f6bcfd97 237 ed.SetMarginWidth(0, 22)
329a56f7 238 ed.StyleSetSpec(stc.STC_STYLE_LINENUMBER, "size:%d,face:%s" % (pb-2, face1))
f6bcfd97
BP
239
240 # setup some markers
8fa876ca
RD
241 ed.SetMarginType(1, stc.STC_MARGIN_SYMBOL)
242 ed.MarkerDefine(0, stc.STC_MARK_ROUNDRECT, "#CCFF00", "RED")
e7cce279 243 ed.MarkerDefine(1, stc.STC_MARK_CIRCLE, "FOREST GREEN", "SIENNA")
8fa876ca
RD
244 ed.MarkerDefine(2, stc.STC_MARK_SHORTARROW, "blue", "blue")
245 ed.MarkerDefine(3, stc.STC_MARK_ARROW, "#00FF00", "#00FF00")
f6bcfd97
BP
246
247 # put some markers on some lines
248 ed.MarkerAdd(17, 0)
249 ed.MarkerAdd(18, 1)
250 ed.MarkerAdd(19, 2)
251 ed.MarkerAdd(20, 3)
252 ed.MarkerAdd(20, 0)
253
254
255 # and finally, an indicator or two
8fa876ca
RD
256 ed.IndicatorSetStyle(0, stc.STC_INDIC_SQUIGGLE)
257 ed.IndicatorSetForeground(0, wx.RED)
258 ed.IndicatorSetStyle(1, stc.STC_INDIC_DIAGONAL)
259 ed.IndicatorSetForeground(1, wx.BLUE)
260 ed.IndicatorSetStyle(2, stc.STC_INDIC_STRIKE)
261 ed.IndicatorSetForeground(2, wx.RED)
262
263 ed.StartStyling(836, stc.STC_INDICS_MASK)
264 ed.SetStyling(10, stc.STC_INDIC0_MASK)
265 ed.SetStyling(10, stc.STC_INDIC1_MASK)
266 ed.SetStyling(10, stc.STC_INDIC2_MASK | stc.STC_INDIC1_MASK)
f6bcfd97 267
10ef30eb 268
c7e7022c 269 # some test stuff...
a29a241f 270 if debug:
c7e7022c
RD
271 print "GetTextLength(): ", ed.GetTextLength(), len(ed.GetText())
272 print "GetText(): ", repr(ed.GetText())
273 print
274 print "GetStyledText(98, 104): ", repr(ed.GetStyledText(98, 104)), len(ed.GetStyledText(98, 104))
275 print
276 print "GetCurLine(): ", repr(ed.GetCurLine())
fea018f8
RD
277 ed.GotoPos(5)
278 print "GetCurLine(): ", repr(ed.GetCurLine())
c7e7022c
RD
279 print
280 print "GetLine(1): ", repr(ed.GetLine(1))
281 print
282 ed.SetSelection(25, 35)
283 print "GetSelectedText(): ", repr(ed.GetSelectedText())
284 print "GetTextRange(25, 35): ", repr(ed.GetTextRange(25, 35))
63b6646e
RD
285 print "FindText(0, max, 'indicators'): ",
286 print ed.FindText(0, ed.GetTextLength(), "indicators")
498fbcb4
RD
287 if wx.USE_UNICODE:
288 end = ed.GetLength()
289 start = ed.PositionFromLine(uniline)
290 print "GetTextRange(%d, %d): " % (start, end),
291 print repr(ed.GetTextRange(start, end))
292
65ec6247 293
88f226fa 294 wx.CallAfter(ed.GotoPos, 0)
cf273c67 295 return p
f6bcfd97
BP
296
297
298
299#----------------------------------------------------------------------
300
301
302overview = """\
303<html><body>
65ec6247 304Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
f6bcfd97
BP
305and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
306be helpful.
307</body><html>
308"""
309
310
0af45411 311if __name__ == '__main__':
8641d30c 312 import sys,os
0af45411 313 import run
8eca4fef 314 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
f6bcfd97 315