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 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
92 virtual void DoSetSize(int x
, int y
,
93 int width
, int height
,
94 int sizeFlags
= wxSIZE_AUTO
) {
96 GetParent()->ClientToScreen(&x
, NULL
);
98 GetParent()->ClientToScreen(NULL
, &y
);
99 wxSTCCallTipBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
102 virtual bool Show( bool show
= TRUE
) {
103 bool retval
= wxSTCCallTipBase::Show(show
);
113 void OnLeftDown(wxMouseEvent
& ) {
120 DECLARE_EVENT_TABLE()
123 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxSTCCallTipBase
)
124 EVT_PAINT(wxSTCCallTip::OnPaint
)
125 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
126 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown
)
131 //----------------------------------------------------------------------
132 // Constructor/Destructor
135 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
136 capturedMouse
= false;
144 ScintillaWX::~ScintillaWX() {
148 //----------------------------------------------------------------------
149 // base class virtuals
152 void ScintillaWX::Initialise() {
153 //ScintillaBase::Initialise();
154 #if wxUSE_DRAG_AND_DROP
155 dropTarget
= new wxSTCDropTarget
;
156 dropTarget
->SetScintilla(this);
157 stc
->SetDropTarget(dropTarget
);
162 void ScintillaWX::Finalise() {
163 ScintillaBase::Finalise();
167 void ScintillaWX::StartDrag() {
168 #if wxUSE_DRAG_AND_DROP
169 wxString
dragText(drag
.s
, wxConvUTF8
, drag
.len
);
171 // Send an event to allow the drag text to be changed
172 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
173 evt
.SetEventObject(stc
);
174 evt
.SetDragText(dragText
);
175 evt
.SetDragAllowMove(TRUE
);
176 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
177 stc
->GetSelectionEnd()));
178 stc
->GetEventHandler()->ProcessEvent(evt
);
179 dragText
= evt
.GetDragText();
181 if (dragText
.Length()) {
182 wxDropSource
source(stc
);
183 wxTextDataObject
data(dragText
);
186 source
.SetData(data
);
187 dropWentOutside
= TRUE
;
188 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
189 if (result
== wxDragMove
&& dropWentOutside
)
192 SetDragPosition(invalidPosition
);
198 void ScintillaWX::SetTicking(bool on
) {
199 wxSTCTimer
* steTimer
;
200 if (timer
.ticking
!= on
) {
203 steTimer
= new wxSTCTimer(this);
204 steTimer
->Start(timer
.tickSize
);
205 timer
.tickerID
= steTimer
;
207 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
213 timer
.ticksToWait
= caret
.period
;
217 void ScintillaWX::SetMouseCapture(bool on
) {
218 if (on
&& !capturedMouse
)
220 else if (!on
&& capturedMouse
)
226 bool ScintillaWX::HaveMouseCapture() {
227 return capturedMouse
;
231 void ScintillaWX::ScrollText(int linesToMove
) {
232 int dy
= vs
.lineHeight
* (linesToMove
);
233 stc
->ScrollWindow(0, dy
);
237 void ScintillaWX::SetVerticalScrollPos() {
238 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
239 stc
->SetScrollPos(wxVERTICAL
, topLine
);
241 else { // otherwise use the one that's been given to us
242 stc
->m_vScrollBar
->SetThumbPosition(topLine
);
246 void ScintillaWX::SetHorizontalScrollPos() {
247 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
248 stc
->SetScrollPos(wxHORIZONTAL
, xOffset
);
250 else { // otherwise use the one that's been given to us
251 stc
->m_hScrollBar
->SetThumbPosition(xOffset
);
256 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
257 bool modified
= false;
259 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
260 int sbMax
= stc
->GetScrollRange(wxVERTICAL
);
261 int sbThumb
= stc
->GetScrollThumb(wxVERTICAL
);
262 int sbPos
= stc
->GetScrollPos(wxVERTICAL
);
263 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
264 stc
->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
268 else { // otherwise use the one that's been given to us
269 int sbMax
= stc
->m_vScrollBar
->GetRange();
270 int sbPage
= stc
->m_vScrollBar
->GetPageSize();
271 int sbPos
= stc
->m_vScrollBar
->GetThumbPosition();
272 if (sbMax
!= nMax
|| sbPage
!= nPage
) {
273 stc
->m_vScrollBar
->SetScrollbar(sbPos
, nPage
, nMax
, nPage
);
279 if (horizontalScrollBarVisible
) {
280 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
281 int sbMax
= stc
->GetScrollRange(wxHORIZONTAL
);
282 int sbThumb
= stc
->GetScrollThumb(wxHORIZONTAL
);
283 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
284 stc
->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
288 else { // otherwise use the one that's been given to us
289 int sbMax
= stc
->m_hScrollBar
->GetRange();
290 int sbPage
= stc
->m_hScrollBar
->GetPageSize();
291 if ((sbMax
!= H_SCROLL_MAX
) || (sbPage
!= H_SCROLL_STEP
)) {
292 stc
->m_hScrollBar
->SetScrollbar(0, H_SCROLL_STEP
, H_SCROLL_MAX
, H_SCROLL_STEP
);
301 void ScintillaWX::NotifyChange() {
306 void ScintillaWX::NotifyParent(SCNotification scn
) {
307 stc
->NotifyParent(&scn
);
312 void ScintillaWX::Copy() {
313 if (currentPos
!= anchor
) {
315 CopySelectionRange(&st
);
316 wxTheClipboard
->Open();
317 wxString
text(st
.s
, wxConvUTF8
, st
.len
);
318 wxTheClipboard
->SetData(new wxTextDataObject(text
));
319 wxTheClipboard
->Close();
324 void ScintillaWX::Paste() {
325 pdoc
->BeginUndoAction();
328 wxTextDataObject data
;
331 wxTheClipboard
->Open();
332 gotData
= wxTheClipboard
->GetData(data
);
333 wxTheClipboard
->Close();
335 wxWX2MBbuf buf
= (wxWX2MBbuf
)data
.GetText().mb_str(wxConvUTF8
);
336 int len
= strlen(buf
);
337 pdoc
->InsertString(currentPos
, buf
, len
);
338 SetEmptySelection(currentPos
+ len
);
341 pdoc
->EndUndoAction();
347 bool ScintillaWX::CanPaste() {
350 wxTheClipboard
->Open();
351 canPaste
= wxTheClipboard
->IsSupported(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
);
352 wxTheClipboard
->Close();
357 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
358 ct
.wCallTip
= new wxSTCCallTip(stc
, &ct
);
359 ct
.wDraw
= ct
.wCallTip
;
363 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
365 ((wxMenu
*)popup
.GetID())->AppendSeparator();
367 ((wxMenu
*)popup
.GetID())->Append(cmd
, wxString(label
, *wxConvCurrent
));
370 ((wxMenu
*)popup
.GetID())->Enable(cmd
, enabled
);
374 void ScintillaWX::ClaimSelection() {
379 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
383 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
384 // switch (iMessage) {
386 // return CanPaste();
388 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
395 //----------------------------------------------------------------------
398 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
400 paintState
= painting
;
401 Surface
* surfaceWindow
= Surface::Allocate();
402 surfaceWindow
->Init(dc
);
403 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
405 Paint(surfaceWindow
, rcPaint
);
407 delete surfaceWindow
;
408 if (paintState
== paintAbandoned
) {
409 // Painting area was insufficient to cover new styling or brace highlight positions
412 paintState
= notPainting
;
414 // On wxGTK the editor window paints can overwrite the listbox...
416 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
421 void ScintillaWX::DoHScroll(int type
, int pos
) {
423 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
424 xPos
-= H_SCROLL_STEP
;
425 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
426 xPos
+= H_SCROLL_STEP
;
427 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
428 xPos
-= H_SCROLL_PAGE
;
429 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
430 xPos
+= H_SCROLL_PAGE
;
431 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
433 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
435 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
438 HorizontalScrollTo(xPos
);
441 void ScintillaWX::DoVScroll(int type
, int pos
) {
442 int topLineNew
= topLine
;
443 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
445 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
447 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
448 topLineNew
-= LinesToScroll();
449 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
450 topLineNew
+= LinesToScroll();
451 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
453 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
454 topLineNew
= MaxScrollPos();
455 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
458 ScrollTo(topLineNew
);
462 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
463 int topLineNew
= topLine
;
466 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
468 KeyCommand(SCI_ZOOMIN
);
471 KeyCommand(SCI_ZOOMOUT
);
474 else { // otherwise just scroll the window
475 wheelRotation
+= rotation
;
476 lines
= wheelRotation
/ delta
;
477 wheelRotation
-= lines
* delta
;
479 lines
*= linesPerAction
;
481 ScrollTo(topLineNew
);
487 void ScintillaWX::DoSize(int width
, int height
) {
488 // PRectangle rcClient(0,0,width,height);
489 // SetScrollBarsTo(rcClient);
494 void ScintillaWX::DoLoseFocus(){
495 SetFocusState(false);
498 void ScintillaWX::DoGainFocus(){
502 void ScintillaWX::DoSysColourChange() {
503 InvalidateStyleData();
506 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
507 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
510 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
511 ButtonUp(pt
, curTime
, ctrl
);
514 void ScintillaWX::DoButtonMove(Point pt
) {
519 void ScintillaWX::DoAddChar(int key
) {
523 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
525 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
526 if (ctrl
&& key
>= 1 && key
<= 26)
531 case WXK_DOWN
: key
= SCK_DOWN
; break;
532 case WXK_UP
: key
= SCK_UP
; break;
533 case WXK_LEFT
: key
= SCK_LEFT
; break;
534 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
535 case WXK_HOME
: key
= SCK_HOME
; break;
536 case WXK_END
: key
= SCK_END
; break;
537 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
538 case WXK_NEXT
: key
= SCK_NEXT
; break;
539 case WXK_DELETE
: key
= SCK_DELETE
; break;
540 case WXK_INSERT
: key
= SCK_INSERT
; break;
541 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
542 case WXK_BACK
: key
= SCK_BACK
; break;
543 case WXK_TAB
: key
= SCK_TAB
; break;
544 case WXK_RETURN
: key
= SCK_RETURN
; break;
545 case WXK_ADD
: // fall through
546 case WXK_NUMPAD_ADD
: key
= SCK_ADD
; break;
547 case WXK_SUBTRACT
: // fall through
548 case WXK_NUMPAD_SUBTRACT
: key
= SCK_SUBTRACT
; break;
549 case WXK_DIVIDE
: // fall through
550 case WXK_NUMPAD_DIVIDE
: key
= SCK_DIVIDE
; break;
551 case WXK_CONTROL
: key
= 0; break;
552 case WXK_ALT
: key
= 0; break;
553 case WXK_SHIFT
: key
= 0; break;
554 case WXK_MENU
: key
= 0; break;
557 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
566 void ScintillaWX::DoCommand(int ID
) {
571 void ScintillaWX::DoContextMenu(Point pt
) {
575 void ScintillaWX::DoOnListBox() {
576 AutoCompleteCompleted();
579 //----------------------------------------------------------------------
581 #if wxUSE_DRAG_AND_DROP
582 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
583 SetDragPosition(invalidPosition
);
585 // Send an event to allow the drag details to be changed
586 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
587 evt
.SetEventObject(stc
);
588 evt
.SetDragResult(dragResult
);
591 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
592 evt
.SetDragText(data
);
593 stc
->GetEventHandler()->ProcessEvent(evt
);
595 dragResult
= evt
.GetDragResult();
596 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
597 DropAt(evt
.GetPosition(),
598 evt
.GetDragText().mb_str(wxConvUTF8
),
599 dragResult
== wxDragMove
,
600 FALSE
); // TODO: rectangular?
607 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
613 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
614 SetDragPosition(PositionFromLocation(Point(x
, y
)));
616 // Send an event to allow the drag result to be changed
617 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
618 evt
.SetEventObject(stc
);
619 evt
.SetDragResult(def
);
622 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
623 stc
->GetEventHandler()->ProcessEvent(evt
);
625 dragResult
= evt
.GetDragResult();
630 void ScintillaWX::DoDragLeave() {
631 SetDragPosition(invalidPosition
);
634 //----------------------------------------------------------------------
636 // Redraw all of text area. This paint will not be abandoned.
637 void ScintillaWX::FullPaint() {
638 paintState
= painting
;
639 rcPaint
= GetTextRectangle();
640 paintingAllText
= true;
642 Surface
* surfaceWindow
= Surface::Allocate();
643 surfaceWindow
->Init(&dc
);
644 Paint(surfaceWindow
, rcPaint
);
645 delete surfaceWindow
;
647 // stc->Refresh(FALSE);
649 paintState
= notPainting
;
653 void ScintillaWX::DoScrollToLine(int line
) {
658 void ScintillaWX::DoScrollToColumn(int column
) {
659 HorizontalScrollTo(column
* vs
.spaceWidth
);
664 //----------------------------------------------------------------------
665 //----------------------------------------------------------------------