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() {
67 #undef wxSTC_USE_POPUP
68 #define wxSTC_USE_POPUP 0
71 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
72 #include <wx/popupwin.h>
73 #define wxSTCCallTipBase wxPopupWindow
74 #define param2 wxBORDER_NONE // popup's 2nd param is flags
76 #define wxSTCCallTipBase wxWindow
77 #define param2 -1 // wxWindows 2nd param is ID
80 class wxSTCCallTip
: public wxSTCCallTipBase
{
82 wxSTCCallTip(wxWindow
* parent
, CallTip
* ct
)
83 : wxSTCCallTipBase(parent
, param2
)
89 if (HasCapture()) ReleaseMouse();
92 void OnPaint(wxPaintEvent
& evt
) {
94 Surface
* surfaceWindow
= Surface::Allocate();
95 surfaceWindow
->Init(&dc
);
96 m_ct
->PaintCT(surfaceWindow
);
100 void OnFocus(wxFocusEvent
& event
) {
101 GetParent()->SetFocus();
105 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
106 virtual void DoSetSize(int x
, int y
,
107 int width
, int height
,
108 int sizeFlags
= wxSIZE_AUTO
) {
110 GetParent()->ClientToScreen(&x
, NULL
);
112 GetParent()->ClientToScreen(NULL
, &y
);
113 wxSTCCallTipBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
116 virtual bool Show( bool show
= TRUE
) {
117 bool retval
= wxSTCCallTipBase::Show(show
);
121 if (HasCapture()) ReleaseMouse();
125 void OnLeftDown(wxMouseEvent
& ) {
132 DECLARE_EVENT_TABLE()
135 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxSTCCallTipBase
)
136 EVT_PAINT(wxSTCCallTip::OnPaint
)
137 EVT_SET_FOCUS(wxSTCCallTip::OnFocus
)
138 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
139 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown
)
144 //----------------------------------------------------------------------
145 // Constructor/Destructor
148 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
149 capturedMouse
= false;
157 ScintillaWX::~ScintillaWX() {
161 //----------------------------------------------------------------------
162 // base class virtuals
165 void ScintillaWX::Initialise() {
166 //ScintillaBase::Initialise();
167 #if wxUSE_DRAG_AND_DROP
168 dropTarget
= new wxSTCDropTarget
;
169 dropTarget
->SetScintilla(this);
170 stc
->SetDropTarget(dropTarget
);
175 void ScintillaWX::Finalise() {
176 ScintillaBase::Finalise();
180 void ScintillaWX::StartDrag() {
181 #if wxUSE_DRAG_AND_DROP
182 wxString dragText
= stc2wx(drag
.s
, drag
.len
);
184 // Send an event to allow the drag text to be changed
185 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
186 evt
.SetEventObject(stc
);
187 evt
.SetDragText(dragText
);
188 evt
.SetDragAllowMove(TRUE
);
189 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
190 stc
->GetSelectionEnd()));
191 stc
->GetEventHandler()->ProcessEvent(evt
);
192 dragText
= evt
.GetDragText();
194 if (dragText
.Length()) {
195 wxDropSource
source(stc
);
196 wxTextDataObject
data(dragText
);
199 source
.SetData(data
);
200 dropWentOutside
= TRUE
;
201 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
202 if (result
== wxDragMove
&& dropWentOutside
)
205 SetDragPosition(invalidPosition
);
211 void ScintillaWX::SetTicking(bool on
) {
212 wxSTCTimer
* steTimer
;
213 if (timer
.ticking
!= on
) {
216 steTimer
= new wxSTCTimer(this);
217 steTimer
->Start(timer
.tickSize
);
218 timer
.tickerID
= steTimer
;
220 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
226 timer
.ticksToWait
= caret
.period
;
230 void ScintillaWX::SetMouseCapture(bool on
) {
231 if (on
&& !capturedMouse
)
233 else if (!on
&& capturedMouse
)
239 bool ScintillaWX::HaveMouseCapture() {
240 return capturedMouse
;
244 void ScintillaWX::ScrollText(int linesToMove
) {
245 int dy
= vs
.lineHeight
* (linesToMove
);
246 stc
->ScrollWindow(0, dy
);
250 void ScintillaWX::SetVerticalScrollPos() {
251 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
252 stc
->SetScrollPos(wxVERTICAL
, topLine
);
254 else { // otherwise use the one that's been given to us
255 stc
->m_vScrollBar
->SetThumbPosition(topLine
);
259 void ScintillaWX::SetHorizontalScrollPos() {
260 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
261 stc
->SetScrollPos(wxHORIZONTAL
, xOffset
);
263 else { // otherwise use the one that's been given to us
264 stc
->m_hScrollBar
->SetThumbPosition(xOffset
);
269 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
270 bool modified
= false;
272 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
273 int sbMax
= stc
->GetScrollRange(wxVERTICAL
);
274 int sbThumb
= stc
->GetScrollThumb(wxVERTICAL
);
275 int sbPos
= stc
->GetScrollPos(wxVERTICAL
);
276 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
277 stc
->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
281 else { // otherwise use the one that's been given to us
282 int sbMax
= stc
->m_vScrollBar
->GetRange();
283 int sbPage
= stc
->m_vScrollBar
->GetPageSize();
284 int sbPos
= stc
->m_vScrollBar
->GetThumbPosition();
285 if (sbMax
!= nMax
|| sbPage
!= nPage
) {
286 stc
->m_vScrollBar
->SetScrollbar(sbPos
, nPage
, nMax
, nPage
);
292 if (horizontalScrollBarVisible
) {
293 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
294 int sbMax
= stc
->GetScrollRange(wxHORIZONTAL
);
295 int sbThumb
= stc
->GetScrollThumb(wxHORIZONTAL
);
296 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
297 stc
->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
301 else { // otherwise use the one that's been given to us
302 int sbMax
= stc
->m_hScrollBar
->GetRange();
303 int sbPage
= stc
->m_hScrollBar
->GetPageSize();
304 if ((sbMax
!= H_SCROLL_MAX
) || (sbPage
!= H_SCROLL_STEP
)) {
305 stc
->m_hScrollBar
->SetScrollbar(0, H_SCROLL_STEP
, H_SCROLL_MAX
, H_SCROLL_STEP
);
314 void ScintillaWX::NotifyChange() {
319 void ScintillaWX::NotifyParent(SCNotification scn
) {
320 stc
->NotifyParent(&scn
);
325 void ScintillaWX::Copy() {
326 if (currentPos
!= anchor
) {
328 CopySelectionRange(&st
);
329 if (wxTheClipboard
->Open()) {
330 wxTheClipboard
->UsePrimarySelection();
331 wxString text
= stc2wx(st
.s
, st
.len
);
332 wxTheClipboard
->SetData(new wxTextDataObject(text
));
333 wxTheClipboard
->Close();
339 void ScintillaWX::Paste() {
340 pdoc
->BeginUndoAction();
343 wxTextDataObject data
;
344 bool gotData
= FALSE
;
346 if (wxTheClipboard
->Open()) {
347 wxTheClipboard
->UsePrimarySelection();
348 gotData
= wxTheClipboard
->GetData(data
);
349 wxTheClipboard
->Close();
352 wxWX2MBbuf buf
= (wxWX2MBbuf
)wx2stc(data
.GetText());
353 int len
= strlen(buf
);
354 pdoc
->InsertString(currentPos
, buf
, len
);
355 SetEmptySelection(currentPos
+ len
);
358 pdoc
->EndUndoAction();
364 bool ScintillaWX::CanPaste() {
365 bool canPaste
= FALSE
;
368 if ( (didOpen
= !wxTheClipboard
->IsOpened()) )
369 wxTheClipboard
->Open();
371 if (wxTheClipboard
->IsOpened()) {
372 wxTheClipboard
->UsePrimarySelection();
373 canPaste
= wxTheClipboard
->IsSupported(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
);
375 wxTheClipboard
->Close();
380 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
381 ct
.wCallTip
= new wxSTCCallTip(stc
, &ct
);
382 ct
.wDraw
= ct
.wCallTip
;
386 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
388 ((wxMenu
*)popup
.GetID())->AppendSeparator();
390 ((wxMenu
*)popup
.GetID())->Append(cmd
, stc2wx(label
));
393 ((wxMenu
*)popup
.GetID())->Enable(cmd
, enabled
);
397 void ScintillaWX::ClaimSelection() {
402 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
406 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
407 // switch (iMessage) {
409 // return CanPaste();
411 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
418 //----------------------------------------------------------------------
421 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
423 paintState
= painting
;
424 Surface
* surfaceWindow
= Surface::Allocate();
425 surfaceWindow
->Init(dc
);
426 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
428 Paint(surfaceWindow
, rcPaint
);
430 delete surfaceWindow
;
431 if (paintState
== paintAbandoned
) {
432 // Painting area was insufficient to cover new styling or brace highlight positions
435 paintState
= notPainting
;
437 // On wxGTK the editor window paints can overwrite the listbox...
439 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
444 void ScintillaWX::DoHScroll(int type
, int pos
) {
446 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
447 xPos
-= H_SCROLL_STEP
;
448 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
449 xPos
+= H_SCROLL_STEP
;
450 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
451 xPos
-= H_SCROLL_PAGE
;
452 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
453 xPos
+= H_SCROLL_PAGE
;
454 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
456 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
458 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
461 HorizontalScrollTo(xPos
);
464 void ScintillaWX::DoVScroll(int type
, int pos
) {
465 int topLineNew
= topLine
;
466 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
468 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
470 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
471 topLineNew
-= LinesToScroll();
472 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
473 topLineNew
+= LinesToScroll();
474 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
476 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
477 topLineNew
= MaxScrollPos();
478 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
481 ScrollTo(topLineNew
);
484 void ScintillaWX::DoMouseWheel(int rotation
, int delta
,
485 int linesPerAction
, int ctrlDown
,
486 bool isPageScroll
) {
487 int topLineNew
= topLine
;
490 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
492 KeyCommand(SCI_ZOOMIN
);
495 KeyCommand(SCI_ZOOMOUT
);
498 else { // otherwise just scroll the window
499 wheelRotation
+= rotation
;
500 lines
= wheelRotation
/ delta
;
501 wheelRotation
-= lines
* delta
;
504 lines
= lines
* LinesOnScreen(); // lines is either +1 or -1
506 lines
*= linesPerAction
;
508 ScrollTo(topLineNew
);
514 void ScintillaWX::DoSize(int width
, int height
) {
515 // PRectangle rcClient(0,0,width,height);
516 // SetScrollBarsTo(rcClient);
521 void ScintillaWX::DoLoseFocus(){
522 SetFocusState(false);
525 void ScintillaWX::DoGainFocus(){
529 void ScintillaWX::DoSysColourChange() {
530 InvalidateStyleData();
533 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
534 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
537 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
538 ButtonUp(pt
, curTime
, ctrl
);
541 void ScintillaWX::DoButtonMove(Point pt
) {
546 void ScintillaWX::DoAddChar(int key
) {
550 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
551 #if defined(__WXGTK__) || defined(__WXMAC__)
552 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
553 if (ctrl
&& key
>= 1 && key
<= 26)
558 case WXK_DOWN
: key
= SCK_DOWN
; break;
559 case WXK_UP
: key
= SCK_UP
; break;
560 case WXK_LEFT
: key
= SCK_LEFT
; break;
561 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
562 case WXK_HOME
: key
= SCK_HOME
; break;
563 case WXK_END
: key
= SCK_END
; break;
564 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
565 case WXK_NEXT
: key
= SCK_NEXT
; break;
566 case WXK_DELETE
: key
= SCK_DELETE
; break;
567 case WXK_INSERT
: key
= SCK_INSERT
; break;
568 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
569 case WXK_BACK
: key
= SCK_BACK
; break;
570 case WXK_TAB
: key
= SCK_TAB
; break;
571 case WXK_RETURN
: key
= SCK_RETURN
; break;
572 case WXK_ADD
: // fall through
573 case WXK_NUMPAD_ADD
: key
= SCK_ADD
; break;
574 case WXK_SUBTRACT
: // fall through
575 case WXK_NUMPAD_SUBTRACT
: key
= SCK_SUBTRACT
; break;
576 case WXK_DIVIDE
: // fall through
577 case WXK_NUMPAD_DIVIDE
: key
= SCK_DIVIDE
; break;
578 case WXK_CONTROL
: key
= 0; break;
579 case WXK_ALT
: key
= 0; break;
580 case WXK_SHIFT
: key
= 0; break;
581 case WXK_MENU
: key
= 0; break;
584 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
593 void ScintillaWX::DoCommand(int ID
) {
598 void ScintillaWX::DoContextMenu(Point pt
) {
599 if (displayPopupMenu
)
603 void ScintillaWX::DoOnListBox() {
604 AutoCompleteCompleted();
607 //----------------------------------------------------------------------
609 #if wxUSE_DRAG_AND_DROP
610 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
611 SetDragPosition(invalidPosition
);
613 // Send an event to allow the drag details to be changed
614 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
615 evt
.SetEventObject(stc
);
616 evt
.SetDragResult(dragResult
);
619 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
620 evt
.SetDragText(data
);
621 stc
->GetEventHandler()->ProcessEvent(evt
);
623 dragResult
= evt
.GetDragResult();
624 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
625 DropAt(evt
.GetPosition(),
626 wx2stc(evt
.GetDragText()),
627 dragResult
== wxDragMove
,
628 FALSE
); // TODO: rectangular?
635 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
641 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
642 SetDragPosition(PositionFromLocation(Point(x
, y
)));
644 // Send an event to allow the drag result to be changed
645 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
646 evt
.SetEventObject(stc
);
647 evt
.SetDragResult(def
);
650 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
651 stc
->GetEventHandler()->ProcessEvent(evt
);
653 dragResult
= evt
.GetDragResult();
658 void ScintillaWX::DoDragLeave() {
659 SetDragPosition(invalidPosition
);
662 //----------------------------------------------------------------------
664 // Redraw all of text area. This paint will not be abandoned.
665 void ScintillaWX::FullPaint() {
666 paintState
= painting
;
667 rcPaint
= GetTextRectangle();
668 paintingAllText
= true;
670 Surface
* surfaceWindow
= Surface::Allocate();
671 surfaceWindow
->Init(&dc
);
672 Paint(surfaceWindow
, rcPaint
);
673 delete surfaceWindow
;
675 // stc->Refresh(FALSE);
677 paintState
= notPainting
;
681 void ScintillaWX::DoScrollToLine(int line
) {
686 void ScintillaWX::DoScrollToColumn(int column
) {
687 HorizontalScrollTo(column
* vs
.spaceWidth
);
692 //----------------------------------------------------------------------
693 //----------------------------------------------------------------------