Remove workaround for slow scrolling in wxStyledTextCtrl.
[wxWidgets.git] / src / stc / stc.cpp.in
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: stc.cpp
3 // Purpose: A wxWidgets 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 licence
16 /////////////////////////////////////////////////////////////////////////////
17
18 /*
19 IMPORTANT: src/stc/stc.cpp is generated by src/stc/gen_iface.py from
20 src/stc/stc.cpp.in, don't edit stc.cpp file as your changes will be
21 lost after the next regeneration, edit stc.cpp.in and rerun the
22 gen_iface.py script instead!
23
24 Parts of this file generated by the script are found in between
25 the special "{{{" and "}}}" markers, the rest of it is copied
26 verbatim from src.h.in.
27 */
28
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #if wxUSE_STC
37
38 #include "wx/stc/stc.h"
39 #include "wx/stc/private.h"
40
41 #ifndef WX_PRECOMP
42 #include "wx/wx.h"
43 #endif // WX_PRECOMP
44
45 #include <ctype.h>
46
47 #include "wx/tokenzr.h"
48 #include "wx/mstream.h"
49 #include "wx/image.h"
50 #include "wx/ffile.h"
51
52 #include "ScintillaWX.h"
53
54 //----------------------------------------------------------------------
55
56 const char wxSTCNameStr[] = "stcwindow";
57
58 #ifdef MAKELONG
59 #undef MAKELONG
60 #endif
61
62 #define MAKELONG(a, b) ((a) | ((b) << 16))
63
64
65 static long wxColourAsLong(const wxColour& co) {
66 return (((long)co.Blue() << 16) |
67 ((long)co.Green() << 8) |
68 ((long)co.Red()));
69 }
70
71 static wxColour wxColourFromLong(long c) {
72 wxColour clr;
73 clr.Set((unsigned char)(c & 0xff),
74 (unsigned char)((c >> 8) & 0xff),
75 (unsigned char)((c >> 16) & 0xff));
76 return clr;
77 }
78
79
80 static wxColour wxColourFromSpec(const wxString& spec) {
81 // spec should be a colour name or "#RRGGBB"
82 if (spec.GetChar(0) == wxT('#')) {
83
84 long red, green, blue;
85 red = green = blue = 0;
86 spec.Mid(1,2).ToLong(&red, 16);
87 spec.Mid(3,2).ToLong(&green, 16);
88 spec.Mid(5,2).ToLong(&blue, 16);
89 return wxColour((unsigned char)red,
90 (unsigned char)green,
91 (unsigned char)blue);
92 }
93 else
94 return wxColour(spec);
95 }
96
97 //----------------------------------------------------------------------
98
99 wxDEFINE_EVENT( wxEVT_STC_CHANGE, wxStyledTextEvent );
100 wxDEFINE_EVENT( wxEVT_STC_STYLENEEDED, wxStyledTextEvent );
101 wxDEFINE_EVENT( wxEVT_STC_CHARADDED, wxStyledTextEvent );
102 wxDEFINE_EVENT( wxEVT_STC_SAVEPOINTREACHED, wxStyledTextEvent );
103 wxDEFINE_EVENT( wxEVT_STC_SAVEPOINTLEFT, wxStyledTextEvent );
104 wxDEFINE_EVENT( wxEVT_STC_ROMODIFYATTEMPT, wxStyledTextEvent );
105 wxDEFINE_EVENT( wxEVT_STC_KEY, wxStyledTextEvent );
106 wxDEFINE_EVENT( wxEVT_STC_DOUBLECLICK, wxStyledTextEvent );
107 wxDEFINE_EVENT( wxEVT_STC_UPDATEUI, wxStyledTextEvent );
108 wxDEFINE_EVENT( wxEVT_STC_MODIFIED, wxStyledTextEvent );
109 wxDEFINE_EVENT( wxEVT_STC_MACRORECORD, wxStyledTextEvent );
110 wxDEFINE_EVENT( wxEVT_STC_MARGINCLICK, wxStyledTextEvent );
111 wxDEFINE_EVENT( wxEVT_STC_NEEDSHOWN, wxStyledTextEvent );
112 wxDEFINE_EVENT( wxEVT_STC_PAINTED, wxStyledTextEvent );
113 wxDEFINE_EVENT( wxEVT_STC_USERLISTSELECTION, wxStyledTextEvent );
114 wxDEFINE_EVENT( wxEVT_STC_URIDROPPED, wxStyledTextEvent );
115 wxDEFINE_EVENT( wxEVT_STC_DWELLSTART, wxStyledTextEvent );
116 wxDEFINE_EVENT( wxEVT_STC_DWELLEND, wxStyledTextEvent );
117 wxDEFINE_EVENT( wxEVT_STC_START_DRAG, wxStyledTextEvent );
118 wxDEFINE_EVENT( wxEVT_STC_DRAG_OVER, wxStyledTextEvent );
119 wxDEFINE_EVENT( wxEVT_STC_DO_DROP, wxStyledTextEvent );
120 wxDEFINE_EVENT( wxEVT_STC_ZOOM, wxStyledTextEvent );
121 wxDEFINE_EVENT( wxEVT_STC_HOTSPOT_CLICK, wxStyledTextEvent );
122 wxDEFINE_EVENT( wxEVT_STC_HOTSPOT_DCLICK, wxStyledTextEvent );
123 wxDEFINE_EVENT( wxEVT_STC_CALLTIP_CLICK, wxStyledTextEvent );
124 wxDEFINE_EVENT( wxEVT_STC_AUTOCOMP_SELECTION, wxStyledTextEvent );
125 wxDEFINE_EVENT( wxEVT_STC_INDICATOR_CLICK, wxStyledTextEvent );
126 wxDEFINE_EVENT( wxEVT_STC_INDICATOR_RELEASE, wxStyledTextEvent );
127 wxDEFINE_EVENT( wxEVT_STC_AUTOCOMP_CANCELLED, wxStyledTextEvent );
128 wxDEFINE_EVENT( wxEVT_STC_AUTOCOMP_CHAR_DELETED, wxStyledTextEvent );
129
130
131
132 BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl)
133 EVT_PAINT (wxStyledTextCtrl::OnPaint)
134 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin)
135 EVT_SCROLL (wxStyledTextCtrl::OnScroll)
136 EVT_SIZE (wxStyledTextCtrl::OnSize)
137 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown)
138 // Let Scintilla see the double click as a second click
139 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown)
140 EVT_MOTION (wxStyledTextCtrl::OnMouseMove)
141 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp)
142 #if defined(__WXGTK__) || defined(__WXMAC__)
143 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp)
144 #else
145 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu)
146 #endif
147 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel)
148 EVT_MIDDLE_UP (wxStyledTextCtrl::OnMouseMiddleUp)
149 EVT_CHAR (wxStyledTextCtrl::OnChar)
150 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown)
151 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus)
152 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus)
153 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged)
154 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground)
155 EVT_MENU_RANGE (10, 16, wxStyledTextCtrl::OnMenu)
156 EVT_LISTBOX_DCLICK (wxID_ANY, wxStyledTextCtrl::OnListBox)
157 END_EVENT_TABLE()
158
159
160 IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
161 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
162
163 #ifdef LINK_LEXERS
164 // forces the linking of the lexer modules
165 int Scintilla_LinkLexers();
166 #endif
167
168 //----------------------------------------------------------------------
169 // Constructor and Destructor
170
171 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent,
172 wxWindowID id,
173 const wxPoint& pos,
174 const wxSize& size,
175 long style,
176 const wxString& name)
177 {
178 m_swx = NULL;
179 Create(parent, id, pos, size, style, name);
180 }
181
182
183 bool wxStyledTextCtrl::Create(wxWindow *parent,
184 wxWindowID id,
185 const wxPoint& pos,
186 const wxSize& size,
187 long style,
188 const wxString& name)
189 {
190 style |= wxVSCROLL | wxHSCROLL;
191 if (!wxControl::Create(parent, id, pos, size,
192 style | wxWANTS_CHARS | wxCLIP_CHILDREN,
193 wxDefaultValidator, name))
194 return false;
195
196 #ifdef LINK_LEXERS
197 Scintilla_LinkLexers();
198 #endif
199 m_swx = new ScintillaWX(this);
200 m_stopWatch.Start();
201 m_lastKeyDownConsumed = false;
202 m_vScrollBar = NULL;
203 m_hScrollBar = NULL;
204 #if wxUSE_UNICODE
205 // Put Scintilla into unicode (UTF-8) mode
206 SetCodePage(wxSTC_CP_UTF8);
207 #endif
208
209 SetInitialSize(size);
210
211 // Reduces flicker on GTK+/X11
212 SetBackgroundStyle(wxBG_STYLE_PAINT);
213
214 // Make sure it can take the focus
215 SetCanFocus(true);
216
217 return true;
218 }
219
220
221 wxStyledTextCtrl::~wxStyledTextCtrl() {
222 delete m_swx;
223 }
224
225
226 //----------------------------------------------------------------------
227
228 wxIntPtr wxStyledTextCtrl::SendMsg(int msg, wxUIntPtr wp, wxIntPtr lp) const
229 {
230 return m_swx->WndProc(msg, wp, lp);
231 }
232
233 //----------------------------------------------------------------------
234
235 // Set the vertical scrollbar to use instead of the ont that's built-in.
236 void wxStyledTextCtrl::SetVScrollBar(wxScrollBar* bar) {
237 m_vScrollBar = bar;
238 if (bar != NULL) {
239 // ensure that the built-in scrollbar is not visible
240 SetScrollbar(wxVERTICAL, 0, 0, 0);
241 }
242 }
243
244
245 // Set the horizontal scrollbar to use instead of the ont that's built-in.
246 void wxStyledTextCtrl::SetHScrollBar(wxScrollBar* bar) {
247 m_hScrollBar = bar;
248 if (bar != NULL) {
249 // ensure that the built-in scrollbar is not visible
250 SetScrollbar(wxHORIZONTAL, 0, 0, 0);
251 }
252 }
253
254 //----------------------------------------------------------------------
255 // Generated methods implementation section {{{
256
257 %(METHOD_IMPS)s
258
259 //}}}
260 //----------------------------------------------------------------------
261
262
263 // Returns the line number of the line with the caret.
264 int wxStyledTextCtrl::GetCurrentLine() {
265 int line = LineFromPosition(GetCurrentPos());
266 return line;
267 }
268
269
270 // Extract style settings from a spec-string which is composed of one or
271 // more of the following comma separated elements:
272 //
273 // bold turns on bold
274 // italic turns on italics
275 // fore:[name or #RRGGBB] sets the foreground colour
276 // back:[name or #RRGGBB] sets the background colour
277 // face:[facename] sets the font face name to use
278 // size:[num] sets the font size in points
279 // eol turns on eol filling
280 // underline turns on underlining
281 //
282 void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
283
284 wxStringTokenizer tkz(spec, wxT(","));
285 while (tkz.HasMoreTokens()) {
286 wxString token = tkz.GetNextToken();
287
288 wxString option = token.BeforeFirst(':');
289 wxString val = token.AfterFirst(':');
290
291 if (option == wxT("bold"))
292 StyleSetBold(styleNum, true);
293
294 else if (option == wxT("italic"))
295 StyleSetItalic(styleNum, true);
296
297 else if (option == wxT("underline"))
298 StyleSetUnderline(styleNum, true);
299
300 else if (option == wxT("eol"))
301 StyleSetEOLFilled(styleNum, true);
302
303 else if (option == wxT("size")) {
304 long points;
305 if (val.ToLong(&points))
306 StyleSetSize(styleNum, points);
307 }
308
309 else if (option == wxT("face"))
310 StyleSetFaceName(styleNum, val);
311
312 else if (option == wxT("fore"))
313 StyleSetForeground(styleNum, wxColourFromSpec(val));
314
315 else if (option == wxT("back"))
316 StyleSetBackground(styleNum, wxColourFromSpec(val));
317 }
318 }
319
320
321 // Get the font of a style
322 wxFont wxStyledTextCtrl::StyleGetFont(int style) {
323 wxFont font;
324 font.SetPointSize(StyleGetSize(style));
325 font.SetFaceName(StyleGetFaceName(style));
326 if( StyleGetBold(style) )
327 font.SetWeight(wxFONTWEIGHT_BOLD);
328 else
329 font.SetWeight(wxFONTWEIGHT_NORMAL);
330
331 if( StyleGetItalic(style) )
332 font.SetStyle(wxFONTSTYLE_ITALIC);
333 else
334 font.SetStyle(wxFONTSTYLE_NORMAL);
335
336 return font;
337 }
338
339
340 // Set style size, face, bold, italic, and underline attributes from
341 // a wxFont's attributes.
342 void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) {
343 #ifdef __WXGTK__
344 // Ensure that the native font is initialized
345 int x, y;
346 GetTextExtent(wxT("X"), &x, &y, NULL, NULL, &font);
347 #endif
348 int size = font.GetPointSize();
349 wxString faceName = font.GetFaceName();
350 bool bold = font.GetWeight() == wxBOLD;
351 bool italic = font.GetStyle() != wxNORMAL;
352 bool under = font.GetUnderlined();
353 wxFontEncoding encoding = font.GetEncoding();
354
355 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under, encoding);
356 }
357
358 // Set all font style attributes at once.
359 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size,
360 const wxString& faceName,
361 bool bold, bool italic,
362 bool underline,
363 wxFontEncoding encoding) {
364 StyleSetSize(styleNum, size);
365 StyleSetFaceName(styleNum, faceName);
366 StyleSetBold(styleNum, bold);
367 StyleSetItalic(styleNum, italic);
368 StyleSetUnderline(styleNum, underline);
369 StyleSetFontEncoding(styleNum, encoding);
370 }
371
372
373 // Set the character set of the font in a style. Converts the Scintilla
374 // character set values to a wxFontEncoding.
375 void wxStyledTextCtrl::StyleSetCharacterSet(int style, int characterSet)
376 {
377 wxFontEncoding encoding;
378
379 // Translate the Scintilla characterSet to a wxFontEncoding
380 switch (characterSet) {
381 default:
382 case wxSTC_CHARSET_ANSI:
383 case wxSTC_CHARSET_DEFAULT:
384 encoding = wxFONTENCODING_DEFAULT;
385 break;
386
387 case wxSTC_CHARSET_BALTIC:
388 encoding = wxFONTENCODING_ISO8859_13;
389 break;
390
391 case wxSTC_CHARSET_CHINESEBIG5:
392 encoding = wxFONTENCODING_CP950;
393 break;
394
395 case wxSTC_CHARSET_EASTEUROPE:
396 encoding = wxFONTENCODING_ISO8859_2;
397 break;
398
399 case wxSTC_CHARSET_GB2312:
400 encoding = wxFONTENCODING_CP936;
401 break;
402
403 case wxSTC_CHARSET_GREEK:
404 encoding = wxFONTENCODING_ISO8859_7;
405 break;
406
407 case wxSTC_CHARSET_HANGUL:
408 encoding = wxFONTENCODING_CP949;
409 break;
410
411 case wxSTC_CHARSET_MAC:
412 encoding = wxFONTENCODING_DEFAULT;
413 break;
414
415 case wxSTC_CHARSET_OEM:
416 encoding = wxFONTENCODING_DEFAULT;
417 break;
418
419 case wxSTC_CHARSET_RUSSIAN:
420 encoding = wxFONTENCODING_KOI8;
421 break;
422
423 case wxSTC_CHARSET_SHIFTJIS:
424 encoding = wxFONTENCODING_CP932;
425 break;
426
427 case wxSTC_CHARSET_SYMBOL:
428 encoding = wxFONTENCODING_DEFAULT;
429 break;
430
431 case wxSTC_CHARSET_TURKISH:
432 encoding = wxFONTENCODING_ISO8859_9;
433 break;
434
435 case wxSTC_CHARSET_JOHAB:
436 encoding = wxFONTENCODING_DEFAULT;
437 break;
438
439 case wxSTC_CHARSET_HEBREW:
440 encoding = wxFONTENCODING_ISO8859_8;
441 break;
442
443 case wxSTC_CHARSET_ARABIC:
444 encoding = wxFONTENCODING_ISO8859_6;
445 break;
446
447 case wxSTC_CHARSET_VIETNAMESE:
448 encoding = wxFONTENCODING_DEFAULT;
449 break;
450
451 case wxSTC_CHARSET_THAI:
452 encoding = wxFONTENCODING_ISO8859_11;
453 break;
454
455 case wxSTC_CHARSET_CYRILLIC:
456 encoding = wxFONTENCODING_ISO8859_5;
457 break;
458
459 case wxSTC_CHARSET_8859_15:
460 encoding = wxFONTENCODING_ISO8859_15;;
461 break;
462 }
463
464 // We just have Scintilla track the wxFontEncoding for us. It gets used
465 // in Font::Create in PlatWX.cpp. We add one to the value so that the
466 // effective wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT and so when
467 // Scintilla internally uses SC_CHARSET_DEFAULT we will translate it back
468 // to wxFONENCODING_DEFAULT in Font::Create.
469 SendMsg(SCI_STYLESETCHARACTERSET, style, encoding+1);
470 }
471
472
473 // Set the font encoding to be used by a style.
474 void wxStyledTextCtrl::StyleSetFontEncoding(int style, wxFontEncoding encoding)
475 {
476 SendMsg(SCI_STYLESETCHARACTERSET, style, encoding+1);
477 }
478
479
480 // Perform one of the operations defined by the wxSTC_CMD_* constants.
481 void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
482 SendMsg(cmd);
483 }
484
485
486 // Set the left and right margin in the edit area, measured in pixels.
487 void wxStyledTextCtrl::SetMargins(int left, int right) {
488 SetMarginLeft(left);
489 SetMarginRight(right);
490 }
491
492
493 // Retrieve the point in the window where a position is displayed.
494 wxPoint wxStyledTextCtrl::PointFromPosition(int pos) {
495 int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos);
496 int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos);
497 return wxPoint(x, y);
498 }
499
500 // Scroll enough to make the given line visible
501 void wxStyledTextCtrl::ScrollToLine(int line) {
502 m_swx->DoScrollToLine(line);
503 }
504
505
506 // Scroll enough to make the given column visible
507 void wxStyledTextCtrl::ScrollToColumn(int column) {
508 m_swx->DoScrollToColumn(column);
509 }
510
511
512 #if wxUSE_TEXTCTRL
513 bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int fileType)
514 {
515 bool ok = wxTextAreaBase::DoSaveFile(filename, fileType);
516 #else
517 bool wxStyledTextCtrl::SaveFile(const wxString& filename)
518 {
519 #if wxUSE_FFILE
520 wxFFile file(filename, wxT("w"));
521 bool ok = file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
522 #else
523 bool ok = false;
524 #endif // wxUSE_FFILE
525 #endif
526 if (ok)
527 {
528 SetSavePoint();
529 }
530 return ok;
531 }
532
533 #if wxUSE_TEXTCTRL
534 bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int fileType)
535 {
536 bool ok = wxTextAreaBase::DoLoadFile(filename, fileType);
537 #else
538 bool wxStyledTextCtrl::LoadFile(const wxString& filename)
539 {
540 #if wxUSE_FFILE
541 wxFFile file(filename);
542 bool ok = file.IsOpened();
543 if (ok)
544 {
545 wxString text;
546 ok = file.ReadAll(&text, *wxConvCurrent);
547 if (ok)
548 {
549 SetValue(text);
550 }
551 }
552 #else
553 bool ok = false;
554 #endif // wxUSE_FFILE
555 #endif
556 if (ok)
557 {
558 EmptyUndoBuffer();
559 SetSavePoint();
560 }
561 return ok;
562 }
563
564 #if wxUSE_DRAG_AND_DROP
565 wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
566 return m_swx->DoDragOver(x, y, def);
567 }
568
569
570 bool wxStyledTextCtrl::DoDropText(long x, long y, const wxString& data) {
571 return m_swx->DoDropText(x, y, data);
572 }
573 #endif
574
575
576 void wxStyledTextCtrl::SetUseAntiAliasing(bool useAA) {
577 m_swx->SetUseAntiAliasing(useAA);
578 }
579
580 bool wxStyledTextCtrl::GetUseAntiAliasing() {
581 return m_swx->GetUseAntiAliasing();
582 }
583
584
585
586
587
588 void wxStyledTextCtrl::AddTextRaw(const char* text, int length)
589 {
590 if (length == -1)
591 length = strlen(text);
592 SendMsg(SCI_ADDTEXT, length, (sptr_t)text);
593 }
594
595 void wxStyledTextCtrl::InsertTextRaw(int pos, const char* text)
596 {
597 SendMsg(SCI_INSERTTEXT, pos, (sptr_t)text);
598 }
599
600 wxCharBuffer wxStyledTextCtrl::GetCurLineRaw(int* linePos)
601 {
602 int len = LineLength(GetCurrentLine());
603 if (!len) {
604 if (linePos) *linePos = 0;
605 wxCharBuffer empty;
606 return empty;
607 }
608
609 wxCharBuffer buf(len);
610 int pos = SendMsg(SCI_GETCURLINE, len, (sptr_t)buf.data());
611 if (linePos) *linePos = pos;
612 return buf;
613 }
614
615 wxCharBuffer wxStyledTextCtrl::GetLineRaw(int line)
616 {
617 int len = LineLength(line);
618 if (!len) {
619 wxCharBuffer empty;
620 return empty;
621 }
622
623 wxCharBuffer buf(len);
624 SendMsg(SCI_GETLINE, line, (sptr_t)buf.data());
625 return buf;
626 }
627
628 wxCharBuffer wxStyledTextCtrl::GetSelectedTextRaw()
629 {
630 // Calculate the length needed first.
631 const int len = SendMsg(SCI_GETSELTEXT, 0, (sptr_t)0);
632
633 // And then really get the data.
634 wxCharBuffer buf(len);
635 SendMsg(SCI_GETSELTEXT, 0, (sptr_t)buf.data());
636 return buf;
637 }
638
639 wxCharBuffer wxStyledTextCtrl::GetTextRangeRaw(int startPos, int endPos)
640 {
641 if (endPos < startPos) {
642 int temp = startPos;
643 startPos = endPos;
644 endPos = temp;
645 }
646 int len = endPos - startPos;
647 if (!len) {
648 wxCharBuffer empty;
649 return empty;
650 }
651
652 wxCharBuffer buf(len);
653 TextRange tr;
654 tr.lpstrText = buf.data();
655 tr.chrg.cpMin = startPos;
656 tr.chrg.cpMax = endPos;
657 SendMsg(SCI_GETTEXTRANGE, 0, (sptr_t)&tr);
658 return buf;
659 }
660
661 void wxStyledTextCtrl::SetTextRaw(const char* text)
662 {
663 SendMsg(SCI_SETTEXT, 0, (sptr_t)text);
664 }
665
666 wxCharBuffer wxStyledTextCtrl::GetTextRaw()
667 {
668 int len = GetTextLength();
669 wxCharBuffer buf(len); // adds 1 for NUL automatically
670 SendMsg(SCI_GETTEXT, len + 1, (sptr_t)buf.data());
671 return buf;
672 }
673
674 void wxStyledTextCtrl::AppendTextRaw(const char* text, int length)
675 {
676 if (length == -1)
677 length = strlen(text);
678 SendMsg(SCI_APPENDTEXT, length, (sptr_t)text);
679 }
680
681
682
683
684
685 //----------------------------------------------------------------------
686 // Event handlers
687
688 void wxStyledTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(evt)) {
689 wxPaintDC dc(this);
690 m_swx->DoPaint(&dc, GetUpdateRegion().GetBox());
691 }
692
693 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
694 if (evt.GetOrientation() == wxHORIZONTAL)
695 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
696 else
697 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
698 }
699
700 void wxStyledTextCtrl::OnScroll(wxScrollEvent& evt) {
701 wxScrollBar* sb = wxDynamicCast(evt.GetEventObject(), wxScrollBar);
702 if (sb) {
703 if (sb->IsVertical())
704 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
705 else
706 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
707 }
708 }
709
710 void wxStyledTextCtrl::OnSize(wxSizeEvent& WXUNUSED(evt)) {
711 if (m_swx) {
712 wxSize sz = GetClientSize();
713 m_swx->DoSize(sz.x, sz.y);
714 }
715 }
716
717 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) {
718 SetFocus();
719 wxPoint pt = evt.GetPosition();
720 m_swx->DoLeftButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(),
721 evt.ShiftDown(), evt.ControlDown(), evt.AltDown());
722 }
723
724 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
725 wxPoint pt = evt.GetPosition();
726 m_swx->DoLeftButtonMove(Point(pt.x, pt.y));
727 }
728
729 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
730 wxPoint pt = evt.GetPosition();
731 m_swx->DoLeftButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
732 evt.ControlDown());
733 }
734
735
736 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) {
737 wxPoint pt = evt.GetPosition();
738 m_swx->DoContextMenu(Point(pt.x, pt.y));
739 }
740
741
742 void wxStyledTextCtrl::OnMouseMiddleUp(wxMouseEvent& evt) {
743 wxPoint pt = evt.GetPosition();
744 m_swx->DoMiddleButtonUp(Point(pt.x, pt.y));
745 }
746
747 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) {
748 wxPoint pt = evt.GetPosition();
749 ScreenToClient(&pt.x, &pt.y);
750 /*
751 Show context menu at event point if it's within the window,
752 or at caret location if not
753 */
754 wxHitTest ht = this->HitTest(pt);
755 if (ht != wxHT_WINDOW_INSIDE) {
756 pt = this->PointFromPosition(this->GetCurrentPos());
757 }
758 m_swx->DoContextMenu(Point(pt.x, pt.y));
759 }
760
761
762 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt)
763 {
764 m_swx->DoMouseWheel(evt.GetWheelRotation(),
765 evt.GetWheelDelta(),
766 evt.GetLinesPerAction(),
767 evt.ControlDown(),
768 evt.IsPageScroll());
769 }
770
771
772 void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) {
773 // On (some?) non-US PC keyboards the AltGr key is required to enter some
774 // common characters. It comes to us as both Alt and Ctrl down so we need
775 // to let the char through in that case, otherwise if only ctrl or only
776 // alt let's skip it.
777 bool ctrl = evt.ControlDown();
778 #ifdef __WXMAC__
779 // On the Mac the Alt key is just a modifier key (like Shift) so we need
780 // to allow the char events to be processed when Alt is pressed.
781 // TODO: Should we check MetaDown instead in this case?
782 bool alt = false;
783 #else
784 bool alt = evt.AltDown();
785 #endif
786 bool skip = ((ctrl || alt) && ! (ctrl && alt));
787
788 #if wxUSE_UNICODE
789 // apparently if we don't do this, Unicode keys pressed after non-char
790 // ASCII ones (e.g. Enter, Tab) are not taken into account (patch 1615989)
791 if (m_lastKeyDownConsumed && evt.GetUnicodeKey() > 255)
792 m_lastKeyDownConsumed = false;
793 #endif
794
795 if (!m_lastKeyDownConsumed && !skip) {
796 #if wxUSE_UNICODE
797 int key = evt.GetUnicodeKey();
798 bool keyOk = true;
799
800 // if the unicode key code is not really a unicode character (it may
801 // be a function key or etc., the platforms appear to always give us a
802 // small value in this case) then fallback to the ascii key code but
803 // don't do anything for function keys or etc.
804 if (key <= 127) {
805 key = evt.GetKeyCode();
806 keyOk = (key <= 127);
807 }
808 if (keyOk) {
809 m_swx->DoAddChar(key);
810 return;
811 }
812 #else
813 int key = evt.GetKeyCode();
814 if (key <= WXK_START || key > WXK_COMMAND) {
815 m_swx->DoAddChar(key);
816 return;
817 }
818 #endif
819 }
820
821 evt.Skip();
822 }
823
824
825 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) {
826 int processed = m_swx->DoKeyDown(evt, &m_lastKeyDownConsumed);
827 if (!processed && !m_lastKeyDownConsumed)
828 evt.Skip();
829 }
830
831
832 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) {
833 m_swx->DoLoseFocus();
834 evt.Skip();
835 }
836
837
838 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) {
839 m_swx->DoGainFocus();
840 evt.Skip();
841 }
842
843
844 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(evt)) {
845 m_swx->DoSysColourChange();
846 }
847
848
849 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt)) {
850 // do nothing to help avoid flashing
851 }
852
853
854
855 void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) {
856 m_swx->DoCommand(evt.GetId());
857 }
858
859
860 void wxStyledTextCtrl::OnListBox(wxCommandEvent& WXUNUSED(evt)) {
861 m_swx->DoOnListBox();
862 }
863
864
865 void wxStyledTextCtrl::OnIdle(wxIdleEvent& evt) {
866 m_swx->DoOnIdle(evt);
867 }
868
869
870 wxSize wxStyledTextCtrl::DoGetBestSize() const
871 {
872 // What would be the best size for a wxSTC?
873 // Just give a reasonable minimum until something else can be figured out.
874 return wxSize(200,100);
875 }
876
877
878 //----------------------------------------------------------------------
879 // Turn notifications from Scintilla into events
880
881
882 void wxStyledTextCtrl::NotifyChange() {
883 wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId());
884 evt.SetEventObject(this);
885 GetEventHandler()->ProcessEvent(evt);
886 }
887
888
889 static void SetEventText(wxStyledTextEvent& evt, const char* text,
890 size_t length) {
891 if(!text) return;
892
893 evt.SetText(stc2wx(text, length));
894 }
895
896
897 void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) {
898 SCNotification& scn = *_scn;
899 wxStyledTextEvent evt(0, GetId());
900
901 evt.SetEventObject(this);
902 evt.SetPosition(scn.position);
903 evt.SetKey(scn.ch);
904 evt.SetModifiers(scn.modifiers);
905
906 switch (scn.nmhdr.code) {
907 case SCN_STYLENEEDED:
908 evt.SetEventType(wxEVT_STC_STYLENEEDED);
909 break;
910
911 case SCN_CHARADDED:
912 evt.SetEventType(wxEVT_STC_CHARADDED);
913 break;
914
915 case SCN_SAVEPOINTREACHED:
916 evt.SetEventType(wxEVT_STC_SAVEPOINTREACHED);
917 break;
918
919 case SCN_SAVEPOINTLEFT:
920 evt.SetEventType(wxEVT_STC_SAVEPOINTLEFT);
921 break;
922
923 case SCN_MODIFYATTEMPTRO:
924 evt.SetEventType(wxEVT_STC_ROMODIFYATTEMPT);
925 break;
926
927 case SCN_KEY:
928 evt.SetEventType(wxEVT_STC_KEY);
929 break;
930
931 case SCN_DOUBLECLICK:
932 evt.SetEventType(wxEVT_STC_DOUBLECLICK);
933 break;
934
935 case SCN_UPDATEUI:
936 evt.SetEventType(wxEVT_STC_UPDATEUI);
937 break;
938
939 case SCN_MODIFIED:
940 evt.SetEventType(wxEVT_STC_MODIFIED);
941 evt.SetModificationType(scn.modificationType);
942 SetEventText(evt, scn.text, scn.length);
943 evt.SetLength(scn.length);
944 evt.SetLinesAdded(scn.linesAdded);
945 evt.SetLine(scn.line);
946 evt.SetFoldLevelNow(scn.foldLevelNow);
947 evt.SetFoldLevelPrev(scn.foldLevelPrev);
948 break;
949
950 case SCN_MACRORECORD:
951 evt.SetEventType(wxEVT_STC_MACRORECORD);
952 evt.SetMessage(scn.message);
953 evt.SetWParam(scn.wParam);
954 evt.SetLParam(scn.lParam);
955 break;
956
957 case SCN_MARGINCLICK:
958 evt.SetEventType(wxEVT_STC_MARGINCLICK);
959 evt.SetMargin(scn.margin);
960 break;
961
962 case SCN_NEEDSHOWN:
963 evt.SetEventType(wxEVT_STC_NEEDSHOWN);
964 evt.SetLength(scn.length);
965 break;
966
967 case SCN_PAINTED:
968 evt.SetEventType(wxEVT_STC_PAINTED);
969 break;
970
971 case SCN_AUTOCSELECTION:
972 evt.SetEventType(wxEVT_STC_AUTOCOMP_SELECTION);
973 evt.SetListType(scn.listType);
974 SetEventText(evt, scn.text, strlen(scn.text));
975 evt.SetPosition(scn.lParam);
976 break;
977
978 case SCN_USERLISTSELECTION:
979 evt.SetEventType(wxEVT_STC_USERLISTSELECTION);
980 evt.SetListType(scn.listType);
981 SetEventText(evt, scn.text, strlen(scn.text));
982 evt.SetPosition(scn.lParam);
983 break;
984
985 case SCN_URIDROPPED:
986 evt.SetEventType(wxEVT_STC_URIDROPPED);
987 SetEventText(evt, scn.text, strlen(scn.text));
988 break;
989
990 case SCN_DWELLSTART:
991 evt.SetEventType(wxEVT_STC_DWELLSTART);
992 evt.SetX(scn.x);
993 evt.SetY(scn.y);
994 break;
995
996 case SCN_DWELLEND:
997 evt.SetEventType(wxEVT_STC_DWELLEND);
998 evt.SetX(scn.x);
999 evt.SetY(scn.y);
1000 break;
1001
1002 case SCN_ZOOM:
1003 evt.SetEventType(wxEVT_STC_ZOOM);
1004 break;
1005
1006 case SCN_HOTSPOTCLICK:
1007 evt.SetEventType(wxEVT_STC_HOTSPOT_CLICK);
1008 break;
1009
1010 case SCN_HOTSPOTDOUBLECLICK:
1011 evt.SetEventType(wxEVT_STC_HOTSPOT_DCLICK);
1012 break;
1013
1014 case SCN_CALLTIPCLICK:
1015 evt.SetEventType(wxEVT_STC_CALLTIP_CLICK);
1016 break;
1017
1018 case SCN_INDICATORCLICK:
1019 evt.SetEventType(wxEVT_STC_INDICATOR_CLICK);
1020 break;
1021
1022 case SCN_INDICATORRELEASE:
1023 evt.SetEventType(wxEVT_STC_INDICATOR_RELEASE);
1024 break;
1025
1026 case SCN_AUTOCCANCELLED:
1027 evt.SetEventType(wxEVT_STC_AUTOCOMP_CANCELLED);
1028 break;
1029
1030 case SCN_AUTOCCHARDELETED:
1031 evt.SetEventType(wxEVT_STC_AUTOCOMP_CHAR_DELETED);
1032 break;
1033
1034 default:
1035 return;
1036 }
1037
1038 GetEventHandler()->ProcessEvent(evt);
1039 }
1040
1041
1042 //----------------------------------------------------------------------
1043 //----------------------------------------------------------------------
1044 //----------------------------------------------------------------------
1045
1046 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id)
1047 : wxCommandEvent(commandType, id)
1048 {
1049 m_position = 0;
1050 m_key = 0;
1051 m_modifiers = 0;
1052 m_modificationType = 0;
1053 m_length = 0;
1054 m_linesAdded = 0;
1055 m_line = 0;
1056 m_foldLevelNow = 0;
1057 m_foldLevelPrev = 0;
1058 m_margin = 0;
1059 m_message = 0;
1060 m_wParam = 0;
1061 m_lParam = 0;
1062 m_listType = 0;
1063 m_x = 0;
1064 m_y = 0;
1065 #if wxUSE_DRAG_AND_DROP
1066 m_dragFlags = wxDrag_CopyOnly;
1067 m_dragResult = wxDragNone;
1068 #endif
1069 }
1070
1071 bool wxStyledTextEvent::GetShift() const { return (m_modifiers & SCI_SHIFT) != 0; }
1072 bool wxStyledTextEvent::GetControl() const { return (m_modifiers & SCI_CTRL) != 0; }
1073 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers & SCI_ALT) != 0; }
1074
1075
1076 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent& event):
1077 wxCommandEvent(event)
1078 {
1079 m_position = event.m_position;
1080 m_key = event.m_key;
1081 m_modifiers = event.m_modifiers;
1082 m_modificationType = event.m_modificationType;
1083 m_text = event.m_text;
1084 m_length = event.m_length;
1085 m_linesAdded = event.m_linesAdded;
1086 m_line = event.m_line;
1087 m_foldLevelNow = event.m_foldLevelNow;
1088 m_foldLevelPrev = event.m_foldLevelPrev;
1089
1090 m_margin = event.m_margin;
1091
1092 m_message = event.m_message;
1093 m_wParam = event.m_wParam;
1094 m_lParam = event.m_lParam;
1095
1096 m_listType = event.m_listType;
1097 m_x = event.m_x;
1098 m_y = event.m_y;
1099
1100 #if wxUSE_DRAG_AND_DROP
1101 m_dragText = event.m_dragText;
1102 m_dragFlags = event.m_dragFlags;
1103 m_dragResult = event.m_dragResult;
1104 #endif
1105 }
1106
1107 //----------------------------------------------------------------------
1108 //----------------------------------------------------------------------
1109
1110 /*static*/ wxVersionInfo wxStyledTextCtrl::GetLibraryVersionInfo()
1111 {
1112 return wxVersionInfo("Scintilla", 2, 3, 0, "Scintilla 2.03");
1113 }
1114
1115 #endif // wxUSE_STC