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
)
84 if (HasCapture()) ReleaseMouse();
87 void OnPaint(wxPaintEvent
& evt
) {
89 Surface
* surfaceWindow
= Surface
::Allocate();
90 surfaceWindow
->Init(&dc
);
91 m_ct
->PaintCT(surfaceWindow
);
95 void OnFocus(wxFocusEvent
& event
) {
96 GetParent()->SetFocus();
100 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
101 virtual void DoSetSize(int x
, int y
,
102 int width
, int height
,
103 int sizeFlags
= wxSIZE_AUTO
) {
105 GetParent()->ClientToScreen(&x
, NULL
);
107 GetParent()->ClientToScreen(NULL
, &y
);
108 wxSTCCallTipBase
::DoSetSize(x
, y
, width
, height
, sizeFlags
);
111 virtual bool Show( bool show
= TRUE
) {
112 bool retval
= wxSTCCallTipBase
::Show(show
);
116 if (HasCapture()) ReleaseMouse();
120 void OnLeftDown(wxMouseEvent
& ) {
127 DECLARE_EVENT_TABLE()
130 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxSTCCallTipBase
)
131 EVT_PAINT(wxSTCCallTip
::OnPaint
)
132 EVT_SET_FOCUS(wxSTCCallTip
::OnFocus
)
133 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
134 EVT_LEFT_DOWN(wxSTCCallTip
::OnLeftDown
)
139 //----------------------------------------------------------------------
140 // Constructor/Destructor
143 ScintillaWX
::ScintillaWX(wxStyledTextCtrl
* win
) {
144 capturedMouse
= false;
152 ScintillaWX
::~ScintillaWX() {
156 //----------------------------------------------------------------------
157 // base class virtuals
160 void ScintillaWX
::Initialise() {
161 //ScintillaBase::Initialise();
162 #if wxUSE_DRAG_AND_DROP
163 dropTarget
= new wxSTCDropTarget
;
164 dropTarget
->SetScintilla(this);
165 stc
->SetDropTarget(dropTarget
);
170 void ScintillaWX
::Finalise() {
171 ScintillaBase
::Finalise();
175 void ScintillaWX
::StartDrag() {
176 #if wxUSE_DRAG_AND_DROP
177 wxString dragText
= stc2wx(drag
.s
, drag
.len
);
179 // Send an event to allow the drag text to be changed
180 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
181 evt
.SetEventObject(stc
);
182 evt
.SetDragText(dragText
);
183 evt
.SetDragAllowMove(TRUE
);
184 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
185 stc
->GetSelectionEnd()));
186 stc
->GetEventHandler()->ProcessEvent(evt
);
187 dragText
= evt
.GetDragText();
189 if (dragText
.Length()) {
190 wxDropSource
source(stc
);
191 wxTextDataObject
data(dragText
);
194 source
.SetData(data
);
195 dropWentOutside
= TRUE
;
196 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
197 if (result
== wxDragMove
&& dropWentOutside
)
200 SetDragPosition(invalidPosition
);
206 void ScintillaWX
::SetTicking(bool on
) {
207 wxSTCTimer
* steTimer
;
208 if (timer
.ticking
!= on
) {
211 steTimer
= new wxSTCTimer(this);
212 steTimer
->Start(timer
.tickSize
);
213 timer
.tickerID
= steTimer
;
215 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
221 timer
.ticksToWait
= caret
.period
;
225 void ScintillaWX
::SetMouseCapture(bool on
) {
226 if (on
&& !capturedMouse
)
228 else if (!on
&& capturedMouse
)
234 bool ScintillaWX
::HaveMouseCapture() {
235 return capturedMouse
;
239 void ScintillaWX
::ScrollText(int linesToMove
) {
240 int dy
= vs
.lineHeight
* (linesToMove
);
241 stc
->ScrollWindow(0, dy
);
245 void ScintillaWX
::SetVerticalScrollPos() {
246 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
247 stc
->SetScrollPos(wxVERTICAL
, topLine
);
249 else { // otherwise use the one that's been given to us
250 stc
->m_vScrollBar
->SetThumbPosition(topLine
);
254 void ScintillaWX
::SetHorizontalScrollPos() {
255 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
256 stc
->SetScrollPos(wxHORIZONTAL
, xOffset
);
258 else { // otherwise use the one that's been given to us
259 stc
->m_hScrollBar
->SetThumbPosition(xOffset
);
264 bool ScintillaWX
::ModifyScrollBars(int nMax
, int nPage
) {
265 bool modified
= false;
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
);
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
, nPage
);
287 if (horizontalScrollBarVisible
) {
288 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
289 int sbMax
= stc
->GetScrollRange(wxHORIZONTAL
);
290 int sbThumb
= stc
->GetScrollThumb(wxHORIZONTAL
);
291 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
292 stc
->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
296 else { // otherwise use the one that's been given to us
297 int sbMax
= stc
->m_hScrollBar
->GetRange();
298 int sbPage
= stc
->m_hScrollBar
->GetPageSize();
299 if ((sbMax
!= H_SCROLL_MAX
) || (sbPage
!= H_SCROLL_STEP
)) {
300 stc
->m_hScrollBar
->SetScrollbar(0, H_SCROLL_STEP
, H_SCROLL_MAX
, H_SCROLL_STEP
);
309 void ScintillaWX
::NotifyChange() {
314 void ScintillaWX
::NotifyParent(SCNotification scn
) {
315 stc
->NotifyParent(&scn
);
320 void ScintillaWX
::Copy() {
321 if (currentPos
!= anchor
) {
323 CopySelectionRange(&st
);
324 if (wxTheClipboard
->Open()) {
325 wxTheClipboard
->UsePrimarySelection();
326 wxString text
= stc2wx(st
.s
, st
.len
);
327 wxTheClipboard
->SetData(new wxTextDataObject(text
));
328 wxTheClipboard
->Close();
334 void ScintillaWX
::Paste() {
335 pdoc
->BeginUndoAction();
338 wxTextDataObject data
;
339 bool gotData
= FALSE
;
341 if (wxTheClipboard
->Open()) {
342 wxTheClipboard
->UsePrimarySelection();
343 gotData
= wxTheClipboard
->GetData(data
);
344 wxTheClipboard
->Close();
347 wxWX2MBbuf buf
= (wxWX2MBbuf
)wx2stc(data
.GetText());
348 int len
= strlen(buf
);
349 pdoc
->InsertString(currentPos
, buf
, len
);
350 SetEmptySelection(currentPos
+ len
);
353 pdoc
->EndUndoAction();
359 bool ScintillaWX
::CanPaste() {
360 bool canPaste
= FALSE
;
363 if ( (didOpen
= !wxTheClipboard
->IsOpened()) )
364 wxTheClipboard
->Open();
366 if (wxTheClipboard
->IsOpened()) {
367 wxTheClipboard
->UsePrimarySelection();
368 canPaste
= wxTheClipboard
->IsSupported(wxUSE_UNICODE ? wxDF_UNICODETEXT
: wxDF_TEXT
);
370 wxTheClipboard
->Close();
375 void ScintillaWX
::CreateCallTipWindow(PRectangle
) {
376 ct
.wCallTip
= new wxSTCCallTip(stc
, &ct
);
377 ct
.wDraw
= ct
.wCallTip
;
381 void ScintillaWX
::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
383 ((wxMenu
*)popup
.GetID())->AppendSeparator();
385 ((wxMenu
*)popup
.GetID())->Append(cmd
, stc2wx(label
));
388 ((wxMenu
*)popup
.GetID())->Enable(cmd
, enabled
);
392 void ScintillaWX
::ClaimSelection() {
397 long ScintillaWX
::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
401 long ScintillaWX
::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
402 // switch (iMessage) {
404 // return CanPaste();
406 return ScintillaBase
::WndProc(iMessage
, wParam
, lParam
);
413 //----------------------------------------------------------------------
416 void ScintillaWX
::DoPaint(wxDC
* dc
, wxRect rect
) {
418 paintState
= painting
;
419 Surface
* surfaceWindow
= Surface
::Allocate();
420 surfaceWindow
->Init(dc
);
421 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
423 Paint(surfaceWindow
, rcPaint
);
425 delete surfaceWindow
;
426 if (paintState
== paintAbandoned
) {
427 // Painting area was insufficient to cover new styling or brace highlight positions
430 paintState
= notPainting
;
432 // On wxGTK the editor window paints can overwrite the listbox...
434 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
439 void ScintillaWX
::DoHScroll(int type
, int pos
) {
441 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
442 xPos
-= H_SCROLL_STEP
;
443 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
444 xPos
+= H_SCROLL_STEP
;
445 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
446 xPos
-= H_SCROLL_PAGE
;
447 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
448 xPos
+= H_SCROLL_PAGE
;
449 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
451 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
453 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
456 HorizontalScrollTo(xPos
);
459 void ScintillaWX
::DoVScroll(int type
, int pos
) {
460 int topLineNew
= topLine
;
461 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
463 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
465 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
466 topLineNew
-= LinesToScroll();
467 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
468 topLineNew
+= LinesToScroll();
469 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
471 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
472 topLineNew
= MaxScrollPos();
473 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
476 ScrollTo(topLineNew
);
479 void ScintillaWX
::DoMouseWheel(int rotation
, int delta
,
480 int linesPerAction
, int ctrlDown
,
481 bool isPageScroll
) {
482 int topLineNew
= topLine
;
485 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
487 KeyCommand(SCI_ZOOMIN
);
490 KeyCommand(SCI_ZOOMOUT
);
493 else { // otherwise just scroll the window
494 wheelRotation
+= rotation
;
495 lines
= wheelRotation
/ delta
;
496 wheelRotation
-= lines
* delta
;
499 lines
= lines
* LinesOnScreen(); // lines is either +1 or -1
501 lines
*= linesPerAction
;
503 ScrollTo(topLineNew
);
509 void ScintillaWX
::DoSize(int width
, int height
) {
510 // PRectangle rcClient(0,0,width,height);
511 // SetScrollBarsTo(rcClient);
516 void ScintillaWX
::DoLoseFocus(){
517 SetFocusState(false);
520 void ScintillaWX
::DoGainFocus(){
524 void ScintillaWX
::DoSysColourChange() {
525 InvalidateStyleData();
528 void ScintillaWX
::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
529 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
532 void ScintillaWX
::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
533 ButtonUp(pt
, curTime
, ctrl
);
536 void ScintillaWX
::DoButtonMove(Point pt
) {
541 void ScintillaWX
::DoAddChar(int key
) {
545 int ScintillaWX
::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
546 #if defined(__WXGTK__) || defined(__WXMAC__)
547 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
548 if (ctrl
&& key
>= 1 && key
<= 26)
553 case WXK_DOWN
: key
= SCK_DOWN
; break;
554 case WXK_UP
: key
= SCK_UP
; break;
555 case WXK_LEFT
: key
= SCK_LEFT
; break;
556 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
557 case WXK_HOME
: key
= SCK_HOME
; break;
558 case WXK_END
: key
= SCK_END
; break;
559 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
560 case WXK_NEXT
: key
= SCK_NEXT
; break;
561 case WXK_DELETE
: key
= SCK_DELETE
; break;
562 case WXK_INSERT
: key
= SCK_INSERT
; break;
563 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
564 case WXK_BACK
: key
= SCK_BACK
; break;
565 case WXK_TAB
: key
= SCK_TAB
; break;
566 case WXK_RETURN
: key
= SCK_RETURN
; break;
567 case WXK_ADD
: // fall through
568 case WXK_NUMPAD_ADD
: key
= SCK_ADD
; break;
569 case WXK_SUBTRACT
: // fall through
570 case WXK_NUMPAD_SUBTRACT
: key
= SCK_SUBTRACT
; break;
571 case WXK_DIVIDE
: // fall through
572 case WXK_NUMPAD_DIVIDE
: key
= SCK_DIVIDE
; break;
573 case WXK_CONTROL
: key
= 0; break;
574 case WXK_ALT
: key
= 0; break;
575 case WXK_SHIFT
: key
= 0; break;
576 case WXK_MENU
: key
= 0; break;
579 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
588 void ScintillaWX
::DoCommand(int ID
) {
593 void ScintillaWX
::DoContextMenu(Point pt
) {
594 if (displayPopupMenu
)
598 void ScintillaWX
::DoOnListBox() {
599 AutoCompleteCompleted();
602 //----------------------------------------------------------------------
604 #if wxUSE_DRAG_AND_DROP
605 bool ScintillaWX
::DoDropText(long x
, long y
, const wxString
& data
) {
606 SetDragPosition(invalidPosition
);
608 // Send an event to allow the drag details to be changed
609 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
610 evt
.SetEventObject(stc
);
611 evt
.SetDragResult(dragResult
);
614 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
615 evt
.SetDragText(data
);
616 stc
->GetEventHandler()->ProcessEvent(evt
);
618 dragResult
= evt
.GetDragResult();
619 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
620 DropAt(evt
.GetPosition(),
621 wx2stc(evt
.GetDragText()),
622 dragResult
== wxDragMove
,
623 FALSE
); // TODO: rectangular?
630 wxDragResult ScintillaWX
::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
636 wxDragResult ScintillaWX
::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
637 SetDragPosition(PositionFromLocation(Point(x
, y
)));
639 // Send an event to allow the drag result to be changed
640 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
641 evt
.SetEventObject(stc
);
642 evt
.SetDragResult(def
);
645 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
646 stc
->GetEventHandler()->ProcessEvent(evt
);
648 dragResult
= evt
.GetDragResult();
653 void ScintillaWX
::DoDragLeave() {
654 SetDragPosition(invalidPosition
);
657 //----------------------------------------------------------------------
659 // Redraw all of text area. This paint will not be abandoned.
660 void ScintillaWX
::FullPaint() {
661 paintState
= painting
;
662 rcPaint
= GetTextRectangle();
663 paintingAllText
= true;
665 Surface
* surfaceWindow
= Surface
::Allocate();
666 surfaceWindow
->Init(&dc
);
667 Paint(surfaceWindow
, rcPaint
);
668 delete surfaceWindow
;
670 // stc->Refresh(FALSE);
672 paintState
= notPainting
;
676 void ScintillaWX
::DoScrollToLine(int line
) {
681 void ScintillaWX
::DoScrollToColumn(int column
) {
682 HorizontalScrollTo(column
* vs
.spaceWidth
);
687 //----------------------------------------------------------------------
688 //----------------------------------------------------------------------