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