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;
102 ScintillaWX::~ScintillaWX() {
106 //----------------------------------------------------------------------
107 // base class virtuals
110 void ScintillaWX::Initialise() {
111 //ScintillaBase::Initialise();
112 dropTarget
= new wxSTCDropTarget
;
113 dropTarget
->SetScintilla(this);
114 stc
->SetDropTarget(dropTarget
);
118 void ScintillaWX::Finalise() {
119 ScintillaBase::Finalise();
123 void ScintillaWX::StartDrag() {
124 wxDropSource
source(wMain
.GetID());
125 wxTextDataObject
data(dragChars
);
128 source
.SetData(data
);
129 result
= source
.DoDragDrop(TRUE
);
130 if (result
== wxDragMove
&& dropWentOutside
)
133 SetDragPosition(invalidPosition
);
137 void ScintillaWX::SetTicking(bool on
) {
138 wxSTCTimer
* steTimer
;
139 if (timer
.ticking
!= on
) {
142 steTimer
= new wxSTCTimer(this);
143 steTimer
->Start(timer
.tickSize
);
144 timer
.tickerID
= (int)steTimer
;
146 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
152 timer
.ticksToWait
= caret
.period
;
156 void ScintillaWX::SetMouseCapture(bool on
) {
157 if (on
&& !capturedMouse
)
158 wMain
.GetID()->CaptureMouse();
159 else if (!on
&& capturedMouse
)
160 wMain
.GetID()->ReleaseMouse();
165 bool ScintillaWX::HaveMouseCapture() {
166 return capturedMouse
;
170 void ScintillaWX::ScrollText(int linesToMove
) {
171 int dy
= vs
.lineHeight
* (linesToMove
);
172 wMain
.GetID()->ScrollWindow(0, dy
);
173 wMain
.GetID()->Update();
176 void ScintillaWX::SetVerticalScrollPos() {
177 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
180 void ScintillaWX::SetHorizontalScrollPos() {
181 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
185 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
186 bool modified
= false;
187 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
188 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
189 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
192 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
193 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
197 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
198 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
199 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
200 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
207 void ScintillaWX::NotifyChange() {
212 void ScintillaWX::NotifyParent(SCNotification scn
) {
213 stc
->NotifyParent(&scn
);
218 void ScintillaWX::Copy() {
219 if (currentPos
!= anchor
) {
220 char* text
= CopySelectionRange();
221 wxTheClipboard
->Open();
222 wxTheClipboard
->SetData(new wxTextDataObject(text
));
223 wxTheClipboard
->Close();
228 void ScintillaWX::Paste() {
229 pdoc
->BeginUndoAction();
232 wxTextDataObject data
;
235 wxTheClipboard
->Open();
236 canPaste
= wxTheClipboard
->GetData(data
);
237 wxTheClipboard
->Close();
239 wxString str
= data
.GetText();
240 int len
= str
.Length();
241 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
242 SetEmptySelection(currentPos
+ len
);
245 pdoc
->EndUndoAction();
251 bool ScintillaWX::CanPaste() {
252 wxTextDataObject data
;
255 wxTheClipboard
->Open();
256 canPaste
= wxTheClipboard
->GetData(data
);
257 wxTheClipboard
->Close();
262 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
263 ct
.wCallTip
= new wxSTCCallTip(wMain
.GetID(), -1, &ct
);
264 ct
.wDraw
= ct
.wCallTip
;
268 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
270 popup
.GetID()->AppendSeparator();
272 popup
.GetID()->Append(cmd
, label
);
275 popup
.GetID()->Enable(cmd
, enabled
);
279 void ScintillaWX::ClaimSelection() {
284 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
288 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
289 // switch (iMessage) {
291 // return CanPaste();
293 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
300 //----------------------------------------------------------------------
303 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
305 paintState
= painting
;
306 Surface surfaceWindow
;
307 surfaceWindow
.Init(dc
);
308 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
310 Paint(&surfaceWindow
, rcPaint
);
312 surfaceWindow
.Release();
313 if (paintState
== paintAbandoned
) {
314 // Painting area was insufficient to cover new styling or brace highlight positions
317 paintState
= notPainting
;
319 // On wxGTK the editor window paints can overwrite the listbox...
321 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
326 void ScintillaWX::DoHScroll(int type
, int pos
) {
328 if (type
== wxEVT_SCROLLWIN_LINEUP
)
329 xPos
-= H_SCROLL_STEP
;
330 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
331 xPos
+= H_SCROLL_STEP
;
332 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
333 xPos
-= H_SCROLL_PAGE
;
334 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
335 xPos
+= H_SCROLL_PAGE
;
336 else if (type
== wxEVT_SCROLLWIN_TOP
)
338 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
340 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
343 HorizontalScrollTo(xPos
);
346 void ScintillaWX::DoVScroll(int type
, int pos
) {
347 int topLineNew
= topLine
;
348 if (type
== wxEVT_SCROLLWIN_LINEUP
)
350 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
352 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
353 topLineNew
-= LinesToScroll();
354 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
355 topLineNew
+= LinesToScroll();
356 else if (type
== wxEVT_SCROLLWIN_TOP
)
358 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
359 topLineNew
= MaxScrollPos();
360 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
363 ScrollTo(topLineNew
);
367 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
368 int topLineNew
= topLine
;
371 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
373 KeyCommand(SCI_ZOOMIN
);
376 KeyCommand(SCI_ZOOMOUT
);
379 else { // otherwise just scroll the window
380 wheelRotation
+= rotation
;
381 lines
= wheelRotation
/ delta
;
382 wheelRotation
-= lines
* delta
;
384 lines
*= linesPerAction
;
386 ScrollTo(topLineNew
);
392 void ScintillaWX::DoSize(int width
, int height
) {
393 PRectangle
rcClient(0,0,width
,height
);
394 SetScrollBarsTo(rcClient
);
398 void ScintillaWX::DoLoseFocus(){
399 SetFocusState(false);
402 void ScintillaWX::DoGainFocus(){
406 void ScintillaWX::DoSysColourChange() {
407 InvalidateStyleData();
410 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
411 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
414 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
415 ButtonUp(pt
, curTime
, ctrl
);
418 void ScintillaWX::DoButtonMove(Point pt
) {
423 void ScintillaWX::DoAddChar(char ch
) {
424 //bool acActiveBeforeCharAdded = ac.Active();
426 //if (acActiveBeforeCharAdded)
427 // AutoCompleteChanged(ch);
430 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
432 case WXK_DOWN
: key
= SCK_DOWN
; break;
433 case WXK_UP
: key
= SCK_UP
; break;
434 case WXK_LEFT
: key
= SCK_LEFT
; break;
435 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
436 case WXK_HOME
: key
= SCK_HOME
; break;
437 case WXK_END
: key
= SCK_END
; break;
438 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
439 case WXK_NEXT
: key
= SCK_NEXT
; break;
440 case WXK_DELETE
: key
= SCK_DELETE
; break;
441 case WXK_INSERT
: key
= SCK_INSERT
; break;
442 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
443 case WXK_BACK
: key
= SCK_BACK
; break;
444 case WXK_TAB
: key
= SCK_TAB
; break;
445 case WXK_RETURN
: key
= SCK_RETURN
; break;
446 case WXK_ADD
: key
= SCK_ADD
; break;
447 case WXK_SUBTRACT
: key
= SCK_SUBTRACT
; break;
448 case WXK_DIVIDE
: key
= SCK_DIVIDE
; break;
449 case WXK_CONTROL
: key
= 0; break;
450 case WXK_ALT
: key
= 0; break;
451 case WXK_SHIFT
: key
= 0; break;
454 return KeyDown(key
, shift
, ctrl
, alt
, consumed
);
458 void ScintillaWX::DoCommand(int ID
) {
463 void ScintillaWX::DoContextMenu(Point pt
) {
467 void ScintillaWX::DoOnListBox() {
468 AutoCompleteCompleted();
471 //----------------------------------------------------------------------
473 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
474 SetDragPosition(invalidPosition
);
475 int movePos
= PositionFromLocation(Point(x
,y
));
476 DropAt(movePos
, data
, dragResult
== wxDragMove
, FALSE
); // TODO: rectangular?
481 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
486 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
487 SetDragPosition(PositionFromLocation(Point(x
, y
)));
493 void ScintillaWX::DoDragLeave() {
494 SetDragPosition(invalidPosition
);
497 //----------------------------------------------------------------------
499 // Redraw all of text area. This paint will not be abandoned.
500 void ScintillaWX::FullPaint() {
501 paintState
= painting
;
502 rcPaint
= GetTextRectangle();
503 paintingAllText
= true;
504 wxClientDC
dc(wMain
.GetID());
505 Surface surfaceWindow
;
506 surfaceWindow
.Init(&dc
);
507 Paint(&surfaceWindow
, rcPaint
);
508 surfaceWindow
.Release();
510 // wMain.GetID()->Refresh(FALSE);
512 paintState
= notPainting
;
516 void ScintillaWX::DoScrollToLine(int line
) {
521 void ScintillaWX::DoScrollToColumn(int column
) {
522 HorizontalScrollTo(column
* vs
.spaceWidth
);
527 //----------------------------------------------------------------------
528 //----------------------------------------------------------------------