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