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