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