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 /////////////////////////////////////////////////////////////////////////////
17 #include "ScintillaWX.h"
18 #include "wx/stc/stc.h"
21 //----------------------------------------------------------------------
23 const int H_SCROLL_MAX
= 2000;
24 const int H_SCROLL_STEP
= 20;
25 const int H_SCROLL_PAGE
= 200;
27 //----------------------------------------------------------------------
30 class wxSTCTimer
: public wxTimer
{
32 wxSTCTimer(ScintillaWX
* swx
) {
46 bool wxSTCDropTarget::OnDropText(wxCoord x
, wxCoord y
, const wxString
& data
) {
47 return swx
->DoDropText(x
, y
, data
);
50 wxDragResult
wxSTCDropTarget::OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
51 return swx
->DoDragEnter(x
, y
, def
);
54 wxDragResult
wxSTCDropTarget::OnDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
55 return swx
->DoDragOver(x
, y
, def
);
58 void wxSTCDropTarget::OnLeave() {
64 //----------------------------------------------------------------------
65 // Constructor/Destructor
68 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
69 capturedMouse
= false;
77 ScintillaWX::~ScintillaWX() {
81 //----------------------------------------------------------------------
82 // base class virtuals
85 void ScintillaWX::Initialise() {
86 //ScintillaBase::Initialise();
87 dropTarget
= new wxSTCDropTarget
;
88 dropTarget
->SetScintilla(this);
89 stc
->SetDropTarget(dropTarget
);
93 void ScintillaWX::Finalise() {
94 ScintillaBase::Finalise();
98 void ScintillaWX::StartDrag() {
100 wxTextDataObject
data(dragChars
);
103 source
.SetData(data
);
104 result
= source
.DoDragDrop(TRUE
);
105 if (result
== wxDragMove
&& dropWentOutside
)
108 SetDragPosition(invalidPosition
);
112 void ScintillaWX::SetTicking(bool on
) {
113 wxSTCTimer
* steTimer
;
114 if (timer
.ticking
!= on
) {
117 steTimer
= new wxSTCTimer(this);
118 steTimer
->Start(timer
.tickSize
);
119 timer
.tickerID
= (int)steTimer
;
121 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
127 timer
.ticksToWait
= caret
.period
;
131 void ScintillaWX::SetMouseCapture(bool on
) {
133 wMain
.GetID()->CaptureMouse();
135 wMain
.GetID()->ReleaseMouse();
140 bool ScintillaWX::HaveMouseCapture() {
141 return capturedMouse
;
145 void ScintillaWX::ScrollText(int linesToMove
) {
146 int dy
= vs
.lineHeight
* (linesToMove
);
147 // TODO: calculate the rectangle to refreshed...
148 wMain
.GetID()->ScrollWindow(0, dy
);
151 void ScintillaWX::SetVerticalScrollPos() {
152 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
155 void ScintillaWX::SetHorizontalScrollPos() {
156 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
160 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
161 bool modified
= false;
162 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
163 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
164 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
167 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
168 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
172 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
173 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
174 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
175 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
182 void ScintillaWX::NotifyChange() {
187 void ScintillaWX::NotifyParent(SCNotification scn
) {
188 stc
->NotifyParent(&scn
);
193 void ScintillaWX::Copy() {
194 if (currentPos
!= anchor
) {
195 char* text
= CopySelectionRange();
196 textDO
.SetText(text
);
197 wxTheClipboard
->Open();
198 wxTheClipboard
->SetData(&textDO
);
199 wxTheClipboard
->Close();
204 void ScintillaWX::Paste() {
205 pdoc
->BeginUndoAction();
208 wxTextDataObject data
;
211 wxTheClipboard
->Open();
212 canPaste
= wxTheClipboard
->GetData(data
);
213 wxTheClipboard
->Close();
215 wxString str
= data
.GetText();
216 int len
= str
.Length();
217 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
218 SetEmptySelection(currentPos
+ len
);
221 pdoc
->EndUndoAction();
227 bool ScintillaWX::CanPaste() {
228 wxTextDataObject data
;
231 wxTheClipboard
->Open();
232 canPaste
= wxTheClipboard
->GetData(data
);
233 wxTheClipboard
->Close();
238 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
239 ct
.wCallTip
= new wxWindow(wDraw
.GetID(), -1);
240 ct
.wDraw
= ct
.wCallTip
;
244 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
246 popup
.GetID()->AppendSeparator();
248 popup
.GetID()->Append(cmd
, label
);
251 popup
.GetID()->Enable(cmd
, enabled
);
253 // TODO: need to create event handler mappings for the cmd ID
257 void ScintillaWX::ClaimSelection() {
262 LRESULT
ScintillaWX::DefWndProc(UINT
/*iMessage*/, WPARAM
/*wParam*/, LPARAM
/*lParam*/) {
266 LRESULT
ScintillaWX::WndProc(UINT iMessage
, WPARAM wParam
, LPARAM lParam
) {
271 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
278 //----------------------------------------------------------------------
281 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
283 paintState
= painting
;
284 Surface surfaceWindow
;
285 surfaceWindow
.Init(dc
);
286 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
288 Paint(&surfaceWindow
, rcPaint
);
290 surfaceWindow
.Release();
291 if (paintState
== paintAbandoned
) {
292 // Painting area was insufficient to cover new styling or brace highlight positions
295 paintState
= notPainting
;
299 void ScintillaWX::DoHScroll(int type
, int pos
) {
302 case wxEVT_SCROLLWIN_LINEUP
:
303 xPos
-= H_SCROLL_STEP
;
305 case wxEVT_SCROLLWIN_LINEDOWN
:
306 xPos
+= H_SCROLL_STEP
;
308 case wxEVT_SCROLLWIN_PAGEUP
:
309 xPos
-= H_SCROLL_PAGE
;
311 case wxEVT_SCROLLWIN_PAGEDOWN
:
312 xPos
+= H_SCROLL_PAGE
;
314 case wxEVT_SCROLLWIN_TOP
:
317 case wxEVT_SCROLLWIN_BOTTOM
:
320 case wxEVT_SCROLLWIN_THUMBTRACK
:
324 HorizontalScrollTo(xPos
);
327 void ScintillaWX::DoVScroll(int type
, int pos
) {
328 int topLineNew
= topLine
;
330 case wxEVT_SCROLLWIN_LINEUP
:
333 case wxEVT_SCROLLWIN_LINEDOWN
:
336 case wxEVT_SCROLLWIN_PAGEUP
:
337 topLineNew
-= LinesToScroll();
339 case wxEVT_SCROLLWIN_PAGEDOWN
:
340 topLineNew
+= LinesToScroll();
342 case wxEVT_SCROLLWIN_TOP
:
345 case wxEVT_SCROLLWIN_BOTTOM
:
346 topLineNew
= MaxScrollPos();
348 case wxEVT_SCROLLWIN_THUMBTRACK
:
352 ScrollTo(topLineNew
);
355 void ScintillaWX::DoSize(int width
, int height
) {
356 PRectangle
rcClient(0,0,width
,height
);
357 SetScrollBarsTo(rcClient
);
361 void ScintillaWX::DoLoseFocus(){
365 void ScintillaWX::DoGainFocus(){
366 ShowCaretAtCurrentPosition();
369 void ScintillaWX::DoSysColourChange() {
370 InvalidateStyleData();
373 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
374 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
377 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
378 ButtonUp(pt
, curTime
, ctrl
);
381 void ScintillaWX::DoButtonMove(Point pt
) {
386 void ScintillaWX::DoAddChar(char ch
) {
390 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
) {
391 return KeyDown(key
, shift
, ctrl
, alt
);
395 void ScintillaWX::DoCommand(int ID
) {
400 void ScintillaWX::DoContextMenu(Point pt
) {
405 //----------------------------------------------------------------------
407 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
408 SetDragPosition(invalidPosition
);
409 int movePos
= PositionFromLocation(Point(x
,y
));
410 DropAt(movePos
, data
, dragResult
== wxDragMove
, FALSE
); // TODO: rectangular?
415 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
420 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
421 SetDragPosition(PositionFromLocation(Point(x
, y
)));
427 void ScintillaWX::DoDragLeave() {
428 SetDragPosition(invalidPosition
);
431 //----------------------------------------------------------------------
433 // Redraw all of text area. This paint will not be abandoned.
434 void ScintillaWX::FullPaint() {
435 paintState
= painting
;
436 rcPaint
= GetTextRectangle();
437 wxClientDC
dc(wMain
.GetID());
438 Surface surfaceWindow
;
439 surfaceWindow
.Init(&dc
);
440 Paint(&surfaceWindow
, rcPaint
);
441 surfaceWindow
.Release();
442 paintState
= notPainting
;
446 void ScintillaWX::DoScrollToLine(int line
) {
451 void ScintillaWX::DoScrollToColumn(int column
) {
452 HorizontalScrollTo(column
* vs
.spaceWidth
);
457 //----------------------------------------------------------------------
458 //----------------------------------------------------------------------