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
) {
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() {
65 class wxSTCCallTip
: public wxWindow
{
67 wxSTCCallTip(wxWindow
* parent
, int ID
, CallTip
* ct
)
68 : wxWindow(parent
, ID
)
73 void OnPaint(wxPaintEvent
& evt
) {
75 Surface surfaceWindow
;
76 surfaceWindow
.Init(&dc
);
77 m_ct
->PaintCT(&surfaceWindow
);
78 surfaceWindow
.Release();
85 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxWindow
)
86 EVT_PAINT(wxSTCCallTip::OnPaint
)
89 //----------------------------------------------------------------------
90 // Constructor/Destructor
93 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
94 capturedMouse
= false;
103 ScintillaWX::~ScintillaWX() {
107 //----------------------------------------------------------------------
108 // base class virtuals
111 void ScintillaWX::Initialise() {
112 //ScintillaBase::Initialise();
113 dropTarget
= new wxSTCDropTarget
;
114 dropTarget
->SetScintilla(this);
115 stc
->SetDropTarget(dropTarget
);
119 void ScintillaWX::Finalise() {
120 ScintillaBase::Finalise();
124 void ScintillaWX::StartDrag() {
125 wxDropSource
source(wMain
.GetID());
126 wxTextDataObject
data(dragChars
);
129 source
.SetData(data
);
130 result
= source
.DoDragDrop(TRUE
);
131 if (result
== wxDragMove
&& dropWentOutside
)
134 SetDragPosition(invalidPosition
);
138 void ScintillaWX::SetTicking(bool on
) {
139 wxSTCTimer
* steTimer
;
140 if (timer
.ticking
!= on
) {
143 steTimer
= new wxSTCTimer(this);
144 steTimer
->Start(timer
.tickSize
);
145 timer
.tickerID
= (int)steTimer
;
147 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
153 timer
.ticksToWait
= caret
.period
;
157 void ScintillaWX::SetMouseCapture(bool on
) {
158 if (on
&& !capturedMouse
)
159 wMain
.GetID()->CaptureMouse();
160 else if (!on
&& capturedMouse
)
161 wMain
.GetID()->ReleaseMouse();
166 bool ScintillaWX::HaveMouseCapture() {
167 return capturedMouse
;
171 void ScintillaWX::ScrollText(int linesToMove
) {
172 int dy
= vs
.lineHeight
* (linesToMove
);
173 // TODO: calculate the rectangle to refreshed...
174 wMain
.GetID()->ScrollWindow(0, dy
);
177 void ScintillaWX::SetVerticalScrollPos() {
178 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
181 void ScintillaWX::SetHorizontalScrollPos() {
182 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
186 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
187 bool modified
= false;
188 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
189 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
190 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
193 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
194 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
198 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
199 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
200 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
201 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
208 void ScintillaWX::NotifyChange() {
213 void ScintillaWX::NotifyParent(SCNotification scn
) {
214 stc
->NotifyParent(&scn
);
219 void ScintillaWX::Copy() {
220 if (currentPos
!= anchor
) {
221 char* text
= CopySelectionRange();
222 wxTheClipboard
->Open();
223 wxTheClipboard
->SetData(new wxTextDataObject(text
));
224 wxTheClipboard
->Close();
229 void ScintillaWX::Paste() {
230 pdoc
->BeginUndoAction();
233 wxTextDataObject data
;
236 wxTheClipboard
->Open();
237 canPaste
= wxTheClipboard
->GetData(data
);
238 wxTheClipboard
->Close();
240 wxString str
= data
.GetText();
241 int len
= str
.Length();
242 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
243 SetEmptySelection(currentPos
+ len
);
246 pdoc
->EndUndoAction();
252 bool ScintillaWX::CanPaste() {
253 wxTextDataObject data
;
256 wxTheClipboard
->Open();
257 canPaste
= wxTheClipboard
->GetData(data
);
258 wxTheClipboard
->Close();
263 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
264 ct
.wCallTip
= new wxSTCCallTip(wDraw
.GetID(), -1, &ct
);
265 ct
.wDraw
= ct
.wCallTip
;
269 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
271 popup
.GetID()->AppendSeparator();
273 popup
.GetID()->Append(cmd
, label
);
276 popup
.GetID()->Enable(cmd
, enabled
);
280 void ScintillaWX::ClaimSelection() {
285 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
289 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
290 // switch (iMessage) {
292 // return CanPaste();
294 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
301 //----------------------------------------------------------------------
304 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
306 paintState
= painting
;
307 Surface surfaceWindow
;
308 surfaceWindow
.Init(dc
);
309 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
311 Paint(&surfaceWindow
, rcPaint
);
313 surfaceWindow
.Release();
314 if (paintState
== paintAbandoned
) {
315 // Painting area was insufficient to cover new styling or brace highlight positions
318 paintState
= notPainting
;
320 // On wxGTK the editor window paints can overwrite the listbox...
322 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
327 void ScintillaWX::DoHScroll(int type
, int pos
) {
329 if (type
== wxEVT_SCROLLWIN_LINEUP
)
330 xPos
-= H_SCROLL_STEP
;
331 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
332 xPos
+= H_SCROLL_STEP
;
333 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
334 xPos
-= H_SCROLL_PAGE
;
335 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
336 xPos
+= H_SCROLL_PAGE
;
337 else if (type
== wxEVT_SCROLLWIN_TOP
)
339 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
341 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
344 HorizontalScrollTo(xPos
);
347 void ScintillaWX::DoVScroll(int type
, int pos
) {
348 int topLineNew
= topLine
;
349 if (type
== wxEVT_SCROLLWIN_LINEUP
)
351 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
353 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
354 topLineNew
-= LinesToScroll();
355 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
356 topLineNew
+= LinesToScroll();
357 else if (type
== wxEVT_SCROLLWIN_TOP
)
359 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
360 topLineNew
= MaxScrollPos();
361 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
364 ScrollTo(topLineNew
);
368 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
) {
369 int topLineNew
= topLine
;
372 wheelRotation
+= rotation
;
373 lines
= wheelRotation
/ delta
;
374 wheelRotation
-= lines
* delta
;
376 lines
*= linesPerAction
;
378 ScrollTo(topLineNew
);
383 void ScintillaWX::DoSize(int width
, int height
) {
384 PRectangle
rcClient(0,0,width
,height
);
385 SetScrollBarsTo(rcClient
);
389 void ScintillaWX::DoLoseFocus(){
393 void ScintillaWX::DoGainFocus(){
394 ShowCaretAtCurrentPosition();
397 void ScintillaWX::DoSysColourChange() {
398 InvalidateStyleData();
401 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
402 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
405 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
406 ButtonUp(pt
, curTime
, ctrl
);
409 void ScintillaWX::DoButtonMove(Point pt
) {
414 void ScintillaWX::DoAddChar(char ch
) {
415 //bool acActiveBeforeCharAdded = ac.Active();
417 //if (acActiveBeforeCharAdded)
418 // AutoCompleteChanged(ch);
421 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
) {
423 case WXK_DOWN
: key
= SCK_DOWN
; break;
424 case WXK_UP
: key
= SCK_UP
; break;
425 case WXK_LEFT
: key
= SCK_LEFT
; break;
426 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
427 case WXK_HOME
: key
= SCK_HOME
; break;
428 case WXK_END
: key
= SCK_END
; break;
429 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
430 case WXK_NEXT
: key
= SCK_NEXT
; break;
431 case WXK_DELETE
: key
= SCK_DELETE
; break;
432 case WXK_INSERT
: key
= SCK_INSERT
; break;
433 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
434 case WXK_BACK
: key
= SCK_BACK
; break;
435 case WXK_TAB
: key
= SCK_TAB
; break;
436 case WXK_RETURN
: key
= SCK_RETURN
; break;
437 case WXK_ADD
: key
= SCK_ADD
; break;
438 case WXK_SUBTRACT
: key
= SCK_SUBTRACT
; break;
439 case WXK_DIVIDE
: key
= SCK_DIVIDE
; break;
440 case WXK_CONTROL
: key
= 0; break;
441 case WXK_ALT
: key
= 0; break;
442 case WXK_SHIFT
: key
= 0; break;
445 return KeyDown(key
, shift
, ctrl
, alt
);
449 void ScintillaWX::DoCommand(int ID
) {
454 void ScintillaWX::DoContextMenu(Point pt
) {
458 void ScintillaWX::DoOnListBox() {
459 AutoCompleteCompleted();
462 //----------------------------------------------------------------------
464 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
465 SetDragPosition(invalidPosition
);
466 int movePos
= PositionFromLocation(Point(x
,y
));
467 DropAt(movePos
, data
, dragResult
== wxDragMove
, FALSE
); // TODO: rectangular?
472 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
477 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
478 SetDragPosition(PositionFromLocation(Point(x
, y
)));
484 void ScintillaWX::DoDragLeave() {
485 SetDragPosition(invalidPosition
);
488 //----------------------------------------------------------------------
490 // Redraw all of text area. This paint will not be abandoned.
491 void ScintillaWX::FullPaint() {
492 paintState
= painting
;
493 rcPaint
= GetTextRectangle();
494 paintingAllText
= true;
495 wxClientDC
dc(wMain
.GetID());
496 Surface surfaceWindow
;
497 surfaceWindow
.Init(&dc
);
498 Paint(&surfaceWindow
, rcPaint
);
499 surfaceWindow
.Release();
501 // wMain.GetID()->Refresh(FALSE);
503 paintState
= notPainting
;
507 void ScintillaWX::DoScrollToLine(int line
) {
512 void ScintillaWX::DoScrollToColumn(int column
) {
513 HorizontalScrollTo(column
* vs
.spaceWidth
);
518 //----------------------------------------------------------------------
519 //----------------------------------------------------------------------