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