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