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