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 wxTheClipboard
->Open();
325 wxTheClipboard
->UsePrimarySelection();
326 wxString text
= stc2wx(st
.s
, st
.len
);
327 wxTheClipboard
->SetData(new wxTextDataObject(text
));
328 wxTheClipboard
->Close();
333 void ScintillaWX::Paste() {
334 pdoc
->BeginUndoAction();
337 wxTextDataObject data
;
340 wxTheClipboard
->Open();
341 wxTheClipboard
->UsePrimarySelection();
342 gotData
= wxTheClipboard
->GetData(data
);
343 wxTheClipboard
->Close();
345 wxWX2MBbuf buf
= (wxWX2MBbuf
)wx2stc(data
.GetText());
346 int len
= strlen(buf
);
347 pdoc
->InsertString(currentPos
, buf
, len
);
348 SetEmptySelection(currentPos
+ len
);
351 pdoc
->EndUndoAction();
357 bool ScintillaWX::CanPaste() {
358 bool canPaste
= FALSE
;
360 if (! wxTheClipboard
->IsOpened()) {
361 wxTheClipboard
->Open();
362 wxTheClipboard
->UsePrimarySelection();
363 canPaste
= wxTheClipboard
->IsSupported(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
);
364 wxTheClipboard
->Close();
369 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
370 ct
.wCallTip
= new wxSTCCallTip(stc
, &ct
);
371 ct
.wDraw
= ct
.wCallTip
;
375 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
377 ((wxMenu
*)popup
.GetID())->AppendSeparator();
379 ((wxMenu
*)popup
.GetID())->Append(cmd
, stc2wx(label
));
382 ((wxMenu
*)popup
.GetID())->Enable(cmd
, enabled
);
386 void ScintillaWX::ClaimSelection() {
391 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
395 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
396 // switch (iMessage) {
398 // return CanPaste();
400 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
407 //----------------------------------------------------------------------
410 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
412 paintState
= painting
;
413 Surface
* surfaceWindow
= Surface::Allocate();
414 surfaceWindow
->Init(dc
);
415 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
417 Paint(surfaceWindow
, rcPaint
);
419 delete surfaceWindow
;
420 if (paintState
== paintAbandoned
) {
421 // Painting area was insufficient to cover new styling or brace highlight positions
424 paintState
= notPainting
;
426 // On wxGTK the editor window paints can overwrite the listbox...
428 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
433 void ScintillaWX::DoHScroll(int type
, int pos
) {
435 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
436 xPos
-= H_SCROLL_STEP
;
437 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
438 xPos
+= H_SCROLL_STEP
;
439 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
440 xPos
-= H_SCROLL_PAGE
;
441 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
442 xPos
+= H_SCROLL_PAGE
;
443 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
445 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
447 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
450 HorizontalScrollTo(xPos
);
453 void ScintillaWX::DoVScroll(int type
, int pos
) {
454 int topLineNew
= topLine
;
455 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
457 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
459 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
460 topLineNew
-= LinesToScroll();
461 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
462 topLineNew
+= LinesToScroll();
463 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
465 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
466 topLineNew
= MaxScrollPos();
467 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
470 ScrollTo(topLineNew
);
473 void ScintillaWX::DoMouseWheel(int rotation
, int delta
,
474 int linesPerAction
, int ctrlDown
,
475 bool isPageScroll
) {
476 int topLineNew
= topLine
;
479 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
481 KeyCommand(SCI_ZOOMIN
);
484 KeyCommand(SCI_ZOOMOUT
);
487 else { // otherwise just scroll the window
488 wheelRotation
+= rotation
;
489 lines
= wheelRotation
/ delta
;
490 wheelRotation
-= lines
* delta
;
493 lines
= lines
* LinesOnScreen(); // lines is either +1 or -1
495 lines
*= linesPerAction
;
497 ScrollTo(topLineNew
);
503 void ScintillaWX::DoSize(int width
, int height
) {
504 // PRectangle rcClient(0,0,width,height);
505 // SetScrollBarsTo(rcClient);
510 void ScintillaWX::DoLoseFocus(){
511 SetFocusState(false);
514 void ScintillaWX::DoGainFocus(){
518 void ScintillaWX::DoSysColourChange() {
519 InvalidateStyleData();
522 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
523 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
526 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
527 ButtonUp(pt
, curTime
, ctrl
);
530 void ScintillaWX::DoButtonMove(Point pt
) {
535 void ScintillaWX::DoAddChar(int key
) {
539 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
540 #if defined(__WXGTK__) || defined(__WXMAC__)
541 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
542 if (ctrl
&& key
>= 1 && key
<= 26)
547 case WXK_DOWN
: key
= SCK_DOWN
; break;
548 case WXK_UP
: key
= SCK_UP
; break;
549 case WXK_LEFT
: key
= SCK_LEFT
; break;
550 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
551 case WXK_HOME
: key
= SCK_HOME
; break;
552 case WXK_END
: key
= SCK_END
; break;
553 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
554 case WXK_NEXT
: key
= SCK_NEXT
; break;
555 case WXK_DELETE
: key
= SCK_DELETE
; break;
556 case WXK_INSERT
: key
= SCK_INSERT
; break;
557 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
558 case WXK_BACK
: key
= SCK_BACK
; break;
559 case WXK_TAB
: key
= SCK_TAB
; break;
560 case WXK_RETURN
: key
= SCK_RETURN
; break;
561 case WXK_ADD
: // fall through
562 case WXK_NUMPAD_ADD
: key
= SCK_ADD
; break;
563 case WXK_SUBTRACT
: // fall through
564 case WXK_NUMPAD_SUBTRACT
: key
= SCK_SUBTRACT
; break;
565 case WXK_DIVIDE
: // fall through
566 case WXK_NUMPAD_DIVIDE
: key
= SCK_DIVIDE
; break;
567 case WXK_CONTROL
: key
= 0; break;
568 case WXK_ALT
: key
= 0; break;
569 case WXK_SHIFT
: key
= 0; break;
570 case WXK_MENU
: key
= 0; break;
573 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
582 void ScintillaWX::DoCommand(int ID
) {
587 void ScintillaWX::DoContextMenu(Point pt
) {
588 if (displayPopupMenu
)
592 void ScintillaWX::DoOnListBox() {
593 AutoCompleteCompleted();
596 //----------------------------------------------------------------------
598 #if wxUSE_DRAG_AND_DROP
599 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
600 SetDragPosition(invalidPosition
);
602 // Send an event to allow the drag details to be changed
603 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
604 evt
.SetEventObject(stc
);
605 evt
.SetDragResult(dragResult
);
608 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
609 evt
.SetDragText(data
);
610 stc
->GetEventHandler()->ProcessEvent(evt
);
612 dragResult
= evt
.GetDragResult();
613 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
614 DropAt(evt
.GetPosition(),
615 wx2stc(evt
.GetDragText()),
616 dragResult
== wxDragMove
,
617 FALSE
); // TODO: rectangular?
624 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
630 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
631 SetDragPosition(PositionFromLocation(Point(x
, y
)));
633 // Send an event to allow the drag result to be changed
634 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
635 evt
.SetEventObject(stc
);
636 evt
.SetDragResult(def
);
639 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
640 stc
->GetEventHandler()->ProcessEvent(evt
);
642 dragResult
= evt
.GetDragResult();
647 void ScintillaWX::DoDragLeave() {
648 SetDragPosition(invalidPosition
);
651 //----------------------------------------------------------------------
653 // Redraw all of text area. This paint will not be abandoned.
654 void ScintillaWX::FullPaint() {
655 paintState
= painting
;
656 rcPaint
= GetTextRectangle();
657 paintingAllText
= true;
659 Surface
* surfaceWindow
= Surface::Allocate();
660 surfaceWindow
->Init(&dc
);
661 Paint(surfaceWindow
, rcPaint
);
662 delete surfaceWindow
;
664 // stc->Refresh(FALSE);
666 paintState
= notPainting
;
670 void ScintillaWX::DoScrollToLine(int line
) {
675 void ScintillaWX::DoScrollToColumn(int column
) {
676 HorizontalScrollTo(column
* vs
.spaceWidth
);
681 //----------------------------------------------------------------------
682 //----------------------------------------------------------------------