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