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 wxString
dragText(drag
.s
, drag
.len
);
126 // Send an event to allow the drag text to be changed
127 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
128 evt
.SetEventObject(stc
);
129 evt
.SetDragText(dragText
);
130 evt
.SetDragAllowMove(TRUE
);
131 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
132 stc
->GetSelectionEnd()));
133 stc
->GetEventHandler()->ProcessEvent(evt
);
134 dragText
= evt
.GetDragText();
136 if (dragText
.Length()) {
137 wxDropSource
source(wMain
.GetID());
138 wxTextDataObject
data(dragText
);
141 source
.SetData(data
);
142 dropWentOutside
= TRUE
;
143 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
144 if (result
== wxDragMove
&& dropWentOutside
)
147 SetDragPosition(invalidPosition
);
152 void ScintillaWX::SetTicking(bool on
) {
153 wxSTCTimer
* steTimer
;
154 if (timer
.ticking
!= on
) {
157 steTimer
= new wxSTCTimer(this);
158 steTimer
->Start(timer
.tickSize
);
159 timer
.tickerID
= (int)steTimer
;
161 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
167 timer
.ticksToWait
= caret
.period
;
171 void ScintillaWX::SetMouseCapture(bool on
) {
172 if (on
&& !capturedMouse
)
173 wMain
.GetID()->CaptureMouse();
174 else if (!on
&& capturedMouse
)
175 wMain
.GetID()->ReleaseMouse();
180 bool ScintillaWX::HaveMouseCapture() {
181 return capturedMouse
;
185 void ScintillaWX::ScrollText(int linesToMove
) {
186 int dy
= vs
.lineHeight
* (linesToMove
);
187 wMain
.GetID()->ScrollWindow(0, dy
);
188 wMain
.GetID()->Update();
191 void ScintillaWX::SetVerticalScrollPos() {
192 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
195 void ScintillaWX::SetHorizontalScrollPos() {
196 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
200 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
201 bool modified
= false;
202 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
203 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
204 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
207 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
208 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
212 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
213 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
214 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
215 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
222 void ScintillaWX::NotifyChange() {
227 void ScintillaWX::NotifyParent(SCNotification scn
) {
228 stc
->NotifyParent(&scn
);
233 void ScintillaWX::Copy() {
234 if (currentPos
!= anchor
) {
236 CopySelectionRange(&st
);
237 wxTheClipboard
->Open();
238 wxTheClipboard
->SetData(new wxTextDataObject(wxString(st
.s
, st
.len
)));
239 wxTheClipboard
->Close();
244 void ScintillaWX::Paste() {
245 pdoc
->BeginUndoAction();
248 wxTextDataObject data
;
251 wxTheClipboard
->Open();
252 gotData
= wxTheClipboard
->GetData(data
);
253 wxTheClipboard
->Close();
255 wxString str
= data
.GetText();
256 int len
= str
.Length();
257 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
258 SetEmptySelection(currentPos
+ len
);
261 pdoc
->EndUndoAction();
267 bool ScintillaWX::CanPaste() {
270 wxTheClipboard
->Open();
271 canPaste
= wxTheClipboard
->IsSupported( wxDF_TEXT
);
272 wxTheClipboard
->Close();
277 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
278 ct
.wCallTip
= new wxSTCCallTip(wMain
.GetID(), -1, &ct
);
279 ct
.wDraw
= ct
.wCallTip
;
283 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
285 popup
.GetID()->AppendSeparator();
287 popup
.GetID()->Append(cmd
, label
);
290 popup
.GetID()->Enable(cmd
, enabled
);
294 void ScintillaWX::ClaimSelection() {
299 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
303 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
304 // switch (iMessage) {
306 // return CanPaste();
308 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
315 //----------------------------------------------------------------------
318 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
320 paintState
= painting
;
321 Surface surfaceWindow
;
322 surfaceWindow
.Init(dc
);
323 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
325 Paint(&surfaceWindow
, rcPaint
);
327 surfaceWindow
.Release();
328 if (paintState
== paintAbandoned
) {
329 // Painting area was insufficient to cover new styling or brace highlight positions
332 paintState
= notPainting
;
334 // On wxGTK the editor window paints can overwrite the listbox...
336 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
341 void ScintillaWX::DoHScroll(int type
, int pos
) {
343 if (type
== wxEVT_SCROLLWIN_LINEUP
)
344 xPos
-= H_SCROLL_STEP
;
345 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
346 xPos
+= H_SCROLL_STEP
;
347 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
348 xPos
-= H_SCROLL_PAGE
;
349 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
350 xPos
+= H_SCROLL_PAGE
;
351 else if (type
== wxEVT_SCROLLWIN_TOP
)
353 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
355 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
358 HorizontalScrollTo(xPos
);
361 void ScintillaWX::DoVScroll(int type
, int pos
) {
362 int topLineNew
= topLine
;
363 if (type
== wxEVT_SCROLLWIN_LINEUP
)
365 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
367 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
368 topLineNew
-= LinesToScroll();
369 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
370 topLineNew
+= LinesToScroll();
371 else if (type
== wxEVT_SCROLLWIN_TOP
)
373 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
374 topLineNew
= MaxScrollPos();
375 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
378 ScrollTo(topLineNew
);
382 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
383 int topLineNew
= topLine
;
386 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
388 KeyCommand(SCI_ZOOMIN
);
391 KeyCommand(SCI_ZOOMOUT
);
394 else { // otherwise just scroll the window
395 wheelRotation
+= rotation
;
396 lines
= wheelRotation
/ delta
;
397 wheelRotation
-= lines
* delta
;
399 lines
*= linesPerAction
;
401 ScrollTo(topLineNew
);
407 void ScintillaWX::DoSize(int width
, int height
) {
408 PRectangle
rcClient(0,0,width
,height
);
409 SetScrollBarsTo(rcClient
);
413 void ScintillaWX::DoLoseFocus(){
414 SetFocusState(false);
417 void ScintillaWX::DoGainFocus(){
421 void ScintillaWX::DoSysColourChange() {
422 InvalidateStyleData();
425 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
426 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
429 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
430 ButtonUp(pt
, curTime
, ctrl
);
433 void ScintillaWX::DoButtonMove(Point pt
) {
438 void ScintillaWX::DoAddChar(char ch
) {
442 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
444 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
445 if (ctrl
&& key
>= 1 && key
<= 26)
450 case WXK_DOWN
: key
= SCK_DOWN
; break;
451 case WXK_UP
: key
= SCK_UP
; break;
452 case WXK_LEFT
: key
= SCK_LEFT
; break;
453 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
454 case WXK_HOME
: key
= SCK_HOME
; break;
455 case WXK_END
: key
= SCK_END
; break;
456 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
457 case WXK_NEXT
: key
= SCK_NEXT
; break;
458 case WXK_DELETE
: key
= SCK_DELETE
; break;
459 case WXK_INSERT
: key
= SCK_INSERT
; break;
460 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
461 case WXK_BACK
: key
= SCK_BACK
; break;
462 case WXK_TAB
: key
= SCK_TAB
; break;
463 case WXK_RETURN
: key
= SCK_RETURN
; break;
466 key
= SCK_ADD
; break;
468 case WXK_NUMPAD_SUBTRACT
:
469 key
= SCK_SUBTRACT
; break;
471 case WXK_NUMPAD_DIVIDE
:
472 key
= SCK_DIVIDE
; break;
473 case WXK_CONTROL
: key
= 0; break;
474 case WXK_ALT
: key
= 0; break;
475 case WXK_SHIFT
: key
= 0; break;
476 case WXK_MENU
: key
= 0; break;
479 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
488 void ScintillaWX::DoCommand(int ID
) {
493 void ScintillaWX::DoContextMenu(Point pt
) {
497 void ScintillaWX::DoOnListBox() {
498 AutoCompleteCompleted();
501 //----------------------------------------------------------------------
503 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
504 SetDragPosition(invalidPosition
);
506 // Send an event to allow the drag details to be changed
507 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
508 evt
.SetEventObject(stc
);
509 evt
.SetDragResult(dragResult
);
512 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
513 evt
.SetDragText(data
);
514 stc
->GetEventHandler()->ProcessEvent(evt
);
516 dragResult
= evt
.GetDragResult();
517 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
518 DropAt(evt
.GetPosition(),
520 dragResult
== wxDragMove
,
521 FALSE
); // TODO: rectangular?
528 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
534 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
535 SetDragPosition(PositionFromLocation(Point(x
, y
)));
537 // Send an event to allow the drag result to be changed
538 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
539 evt
.SetEventObject(stc
);
540 evt
.SetDragResult(def
);
543 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
544 stc
->GetEventHandler()->ProcessEvent(evt
);
546 dragResult
= evt
.GetDragResult();
551 void ScintillaWX::DoDragLeave() {
552 SetDragPosition(invalidPosition
);
555 //----------------------------------------------------------------------
557 // Redraw all of text area. This paint will not be abandoned.
558 void ScintillaWX::FullPaint() {
559 paintState
= painting
;
560 rcPaint
= GetTextRectangle();
561 paintingAllText
= true;
562 wxClientDC
dc(wMain
.GetID());
563 Surface surfaceWindow
;
564 surfaceWindow
.Init(&dc
);
565 Paint(&surfaceWindow
, rcPaint
);
566 surfaceWindow
.Release();
568 // wMain.GetID()->Refresh(FALSE);
570 paintState
= notPainting
;
574 void ScintillaWX::DoScrollToLine(int line
) {
579 void ScintillaWX::DoScrollToColumn(int column
) {
580 HorizontalScrollTo(column
* vs
.spaceWidth
);
585 //----------------------------------------------------------------------
586 //----------------------------------------------------------------------