]> git.saurik.com Git - wxWidgets.git/blob - src/stc/stc.cpp.in
Add middle-item click support.
[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 #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 (!m_lastKeyDownConsumed && !skip) {
760 #if wxUSE_UNICODE
761 int key = evt.GetUnicodeKey();
762 bool keyOk = true;
763
764 // if the unicode key code is not really a unicode character (it may
765 // be a function key or etc., the platforms appear to always give us a
766 // small value in this case) then fallback to the ascii key code but
767 // don't do anything for function keys or etc.
768 if (key <= 127) {
769 key = evt.GetKeyCode();
770 keyOk = (key <= 127);
771 }
772 if (keyOk) {
773 m_swx->DoAddChar(key);
774 return;
775 }
776 #else
777 int key = evt.GetKeyCode();
778 if (key <= WXK_START || key > WXK_COMMAND) {
779 m_swx->DoAddChar(key);
780 return;
781 }
782 #endif
783 }
784
785 evt.Skip();
786 }
787
788
789 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) {
790 int processed = m_swx->DoKeyDown(evt, &m_lastKeyDownConsumed);
791 if (!processed && !m_lastKeyDownConsumed)
792 evt.Skip();
793 }
794
795
796 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) {
797 m_swx->DoLoseFocus();
798 evt.Skip();
799 }
800
801
802 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) {
803 m_swx->DoGainFocus();
804 evt.Skip();
805 }
806
807
808 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(evt)) {
809 m_swx->DoSysColourChange();
810 }
811
812
813 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt)) {
814 // do nothing to help avoid flashing
815 }
816
817
818
819 void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) {
820 m_swx->DoCommand(evt.GetId());
821 }
822
823
824 void wxStyledTextCtrl::OnListBox(wxCommandEvent& WXUNUSED(evt)) {
825 m_swx->DoOnListBox();
826 }
827
828
829 void wxStyledTextCtrl::OnIdle(wxIdleEvent& evt) {
830 m_swx->DoOnIdle(evt);
831 }
832
833
834 wxSize wxStyledTextCtrl::DoGetBestSize() const
835 {
836 // What would be the best size for a wxSTC?
837 // Just give a reasonable minimum until something else can be figured out.
838 return wxSize(200,100);
839 }
840
841
842 //----------------------------------------------------------------------
843 // Turn notifications from Scintilla into events
844
845
846 void wxStyledTextCtrl::NotifyChange() {
847 wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId());
848 evt.SetEventObject(this);
849 GetEventHandler()->ProcessEvent(evt);
850 }
851
852
853 static void SetEventText(wxStyledTextEvent& evt, const char* text,
854 size_t length) {
855 if(!text) return;
856
857 evt.SetText(stc2wx(text, length));
858 }
859
860
861 void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) {
862 SCNotification& scn = *_scn;
863 wxStyledTextEvent evt(0, GetId());
864
865 evt.SetEventObject(this);
866 evt.SetPosition(scn.position);
867 evt.SetKey(scn.ch);
868 evt.SetModifiers(scn.modifiers);
869
870 switch (scn.nmhdr.code) {
871 case SCN_STYLENEEDED:
872 evt.SetEventType(wxEVT_STC_STYLENEEDED);
873 break;
874
875 case SCN_CHARADDED:
876 evt.SetEventType(wxEVT_STC_CHARADDED);
877 break;
878
879 case SCN_SAVEPOINTREACHED:
880 evt.SetEventType(wxEVT_STC_SAVEPOINTREACHED);
881 break;
882
883 case SCN_SAVEPOINTLEFT:
884 evt.SetEventType(wxEVT_STC_SAVEPOINTLEFT);
885 break;
886
887 case SCN_MODIFYATTEMPTRO:
888 evt.SetEventType(wxEVT_STC_ROMODIFYATTEMPT);
889 break;
890
891 case SCN_KEY:
892 evt.SetEventType(wxEVT_STC_KEY);
893 break;
894
895 case SCN_DOUBLECLICK:
896 evt.SetEventType(wxEVT_STC_DOUBLECLICK);
897 break;
898
899 case SCN_UPDATEUI:
900 evt.SetEventType(wxEVT_STC_UPDATEUI);
901 break;
902
903 case SCN_MODIFIED:
904 evt.SetEventType(wxEVT_STC_MODIFIED);
905 evt.SetModificationType(scn.modificationType);
906 SetEventText(evt, scn.text, scn.length);
907 evt.SetLength(scn.length);
908 evt.SetLinesAdded(scn.linesAdded);
909 evt.SetLine(scn.line);
910 evt.SetFoldLevelNow(scn.foldLevelNow);
911 evt.SetFoldLevelPrev(scn.foldLevelPrev);
912 break;
913
914 case SCN_MACRORECORD:
915 evt.SetEventType(wxEVT_STC_MACRORECORD);
916 evt.SetMessage(scn.message);
917 evt.SetWParam(scn.wParam);
918 evt.SetLParam(scn.lParam);
919 break;
920
921 case SCN_MARGINCLICK:
922 evt.SetEventType(wxEVT_STC_MARGINCLICK);
923 evt.SetMargin(scn.margin);
924 break;
925
926 case SCN_NEEDSHOWN:
927 evt.SetEventType(wxEVT_STC_NEEDSHOWN);
928 evt.SetLength(scn.length);
929 break;
930
931 case SCN_PAINTED:
932 evt.SetEventType(wxEVT_STC_PAINTED);
933 break;
934
935 case SCN_USERLISTSELECTION:
936 evt.SetEventType(wxEVT_STC_USERLISTSELECTION);
937 evt.SetListType(scn.listType);
938 SetEventText(evt, scn.text, strlen(scn.text));
939 break;
940
941 case SCN_URIDROPPED:
942 evt.SetEventType(wxEVT_STC_URIDROPPED);
943 SetEventText(evt, scn.text, strlen(scn.text));
944 break;
945
946 case SCN_DWELLSTART:
947 evt.SetEventType(wxEVT_STC_DWELLSTART);
948 evt.SetX(scn.x);
949 evt.SetY(scn.y);
950 break;
951
952 case SCN_DWELLEND:
953 evt.SetEventType(wxEVT_STC_DWELLEND);
954 evt.SetX(scn.x);
955 evt.SetY(scn.y);
956 break;
957
958 case SCN_ZOOM:
959 evt.SetEventType(wxEVT_STC_ZOOM);
960 break;
961
962 case SCN_HOTSPOTCLICK:
963 evt.SetEventType(wxEVT_STC_HOTSPOT_CLICK);
964 break;
965
966 case SCN_HOTSPOTDOUBLECLICK:
967 evt.SetEventType(wxEVT_STC_HOTSPOT_DCLICK);
968 break;
969
970 case SCN_CALLTIPCLICK:
971 evt.SetEventType(wxEVT_STC_CALLTIP_CLICK);
972 break;
973
974 case SCN_AUTOCSELECTION:
975 evt.SetEventType(wxEVT_STC_AUTOCOMP_SELECTION);
976 break;
977
978 default:
979 return;
980 }
981
982 GetEventHandler()->ProcessEvent(evt);
983 }
984
985
986 //----------------------------------------------------------------------
987 //----------------------------------------------------------------------
988 //----------------------------------------------------------------------
989
990 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id)
991 : wxCommandEvent(commandType, id)
992 {
993 m_position = 0;
994 m_key = 0;
995 m_modifiers = 0;
996 m_modificationType = 0;
997 m_length = 0;
998 m_linesAdded = 0;
999 m_line = 0;
1000 m_foldLevelNow = 0;
1001 m_foldLevelPrev = 0;
1002 m_margin = 0;
1003 m_message = 0;
1004 m_wParam = 0;
1005 m_lParam = 0;
1006 m_listType = 0;
1007 m_x = 0;
1008 m_y = 0;
1009 m_dragAllowMove = false;
1010 #if wxUSE_DRAG_AND_DROP
1011 m_dragResult = wxDragNone;
1012 #endif
1013 }
1014
1015 bool wxStyledTextEvent::GetShift() const { return (m_modifiers & SCI_SHIFT) != 0; }
1016 bool wxStyledTextEvent::GetControl() const { return (m_modifiers & SCI_CTRL) != 0; }
1017 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers & SCI_ALT) != 0; }
1018
1019
1020 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent& event):
1021 wxCommandEvent(event)
1022 {
1023 m_position = event.m_position;
1024 m_key = event.m_key;
1025 m_modifiers = event.m_modifiers;
1026 m_modificationType = event.m_modificationType;
1027 m_text = event.m_text;
1028 m_length = event.m_length;
1029 m_linesAdded = event.m_linesAdded;
1030 m_line = event.m_line;
1031 m_foldLevelNow = event.m_foldLevelNow;
1032 m_foldLevelPrev = event.m_foldLevelPrev;
1033
1034 m_margin = event.m_margin;
1035
1036 m_message = event.m_message;
1037 m_wParam = event.m_wParam;
1038 m_lParam = event.m_lParam;
1039
1040 m_listType = event.m_listType;
1041 m_x = event.m_x;
1042 m_y = event.m_y;
1043
1044 m_dragText = event.m_dragText;
1045 m_dragAllowMove =event.m_dragAllowMove;
1046 #if wxUSE_DRAG_AND_DROP
1047 m_dragResult = event.m_dragResult;
1048 #endif
1049 }
1050
1051 //----------------------------------------------------------------------
1052 //----------------------------------------------------------------------
1053
1054
1055
1056
1057
1058
1059
1060
1061