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