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