]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxStyledTextCtrl_1.py
fixed the last of the off-by-one errors (some are refixed, again...)
[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
36 if wxPlatform == '__WXMSW__':
37 face1 = 'Arial'
38 face2 = 'Times New Roman'
39 face3 = 'Courier New'
40 pb = 6
41 else:
42 face1 = 'Helvetica'
43 face2 = 'Times'
44 face3 = 'Courier'
45 pb = 10
46
47
48 #----------------------------------------------------------------------
49 # This shows how to catch the Modified event from the wxStyledTextCtrl
50
51 class MySTC(wxStyledTextCtrl):
52 def __init__(self, parent, ID, log):
53 wxStyledTextCtrl.__init__(self, parent, ID)
54 self.log = log
55
56 EVT_STC_MODIFIED(self, ID, self.OnModified)
57
58
59 def OnModified(self, evt):
60 self.log.write("""OnModified
61 Mod type: %s
62 At position: %d
63 Lines added: %d
64 Text Length: %d
65 Text: %s\n""" % ( self.transModType(evt.GetModificationType()),
66 evt.GetPosition(),
67 evt.GetLinesAdded(),
68 evt.GetLength(),
69 evt.GetText() ))
70
71 def transModType(self, modType):
72 st = ""
73 table = [(wxSTC_MOD_INSERTTEXT, "InsertText"),
74 (wxSTC_MOD_DELETETEXT, "DeleteText"),
75 (wxSTC_MOD_CHANGESTYLE, "ChangeStyle"),
76 (wxSTC_MOD_CHANGEFOLD, "ChangeFold"),
77 (wxSTC_PERFORMED_USER, "UserFlag"),
78 (wxSTC_PERFORMED_UNDO, "Undo"),
79 (wxSTC_PERFORMED_REDO, "Redo"),
80 (wxSTC_LASTSTEPINUNDOREDO, "Last-Undo/Redo"),
81 (wxSTC_MOD_CHANGEMARKER, "ChangeMarker"),
82 (wxSTC_MOD_BEFOREINSERT, "B4-Insert"),
83 (wxSTC_MOD_BEFOREDELETE, "B4-Delete")
84 ]
85
86 for flag,text in table:
87 if flag & modType:
88 st = st + text + " "
89
90 if not st:
91 st = 'UNKNOWN'
92
93 return st
94
95
96
97 #----------------------------------------------------------------------
98
99 _USE_PANEL = 1
100
101 def runTest(frame, nb, log):
102 if not _USE_PANEL:
103 ed = p = MySTC(nb, -1, log)
104
105 else:
106 p = wxPanel(nb, -1)
107 ed = MySTC(p, -1, log)
108 s = wxBoxSizer(wxHORIZONTAL)
109 s.Add(ed, 1, wxEXPAND)
110 p.SetSizer(s)
111 p.SetAutoLayout(true)
112
113 ed.SetText(demoText)
114 ed.EmptyUndoBuffer()
115
116 # make some styles
117 ed.StyleSetSpec(wxSTC_STYLE_DEFAULT, "size:%d,face:%s" % (pb+2, face3))
118 ed.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb+3, face1))
119 ed.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2, pb+2))
120 ed.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2, pb+3))
121 ed.StyleSetSpec(4, "face:%s,size:%d" % (face1, pb))
122
123
124 # now set some text to those styles... Normally this would be
125 # done in an event handler that happens when text needs displayed.
126 ed.StartStyling(98, 0xff)
127 ed.SetStyling(6, 1) # set style for 6 characters using style 1
128
129 ed.StartStyling(190, 0xff)
130 ed.SetStyling(20, 2)
131
132 ed.StartStyling(310, 0xff)
133 ed.SetStyling(4, 3)
134 ed.SetStyling(2, 0)
135 ed.SetStyling(10, 4)
136
137
138 # line numbers in the margin
139 ed.SetMarginType(0, wxSTC_MARGIN_NUMBER)
140 ed.SetMarginWidth(0, 22)
141 ed.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "size:%d,face:%s" % (pb, face1))
142
143 # setup some markers
144 ed.SetMarginType(1, wxSTC_MARGIN_SYMBOL)
145 ed.MarkerDefine(0, wxSTC_MARK_ROUNDRECT, "#CCFF00", "RED")
146 ed.MarkerDefine(1, wxSTC_MARK_CIRCLE, "FOREST GREEN", "SIENNA")
147 ed.MarkerDefine(2, wxSTC_MARK_SHORTARROW, "blue", "blue")
148 ed.MarkerDefine(3, wxSTC_MARK_ARROW, "#00FF00", "#00FF00")
149
150 # put some markers on some lines
151 ed.MarkerAdd(17, 0)
152 ed.MarkerAdd(18, 1)
153 ed.MarkerAdd(19, 2)
154 ed.MarkerAdd(20, 3)
155 ed.MarkerAdd(20, 0)
156
157
158 # and finally, an indicator or two
159 ed.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE)
160 ed.IndicatorSetForeground(0, wxRED)
161 ed.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL)
162 ed.IndicatorSetForeground(1, wxBLUE)
163 ed.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE)
164 ed.IndicatorSetForeground(2, wxRED)
165
166 ed.StartStyling(836, wxSTC_INDICS_MASK)
167 ed.SetStyling(10, wxSTC_INDIC0_MASK)
168 ed.SetStyling(10, wxSTC_INDIC1_MASK)
169 ed.SetStyling(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK)
170
171
172 return p
173
174
175
176 #----------------------------------------------------------------------
177
178
179 overview = """\
180 <html><body>
181 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a>
182 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
183 be helpful.
184 </body><html>
185 """
186
187
188
189 if __name__ == '__main__':
190 import sys
191 app = wxPySimpleApp()
192 frame = wxFrame(None, -1, "Tester...", size=(640, 480))
193 win = runTest(frame, frame, sys.stdout)
194 frame.Show(true)
195 app.MainLoop()
196
197