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