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