don't interfere with Scintilla drag-and-drop (#9088)
[wxWidgets.git] / src / stc / ScintillaWX.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: ScintillaWX.cxx
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 license
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 #include "ScintillaWX.h"
38 #include "ExternalLexer.h"
39 #include "wx/stc/stc.h"
40 #include "wx/stc/private.h"
41 #include "PlatWX.h"
42
43 #ifdef __WXMSW__
44 // GetHwndOf()
45 #include "wx/msw/private.h"
46 #endif
47
48 //----------------------------------------------------------------------
49 // Helper classes
50
51 class wxSTCTimer : public wxTimer {
52 public:
53 wxSTCTimer(ScintillaWX* swx) {
54 m_swx = swx;
55 }
56
57 void Notify() {
58 m_swx->DoTick();
59 }
60
61 private:
62 ScintillaWX* m_swx;
63 };
64
65
66 #if wxUSE_DRAG_AND_DROP
67 bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
68 return m_swx->DoDropText(x, y, data);
69 }
70
71 wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) {
72 return m_swx->DoDragEnter(x, y, def);
73 }
74
75 wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
76 return m_swx->DoDragOver(x, y, def);
77 }
78
79 void wxSTCDropTarget::OnLeave() {
80 m_swx->DoDragLeave();
81 }
82 #endif // wxUSE_DRAG_AND_DROP
83
84
85 #if wxUSE_POPUPWIN
86 #include "wx/popupwin.h"
87 #define wxSTCCallTipBase wxPopupWindow
88 #else
89 #include "wx/frame.h"
90 #define wxSTCCallTipBase wxFrame
91 #endif
92
93 #include "wx/dcbuffer.h"
94
95 class wxSTCCallTip : public wxSTCCallTipBase {
96 public:
97 wxSTCCallTip(wxWindow* parent, CallTip* ct, ScintillaWX* swx) :
98 #if wxUSE_POPUPWIN
99 wxSTCCallTipBase(parent, wxBORDER_NONE),
100 #else
101 wxSTCCallTipBase(parent, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize,
102 wxFRAME_NO_TASKBAR
103 | wxFRAME_FLOAT_ON_PARENT
104 | wxBORDER_NONE
105 #ifdef __WXMAC__
106 | wxPOPUP_WINDOW
107 #endif
108 ),
109 #endif
110 m_ct(ct), m_swx(swx), m_cx(wxDefaultCoord), m_cy(wxDefaultCoord)
111 {
112 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
113 }
114
115 ~wxSTCCallTip() {
116 #if wxUSE_POPUPWIN && defined(__WXGTK__)
117 wxRect rect = GetRect();
118 rect.x = m_cx;
119 rect.y = m_cy;
120 GetParent()->Refresh(false, &rect);
121 #endif
122 }
123
124 bool AcceptsFocus() const { return false; }
125
126 void OnPaint(wxPaintEvent& WXUNUSED(evt))
127 {
128 wxAutoBufferedPaintDC dc(this);
129 Surface* surfaceWindow = Surface::Allocate();
130 surfaceWindow->Init(&dc, m_ct->wDraw.GetID());
131 m_ct->PaintCT(surfaceWindow);
132 surfaceWindow->Release();
133 delete surfaceWindow;
134 }
135
136 void OnFocus(wxFocusEvent& event)
137 {
138 GetParent()->SetFocus();
139 event.Skip();
140 }
141
142 void OnLeftDown(wxMouseEvent& event)
143 {
144 wxPoint pt = event.GetPosition();
145 Point p(pt.x, pt.y);
146 m_ct->MouseClick(p);
147 m_swx->CallTipClick();
148 }
149
150 virtual void DoSetSize(int x, int y,
151 int width, int height,
152 int sizeFlags = wxSIZE_AUTO)
153 {
154 // convert coords to screen coords since we're a top-level window
155 if (x != wxDefaultCoord) {
156 m_cx = x;
157 GetParent()->ClientToScreen(&x, NULL);
158 }
159 if (y != wxDefaultCoord) {
160 m_cy = y;
161 GetParent()->ClientToScreen(NULL, &y);
162 }
163 wxSTCCallTipBase::DoSetSize(x, y, width, height, sizeFlags);
164 }
165
166 #if wxUSE_POPUPWIN
167 #else
168 virtual bool Show( bool show = true )
169 {
170 // Although we're a frame, we always want the parent to be active, so
171 // raise it whenever we get shown.
172 bool rv = wxSTCCallTipBase::Show(show);
173 if (rv && show)
174 {
175 wxTopLevelWindow *frame = wxDynamicCast(
176 wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
177 if (frame)
178 frame->Raise();
179 }
180 return rv;
181 }
182 #endif
183
184 wxPoint GetMyPosition()
185 {
186 return wxPoint(m_cx, m_cy);
187 }
188
189 private:
190 CallTip* m_ct;
191 ScintillaWX* m_swx;
192 int m_cx, m_cy;
193 DECLARE_EVENT_TABLE()
194 };
195
196 BEGIN_EVENT_TABLE(wxSTCCallTip, wxSTCCallTipBase)
197 EVT_PAINT(wxSTCCallTip::OnPaint)
198 EVT_SET_FOCUS(wxSTCCallTip::OnFocus)
199 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown)
200 END_EVENT_TABLE()
201
202
203 //----------------------------------------------------------------------
204
205 #if wxUSE_DATAOBJ
206 static wxTextFileType wxConvertEOLMode(int scintillaMode)
207 {
208 wxTextFileType type;
209
210 switch (scintillaMode) {
211 case wxSTC_EOL_CRLF:
212 type = wxTextFileType_Dos;
213 break;
214
215 case wxSTC_EOL_CR:
216 type = wxTextFileType_Mac;
217 break;
218
219 case wxSTC_EOL_LF:
220 type = wxTextFileType_Unix;
221 break;
222
223 default:
224 type = wxTextBuffer::typeDefault;
225 break;
226 }
227 return type;
228 }
229 #endif // wxUSE_DATAOBJ
230
231
232 //----------------------------------------------------------------------
233 // Constructor/Destructor
234
235
236 ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
237 capturedMouse = false;
238 focusEvent = false;
239 wMain = win;
240 stc = win;
241 wheelRotation = 0;
242 Initialise();
243 #ifdef __WXMSW__
244 sysCaretBitmap = 0;
245 sysCaretWidth = 0;
246 sysCaretHeight = 0;
247 #endif
248 }
249
250
251 ScintillaWX::~ScintillaWX() {
252 Finalise();
253 }
254
255 //----------------------------------------------------------------------
256 // base class virtuals
257
258
259 void ScintillaWX::Initialise() {
260 //ScintillaBase::Initialise();
261 #if wxUSE_DRAG_AND_DROP
262 dropTarget = new wxSTCDropTarget;
263 dropTarget->SetScintilla(this);
264 stc->SetDropTarget(dropTarget);
265 #endif // wxUSE_DRAG_AND_DROP
266 #ifdef __WXMAC__
267 vs.extraFontFlag = false; // UseAntiAliasing
268 #else
269 vs.extraFontFlag = true; // UseAntiAliasing
270 #endif
271 }
272
273
274 void ScintillaWX::Finalise() {
275 ScintillaBase::Finalise();
276 SetTicking(false);
277 SetIdle(false);
278 DestroySystemCaret();
279 }
280
281
282 void ScintillaWX::StartDrag() {
283 #if wxUSE_DRAG_AND_DROP
284 wxString dragText = stc2wx(drag.s, drag.len);
285
286 // Send an event to allow the drag text to be changed
287 wxStyledTextEvent evt(wxEVT_STC_START_DRAG, stc->GetId());
288 evt.SetEventObject(stc);
289 evt.SetDragText(dragText);
290 evt.SetDragAllowMove(true);
291 evt.SetPosition(wxMin(stc->GetSelectionStart(),
292 stc->GetSelectionEnd()));
293 stc->GetEventHandler()->ProcessEvent(evt);
294 dragText = evt.GetDragText();
295
296 if (dragText.length()) {
297 wxDropSource source(stc);
298 wxTextDataObject data(dragText);
299 wxDragResult result;
300
301 source.SetData(data);
302 dropWentOutside = true;
303 inDragDrop = ddDragging;
304 result = source.DoDragDrop(evt.GetDragAllowMove());
305 if (result == wxDragMove && dropWentOutside)
306 ClearSelection();
307 inDragDrop = ddNone;
308 SetDragPosition(invalidPosition);
309 }
310 #endif // wxUSE_DRAG_AND_DROP
311 }
312
313
314 bool ScintillaWX::SetIdle(bool on) {
315 if (idler.state != on) {
316 // connect or disconnect the EVT_IDLE handler
317 if (on)
318 stc->Connect(wxID_ANY, wxEVT_IDLE,
319 (wxObjectEventFunction) (wxEventFunction) (wxIdleEventFunction) &wxStyledTextCtrl::OnIdle);
320 else
321 stc->Disconnect(wxID_ANY, wxEVT_IDLE,
322 (wxObjectEventFunction) (wxEventFunction) (wxIdleEventFunction) &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 (currentPos != anchor) {
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 pdoc->InsertString(currentPos, buf, len);
514 SetEmptySelection(currentPos + len);
515 }
516 #endif // wxUSE_DATAOBJ
517
518 pdoc->EndUndoAction();
519 NotifyChange();
520 Redraw();
521 }
522
523
524 void ScintillaWX::CopyToClipboard(const SelectionText& st) {
525 #if wxUSE_CLIPBOARD
526 wxTheClipboard->UsePrimarySelection(false);
527 if (wxTheClipboard->Open()) {
528 wxString text = wxTextBuffer::Translate(stc2wx(st.s, st.len-1));
529 wxTheClipboard->SetData(new wxTextDataObject(text));
530 wxTheClipboard->Close();
531 }
532 #else
533 wxUnusedVar(st);
534 #endif // wxUSE_CLIPBOARD
535 }
536
537
538 bool ScintillaWX::CanPaste() {
539 #if wxUSE_CLIPBOARD
540 bool canPaste = false;
541 bool didOpen;
542
543 if (Editor::CanPaste()) {
544 wxTheClipboard->UsePrimarySelection(false);
545 didOpen = !wxTheClipboard->IsOpened();
546 if ( didOpen )
547 wxTheClipboard->Open();
548
549 if (wxTheClipboard->IsOpened()) {
550 canPaste = wxTheClipboard->IsSupported(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT);
551 if (didOpen)
552 wxTheClipboard->Close();
553 }
554 }
555 return canPaste;
556 #else
557 return false;
558 #endif // wxUSE_CLIPBOARD
559 }
560
561 void ScintillaWX::CreateCallTipWindow(PRectangle) {
562 if (! ct.wCallTip.Created() ) {
563 ct.wCallTip = new wxSTCCallTip(stc, &ct, this);
564 ct.wDraw = ct.wCallTip;
565 }
566 }
567
568
569 void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) {
570 if (!label[0])
571 ((wxMenu*)popup.GetID())->AppendSeparator();
572 else
573 ((wxMenu*)popup.GetID())->Append(cmd, wxGetTranslation(stc2wx(label)));
574
575 if (!enabled)
576 ((wxMenu*)popup.GetID())->Enable(cmd, enabled);
577 }
578
579
580 // This is called by the Editor base class whenever something is selected.
581 // For wxGTK we can put this text in the primary selection and then other apps
582 // can paste with the middle button.
583 void ScintillaWX::ClaimSelection() {
584 #ifdef __WXGTK__
585 // Put the selected text in the PRIMARY selection
586 if (currentPos != anchor) {
587 SelectionText st;
588 CopySelectionRange(&st);
589 wxTheClipboard->UsePrimarySelection(true);
590 if (wxTheClipboard->Open()) {
591 wxString text = stc2wx(st.s, st.len);
592 wxTheClipboard->SetData(new wxTextDataObject(text));
593 wxTheClipboard->Close();
594 }
595 wxTheClipboard->UsePrimarySelection(false);
596 }
597 #endif
598 }
599
600
601 void ScintillaWX::UpdateSystemCaret() {
602 #ifdef __WXMSW__
603 if (hasFocus) {
604 if (HasCaretSizeChanged()) {
605 DestroySystemCaret();
606 CreateSystemCaret();
607 }
608 Point pos = LocationFromPosition(currentPos);
609 ::SetCaretPos(pos.x, pos.y);
610 }
611 #endif
612 }
613
614
615 bool ScintillaWX::HasCaretSizeChanged() {
616 #ifdef __WXMSW__
617 if (( (0 != vs.caretWidth) && (sysCaretWidth != vs.caretWidth) )
618 || (0 != vs.lineHeight) && (sysCaretHeight != vs.lineHeight)) {
619 return true;
620 }
621 #endif
622 return false;
623 }
624
625 bool ScintillaWX::CreateSystemCaret() {
626 #ifdef __WXMSW__
627 sysCaretWidth = vs.caretWidth;
628 if (0 == sysCaretWidth) {
629 sysCaretWidth = 1;
630 }
631 sysCaretHeight = vs.lineHeight;
632 int bitmapSize = (((sysCaretWidth + 15) & ~15) >> 3) * sysCaretHeight;
633 char *bits = new char[bitmapSize];
634 memset(bits, 0, bitmapSize);
635 sysCaretBitmap = ::CreateBitmap(sysCaretWidth, sysCaretHeight, 1,
636 1, reinterpret_cast<BYTE *>(bits));
637 delete [] bits;
638 BOOL retval = ::CreateCaret(GetHwndOf(stc), sysCaretBitmap,
639 sysCaretWidth, sysCaretHeight);
640 ::ShowCaret(GetHwndOf(stc));
641 return retval != 0;
642 #else
643 return false;
644 #endif
645 }
646
647 bool ScintillaWX::DestroySystemCaret() {
648 #ifdef __WXMSW__
649 ::HideCaret(GetHwndOf(stc));
650 BOOL retval = ::DestroyCaret();
651 if (sysCaretBitmap) {
652 ::DeleteObject(sysCaretBitmap);
653 sysCaretBitmap = 0;
654 }
655 return retval != 0;
656 #else
657 return false;
658 #endif
659 }
660
661
662 //----------------------------------------------------------------------
663
664
665 sptr_t ScintillaWX::DefWndProc(unsigned int /*iMessage*/, uptr_t /*wParam*/, sptr_t /*lParam*/) {
666 return 0;
667 }
668
669 sptr_t ScintillaWX::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
670 switch (iMessage) {
671 case SCI_CALLTIPSHOW: {
672 // NOTE: This is copied here from scintilla/src/ScintillaBase.cxx
673 // because of the little tweak that needs done below for wxGTK.
674 // When updating new versions double check that this is still
675 // needed, and that any new code there is copied here too.
676 Point pt = LocationFromPosition(wParam);
677 char* defn = reinterpret_cast<char *>(lParam);
678 AutoCompleteCancel();
679 pt.y += vs.lineHeight;
680 PRectangle rc = ct.CallTipStart(currentPos, pt,
681 defn,
682 vs.styles[STYLE_DEFAULT].fontName,
683 vs.styles[STYLE_DEFAULT].sizeZoomed,
684 CodePage(),
685 vs.styles[STYLE_DEFAULT].characterSet,
686 wMain);
687 // If the call-tip window would be out of the client
688 // space, adjust so it displays above the text.
689 PRectangle rcClient = GetClientRectangle();
690 if (rc.bottom > rcClient.bottom) {
691 #ifdef __WXGTK__
692 int offset = int(vs.lineHeight * 1.25) + rc.Height();
693 #else
694 int offset = vs.lineHeight + rc.Height();
695 #endif
696 rc.top -= offset;
697 rc.bottom -= offset;
698 }
699 // Now display the window.
700 CreateCallTipWindow(rc);
701 ct.wCallTip.SetPositionRelative(rc, wMain);
702 ct.wCallTip.Show();
703 break;
704 }
705
706 #ifdef SCI_LEXER
707 case SCI_LOADLEXERLIBRARY:
708 LexerManager::GetInstance()->Load((const char*)lParam);
709 break;
710 #endif
711
712 default:
713 return ScintillaBase::WndProc(iMessage, wParam, lParam);
714 }
715 return 0;
716 }
717
718
719
720 //----------------------------------------------------------------------
721 // Event delegates
722
723 void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
724
725 paintState = painting;
726 Surface* surfaceWindow = Surface::Allocate();
727 surfaceWindow->Init(dc, wMain.GetID());
728 rcPaint = PRectangleFromwxRect(rect);
729 PRectangle rcClient = GetClientRectangle();
730 paintingAllText = rcPaint.Contains(rcClient);
731
732 ClipChildren(*dc, rcPaint);
733 Paint(surfaceWindow, rcPaint);
734
735 delete surfaceWindow;
736 if (paintState == paintAbandoned) {
737 // Painting area was insufficient to cover new styling or brace
738 // highlight positions
739 FullPaint();
740 }
741 paintState = notPainting;
742 }
743
744
745 void ScintillaWX::DoHScroll(int type, int pos) {
746 int xPos = xOffset;
747 PRectangle rcText = GetTextRectangle();
748 int pageWidth = rcText.Width() * 2 / 3;
749 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
750 xPos -= H_SCROLL_STEP;
751 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
752 xPos += H_SCROLL_STEP;
753 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
754 xPos -= pageWidth;
755 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN) {
756 xPos += pageWidth;
757 if (xPos > scrollWidth - rcText.Width()) {
758 xPos = scrollWidth - rcText.Width();
759 }
760 }
761 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
762 xPos = 0;
763 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
764 xPos = scrollWidth;
765 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
766 xPos = pos;
767
768 HorizontalScrollTo(xPos);
769 }
770
771 void ScintillaWX::DoVScroll(int type, int pos) {
772 int topLineNew = topLine;
773 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
774 topLineNew -= 1;
775 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
776 topLineNew += 1;
777 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
778 topLineNew -= LinesToScroll();
779 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
780 topLineNew += LinesToScroll();
781 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
782 topLineNew = 0;
783 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
784 topLineNew = MaxScrollPos();
785 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
786 topLineNew = pos;
787
788 ScrollTo(topLineNew);
789 }
790
791 void ScintillaWX::DoMouseWheel(int rotation, int delta,
792 int linesPerAction, int ctrlDown,
793 bool isPageScroll ) {
794 int topLineNew = topLine;
795 int lines;
796
797 if (ctrlDown) { // Zoom the fonts if Ctrl key down
798 if (rotation < 0) {
799 KeyCommand(SCI_ZOOMIN);
800 }
801 else {
802 KeyCommand(SCI_ZOOMOUT);
803 }
804 }
805 else { // otherwise just scroll the window
806 if ( !delta )
807 delta = 120;
808 wheelRotation += rotation;
809 lines = wheelRotation / delta;
810 wheelRotation -= lines * delta;
811 if (lines != 0) {
812 if (isPageScroll)
813 lines = lines * LinesOnScreen(); // lines is either +1 or -1
814 else
815 lines *= linesPerAction;
816 topLineNew -= lines;
817 ScrollTo(topLineNew);
818 }
819 }
820 }
821
822
823 void ScintillaWX::DoSize(int WXUNUSED(width), int WXUNUSED(height)) {
824 ChangeSize();
825 }
826
827 void ScintillaWX::DoLoseFocus(){
828 focusEvent = true;
829 SetFocusState(false);
830 focusEvent = false;
831 DestroySystemCaret();
832 }
833
834 void ScintillaWX::DoGainFocus(){
835 focusEvent = true;
836 SetFocusState(true);
837 focusEvent = false;
838 DestroySystemCaret();
839 CreateSystemCaret();
840 }
841
842 void ScintillaWX::DoSysColourChange() {
843 InvalidateStyleData();
844 }
845
846 void ScintillaWX::DoLeftButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
847 ButtonDown(pt, curTime, shift, ctrl, alt);
848 }
849
850 void ScintillaWX::DoLeftButtonUp(Point pt, unsigned int curTime, bool ctrl) {
851 ButtonUp(pt, curTime, ctrl);
852 }
853
854 void ScintillaWX::DoLeftButtonMove(Point pt) {
855 ButtonMove(pt);
856 }
857
858 #ifdef __WXGTK__
859 void ScintillaWX::DoMiddleButtonUp(Point pt) {
860 // Set the current position to the mouse click point and
861 // then paste in the PRIMARY selection, if any. wxGTK only.
862 int newPos = PositionFromLocation(pt);
863 MovePositionTo(newPos, noSel, true);
864
865 pdoc->BeginUndoAction();
866 wxTextDataObject data;
867 bool gotData = false;
868 wxTheClipboard->UsePrimarySelection(true);
869 if (wxTheClipboard->Open()) {
870 gotData = wxTheClipboard->GetData(data);
871 wxTheClipboard->Close();
872 }
873 wxTheClipboard->UsePrimarySelection(false);
874 if (gotData) {
875 wxString text = wxTextBuffer::Translate(data.GetText(),
876 wxConvertEOLMode(pdoc->eolMode));
877 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
878 int len = strlen(buf);
879 pdoc->InsertString(currentPos, buf, len);
880 SetEmptySelection(currentPos + len);
881 }
882 pdoc->EndUndoAction();
883 NotifyChange();
884 Redraw();
885
886 ShowCaretAtCurrentPosition();
887 EnsureCaretVisible();
888 }
889 #else
890 void ScintillaWX::DoMiddleButtonUp(Point WXUNUSED(pt)) {
891 }
892 #endif
893
894
895 void ScintillaWX::DoAddChar(int key) {
896 #if wxUSE_UNICODE
897 wxChar wszChars[2];
898 wszChars[0] = (wxChar)key;
899 wszChars[1] = 0;
900 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(wszChars);
901 AddCharUTF((char*)buf.data(), strlen(buf));
902 #else
903 AddChar((char)key);
904 #endif
905 }
906
907
908 int ScintillaWX::DoKeyDown(const wxKeyEvent& evt, bool* consumed)
909 {
910 int key = evt.GetKeyCode();
911 bool shift = evt.ShiftDown(),
912 ctrl = evt.ControlDown(),
913 alt = evt.AltDown();
914
915 if (ctrl && key >= 1 && key <= 26 && key != WXK_BACK)
916 key += 'A' - 1;
917
918 switch (key) {
919 case WXK_DOWN: key = SCK_DOWN; break;
920 case WXK_UP: key = SCK_UP; break;
921 case WXK_LEFT: key = SCK_LEFT; break;
922 case WXK_RIGHT: key = SCK_RIGHT; break;
923 case WXK_HOME: key = SCK_HOME; break;
924 case WXK_END: key = SCK_END; break;
925 case WXK_PAGEUP: key = SCK_PRIOR; break;
926 case WXK_PAGEDOWN: key = SCK_NEXT; break;
927 case WXK_NUMPAD_DOWN: key = SCK_DOWN; break;
928 case WXK_NUMPAD_UP: key = SCK_UP; break;
929 case WXK_NUMPAD_LEFT: key = SCK_LEFT; break;
930 case WXK_NUMPAD_RIGHT: key = SCK_RIGHT; break;
931 case WXK_NUMPAD_HOME: key = SCK_HOME; break;
932 case WXK_NUMPAD_END: key = SCK_END; break;
933 case WXK_NUMPAD_PAGEUP: key = SCK_PRIOR; break;
934 case WXK_NUMPAD_PAGEDOWN: key = SCK_NEXT; break;
935 case WXK_NUMPAD_DELETE: key = SCK_DELETE; break;
936 case WXK_NUMPAD_INSERT: key = SCK_INSERT; break;
937 case WXK_DELETE: key = SCK_DELETE; break;
938 case WXK_INSERT: key = SCK_INSERT; break;
939 case WXK_ESCAPE: key = SCK_ESCAPE; break;
940 case WXK_BACK: key = SCK_BACK; break;
941 case WXK_TAB: key = SCK_TAB; break;
942 case WXK_NUMPAD_ENTER: // fall through
943 case WXK_RETURN: key = SCK_RETURN; break;
944 case WXK_ADD: // fall through
945 case WXK_NUMPAD_ADD: key = SCK_ADD; break;
946 case WXK_SUBTRACT: // fall through
947 case WXK_NUMPAD_SUBTRACT: key = SCK_SUBTRACT; break;
948 case WXK_DIVIDE: // fall through
949 case WXK_NUMPAD_DIVIDE: key = SCK_DIVIDE; break;
950 case WXK_CONTROL: key = 0; break;
951 case WXK_ALT: key = 0; break;
952 case WXK_SHIFT: key = 0; break;
953 case WXK_MENU: key = 0; break;
954 }
955
956 #ifdef __WXMAC__
957 if ( evt.MetaDown() ) {
958 // check for a few common Mac Meta-key combos and remap them to Ctrl
959 // for Scintilla
960 switch ( key ) {
961 case 'Z': // Undo
962 case 'X': // Cut
963 case 'C': // Copy
964 case 'V': // Paste
965 case 'A': // Select All
966 ctrl = true;
967 break;
968 }
969 }
970 #endif
971
972 int rv = KeyDown(key, shift, ctrl, alt, consumed);
973
974 if (key)
975 return rv;
976 else
977 return 1;
978 }
979
980
981 void ScintillaWX::DoCommand(int ID) {
982 Command(ID);
983 }
984
985
986 void ScintillaWX::DoContextMenu(Point pt) {
987 if (displayPopupMenu)
988 ContextMenu(pt);
989 }
990
991 void ScintillaWX::DoOnListBox() {
992 AutoCompleteCompleted();
993 }
994
995
996 void ScintillaWX::DoOnIdle(wxIdleEvent& evt) {
997
998 if ( Idle() )
999 evt.RequestMore();
1000 else
1001 SetIdle(false);
1002 }
1003
1004 //----------------------------------------------------------------------
1005
1006 #if wxUSE_DRAG_AND_DROP
1007 bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
1008 SetDragPosition(invalidPosition);
1009
1010 wxString text = wxTextBuffer::Translate(data,
1011 wxConvertEOLMode(pdoc->eolMode));
1012
1013 // Send an event to allow the drag details to be changed
1014 wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId());
1015 evt.SetEventObject(stc);
1016 evt.SetDragResult(dragResult);
1017 evt.SetX(x);
1018 evt.SetY(y);
1019 evt.SetPosition(PositionFromLocation(Point(x,y)));
1020 evt.SetDragText(text);
1021 stc->GetEventHandler()->ProcessEvent(evt);
1022
1023 dragResult = evt.GetDragResult();
1024 if (dragResult == wxDragMove || dragResult == wxDragCopy) {
1025 DropAt(evt.GetPosition(),
1026 wx2stc(evt.GetDragText()),
1027 dragResult == wxDragMove,
1028 false); // TODO: rectangular?
1029 return true;
1030 }
1031 return false;
1032 }
1033
1034
1035 wxDragResult ScintillaWX::DoDragEnter(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) {
1036 dragResult = def;
1037 return dragResult;
1038 }
1039
1040
1041 wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
1042 SetDragPosition(PositionFromLocation(Point(x, y)));
1043
1044 // Send an event to allow the drag result to be changed
1045 wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId());
1046 evt.SetEventObject(stc);
1047 evt.SetDragResult(def);
1048 evt.SetX(x);
1049 evt.SetY(y);
1050 evt.SetPosition(PositionFromLocation(Point(x,y)));
1051 stc->GetEventHandler()->ProcessEvent(evt);
1052
1053 dragResult = evt.GetDragResult();
1054 return dragResult;
1055 }
1056
1057
1058 void ScintillaWX::DoDragLeave() {
1059 SetDragPosition(invalidPosition);
1060 }
1061 #endif // wxUSE_DRAG_AND_DROP
1062 //----------------------------------------------------------------------
1063
1064 // Force the whole window to be repainted
1065 void ScintillaWX::FullPaint() {
1066 #ifndef __WXMAC__
1067 stc->Refresh(false);
1068 #endif
1069 stc->Update();
1070 }
1071
1072
1073 void ScintillaWX::DoScrollToLine(int line) {
1074 ScrollTo(line);
1075 }
1076
1077
1078 void ScintillaWX::DoScrollToColumn(int column) {
1079 HorizontalScrollTo(column * vs.spaceWidth);
1080 }
1081
1082 // wxGTK doesn't appear to need this explicit clipping code any longer, but I
1083 // will leave it here commented out for a while just in case...
1084 void ScintillaWX::ClipChildren(wxDC& WXUNUSED(dc), PRectangle WXUNUSED(rect))
1085 {
1086 // wxRegion rgn(wxRectFromPRectangle(rect));
1087 // if (ac.Active()) {
1088 // wxRect childRect = ((wxWindow*)ac.lb->GetID())->GetRect();
1089 // rgn.Subtract(childRect);
1090 // }
1091 // if (ct.inCallTipMode) {
1092 // wxSTCCallTip* tip = (wxSTCCallTip*)ct.wCallTip.GetID();
1093 // wxRect childRect = tip->GetRect();
1094 // #if wxUSE_POPUPWIN
1095 // childRect.SetPosition(tip->GetMyPosition());
1096 // #endif
1097 // rgn.Subtract(childRect);
1098 // }
1099 // dc.SetClippingRegion(rgn);
1100 }
1101
1102
1103 void ScintillaWX::SetUseAntiAliasing(bool useAA) {
1104 vs.extraFontFlag = useAA;
1105 InvalidateStyleRedraw();
1106 }
1107
1108 bool ScintillaWX::GetUseAntiAliasing() {
1109 return vs.extraFontFlag;
1110 }
1111
1112 //----------------------------------------------------------------------
1113 //----------------------------------------------------------------------
1114
1115 #endif // wxUSE_STC