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() {
66 //----------------------------------------------------------------------
67 // Constructor/Destructor
70 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
71 capturedMouse
= false;
79 ScintillaWX::~ScintillaWX() {
83 //----------------------------------------------------------------------
84 // base class virtuals
87 void ScintillaWX::Initialise() {
88 //ScintillaBase::Initialise();
89 dropTarget
= new wxSTCDropTarget
;
90 dropTarget
->SetScintilla(this);
91 stc
->SetDropTarget(dropTarget
);
95 void ScintillaWX::Finalise() {
96 ScintillaBase::Finalise();
100 void ScintillaWX::StartDrag() {
101 wxDropSource
source(wMain
.GetID());
102 wxTextDataObject
data(dragChars
);
105 source
.SetData(data
);
106 result
= source
.DoDragDrop(TRUE
);
107 if (result
== wxDragMove
&& dropWentOutside
)
110 SetDragPosition(invalidPosition
);
114 void ScintillaWX::SetTicking(bool on
) {
115 wxSTCTimer
* steTimer
;
116 if (timer
.ticking
!= on
) {
119 steTimer
= new wxSTCTimer(this);
120 steTimer
->Start(timer
.tickSize
);
121 timer
.tickerID
= (int)steTimer
;
123 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
129 timer
.ticksToWait
= caret
.period
;
133 void ScintillaWX::SetMouseCapture(bool on
) {
134 if (on
&& !capturedMouse
)
135 wMain
.GetID()->CaptureMouse();
136 else if (!on
&& capturedMouse
)
137 wMain
.GetID()->ReleaseMouse();
142 bool ScintillaWX::HaveMouseCapture() {
143 return capturedMouse
;
147 void ScintillaWX::ScrollText(int linesToMove
) {
148 int dy
= vs
.lineHeight
* (linesToMove
);
149 // TODO: calculate the rectangle to refreshed...
150 wMain
.GetID()->ScrollWindow(0, dy
);
153 void ScintillaWX::SetVerticalScrollPos() {
154 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
157 void ScintillaWX::SetHorizontalScrollPos() {
158 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
162 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
163 bool modified
= false;
164 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
165 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
166 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
169 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
170 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
174 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
175 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
176 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
177 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
184 void ScintillaWX::NotifyChange() {
189 void ScintillaWX::NotifyParent(SCNotification scn
) {
190 stc
->NotifyParent(&scn
);
195 void ScintillaWX::Copy() {
196 if (currentPos
!= anchor
) {
197 char* text
= CopySelectionRange();
198 textDO
.SetText(text
);
199 wxTheClipboard
->Open();
200 wxTheClipboard
->SetData(&textDO
);
201 wxTheClipboard
->Close();
206 void ScintillaWX::Paste() {
207 pdoc
->BeginUndoAction();
210 wxTextDataObject data
;
213 wxTheClipboard
->Open();
214 canPaste
= wxTheClipboard
->GetData(data
);
215 wxTheClipboard
->Close();
217 wxString str
= data
.GetText();
218 int len
= str
.Length();
219 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
220 SetEmptySelection(currentPos
+ len
);
223 pdoc
->EndUndoAction();
229 bool ScintillaWX::CanPaste() {
230 wxTextDataObject data
;
233 wxTheClipboard
->Open();
234 canPaste
= wxTheClipboard
->GetData(data
);
235 wxTheClipboard
->Close();
240 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
241 ct
.wCallTip
= new wxWindow(wDraw
.GetID(), -1);
242 ct
.wDraw
= ct
.wCallTip
;
246 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
248 popup
.GetID()->AppendSeparator();
250 popup
.GetID()->Append(cmd
, label
);
253 popup
.GetID()->Enable(cmd
, enabled
);
255 // TODO: need to create event handler mappings for the cmd ID
259 void ScintillaWX::ClaimSelection() {
264 LRESULT
ScintillaWX::DefWndProc(UINT
/*iMessage*/, WPARAM
/*wParam*/, LPARAM
/*lParam*/) {
268 LRESULT
ScintillaWX::WndProc(UINT iMessage
, WPARAM wParam
, LPARAM lParam
) {
273 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
280 //----------------------------------------------------------------------
283 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
285 paintState
= painting
;
286 Surface surfaceWindow
;
287 surfaceWindow
.Init(dc
);
288 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
290 Paint(&surfaceWindow
, rcPaint
);
292 surfaceWindow
.Release();
293 if (paintState
== paintAbandoned
) {
294 // Painting area was insufficient to cover new styling or brace highlight positions
297 paintState
= notPainting
;
301 void ScintillaWX::DoHScroll(int type
, int pos
) {
304 case wxEVT_SCROLLWIN_LINEUP
:
305 xPos
-= H_SCROLL_STEP
;
307 case wxEVT_SCROLLWIN_LINEDOWN
:
308 xPos
+= H_SCROLL_STEP
;
310 case wxEVT_SCROLLWIN_PAGEUP
:
311 xPos
-= H_SCROLL_PAGE
;
313 case wxEVT_SCROLLWIN_PAGEDOWN
:
314 xPos
+= H_SCROLL_PAGE
;
316 case wxEVT_SCROLLWIN_TOP
:
319 case wxEVT_SCROLLWIN_BOTTOM
:
322 case wxEVT_SCROLLWIN_THUMBTRACK
:
326 HorizontalScrollTo(xPos
);
329 void ScintillaWX::DoVScroll(int type
, int pos
) {
330 int topLineNew
= topLine
;
332 case wxEVT_SCROLLWIN_LINEUP
:
335 case wxEVT_SCROLLWIN_LINEDOWN
:
338 case wxEVT_SCROLLWIN_PAGEUP
:
339 topLineNew
-= LinesToScroll();
341 case wxEVT_SCROLLWIN_PAGEDOWN
:
342 topLineNew
+= LinesToScroll();
344 case wxEVT_SCROLLWIN_TOP
:
347 case wxEVT_SCROLLWIN_BOTTOM
:
348 topLineNew
= MaxScrollPos();
350 case wxEVT_SCROLLWIN_THUMBTRACK
:
354 ScrollTo(topLineNew
);
357 void ScintillaWX::DoSize(int width
, int height
) {
358 PRectangle
rcClient(0,0,width
,height
);
359 SetScrollBarsTo(rcClient
);
363 void ScintillaWX::DoLoseFocus(){
367 void ScintillaWX::DoGainFocus(){
368 ShowCaretAtCurrentPosition();
371 void ScintillaWX::DoSysColourChange() {
372 InvalidateStyleData();
375 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
376 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
379 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
380 ButtonUp(pt
, curTime
, ctrl
);
383 void ScintillaWX::DoButtonMove(Point pt
) {
388 void ScintillaWX::DoAddChar(char ch
) {
392 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
) {
393 return KeyDown(key
, shift
, ctrl
, alt
);
397 void ScintillaWX::DoCommand(int ID
) {
402 void ScintillaWX::DoContextMenu(Point pt
) {
407 //----------------------------------------------------------------------
409 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
410 SetDragPosition(invalidPosition
);
411 int movePos
= PositionFromLocation(Point(x
,y
));
412 DropAt(movePos
, data
, dragResult
== wxDragMove
, FALSE
); // TODO: rectangular?
417 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
422 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
423 SetDragPosition(PositionFromLocation(Point(x
, y
)));
429 void ScintillaWX::DoDragLeave() {
430 SetDragPosition(invalidPosition
);
433 //----------------------------------------------------------------------
435 // Redraw all of text area. This paint will not be abandoned.
436 void ScintillaWX::FullPaint() {
437 paintState
= painting
;
438 rcPaint
= GetTextRectangle();
439 wxClientDC
dc(wMain
.GetID());
440 Surface surfaceWindow
;
441 surfaceWindow
.Init(&dc
);
442 Paint(&surfaceWindow
, rcPaint
);
443 surfaceWindow
.Release();
444 paintState
= notPainting
;
448 void ScintillaWX::DoScrollToLine(int line
) {
453 void ScintillaWX::DoScrollToColumn(int column
) {
454 HorizontalScrollTo(column
* vs
.spaceWidth
);
459 //----------------------------------------------------------------------
460 //----------------------------------------------------------------------