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