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