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
.SetScintilla(this);
88 stc
->SetDropTarget(&dropTarget
);
92 void ScintillaWX::Finalise() {
93 ScintillaBase::Finalise();
97 void ScintillaWX::StartDrag() {
99 wxTextDataObject
data(dragChars
);
102 source
.SetData(data
);
103 result
= source
.DoDragDrop(TRUE
);
104 if (result
== wxDragMove
&& dropWentOutside
)
107 SetDragPosition(invalidPosition
);
111 void ScintillaWX::SetTicking(bool on
) {
112 wxSTCTimer
* steTimer
;
113 if (timer
.ticking
!= on
) {
116 steTimer
= new wxSTCTimer(this);
117 steTimer
->Start(timer
.tickSize
);
118 timer
.tickerID
= (int)steTimer
;
120 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
126 timer
.ticksToWait
= caret
.period
;
130 void ScintillaWX::SetMouseCapture(bool on
) {
132 wMain
.GetID()->CaptureMouse();
134 wMain
.GetID()->ReleaseMouse();
139 bool ScintillaWX::HaveMouseCapture() {
140 return capturedMouse
;
144 void ScintillaWX::ScrollText(int linesToMove
) {
145 int dy
= vs
.lineHeight
* (linesToMove
);
146 // TODO: calculate the rectangle to refreshed...
147 wMain
.GetID()->ScrollWindow(0, dy
);
150 void ScintillaWX::SetVerticalScrollPos() {
151 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
154 void ScintillaWX::SetHorizontalScrollPos() {
155 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
159 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
160 bool modified
= false;
161 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
162 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
163 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
166 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
167 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
171 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
172 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
173 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
174 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
181 void ScintillaWX::NotifyChange() {
186 void ScintillaWX::NotifyParent(SCNotification scn
) {
187 stc
->NotifyParent(&scn
);
192 void ScintillaWX::Copy() {
193 if (currentPos
!= anchor
) {
194 char* text
= CopySelectionRange();
195 textDO
.SetText(text
);
196 wxTheClipboard
->Open();
197 wxTheClipboard
->SetData(&textDO
);
198 wxTheClipboard
->Close();
203 void ScintillaWX::Paste() {
204 pdoc
->BeginUndoAction();
207 wxTextDataObject data
;
210 wxTheClipboard
->Open();
211 canPaste
= wxTheClipboard
->GetData(data
);
212 wxTheClipboard
->Close();
214 wxString str
= data
.GetText();
215 int len
= str
.Length();
216 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
217 SetEmptySelection(currentPos
+ len
);
220 pdoc
->EndUndoAction();
226 bool ScintillaWX::CanPaste() {
227 wxTextDataObject data
;
230 wxTheClipboard
->Open();
231 canPaste
= wxTheClipboard
->GetData(data
);
232 wxTheClipboard
->Close();
237 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
238 ct
.wCallTip
= new wxWindow(wDraw
.GetID(), -1);
239 ct
.wDraw
= ct
.wCallTip
;
243 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
245 popup
.GetID()->AppendSeparator();
247 popup
.GetID()->Append(cmd
, label
);
250 popup
.GetID()->Enable(cmd
, enabled
);
252 // TODO: need to create event handler mappings for the cmd ID
256 void ScintillaWX::ClaimSelection() {
261 LRESULT
ScintillaWX::DefWndProc(UINT
/*iMessage*/, WPARAM
/*wParam*/, LPARAM
/*lParam*/) {
265 LRESULT
ScintillaWX::WndProc(UINT iMessage
, WPARAM wParam
, LPARAM lParam
) {
270 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
277 //----------------------------------------------------------------------
280 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
282 paintState
= painting
;
283 Surface surfaceWindow
;
284 surfaceWindow
.Init(dc
);
285 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
287 Paint(&surfaceWindow
, rcPaint
);
289 surfaceWindow
.Release();
290 if (paintState
== paintAbandoned
) {
291 // Painting area was insufficient to cover new styling or brace highlight positions
294 paintState
= notPainting
;
298 void ScintillaWX::DoHScroll(int type
, int pos
) {
301 case wxEVT_SCROLLWIN_LINEUP
:
302 xPos
-= H_SCROLL_STEP
;
304 case wxEVT_SCROLLWIN_LINEDOWN
:
305 xPos
+= H_SCROLL_STEP
;
307 case wxEVT_SCROLLWIN_PAGEUP
:
308 xPos
-= H_SCROLL_PAGE
;
310 case wxEVT_SCROLLWIN_PAGEDOWN
:
311 xPos
+= H_SCROLL_PAGE
;
313 case wxEVT_SCROLLWIN_TOP
:
316 case wxEVT_SCROLLWIN_BOTTOM
:
319 case wxEVT_SCROLLWIN_THUMBTRACK
:
323 HorizontalScrollTo(xPos
);
326 void ScintillaWX::DoVScroll(int type
, int pos
) {
327 int topLineNew
= topLine
;
329 case wxEVT_SCROLLWIN_LINEUP
:
332 case wxEVT_SCROLLWIN_LINEDOWN
:
335 case wxEVT_SCROLLWIN_PAGEUP
:
336 topLineNew
-= LinesToScroll();
338 case wxEVT_SCROLLWIN_PAGEDOWN
:
339 topLineNew
+= LinesToScroll();
341 case wxEVT_SCROLLWIN_TOP
:
344 case wxEVT_SCROLLWIN_BOTTOM
:
345 topLineNew
= MaxScrollPos();
347 case wxEVT_SCROLLWIN_THUMBTRACK
:
351 ScrollTo(topLineNew
);
354 void ScintillaWX::DoSize(int width
, int height
) {
355 PRectangle
rcClient(0,0,width
,height
);
356 SetScrollBarsTo(rcClient
);
360 void ScintillaWX::DoLoseFocus(){
364 void ScintillaWX::DoGainFocus(){
365 ShowCaretAtCurrentPosition();
368 void ScintillaWX::DoSysColourChange() {
369 InvalidateStyleData();
372 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
373 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
376 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
377 ButtonUp(pt
, curTime
, ctrl
);
380 void ScintillaWX::DoButtonMove(Point pt
) {
385 void ScintillaWX::DoAddChar(char ch
) {
389 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
) {
390 return KeyDown(key
, shift
, ctrl
, alt
);
394 void ScintillaWX::DoCommand(int ID
) {
399 void ScintillaWX::DoContextMenu(Point pt
) {
404 //----------------------------------------------------------------------
406 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
407 SetDragPosition(invalidPosition
);
408 int movePos
= PositionFromLocation(Point(x
,y
));
409 DropAt(movePos
, data
, dragResult
== wxDragMove
, FALSE
); // TODO: rectangular?
414 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
419 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
420 SetDragPosition(PositionFromLocation(Point(x
, y
)));
426 void ScintillaWX::DoDragLeave() {
427 SetDragPosition(invalidPosition
);
430 //----------------------------------------------------------------------
432 // Redraw all of text area. This paint will not be abandoned.
433 void ScintillaWX::FullPaint() {
434 paintState
= painting
;
435 rcPaint
= GetTextRectangle();
436 wxClientDC
dc(wMain
.GetID());
437 Surface surfaceWindow
;
438 surfaceWindow
.Init(&dc
);
439 Paint(&surfaceWindow
, rcPaint
);
440 surfaceWindow
.Release();
441 paintState
= notPainting
;
445 void ScintillaWX::DoScrollToLine(int line
) {
450 void ScintillaWX::DoScrollToColumn(int column
) {
451 HorizontalScrollTo(column
* vs
.spaceWidth
);
456 //----------------------------------------------------------------------
457 //----------------------------------------------------------------------