]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/wxStyledTextCtrl_1.py
Bitmap button updates
[wxWidgets.git] / wxPython / demo / wxStyledTextCtrl_1.py
... / ...
CommitLineData
1
2from wxPython.wx import *
3from wxPython.stc import *
4
5#----------------------------------------------------------------------
6
7debug = 1
8
9
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.)
36"""
37
38if wxPlatform == '__WXMSW__':
39 face1 = 'Arial'
40 face2 = 'Times New Roman'
41 face3 = 'Courier New'
42 pb = 10
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
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)
61 EVT_STC_MODIFIED(self, ID, self.OnModified)
62
63 EVT_WINDOW_DESTROY(self, self.OnDestroy)
64
65 def OnDestroy(self, evt):
66 # This is how the clipboard contents can be preserved after
67 # the app has exited.
68 wxTheClipboard.Flush()
69 evt.Skip()
70
71
72 def OnStartDrag(self, evt):
73 self.log.write("OnStartDrag: %d, %s\n"
74 % (evt.GetDragAllowMove(), evt.GetDragText()))
75
76 if debug and evt.GetPosition() < 250:
77 evt.SetDragAllowMove(false) # you can prevent moving of text (only copy)
78 evt.SetDragText("DRAGGED TEXT") # you can change what is dragged
79 #evt.SetDragText("") # or prevent the drag with empty text
80
81
82 def OnDragOver(self, evt):
83 self.log.write("OnDragOver: x,y=(%d, %d) pos: %d DragResult: %d\n"
84 % (evt.GetX(), evt.GetY(), evt.GetPosition(), evt.GetDragResult()))
85
86 if debug and evt.GetPosition() < 250:
87 evt.SetDragResult(wxDragNone) # prevent dropping at the begining of the buffer
88
89
90 def OnDoDrop(self, evt):
91 self.log.write("OnDoDrop: x,y=(%d, %d) pos: %d DragResult: %d\n"
92 "\ttext: %s\n"
93 % (evt.GetX(), evt.GetY(), evt.GetPosition(), evt.GetDragResult(),
94 evt.GetDragText()))
95
96 if debug and evt.GetPosition() < 500:
97 evt.SetDragText("DROPPED TEXT") # Can change text if needed
98 ##evt.SetDragResult(wxDragNone) # Can also change the drag operation, but it
99 # is probably better to do it in OnDragOver so
100 # there is visual feedback
101
102 ##evt.SetPosition(25) # Can also change position, but I'm not sure why
103 # you would want to...
104
105
106
107
108 def OnModified(self, evt):
109 self.log.write("""OnModified
110 Mod type: %s
111 At position: %d
112 Lines added: %d
113 Text Length: %d
114 Text: %s\n""" % ( self.transModType(evt.GetModificationType()),
115 evt.GetPosition(),
116 evt.GetLinesAdded(),
117 evt.GetLength(),
118 repr(evt.GetText()) ))
119
120
121 def transModType(self, modType):
122 st = ""
123 table = [(wxSTC_MOD_INSERTTEXT, "InsertText"),
124 (wxSTC_MOD_DELETETEXT, "DeleteText"),
125 (wxSTC_MOD_CHANGESTYLE, "ChangeStyle"),
126 (wxSTC_MOD_CHANGEFOLD, "ChangeFold"),
127 (wxSTC_PERFORMED_USER, "UserFlag"),
128 (wxSTC_PERFORMED_UNDO, "Undo"),
129 (wxSTC_PERFORMED_REDO, "Redo"),
130 (wxSTC_LASTSTEPINUNDOREDO, "Last-Undo/Redo"),
131 (wxSTC_MOD_CHANGEMARKER, "ChangeMarker"),
132 (wxSTC_MOD_BEFOREINSERT, "B4-Insert"),
133 (wxSTC_MOD_BEFOREDELETE, "B4-Delete")
134 ]
135
136 for flag,text in table:
137 if flag & modType:
138 st = st + text + " "
139
140 if not st:
141 st = 'UNKNOWN'
142
143 return st
144
145
146
147
148#----------------------------------------------------------------------
149
150_USE_PANEL = 1
151
152def runTest(frame, nb, log):
153 if not _USE_PANEL:
154 ed = p = MySTC(nb, -1, log)
155
156 else:
157 p = wxPanel(nb, -1, style=wxNO_FULL_REPAINT_ON_RESIZE)
158 ed = MySTC(p, -1, log)
159 s = wxBoxSizer(wxHORIZONTAL)
160 s.Add(ed, 1, wxEXPAND)
161 p.SetSizer(s)
162 p.SetAutoLayout(true)
163
164
165 #ed.SetBufferedDraw(false)
166 #ed.StyleClearAll()
167 #ed.SetScrollWidth(800)
168 #ed.SetWrapMode(true)
169
170 ed.SetText(demoText)
171 if wxUSE_UNICODE:
172 import codecs
173 decode = codecs.lookup("utf-8")[1]
174
175 ed.GotoPos(ed.GetLength())
176 ed.AddText("\n\nwxStyledTextCtrl can also do Unicode:\n")
177 unitext, l = decode('\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd - '
178 '\xd0\xbb\xd1\x83\xd1\x87\xd1\x88\xd0\xb8\xd0\xb9 '
179 '\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')
180 ed.AddText('\tRussian: ')
181 ed.AddText(unitext)
182 ed.GotoPos(0)
183
184 ed.EmptyUndoBuffer()
185
186 # make some styles
187 ed.StyleSetSpec(wxSTC_STYLE_DEFAULT, "size:%d,face:%s" % (pb, face3))
188 ed.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb+2, face1))
189 ed.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2, pb))
190 ed.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2, pb+2))
191 ed.StyleSetSpec(4, "face:%s,size:%d" % (face1, pb-1))
192
193 # Now set some text to those styles... Normally this would be
194 # done in an event handler that happens when text needs displayed.
195 ed.StartStyling(98, 0xff)
196 ed.SetStyling(6, 1) # set style for 6 characters using style 1
197
198 ed.StartStyling(190, 0xff)
199 ed.SetStyling(20, 2)
200
201 ed.StartStyling(310, 0xff)
202 ed.SetStyling(4, 3)
203 ed.SetStyling(2, 0)
204 ed.SetStyling(10, 4)
205
206
207 # line numbers in the margin
208 ed.SetMarginType(0, wxSTC_MARGIN_NUMBER)
209 ed.SetMarginWidth(0, 22)
210 ed.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "size:%d,face:%s" % (pb, face1))
211
212 # setup some markers
213 ed.SetMarginType(1, wxSTC_MARGIN_SYMBOL)
214 ed.MarkerDefine(0, wxSTC_MARK_ROUNDRECT, "#CCFF00", "RED")
215 ed.MarkerDefine(1, wxSTC_MARK_CIRCLE, "FOREST GREEN", "SIENNA")
216 ed.MarkerDefine(2, wxSTC_MARK_SHORTARROW, "blue", "blue")
217 ed.MarkerDefine(3, wxSTC_MARK_ARROW, "#00FF00", "#00FF00")
218
219 # put some markers on some lines
220 ed.MarkerAdd(17, 0)
221 ed.MarkerAdd(18, 1)
222 ed.MarkerAdd(19, 2)
223 ed.MarkerAdd(20, 3)
224 ed.MarkerAdd(20, 0)
225
226
227 # and finally, an indicator or two
228 ed.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE)
229 ed.IndicatorSetForeground(0, wxRED)
230 ed.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL)
231 ed.IndicatorSetForeground(1, wxBLUE)
232 ed.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE)
233 ed.IndicatorSetForeground(2, wxRED)
234
235 ed.StartStyling(836, wxSTC_INDICS_MASK)
236 ed.SetStyling(10, wxSTC_INDIC0_MASK)
237 ed.SetStyling(10, wxSTC_INDIC1_MASK)
238 ed.SetStyling(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK)
239
240
241 # some test stuff...
242 if debug:
243 print "GetTextLength(): ", ed.GetTextLength(), len(ed.GetText())
244 print "GetText(): ", repr(ed.GetText())
245 print
246 print "GetStyledText(98, 104): ", repr(ed.GetStyledText(98, 104)), len(ed.GetStyledText(98, 104))
247 print
248 print "GetCurLine(): ", repr(ed.GetCurLine())
249 ed.GotoPos(5)
250 print "GetCurLine(): ", repr(ed.GetCurLine())
251 print
252 print "GetLine(1): ", repr(ed.GetLine(1))
253 print
254 ed.SetSelection(25, 35)
255 print "GetSelectedText(): ", repr(ed.GetSelectedText())
256 print "GetTextRange(25, 35): ", repr(ed.GetTextRange(25, 35))
257 print "FindText(0, max, 'indicators'): ",
258 print ed.FindText(0, ed.GetTextLength(), "indicators")
259
260 ed.GotoPos(0)
261
262
263 return p
264
265
266
267#----------------------------------------------------------------------
268
269
270overview = """\
271<html><body>
272Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
273and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
274be helpful.
275</body><html>
276"""
277
278
279if __name__ == '__main__':
280 import sys,os
281 import run
282 run.main(['', os.path.basename(sys.argv[0])])
283