]>
Commit | Line | Data |
---|---|---|
f6bcfd97 BP |
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 | def runTest(frame, nb, log): | |
100 | ed = MySTC(nb, -1, log) | |
101 | ||
102 | ed.SetText(demoText) | |
103 | ed.EmptyUndoBuffer() | |
104 | ||
105 | # make some styles | |
106 | ed.StyleSetSpec(wxSTC_STYLE_DEFAULT, "size:%d,face:%s" % (pb+2, face3)) | |
107 | ed.StyleSetSpec(1, "size:%d,bold,face:%s,fore:#0000FF" % (pb+3, face1)) | |
108 | ed.StyleSetSpec(2, "face:%s,italic,fore:#FF0000,size:%d" % (face2, pb+2)) | |
109 | ed.StyleSetSpec(3, "face:%s,bold,size:%d" % (face2, pb+3)) | |
110 | ed.StyleSetSpec(4, "face:%s,size:%d" % (face1, pb)) | |
111 | ||
112 | ||
113 | # now set some text to those styles... Normally this would be | |
114 | # done in an event handler that happens when text needs displayed. | |
115 | ed.StartStyling(98, 0xff) | |
c368d904 | 116 | ed.SetStyling(6, 1) # set style for 6 characters using style 1 |
f6bcfd97 BP |
117 | |
118 | ed.StartStyling(190, 0xff) | |
c368d904 | 119 | ed.SetStyling(20, 2) |
f6bcfd97 BP |
120 | |
121 | ed.StartStyling(310, 0xff) | |
c368d904 RD |
122 | ed.SetStyling(4, 3) |
123 | ed.SetStyling(2, 0) | |
124 | ed.SetStyling(10, 4) | |
f6bcfd97 BP |
125 | |
126 | ||
127 | # line numbers in the margin | |
128 | ed.SetMarginType(0, wxSTC_MARGIN_NUMBER) | |
129 | ed.SetMarginWidth(0, 22) | |
130 | ed.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "size:%d,face:%s" % (pb, face1)) | |
131 | ||
132 | # setup some markers | |
133 | ed.SetMarginType(1, wxSTC_MARGIN_SYMBOL) | |
134 | ed.MarkerDefine(0, wxSTC_MARK_ROUNDRECT, "#CCFF00", "RED") | |
135 | ed.MarkerDefine(1, wxSTC_MARK_CIRCLE, "FOREST GREEN", "SIENNA") | |
136 | ed.MarkerDefine(2, wxSTC_MARK_SHORTARROW, "blue", "blue") | |
137 | ed.MarkerDefine(3, wxSTC_MARK_ARROW, "#00FF00", "#00FF00") | |
138 | ||
139 | # put some markers on some lines | |
140 | ed.MarkerAdd(17, 0) | |
141 | ed.MarkerAdd(18, 1) | |
142 | ed.MarkerAdd(19, 2) | |
143 | ed.MarkerAdd(20, 3) | |
144 | ed.MarkerAdd(20, 0) | |
145 | ||
146 | ||
147 | # and finally, an indicator or two | |
148 | ed.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE) | |
c368d904 | 149 | ed.IndicatorSetForeground(0, wxRED) |
f6bcfd97 | 150 | ed.IndicatorSetStyle(1, wxSTC_INDIC_DIAGONAL) |
c368d904 | 151 | ed.IndicatorSetForeground(1, wxBLUE) |
f6bcfd97 | 152 | ed.IndicatorSetStyle(2, wxSTC_INDIC_STRIKE) |
c368d904 | 153 | ed.IndicatorSetForeground(2, wxRED) |
f6bcfd97 BP |
154 | |
155 | ed.StartStyling(836, wxSTC_INDICS_MASK) | |
c368d904 RD |
156 | ed.SetStyling(10, wxSTC_INDIC0_MASK) |
157 | ed.SetStyling(10, wxSTC_INDIC1_MASK) | |
158 | ed.SetStyling(10, wxSTC_INDIC2_MASK | wxSTC_INDIC1_MASK) | |
f6bcfd97 BP |
159 | |
160 | ||
161 | return ed | |
162 | ||
163 | ||
164 | ||
165 | #---------------------------------------------------------------------- | |
166 | ||
167 | ||
168 | overview = """\ | |
169 | <html><body> | |
170 | Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a> | |
171 | and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should | |
172 | be helpful. | |
173 | </body><html> | |
174 | """ | |
175 | ||
176 | ||
177 | ||
178 | if __name__ == '__main__': | |
179 | import sys | |
180 | app = wxPySimpleApp() | |
181 | frame = wxFrame(None, -1, "Tester...", size=(640, 480)) | |
182 | win = runTest(frame, frame, sys.stdout) | |
183 | frame.Show(true) | |
184 | app.MainLoop() | |
185 | ||
186 |