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(wxString(drag
.s
, drag
.len
));
128 dropWentOutside
= true;
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 wMain
.GetID()->ScrollWindow(0, dy
);
174 wMain
.GetID()->Update();
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
) {
222 CopySelectionRange(&st
);
223 wxTheClipboard
->Open();
224 wxTheClipboard
->SetData(new wxTextDataObject(wxString(st
.s
, st
.len
)));
225 wxTheClipboard
->Close();
230 void ScintillaWX::Paste() {
231 pdoc
->BeginUndoAction();
234 wxTextDataObject data
;
237 wxTheClipboard
->Open();
238 canPaste
= wxTheClipboard
->GetData(data
);
239 wxTheClipboard
->Close();
241 wxString str
= data
.GetText();
242 int len
= str
.Length();
243 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
244 SetEmptySelection(currentPos
+ len
);
247 pdoc
->EndUndoAction();
253 bool ScintillaWX::CanPaste() {
254 wxTextDataObject data
;
257 wxTheClipboard
->Open();
258 canPaste
= wxTheClipboard
->GetData(data
);
259 wxTheClipboard
->Close();
264 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
265 ct
.wCallTip
= new wxSTCCallTip(wMain
.GetID(), -1, &ct
);
266 ct
.wDraw
= ct
.wCallTip
;
270 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
272 popup
.GetID()->AppendSeparator();
274 popup
.GetID()->Append(cmd
, label
);
277 popup
.GetID()->Enable(cmd
, enabled
);
281 void ScintillaWX::ClaimSelection() {
286 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
290 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
291 // switch (iMessage) {
293 // return CanPaste();
295 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
302 //----------------------------------------------------------------------
305 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
307 paintState
= painting
;
308 Surface surfaceWindow
;
309 surfaceWindow
.Init(dc
);
310 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
312 Paint(&surfaceWindow
, rcPaint
);
314 surfaceWindow
.Release();
315 if (paintState
== paintAbandoned
) {
316 // Painting area was insufficient to cover new styling or brace highlight positions
319 paintState
= notPainting
;
321 // On wxGTK the editor window paints can overwrite the listbox...
323 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
328 void ScintillaWX::DoHScroll(int type
, int pos
) {
330 if (type
== wxEVT_SCROLLWIN_LINEUP
)
331 xPos
-= H_SCROLL_STEP
;
332 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
333 xPos
+= H_SCROLL_STEP
;
334 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
335 xPos
-= H_SCROLL_PAGE
;
336 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
337 xPos
+= H_SCROLL_PAGE
;
338 else if (type
== wxEVT_SCROLLWIN_TOP
)
340 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
342 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
345 HorizontalScrollTo(xPos
);
348 void ScintillaWX::DoVScroll(int type
, int pos
) {
349 int topLineNew
= topLine
;
350 if (type
== wxEVT_SCROLLWIN_LINEUP
)
352 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
354 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
355 topLineNew
-= LinesToScroll();
356 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
357 topLineNew
+= LinesToScroll();
358 else if (type
== wxEVT_SCROLLWIN_TOP
)
360 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
361 topLineNew
= MaxScrollPos();
362 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
365 ScrollTo(topLineNew
);
369 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
370 int topLineNew
= topLine
;
373 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
375 KeyCommand(SCI_ZOOMIN
);
378 KeyCommand(SCI_ZOOMOUT
);
381 else { // otherwise just scroll the window
382 wheelRotation
+= rotation
;
383 lines
= wheelRotation
/ delta
;
384 wheelRotation
-= lines
* delta
;
386 lines
*= linesPerAction
;
388 ScrollTo(topLineNew
);
394 void ScintillaWX::DoSize(int width
, int height
) {
395 PRectangle
rcClient(0,0,width
,height
);
396 SetScrollBarsTo(rcClient
);
400 void ScintillaWX::DoLoseFocus(){
401 SetFocusState(false);
404 void ScintillaWX::DoGainFocus(){
408 void ScintillaWX::DoSysColourChange() {
409 InvalidateStyleData();
412 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
413 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
416 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
417 ButtonUp(pt
, curTime
, ctrl
);
420 void ScintillaWX::DoButtonMove(Point pt
) {
425 void ScintillaWX::DoAddChar(char ch
) {
429 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
431 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
432 if (ctrl
&& key
>= 1 && key
<= 26)
437 case WXK_DOWN
: key
= SCK_DOWN
; break;
438 case WXK_UP
: key
= SCK_UP
; break;
439 case WXK_LEFT
: key
= SCK_LEFT
; break;
440 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
441 case WXK_HOME
: key
= SCK_HOME
; break;
442 case WXK_END
: key
= SCK_END
; break;
443 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
444 case WXK_NEXT
: key
= SCK_NEXT
; break;
445 case WXK_DELETE
: key
= SCK_DELETE
; break;
446 case WXK_INSERT
: key
= SCK_INSERT
; break;
447 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
448 case WXK_BACK
: key
= SCK_BACK
; break;
449 case WXK_TAB
: key
= SCK_TAB
; break;
450 case WXK_RETURN
: key
= SCK_RETURN
; break;
453 key
= SCK_ADD
; break;
455 case WXK_NUMPAD_SUBTRACT
:
456 key
= SCK_SUBTRACT
; break;
458 case WXK_NUMPAD_DIVIDE
:
459 key
= SCK_DIVIDE
; break;
460 case WXK_CONTROL
: key
= 0; break;
461 case WXK_ALT
: key
= 0; break;
462 case WXK_SHIFT
: key
= 0; break;
463 case WXK_MENU
: key
= 0; break;
466 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
475 void ScintillaWX::DoCommand(int ID
) {
480 void ScintillaWX::DoContextMenu(Point pt
) {
484 void ScintillaWX::DoOnListBox() {
485 AutoCompleteCompleted();
488 //----------------------------------------------------------------------
490 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
491 SetDragPosition(invalidPosition
);
492 int movePos
= PositionFromLocation(Point(x
,y
));
493 DropAt(movePos
, data
, dragResult
== wxDragMove
, FALSE
); // TODO: rectangular?
498 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
504 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
505 SetDragPosition(PositionFromLocation(Point(x
, y
)));
511 void ScintillaWX::DoDragLeave() {
512 SetDragPosition(invalidPosition
);
515 //----------------------------------------------------------------------
517 // Redraw all of text area. This paint will not be abandoned.
518 void ScintillaWX::FullPaint() {
519 paintState
= painting
;
520 rcPaint
= GetTextRectangle();
521 paintingAllText
= true;
522 wxClientDC
dc(wMain
.GetID());
523 Surface surfaceWindow
;
524 surfaceWindow
.Init(&dc
);
525 Paint(&surfaceWindow
, rcPaint
);
526 surfaceWindow
.Release();
528 // wMain.GetID()->Refresh(FALSE);
530 paintState
= notPainting
;
534 void ScintillaWX::DoScrollToLine(int line
) {
539 void ScintillaWX::DoScrollToColumn(int column
) {
540 HorizontalScrollTo(column
* vs
.spaceWidth
);
545 //----------------------------------------------------------------------
546 //----------------------------------------------------------------------