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