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