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 /////////////////////////////////////////////////////////////////////////////
19 #include "ScintillaWX.h"
20 #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 class wxSTCCallTip
: public wxWindow
{
68 wxSTCCallTip(wxWindow
* parent
, int ID
, CallTip
* ct
)
69 : wxWindow(parent
, ID
)
74 void OnPaint(wxPaintEvent
& evt
) {
76 Surface surfaceWindow
;
77 surfaceWindow
.Init(&dc
);
78 m_ct
->PaintCT(&surfaceWindow
);
79 surfaceWindow
.Release();
86 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxWindow
)
87 EVT_PAINT(wxSTCCallTip::OnPaint
)
90 //----------------------------------------------------------------------
91 // Constructor/Destructor
94 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
95 capturedMouse
= false;
103 ScintillaWX::~ScintillaWX() {
107 //----------------------------------------------------------------------
108 // base class virtuals
111 void ScintillaWX::Initialise() {
112 //ScintillaBase::Initialise();
113 #if wxUSE_DRAG_AND_DROP
114 dropTarget
= new wxSTCDropTarget
;
115 dropTarget
->SetScintilla(this);
116 stc
->SetDropTarget(dropTarget
);
121 void ScintillaWX::Finalise() {
122 ScintillaBase::Finalise();
126 void ScintillaWX::StartDrag() {
127 #if wxUSE_DRAG_AND_DROP
128 wxString
dragText(drag
.s
, drag
.len
);
130 // Send an event to allow the drag text to be changed
131 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
132 evt
.SetEventObject(stc
);
133 evt
.SetDragText(dragText
);
134 evt
.SetDragAllowMove(TRUE
);
135 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
136 stc
->GetSelectionEnd()));
137 stc
->GetEventHandler()->ProcessEvent(evt
);
138 dragText
= evt
.GetDragText();
140 if (dragText
.Length()) {
141 wxDropSource
source(stc
);
142 wxTextDataObject
data(dragText
);
145 source
.SetData(data
);
146 dropWentOutside
= TRUE
;
147 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
148 if (result
== wxDragMove
&& dropWentOutside
)
151 SetDragPosition(invalidPosition
);
157 void ScintillaWX::SetTicking(bool on
) {
158 wxSTCTimer
* steTimer
;
159 if (timer
.ticking
!= on
) {
162 steTimer
= new wxSTCTimer(this);
163 steTimer
->Start(timer
.tickSize
);
164 timer
.tickerID
= (int)steTimer
;
166 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
172 timer
.ticksToWait
= caret
.period
;
176 void ScintillaWX::SetMouseCapture(bool on
) {
177 if (on
&& !capturedMouse
)
179 else if (!on
&& capturedMouse
)
185 bool ScintillaWX::HaveMouseCapture() {
186 return capturedMouse
;
190 void ScintillaWX::ScrollText(int linesToMove
) {
191 int dy
= vs
.lineHeight
* (linesToMove
);
192 stc
->ScrollWindow(0, dy
);
196 void ScintillaWX::SetVerticalScrollPos() {
197 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
198 stc
->SetScrollPos(wxVERTICAL
, topLine
);
200 else { // otherwise use the one that's been given to us
201 stc
->m_vScrollBar
->SetThumbPosition(topLine
);
205 void ScintillaWX::SetHorizontalScrollPos() {
206 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
207 stc
->SetScrollPos(wxHORIZONTAL
, xOffset
);
209 else { // otherwise use the one that's been given to us
210 stc
->m_hScrollBar
->SetThumbPosition(xOffset
);
215 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
216 bool modified
= false;
218 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
219 int sbMax
= stc
->GetScrollRange(wxVERTICAL
);
220 int sbThumb
= stc
->GetScrollThumb(wxVERTICAL
);
221 int sbPos
= stc
->GetScrollPos(wxVERTICAL
);
222 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
223 stc
->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
227 else { // otherwise use the one that's been given to us
228 int sbMax
= stc
->m_vScrollBar
->GetRange();
229 int sbPage
= stc
->m_vScrollBar
->GetPageSize();
230 int sbPos
= stc
->m_vScrollBar
->GetThumbPosition();
231 if (sbMax
!= nMax
|| sbPage
!= nPage
) {
232 stc
->m_vScrollBar
->SetScrollbar(sbPos
, nPage
, nMax
, nPage
);
238 if (horizontalScrollBarVisible
) {
239 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
240 int sbMax
= stc
->GetScrollRange(wxHORIZONTAL
);
241 int sbThumb
= stc
->GetScrollThumb(wxHORIZONTAL
);
242 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
243 stc
->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
247 else { // otherwise use the one that's been given to us
248 int sbMax
= stc
->m_hScrollBar
->GetRange();
249 int sbPage
= stc
->m_hScrollBar
->GetPageSize();
250 if ((sbMax
!= H_SCROLL_MAX
) || (sbPage
!= H_SCROLL_STEP
)) {
251 stc
->m_hScrollBar
->SetScrollbar(0, H_SCROLL_STEP
, H_SCROLL_MAX
, H_SCROLL_STEP
);
260 void ScintillaWX::NotifyChange() {
265 void ScintillaWX::NotifyParent(SCNotification scn
) {
266 stc
->NotifyParent(&scn
);
271 void ScintillaWX::Copy() {
272 if (currentPos
!= anchor
) {
274 CopySelectionRange(&st
);
275 wxTheClipboard
->Open();
276 wxTheClipboard
->SetData(new wxTextDataObject(wxString(st
.s
, st
.len
)));
277 wxTheClipboard
->Close();
282 void ScintillaWX::Paste() {
283 pdoc
->BeginUndoAction();
286 wxTextDataObject data
;
289 wxTheClipboard
->Open();
290 gotData
= wxTheClipboard
->GetData(data
);
291 wxTheClipboard
->Close();
293 wxString str
= data
.GetText();
294 int len
= str
.Length();
295 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
296 SetEmptySelection(currentPos
+ len
);
299 pdoc
->EndUndoAction();
305 bool ScintillaWX::CanPaste() {
308 wxTheClipboard
->Open();
309 canPaste
= wxTheClipboard
->IsSupported( wxDF_TEXT
);
310 wxTheClipboard
->Close();
315 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
316 ct
.wCallTip
= new wxSTCCallTip(stc
, -1, &ct
);
317 ct
.wDraw
= ct
.wCallTip
;
321 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
323 popup
.GetID()->AppendSeparator();
325 popup
.GetID()->Append(cmd
, label
);
328 popup
.GetID()->Enable(cmd
, enabled
);
332 void ScintillaWX::ClaimSelection() {
337 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
341 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
342 // switch (iMessage) {
344 // return CanPaste();
346 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
353 //----------------------------------------------------------------------
356 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
358 paintState
= painting
;
359 Surface surfaceWindow
;
360 surfaceWindow
.Init(dc
);
361 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
363 Paint(&surfaceWindow
, rcPaint
);
365 surfaceWindow
.Release();
366 if (paintState
== paintAbandoned
) {
367 // Painting area was insufficient to cover new styling or brace highlight positions
370 paintState
= notPainting
;
372 // On wxGTK the editor window paints can overwrite the listbox...
374 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
379 void ScintillaWX::DoHScroll(int type
, int pos
) {
381 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
382 xPos
-= H_SCROLL_STEP
;
383 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
384 xPos
+= H_SCROLL_STEP
;
385 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
386 xPos
-= H_SCROLL_PAGE
;
387 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
388 xPos
+= H_SCROLL_PAGE
;
389 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
391 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
393 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
396 HorizontalScrollTo(xPos
);
399 void ScintillaWX::DoVScroll(int type
, int pos
) {
400 int topLineNew
= topLine
;
401 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
403 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
405 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
406 topLineNew
-= LinesToScroll();
407 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
408 topLineNew
+= LinesToScroll();
409 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
411 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
412 topLineNew
= MaxScrollPos();
413 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
416 ScrollTo(topLineNew
);
420 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
421 int topLineNew
= topLine
;
424 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
426 KeyCommand(SCI_ZOOMIN
);
429 KeyCommand(SCI_ZOOMOUT
);
432 else { // otherwise just scroll the window
433 wheelRotation
+= rotation
;
434 lines
= wheelRotation
/ delta
;
435 wheelRotation
-= lines
* delta
;
437 lines
*= linesPerAction
;
439 ScrollTo(topLineNew
);
445 void ScintillaWX::DoSize(int width
, int height
) {
446 PRectangle
rcClient(0,0,width
,height
);
447 SetScrollBarsTo(rcClient
);
451 void ScintillaWX::DoLoseFocus(){
452 SetFocusState(false);
455 void ScintillaWX::DoGainFocus(){
459 void ScintillaWX::DoSysColourChange() {
460 InvalidateStyleData();
463 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
464 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
467 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
468 ButtonUp(pt
, curTime
, ctrl
);
471 void ScintillaWX::DoButtonMove(Point pt
) {
476 void ScintillaWX::DoAddChar(char ch
) {
480 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
482 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
483 if (ctrl
&& key
>= 1 && key
<= 26)
488 case WXK_DOWN
: key
= SCK_DOWN
; break;
489 case WXK_UP
: key
= SCK_UP
; break;
490 case WXK_LEFT
: key
= SCK_LEFT
; break;
491 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
492 case WXK_HOME
: key
= SCK_HOME
; break;
493 case WXK_END
: key
= SCK_END
; break;
494 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
495 case WXK_NEXT
: key
= SCK_NEXT
; break;
496 case WXK_DELETE
: key
= SCK_DELETE
; break;
497 case WXK_INSERT
: key
= SCK_INSERT
; break;
498 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
499 case WXK_BACK
: key
= SCK_BACK
; break;
500 case WXK_TAB
: key
= SCK_TAB
; break;
501 case WXK_RETURN
: key
= SCK_RETURN
; break;
504 key
= SCK_ADD
; break;
506 case WXK_NUMPAD_SUBTRACT
:
507 key
= SCK_SUBTRACT
; break;
509 case WXK_NUMPAD_DIVIDE
:
510 key
= SCK_DIVIDE
; break;
511 case WXK_CONTROL
: key
= 0; break;
512 case WXK_ALT
: key
= 0; break;
513 case WXK_SHIFT
: key
= 0; break;
514 case WXK_MENU
: key
= 0; break;
517 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
526 void ScintillaWX::DoCommand(int ID
) {
531 void ScintillaWX::DoContextMenu(Point pt
) {
535 void ScintillaWX::DoOnListBox() {
536 AutoCompleteCompleted();
539 //----------------------------------------------------------------------
541 #if wxUSE_DRAG_AND_DROP
542 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
543 SetDragPosition(invalidPosition
);
545 // Send an event to allow the drag details to be changed
546 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
547 evt
.SetEventObject(stc
);
548 evt
.SetDragResult(dragResult
);
551 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
552 evt
.SetDragText(data
);
553 stc
->GetEventHandler()->ProcessEvent(evt
);
555 dragResult
= evt
.GetDragResult();
556 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
557 DropAt(evt
.GetPosition(),
559 dragResult
== wxDragMove
,
560 FALSE
); // TODO: rectangular?
567 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
573 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
574 SetDragPosition(PositionFromLocation(Point(x
, y
)));
576 // Send an event to allow the drag result to be changed
577 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
578 evt
.SetEventObject(stc
);
579 evt
.SetDragResult(def
);
582 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
583 stc
->GetEventHandler()->ProcessEvent(evt
);
585 dragResult
= evt
.GetDragResult();
590 void ScintillaWX::DoDragLeave() {
591 SetDragPosition(invalidPosition
);
594 //----------------------------------------------------------------------
596 // Redraw all of text area. This paint will not be abandoned.
597 void ScintillaWX::FullPaint() {
598 paintState
= painting
;
599 rcPaint
= GetTextRectangle();
600 paintingAllText
= true;
602 Surface surfaceWindow
;
603 surfaceWindow
.Init(&dc
);
604 Paint(&surfaceWindow
, rcPaint
);
605 surfaceWindow
.Release();
607 // stc->Refresh(FALSE);
609 paintState
= notPainting
;
613 void ScintillaWX::DoScrollToLine(int line
) {
618 void ScintillaWX::DoScrollToColumn(int column
) {
619 HorizontalScrollTo(column
* vs
.spaceWidth
);
624 //----------------------------------------------------------------------
625 //----------------------------------------------------------------------