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