]>
Commit | Line | Data |
---|---|---|
f97d84a6 RD |
1 | //////////////////////////////////////////////////////////////////////////// |
2 | // Name: stc.cpp | |
be5a51fb | 3 | // Purpose: A wxWidgets implementation of Scintilla. This class is the |
f97d84a6 RD |
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 | ||
9e730a78 | 20 | #include <wx/wx.h> |
f97d84a6 | 21 | #include <wx/tokenzr.h> |
9e730a78 RD |
22 | #include <wx/mstream.h> |
23 | #include <wx/image.h> | |
51566b0b | 24 | #include <wx/file.h> |
f97d84a6 | 25 | |
f9ee2e27 RD |
26 | #include "wx/stc/stc.h" |
27 | #include "ScintillaWX.h" | |
f97d84a6 RD |
28 | |
29 | //---------------------------------------------------------------------- | |
30 | ||
10ef30eb | 31 | const wxChar* wxSTCNameStr = wxT("stcwindow"); |
f97d84a6 | 32 | |
451c5cc7 RD |
33 | #ifdef MAKELONG |
34 | #undef MAKELONG | |
35 | #endif | |
36 | ||
37 | #define MAKELONG(a, b) ((a) | ((b) << 16)) | |
38 | ||
39 | ||
40 | static long wxColourAsLong(const wxColour& co) { | |
41 | return (((long)co.Blue() << 16) | | |
42 | ((long)co.Green() << 8) | | |
43 | ((long)co.Red())); | |
44 | } | |
45 | ||
46 | static wxColour wxColourFromLong(long c) { | |
47 | wxColour clr; | |
a5b274d7 WS |
48 | clr.Set((unsigned char)(c & 0xff), |
49 | (unsigned char)((c >> 8) & 0xff), | |
50 | (unsigned char)((c >> 16) & 0xff)); | |
451c5cc7 RD |
51 | return clr; |
52 | } | |
53 | ||
54 | ||
55 | static wxColour wxColourFromSpec(const wxString& spec) { | |
5ee1d760 RD |
56 | // spec should be a colour name or "#RRGGBB" |
57 | if (spec.GetChar(0) == wxT('#')) { | |
dc8005e2 | 58 | |
5ee1d760 RD |
59 | long red, green, blue; |
60 | red = green = blue = 0; | |
61 | spec.Mid(1,2).ToLong(&red, 16); | |
62 | spec.Mid(3,2).ToLong(&green, 16); | |
63 | spec.Mid(5,2).ToLong(&blue, 16); | |
a5b274d7 WS |
64 | return wxColour((unsigned char)red, |
65 | (unsigned char)green, | |
66 | (unsigned char)blue); | |
5ee1d760 RD |
67 | } |
68 | else | |
69 | return wxColour(spec); | |
451c5cc7 RD |
70 | } |
71 | ||
72 | //---------------------------------------------------------------------- | |
73 | ||
d25f5fbb RD |
74 | DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE ) |
75 | DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED ) | |
76 | DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED ) | |
d25f5fbb RD |
77 | DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED ) |
78 | DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT ) | |
79 | DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT ) | |
65ec6247 | 80 | DEFINE_EVENT_TYPE( wxEVT_STC_KEY ) |
d25f5fbb | 81 | DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK ) |
65ec6247 | 82 | DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI ) |
d25f5fbb | 83 | DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED ) |
d25f5fbb RD |
84 | DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD ) |
85 | DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK ) | |
86 | DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN ) | |
65ec6247 RD |
87 | DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED ) |
88 | DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION ) | |
89 | DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED ) | |
90 | DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART ) | |
91 | DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND ) | |
a29a241f RD |
92 | DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG ) |
93 | DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER ) | |
94 | DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP ) | |
a834585d | 95 | DEFINE_EVENT_TYPE( wxEVT_STC_ZOOM ) |
9e730a78 RD |
96 | DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_CLICK ) |
97 | DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_DCLICK ) | |
98 | DEFINE_EVENT_TYPE( wxEVT_STC_CALLTIP_CLICK ) | |
99 | ||
d25f5fbb RD |
100 | |
101 | ||
f97d84a6 RD |
102 | BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl) |
103 | EVT_PAINT (wxStyledTextCtrl::OnPaint) | |
104 | EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin) | |
5fa4613c | 105 | EVT_SCROLL (wxStyledTextCtrl::OnScroll) |
f97d84a6 RD |
106 | EVT_SIZE (wxStyledTextCtrl::OnSize) |
107 | EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown) | |
4ceb1196 RD |
108 | // Let Scintilla see the double click as a second click |
109 | EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown) | |
f97d84a6 RD |
110 | EVT_MOTION (wxStyledTextCtrl::OnMouseMove) |
111 | EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp) | |
451c5cc7 | 112 | #if defined(__WXGTK__) || defined(__WXMAC__) |
ddf2da08 RD |
113 | EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp) |
114 | #else | |
65ec6247 | 115 | EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu) |
ddf2da08 | 116 | #endif |
37d62433 | 117 | EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel) |
2b5f62a0 | 118 | EVT_MIDDLE_UP (wxStyledTextCtrl::OnMouseMiddleUp) |
f97d84a6 RD |
119 | EVT_CHAR (wxStyledTextCtrl::OnChar) |
120 | EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown) | |
121 | EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus) | |
122 | EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus) | |
123 | EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged) | |
124 | EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground) | |
dd4aa550 | 125 | EVT_MENU_RANGE (10, 16, wxStyledTextCtrl::OnMenu) |
dc8005e2 | 126 | EVT_LISTBOX_DCLICK (wxID_ANY, wxStyledTextCtrl::OnListBox) |
f97d84a6 RD |
127 | END_EVENT_TABLE() |
128 | ||
129 | ||
130 | IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl) | |
131 | IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent) | |
132 | ||
40716a51 | 133 | #ifdef LINK_LEXERS |
1a2fb4cd | 134 | // forces the linking of the lexer modules |
a834585d | 135 | int Scintilla_LinkLexers(); |
40716a51 | 136 | #endif |
1a2fb4cd | 137 | |
f97d84a6 RD |
138 | //---------------------------------------------------------------------- |
139 | // Constructor and Destructor | |
140 | ||
141 | wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent, | |
142 | wxWindowID id, | |
143 | const wxPoint& pos, | |
144 | const wxSize& size, | |
145 | long style, | |
39c0acb6 RD |
146 | const wxString& name) |
147 | { | |
148 | m_swx = NULL; | |
149 | Create(parent, id, pos, size, style, name); | |
150 | } | |
151 | ||
152 | ||
a48cb415 RD |
153 | bool wxStyledTextCtrl::Create(wxWindow *parent, |
154 | wxWindowID id, | |
155 | const wxPoint& pos, | |
156 | const wxSize& size, | |
157 | long style, | |
158 | const wxString& name) | |
f97d84a6 | 159 | { |
2659dad3 RD |
160 | #ifdef __WXMAC__ |
161 | style |= wxVSCROLL | wxHSCROLL; | |
162 | #endif | |
a48cb415 RD |
163 | if (!wxControl::Create(parent, id, pos, size, |
164 | style | wxWANTS_CHARS | wxCLIP_CHILDREN, | |
165 | wxDefaultValidator, name)) | |
166 | return false; | |
39c0acb6 | 167 | |
40716a51 | 168 | #ifdef LINK_LEXERS |
a834585d | 169 | Scintilla_LinkLexers(); |
40716a51 | 170 | #endif |
f97d84a6 RD |
171 | m_swx = new ScintillaWX(this); |
172 | m_stopWatch.Start(); | |
dc8005e2 | 173 | m_lastKeyDownConsumed = false; |
5fa4613c RD |
174 | m_vScrollBar = NULL; |
175 | m_hScrollBar = NULL; | |
10ef30eb RD |
176 | #if wxUSE_UNICODE |
177 | // Put Scintilla into unicode (UTF-8) mode | |
178 | SetCodePage(wxSTC_CP_UTF8); | |
179 | #endif | |
8ae4f086 RD |
180 | |
181 | SetBestFittingSize(size); | |
f9ee2e27 RD |
182 | |
183 | // Reduces flicker on GTK+/X11 | |
184 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
a48cb415 | 185 | return true; |
f97d84a6 RD |
186 | } |
187 | ||
188 | ||
189 | wxStyledTextCtrl::~wxStyledTextCtrl() { | |
190 | delete m_swx; | |
191 | } | |
192 | ||
193 | ||
194 | //---------------------------------------------------------------------- | |
195 | ||
196 | long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) { | |
197 | ||
198 | return m_swx->WndProc(msg, wp, lp); | |
199 | } | |
200 | ||
ccfc3219 RD |
201 | //---------------------------------------------------------------------- |
202 | ||
203 | // Set the vertical scrollbar to use instead of the ont that's built-in. | |
204 | void wxStyledTextCtrl::SetVScrollBar(wxScrollBar* bar) { | |
205 | m_vScrollBar = bar; | |
206 | if (bar != NULL) { | |
207 | // ensure that the built-in scrollbar is not visible | |
208 | SetScrollbar(wxVERTICAL, 0, 0, 0); | |
209 | } | |
210 | } | |
f97d84a6 | 211 | |
f97d84a6 | 212 | |
ccfc3219 RD |
213 | // Set the horizontal scrollbar to use instead of the ont that's built-in. |
214 | void wxStyledTextCtrl::SetHScrollBar(wxScrollBar* bar) { | |
215 | m_hScrollBar = bar; | |
216 | if (bar != NULL) { | |
217 | // ensure that the built-in scrollbar is not visible | |
218 | SetScrollbar(wxHORIZONTAL, 0, 0, 0); | |
219 | } | |
220 | } | |
221 | ||
f97d84a6 RD |
222 | //---------------------------------------------------------------------- |
223 | // BEGIN generated section. The following code is automatically generated | |
224 | // by gen_iface.py from the contents of Scintilla.iface. Do not edit | |
225 | // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate. | |
226 | ||
227 | %(METHOD_IMPS)s | |
228 | ||
229 | // END of generated section | |
230 | //---------------------------------------------------------------------- | |
231 | ||
232 | ||
233 | // Returns the line number of the line with the caret. | |
234 | int wxStyledTextCtrl::GetCurrentLine() { | |
235 | int line = LineFromPosition(GetCurrentPos()); | |
236 | return line; | |
237 | } | |
238 | ||
239 | ||
240 | // Extract style settings from a spec-string which is composed of one or | |
241 | // more of the following comma separated elements: | |
242 | // | |
243 | // bold turns on bold | |
244 | // italic turns on italics | |
5ee1d760 RD |
245 | // fore:[name or #RRGGBB] sets the foreground colour |
246 | // back:[name or #RRGGBB] sets the background colour | |
f97d84a6 RD |
247 | // face:[facename] sets the font face name to use |
248 | // size:[num] sets the font size in points | |
249 | // eol turns on eol filling | |
250 | // underline turns on underlining | |
251 | // | |
252 | void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) { | |
253 | ||
451c5cc7 | 254 | wxStringTokenizer tkz(spec, wxT(",")); |
f97d84a6 RD |
255 | while (tkz.HasMoreTokens()) { |
256 | wxString token = tkz.GetNextToken(); | |
257 | ||
258 | wxString option = token.BeforeFirst(':'); | |
259 | wxString val = token.AfterFirst(':'); | |
260 | ||
451c5cc7 | 261 | if (option == wxT("bold")) |
f97d84a6 RD |
262 | StyleSetBold(styleNum, true); |
263 | ||
451c5cc7 | 264 | else if (option == wxT("italic")) |
f97d84a6 RD |
265 | StyleSetItalic(styleNum, true); |
266 | ||
451c5cc7 | 267 | else if (option == wxT("underline")) |
f97d84a6 RD |
268 | StyleSetUnderline(styleNum, true); |
269 | ||
451c5cc7 | 270 | else if (option == wxT("eol")) |
f97d84a6 RD |
271 | StyleSetEOLFilled(styleNum, true); |
272 | ||
451c5cc7 | 273 | else if (option == wxT("size")) { |
f97d84a6 RD |
274 | long points; |
275 | if (val.ToLong(&points)) | |
276 | StyleSetSize(styleNum, points); | |
277 | } | |
278 | ||
451c5cc7 | 279 | else if (option == wxT("face")) |
f97d84a6 RD |
280 | StyleSetFaceName(styleNum, val); |
281 | ||
451c5cc7 | 282 | else if (option == wxT("fore")) |
f97d84a6 RD |
283 | StyleSetForeground(styleNum, wxColourFromSpec(val)); |
284 | ||
451c5cc7 | 285 | else if (option == wxT("back")) |
f97d84a6 RD |
286 | StyleSetBackground(styleNum, wxColourFromSpec(val)); |
287 | } | |
288 | } | |
289 | ||
290 | ||
291 | // Set style size, face, bold, italic, and underline attributes from | |
292 | // a wxFont's attributes. | |
293 | void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) { | |
7475e814 RD |
294 | #ifdef __WXGTK__ |
295 | // Ensure that the native font is initialized | |
296 | int x, y; | |
297 | GetTextExtent(wxT("X"), &x, &y, NULL, NULL, &font); | |
298 | #endif | |
f97d84a6 RD |
299 | int size = font.GetPointSize(); |
300 | wxString faceName = font.GetFaceName(); | |
301 | bool bold = font.GetWeight() == wxBOLD; | |
302 | bool italic = font.GetStyle() != wxNORMAL; | |
303 | bool under = font.GetUnderlined(); | |
304 | ||
305 | // TODO: add encoding/charset mapping | |
306 | StyleSetFontAttr(styleNum, size, faceName, bold, italic, under); | |
307 | } | |
308 | ||
309 | // Set all font style attributes at once. | |
310 | void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size, | |
311 | const wxString& faceName, | |
312 | bool bold, bool italic, | |
313 | bool underline) { | |
314 | StyleSetSize(styleNum, size); | |
315 | StyleSetFaceName(styleNum, faceName); | |
316 | StyleSetBold(styleNum, bold); | |
317 | StyleSetItalic(styleNum, italic); | |
318 | StyleSetUnderline(styleNum, underline); | |
319 | ||
320 | // TODO: add encoding/charset mapping | |
321 | } | |
322 | ||
323 | ||
324 | // Perform one of the operations defined by the wxSTC_CMD_* constants. | |
325 | void wxStyledTextCtrl::CmdKeyExecute(int cmd) { | |
326 | SendMsg(cmd); | |
327 | } | |
328 | ||
329 | ||
330 | // Set the left and right margin in the edit area, measured in pixels. | |
331 | void wxStyledTextCtrl::SetMargins(int left, int right) { | |
332 | SetMarginLeft(left); | |
333 | SetMarginRight(right); | |
334 | } | |
335 | ||
336 | ||
337 | // Retrieve the start and end positions of the current selection. | |
338 | void wxStyledTextCtrl::GetSelection(int* startPos, int* endPos) { | |
339 | if (startPos != NULL) | |
340 | *startPos = SendMsg(SCI_GETSELECTIONSTART); | |
341 | if (endPos != NULL) | |
342 | *endPos = SendMsg(SCI_GETSELECTIONEND); | |
343 | } | |
344 | ||
345 | ||
346 | // Retrieve the point in the window where a position is displayed. | |
347 | wxPoint wxStyledTextCtrl::PointFromPosition(int pos) { | |
348 | int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos); | |
349 | int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos); | |
350 | return wxPoint(x, y); | |
351 | } | |
352 | ||
353 | // Scroll enough to make the given line visible | |
354 | void wxStyledTextCtrl::ScrollToLine(int line) { | |
355 | m_swx->DoScrollToLine(line); | |
356 | } | |
357 | ||
358 | ||
359 | // Scroll enough to make the given column visible | |
360 | void wxStyledTextCtrl::ScrollToColumn(int column) { | |
361 | m_swx->DoScrollToColumn(column); | |
362 | } | |
363 | ||
364 | ||
51566b0b RD |
365 | bool wxStyledTextCtrl::SaveFile(const wxString& filename) |
366 | { | |
367 | wxFile file(filename, wxFile::write); | |
368 | ||
369 | if (!file.IsOpened()) | |
dc8005e2 | 370 | return false; |
51566b0b | 371 | |
4a65f2c8 | 372 | bool success = file.Write(GetText(), *wxConvCurrent); |
51566b0b RD |
373 | |
374 | if (success) | |
375 | SetSavePoint(); | |
376 | ||
377 | return success; | |
378 | } | |
379 | ||
380 | bool wxStyledTextCtrl::LoadFile(const wxString& filename) | |
381 | { | |
041973c5 | 382 | bool success = false; |
51566b0b RD |
383 | wxFile file(filename, wxFile::read); |
384 | ||
041973c5 | 385 | if (file.IsOpened()) |
51566b0b | 386 | { |
041973c5 | 387 | wxString contents; |
a5b274d7 WS |
388 | // get the file size (assume it is not huge file...) |
389 | ssize_t len = (ssize_t)file.Length(); | |
390 | ||
041973c5 RD |
391 | if (len > 0) |
392 | { | |
1e545382 | 393 | #if wxUSE_UNICODE |
1c26fbe0 | 394 | wxMemoryBuffer buffer(len+1); |
8e5ec9cc | 395 | success = (file.Read(buffer.GetData(), len) == len); |
dc8005e2 | 396 | if (success) { |
9efe0302 RD |
397 | ((char*)buffer.GetData())[len] = 0; |
398 | contents = wxString(buffer, *wxConvCurrent, len); | |
399 | } | |
4a65f2c8 | 400 | #else |
8e5ec9cc MB |
401 | wxString buffer; |
402 | success = (file.Read(wxStringBuffer(buffer, len), len) == len); | |
4a65f2c8 RD |
403 | contents = buffer; |
404 | #endif | |
041973c5 RD |
405 | } |
406 | else | |
a5b274d7 WS |
407 | { |
408 | if (len == 0) | |
409 | success = true; // empty file is ok | |
410 | else | |
411 | success = false; // len == wxInvalidOffset | |
412 | } | |
041973c5 RD |
413 | |
414 | if (success) | |
415 | { | |
416 | SetText(contents); | |
417 | EmptyUndoBuffer(); | |
418 | SetSavePoint(); | |
419 | } | |
51566b0b RD |
420 | } |
421 | ||
422 | return success; | |
423 | } | |
424 | ||
f97d84a6 | 425 | |
2fcce896 | 426 | #if wxUSE_DRAG_AND_DROP |
dc8005e2 RD |
427 | wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) { |
428 | return m_swx->DoDragOver(x, y, def); | |
429 | } | |
4a65f2c8 RD |
430 | |
431 | ||
dc8005e2 | 432 | bool wxStyledTextCtrl::DoDropText(long x, long y, const wxString& data) { |
4a65f2c8 RD |
433 | return m_swx->DoDropText(x, y, data); |
434 | } | |
2fcce896 | 435 | #endif |
4a65f2c8 RD |
436 | |
437 | ||
d1558f3d RD |
438 | void wxStyledTextCtrl::SetUseAntiAliasing(bool useAA) { |
439 | m_swx->SetUseAntiAliasing(useAA); | |
440 | } | |
441 | ||
442 | bool wxStyledTextCtrl::GetUseAntiAliasing() { | |
443 | return m_swx->GetUseAntiAliasing(); | |
444 | } | |
445 | ||
41a499cd RD |
446 | |
447 | ||
448 | ||
449 | ||
450 | void wxStyledTextCtrl::AddTextRaw(const char* text) | |
451 | { | |
452 | SendMsg(SCI_ADDTEXT, strlen(text), (long)text); | |
453 | } | |
454 | ||
455 | void wxStyledTextCtrl::InsertTextRaw(int pos, const char* text) | |
456 | { | |
457 | SendMsg(SCI_INSERTTEXT, pos, (long)text); | |
458 | } | |
459 | ||
460 | wxCharBuffer wxStyledTextCtrl::GetCurLineRaw(int* linePos) | |
461 | { | |
462 | int len = LineLength(GetCurrentLine()); | |
463 | if (!len) { | |
464 | if (linePos) *linePos = 0; | |
465 | wxCharBuffer empty; | |
466 | return empty; | |
467 | } | |
468 | ||
469 | wxCharBuffer buf(len); | |
470 | int pos = SendMsg(SCI_GETCURLINE, len, (long)buf.data()); | |
471 | if (linePos) *linePos = pos; | |
472 | return buf; | |
473 | } | |
474 | ||
475 | wxCharBuffer wxStyledTextCtrl::GetLineRaw(int line) | |
476 | { | |
477 | int len = LineLength(line); | |
478 | if (!len) { | |
479 | wxCharBuffer empty; | |
480 | return empty; | |
481 | } | |
482 | ||
483 | wxCharBuffer buf(len); | |
484 | SendMsg(SCI_GETLINE, line, (long)buf.data()); | |
485 | return buf; | |
486 | } | |
487 | ||
488 | wxCharBuffer wxStyledTextCtrl::GetSelectedTextRaw() | |
489 | { | |
490 | int start; | |
491 | int end; | |
492 | ||
493 | GetSelection(&start, &end); | |
494 | int len = end - start; | |
495 | if (!len) { | |
496 | wxCharBuffer empty; | |
497 | return empty; | |
498 | } | |
499 | ||
500 | wxCharBuffer buf(len); | |
501 | SendMsg(SCI_GETSELTEXT, 0, (long)buf.data()); | |
502 | return buf; | |
503 | } | |
504 | ||
505 | wxCharBuffer wxStyledTextCtrl::GetTextRangeRaw(int startPos, int endPos) | |
506 | { | |
507 | if (endPos < startPos) { | |
508 | int temp = startPos; | |
509 | startPos = endPos; | |
510 | endPos = temp; | |
511 | } | |
512 | int len = endPos - startPos; | |
513 | if (!len) { | |
514 | wxCharBuffer empty; | |
515 | return empty; | |
516 | } | |
517 | ||
518 | wxCharBuffer buf(len); | |
519 | TextRange tr; | |
520 | tr.lpstrText = buf.data(); | |
521 | tr.chrg.cpMin = startPos; | |
522 | tr.chrg.cpMax = endPos; | |
523 | SendMsg(SCI_GETTEXTRANGE, 0, (long)&tr); | |
524 | return buf; | |
525 | } | |
526 | ||
527 | void wxStyledTextCtrl::SetTextRaw(const char* text) | |
528 | { | |
529 | SendMsg(SCI_SETTEXT, 0, (long)text); | |
530 | } | |
531 | ||
532 | wxCharBuffer wxStyledTextCtrl::GetTextRaw() | |
533 | { | |
534 | int len = GetTextLength(); | |
535 | wxCharBuffer buf(len); | |
536 | SendMsg(SCI_GETTEXT, len, (long)buf.data()); | |
537 | return buf; | |
538 | } | |
539 | ||
540 | void wxStyledTextCtrl::AppendTextRaw(const char* text) | |
541 | { | |
542 | SendMsg(SCI_APPENDTEXT, strlen(text), (long)text); | |
543 | } | |
544 | ||
545 | ||
546 | ||
547 | ||
548 | ||
f97d84a6 RD |
549 | //---------------------------------------------------------------------- |
550 | // Event handlers | |
551 | ||
88a8b04e | 552 | void wxStyledTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(evt)) { |
f97d84a6 | 553 | wxPaintDC dc(this); |
9e730a78 | 554 | m_swx->DoPaint(&dc, GetUpdateRegion().GetBox()); |
f97d84a6 RD |
555 | } |
556 | ||
557 | void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) { | |
558 | if (evt.GetOrientation() == wxHORIZONTAL) | |
559 | m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition()); | |
560 | else | |
561 | m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition()); | |
562 | } | |
563 | ||
5fa4613c RD |
564 | void wxStyledTextCtrl::OnScroll(wxScrollEvent& evt) { |
565 | wxScrollBar* sb = wxDynamicCast(evt.GetEventObject(), wxScrollBar); | |
566 | if (sb) { | |
567 | if (sb->IsVertical()) | |
568 | m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition()); | |
569 | else | |
570 | m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition()); | |
571 | } | |
572 | } | |
573 | ||
88a8b04e | 574 | void wxStyledTextCtrl::OnSize(wxSizeEvent& WXUNUSED(evt)) { |
39c0acb6 RD |
575 | if (m_swx) { |
576 | wxSize sz = GetClientSize(); | |
577 | m_swx->DoSize(sz.x, sz.y); | |
578 | } | |
f97d84a6 RD |
579 | } |
580 | ||
581 | void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) { | |
cb1871ca | 582 | SetFocus(); |
f97d84a6 | 583 | wxPoint pt = evt.GetPosition(); |
2b5f62a0 | 584 | m_swx->DoLeftButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(), |
f97d84a6 RD |
585 | evt.ShiftDown(), evt.ControlDown(), evt.AltDown()); |
586 | } | |
587 | ||
588 | void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) { | |
589 | wxPoint pt = evt.GetPosition(); | |
2b5f62a0 | 590 | m_swx->DoLeftButtonMove(Point(pt.x, pt.y)); |
f97d84a6 RD |
591 | } |
592 | ||
593 | void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) { | |
594 | wxPoint pt = evt.GetPosition(); | |
2b5f62a0 | 595 | m_swx->DoLeftButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(), |
f97d84a6 RD |
596 | evt.ControlDown()); |
597 | } | |
598 | ||
599 | ||
ddf2da08 RD |
600 | void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) { |
601 | wxPoint pt = evt.GetPosition(); | |
602 | m_swx->DoContextMenu(Point(pt.x, pt.y)); | |
603 | } | |
604 | ||
605 | ||
2b5f62a0 VZ |
606 | void wxStyledTextCtrl::OnMouseMiddleUp(wxMouseEvent& evt) { |
607 | wxPoint pt = evt.GetPosition(); | |
608 | m_swx->DoMiddleButtonUp(Point(pt.x, pt.y)); | |
609 | } | |
610 | ||
65ec6247 | 611 | void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) { |
f97d84a6 | 612 | wxPoint pt = evt.GetPosition(); |
65ec6247 | 613 | ScreenToClient(&pt.x, &pt.y); |
25484746 RD |
614 | /* |
615 | Show context menu at event point if it's within the window, | |
616 | or at caret location if not | |
617 | */ | |
618 | wxHitTest ht = this->HitTest(pt); | |
619 | if (ht != wxHT_WINDOW_INSIDE) { | |
620 | pt = this->PointFromPosition(this->GetCurrentPos()); | |
621 | } | |
f97d84a6 RD |
622 | m_swx->DoContextMenu(Point(pt.x, pt.y)); |
623 | } | |
624 | ||
37d62433 RD |
625 | |
626 | void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) { | |
627 | m_swx->DoMouseWheel(evt.GetWheelRotation(), | |
628 | evt.GetWheelDelta(), | |
65ec6247 | 629 | evt.GetLinesPerAction(), |
9b9337da RD |
630 | evt.ControlDown(), |
631 | evt.IsPageScroll()); | |
37d62433 RD |
632 | } |
633 | ||
634 | ||
f97d84a6 | 635 | void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) { |
5fd656d5 | 636 | // On (some?) non-US PC keyboards the AltGr key is required to enter some |
f3c2c221 RD |
637 | // common characters. It comes to us as both Alt and Ctrl down so we need |
638 | // to let the char through in that case, otherwise if only ctrl or only | |
639 | // alt let's skip it. | |
640 | bool ctrl = evt.ControlDown(); | |
28e0c28e RD |
641 | #ifdef __WXMAC__ |
642 | // On the Mac the Alt key is just a modifier key (like Shift) so we need | |
643 | // to allow the char events to be processed when Alt is pressed. | |
644 | // TODO: Should we check MetaDown instead in this case? | |
645 | bool alt = false; | |
646 | #else | |
f3c2c221 | 647 | bool alt = evt.AltDown(); |
28e0c28e | 648 | #endif |
00c64037 | 649 | bool skip = ((ctrl || alt) && ! (ctrl && alt)); |
f3c2c221 | 650 | |
5fd656d5 RD |
651 | if (!m_lastKeyDownConsumed && !skip) { |
652 | #if wxUSE_UNICODE | |
653 | int key = evt.GetUnicodeKey(); | |
654 | bool keyOk = true; | |
655 | ||
656 | // if the unicode key code is not really a unicode character (it may | |
657 | // be a function key or etc., the platforms appear to always give us a | |
658 | // small value in this case) then fallback to the ascii key code but | |
659 | // don't do anything for function keys or etc. | |
1b14227e | 660 | if (key <= 127) { |
5fd656d5 | 661 | key = evt.GetKeyCode(); |
1b14227e | 662 | keyOk = (key <= 127); |
5fd656d5 RD |
663 | } |
664 | if (keyOk) { | |
665 | m_swx->DoAddChar(key); | |
666 | return; | |
667 | } | |
668 | #else | |
669 | int key = evt.GetKeyCode(); | |
670 | if (key <= WXK_START || key > WXK_COMMAND) { | |
671 | m_swx->DoAddChar(key); | |
672 | return; | |
673 | } | |
674 | #endif | |
f97d84a6 | 675 | } |
5fd656d5 | 676 | |
f3c2c221 | 677 | evt.Skip(); |
f97d84a6 RD |
678 | } |
679 | ||
d6582821 | 680 | |
f97d84a6 | 681 | void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) { |
5fd656d5 | 682 | int processed = m_swx->DoKeyDown(evt, &m_lastKeyDownConsumed); |
d6582821 | 683 | if (!processed && !m_lastKeyDownConsumed) |
f97d84a6 RD |
684 | evt.Skip(); |
685 | } | |
686 | ||
d6582821 | 687 | |
b6bfd8e8 | 688 | void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) { |
ec830416 | 689 | m_swx->DoLoseFocus(); |
b6bfd8e8 | 690 | evt.Skip(); |
f97d84a6 RD |
691 | } |
692 | ||
d6582821 | 693 | |
b6bfd8e8 | 694 | void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) { |
f97d84a6 | 695 | m_swx->DoGainFocus(); |
b6bfd8e8 | 696 | evt.Skip(); |
f97d84a6 RD |
697 | } |
698 | ||
d6582821 | 699 | |
88a8b04e | 700 | void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(evt)) { |
f97d84a6 RD |
701 | m_swx->DoSysColourChange(); |
702 | } | |
703 | ||
d6582821 | 704 | |
88a8b04e | 705 | void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt)) { |
f97d84a6 RD |
706 | // do nothing to help avoid flashing |
707 | } | |
708 | ||
709 | ||
710 | ||
711 | void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) { | |
712 | m_swx->DoCommand(evt.GetId()); | |
713 | } | |
714 | ||
715 | ||
88a8b04e | 716 | void wxStyledTextCtrl::OnListBox(wxCommandEvent& WXUNUSED(evt)) { |
f97d84a6 RD |
717 | m_swx->DoOnListBox(); |
718 | } | |
719 | ||
720 | ||
8e54aaed RD |
721 | void wxStyledTextCtrl::OnIdle(wxIdleEvent& evt) { |
722 | m_swx->DoOnIdle(evt); | |
723 | } | |
724 | ||
725 | ||
8ae4f086 RD |
726 | wxSize wxStyledTextCtrl::DoGetBestSize() const |
727 | { | |
728 | // What would be the best size for a wxSTC? | |
729 | // Just give a reasonable minimum until something else can be figured out. | |
730 | return wxSize(200,100); | |
731 | } | |
732 | ||
733 | ||
f97d84a6 RD |
734 | //---------------------------------------------------------------------- |
735 | // Turn notifications from Scintilla into events | |
736 | ||
737 | ||
738 | void wxStyledTextCtrl::NotifyChange() { | |
739 | wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId()); | |
a29a241f | 740 | evt.SetEventObject(this); |
f97d84a6 RD |
741 | GetEventHandler()->ProcessEvent(evt); |
742 | } | |
743 | ||
2b5f62a0 VZ |
744 | |
745 | static void SetEventText(wxStyledTextEvent& evt, const char* text, | |
746 | size_t length) { | |
747 | if(!text) return; | |
748 | ||
749 | // The unicode conversion MUST have a null byte to terminate the | |
750 | // string so move it into a buffer first and give it one. | |
751 | wxMemoryBuffer buf(length+1); | |
752 | buf.AppendData((void*)text, length); | |
753 | buf.AppendByte(0); | |
754 | evt.SetText(stc2wx(buf)); | |
755 | } | |
756 | ||
757 | ||
f97d84a6 RD |
758 | void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) { |
759 | SCNotification& scn = *_scn; | |
65ec6247 RD |
760 | wxStyledTextEvent evt(0, GetId()); |
761 | ||
a29a241f | 762 | evt.SetEventObject(this); |
65ec6247 RD |
763 | evt.SetPosition(scn.position); |
764 | evt.SetKey(scn.ch); | |
765 | evt.SetModifiers(scn.modifiers); | |
766 | ||
f97d84a6 RD |
767 | switch (scn.nmhdr.code) { |
768 | case SCN_STYLENEEDED: | |
65ec6247 | 769 | evt.SetEventType(wxEVT_STC_STYLENEEDED); |
f97d84a6 | 770 | break; |
65ec6247 | 771 | |
f97d84a6 | 772 | case SCN_CHARADDED: |
65ec6247 | 773 | evt.SetEventType(wxEVT_STC_CHARADDED); |
f97d84a6 | 774 | break; |
65ec6247 | 775 | |
f97d84a6 | 776 | case SCN_SAVEPOINTREACHED: |
65ec6247 | 777 | evt.SetEventType(wxEVT_STC_SAVEPOINTREACHED); |
f97d84a6 | 778 | break; |
65ec6247 | 779 | |
f97d84a6 | 780 | case SCN_SAVEPOINTLEFT: |
65ec6247 | 781 | evt.SetEventType(wxEVT_STC_SAVEPOINTLEFT); |
f97d84a6 | 782 | break; |
65ec6247 | 783 | |
f97d84a6 | 784 | case SCN_MODIFYATTEMPTRO: |
65ec6247 RD |
785 | evt.SetEventType(wxEVT_STC_ROMODIFYATTEMPT); |
786 | break; | |
787 | ||
788 | case SCN_KEY: | |
789 | evt.SetEventType(wxEVT_STC_KEY); | |
f97d84a6 | 790 | break; |
65ec6247 | 791 | |
f97d84a6 | 792 | case SCN_DOUBLECLICK: |
65ec6247 | 793 | evt.SetEventType(wxEVT_STC_DOUBLECLICK); |
f97d84a6 | 794 | break; |
65ec6247 RD |
795 | |
796 | case SCN_UPDATEUI: | |
797 | evt.SetEventType(wxEVT_STC_UPDATEUI); | |
f97d84a6 | 798 | break; |
65ec6247 RD |
799 | |
800 | case SCN_MODIFIED: | |
801 | evt.SetEventType(wxEVT_STC_MODIFIED); | |
802 | evt.SetModificationType(scn.modificationType); | |
2b5f62a0 | 803 | SetEventText(evt, scn.text, scn.length); |
65ec6247 RD |
804 | evt.SetLength(scn.length); |
805 | evt.SetLinesAdded(scn.linesAdded); | |
806 | evt.SetLine(scn.line); | |
807 | evt.SetFoldLevelNow(scn.foldLevelNow); | |
808 | evt.SetFoldLevelPrev(scn.foldLevelPrev); | |
f97d84a6 | 809 | break; |
65ec6247 | 810 | |
f97d84a6 | 811 | case SCN_MACRORECORD: |
65ec6247 RD |
812 | evt.SetEventType(wxEVT_STC_MACRORECORD); |
813 | evt.SetMessage(scn.message); | |
814 | evt.SetWParam(scn.wParam); | |
815 | evt.SetLParam(scn.lParam); | |
f97d84a6 | 816 | break; |
65ec6247 | 817 | |
f97d84a6 | 818 | case SCN_MARGINCLICK: |
65ec6247 RD |
819 | evt.SetEventType(wxEVT_STC_MARGINCLICK); |
820 | evt.SetMargin(scn.margin); | |
f97d84a6 | 821 | break; |
65ec6247 | 822 | |
f97d84a6 | 823 | case SCN_NEEDSHOWN: |
65ec6247 RD |
824 | evt.SetEventType(wxEVT_STC_NEEDSHOWN); |
825 | evt.SetLength(scn.length); | |
f97d84a6 | 826 | break; |
65ec6247 | 827 | |
65ec6247 RD |
828 | case SCN_PAINTED: |
829 | evt.SetEventType(wxEVT_STC_PAINTED); | |
830 | break; | |
831 | ||
832 | case SCN_USERLISTSELECTION: | |
833 | evt.SetEventType(wxEVT_STC_USERLISTSELECTION); | |
834 | evt.SetListType(scn.listType); | |
2b5f62a0 | 835 | SetEventText(evt, scn.text, strlen(scn.text)); |
f97d84a6 | 836 | break; |
f97d84a6 | 837 | |
65ec6247 RD |
838 | case SCN_URIDROPPED: |
839 | evt.SetEventType(wxEVT_STC_URIDROPPED); | |
2b5f62a0 | 840 | SetEventText(evt, scn.text, strlen(scn.text)); |
65ec6247 RD |
841 | break; |
842 | ||
843 | case SCN_DWELLSTART: | |
844 | evt.SetEventType(wxEVT_STC_DWELLSTART); | |
845 | evt.SetX(scn.x); | |
846 | evt.SetY(scn.y); | |
847 | break; | |
848 | ||
849 | case SCN_DWELLEND: | |
850 | evt.SetEventType(wxEVT_STC_DWELLEND); | |
851 | evt.SetX(scn.x); | |
852 | evt.SetY(scn.y); | |
853 | break; | |
854 | ||
a834585d RD |
855 | case SCN_ZOOM: |
856 | evt.SetEventType(wxEVT_STC_ZOOM); | |
857 | break; | |
858 | ||
9e730a78 RD |
859 | case SCN_HOTSPOTCLICK: |
860 | evt.SetEventType(wxEVT_STC_HOTSPOT_CLICK); | |
861 | break; | |
862 | ||
863 | case SCN_HOTSPOTDOUBLECLICK: | |
864 | evt.SetEventType(wxEVT_STC_HOTSPOT_DCLICK); | |
865 | break; | |
866 | ||
867 | case SCN_CALLTIPCLICK: | |
868 | evt.SetEventType(wxEVT_STC_CALLTIP_CLICK); | |
869 | break; | |
870 | ||
65ec6247 RD |
871 | default: |
872 | return; | |
f97d84a6 | 873 | } |
65ec6247 RD |
874 | |
875 | GetEventHandler()->ProcessEvent(evt); | |
f97d84a6 RD |
876 | } |
877 | ||
878 | ||
f97d84a6 RD |
879 | //---------------------------------------------------------------------- |
880 | //---------------------------------------------------------------------- | |
881 | //---------------------------------------------------------------------- | |
882 | ||
883 | wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id) | |
884 | : wxCommandEvent(commandType, id) | |
885 | { | |
886 | m_position = 0; | |
887 | m_key = 0; | |
888 | m_modifiers = 0; | |
889 | m_modificationType = 0; | |
890 | m_length = 0; | |
891 | m_linesAdded = 0; | |
892 | m_line = 0; | |
893 | m_foldLevelNow = 0; | |
894 | m_foldLevelPrev = 0; | |
895 | m_margin = 0; | |
896 | m_message = 0; | |
897 | m_wParam = 0; | |
898 | m_lParam = 0; | |
65ec6247 RD |
899 | m_listType = 0; |
900 | m_x = 0; | |
901 | m_y = 0; | |
dc8005e2 | 902 | m_dragAllowMove = false; |
92bbd64f | 903 | #if wxUSE_DRAG_AND_DROP |
a29a241f | 904 | m_dragResult = wxDragNone; |
92bbd64f | 905 | #endif |
f97d84a6 RD |
906 | } |
907 | ||
908 | bool wxStyledTextEvent::GetShift() const { return (m_modifiers & SCI_SHIFT) != 0; } | |
909 | bool wxStyledTextEvent::GetControl() const { return (m_modifiers & SCI_CTRL) != 0; } | |
910 | bool wxStyledTextEvent::GetAlt() const { return (m_modifiers & SCI_ALT) != 0; } | |
911 | ||
f97d84a6 | 912 | |
5fa4613c RD |
913 | wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent& event): |
914 | wxCommandEvent(event) | |
915 | { | |
916 | m_position = event.m_position; | |
917 | m_key = event.m_key; | |
918 | m_modifiers = event.m_modifiers; | |
919 | m_modificationType = event.m_modificationType; | |
920 | m_text = event.m_text; | |
921 | m_length = event.m_length; | |
922 | m_linesAdded = event.m_linesAdded; | |
923 | m_line = event.m_line; | |
924 | m_foldLevelNow = event.m_foldLevelNow; | |
925 | m_foldLevelPrev = event.m_foldLevelPrev; | |
926 | ||
927 | m_margin = event.m_margin; | |
928 | ||
929 | m_message = event.m_message; | |
930 | m_wParam = event.m_wParam; | |
931 | m_lParam = event.m_lParam; | |
932 | ||
933 | m_listType = event.m_listType; | |
934 | m_x = event.m_x; | |
935 | m_y = event.m_y; | |
f97d84a6 | 936 | |
5fa4613c RD |
937 | m_dragText = event.m_dragText; |
938 | m_dragAllowMove =event.m_dragAllowMove; | |
92bbd64f | 939 | #if wxUSE_DRAG_AND_DROP |
5fa4613c | 940 | m_dragResult = event.m_dragResult; |
92bbd64f | 941 | #endif |
f97d84a6 RD |
942 | } |
943 | ||
944 | //---------------------------------------------------------------------- | |
945 | //---------------------------------------------------------------------- | |
946 | ||
a29a241f RD |
947 | |
948 | ||
949 | ||
5fa4613c RD |
950 | |
951 | ||
952 | ||
953 | ||
954 |