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 #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
);
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
);
105 DECLARE_EVENT_TABLE()
108 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxSTCCallTipBase
)
109 EVT_PAINT(wxSTCCallTip::OnPaint
)
113 //----------------------------------------------------------------------
114 // Constructor/Destructor
117 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
118 capturedMouse
= false;
126 ScintillaWX::~ScintillaWX() {
130 //----------------------------------------------------------------------
131 // base class virtuals
134 void ScintillaWX::Initialise() {
135 //ScintillaBase::Initialise();
136 #if wxUSE_DRAG_AND_DROP
137 dropTarget
= new wxSTCDropTarget
;
138 dropTarget
->SetScintilla(this);
139 stc
->SetDropTarget(dropTarget
);
144 void ScintillaWX::Finalise() {
145 ScintillaBase::Finalise();
149 void ScintillaWX::StartDrag() {
150 #if wxUSE_DRAG_AND_DROP
151 wxString
dragText(drag
.s
, wxConvUTF8
, drag
.len
);
153 // Send an event to allow the drag text to be changed
154 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
155 evt
.SetEventObject(stc
);
156 evt
.SetDragText(dragText
);
157 evt
.SetDragAllowMove(TRUE
);
158 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
159 stc
->GetSelectionEnd()));
160 stc
->GetEventHandler()->ProcessEvent(evt
);
161 dragText
= evt
.GetDragText();
163 if (dragText
.Length()) {
164 wxDropSource
source(stc
);
165 wxTextDataObject
data(dragText
);
168 source
.SetData(data
);
169 dropWentOutside
= TRUE
;
170 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
171 if (result
== wxDragMove
&& dropWentOutside
)
174 SetDragPosition(invalidPosition
);
180 void ScintillaWX::SetTicking(bool on
) {
181 wxSTCTimer
* steTimer
;
182 if (timer
.ticking
!= on
) {
185 steTimer
= new wxSTCTimer(this);
186 steTimer
->Start(timer
.tickSize
);
187 timer
.tickerID
= steTimer
;
189 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
195 timer
.ticksToWait
= caret
.period
;
199 void ScintillaWX::SetMouseCapture(bool on
) {
200 if (on
&& !capturedMouse
)
202 else if (!on
&& capturedMouse
)
208 bool ScintillaWX::HaveMouseCapture() {
209 return capturedMouse
;
213 void ScintillaWX::ScrollText(int linesToMove
) {
214 int dy
= vs
.lineHeight
* (linesToMove
);
215 stc
->ScrollWindow(0, dy
);
219 void ScintillaWX::SetVerticalScrollPos() {
220 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
221 stc
->SetScrollPos(wxVERTICAL
, topLine
);
223 else { // otherwise use the one that's been given to us
224 stc
->m_vScrollBar
->SetThumbPosition(topLine
);
228 void ScintillaWX::SetHorizontalScrollPos() {
229 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
230 stc
->SetScrollPos(wxHORIZONTAL
, xOffset
);
232 else { // otherwise use the one that's been given to us
233 stc
->m_hScrollBar
->SetThumbPosition(xOffset
);
238 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
239 bool modified
= false;
241 if (stc
->m_vScrollBar
== NULL
) { // Use built-in scrollbar
242 int sbMax
= stc
->GetScrollRange(wxVERTICAL
);
243 int sbThumb
= stc
->GetScrollThumb(wxVERTICAL
);
244 int sbPos
= stc
->GetScrollPos(wxVERTICAL
);
245 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
246 stc
->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
250 else { // otherwise use the one that's been given to us
251 int sbMax
= stc
->m_vScrollBar
->GetRange();
252 int sbPage
= stc
->m_vScrollBar
->GetPageSize();
253 int sbPos
= stc
->m_vScrollBar
->GetThumbPosition();
254 if (sbMax
!= nMax
|| sbPage
!= nPage
) {
255 stc
->m_vScrollBar
->SetScrollbar(sbPos
, nPage
, nMax
, nPage
);
261 if (horizontalScrollBarVisible
) {
262 if (stc
->m_hScrollBar
== NULL
) { // Use built-in scrollbar
263 int sbMax
= stc
->GetScrollRange(wxHORIZONTAL
);
264 int sbThumb
= stc
->GetScrollThumb(wxHORIZONTAL
);
265 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
266 stc
->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
270 else { // otherwise use the one that's been given to us
271 int sbMax
= stc
->m_hScrollBar
->GetRange();
272 int sbPage
= stc
->m_hScrollBar
->GetPageSize();
273 if ((sbMax
!= H_SCROLL_MAX
) || (sbPage
!= H_SCROLL_STEP
)) {
274 stc
->m_hScrollBar
->SetScrollbar(0, H_SCROLL_STEP
, H_SCROLL_MAX
, H_SCROLL_STEP
);
283 void ScintillaWX::NotifyChange() {
288 void ScintillaWX::NotifyParent(SCNotification scn
) {
289 stc
->NotifyParent(&scn
);
294 void ScintillaWX::Copy() {
295 if (currentPos
!= anchor
) {
297 CopySelectionRange(&st
);
298 wxTheClipboard
->Open();
299 wxString
text(st
.s
, wxConvUTF8
, st
.len
);
300 wxTheClipboard
->SetData(new wxTextDataObject(text
));
301 wxTheClipboard
->Close();
306 void ScintillaWX::Paste() {
307 pdoc
->BeginUndoAction();
310 wxTextDataObject data
;
313 wxTheClipboard
->Open();
314 gotData
= wxTheClipboard
->GetData(data
);
315 wxTheClipboard
->Close();
317 wxWX2MBbuf buf
= (wxWX2MBbuf
)data
.GetText().mb_str(wxConvUTF8
);
318 int len
= strlen(buf
);
319 pdoc
->InsertString(currentPos
, buf
, len
);
320 SetEmptySelection(currentPos
+ len
);
323 pdoc
->EndUndoAction();
329 bool ScintillaWX::CanPaste() {
332 wxTheClipboard
->Open();
333 canPaste
= wxTheClipboard
->IsSupported(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
);
334 wxTheClipboard
->Close();
339 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
340 ct
.wCallTip
= new wxSTCCallTip(stc
, &ct
);
341 ct
.wDraw
= ct
.wCallTip
;
345 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
347 ((wxMenu
*)popup
.GetID())->AppendSeparator();
349 ((wxMenu
*)popup
.GetID())->Append(cmd
, wxString(label
, *wxConvCurrent
));
352 ((wxMenu
*)popup
.GetID())->Enable(cmd
, enabled
);
356 void ScintillaWX::ClaimSelection() {
361 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
365 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
366 // switch (iMessage) {
368 // return CanPaste();
370 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
377 //----------------------------------------------------------------------
380 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
382 paintState
= painting
;
383 Surface
* surfaceWindow
= Surface::Allocate();
384 surfaceWindow
->Init(dc
);
385 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
387 Paint(surfaceWindow
, rcPaint
);
389 delete surfaceWindow
;
390 if (paintState
== paintAbandoned
) {
391 // Painting area was insufficient to cover new styling or brace highlight positions
394 paintState
= notPainting
;
396 // On wxGTK the editor window paints can overwrite the listbox...
398 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
403 void ScintillaWX::DoHScroll(int type
, int pos
) {
405 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
406 xPos
-= H_SCROLL_STEP
;
407 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
408 xPos
+= H_SCROLL_STEP
;
409 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
410 xPos
-= H_SCROLL_PAGE
;
411 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
412 xPos
+= H_SCROLL_PAGE
;
413 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
415 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
417 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
420 HorizontalScrollTo(xPos
);
423 void ScintillaWX::DoVScroll(int type
, int pos
) {
424 int topLineNew
= topLine
;
425 if (type
== wxEVT_SCROLLWIN_LINEUP
|| type
== wxEVT_SCROLL_LINEUP
)
427 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
|| type
== wxEVT_SCROLL_LINEDOWN
)
429 else if (type
== wxEVT_SCROLLWIN_PAGEUP
|| type
== wxEVT_SCROLL_PAGEUP
)
430 topLineNew
-= LinesToScroll();
431 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
|| type
== wxEVT_SCROLL_PAGEDOWN
)
432 topLineNew
+= LinesToScroll();
433 else if (type
== wxEVT_SCROLLWIN_TOP
|| type
== wxEVT_SCROLL_TOP
)
435 else if (type
== wxEVT_SCROLLWIN_BOTTOM
|| type
== wxEVT_SCROLL_BOTTOM
)
436 topLineNew
= MaxScrollPos();
437 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
|| type
== wxEVT_SCROLL_THUMBTRACK
)
440 ScrollTo(topLineNew
);
444 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
445 int topLineNew
= topLine
;
448 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
450 KeyCommand(SCI_ZOOMIN
);
453 KeyCommand(SCI_ZOOMOUT
);
456 else { // otherwise just scroll the window
457 wheelRotation
+= rotation
;
458 lines
= wheelRotation
/ delta
;
459 wheelRotation
-= lines
* delta
;
461 lines
*= linesPerAction
;
463 ScrollTo(topLineNew
);
469 void ScintillaWX::DoSize(int width
, int height
) {
470 // PRectangle rcClient(0,0,width,height);
471 // SetScrollBarsTo(rcClient);
476 void ScintillaWX::DoLoseFocus(){
477 SetFocusState(false);
480 void ScintillaWX::DoGainFocus(){
484 void ScintillaWX::DoSysColourChange() {
485 InvalidateStyleData();
488 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
489 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
492 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
493 ButtonUp(pt
, curTime
, ctrl
);
496 void ScintillaWX::DoButtonMove(Point pt
) {
501 void ScintillaWX::DoAddChar(int key
) {
505 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
507 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
508 if (ctrl
&& key
>= 1 && key
<= 26)
513 case WXK_DOWN
: key
= SCK_DOWN
; break;
514 case WXK_UP
: key
= SCK_UP
; break;
515 case WXK_LEFT
: key
= SCK_LEFT
; break;
516 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
517 case WXK_HOME
: key
= SCK_HOME
; break;
518 case WXK_END
: key
= SCK_END
; break;
519 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
520 case WXK_NEXT
: key
= SCK_NEXT
; break;
521 case WXK_DELETE
: key
= SCK_DELETE
; break;
522 case WXK_INSERT
: key
= SCK_INSERT
; break;
523 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
524 case WXK_BACK
: key
= SCK_BACK
; break;
525 case WXK_TAB
: key
= SCK_TAB
; break;
526 case WXK_RETURN
: key
= SCK_RETURN
; break;
529 key
= SCK_ADD
; break;
531 case WXK_NUMPAD_SUBTRACT
:
532 key
= SCK_SUBTRACT
; break;
534 case WXK_NUMPAD_DIVIDE
:
535 key
= SCK_DIVIDE
; break;
536 case WXK_CONTROL
: key
= 0; break;
537 case WXK_ALT
: key
= 0; break;
538 case WXK_SHIFT
: key
= 0; break;
539 case WXK_MENU
: key
= 0; break;
542 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
551 void ScintillaWX::DoCommand(int ID
) {
556 void ScintillaWX::DoContextMenu(Point pt
) {
560 void ScintillaWX::DoOnListBox() {
561 AutoCompleteCompleted();
564 //----------------------------------------------------------------------
566 #if wxUSE_DRAG_AND_DROP
567 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
568 SetDragPosition(invalidPosition
);
570 // Send an event to allow the drag details to be changed
571 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
572 evt
.SetEventObject(stc
);
573 evt
.SetDragResult(dragResult
);
576 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
577 evt
.SetDragText(data
);
578 stc
->GetEventHandler()->ProcessEvent(evt
);
580 dragResult
= evt
.GetDragResult();
581 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
582 DropAt(evt
.GetPosition(),
583 evt
.GetDragText().mb_str(wxConvUTF8
),
584 dragResult
== wxDragMove
,
585 FALSE
); // TODO: rectangular?
592 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
598 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
599 SetDragPosition(PositionFromLocation(Point(x
, y
)));
601 // Send an event to allow the drag result to be changed
602 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
603 evt
.SetEventObject(stc
);
604 evt
.SetDragResult(def
);
607 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
608 stc
->GetEventHandler()->ProcessEvent(evt
);
610 dragResult
= evt
.GetDragResult();
615 void ScintillaWX::DoDragLeave() {
616 SetDragPosition(invalidPosition
);
619 //----------------------------------------------------------------------
621 // Redraw all of text area. This paint will not be abandoned.
622 void ScintillaWX::FullPaint() {
623 paintState
= painting
;
624 rcPaint
= GetTextRectangle();
625 paintingAllText
= true;
627 Surface
* surfaceWindow
= Surface::Allocate();
628 surfaceWindow
->Init(&dc
);
629 Paint(surfaceWindow
, rcPaint
);
630 delete surfaceWindow
;
632 // stc->Refresh(FALSE);
634 paintState
= notPainting
;
638 void ScintillaWX::DoScrollToLine(int line
) {
643 void ScintillaWX::DoScrollToColumn(int column
) {
644 HorizontalScrollTo(column
* vs
.spaceWidth
);
649 //----------------------------------------------------------------------
650 //----------------------------------------------------------------------