]>
Commit | Line | Data |
---|---|---|
f97d84a6 RD |
1 | //////////////////////////////////////////////////////////////////////////// |
2 | // Name: stc.cpp | |
3 | // Purpose: A wxWindows implementation of Scintilla. This class is the | |
4 | // one meant to be used directly by wx applications. It does not | |
5 | // derive directly from the Scintilla classes, but instead | |
6 | // delegates most things to the real Scintilla class. | |
7 | // This allows the use of Scintilla without polluting the | |
8 | // namespace with all the classes and identifiers from Scintilla. | |
9 | // | |
10 | // Author: Robin Dunn | |
11 | // | |
12 | // Created: 13-Jan-2000 | |
13 | // RCS-ID: $Id$ | |
14 | // Copyright: (c) 2000 by Total Control Software | |
15 | // Licence: wxWindows license | |
16 | ///////////////////////////////////////////////////////////////////////////// | |
17 | ||
18 | #include <ctype.h> | |
19 | ||
20 | #include "wx/stc/stc.h" | |
21 | #include "ScintillaWX.h" | |
22 | ||
23 | #include <wx/tokenzr.h> | |
24 | ||
25 | // The following code forces a reference to all of the Scintilla lexers. | |
26 | // If we don't do something like this, then the linker tends to "optimize" | |
27 | // them away. (eric@sourcegear.com) | |
28 | ||
29 | int wxForceScintillaLexers(void) | |
30 | { | |
65ec6247 RD |
31 | extern LexerModule lmAda; |
32 | extern LexerModule lmAVE; | |
33 | extern LexerModule lmConf; | |
f97d84a6 | 34 | extern LexerModule lmCPP; |
b8b0e402 | 35 | extern LexerModule lmNncrontab; |
65ec6247 | 36 | extern LexerModule lmEiffel; |
f97d84a6 | 37 | extern LexerModule lmHTML; |
65ec6247 RD |
38 | extern LexerModule lmLISP; |
39 | extern LexerModule lmLua; | |
40 | extern LexerModule lmBatch; // In LexOthers.cxx | |
41 | extern LexerModule lmPascal; | |
f97d84a6 RD |
42 | extern LexerModule lmPerl; |
43 | extern LexerModule lmPython; | |
65ec6247 | 44 | extern LexerModule lmRuby; |
f97d84a6 RD |
45 | extern LexerModule lmSQL; |
46 | extern LexerModule lmVB; | |
47 | ||
65ec6247 RD |
48 | if ( &lmAda |
49 | && &lmAVE | |
50 | && &lmConf | |
51 | && &lmCPP | |
b8b0e402 | 52 | && &lmNncrontab |
65ec6247 RD |
53 | && &lmEiffel |
54 | && &lmHTML | |
55 | && &lmLISP | |
56 | && &lmLua | |
57 | && &lmBatch | |
58 | && &lmPascal | |
59 | && &lmPerl | |
60 | && &lmPython | |
61 | && &lmRuby | |
62 | && &lmSQL | |
63 | && &lmVB ) | |
f97d84a6 RD |
64 | { |
65 | return 1; | |
66 | } | |
67 | else | |
68 | { | |
69 | return 0; | |
70 | } | |
71 | } | |
72 | ||
73 | //---------------------------------------------------------------------- | |
74 | ||
75 | const wxChar* wxSTCNameStr = "stcwindow"; | |
76 | ||
d25f5fbb RD |
77 | DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE ) |
78 | DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED ) | |
79 | DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED ) | |
d25f5fbb RD |
80 | DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED ) |
81 | DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT ) | |
82 | DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT ) | |
65ec6247 | 83 | DEFINE_EVENT_TYPE( wxEVT_STC_KEY ) |
d25f5fbb | 84 | DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK ) |
65ec6247 | 85 | DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI ) |
d25f5fbb | 86 | DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED ) |
d25f5fbb RD |
87 | DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD ) |
88 | DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK ) | |
89 | DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN ) | |
90 | DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED ) | |
65ec6247 RD |
91 | DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED ) |
92 | DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION ) | |
93 | DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED ) | |
94 | DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART ) | |
95 | DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND ) | |
1bc32508 | 96 | #if wxUSE_DRAG_AND_DROP |
a29a241f RD |
97 | DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG ) |
98 | DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER ) | |
99 | DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP ) | |
1bc32508 | 100 | #endif |
d25f5fbb RD |
101 | |
102 | ||
f97d84a6 RD |
103 | BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl) |
104 | EVT_PAINT (wxStyledTextCtrl::OnPaint) | |
105 | EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin) | |
106 | EVT_SIZE (wxStyledTextCtrl::OnSize) | |
107 | EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown) | |
4ceb1196 RD |
108 | #ifdef __WXMSW__ |
109 | // Let Scintilla see the double click as a second click | |
110 | EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown) | |
111 | #endif | |
f97d84a6 RD |
112 | EVT_MOTION (wxStyledTextCtrl::OnMouseMove) |
113 | EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp) | |
65ec6247 | 114 | EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu) |
37d62433 | 115 | EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel) |
f97d84a6 RD |
116 | EVT_CHAR (wxStyledTextCtrl::OnChar) |
117 | EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown) | |
118 | EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus) | |
119 | EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus) | |
120 | EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged) | |
121 | EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground) | |
122 | EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu) | |
123 | EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox) | |
124 | END_EVENT_TABLE() | |
125 | ||
126 | ||
127 | IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl) | |
128 | IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent) | |
129 | ||
130 | //---------------------------------------------------------------------- | |
131 | // Constructor and Destructor | |
132 | ||
133 | wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent, | |
134 | wxWindowID id, | |
135 | const wxPoint& pos, | |
136 | const wxSize& size, | |
137 | long style, | |
138 | const wxString& name) : | |
139 | wxControl(parent, id, pos, size, | |
140 | style | wxVSCROLL | wxHSCROLL | wxWANTS_CHARS | wxCLIP_CHILDREN, | |
141 | wxDefaultValidator, name) | |
142 | { | |
143 | m_swx = new ScintillaWX(this); | |
144 | m_stopWatch.Start(); | |
d6582821 | 145 | m_lastKeyDownConsumed = FALSE; |
f97d84a6 RD |
146 | } |
147 | ||
148 | ||
149 | wxStyledTextCtrl::~wxStyledTextCtrl() { | |
150 | delete m_swx; | |
151 | } | |
152 | ||
153 | ||
154 | //---------------------------------------------------------------------- | |
155 | ||
156 | long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) { | |
157 | ||
158 | return m_swx->WndProc(msg, wp, lp); | |
159 | } | |
160 | ||
161 | ||
162 | #ifdef MAKELONG | |
163 | #undef MAKELONG | |
164 | #endif | |
165 | ||
166 | #define MAKELONG(a, b) ((a) | ((b) << 16)) | |
167 | ||
168 | ||
169 | static long wxColourAsLong(const wxColour& co) { | |
170 | return (((long)co.Blue() << 16) | | |
171 | ((long)co.Green() << 8) | | |
172 | ((long)co.Red())); | |
173 | } | |
174 | ||
175 | static wxColour wxColourFromLong(long c) { | |
176 | wxColour clr; | |
177 | clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff); | |
178 | return clr; | |
179 | } | |
180 | ||
181 | ||
182 | static wxColour wxColourFromSpec(const wxString& spec) { | |
183 | // spec should be #RRGGBB | |
184 | char* junk; | |
185 | int red = strtol(spec.Mid(1,2), &junk, 16); | |
186 | int green = strtol(spec.Mid(3,2), &junk, 16); | |
187 | int blue = strtol(spec.Mid(5,2), &junk, 16); | |
188 | return wxColour(red, green, blue); | |
189 | } | |
190 | ||
191 | ||
192 | //---------------------------------------------------------------------- | |
193 | // BEGIN generated section. The following code is automatically generated | |
194 | // by gen_iface.py from the contents of Scintilla.iface. Do not edit | |
195 | // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate. | |
196 | ||
197 | %(METHOD_IMPS)s | |
198 | ||
199 | // END of generated section | |
200 | //---------------------------------------------------------------------- | |
201 | ||
202 | ||
203 | // Returns the line number of the line with the caret. | |
204 | int wxStyledTextCtrl::GetCurrentLine() { | |
205 | int line = LineFromPosition(GetCurrentPos()); | |
206 | return line; | |
207 | } | |
208 | ||
209 | ||
210 | // Extract style settings from a spec-string which is composed of one or | |
211 | // more of the following comma separated elements: | |
212 | // | |
213 | // bold turns on bold | |
214 | // italic turns on italics | |
215 | // fore:#RRGGBB sets the foreground colour | |
216 | // back:#RRGGBB sets the background colour | |
217 | // face:[facename] sets the font face name to use | |
218 | // size:[num] sets the font size in points | |
219 | // eol turns on eol filling | |
220 | // underline turns on underlining | |
221 | // | |
222 | void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) { | |
223 | ||
224 | wxStringTokenizer tkz(spec, ","); | |
225 | while (tkz.HasMoreTokens()) { | |
226 | wxString token = tkz.GetNextToken(); | |
227 | ||
228 | wxString option = token.BeforeFirst(':'); | |
229 | wxString val = token.AfterFirst(':'); | |
230 | ||
231 | if (option == "bold") | |
232 | StyleSetBold(styleNum, true); | |
233 | ||
234 | else if (option == "italic") | |
235 | StyleSetItalic(styleNum, true); | |
236 | ||
237 | else if (option == "underline") | |
238 | StyleSetUnderline(styleNum, true); | |
239 | ||
240 | else if (option == "eol") | |
241 | StyleSetEOLFilled(styleNum, true); | |
242 | ||
243 | else if (option == "size") { | |
244 | long points; | |
245 | if (val.ToLong(&points)) | |
246 | StyleSetSize(styleNum, points); | |
247 | } | |
248 | ||
249 | else if (option == "face") | |
250 | StyleSetFaceName(styleNum, val); | |
251 | ||
252 | else if (option == "fore") | |
253 | StyleSetForeground(styleNum, wxColourFromSpec(val)); | |
254 | ||
255 | else if (option == "back") | |
256 | StyleSetBackground(styleNum, wxColourFromSpec(val)); | |
257 | } | |
258 | } | |
259 | ||
260 | ||
261 | // Set style size, face, bold, italic, and underline attributes from | |
262 | // a wxFont's attributes. | |
263 | void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) { | |
264 | int size = font.GetPointSize(); | |
265 | wxString faceName = font.GetFaceName(); | |
266 | bool bold = font.GetWeight() == wxBOLD; | |
267 | bool italic = font.GetStyle() != wxNORMAL; | |
268 | bool under = font.GetUnderlined(); | |
269 | ||
270 | // TODO: add encoding/charset mapping | |
271 | StyleSetFontAttr(styleNum, size, faceName, bold, italic, under); | |
272 | } | |
273 | ||
274 | // Set all font style attributes at once. | |
275 | void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size, | |
276 | const wxString& faceName, | |
277 | bool bold, bool italic, | |
278 | bool underline) { | |
279 | StyleSetSize(styleNum, size); | |
280 | StyleSetFaceName(styleNum, faceName); | |
281 | StyleSetBold(styleNum, bold); | |
282 | StyleSetItalic(styleNum, italic); | |
283 | StyleSetUnderline(styleNum, underline); | |
284 | ||
285 | // TODO: add encoding/charset mapping | |
286 | } | |
287 | ||
288 | ||
289 | // Perform one of the operations defined by the wxSTC_CMD_* constants. | |
290 | void wxStyledTextCtrl::CmdKeyExecute(int cmd) { | |
291 | SendMsg(cmd); | |
292 | } | |
293 | ||
294 | ||
295 | // Set the left and right margin in the edit area, measured in pixels. | |
296 | void wxStyledTextCtrl::SetMargins(int left, int right) { | |
297 | SetMarginLeft(left); | |
298 | SetMarginRight(right); | |
299 | } | |
300 | ||
301 | ||
302 | // Retrieve the start and end positions of the current selection. | |
303 | void wxStyledTextCtrl::GetSelection(int* startPos, int* endPos) { | |
304 | if (startPos != NULL) | |
305 | *startPos = SendMsg(SCI_GETSELECTIONSTART); | |
306 | if (endPos != NULL) | |
307 | *endPos = SendMsg(SCI_GETSELECTIONEND); | |
308 | } | |
309 | ||
310 | ||
311 | // Retrieve the point in the window where a position is displayed. | |
312 | wxPoint wxStyledTextCtrl::PointFromPosition(int pos) { | |
313 | int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos); | |
314 | int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos); | |
315 | return wxPoint(x, y); | |
316 | } | |
317 | ||
318 | // Scroll enough to make the given line visible | |
319 | void wxStyledTextCtrl::ScrollToLine(int line) { | |
320 | m_swx->DoScrollToLine(line); | |
321 | } | |
322 | ||
323 | ||
324 | // Scroll enough to make the given column visible | |
325 | void wxStyledTextCtrl::ScrollToColumn(int column) { | |
326 | m_swx->DoScrollToColumn(column); | |
327 | } | |
328 | ||
329 | ||
330 | ||
331 | //---------------------------------------------------------------------- | |
332 | // Event handlers | |
333 | ||
334 | void wxStyledTextCtrl::OnPaint(wxPaintEvent& evt) { | |
335 | wxPaintDC dc(this); | |
336 | wxRegion region = GetUpdateRegion(); | |
337 | ||
338 | m_swx->DoPaint(&dc, region.GetBox()); | |
339 | } | |
340 | ||
341 | void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) { | |
342 | if (evt.GetOrientation() == wxHORIZONTAL) | |
343 | m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition()); | |
344 | else | |
345 | m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition()); | |
346 | } | |
347 | ||
348 | void wxStyledTextCtrl::OnSize(wxSizeEvent& evt) { | |
349 | wxSize sz = GetClientSize(); | |
350 | m_swx->DoSize(sz.x, sz.y); | |
351 | } | |
352 | ||
353 | void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) { | |
354 | wxPoint pt = evt.GetPosition(); | |
355 | m_swx->DoButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(), | |
356 | evt.ShiftDown(), evt.ControlDown(), evt.AltDown()); | |
357 | } | |
358 | ||
359 | void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) { | |
360 | wxPoint pt = evt.GetPosition(); | |
361 | m_swx->DoButtonMove(Point(pt.x, pt.y)); | |
362 | } | |
363 | ||
364 | void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) { | |
365 | wxPoint pt = evt.GetPosition(); | |
366 | m_swx->DoButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(), | |
367 | evt.ControlDown()); | |
368 | } | |
369 | ||
370 | ||
65ec6247 | 371 | void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) { |
f97d84a6 | 372 | wxPoint pt = evt.GetPosition(); |
65ec6247 | 373 | ScreenToClient(&pt.x, &pt.y); |
f97d84a6 RD |
374 | m_swx->DoContextMenu(Point(pt.x, pt.y)); |
375 | } | |
376 | ||
37d62433 RD |
377 | |
378 | void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) { | |
379 | m_swx->DoMouseWheel(evt.GetWheelRotation(), | |
380 | evt.GetWheelDelta(), | |
65ec6247 RD |
381 | evt.GetLinesPerAction(), |
382 | evt.ControlDown()); | |
37d62433 RD |
383 | } |
384 | ||
385 | ||
f97d84a6 RD |
386 | void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) { |
387 | long key = evt.KeyCode(); | |
f3c2c221 RD |
388 | |
389 | // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n", | |
390 | // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown()); | |
391 | ||
392 | // AltGr keys??? | |
393 |