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