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
= 2000;
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(wMain
.GetID());
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
)
178 wMain
.GetID()->CaptureMouse();
179 else if (!on
&& capturedMouse
)
180 wMain
.GetID()->ReleaseMouse();
185 bool ScintillaWX::HaveMouseCapture() {
186 return capturedMouse
;
190 void ScintillaWX::ScrollText(int linesToMove
) {
191 int dy
= vs
.lineHeight
* (linesToMove
);
192 wMain
.GetID()->ScrollWindow(0, dy
);
193 wMain
.GetID()->Update();
196 void ScintillaWX::SetVerticalScrollPos() {
197 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
200 void ScintillaWX::SetHorizontalScrollPos() {
201 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
205 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
206 bool modified
= false;
207 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
208 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
209 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
212 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
213 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
217 if (horizontalScrollBarVisible
) {
218 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
219 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
220 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
221 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
229 void ScintillaWX::NotifyChange() {
234 void ScintillaWX::NotifyParent(SCNotification scn
) {
235 stc
->NotifyParent(&scn
);
240 void ScintillaWX::Copy() {
241 if (currentPos
!= anchor
) {
243 CopySelectionRange(&st
);
244 wxTheClipboard
->Open();
245 wxTheClipboard
->SetData(new wxTextDataObject(wxString(st
.s
, st
.len
)));
246 wxTheClipboard
->Close();
251 void ScintillaWX::Paste() {
252 pdoc
->BeginUndoAction();
255 wxTextDataObject data
;
258 wxTheClipboard
->Open();
259 gotData
= wxTheClipboard
->GetData(data
);
260 wxTheClipboard
->Close();
262 wxString str
= data
.GetText();
263 int len
= str
.Length();
264 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
265 SetEmptySelection(currentPos
+ len
);
268 pdoc
->EndUndoAction();
274 bool ScintillaWX::CanPaste() {
277 wxTheClipboard
->Open();
278 canPaste
= wxTheClipboard
->IsSupported( wxDF_TEXT
);
279 wxTheClipboard
->Close();
284 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
285 ct
.wCallTip
= new wxSTCCallTip(wMain
.GetID(), -1, &ct
);
286 ct
.wDraw
= ct
.wCallTip
;
290 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
292 popup
.GetID()->AppendSeparator();
294 popup
.GetID()->Append(cmd
, label
);
297 popup
.GetID()->Enable(cmd
, enabled
);
301 void ScintillaWX::ClaimSelection() {
306 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
310 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
311 // switch (iMessage) {
313 // return CanPaste();
315 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
322 //----------------------------------------------------------------------
325 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
327 paintState
= painting
;
328 Surface surfaceWindow
;
329 surfaceWindow
.Init(dc
);
330 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
332 Paint(&surfaceWindow
, rcPaint
);
334 surfaceWindow
.Release();
335 if (paintState
== paintAbandoned
) {
336 // Painting area was insufficient to cover new styling or brace highlight positions
339 paintState
= notPainting
;
341 // On wxGTK the editor window paints can overwrite the listbox...
343 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
348 void ScintillaWX::DoHScroll(int type
, int pos
) {
350 if (type
== wxEVT_SCROLLWIN_LINEUP
)
351 xPos
-= H_SCROLL_STEP
;
352 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
353 xPos
+= H_SCROLL_STEP
;
354 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
355 xPos
-= H_SCROLL_PAGE
;
356 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
357 xPos
+= H_SCROLL_PAGE
;
358 else if (type
== wxEVT_SCROLLWIN_TOP
)
360 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
362 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
365 HorizontalScrollTo(xPos
);
368 void ScintillaWX::DoVScroll(int type
, int pos
) {
369 int topLineNew
= topLine
;
370 if (type
== wxEVT_SCROLLWIN_LINEUP
)
372 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
374 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
375 topLineNew
-= LinesToScroll();
376 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
377 topLineNew
+= LinesToScroll();
378 else if (type
== wxEVT_SCROLLWIN_TOP
)
380 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
381 topLineNew
= MaxScrollPos();
382 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
385 ScrollTo(topLineNew
);
389 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
390 int topLineNew
= topLine
;
393 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
395 KeyCommand(SCI_ZOOMIN
);
398 KeyCommand(SCI_ZOOMOUT
);
401 else { // otherwise just scroll the window
402 wheelRotation
+= rotation
;
403 lines
= wheelRotation
/ delta
;
404 wheelRotation
-= lines
* delta
;
406 lines
*= linesPerAction
;
408 ScrollTo(topLineNew
);
414 void ScintillaWX::DoSize(int width
, int height
) {
415 PRectangle
rcClient(0,0,width
,height
);
416 SetScrollBarsTo(rcClient
);
420 void ScintillaWX::DoLoseFocus(){
421 SetFocusState(false);
424 void ScintillaWX::DoGainFocus(){
428 void ScintillaWX::DoSysColourChange() {
429 InvalidateStyleData();
432 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
433 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
436 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
437 ButtonUp(pt
, curTime
, ctrl
);
440 void ScintillaWX::DoButtonMove(Point pt
) {
445 void ScintillaWX::DoAddChar(char ch
) {
449 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
451 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
452 if (ctrl
&& key
>= 1 && key
<= 26)
457 case WXK_DOWN
: key
= SCK_DOWN
; break;
458 case WXK_UP
: key
= SCK_UP
; break;
459 case WXK_LEFT
: key
= SCK_LEFT
; break;
460 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
461 case WXK_HOME
: key
= SCK_HOME
; break;
462 case WXK_END
: key
= SCK_END
; break;
463 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
464 case WXK_NEXT
: key
= SCK_NEXT
; break;
465 case WXK_DELETE
: key
= SCK_DELETE
; break;
466 case WXK_INSERT
: key
= SCK_INSERT
; break;
467 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
468 case WXK_BACK
: key
= SCK_BACK
; break;
469 case WXK_TAB
: key
= SCK_TAB
; break;
470 case WXK_RETURN
: key
= SCK_RETURN
; break;
473 key
= SCK_ADD
; break;
475 case WXK_NUMPAD_SUBTRACT
:
476 key
= SCK_SUBTRACT
; break;
478 case WXK_NUMPAD_DIVIDE
:
479 key
= SCK_DIVIDE
; break;
480 case WXK_CONTROL
: key
= 0; break;
481 case WXK_ALT
: key
= 0; break;
482 case WXK_SHIFT
: key
= 0; break;
483 case WXK_MENU
: key
= 0; break;
486 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
495 void ScintillaWX::DoCommand(int ID
) {
500 void ScintillaWX::DoContextMenu(Point pt
) {
504 void ScintillaWX::DoOnListBox() {
505 AutoCompleteCompleted();
508 //----------------------------------------------------------------------
510 #if wxUSE_DRAG_AND_DROP
511 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
512 SetDragPosition(invalidPosition
);
514 // Send an event to allow the drag details to be changed
515 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
516 evt
.SetEventObject(stc
);
517 evt
.SetDragResult(dragResult
);
520 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
521 evt
.SetDragText(data
);
522 stc
->GetEventHandler()->ProcessEvent(evt
);
524 dragResult
= evt
.GetDragResult();
525 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
526 DropAt(evt
.GetPosition(),
528 dragResult
== wxDragMove
,
529 FALSE
); // TODO: rectangular?
536 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
542 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
543 SetDragPosition(PositionFromLocation(Point(x
, y
)));
545 // Send an event to allow the drag result to be changed
546 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
547 evt
.SetEventObject(stc
);
548 evt
.SetDragResult(def
);
551 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
552 stc
->GetEventHandler()->ProcessEvent(evt
);
554 dragResult
= evt
.GetDragResult();
559 void ScintillaWX::DoDragLeave() {
560 SetDragPosition(invalidPosition
);
563 //----------------------------------------------------------------------
565 // Redraw all of text area. This paint will not be abandoned.
566 void ScintillaWX::FullPaint() {
567 paintState
= painting
;
568 rcPaint
= GetTextRectangle();
569 paintingAllText
= true;
570 wxClientDC
dc(wMain
.GetID());
571 Surface surfaceWindow
;
572 surfaceWindow
.Init(&dc
);
573 Paint(&surfaceWindow
, rcPaint
);
574 surfaceWindow
.Release();
576 // wMain.GetID()->Refresh(FALSE);
578 paintState
= notPainting
;
582 void ScintillaWX::DoScrollToLine(int line
) {
587 void ScintillaWX::DoScrollToColumn(int column
) {
588 HorizontalScrollTo(column
* vs
.spaceWidth
);
593 //----------------------------------------------------------------------
594 //----------------------------------------------------------------------