Refactor wxStyledTextCtrl to share common file save/load code.
[wxWidgets.git] / src / stc / stc.cpp.in
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: stc.cpp
3 // Purpose: A wxWidgets implementation of Scintilla. This class is the
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 /*
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
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #if wxUSE_STC
37
38 #include "wx/stc/stc.h"
39 #include "wx/stc/private.h"
40
41 #ifndef WX_PRECOMP
42 #include "wx/wx.h"
43 #endif // WX_PRECOMP
44
45 #include <ctype.h>
46
47 #include "wx/tokenzr.h"
48 #include "wx/mstream.h"
49 #include "wx/image.h"
50 #include "wx/ffile.h"
51
52 #include "ScintillaWX.h"
53
54 //----------------------------------------------------------------------
55
56 const char wxSTCNameStr[] = "stcwindow";
57
58 #ifdef MAKELONG
59 #undef MAKELONG
60 #endif
61
62 #define MAKELONG(a, b) ((a) | ((b) << 16))
63
64
65 static long wxColourAsLong(const wxColour& co) {
66 return (((long)co.Blue() << 16) |
67 ((long)co.Green() << 8) |
68 ((long)co.Red()));
69 }
70
71 static wxColour wxColourFromLong(long c) {
72 wxColour clr;
73 clr.Set((unsigned char)(c & 0xff),
74 (unsigned char)((c >> 8) & 0xff),
75 (unsigned char)((c >> 16) & 0xff));
76 return clr;
77 }
78
79
80 static wxColour wxColourFromSpec(const wxString& spec) {
81 // spec should be a colour name or "#RRGGBB"
82 if (spec.GetChar(0) == wxT('#')) {
83
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);
89 return wxColour((unsigned char)red,
90 (unsigned char)green,
91 (unsigned char)blue);
92 }
93 else
94 return wxColour(spec);
95 }
96
97 //----------------------------------------------------------------------
98
99 wxDEFINE_EVENT( wxEVT_STC_CHANGE, wxStyledTextEvent );
100 wxDEFINE_EVENT( wxEVT_STC_STYLENEEDED, wxStyledTextEvent );
101 wxDEFINE_EVENT( wxEVT_STC_CHARADDED, wxStyledTextEvent );
102 wxDEFINE_EVENT( wxEVT_STC_SAVEPOINTREACHED, wxStyledTextEvent );
103 wxDEFINE_EVENT( wxEVT_STC_SAVEPOINTLEFT, wxStyledTextEvent );
104 wxDEFINE_EVENT( wxEVT_STC_ROMODIFYATTEMPT, wxStyledTextEvent );
105 wxDEFINE_EVENT( wxEVT_STC_KEY, wxStyledTextEvent );
106 wxDEFINE_EVENT( wxEVT_STC_DOUBLECLICK, wxStyledTextEvent );
107 wxDEFINE_EVENT( wxEVT_STC_UPDATEUI, wxStyledTextEvent );
108 wxDEFINE_EVENT( wxEVT_STC_MODIFIED, wxStyledTextEvent );
109 wxDEFINE_EVENT( wxEVT_STC_MACRORECORD, wxStyledTextEvent );
110 wxDEFINE_EVENT( wxEVT_STC_MARGINCLICK, wxStyledTextEvent );
111 wxDEFINE_EVENT( wxEVT_STC_NEEDSHOWN, wxStyledTextEvent );
112 wxDEFINE_EVENT( wxEVT_STC_PAINTED, wxStyledTextEvent );
113 wxDEFINE_EVENT( wxEVT_STC_USERLISTSELECTION, wxStyledTextEvent );
114 wxDEFINE_EVENT( wxEVT_STC_URIDROPPED, wxStyledTextEvent );
115 wxDEFINE_EVENT( wxEVT_STC_DWELLSTART, wxStyledTextEvent );
116 wxDEFINE_EVENT( wxEVT_STC_DWELLEND, wxStyledTextEvent );
117 wxDEFINE_EVENT( wxEVT_STC_START_DRAG, wxStyledTextEvent );
118 wxDEFINE_EVENT( wxEVT_STC_DRAG_OVER, wxStyledTextEvent );
119 wxDEFINE_EVENT( wxEVT_STC_DO_DROP, wxStyledTextEvent );
120 wxDEFINE_EVENT( wxEVT_STC_ZOOM, wxStyledTextEvent );
121 wxDEFINE_EVENT( wxEVT_STC_HOTSPOT_CLICK, wxStyledTextEvent );
122 wxDEFINE_EVENT( wxEVT_STC_HOTSPOT_DCLICK, wxStyledTextEvent );
123 wxDEFINE_EVENT( wxEVT_STC_CALLTIP_CLICK, wxStyledTextEvent );
124 wxDEFINE_EVENT( wxEVT_STC_AUTOCOMP_SELECTION, wxStyledTextEvent );
125 wxDEFINE_EVENT( wxEVT_STC_INDICATOR_CLICK, wxStyledTextEvent );
126 wxDEFINE_EVENT( wxEVT_STC_INDICATOR_RELEASE, wxStyledTextEvent );
127
128
129
130 BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl)
131 EVT_PAINT (wxStyledTextCtrl::OnPaint)
132 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin)
133 EVT_SCROLL (wxStyledTextCtrl::OnScroll)
134 EVT_SIZE (wxStyledTextCtrl::OnSize)
135 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown)
136 // Let Scintilla see the double click as a second click
137 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown)
138 EVT_MOTION (wxStyledTextCtrl::OnMouseMove)
139 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp)
140 #if defined(__WXGTK__) || defined(__WXMAC__)
141 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp)
142 #else
143 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu)
144 #endif
145 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel)
146 EVT_MIDDLE_UP (wxStyledTextCtrl::OnMouseMiddleUp)
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)
153 EVT_MENU_RANGE (10, 16, wxStyledTextCtrl::OnMenu)
154 EVT_LISTBOX_DCLICK (wxID_ANY, wxStyledTextCtrl::OnListBox)
155 END_EVENT_TABLE()
156
157
158 IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
159 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
160
161 #ifdef LINK_LEXERS
162 // forces the linking of the lexer modules
163 int Scintilla_LinkLexers();
164 #endif
165
166 //----------------------------------------------------------------------
167 // Constructor and Destructor
168
169 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent,
170 wxWindowID id,
171 const wxPoint& pos,
172 const wxSize& size,
173 long style,
174 const wxString& name)
175 {
176 m_swx = NULL;
177 Create(parent, id, pos, size, style, name);
178 }
179
180
181 bool wxStyledTextCtrl::Create(wxWindow *parent,
182 wxWindowID id,
183 const wxPoint& pos,
184 const wxSize& size,
185 long style,
186 const wxString& name)
187 {
188 style |= wxVSCROLL | wxHSCROLL;
189 if (!wxControl::Create(parent, id, pos, size,
190 style | wxWANTS_CHARS | wxCLIP_CHILDREN,
191 wxDefaultValidator, name))
192 return false;
193
194 #ifdef LINK_LEXERS
195 Scintilla_LinkLexers();
196 #endif
197 m_swx = new ScintillaWX(this);
198 m_stopWatch.Start();
199 m_lastKeyDownConsumed = false;
200 m_lastWheelTimestamp = 0;
201 m_vScrollBar = NULL;
202 m_hScrollBar = NULL;
203 #if wxUSE_UNICODE
204 // Put Scintilla into unicode (UTF-8) mode
205 SetCodePage(wxSTC_CP_UTF8);
206 #endif
207
208 SetInitialSize(size);
209
210 // Reduces flicker on GTK+/X11
211 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
212
213 // Make sure it can take the focus
214 SetCanFocus(true);
215
216 return true;
217 }
218
219
220 wxStyledTextCtrl::~wxStyledTextCtrl() {
221 delete m_swx;
222 }
223
224
225 //----------------------------------------------------------------------
226
227 wxIntPtr wxStyledTextCtrl::SendMsg(int msg, wxUIntPtr wp, wxIntPtr lp) const
228 {
229 return m_swx->WndProc(msg, wp, lp);
230 }
231
232 //----------------------------------------------------------------------
233
234 // Set the vertical scrollbar to use instead of the ont that's built-in.
235 void 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 }
242
243
244 // Set the horizontal scrollbar to use instead of the ont that's built-in.
245 void 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
253 //----------------------------------------------------------------------
254 // Generated methods implementation section {{{
255
256 %(METHOD_IMPS)s
257
258 //}}}
259 //----------------------------------------------------------------------
260
261
262 // Returns the line number of the line with the caret.
263 int 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
274 // fore:[name or #RRGGBB] sets the foreground colour
275 // back:[name or #RRGGBB] sets the background colour
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 //
281 void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
282
283 wxStringTokenizer tkz(spec, wxT(","));
284 while (tkz.HasMoreTokens()) {
285 wxString token = tkz.GetNextToken();
286
287 wxString option = token.BeforeFirst(':');
288 wxString val = token.AfterFirst(':');
289
290 if (option == wxT("bold"))
291 StyleSetBold(styleNum, true);
292
293 else if (option == wxT("italic"))
294 StyleSetItalic(styleNum, true);
295
296 else if (option == wxT("underline"))
297 StyleSetUnderline(styleNum, true);
298
299 else if (option == wxT("eol"))
300 StyleSetEOLFilled(styleNum, true);
301
302 else if (option == wxT("size")) {
303 long points;
304 if (val.ToLong(&points))
305 StyleSetSize(styleNum, points);
306 }
307
308 else if (option == wxT("face"))
309 StyleSetFaceName(styleNum, val);
310
311 else if (option == wxT("fore"))
312 StyleSetForeground(styleNum, wxColourFromSpec(val));
313
314 else if (option == wxT("back"))
315 StyleSetBackground(styleNum, wxColourFromSpec(val));
316 }
317 }
318
319
320 // Get the font of a style
321 wxFont 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
339 // Set style size, face, bold, italic, and underline attributes from
340 // a wxFont's attributes.
341 void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) {
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
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();
352 wxFontEncoding encoding = font.GetEncoding();
353
354 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under, encoding);
355 }
356
357 // Set all font style attributes at once.
358 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size,
359 const wxString& faceName,
360 bool bold, bool italic,
361 bool underline,
362 wxFontEncoding encoding) {
363 StyleSetSize(styleNum, size);
364 StyleSetFaceName(styleNum, faceName);
365 StyleSetBold(styleNum, bold);
366 StyleSetItalic(styleNum, italic);
367 StyleSetUnderline(styleNum, underline);
368 StyleSetFontEncoding(styleNum, encoding);
369 }
370
371
372 // Set the character set of the font in a style. Converts the Scintilla
373 // character set values to a wxFontEncoding.
374 void 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;
453
454 case wxSTC_CHARSET_CYRILLIC:
455 encoding = wxFONTENCODING_ISO8859_5;
456 break;
457
458 case wxSTC_CHARSET_8859_15:
459 encoding = wxFONTENCODING_ISO8859_15;;
460 break;
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.
473 void wxStyledTextCtrl::StyleSetFontEncoding(int style, wxFontEncoding encoding)
474 {
475 SendMsg(SCI_STYLESETCHARACTERSET, style, encoding+1);
476 }
477
478
479 // Perform one of the operations defined by the wxSTC_CMD_* constants.
480 void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
481 SendMsg(cmd);
482 }
483
484
485 // Set the left and right margin in the edit area, measured in pixels.
486 void wxStyledTextCtrl::SetMargins(int left, int right) {
487 SetMarginLeft(left);
488 SetMarginRight(right);
489 }
490
491
492 // Retrieve the point in the window where a position is displayed.
493 wxPoint 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
500 void wxStyledTextCtrl::ScrollToLine(int line) {
501 m_swx->DoScrollToLine(line);
502 }
503
504
505 // Scroll enough to make the given column visible
506 void wxStyledTextCtrl::ScrollToColumn(int column) {
507 m_swx->DoScrollToColumn(column);
508 }
509
510
511 #if wxUSE_TEXTCTRL
512 bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int fileType)
513 {
514 bool ok = wxTextAreaBase::DoSaveFile(filename, fileType);
515 #else
516 bool wxStyledTextCtrl::SaveFile(const wxString& filename)
517 {
518 #if wxUSE_FFILE
519 wxFFile file(filename, wxT("w"));
520 bool ok = file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
521 #else
522 bool ok = false;
523 #endif // wxUSE_FFILE
524 #endif
525 if (ok)
526 {
527 SetSavePoint();
528 }
529 return ok;
530 }
531
532 #if wxUSE_TEXTCTRL
533 bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int fileType)
534 {
535 bool ok = wxTextAreaBase::DoLoadFile(filename, fileType);
536 #else
537 bool wxStyledTextCtrl::LoadFile(const wxString& filename)
538 {
539 #if wxUSE_FFILE
540 wxFFile file(filename);
541 bool ok = file.IsOpened();
542 if (ok)
543 {
544 wxString text;
545 ok = file.ReadAll(&text, *wxConvCurrent);
546 if (ok)
547 {
548 SetValue(text);
549 }
550 }
551 #else
552 bool ok = false;
553 #endif // wxUSE_FFILE
554 #endif
555 if (ok)
556 {
557 EmptyUndoBuffer();
558 SetSavePoint();
559 }
560 return ok;
561 }
562
563 #if wxUSE_DRAG_AND_DROP
564 wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
565 return m_swx->DoDragOver(x, y, def);
566 }
567
568
569 bool wxStyledTextCtrl::DoDropText(long x, long y, const wxString& data) {
570 return m_swx->DoDropText(x, y, data);
571 }
572 #endif
573
574
575 void wxStyledTextCtrl::SetUseAntiAliasing(bool useAA) {
576 m_swx->SetUseAntiAliasing(useAA);
577 }
578
579 bool wxStyledTextCtrl::GetUseAntiAliasing() {
580 return m_swx->GetUseAntiAliasing();
581 }
582
583
584
585
586
587 void wxStyledTextCtrl::AddTextRaw(const char* text)
588 {
589 SendMsg(SCI_ADDTEXT, strlen(text), (sptr_t)text);
590 }
591
592 void wxStyledTextCtrl::InsertTextRaw(int pos, const char* text)
593 {
594 SendMsg(SCI_INSERTTEXT, pos, (sptr_t)text);
595 }
596
597 wxCharBuffer wxStyledTextCtrl::GetCurLineRaw(int* linePos)
598 {
599 int len = LineLength(GetCurrentLine());
600 if (!len) {
601 if (linePos) *linePos = 0;
602 wxCharBuffer empty;
603 return empty;
604 }
605
606 wxCharBuffer buf(len);
607 int pos = SendMsg(SCI_GETCURLINE, len, (sptr_t)buf.data());
608 if (linePos) *linePos = pos;
609 return buf;
610 }
611
612 wxCharBuffer wxStyledTextCtrl::GetLineRaw(int line)
613 {
614 int len = LineLength(line);
615 if (!len) {
616 wxCharBuffer empty;
617 return empty;
618 }
619
620 wxCharBuffer buf(len);
621 SendMsg(SCI_GETLINE, line, (sptr_t)buf.data());
622 return buf;
623 }
624
625 wxCharBuffer wxStyledTextCtrl::GetSelectedTextRaw()
626 {
627 long start;
628 long end;
629
630 GetSelection(&start, &end);
631 int len = end - start;
632 if (!len) {
633 wxCharBuffer empty;
634 return empty;
635 }
636
637 wxCharBuffer buf(len);
638 SendMsg(SCI_GETSELTEXT, 0, (sptr_t)buf.data());
639 return buf;
640 }
641
642 wxCharBuffer wxStyledTextCtrl::GetTextRangeRaw(int startPos, int endPos)
643 {
644 if (endPos < startPos) {
645 int temp = startPos;
646 startPos = endPos;
647 endPos = temp;
648 }
649 int len = endPos - startPos;
650 if (!len) {
651 wxCharBuffer empty;
652 return empty;
653 }
654
655 wxCharBuffer buf(len);
656 TextRange tr;
657 tr.lpstrText = buf.data();
658 tr.chrg.cpMin = startPos;
659 tr.chrg.cpMax = endPos;
660 SendMsg(SCI_GETTEXTRANGE, 0, (sptr_t)&tr);
661 return buf;
662 }
663
664 void wxStyledTextCtrl::SetTextRaw(const char* text)
665 {
666 SendMsg(SCI_SETTEXT, 0, (sptr_t)text);
667 }
668
669 wxCharBuffer wxStyledTextCtrl::GetTextRaw()
670 {
671 int len = GetTextLength();
672 wxCharBuffer buf(len); // adds 1 for NUL automatically
673 SendMsg(SCI_GETTEXT, len + 1, (sptr_t)buf.data());
674 return buf;
675 }
676
677 void wxStyledTextCtrl::AppendTextRaw(const char* text)
678 {
679 SendMsg(SCI_APPENDTEXT, strlen(text), (sptr_t)text);
680 }
681
682
683
684
685
686 //----------------------------------------------------------------------
687 // Event handlers
688
689 void wxStyledTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(evt)) {
690 wxPaintDC dc(this);
691 m_swx->DoPaint(&dc, GetUpdateRegion().GetBox());
692 }
693
694 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
695 if (evt.GetOrientation() == wxHORIZONTAL)
696 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
697 else
698 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
699 }
700
701 void wxStyledTextCtrl::OnScroll(wxScrollEvent& evt) {
702 wxScrollBar* sb = wxDynamicCast(evt.GetEventObject(), wxScrollBar);
703 if (sb) {
704 if (sb->IsVertical())
705 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
706 else
707 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
708 }
709 }
710
711 void wxStyledTextCtrl::OnSize(wxSizeEvent& WXUNUSED(evt)) {
712 if (m_swx) {
713 wxSize sz = GetClientSize();
714 m_swx->DoSize(sz.x, sz.y);
715 }
716 }
717
718 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) {
719 SetFocus();
720 wxPoint pt = evt.GetPosition();
721 m_swx->DoLeftButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(),
722 evt.ShiftDown(), evt.ControlDown(), evt.AltDown());
723 }
724
725 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
726 wxPoint pt = evt.GetPosition();
727 m_swx->DoLeftButtonMove(Point(pt.x, pt.y));
728 }
729
730 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
731 wxPoint pt = evt.GetPosition();
732 m_swx->DoLeftButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
733 evt.ControlDown());
734 }
735
736
737 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) {
738 wxPoint pt = evt.GetPosition();
739 m_swx->DoContextMenu(Point(pt.x, pt.y));
740 }
741
742
743 void wxStyledTextCtrl::OnMouseMiddleUp(wxMouseEvent& evt) {
744 wxPoint pt = evt.GetPosition();
745 m_swx->DoMiddleButtonUp(Point(pt.x, pt.y));
746 }
747
748 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) {
749 wxPoint pt = evt.GetPosition();
750 ScreenToClient(&pt.x, &pt.y);
751 /*
752 Show context menu at event point if it's within the window,
753 or at caret location if not
754 */
755 wxHitTest ht = this->HitTest(pt);
756 if (ht != wxHT_WINDOW_INSIDE) {
757 pt = this->PointFromPosition(this->GetCurrentPos());
758 }
759 m_swx->DoContextMenu(Point(pt.x, pt.y));
760 }
761
762
763 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt)
764 {
765 // prevent having an event queue with wheel events that cannot be processed
766 // reasonably fast (see ticket #9057)
767 if ( m_lastWheelTimestamp <= evt.GetTimestamp() )
768 {
769 m_lastWheelTimestamp = m_stopWatch.Time();
770 m_swx->DoMouseWheel(evt.GetWheelRotation(),
771 evt.GetWheelDelta(),
772 evt.GetLinesPerAction(),
773 evt.ControlDown(),
774 evt.IsPageScroll());
775 m_lastWheelTimestamp = m_stopWatch.Time() - m_lastWheelTimestamp;
776 m_lastWheelTimestamp += evt.GetTimestamp();
777 }
778 }
779
780
781 void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) {
782 // On (some?) non-US PC keyboards the AltGr key is required to enter some
783 // common characters. It comes to us as both Alt and Ctrl down so we need
784 // to let the char through in that case, otherwise if only ctrl or only
785 // alt let's skip it.
786 bool ctrl = evt.ControlDown();
787 #ifdef __WXMAC__
788 // On the Mac the Alt key is just a modifier key (like Shift) so we need
789 // to allow the char events to be processed when Alt is pressed.
790 // TODO: Should we check MetaDown instead in this case?
791 bool alt = false;
792 #else
793 bool alt = evt.AltDown();
794 #endif
795 bool skip = ((ctrl || alt) && ! (ctrl && alt));
796
797 #if wxUSE_UNICODE
798 // apparently if we don't do this, Unicode keys pressed after non-char
799 // ASCII ones (e.g. Enter, Tab) are not taken into account (patch 1615989)
800 if (m_lastKeyDownConsumed && evt.GetUnicodeKey() > 255)
801 m_lastKeyDownConsumed = false;
802 #endif
803
804 if (!m_lastKeyDownConsumed && !skip) {
805 #if wxUSE_UNICODE
806 int key = evt.GetUnicodeKey();
807 bool keyOk = true;
808
809 // if the unicode key code is not really a unicode character (it may
810 // be a function key or etc., the platforms appear to always give us a
811 // small value in this case) then fallback to the ascii key code but
812 // don't do anything for function keys or etc.
813 if (key <= 127) {
814 key = evt.GetKeyCode();
815 keyOk = (key <= 127);
816 }
817 if (keyOk) {
818 m_swx->DoAddChar(key);
819 return;
820 }
821 #else
822 int key = evt.GetKeyCode();
823 if (key <= WXK_START || key > WXK_COMMAND) {
824 m_swx->DoAddChar(key);
825 return;
826 }
827 #endif
828 }
829
830 evt.Skip();
831 }
832
833
834 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) {
835 int processed = m_swx->DoKeyDown(evt, &m_lastKeyDownConsumed);
836 if (!processed && !m_lastKeyDownConsumed)
837 evt.Skip();
838 }
839
840
841 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) {
842 m_swx->DoLoseFocus();
843 evt.Skip();
844 }
845
846
847 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) {
848 m_swx->DoGainFocus();
849 evt.Skip();
850 }
851
852
853 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(evt)) {
854 m_swx->DoSysColourChange();
855 }
856
857
858 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt)) {
859 // do nothing to help avoid flashing
860 }
861
862
863
864 void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) {
865 m_swx->DoCommand(evt.GetId());
866 }
867
868
869 void wxStyledTextCtrl::OnListBox(wxCommandEvent& WXUNUSED(evt)) {
870 m_swx->DoOnListBox();
871 }
872
873
874 void wxStyledTextCtrl::OnIdle(wxIdleEvent& evt) {
875 m_swx->DoOnIdle(evt);
876 }
877
878
879 wxSize wxStyledTextCtrl::DoGetBestSize() const
880 {
881 // What would be the best size for a wxSTC?
882 // Just give a reasonable minimum until something else can be figured out.
883 return wxSize(200,100);
884 }
885
886
887 //----------------------------------------------------------------------
888 // Turn notifications from Scintilla into events
889
890
891 void wxStyledTextCtrl::NotifyChange() {
892 wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId());
893 evt.SetEventObject(this);
894 GetEventHandler()->ProcessEvent(evt);
895 }
896
897
898 static void SetEventText(wxStyledTextEvent& evt, const char* text,
899 size_t length) {
900 if(!text) return;
901
902 evt.SetText(stc2wx(text, length));
903 }
904
905
906 void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) {
907 SCNotification& scn = *_scn;
908 wxStyledTextEvent evt(0, GetId());
909
910 evt.SetEventObject(this);
911 evt.SetPosition(scn.position);
912 evt.SetKey(scn.ch);
913 evt.SetModifiers(scn.modifiers);
914
915 switch (scn.nmhdr.code) {
916 case SCN_STYLENEEDED:
917 evt.SetEventType(wxEVT_STC_STYLENEEDED);
918 break;
919
920 case SCN_CHARADDED:
921 evt.SetEventType(wxEVT_STC_CHARADDED);
922 break;
923
924 case SCN_SAVEPOINTREACHED:
925 evt.SetEventType(wxEVT_STC_SAVEPOINTREACHED);
926 break;
927
928 case SCN_SAVEPOINTLEFT:
929 evt.SetEventType(wxEVT_STC_SAVEPOINTLEFT);
930 break;
931
932 case SCN_MODIFYATTEMPTRO:
933 evt.SetEventType(wxEVT_STC_ROMODIFYATTEMPT);
934 break;
935
936 case SCN_KEY:
937 evt.SetEventType(wxEVT_STC_KEY);
938 break;
939
940 case SCN_DOUBLECLICK:
941 evt.SetEventType(wxEVT_STC_DOUBLECLICK);
942 break;
943
944 case SCN_UPDATEUI:
945 evt.SetEventType(wxEVT_STC_UPDATEUI);
946 break;
947
948 case SCN_MODIFIED:
949 evt.SetEventType(wxEVT_STC_MODIFIED);
950 evt.SetModificationType(scn.modificationType);
951 SetEventText(evt, scn.text, scn.length);
952 evt.SetLength(scn.length);
953 evt.SetLinesAdded(scn.linesAdded);
954 evt.SetLine(scn.line);
955 evt.SetFoldLevelNow(scn.foldLevelNow);
956 evt.SetFoldLevelPrev(scn.foldLevelPrev);
957 break;
958
959 case SCN_MACRORECORD:
960 evt.SetEventType(wxEVT_STC_MACRORECORD);
961 evt.SetMessage(scn.message);
962 evt.SetWParam(scn.wParam);
963 evt.SetLParam(scn.lParam);
964 break;
965
966 case SCN_MARGINCLICK:
967 evt.SetEventType(wxEVT_STC_MARGINCLICK);
968 evt.SetMargin(scn.margin);
969 break;
970
971 case SCN_NEEDSHOWN:
972 evt.SetEventType(wxEVT_STC_NEEDSHOWN);
973 evt.SetLength(scn.length);
974 break;
975
976 case SCN_PAINTED:
977 evt.SetEventType(wxEVT_STC_PAINTED);
978 break;
979
980 case SCN_AUTOCSELECTION:
981 evt.SetEventType(wxEVT_STC_AUTOCOMP_SELECTION);
982 evt.SetListType(scn.listType);
983 SetEventText(evt, scn.text, strlen(scn.text));
984 evt.SetPosition(scn.lParam);
985 break;
986
987 case SCN_USERLISTSELECTION:
988 evt.SetEventType(wxEVT_STC_USERLISTSELECTION);
989 evt.SetListType(scn.listType);
990 SetEventText(evt, scn.text, strlen(scn.text));
991 evt.SetPosition(scn.lParam);
992 break;
993
994 case SCN_URIDROPPED:
995 evt.SetEventType(wxEVT_STC_URIDROPPED);
996 SetEventText(evt, scn.text, strlen(scn.text));
997 break;
998
999 case SCN_DWELLSTART:
1000 evt.SetEventType(wxEVT_STC_DWELLSTART);
1001 evt.SetX(scn.x);
1002 evt.SetY(scn.y);
1003 break;
1004
1005 case SCN_DWELLEND:
1006 evt.SetEventType(wxEVT_STC_DWELLEND);
1007 evt.SetX(scn.x);
1008 evt.SetY(scn.y);
1009 break;
1010
1011 case SCN_ZOOM:
1012 evt.SetEventType(wxEVT_STC_ZOOM);
1013 break;
1014
1015 case SCN_HOTSPOTCLICK:
1016 evt.SetEventType(wxEVT_STC_HOTSPOT_CLICK);
1017 break;
1018
1019 case SCN_HOTSPOTDOUBLECLICK:
1020 evt.SetEventType(wxEVT_STC_HOTSPOT_DCLICK);
1021 break;
1022
1023 case SCN_CALLTIPCLICK:
1024 evt.SetEventType(wxEVT_STC_CALLTIP_CLICK);
1025 break;
1026
1027 case SCN_INDICATORCLICK:
1028 evt.SetEventType(wxEVT_STC_INDICATOR_CLICK);
1029 break;
1030
1031 case SCN_INDICATORRELEASE:
1032 evt.SetEventType(wxEVT_STC_INDICATOR_RELEASE);
1033 break;
1034
1035 default:
1036 return;
1037 }
1038
1039 GetEventHandler()->ProcessEvent(evt);
1040 }
1041
1042
1043 //----------------------------------------------------------------------
1044 //----------------------------------------------------------------------
1045 //----------------------------------------------------------------------
1046
1047 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id)
1048 : wxCommandEvent(commandType, id)
1049 {
1050 m_position = 0;
1051 m_key = 0;
1052 m_modifiers = 0;
1053 m_modificationType = 0;
1054 m_length = 0;
1055 m_linesAdded = 0;
1056 m_line = 0;
1057 m_foldLevelNow = 0;
1058 m_foldLevelPrev = 0;
1059 m_margin = 0;
1060 m_message = 0;
1061 m_wParam = 0;
1062 m_lParam = 0;
1063 m_listType = 0;
1064 m_x = 0;
1065 m_y = 0;
1066 m_dragAllowMove = false;
1067 #if wxUSE_DRAG_AND_DROP
1068 m_dragResult = wxDragNone;
1069 #endif
1070 }
1071
1072 bool wxStyledTextEvent::GetShift() const { return (m_modifiers & SCI_SHIFT) != 0; }
1073 bool wxStyledTextEvent::GetControl() const { return (m_modifiers & SCI_CTRL) != 0; }
1074 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers & SCI_ALT) != 0; }
1075
1076
1077 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent& event):
1078 wxCommandEvent(event)
1079 {
1080 m_position = event.m_position;
1081 m_key = event.m_key;
1082 m_modifiers = event.m_modifiers;
1083 m_modificationType = event.m_modificationType;
1084 m_text = event.m_text;
1085 m_length = event.m_length;
1086 m_linesAdded = event.m_linesAdded;
1087 m_line = event.m_line;
1088 m_foldLevelNow = event.m_foldLevelNow;
1089 m_foldLevelPrev = event.m_foldLevelPrev;
1090
1091 m_margin = event.m_margin;
1092
1093 m_message = event.m_message;
1094 m_wParam = event.m_wParam;
1095 m_lParam = event.m_lParam;
1096
1097 m_listType = event.m_listType;
1098 m_x = event.m_x;
1099 m_y = event.m_y;
1100
1101 m_dragText = event.m_dragText;
1102 m_dragAllowMove =event.m_dragAllowMove;
1103 #if wxUSE_DRAG_AND_DROP
1104 m_dragResult = event.m_dragResult;
1105 #endif
1106 }
1107
1108 //----------------------------------------------------------------------
1109 //----------------------------------------------------------------------
1110
1111 #endif // wxUSE_STC