Ignore WXK_NONE events in wxStyledTextCtrl.
[wxWidgets.git] / src / stc / ScintillaWX.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: src/stc/ScintillaWX.cpp
3 // Purpose: A wxWidgets implementation of Scintilla. A class derived
4 // from ScintillaBase that uses the "wx platform" defined in
5 // PlatformWX.cxx This class is one end of a bridge between
6 // the wx world and the Scintilla world. It needs a peer
7 // object of type wxStyledTextCtrl to function.
8 //
9 // Author: Robin Dunn
10 //
11 // Created: 13-Jan-2000
12 // RCS-ID: $Id$
13 // Copyright: (c) 2000 by Total Control Software
14 // Licence: wxWindows licence
15 /////////////////////////////////////////////////////////////////////////////
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #if wxUSE_STC
25
26 #ifndef WX_PRECOMP
27 #include "wx/scrolbar.h"
28 #include "wx/menu.h"
29 #include "wx/timer.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/textbuf.h"
33 #include "wx/dataobj.h"
34 #include "wx/clipbrd.h"
35 #include "wx/dnd.h"
36
37 #if !wxUSE_STD_CONTAINERS && !wxUSE_STD_IOSTREAM && !wxUSE_STD_STRING
38 #include "wx/beforestd.h"
39 #include <string>
40 #include "wx/afterstd.h"
41 #endif
42
43 #include "ScintillaWX.h"
44 #include "ExternalLexer.h"
45 #include "wx/stc/stc.h"
46 #include "wx/stc/private.h"
47 #include "PlatWX.h"
48
49 #ifdef __WXMSW__
50 // GetHwndOf()
51 #include "wx/msw/private.h"
52 #endif
53
54 //----------------------------------------------------------------------
55 // Helper classes
56
57 class wxSTCTimer : public wxTimer {
58 public:
59 wxSTCTimer(ScintillaWX* swx) {
60 m_swx = swx;
61 }
62
63 void Notify() {
64 m_swx->DoTick();
65 }
66
67 private:
68 ScintillaWX* m_swx;
69 };
70
71
72 #if wxUSE_DRAG_AND_DROP
73 bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
74 return m_swx->DoDropText(x, y, data);
75 }
76
77 wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) {
78 return m_swx->DoDragEnter(x, y, def);
79 }
80
81 wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
82 return m_swx->DoDragOver(x, y, def);
83 }
84
85 void wxSTCDropTarget::OnLeave() {
86 m_swx->DoDragLeave();
87 }
88 #endif // wxUSE_DRAG_AND_DROP
89
90
91 #if wxUSE_POPUPWIN
92 #include "wx/popupwin.h"
93 #define wxSTCCallTipBase wxPopupWindow
94 #else
95 #include "wx/frame.h"
96 #define wxSTCCallTipBase wxFrame
97 #endif
98
99 #include "wx/dcbuffer.h"
100
101 class wxSTCCallTip : public wxSTCCallTipBase {
102 public:
103 wxSTCCallTip(wxWindow* parent, CallTip* ct, ScintillaWX* swx) :
104 #if wxUSE_POPUPWIN
105 wxSTCCallTipBase(parent, wxBORDER_NONE),
106 #else
107 wxSTCCallTipBase(parent, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize,
108 wxFRAME_NO_TASKBAR
109 | wxFRAME_FLOAT_ON_PARENT
110 | wxBORDER_NONE
111 #ifdef __WXMAC__
112 | wxPOPUP_WINDOW
113 #endif
114 ),
115 #endif
116 m_ct(ct), m_swx(swx), m_cx(wxDefaultCoord), m_cy(wxDefaultCoord)
117 {
118 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
119 }
120
121 ~wxSTCCallTip() {
122 #if wxUSE_POPUPWIN && defined(__WXGTK__)
123 wxRect rect = GetRect();
124 rect.x = m_cx;
125 rect.y = m_cy;
126 GetParent()->Refresh(false, &rect);
127 #endif
128 }
129
130 bool AcceptsFocus() const { return false; }
131
132 void OnPaint(wxPaintEvent& WXUNUSED(evt))
133 {
134 wxAutoBufferedPaintDC dc(this);
135 Surface* surfaceWindow = Surface::Allocate();
136 surfaceWindow->Init(&dc, m_ct->wDraw.GetID());
137 m_ct->PaintCT(surfaceWindow);
138 surfaceWindow->Release();
139 delete surfaceWindow;
140 }
141
142 void OnFocus(wxFocusEvent& event)
143 {
144 GetParent()->SetFocus();
145 event.Skip();
146 }
147
148 void OnLeftDown(wxMouseEvent& event)
149 {
150 wxPoint pt = event.GetPosition();
151 Point p(pt.x, pt.y);
152 m_ct->MouseClick(p);
153 m_swx->CallTipClick();
154 }
155
156 virtual void DoSetSize(int x, int y,
157 int width, int height,
158 int sizeFlags = wxSIZE_AUTO)
159 {
160 // convert coords to screen coords since we're a top-level window
161 if (x != wxDefaultCoord) {
162 m_cx = x;
163 GetParent()->ClientToScreen(&x, NULL);
164 }
165 if (y != wxDefaultCoord) {
166 m_cy = y;
167 GetParent()->ClientToScreen(NULL, &y);
168 }
169 wxSTCCallTipBase::DoSetSize(x, y, width, height, sizeFlags);
170 }
171
172 #if wxUSE_POPUPWIN
173 #else
174 virtual bool Show( bool show = true )
175 {
176 // Although we're a frame, we always want the parent to be active, so
177 // raise it whenever we get shown.
178 bool rv = wxSTCCallTipBase::Show(show);
179 if (rv && show)
180 {
181 wxTopLevelWindow *frame = wxDynamicCast(
182 wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
183 if (frame)
184 frame->Raise();
185 }
186 return rv;
187 }
188 #endif
189
190 wxPoint GetMyPosition()
191 {
192 return wxPoint(m_cx, m_cy);
193 }
194
195 private:
196 CallTip* m_ct;
197 ScintillaWX* m_swx;
198 int m_cx, m_cy;
199 DECLARE_EVENT_TABLE()
200 };
201
202 BEGIN_EVENT_TABLE(wxSTCCallTip, wxSTCCallTipBase)
203 EVT_PAINT(wxSTCCallTip::OnPaint)
204 EVT_SET_FOCUS(wxSTCCallTip::OnFocus)
205 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown)
206 END_EVENT_TABLE()
207
208
209 //----------------------------------------------------------------------
210
211 #if wxUSE_DATAOBJ
212 static wxTextFileType wxConvertEOLMode(int scintillaMode)
213 {
214 wxTextFileType type;
215
216 switch (scintillaMode) {
217 case wxSTC_EOL_CRLF:
218 type = wxTextFileType_Dos;
219 break;
220
221 case wxSTC_EOL_CR:
222 type = wxTextFileType_Mac;
223 break;
224
225 case wxSTC_EOL_LF:
226 type = wxTextFileType_Unix;
227 break;
228
229 default:
230 type = wxTextBuffer::typeDefault;
231 break;
232 }
233 return type;
234 }
235 #endif // wxUSE_DATAOBJ
236
237
238 //----------------------------------------------------------------------
239 // Constructor/Destructor
240
241
242 ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
243 capturedMouse = false;
244 focusEvent = false;
245 wMain = win;
246 stc = win;
247 wheelRotation = 0;
248 Initialise();
249 #ifdef __WXMSW__
250 sysCaretBitmap = 0;
251 sysCaretWidth = 0;
252 sysCaretHeight = 0;
253 #endif
254 }
255
256
257 ScintillaWX::~ScintillaWX() {
258 Finalise();
259 }
260
261 //----------------------------------------------------------------------
262 // base class virtuals
263
264
265 void ScintillaWX::Initialise() {
266 //ScintillaBase::Initialise();
267 #if wxUSE_DRAG_AND_DROP
268 dropTarget = new wxSTCDropTarget;
269 dropTarget->SetScintilla(this);
270 stc->SetDropTarget(dropTarget);
271 #endif // wxUSE_DRAG_AND_DROP
272 vs.extraFontFlag = true; // UseAntiAliasing
273 }
274
275
276 void ScintillaWX::Finalise() {
277 ScintillaBase::Finalise();
278 SetTicking(false);
279 SetIdle(false);
280 DestroySystemCaret();
281 }
282
283
284 void ScintillaWX::StartDrag() {
285 #if wxUSE_DRAG_AND_DROP
286 wxString dragText = stc2wx(drag.s, drag.len);
287
288 // Send an event to allow the drag text to be changed
289 wxStyledTextEvent evt(wxEVT_STC_START_DRAG, stc->GetId());
290 evt.SetEventObject(stc);
291 evt.SetDragText(dragText);
292 evt.SetDragFlags(wxDrag_DefaultMove);
293 evt.SetPosition(wxMin(stc->GetSelectionStart(),
294 stc->GetSelectionEnd()));
295 stc->GetEventHandler()->ProcessEvent(evt);
296 dragText = evt.GetDragText();
297
298 if ( !dragText.empty() ) {
299 wxDropSource source(stc);
300 wxTextDataObject data(dragText);
301 wxDragResult result;
302
303 source.SetData(data);
304 dropWentOutside = true;
305 inDragDrop = ddDragging;
306 result = source.DoDragDrop(evt.GetDragFlags());
307 if (result == wxDragMove && dropWentOutside)
308 ClearSelection();
309 inDragDrop = ddNone;
310 SetDragPosition(SelectionPosition(invalidPosition));
311 }
312 #endif // wxUSE_DRAG_AND_DROP
313 }
314
315
316 bool ScintillaWX::SetIdle(bool on) {
317 if (idler.state != on) {
318 // connect or disconnect the EVT_IDLE handler
319 if (on)
320 stc->Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(wxStyledTextCtrl::OnIdle));
321 else
322 stc->Disconnect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(wxStyledTextCtrl::OnIdle));
323 idler.state = on;
324 }
325 return idler.state;
326 }
327
328
329 void ScintillaWX::SetTicking(bool on) {
330 wxSTCTimer* steTimer;
331 if (timer.ticking != on) {
332 timer.ticking = on;
333 if (timer.ticking) {
334 steTimer = new wxSTCTimer(this);
335 steTimer->Start(timer.tickSize);
336 timer.tickerID = steTimer;
337 } else {
338 steTimer = (wxSTCTimer*)timer.tickerID;
339 steTimer->Stop();
340 delete steTimer;
341 timer.tickerID = 0;
342 }
343 }
344 timer.ticksToWait = caret.period;
345 }
346
347
348 void ScintillaWX::SetMouseCapture(bool on) {
349 if (mouseDownCaptures) {
350 if (on && !capturedMouse)
351 stc->CaptureMouse();
352 else if (!on && capturedMouse && stc->HasCapture())
353 stc->ReleaseMouse();
354 capturedMouse = on;
355 }
356 }
357
358
359 bool ScintillaWX::HaveMouseCapture() {
360 return capturedMouse;
361 }
362
363
364 void ScintillaWX::ScrollText(int linesToMove) {
365 int dy = vs.lineHeight * (linesToMove);
366 stc->ScrollWindow(0, dy);
367 stc->Update();
368 }
369
370 void ScintillaWX::SetVerticalScrollPos() {
371 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
372 stc->SetScrollPos(wxVERTICAL, topLine);
373 }
374 else { // otherwise use the one that's been given to us
375 stc->m_vScrollBar->SetThumbPosition(topLine);
376 }
377 }
378
379 void ScintillaWX::SetHorizontalScrollPos() {
380 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
381 stc->SetScrollPos(wxHORIZONTAL, xOffset);
382 }
383 else { // otherwise use the one that's been given to us
384 stc->m_hScrollBar->SetThumbPosition(xOffset);
385 }
386 }
387
388
389 const int H_SCROLL_STEP = 20;
390
391 bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) {
392 bool modified = false;
393
394 int vertEnd = nMax;
395 if (!verticalScrollBarVisible)
396 vertEnd = 0;
397
398 // Check the vertical scrollbar
399 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
400 int sbMax = stc->GetScrollRange(wxVERTICAL);
401 int sbThumb = stc->GetScrollThumb(wxVERTICAL);
402 int sbPos = stc->GetScrollPos(wxVERTICAL);
403 if (sbMax != vertEnd || sbThumb != nPage) {
404 stc->SetScrollbar(wxVERTICAL, sbPos, nPage, vertEnd+1);
405 modified = true;
406 }
407 }
408 else { // otherwise use the one that's been given to us
409 int sbMax = stc->m_vScrollBar->GetRange();
410 int sbPage = stc->m_vScrollBar->GetPageSize();
411 int sbPos = stc->m_vScrollBar->GetThumbPosition();
412 if (sbMax != vertEnd || sbPage != nPage) {
413 stc->m_vScrollBar->SetScrollbar(sbPos, nPage, vertEnd+1, nPage);
414 modified = true;
415 }
416 }
417
418
419 // Check the horizontal scrollbar
420 PRectangle rcText = GetTextRectangle();
421 int horizEnd = scrollWidth;
422 if (horizEnd < 0)
423 horizEnd = 0;
424 if (!horizontalScrollBarVisible || (wrapState != eWrapNone))
425 horizEnd = 0;
426 int pageWidth = rcText.Width();
427
428 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
429 int sbMax = stc->GetScrollRange(wxHORIZONTAL);
430 int sbThumb = stc->GetScrollThumb(wxHORIZONTAL);
431 int sbPos = stc->GetScrollPos(wxHORIZONTAL);
432 if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) {
433 stc->SetScrollbar(wxHORIZONTAL, sbPos, pageWidth, horizEnd);
434 modified = true;
435 if (scrollWidth < pageWidth) {
436 HorizontalScrollTo(0);
437 }
438 }
439 }
440 else { // otherwise use the one that's been given to us
441 int sbMax = stc->m_hScrollBar->GetRange();
442 int sbThumb = stc->m_hScrollBar->GetPageSize();
443 int sbPos = stc->m_hScrollBar->GetThumbPosition();
444 if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) {
445 stc->m_hScrollBar->SetScrollbar(sbPos, pageWidth, horizEnd, pageWidth);
446 modified = true;
447 if (scrollWidth < pageWidth) {
448 HorizontalScrollTo(0);
449 }
450 }
451 }
452
453 return modified;
454 }
455
456
457 void ScintillaWX::NotifyChange() {
458 stc->NotifyChange();
459 }
460
461
462 void ScintillaWX::NotifyParent(SCNotification scn) {
463 stc->NotifyParent(&scn);
464 }
465
466
467 // This method is overloaded from ScintillaBase in order to prevent the
468 // AutoComplete window from being destroyed when it gets the focus. There is
469 // a side effect that the AutoComp will also not be destroyed when switching
470 // to another window, but I think that is okay.
471 void ScintillaWX::CancelModes() {
472 if (! focusEvent)
473 AutoCompleteCancel();
474 ct.CallTipCancel();
475 Editor::CancelModes();
476 }
477
478
479
480 void ScintillaWX::Copy() {
481 if (!sel.Empty()) {
482 SelectionText st;
483 CopySelectionRange(&st);
484 CopyToClipboard(st);
485 }
486 }
487
488
489 void ScintillaWX::Paste() {
490 pdoc->BeginUndoAction();
491 ClearSelection();
492
493 #if wxUSE_DATAOBJ
494 wxTextDataObject data;
495 bool gotData = false;
496
497 wxTheClipboard->UsePrimarySelection(false);
498 if (wxTheClipboard->Open()) {
499 gotData = wxTheClipboard->GetData(data);
500 wxTheClipboard->Close();
501 }
502 if (gotData) {
503 wxString text = wxTextBuffer::Translate(data.GetText(),
504 wxConvertEOLMode(pdoc->eolMode));
505 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
506
507 #if wxUSE_UNICODE
508 // free up the old character buffer in case the text is real big
509 data.SetText(wxEmptyString);
510 text = wxEmptyString;
511 #endif
512 int len = strlen(buf);
513 int caretMain = sel.MainCaret();
514 pdoc->InsertString(caretMain, buf, len);
515 SetEmptySelection(caretMain + len);
516 }
517 #endif // wxUSE_DATAOBJ
518
519 pdoc->EndUndoAction();
520 NotifyChange();
521 Redraw();
522 }
523
524
525 void ScintillaWX::CopyToClipboard(const SelectionText& st) {
526 #if wxUSE_CLIPBOARD
527 if ( !st.len )
528 return;
529
530 wxTheClipboard->UsePrimarySelection(false);
531 if (wxTheClipboard->Open()) {
532 wxString text = wxTextBuffer::Translate(stc2wx(st.s, st.len-1));
533 wxTheClipboard->SetData(new wxTextDataObject(text));
534 wxTheClipboard->Close();
535 }
536 #else
537 wxUnusedVar(st);
538 #endif // wxUSE_CLIPBOARD
539 }
540
541
542 bool ScintillaWX::CanPaste() {
543 #if wxUSE_CLIPBOARD
544 bool canPaste = false;
545 bool didOpen;
546
547 if (Editor::CanPaste()) {
548 wxTheClipboard->UsePrimarySelection(false);
549 didOpen = !wxTheClipboard->IsOpened();
550 if ( didOpen )
551 wxTheClipboard->Open();
552
553 if (wxTheClipboard->IsOpened()) {
554 canPaste = wxTheClipboard->IsSupported(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT);
555 if (didOpen)
556 wxTheClipboard->Close();
557 }
558 }
559 return canPaste;
560 #else
561 return false;
562 #endif // wxUSE_CLIPBOARD
563 }
564
565 void ScintillaWX::CreateCallTipWindow(PRectangle) {
566 if (! ct.wCallTip.Created() ) {
567 ct.wCallTip = new wxSTCCallTip(stc, &ct, this);
568 ct.wDraw = ct.wCallTip;
569 }
570 }
571
572
573 void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) {
574 if (!label[0])
575 ((wxMenu*)popup.GetID())->AppendSeparator();
576 else
577 ((wxMenu*)popup.GetID())->Append(cmd, wxGetTranslation(stc2wx(label)));
578
579 if (!enabled)
580 ((wxMenu*)popup.GetID())->Enable(cmd, enabled);
581 }
582
583
584 // This is called by the Editor base class whenever something is selected.
585 // For wxGTK we can put this text in the primary selection and then other apps
586 // can paste with the middle button.
587 void ScintillaWX::ClaimSelection() {
588 #ifdef __WXGTK__
589 // Put the selected text in the PRIMARY selection
590 if (!sel.Empty()) {
591 SelectionText st;
592 CopySelectionRange(&st);
593 wxTheClipboard->UsePrimarySelection(true);
594 if (wxTheClipboard->Open()) {
595 wxString text = stc2wx(st.s, st.len);
596 wxTheClipboard->SetData(new wxTextDataObject(text));
597 wxTheClipboard->Close();
598 }
599 wxTheClipboard->UsePrimarySelection(false);
600 }
601 #endif
602 }
603
604
605 void ScintillaWX::UpdateSystemCaret() {
606 #ifdef __WXMSW__
607 if (hasFocus) {
608 if (HasCaretSizeChanged()) {
609 DestroySystemCaret();
610 CreateSystemCaret();
611 }
612 Point pos = PointMainCaret();
613 ::SetCaretPos(pos.x, pos.y);
614 }
615 #endif
616 }
617
618
619 bool ScintillaWX::HasCaretSizeChanged() {
620 #ifdef __WXMSW__
621 if ( (vs.caretWidth && (sysCaretWidth != vs.caretWidth))
622 || (vs.lineHeight && (sysCaretHeight != vs.lineHeight)) ) {
623 return true;
624 }
625 #endif
626 return false;
627 }
628
629 bool ScintillaWX::CreateSystemCaret() {
630 #ifdef __WXMSW__
631 sysCaretWidth = vs.caretWidth;
632 if (0 == sysCaretWidth) {
633 sysCaretWidth = 1;
634 }
635 sysCaretHeight = vs.lineHeight;
636 int bitmapSize = (((sysCaretWidth + 15) & ~15) >> 3) * sysCaretHeight;
637 char *bits = new char[bitmapSize];
638 memset(bits, 0, bitmapSize);
639 sysCaretBitmap = ::CreateBitmap(sysCaretWidth, sysCaretHeight, 1,
640 1, reinterpret_cast<BYTE *>(bits));
641 delete [] bits;
642 BOOL retval = ::CreateCaret(GetHwndOf(stc), sysCaretBitmap,
643 sysCaretWidth, sysCaretHeight);
644 ::ShowCaret(GetHwndOf(stc));
645 return retval != 0;
646 #else
647 return false;
648 #endif
649 }
650
651 bool ScintillaWX::DestroySystemCaret() {
652 #ifdef __WXMSW__
653 ::HideCaret(GetHwndOf(stc));
654 BOOL retval = ::DestroyCaret();
655 if (sysCaretBitmap) {
656 ::DeleteObject(sysCaretBitmap);
657 sysCaretBitmap = 0;
658 }
659 return retval != 0;
660 #else
661 return false;
662 #endif
663 }
664
665
666 //----------------------------------------------------------------------
667
668
669 sptr_t ScintillaWX::DefWndProc(unsigned int /*iMessage*/, uptr_t /*wParam*/, sptr_t /*lParam*/) {
670 return 0;
671 }
672
673 sptr_t ScintillaWX::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
674 switch (iMessage) {
675 case SCI_CALLTIPSHOW: {
676 // NOTE: This is copied here from scintilla/src/ScintillaBase.cxx
677 // because of the little tweak that needs done below for wxGTK.
678 // When updating new versions double check that this is still
679 // needed, and that any new code there is copied here too.
680 Point pt = LocationFromPosition(wParam);
681 char* defn = reinterpret_cast<char *>(lParam);
682 AutoCompleteCancel();
683 pt.y += vs.lineHeight;
684 int ctStyle = ct.UseStyleCallTip() ? STYLE_CALLTIP : STYLE_DEFAULT;
685 if (ct.UseStyleCallTip())
686 {
687 ct.SetForeBack(vs.styles[STYLE_CALLTIP].fore, vs.styles[STYLE_CALLTIP].back);
688 }
689 int caretMain = sel.MainCaret();
690 PRectangle rc = ct.CallTipStart(caretMain, pt,
691 defn,
692 vs.styles[ctStyle].fontName,
693 vs.styles[ctStyle].sizeZoomed,
694 CodePage(),
695 vs.styles[ctStyle].characterSet,
696 wMain);
697 // If the call-tip window would be out of the client
698 // space, adjust so it displays above the text.
699 PRectangle rcClient = GetClientRectangle();
700 if (rc.bottom > rcClient.bottom) {
701 #ifdef __WXGTK__
702 int offset = int(vs.lineHeight * 1.25) + rc.Height();
703 #else
704 int offset = vs.lineHeight + rc.Height();
705 #endif
706 rc.top -= offset;
707 rc.bottom -= offset;
708 }
709 // Now display the window.
710 CreateCallTipWindow(rc);
711 ct.wCallTip.SetPositionRelative(rc, wMain);
712 ct.wCallTip.Show();
713 break;
714 }
715
716 #ifdef SCI_LEXER
717 case SCI_LOADLEXERLIBRARY:
718 LexerManager::GetInstance()->Load((const char*)lParam);
719 break;
720 #endif
721
722 default:
723 return ScintillaBase::WndProc(iMessage, wParam, lParam);
724 }
725 return 0;
726 }
727
728
729
730 //----------------------------------------------------------------------
731 // Event delegates
732
733 void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
734
735 paintState = painting;
736 Surface* surfaceWindow = Surface::Allocate();
737 surfaceWindow->Init(dc, wMain.GetID());
738 rcPaint = PRectangleFromwxRect(rect);
739 PRectangle rcClient = GetClientRectangle();
740 paintingAllText = rcPaint.Contains(rcClient);
741
742 ClipChildren(*dc, rcPaint);
743 Paint(surfaceWindow, rcPaint);
744
745 delete surfaceWindow;
746 if (paintState == paintAbandoned) {
747 // Painting area was insufficient to cover new styling or brace
748 // highlight positions
749 FullPaint();
750 }
751 paintState = notPainting;
752 }
753
754
755 void ScintillaWX::DoHScroll(int type, int pos) {
756 int xPos = xOffset;
757 PRectangle rcText = GetTextRectangle();
758 int pageWidth = rcText.Width() * 2 / 3;
759 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
760 xPos -= H_SCROLL_STEP;
761 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
762 xPos += H_SCROLL_STEP;
763 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
764 xPos -= pageWidth;
765 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN) {
766 xPos += pageWidth;
767 if (xPos > scrollWidth - rcText.Width()) {
768 xPos = scrollWidth - rcText.Width();
769 }
770 }
771 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
772 xPos = 0;
773 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
774 xPos = scrollWidth;
775 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
776 xPos = pos;
777
778 HorizontalScrollTo(xPos);
779 }
780
781 void ScintillaWX::DoVScroll(int type, int pos) {
782 int topLineNew = topLine;
783 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
784 topLineNew -= 1;
785 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
786 topLineNew += 1;
787 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
788 topLineNew -= LinesToScroll();
789 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
790 topLineNew += LinesToScroll();
791 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
792 topLineNew = 0;
793 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
794 topLineNew = MaxScrollPos();
795 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
796 topLineNew = pos;
797
798 ScrollTo(topLineNew);
799 }
800
801 void ScintillaWX::DoMouseWheel(int rotation, int delta,
802 int linesPerAction, int ctrlDown,
803 bool isPageScroll ) {
804 int topLineNew = topLine;
805 int lines;
806
807 if (ctrlDown) { // Zoom the fonts if Ctrl key down
808 if (rotation < 0) {
809 KeyCommand(SCI_ZOOMIN);
810 }
811 else {
812 KeyCommand(SCI_ZOOMOUT);
813 }
814 }
815 else { // otherwise just scroll the window
816 if ( !delta )
817 delta = 120;
818 wheelRotation += rotation;
819 lines = wheelRotation / delta;
820 wheelRotation -= lines * delta;
821 if (lines != 0) {
822 if (isPageScroll)
823 lines = lines * LinesOnScreen(); // lines is either +1 or -1
824 else
825 lines *= linesPerAction;
826 topLineNew -= lines;
827 ScrollTo(topLineNew);
828 }
829 }
830 }
831
832
833 void ScintillaWX::DoSize(int WXUNUSED(width), int WXUNUSED(height)) {
834 ChangeSize();
835 }
836
837 void ScintillaWX::DoLoseFocus(){
838 focusEvent = true;
839 SetFocusState(false);
840 focusEvent = false;
841 DestroySystemCaret();
842 }
843
844 void ScintillaWX::DoGainFocus(){
845 focusEvent = true;
846 SetFocusState(true);
847 focusEvent = false;
848 DestroySystemCaret();
849 CreateSystemCaret();
850 }
851
852 void ScintillaWX::DoSysColourChange() {
853 InvalidateStyleData();
854 }
855
856 void ScintillaWX::DoLeftButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
857 ButtonDown(pt, curTime, shift, ctrl, alt);
858 }
859
860 void ScintillaWX::DoLeftButtonUp(Point pt, unsigned int curTime, bool ctrl) {
861 ButtonUp(pt, curTime, ctrl);
862 }
863
864 void ScintillaWX::DoLeftButtonMove(Point pt) {
865 ButtonMove(pt);
866 }
867
868 #ifdef __WXGTK__
869 void ScintillaWX::DoMiddleButtonUp(Point pt) {
870 // Set the current position to the mouse click point and
871 // then paste in the PRIMARY selection, if any. wxGTK only.
872 int newPos = PositionFromLocation(pt);
873 MovePositionTo(newPos, Selection::noSel, true);
874
875 pdoc->BeginUndoAction();
876 wxTextDataObject data;
877 bool gotData = false;
878 wxTheClipboard->UsePrimarySelection(true);
879 if (wxTheClipboard->Open()) {
880 gotData = wxTheClipboard->GetData(data);
881 wxTheClipboard->Close();
882 }
883 wxTheClipboard->UsePrimarySelection(false);
884 if (gotData) {
885 wxString text = wxTextBuffer::Translate(data.GetText(),
886 wxConvertEOLMode(pdoc->eolMode));
887 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
888 int len = strlen(buf);
889 int caretMain = sel.MainCaret();
890 pdoc->InsertString(caretMain, buf, len);
891 SetEmptySelection(caretMain + len);
892 }
893 pdoc->EndUndoAction();
894 NotifyChange();
895 Redraw();
896
897 ShowCaretAtCurrentPosition();
898 EnsureCaretVisible();
899 }
900 #else
901 void ScintillaWX::DoMiddleButtonUp(Point WXUNUSED(pt)) {
902 }
903 #endif
904
905
906 void ScintillaWX::DoAddChar(int key) {
907 #if wxUSE_UNICODE
908 wxChar wszChars[2];
909 wszChars[0] = (wxChar)key;
910 wszChars[1] = 0;
911 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(wszChars);
912 AddCharUTF((char*)buf.data(), strlen(buf));
913 #else
914 AddChar((char)key);
915 #endif
916 }
917
918
919 int ScintillaWX::DoKeyDown(const wxKeyEvent& evt, bool* consumed)
920 {
921 int key = evt.GetKeyCode();
922 if (key == WXK_NONE) {
923 // This is a Unicode character not representable in Latin-1 or some key
924 // without key code at all (e.g. dead key or VK_PROCESSKEY under MSW).
925 if ( consumed )
926 *consumed = false;
927 return 0;
928 }
929
930 bool shift = evt.ShiftDown(),
931 ctrl = evt.ControlDown(),
932 alt = evt.AltDown();
933
934 if (ctrl && key >= 1 && key <= 26 && key != WXK_BACK)
935 key += 'A' - 1;
936
937 switch (key) {
938 case WXK_DOWN: key = SCK_DOWN; break;
939 case WXK_UP: key = SCK_UP; break;
940 case WXK_LEFT: key = SCK_LEFT; break;
941 case WXK_RIGHT: key = SCK_RIGHT; break;
942 case WXK_HOME: key = SCK_HOME; break;
943 case WXK_END: key = SCK_END; break;
944 case WXK_PAGEUP: key = SCK_PRIOR; break;
945 case WXK_PAGEDOWN: key = SCK_NEXT; break;
946 case WXK_NUMPAD_DOWN: key = SCK_DOWN; break;
947 case WXK_NUMPAD_UP: key = SCK_UP; break;
948 case WXK_NUMPAD_LEFT: key = SCK_LEFT; break;
949 case WXK_NUMPAD_RIGHT: key = SCK_RIGHT; break;
950 case WXK_NUMPAD_HOME: key = SCK_HOME; break;
951 case WXK_NUMPAD_END: key = SCK_END; break;
952 case WXK_NUMPAD_PAGEUP: key = SCK_PRIOR; break;
953 case WXK_NUMPAD_PAGEDOWN: key = SCK_NEXT; break;
954 case WXK_NUMPAD_DELETE: key = SCK_DELETE; break;
955 case WXK_NUMPAD_INSERT: key = SCK_INSERT; break;
956 case WXK_DELETE: key = SCK_DELETE; break;
957 case WXK_INSERT: key = SCK_INSERT; break;
958 case WXK_ESCAPE: key = SCK_ESCAPE; break;
959 case WXK_BACK: key = SCK_BACK; break;
960 case WXK_TAB: key = SCK_TAB; break;
961 case WXK_NUMPAD_ENTER: // fall through
962 case WXK_RETURN: key = SCK_RETURN; break;
963 case WXK_ADD: // fall through
964 case WXK_NUMPAD_ADD: key = SCK_ADD; break;
965 case WXK_SUBTRACT: // fall through
966 case WXK_NUMPAD_SUBTRACT: key = SCK_SUBTRACT; break;
967 case WXK_DIVIDE: // fall through
968 case WXK_NUMPAD_DIVIDE: key = SCK_DIVIDE; break;
969 case WXK_CONTROL: key = 0; break;
970 case WXK_ALT: key = 0; break;
971 case WXK_SHIFT: key = 0; break;
972 case WXK_MENU: key = 0; break;
973 }
974
975 #ifdef __WXMAC__
976 if ( evt.MetaDown() ) {
977 // check for a few common Mac Meta-key combos and remap them to Ctrl
978 // for Scintilla
979 switch ( key ) {
980 case 'Z': // Undo
981 case 'X': // Cut
982 case 'C': // Copy
983 case 'V': // Paste
984 case 'A': // Select All
985 ctrl = true;
986 break;
987 }
988 }
989 #endif
990
991 int rv = KeyDown(key, shift, ctrl, alt, consumed);
992
993 if (key)
994 return rv;
995 else
996 return 1;
997 }
998
999
1000 void ScintillaWX::DoCommand(int ID) {
1001 Command(ID);
1002 }
1003
1004
1005 void ScintillaWX::DoContextMenu(Point pt) {
1006 if (displayPopupMenu)
1007 ContextMenu(pt);
1008 }
1009
1010 void ScintillaWX::DoOnListBox() {
1011 AutoCompleteCompleted();
1012 }
1013
1014
1015 void ScintillaWX::DoOnIdle(wxIdleEvent& evt) {
1016
1017 if ( Idle() )
1018 evt.RequestMore();
1019 else
1020 SetIdle(false);
1021 }
1022
1023 //----------------------------------------------------------------------
1024
1025 #if wxUSE_DRAG_AND_DROP
1026 bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
1027 SetDragPosition(SelectionPosition(invalidPosition));
1028
1029 wxString text = wxTextBuffer::Translate(data,
1030 wxConvertEOLMode(pdoc->eolMode));
1031
1032 // Send an event to allow the drag details to be changed
1033 wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId());
1034 evt.SetEventObject(stc);
1035 evt.SetDragResult(dragResult);
1036 evt.SetX(x);
1037 evt.SetY(y);
1038 evt.SetPosition(PositionFromLocation(Point(x,y)));
1039 evt.SetDragText(text);
1040 stc->GetEventHandler()->ProcessEvent(evt);
1041
1042 dragResult = evt.GetDragResult();
1043 if (dragResult == wxDragMove || dragResult == wxDragCopy) {
1044 DropAt(SelectionPosition(evt.GetPosition()),
1045 wx2stc(evt.GetDragText()),
1046 dragResult == wxDragMove,
1047 false); // TODO: rectangular?
1048 return true;
1049 }
1050 return false;
1051 }
1052
1053
1054 wxDragResult ScintillaWX::DoDragEnter(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) {
1055 dragResult = def;
1056 return dragResult;
1057 }
1058
1059
1060 wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
1061 SetDragPosition(SelectionPosition(PositionFromLocation(Point(x, y))));
1062
1063 // Send an event to allow the drag result to be changed
1064 wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId());
1065 evt.SetEventObject(stc);
1066 evt.SetDragResult(def);
1067 evt.SetX(x);
1068 evt.SetY(y);
1069 evt.SetPosition(PositionFromLocation(Point(x,y)));
1070 stc->GetEventHandler()->ProcessEvent(evt);
1071
1072 dragResult = evt.GetDragResult();
1073 return dragResult;
1074 }
1075
1076
1077 void ScintillaWX::DoDragLeave() {
1078 SetDragPosition(SelectionPosition(invalidPosition));
1079 }
1080 #endif // wxUSE_DRAG_AND_DROP
1081 //----------------------------------------------------------------------
1082
1083 // Force the whole window to be repainted
1084 void ScintillaWX::FullPaint() {
1085 stc->Refresh(false);
1086 stc->Update();
1087 }
1088
1089
1090 void ScintillaWX::DoScrollToLine(int line) {
1091 ScrollTo(line);
1092 }
1093
1094
1095 void ScintillaWX::DoScrollToColumn(int column) {
1096 HorizontalScrollTo(column * vs.spaceWidth);
1097 }
1098
1099 // wxGTK doesn't appear to need this explicit clipping code any longer, but I
1100 // will leave it here commented out for a while just in case...
1101 void ScintillaWX::ClipChildren(wxDC& WXUNUSED(dc), PRectangle WXUNUSED(rect))
1102 {
1103 // wxRegion rgn(wxRectFromPRectangle(rect));
1104 // if (ac.Active()) {
1105 // wxRect childRect = ((wxWindow*)ac.lb->GetID())->GetRect();
1106 // rgn.Subtract(childRect);
1107 // }
1108 // if (ct.inCallTipMode) {
1109 // wxSTCCallTip* tip = (wxSTCCallTip*)ct.wCallTip.GetID();
1110 // wxRect childRect = tip->GetRect();
1111 // #if wxUSE_POPUPWIN
1112 // childRect.SetPosition(tip->GetMyPosition());
1113 // #endif
1114 // rgn.Subtract(childRect);
1115 // }
1116 // dc.SetClippingRegion(rgn);
1117 }
1118
1119
1120 void ScintillaWX::SetUseAntiAliasing(bool useAA) {
1121 vs.extraFontFlag = useAA;
1122 InvalidateStyleRedraw();
1123 }
1124
1125 bool ScintillaWX::GetUseAntiAliasing() {
1126 return vs.extraFontFlag != 0;
1127 }
1128
1129 //----------------------------------------------------------------------
1130 //----------------------------------------------------------------------
1131
1132 #endif // wxUSE_STC