]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxStyledTextCtrl_1.py
patch fix for a constant definition under OS/2 VA V4.0
[wxWidgets.git] / wxPython / demo / wxStyledTextCtrl_1.py
1
2 from wxPython.wx import *
3 from wxPython.stc import *
4
5 #----------------------------------------------------------------------
6
7 demoText = """\
8
9 This editor is provided by a class named wxStyledTextCtrl. As
10 the name suggests, you can define styles that can be applied to
11 sections of text. This will typically be used for things like
12 syntax highlighting code editors, but I'm sure that there are other
13 applications as well. A style is a combination of font, point size,
14 forground and background colours. The editor can handle
15 proportional fonts just as easily as monospaced fonts, and various
16 styles can use different sized fonts.
17
18 There are a few canned language lexers and colourizers included,
19 (see the next demo) or you can handle the colourization yourself.
20 If you do you can simply register an event handler and the editor
21 will let you know when the visible portion of the text needs
22 styling.
23
24 wxStyledTextEditor also supports setting markers in the margin...
25
26
27
28
29 ...and indicators within the text. You can use these for whatever
30 you want in your application. Cut, Copy, Paste, Drag and Drop of
31 text works, as well as virtually unlimited Undo and Redo
32 capabilities, (right click to try it out.)
33 """
34
35 if wxPlatform == '__WXMSW__':
36 face1 = 'Arial'
37 face2 = 'Times New Roman'
38 face3 = 'Courier New'
39 pb = 10
40 else:
41 face1 = 'Helvetica'
42 face2 = 'Times'
43 face3 = 'Courier'
44 pb = 10
45
46
47 #----------------------------------------------------------------------
48 # This shows how to catch the Modified event from the wxStyledTextCtrl
49
50 class MySTC(wxStyledTextCtrl):
51 def __init__(self, parent, ID, log):
52 wxStyledTextCtrl.__init__(self, parent, ID)
53 self.log = log
54
55 EVT_STC_MODIFIED(self, ID, self.OnModified)
56
57
58 def OnModified(self, evt):
59 self.log.write("""OnModified
60 Mod type: %s
61 At position: %d
62 Lines added: %d
63 Text Length: %d
64 Text: %s\n""" % ( self.transModType(evt.GetModificationType()),
65 evt.GetPosition(),
66 evt.GetLinesAdded(),
67 evt.GetLength(),
68 evt.GetText() ))
69
70 def transModType(self, modType):
71 st = ""
72 table = [(wxSTC_MOD_INSERTTEXT, "InsertText"),
73 (wxSTC_MOD_DELETETEXT, "DeleteText"),
74 (wxSTC_MOD_CHANGESTYLE, "ChangeStyle"),
75 (wxSTC_MOD_CHANGEFOLD, "ChangeFold"),
76 (wxSTC_PERFORMED_USER, "UserFlag"),
77 (wxSTC_PERFORMED_UNDO, "Undo"),
78 (wxSTC_PERFORMED_REDO, "Redo"),
79 (wxSTC_LASTSTEPINUNDOREDO, "Last-Undo/Redo"),
80 (wxSTC_MOD_CHANGEMARKER, "ChangeMarker"),
81 (wxSTC_MOD_BEFOREINSERT, "B4-Insert"),
82 (wxSTC_MOD_BEFOREDELETE, "B4-Delete")
83 ]
84
85 for flag,text in table:
86 if flag & modType:
87 st = st + text + " "
88
89 if not st:
90 st = 'UNKNOWN'
91
92 return st
93
94
95
96 #----------------------------------------------------------------------
97
98 _USE_PANEL = 1
99
100 def runTest(frame, nb, log):
101 if not _USE_PANEL:
102 ed = p = MySTC(nb, -1, log)
103
104 else:
105 p = wxPanel(nb, -1)
106 ed = MySTC(p, -1, log)
107 s = wxBoxSizer(wxHORIZONTAL)
108 s.Add(ed, 1, wxEXPAND)
109 p.SetSizer(s)
110 p.SetAutoLayout(true)
111
112 ed.SetText(demoText)
113 ed.EmptyUndoBuffer()
114
115 # make some styles
116 ed.StyleSetSpec(wxSTC_STYLE_DEFAULT, "size:%d,face:%s" % (pb, face3))
117 ed.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb+2, face1))
118 ed.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2, pb))
119 ed.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2, pb+2))
120 ed.StyleSetSpec(4, "face:%s,size:%d" % (face1, pb-1))
121
122
123 # now set some text to those styles... Normally this would be
124 # done in an event handler that happens when text needs displayed.
125 ed.StartStyling(98, 0xff)
126 ed.SetStyling(6, 1) # set style for 6 characters using style 1
127
128 ed.StartStyling(190, 0xff)
129 ed.SetStyling(20, 2)
130
131 ed.StartStyling(310, 0xff)
132 ed.SetStyling(4, 3)
133 ed.SetStyling(2, 0)
134 ed.SetStyling(10, 4)
135
136
137 # line numbers in the margin
138 ed.SetMarginType(0, wxSTC_MARGIN_NUMBER)
139 ed.SetMarginWidth(0, 22)
140 ed.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "size:%d,face:%s" % (pb, face1))
141
142 # setup some markers
143 ed.SetMarginType(1, wxSTC_MARGIN_SYMBOL)
144 ed.MarkerDefine(0, wxSTC_MARK_ROUNDRECT, "#CCFF00", "RED")
145 ed.MarkerDefine(1, wxSTC_MARK_CIRCLE, "FOREST GREEN", "SIENNA")
146 ed.MarkerDefine(2, wxSTC_MARK_SHORTARROW, "blue", "blue")
147 ed.MarkerDefine(3, wxSTC_MARK_ARROW, "#00FF00", "#00FF00")
148
149 # put some markers on some lines
150 ed.MarkerAdd(17, 0)
151 ed.MarkerAdd(18, 1)
152 ed.MarkerAdd(19, 2)
153 ed.MarkerAdd(20, 3)
154 ed.MarkerAdd(20, 0)
155
156
157 # and finally, an indicator or two
158 ed.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE)
159 ed.IndicatorSetForeground(0, wxRED)
160 ed.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL)
161 ed.IndicatorSetForeground(1, wxBLUE)
162 ed.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE)
163 ed.IndicatorSetForeground(2, wxRED)
164
165 ed.StartStyling(836, wxSTC_INDICS_MASK)
166 ed.SetStyling(10, wxSTC_INDIC0_MASK)
167 ed.SetStyling(10, wxSTC_INDIC1_MASK)
168 ed.SetStyling(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK)
169
170 # some test stuff...
171 if 1:
172 print "GetTextLength(): ", ed.GetTextLength(), len(ed.GetText())
173 print "GetText(): ", repr(ed.GetText())
174 print
175 print "GetStyledText(98, 104): ", repr(ed.GetStyledText(98, 104)), len(ed.GetStyledText(98, 104))
176 print
177 print "GetCurLine(): ", repr(ed.GetCurLine())
178 ed.GotoPos(5)
179 print "GetCurLine(): ", repr(ed.GetCurLine())
180 print
181 print "GetLine(1): ", repr(ed.GetLine(1))
182 print
183 ed.SetSelection(25, 35)
184 print "GetSelectedText(): ", repr(ed.GetSelectedText())
185 print "GetTextRange(25, 35): ", repr(ed.GetTextRange(25, 35))
186
187
188 ed.GotoPos(0)
189
190 return p
191
192
193
194 #----------------------------------------------------------------------
195
196
197 overview = """\
198 <html><body>
199 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
200 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
201 be helpful.
202 </body><html>
203 """
204
205
206
207 if __name__ == '__main__':
208 import sys
209 app = wxPySimpleApp()
210 frame = wxFrame(None, -1, "Tester...", size=(640, 480))
211 win = runTest(frame, frame, sys.stdout)
212 frame.Show(true)
213 app.MainLoop()
214
215