1 ////////////////////////////////////////////////////////////////////////////
2 // Name: ScintillaWX.cxx
3 // Purpose: A wxWindows 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.
11 // Created: 13-Jan-2000
13 // Copyright: (c) 2000 by Total Control Software
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
18 #include "ScintillaWX.h"
19 #include "wx/stc/stc.h"
22 //----------------------------------------------------------------------
25 class wxSTCTimer
: public wxTimer
{
27 wxSTCTimer(ScintillaWX
* swx
) {
40 #if wxUSE_DRAG_AND_DROP
41 bool wxSTCDropTarget::OnDropText(wxCoord x
, wxCoord y
, const wxString
& data
) {
42 return swx
->DoDropText(x
, y
, data
);
45 wxDragResult
wxSTCDropTarget::OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
46 return swx
->DoDragEnter(x
, y
, def
);
49 wxDragResult
wxSTCDropTarget::OnDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
50 return swx
->DoDragOver(x
, y
, def
);
53 void wxSTCDropTarget::OnLeave() {
60 #undef wxSTC_USE_POPUP
61 #define wxSTC_USE_POPUP 0
64 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
65 #include <wx/popupwin.h>
66 #define wxSTCCallTipBase wxPopupWindow
67 #define param2 wxBORDER_NONE // popup's 2nd param is flags
69 #define wxSTCCallTipBase wxWindow
70 #define param2 -1 // wxWindows 2nd param is ID
73 class wxSTCCallTip
: public wxSTCCallTipBase
{
75 wxSTCCallTip(wxWindow
* parent
, CallTip
* ct
)
76 : wxSTCCallTipBase(parent
, param2
)
82 if (HasCapture()) ReleaseMouse();
85 void OnPaint(wxPaintEvent
& evt
) {
87 Surface
* surfaceWindow
= Surface::Allocate();
88 surfaceWindow
->Init(&dc
);
89 m_ct
->PaintCT(surfaceWindow
);
93 void OnFocus(wxFocusEvent
& event
) {
94 GetParent()->SetFocus();
98 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
99 virtual void DoSetSize(int x
, int y
,
100 int width
, int height
,
101 int sizeFlags
= wxSIZE_AUTO
) {
103 GetParent()->ClientToScreen(&x
, NULL
);
105 GetParent()->ClientToScreen(NULL
, &y
);
106 wxSTCCallTipBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
109 virtual bool Show( bool show
= TRUE
) {
110 bool retval
= wxSTCCallTipBase::Show(show
);
114 if (HasCapture()) ReleaseMouse();
118 void OnLeftDown(wxMouseEvent
& ) {
125 DECLARE_EVENT_TABLE()
128 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxSTCCallTipBase
)
129 EVT_PAINT(wxSTCCallTip::OnPaint
)
130 EVT_SET_FOCUS(wxSTCCallTip::OnFocus
)
131 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
132 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown
)
137 //----------------------------------------------------------------------
138 // Constructor/Destructor
141 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
142 capturedMouse
= false;
150 ScintillaWX::~ScintillaWX() {
154 //----------------------------------------------------------------------
155 // base class virtuals
158 void ScintillaWX::Initialise() {
159 //ScintillaBase::Initialise();
160 #if wxUSE_DRAG_AND_DROP
161 dropTarget
= new wxSTCDropTarget
;
162 dropTarget
->SetScintilla(this);
163 stc
->SetDropTarget(dropTarget
);
168 void ScintillaWX::Finalise() {
169 ScintillaBase::Finalise();
173 void ScintillaWX::StartDrag() {
174 #if wxUSE_DRAG_AND_DROP
175 wxString dragText
= stc2wx(drag
.s
, drag
.len
);
177 // Send an event to allow the drag text to be changed
178 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
179 evt
.SetEventObject(stc
);
180 evt
.SetDragText(dragText
);
181 evt
.SetDragAllowMove(TRUE
);
182 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
183 stc
->GetSelectionEnd()));
184 stc
->GetEventHandler()->ProcessEvent(evt
);
185 dragText
= evt
.GetDragText();
187 if (dragText
.Length()) {
188 wxDropSource
source(stc
);
189 wxTextDataObject
data(dragText
);
192 source
.SetData(data
);
193 dropWentOutside
= TRUE
;
194 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
195 if (result
== wxDragMove
&& dropWentOutside
)
198 SetDragPosition(invalidPosition
);
204 void ScintillaWX::SetTicking(bool on
) {
205 wxSTCTimer
* steTimer
;
206 if (timer
.ticking
!= on
) {
209 steTimer
= new wxSTCTimer(this);
210 steTimer
->Start(timer
.tickSize
);
211 timer
.tickerID
= steTimer
;
213 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
219 timer
.ticksToWait
= caret
.period
;
223 void ScintillaWX::SetMouseCapture(bool on
) {
224 if (on
&& !capturedMouse
)
226 else if (!on
&& capturedMouse
)
232 bool ScintillaWX::HaveMouseCapture() {
233 return capturedMouse
;
237 void ScintillaWX::ScrollText(int linesToMove
) {
238 int dy
= vs
.lineHeight
* (linesToMove
);
239 stc
->ScrollWindow(0, dy
);
243 void ScintillaWX::SetVerticalScrollPos() {
244 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
245 stc
->SetScrollPos(wxVERTICAL
, topLine
);
247 else { // otherwise use the one that's been given to us
248 stc
->m_vScrollBar
->SetThumbPosition(topLine
);
252 void ScintillaWX::SetHorizontalScrollPos() {
253 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
254 stc
->SetScrollPos(wxHORIZONTAL
, xOffset
);
256 else { // otherwise use the one that's been given to us
257 stc
->m_hScrollBar
->SetThumbPosition(xOffset
);
261 const int H_SCROLL_STEP
= 20;
263 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
264 bool modified
= false;
266 // Check the vertical scrollbar
267 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
268 int sbMax
= stc
->GetScrollRange(wxVERTICAL
);
269 int sbThumb
= stc
->GetScrollThumb(wxVERTICAL
);
270 int sbPos
= stc
->GetScrollPos(wxVERTICAL
);
271 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
272 stc
->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
+1);
276 else { // otherwise use the one that's been given to us
277 int sbMax
= stc
->m_vScrollBar
->GetRange();
278 int sbPage
= stc
->m_vScrollBar
->GetPageSize();
279 int sbPos
= stc
->m_vScrollBar
->GetThumbPosition();
280 if (sbMax
!= nMax
|| sbPage
!= nPage
) {
281 stc
->m_vScrollBar
->SetScrollbar(sbPos
, nPage
, nMax
+1, nPage
);
287 // Check the horizontal scrollbar
288 PRectangle rcText
= GetTextRectangle();
289 int horizEnd
= scrollWidth
;
292 if (!horizontalScrollBarVisible
|| (wrapState
!= eWrapNone
))
294 int pageWidth
= rcText
.Width();
296 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
297 int sbMax
= stc
->GetScrollRange(wxHORIZONTAL
);
298 int sbThumb
= stc
->GetScrollThumb(wxHORIZONTAL
);
299 int sbPos
= stc
->GetScrollPos(wxHORIZONTAL
);
300 if ((sbMax
!= horizEnd
) || (sbThumb
!= pageWidth
) || (sbPos
!= 0)) {
301 stc
->SetScrollbar(wxHORIZONTAL
, 0, pageWidth
, horizEnd
);
303 if (scrollWidth
< pageWidth
) {
304 HorizontalScrollTo(0);
308 else { // otherwise use the one that's been given to us
309 int sbMax
= stc
->m_hScrollBar
->GetRange();
310 int sbThumb
= stc
->m_hScrollBar
->GetPageSize();
311 int sbPos
= stc
->m_hScrollBar
->GetThumbPosition();
312 if ((sbMax
!= horizEnd
) || (sbThumb
!= pageWidth
) || (sbPos
!= 0)) {
313 stc
->m_hScrollBar
->SetScrollbar(0, pageWidth
, horizEnd
, pageWidth
);
315 if (scrollWidth
< pageWidth
) {
316 HorizontalScrollTo(0);
325 void ScintillaWX::NotifyChange() {
330 void ScintillaWX::NotifyParent(SCNotification scn
) {
331 stc
->NotifyParent(&scn
);
336 void ScintillaWX::Copy() {
337 if (currentPos
!= anchor
) {
339 CopySelectionRange(&st
);
340 if (wxTheClipboard
->Open()) {
341 wxTheClipboard
->UsePrimarySelection(FALSE
);
342 wxString text
= stc2wx(st
.s
, st
.len
);
343 wxTheClipboard
->SetData(new wxTextDataObject(text
));
344 wxTheClipboard
->Close();
350 void ScintillaWX::Paste() {
351 pdoc
->BeginUndoAction();
354 wxTextDataObject data
;
355 bool gotData
= FALSE
;
357 if (wxTheClipboard
->Open()) {
358 wxTheClipboard
->UsePrimarySelection(FALSE
);
359 gotData
= wxTheClipboard
->GetData(data
);
360 wxTheClipboard
->Close();
363 wxWX2MBbuf buf
= (wxWX2MBbuf
)wx2stc(data
.GetText());
364 int len
= strlen(buf
);
365 pdoc
->InsertString(currentPos
, buf
, len
);
366 SetEmptySelection(currentPos
+ len
);
369 pdoc
->EndUndoAction();
375 bool ScintillaWX::CanPaste() {
376 bool canPaste
= FALSE
;
379 if ( (didOpen
= !wxTheClipboard
->IsOpened()) )
380 wxTheClipboard
->Open();
382 if (wxTheClipboard
->IsOpened()) {
383 wxTheClipboard
->UsePrimarySelection(FALSE
);
384 canPaste
= wxTheClipboard
->IsSupported(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
);
386 wxTheClipboard
->Close();
391 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
392 ct
.wCallTip
= new wxSTCCallTip(stc
, &ct
);
393 ct
.wDraw
= ct
.wCallTip
;
397 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
399 ((wxMenu
*)popup
.GetID())->AppendSeparator();
401 ((wxMenu
*)popup
.GetID())->Append(cmd
, stc2wx(label
));
404 ((wxMenu
*)popup
.GetID())->Enable(cmd
, enabled
);
408 // This is called by the Editor base class whenever something is selected
409 void ScintillaWX::ClaimSelection() {
411 // Put the selected text in the PRIMARY selection
412 if (currentPos
!= anchor
) {
414 CopySelectionRange(&st
);
415 if (wxTheClipboard
->Open()) {
416 wxTheClipboard
->UsePrimarySelection(TRUE
);
417 wxString text
= stc2wx(st
.s
, st
.len
);
418 wxTheClipboard
->SetData(new wxTextDataObject(text
));
419 wxTheClipboard
->UsePrimarySelection(FALSE
);
420 wxTheClipboard
->Close();
427 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
431 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
432 // switch (iMessage) {
434 // return CanPaste();
436 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
443 //----------------------------------------------------------------------
446 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
448 paintState
= painting
;
449 Surface
* surfaceWindow
= Surface::Allocate();
450 surfaceWindow
->Init(dc
);
451 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
453 Paint(surfaceWindow
, rcPaint
);
455 delete surfaceWindow
;
456 if (paintState
== paintAbandoned
) {
457 // Painting area was insufficient to cover new styling or brace highlight positions
460 paintState
= notPainting
;
462 // On wxGTK the editor window paints can overwrite the listbox...
464 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
469 void ScintillaWX::DoHScroll(int type
, int pos
) {
471 PRectangle rcText
= GetTextRectangle();
472 int pageWidth
= rcText
.Width() * 2 / 3;
473 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
474 xPos
-= H_SCROLL_STEP
;
475 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
476 xPos
+= H_SCROLL_STEP
;
477 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
479 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
) {
481 if (xPos
> scrollWidth
- rcText
.Width()) {
482 xPos
= scrollWidth
- rcText
.Width();
485 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
487 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
489 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
492 HorizontalScrollTo(xPos
);
495 void ScintillaWX::DoVScroll(int type
, int pos
) {
496 int topLineNew
= topLine
;
497 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
499 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
501 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
502 topLineNew
-= LinesToScroll();
503 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
504 topLineNew
+= LinesToScroll();
505 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
507 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
508 topLineNew
= MaxScrollPos();
509 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
512 ScrollTo(topLineNew
);
515 void ScintillaWX::DoMouseWheel(int rotation
, int delta
,
516 int linesPerAction
, int ctrlDown
,
517 bool isPageScroll
) {
518 int topLineNew
= topLine
;
521 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
523 KeyCommand(SCI_ZOOMIN
);
526 KeyCommand(SCI_ZOOMOUT
);
529 else { // otherwise just scroll the window
530 wheelRotation
+= rotation
;
531 lines
= wheelRotation
/ delta
;
532 wheelRotation
-= lines
* delta
;
535 lines
= lines
* LinesOnScreen(); // lines is either +1 or -1
537 lines
*= linesPerAction
;
539 ScrollTo(topLineNew
);
545 void ScintillaWX::DoSize(int width
, int height
) {
546 // PRectangle rcClient(0,0,width,height);
547 // SetScrollBarsTo(rcClient);
552 void ScintillaWX::DoLoseFocus(){
553 SetFocusState(false);
556 void ScintillaWX::DoGainFocus(){
560 void ScintillaWX::DoSysColourChange() {
561 InvalidateStyleData();
564 void ScintillaWX::DoLeftButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
565 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
568 void ScintillaWX::DoLeftButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
569 ButtonUp(pt
, curTime
, ctrl
);
572 void ScintillaWX::DoLeftButtonMove(Point pt
) {
576 void ScintillaWX::DoMiddleButtonUp(Point pt
) {
578 // Set the current position to the mouse click point and
579 // then paste in the PRIMARY selection, if any. wxGTK only.
580 int newPos
= PositionFromLocation(pt
);
581 MovePositionTo(newPos
, 0, 1);
583 pdoc
->BeginUndoAction();
584 wxTextDataObject data
;
585 bool gotData
= FALSE
;
586 if (wxTheClipboard
->Open()) {
587 wxTheClipboard
->UsePrimarySelection(TRUE
);
588 gotData
= wxTheClipboard
->GetData(data
);
589 wxTheClipboard
->UsePrimarySelection(FALSE
);
590 wxTheClipboard
->Close();
593 wxWX2MBbuf buf
= (wxWX2MBbuf
)wx2stc(data
.GetText());
594 int len
= strlen(buf
);
595 pdoc
->InsertString(currentPos
, buf
, len
);
596 SetEmptySelection(currentPos
+ len
);
598 pdoc
->EndUndoAction();
602 ShowCaretAtCurrentPosition();
603 EnsureCaretVisible();
608 void ScintillaWX::DoAddChar(int key
) {
613 wxString
uniChar(ansiChars
, wxConvLocal
);
614 wxWX2MBbuf buf
= (wxWX2MBbuf
)wx2stc(uniChar
);
615 AddCharUTF((char*)buf
.data(), strlen(buf
));
622 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
623 #if defined(__WXGTK__) || defined(__WXMAC__)
624 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
625 if (ctrl
&& key
>= 1 && key
<= 26)
630 case WXK_DOWN
: key
= SCK_DOWN
; break;
631 case WXK_UP
: key
= SCK_UP
; break;
632 case WXK_LEFT
: key
= SCK_LEFT
; break;
633 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
634 case WXK_HOME
: key
= SCK_HOME
; break;
635 case WXK_END
: key
= SCK_END
; break;
636 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
637 case WXK_NEXT
: key
= SCK_NEXT
; break;
638 case WXK_DELETE
: key
= SCK_DELETE
; break;
639 case WXK_INSERT
: key
= SCK_INSERT
; break;
640 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
641 case WXK_BACK
: key
= SCK_BACK
; break;
642 case WXK_TAB
: key
= SCK_TAB
; break;
643 case WXK_RETURN
: key
= SCK_RETURN
; break;
644 case WXK_ADD
: // fall through
645 case WXK_NUMPAD_ADD
: key
= SCK_ADD
; break;
646 case WXK_SUBTRACT
: // fall through
647 case WXK_NUMPAD_SUBTRACT
: key
= SCK_SUBTRACT
; break;
648 case WXK_DIVIDE
: // fall through
649 case WXK_NUMPAD_DIVIDE
: key
= SCK_DIVIDE
; break;
650 case WXK_CONTROL
: key
= 0; break;
651 case WXK_ALT
: key
= 0; break;
652 case WXK_SHIFT
: key
= 0; break;
653 case WXK_MENU
: key
= 0; break;
656 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
665 void ScintillaWX::DoCommand(int ID
) {
670 void ScintillaWX::DoContextMenu(Point pt
) {
671 if (displayPopupMenu
)
675 void ScintillaWX::DoOnListBox() {
676 AutoCompleteCompleted();
679 //----------------------------------------------------------------------
681 #if wxUSE_DRAG_AND_DROP
682 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
683 SetDragPosition(invalidPosition
);
685 // Send an event to allow the drag details to be changed
686 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
687 evt
.SetEventObject(stc
);
688 evt
.SetDragResult(dragResult
);
691 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
692 evt
.SetDragText(data
);
693 stc
->GetEventHandler()->ProcessEvent(evt
);
695 dragResult
= evt
.GetDragResult();
696 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
697 DropAt(evt
.GetPosition(),
698 wx2stc(evt
.GetDragText()),
699 dragResult
== wxDragMove
,
700 FALSE
); // TODO: rectangular?
707 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
713 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
714 SetDragPosition(PositionFromLocation(Point(x
, y
)));
716 // Send an event to allow the drag result to be changed
717 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
718 evt
.SetEventObject(stc
);
719 evt
.SetDragResult(def
);
722 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
723 stc
->GetEventHandler()->ProcessEvent(evt
);
725 dragResult
= evt
.GetDragResult();
730 void ScintillaWX::DoDragLeave() {
731 SetDragPosition(invalidPosition
);
734 //----------------------------------------------------------------------
736 // Redraw all of text area. This paint will not be abandoned.
737 void ScintillaWX::FullPaint() {
738 paintState
= painting
;
739 rcPaint
= GetTextRectangle();
740 paintingAllText
= true;
742 Surface
* surfaceWindow
= Surface::Allocate();
743 surfaceWindow
->Init(&dc
);
744 Paint(surfaceWindow
, rcPaint
);
745 delete surfaceWindow
;
747 // stc->Refresh(FALSE);
749 paintState
= notPainting
;
753 void ScintillaWX::DoScrollToLine(int line
) {
758 void ScintillaWX::DoScrollToColumn(int column
) {
759 HorizontalScrollTo(column
* vs
.spaceWidth
);
764 //----------------------------------------------------------------------
765 //----------------------------------------------------------------------