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