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"
23 //----------------------------------------------------------------------
25 const int H_SCROLL_MAX
= 4000;
26 const int H_SCROLL_STEP
= 20;
27 const int H_SCROLL_PAGE
= 200;
29 //----------------------------------------------------------------------
32 class wxSTCTimer
: public wxTimer
{
34 wxSTCTimer(ScintillaWX
* swx
) {
47 #if wxUSE_DRAG_AND_DROP
48 bool wxSTCDropTarget::OnDropText(wxCoord x
, wxCoord y
, const wxString
& data
) {
49 return swx
->DoDropText(x
, y
, data
);
52 wxDragResult
wxSTCDropTarget::OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
53 return swx
->DoDragEnter(x
, y
, def
);
56 wxDragResult
wxSTCDropTarget::OnDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
57 return swx
->DoDragOver(x
, y
, def
);
60 void wxSTCDropTarget::OnLeave() {
66 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
67 #include <wx/popupwin.h>
68 #define wxSTCCallTipBase wxPopupWindow
69 #define param2 wxBORDER_NONE // popup's 2nd param is flags
71 #define wxSTCCallTipBase wxWindow
72 #define param2 -1 // wxWindows 2nd param is ID
75 class wxSTCCallTip
: public wxSTCCallTipBase
{
77 wxSTCCallTip(wxWindow
* parent
, CallTip
* ct
)
78 : wxSTCCallTipBase(parent
, param2
)
83 void OnPaint(wxPaintEvent
& evt
) {
85 Surface
* surfaceWindow
= Surface::Allocate();
86 surfaceWindow
->Init(&dc
);
87 m_ct
->PaintCT(surfaceWindow
);
91 void OnFocus(wxFocusEvent
& event
) {
92 GetParent()->SetFocus();
96 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
97 virtual void DoSetSize(int x
, int y
,
98 int width
, int height
,
99 int sizeFlags
= wxSIZE_AUTO
) {
101 GetParent()->ClientToScreen(&x
, NULL
);
103 GetParent()->ClientToScreen(NULL
, &y
);
104 wxSTCCallTipBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
107 virtual bool Show( bool show
= TRUE
) {
108 bool retval
= wxSTCCallTipBase::Show(show
);
113 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
);
262 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
263 bool modified
= false;
265 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
266 int sbMax
= stc
->GetScrollRange(wxVERTICAL
);
267 int sbThumb
= stc
->GetScrollThumb(wxVERTICAL
);
268 int sbPos
= stc
->GetScrollPos(wxVERTICAL
);
269 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
270 stc
->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
274 else { // otherwise use the one that's been given to us
275 int sbMax
= stc
->m_vScrollBar
->GetRange();
276 int sbPage
= stc
->m_vScrollBar
->GetPageSize();
277 int sbPos
= stc
->m_vScrollBar
->GetThumbPosition();
278 if (sbMax
!= nMax
|| sbPage
!= nPage
) {
279 stc
->m_vScrollBar
->SetScrollbar(sbPos
, nPage
, nMax
, nPage
);
285 if (horizontalScrollBarVisible
) {
286 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
287 int sbMax
= stc
->GetScrollRange(wxHORIZONTAL
);
288 int sbThumb
= stc
->GetScrollThumb(wxHORIZONTAL
);
289 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
290 stc
->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
294 else { // otherwise use the one that's been given to us
295 int sbMax
= stc
->m_hScrollBar
->GetRange();
296 int sbPage
= stc
->m_hScrollBar
->GetPageSize();
297 if ((sbMax
!= H_SCROLL_MAX
) || (sbPage
!= H_SCROLL_STEP
)) {
298 stc
->m_hScrollBar
->SetScrollbar(0, H_SCROLL_STEP
, H_SCROLL_MAX
, H_SCROLL_STEP
);
307 void ScintillaWX::NotifyChange() {
312 void ScintillaWX::NotifyParent(SCNotification scn
) {
313 stc
->NotifyParent(&scn
);
318 void ScintillaWX::Copy() {
319 if (currentPos
!= anchor
) {
321 CopySelectionRange(&st
);
322 wxTheClipboard
->Open();
323 wxTheClipboard
->UsePrimarySelection();
324 wxString text
= stc2wx(st
.s
, st
.len
);
325 wxTheClipboard
->SetData(new wxTextDataObject(text
));
326 wxTheClipboard
->Close();
331 void ScintillaWX::Paste() {
332 pdoc
->BeginUndoAction();
335 wxTextDataObject data
;
338 wxTheClipboard
->Open();
339 wxTheClipboard
->UsePrimarySelection();
340 gotData
= wxTheClipboard
->GetData(data
);
341 wxTheClipboard
->Close();
343 wxWX2MBbuf buf
= (wxWX2MBbuf
)wx2stc(data
.GetText());
344 int len
= strlen(buf
);
345 pdoc
->InsertString(currentPos
, buf
, len
);
346 SetEmptySelection(currentPos
+ len
);
349 pdoc
->EndUndoAction();
355 bool ScintillaWX::CanPaste() {
358 wxTheClipboard
->Open();
359 wxTheClipboard
->UsePrimarySelection();
360 canPaste
= wxTheClipboard
->IsSupported(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
);
361 wxTheClipboard
->Close();
366 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
367 ct
.wCallTip
= new wxSTCCallTip(stc
, &ct
);
368 ct
.wDraw
= ct
.wCallTip
;
372 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
374 ((wxMenu
*)popup
.GetID())->AppendSeparator();
376 ((wxMenu
*)popup
.GetID())->Append(cmd
, stc2wx(label
));
379 ((wxMenu
*)popup
.GetID())->Enable(cmd
, enabled
);
383 void ScintillaWX::ClaimSelection() {
388 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
392 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
393 // switch (iMessage) {
395 // return CanPaste();
397 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
404 //----------------------------------------------------------------------
407 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
409 paintState
= painting
;
410 Surface
* surfaceWindow
= Surface::Allocate();
411 surfaceWindow
->Init(dc
);
412 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
414 Paint(surfaceWindow
, rcPaint
);
416 delete surfaceWindow
;
417 if (paintState
== paintAbandoned
) {
418 // Painting area was insufficient to cover new styling or brace highlight positions
421 paintState
= notPainting
;
423 // On wxGTK the editor window paints can overwrite the listbox...
425 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
430 void ScintillaWX::DoHScroll(int type
, int pos
) {
432 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
433 xPos
-= H_SCROLL_STEP
;
434 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
435 xPos
+= H_SCROLL_STEP
;
436 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
437 xPos
-= H_SCROLL_PAGE
;
438 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
439 xPos
+= H_SCROLL_PAGE
;
440 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
442 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
444 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
447 HorizontalScrollTo(xPos
);
450 void ScintillaWX::DoVScroll(int type
, int pos
) {
451 int topLineNew
= topLine
;
452 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
454 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
456 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
457 topLineNew
-= LinesToScroll();
458 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
459 topLineNew
+= LinesToScroll();
460 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
462 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
463 topLineNew
= MaxScrollPos();
464 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
467 ScrollTo(topLineNew
);
470 void ScintillaWX::DoMouseWheel(int rotation
, int delta
,
471 int linesPerAction
, int ctrlDown
,
472 bool isPageScroll
) {
473 int topLineNew
= topLine
;
476 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
478 KeyCommand(SCI_ZOOMIN
);
481 KeyCommand(SCI_ZOOMOUT
);
484 else { // otherwise just scroll the window
485 wheelRotation
+= rotation
;
486 lines
= wheelRotation
/ delta
;
487 wheelRotation
-= lines
* delta
;
490 lines
= lines
* LinesOnScreen(); // lines is either +1 or -1
492 lines
*= linesPerAction
;
494 ScrollTo(topLineNew
);
500 void ScintillaWX::DoSize(int width
, int height
) {
501 // PRectangle rcClient(0,0,width,height);
502 // SetScrollBarsTo(rcClient);
507 void ScintillaWX::DoLoseFocus(){
508 SetFocusState(false);
511 void ScintillaWX::DoGainFocus(){
515 void ScintillaWX::DoSysColourChange() {
516 InvalidateStyleData();
519 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
520 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
523 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
524 ButtonUp(pt
, curTime
, ctrl
);
527 void ScintillaWX::DoButtonMove(Point pt
) {
532 void ScintillaWX::DoAddChar(int key
) {
536 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
537 #if defined(__WXGTK__) || defined(__WXMAC__)
538 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
539 if (ctrl
&& key
>= 1 && key
<= 26)
544 case WXK_DOWN
: key
= SCK_DOWN
; break;
545 case WXK_UP
: key
= SCK_UP
; break;
546 case WXK_LEFT
: key
= SCK_LEFT
; break;
547 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
548 case WXK_HOME
: key
= SCK_HOME
; break;
549 case WXK_END
: key
= SCK_END
; break;
550 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
551 case WXK_NEXT
: key
= SCK_NEXT
; break;
552 case WXK_DELETE
: key
= SCK_DELETE
; break;
553 case WXK_INSERT
: key
= SCK_INSERT
; break;
554 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
555 case WXK_BACK
: key
= SCK_BACK
; break;
556 case WXK_TAB
: key
= SCK_TAB
; break;
557 case WXK_RETURN
: key
= SCK_RETURN
; break;
558 case WXK_ADD
: // fall through
559 case WXK_NUMPAD_ADD
: key
= SCK_ADD
; break;
560 case WXK_SUBTRACT
: // fall through
561 case WXK_NUMPAD_SUBTRACT
: key
= SCK_SUBTRACT
; break;
562 case WXK_DIVIDE
: // fall through
563 case WXK_NUMPAD_DIVIDE
: key
= SCK_DIVIDE
; break;
564 case WXK_CONTROL
: key
= 0; break;
565 case WXK_ALT
: key
= 0; break;
566 case WXK_SHIFT
: key
= 0; break;
567 case WXK_MENU
: key
= 0; break;
570 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
579 void ScintillaWX::DoCommand(int ID
) {
584 void ScintillaWX::DoContextMenu(Point pt
) {
585 if (displayPopupMenu
)
589 void ScintillaWX::DoOnListBox() {
590 AutoCompleteCompleted();
593 //----------------------------------------------------------------------
595 #if wxUSE_DRAG_AND_DROP
596 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
597 SetDragPosition(invalidPosition
);
599 // Send an event to allow the drag details to be changed
600 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
601 evt
.SetEventObject(stc
);
602 evt
.SetDragResult(dragResult
);
605 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
606 evt
.SetDragText(data
);
607 stc
->GetEventHandler()->ProcessEvent(evt
);
609 dragResult
= evt
.GetDragResult();
610 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
611 DropAt(evt
.GetPosition(),
612 wx2stc(evt
.GetDragText()),
613 dragResult
== wxDragMove
,
614 FALSE
); // TODO: rectangular?
621 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
627 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
628 SetDragPosition(PositionFromLocation(Point(x
, y
)));
630 // Send an event to allow the drag result to be changed
631 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
632 evt
.SetEventObject(stc
);
633 evt
.SetDragResult(def
);
636 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
637 stc
->GetEventHandler()->ProcessEvent(evt
);
639 dragResult
= evt
.GetDragResult();
644 void ScintillaWX::DoDragLeave() {
645 SetDragPosition(invalidPosition
);
648 //----------------------------------------------------------------------
650 // Redraw all of text area. This paint will not be abandoned.
651 void ScintillaWX::FullPaint() {
652 paintState
= painting
;
653 rcPaint
= GetTextRectangle();
654 paintingAllText
= true;
656 Surface
* surfaceWindow
= Surface::Allocate();
657 surfaceWindow
->Init(&dc
);
658 Paint(surfaceWindow
, rcPaint
);
659 delete surfaceWindow
;
661 // stc->Refresh(FALSE);
663 paintState
= notPainting
;
667 void ScintillaWX::DoScrollToLine(int line
) {
672 void ScintillaWX::DoScrollToColumn(int column
) {
673 HorizontalScrollTo(column
* vs
.spaceWidth
);
678 //----------------------------------------------------------------------
679 //----------------------------------------------------------------------