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