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