]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxStyledTextCtrl_1.py
always let the multiline text control process the Enter key presses
[wxWidgets.git] / wxPython / demo / wxStyledTextCtrl_1.py
CommitLineData
f6bcfd97
BP
1
2from wxPython.wx import *
3from wxPython.stc import *
4
5#----------------------------------------------------------------------
6
a29a241f
RD
7debug = 1
8
9
f6bcfd97
BP
10demoText = """\
11
12This editor is provided by a class named wxStyledTextCtrl. As
13the name suggests, you can define styles that can be applied to
14sections of text. This will typically be used for things like
15syntax highlighting code editors, but I'm sure that there are other
16applications as well. A style is a combination of font, point size,
17forground and background colours. The editor can handle
18proportional fonts just as easily as monospaced fonts, and various
19styles can use different sized fonts.
20
21There are a few canned language lexers and colourizers included,
22(see the next demo) or you can handle the colourization yourself.
23If you do you can simply register an event handler and the editor
24will let you know when the visible portion of the text needs
25styling.
26
27wxStyledTextEditor also supports setting markers in the margin...
28
29
30
31
32...and indicators within the text. You can use these for whatever
33you want in your application. Cut, Copy, Paste, Drag and Drop of
34text works, as well as virtually unlimited Undo and Redo
35capabilities, (right click to try it out.)
f6bcfd97
BP
36"""
37
38if wxPlatform == '__WXMSW__':
39 face1 = 'Arial'
40 face2 = 'Times New Roman'
41 face3 = 'Courier New'
9968ba85 42 pb = 10
f6bcfd97
BP
43else:
44 face1 = 'Helvetica'
45 face2 = 'Times'
46 face3 = 'Courier'
47 pb = 10
48
49
50#----------------------------------------------------------------------
51# This shows how to catch the Modified event from the wxStyledTextCtrl
52
53class MySTC(wxStyledTextCtrl):
54 def __init__(self, parent, ID, log):
55 wxStyledTextCtrl.__init__(self, parent, ID)
56 self.log = log
57
a29a241f
RD
58 EVT_STC_DO_DROP(self, ID, self.OnDoDrop)
59 EVT_STC_DRAG_OVER(self, ID, self.OnDragOver)
60 EVT_STC_START_DRAG(self, ID, self.OnStartDrag)
f6bcfd97
BP
61 EVT_STC_MODIFIED(self, ID, self.OnModified)
62
63
a29a241f
RD
64 def OnStartDrag(self, evt):
65 self.log.write("OnStartDrag: %d, %s\n"
66 % (evt.GetDragAllowMove(), evt.GetDragText()))
67
68 if debug and evt.GetPosition() < 250:
69 evt.SetDragAllowMove(false) # you can prevent moving of text (only copy)
70 evt.SetDragText("DRAGGED TEXT") # you can change what is dragged
71 #evt.SetDragText("") # or prevent the drag with empty text
72
73
74 def OnDragOver(self, evt):
75 self.log.write("OnDragOver: x,y=(%d, %d) pos: %d DragResult: %d\n"
76 % (evt.GetX(), evt.GetY(), evt.GetPosition(), evt.GetDragResult()))
77
78 if debug and evt.GetPosition() < 250:
79 evt.SetDragResult(wxDragNone) # prevent dropping at the begining of the buffer
80
81
82 def OnDoDrop(self, evt):
83 self.log.write("OnDoDrop: x,y=(%d, %d) pos: %d DragResult: %d\n"
84 "\ttext: %s\n"
85 % (evt.GetX(), evt.GetY(), evt.GetPosition(), evt.GetDragResult(),
86 evt.GetDragText()))
87
88 if debug and evt.GetPosition() < 500:
89 evt.SetDragText("DROPPED TEXT") # Can change text if needed
90 ##evt.SetDragResult(wxDragNone) # Can also change the drag operation, but it
91 # is probably better to do it in OnDragOver so
92 # there is visual feedback
93
94 ##evt.SetPosition(25) # Can also change position, but I'm not sure why
95 # you would want to...
96
97
98
99
f6bcfd97
BP
100 def OnModified(self, evt):
101 self.log.write("""OnModified
102 Mod type: %s
103 At position: %d
104 Lines added: %d
105 Text Length: %d
106 Text: %s\n""" % ( self.transModType(evt.GetModificationType()),
107 evt.GetPosition(),
108 evt.GetLinesAdded(),
109 evt.GetLength(),
10ef30eb 110 repr(evt.GetText()) ))
f6bcfd97 111
a29a241f 112
f6bcfd97
BP
113 def transModType(self, modType):
114 st = ""
115 table = [(wxSTC_MOD_INSERTTEXT, "InsertText"),
116 (wxSTC_MOD_DELETETEXT, "DeleteText"),
117 (wxSTC_MOD_CHANGESTYLE, "ChangeStyle"),
118 (wxSTC_MOD_CHANGEFOLD, "ChangeFold"),
119 (wxSTC_PERFORMED_USER, "UserFlag"),
120 (wxSTC_PERFORMED_UNDO, "Undo"),
121 (wxSTC_PERFORMED_REDO, "Redo"),
122 (wxSTC_LASTSTEPINUNDOREDO, "Last-Undo/Redo"),
123 (wxSTC_MOD_CHANGEMARKER, "ChangeMarker"),
124 (wxSTC_MOD_BEFOREINSERT, "B4-Insert"),
125 (wxSTC_MOD_BEFOREDELETE, "B4-Delete")
126 ]
127
128 for flag,text in table:
129 if flag & modType:
130 st = st + text + " "
131
132 if not st:
133 st = 'UNKNOWN'
134
135 return st
136
137
138
a29a241f 139
f6bcfd97
BP
140#----------------------------------------------------------------------
141
cf273c67
RD
142_USE_PANEL = 1
143
f6bcfd97 144def runTest(frame, nb, log):
cf273c67
RD
145 if not _USE_PANEL:
146 ed = p = MySTC(nb, -1, log)
147
148 else:
149 p = wxPanel(nb, -1)
150 ed = MySTC(p, -1, log)
151 s = wxBoxSizer(wxHORIZONTAL)
152 s.Add(ed, 1, wxEXPAND)
153 p.SetSizer(s)
154 p.SetAutoLayout(true)
f6bcfd97 155
10ef30eb
RD
156
157## ed.SetBufferedDraw(false)
158## ed.StyleClearAll()
f6bcfd97 159 ed.SetText(demoText)
10ef30eb
RD
160 if wxUSE_UNICODE:
161 import codecs
162 decode = codecs.lookup("utf-8")[1]
163
164 ed.GotoPos(ed.GetLength())
165 ed.AddText("\n\nwxStyledTextCtrl can also do Unicode:\n")
166 unitext, l = decode('\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd - '
167 '\xd0\xbb\xd1\x83\xd1\x87\xd1\x88\xd0\xb8\xd0\xb9 '
168 '\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')
169 ed.AddText('\tRussian: ')
170 ed.AddText(unitext)
171 ed.GotoPos(0)
172
f6bcfd97
BP
173 ed.EmptyUndoBuffer()
174
175 # make some styles
9968ba85
RD
176 ed.StyleSetSpec(wxSTC_STYLE_DEFAULT, "size:%d,face:%s" % (pb, face3))
177 ed.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb+2, face1))
178 ed.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2, pb))
179 ed.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2, pb+2))
180 ed.StyleSetSpec(4, "face:%s,size:%d" % (face1, pb-1))
f6bcfd97 181
10ef30eb 182 # Now set some text to those styles... Normally this would be
f6bcfd97
BP
183 # done in an event handler that happens when text needs displayed.
184 ed.StartStyling(98, 0xff)
c368d904 185 ed.SetStyling(6, 1) # set style for 6 characters using style 1
f6bcfd97
BP
186
187 ed.StartStyling(190, 0xff)
c368d904 188 ed.SetStyling(20, 2)
f6bcfd97
BP
189
190 ed.StartStyling(310, 0xff)
c368d904
RD
191 ed.SetStyling(4, 3)
192 ed.SetStyling(2, 0)
193 ed.SetStyling(10, 4)
f6bcfd97
BP
194
195
196 # line numbers in the margin
197 ed.SetMarginType(0, wxSTC_MARGIN_NUMBER)
198 ed.SetMarginWidth(0, 22)
199 ed.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "size:%d,face:%s" % (pb, face1))
200
201 # setup some markers
202 ed.SetMarginType(1, wxSTC_MARGIN_SYMBOL)
203 ed.MarkerDefine(0, wxSTC_MARK_ROUNDRECT, "#CCFF00", "RED")
204 ed.MarkerDefine(1, wxSTC_MARK_CIRCLE, "FOREST GREEN", "SIENNA")
205 ed.MarkerDefine(2, wxSTC_MARK_SHORTARROW, "blue", "blue")
206 ed.MarkerDefine(3, wxSTC_MARK_ARROW, "#00FF00", "#00FF00")
207
208 # put some markers on some lines
209 ed.MarkerAdd(17, 0)
210 ed.MarkerAdd(18, 1)
211 ed.MarkerAdd(19, 2)
212 ed.MarkerAdd(20, 3)
213 ed.MarkerAdd(20, 0)
214
215
216 # and finally, an indicator or two
217 ed.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE)
c368d904 218 ed.IndicatorSetForeground(0, wxRED)
f6bcfd97 219 ed.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL)
c368d904 220 ed.IndicatorSetForeground(1, wxBLUE)
f6bcfd97 221 ed.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE)
c368d904 222 ed.IndicatorSetForeground(2, wxRED)
f6bcfd97
BP
223
224 ed.StartStyling(836, wxSTC_INDICS_MASK)
c368d904
RD
225 ed.SetStyling(10, wxSTC_INDIC0_MASK)
226 ed.SetStyling(10, wxSTC_INDIC1_MASK)
227 ed.SetStyling(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK)
f6bcfd97 228
10ef30eb 229
c7e7022c 230 # some test stuff...
a29a241f 231 if debug:
c7e7022c
RD
232 print "GetTextLength(): ", ed.GetTextLength(), len(ed.GetText())
233 print "GetText(): ", repr(ed.GetText())
234 print
235 print "GetStyledText(98, 104): ", repr(ed.GetStyledText(98, 104)), len(ed.GetStyledText(98, 104))
236 print
237 print "GetCurLine(): ", repr(ed.GetCurLine())
fea018f8
RD
238 ed.GotoPos(5)
239 print "GetCurLine(): ", repr(ed.GetCurLine())
c7e7022c
RD
240 print
241 print "GetLine(1): ", repr(ed.GetLine(1))
242 print
243 ed.SetSelection(25, 35)
244 print "GetSelectedText(): ", repr(ed.GetSelectedText())
245 print "GetTextRange(25, 35): ", repr(ed.GetTextRange(25, 35))
246
65ec6247 247
c7e7022c 248 ed.GotoPos(0)
f6bcfd97 249
cf273c67 250 return p
f6bcfd97
BP
251
252
253
254#----------------------------------------------------------------------
255
256
257overview = """\
258<html><body>
65ec6247 259Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
f6bcfd97
BP
260and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
261be helpful.
262</body><html>
263"""
264
265
266
267if __name__ == '__main__':
268 import sys
269 app = wxPySimpleApp()
270 frame = wxFrame(None, -1, "Tester...", size=(640, 480))
271 win = runTest(frame, frame, sys.stdout)
272 frame.Show(true)
273 app.MainLoop()
274
275