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