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