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