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