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