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
) {
47 #if wxUSE_DRAG_AND_DROP
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 class wxSTCCallTip
: public wxWindow
{
68 wxSTCCallTip(wxWindow
* parent
, int ID
, CallTip
* ct
)
69 : wxWindow(parent
, ID
)
74 void OnPaint(wxPaintEvent
& evt
) {
76 Surface surfaceWindow
;
77 surfaceWindow
.Init(&dc
);
78 m_ct
->PaintCT(&surfaceWindow
);
79 surfaceWindow
.Release();
86 BEGIN_EVENT_TABLE(wxSTCCallTip
, wxWindow
)
87 EVT_PAINT(wxSTCCallTip::OnPaint
)
90 //----------------------------------------------------------------------
91 // Constructor/Destructor
94 ScintillaWX::ScintillaWX(wxStyledTextCtrl
* win
) {
95 capturedMouse
= false;
103 ScintillaWX::~ScintillaWX() {
107 //----------------------------------------------------------------------
108 // base class virtuals
111 void ScintillaWX::Initialise() {
112 //ScintillaBase::Initialise();
113 #if wxUSE_DRAG_AND_DROP
114 dropTarget
= new wxSTCDropTarget
;
115 dropTarget
->SetScintilla(this);
116 stc
->SetDropTarget(dropTarget
);
121 void ScintillaWX::Finalise() {
122 ScintillaBase::Finalise();
126 void ScintillaWX::StartDrag() {
127 #if wxUSE_DRAG_AND_DROP
128 wxString
dragText(drag
.s
, drag
.len
);
130 // Send an event to allow the drag text to be changed
131 wxStyledTextEvent
evt(wxEVT_STC_START_DRAG
, stc
->GetId());
132 evt
.SetEventObject(stc
);
133 evt
.SetDragText(dragText
);
134 evt
.SetDragAllowMove(TRUE
);
135 evt
.SetPosition(wxMin(stc
->GetSelectionStart(),
136 stc
->GetSelectionEnd()));
137 stc
->GetEventHandler()->ProcessEvent(evt
);
138 dragText
= evt
.GetDragText();
140 if (dragText
.Length()) {
141 wxDropSource
source(wMain
.GetID());
142 wxTextDataObject
data(dragText
);
145 source
.SetData(data
);
146 dropWentOutside
= TRUE
;
147 result
= source
.DoDragDrop(evt
.GetDragAllowMove());
148 if (result
== wxDragMove
&& dropWentOutside
)
151 SetDragPosition(invalidPosition
);
157 void ScintillaWX::SetTicking(bool on
) {
158 wxSTCTimer
* steTimer
;
159 if (timer
.ticking
!= on
) {
162 steTimer
= new wxSTCTimer(this);
163 steTimer
->Start(timer
.tickSize
);
164 timer
.tickerID
= (int)steTimer
;
166 steTimer
= (wxSTCTimer
*)timer
.tickerID
;
172 timer
.ticksToWait
= caret
.period
;
176 void ScintillaWX::SetMouseCapture(bool on
) {
177 if (on
&& !capturedMouse
)
178 wMain
.GetID()->CaptureMouse();
179 else if (!on
&& capturedMouse
)
180 wMain
.GetID()->ReleaseMouse();
185 bool ScintillaWX::HaveMouseCapture() {
186 return capturedMouse
;
190 void ScintillaWX::ScrollText(int linesToMove
) {
191 int dy
= vs
.lineHeight
* (linesToMove
);
192 wMain
.GetID()->ScrollWindow(0, dy
);
193 wMain
.GetID()->Update();
196 void ScintillaWX::SetVerticalScrollPos() {
197 wMain
.GetID()->SetScrollPos(wxVERTICAL
, topLine
);
200 void ScintillaWX::SetHorizontalScrollPos() {
201 wMain
.GetID()->SetScrollPos(wxHORIZONTAL
, xOffset
);
205 bool ScintillaWX::ModifyScrollBars(int nMax
, int nPage
) {
206 bool modified
= false;
207 int sbMax
= wMain
.GetID()->GetScrollRange(wxVERTICAL
);
208 int sbThumb
= wMain
.GetID()->GetScrollThumb(wxVERTICAL
);
209 int sbPos
= wMain
.GetID()->GetScrollPos(wxVERTICAL
);
212 if (sbMax
!= nMax
|| sbThumb
!= nPage
) {
213 wMain
.GetID()->SetScrollbar(wxVERTICAL
, sbPos
, nPage
, nMax
);
217 sbMax
= wMain
.GetID()->GetScrollRange(wxHORIZONTAL
);
218 sbThumb
= wMain
.GetID()->GetScrollThumb(wxHORIZONTAL
);
219 if ((sbMax
!= H_SCROLL_MAX
) || (sbThumb
!= H_SCROLL_STEP
)) {
220 wMain
.GetID()->SetScrollbar(wxHORIZONTAL
, 0, H_SCROLL_STEP
, H_SCROLL_MAX
);
227 void ScintillaWX::NotifyChange() {
232 void ScintillaWX::NotifyParent(SCNotification scn
) {
233 stc
->NotifyParent(&scn
);
238 void ScintillaWX::Copy() {
239 if (currentPos
!= anchor
) {
241 CopySelectionRange(&st
);
242 wxTheClipboard
->Open();
243 wxTheClipboard
->SetData(new wxTextDataObject(wxString(st
.s
, st
.len
)));
244 wxTheClipboard
->Close();
249 void ScintillaWX::Paste() {
250 pdoc
->BeginUndoAction();
253 wxTextDataObject data
;
256 wxTheClipboard
->Open();
257 gotData
= wxTheClipboard
->GetData(data
);
258 wxTheClipboard
->Close();
260 wxString str
= data
.GetText();
261 int len
= str
.Length();
262 pdoc
->InsertString(currentPos
, str
.c_str(), len
);
263 SetEmptySelection(currentPos
+ len
);
266 pdoc
->EndUndoAction();
272 bool ScintillaWX::CanPaste() {
275 wxTheClipboard
->Open();
276 canPaste
= wxTheClipboard
->IsSupported( wxDF_TEXT
);
277 wxTheClipboard
->Close();
282 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
283 ct
.wCallTip
= new wxSTCCallTip(wMain
.GetID(), -1, &ct
);
284 ct
.wDraw
= ct
.wCallTip
;
288 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
290 popup
.GetID()->AppendSeparator();
292 popup
.GetID()->Append(cmd
, label
);
295 popup
.GetID()->Enable(cmd
, enabled
);
299 void ScintillaWX::ClaimSelection() {
304 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
308 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
309 // switch (iMessage) {
311 // return CanPaste();
313 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
320 //----------------------------------------------------------------------
323 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
325 paintState
= painting
;
326 Surface surfaceWindow
;
327 surfaceWindow
.Init(dc
);
328 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
330 Paint(&surfaceWindow
, rcPaint
);
332 surfaceWindow
.Release();
333 if (paintState
== paintAbandoned
) {
334 // Painting area was insufficient to cover new styling or brace highlight positions
337 paintState
= notPainting
;
339 // On wxGTK the editor window paints can overwrite the listbox...
341 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
346 void ScintillaWX::DoHScroll(int type
, int pos
) {
348 if (type
== wxEVT_SCROLLWIN_LINEUP
)
349 xPos
-= H_SCROLL_STEP
;
350 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
351 xPos
+= H_SCROLL_STEP
;
352 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
353 xPos
-= H_SCROLL_PAGE
;
354 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
355 xPos
+= H_SCROLL_PAGE
;
356 else if (type
== wxEVT_SCROLLWIN_TOP
)
358 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
360 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
363 HorizontalScrollTo(xPos
);
366 void ScintillaWX::DoVScroll(int type
, int pos
) {
367 int topLineNew
= topLine
;
368 if (type
== wxEVT_SCROLLWIN_LINEUP
)
370 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
372 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
373 topLineNew
-= LinesToScroll();
374 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
375 topLineNew
+= LinesToScroll();
376 else if (type
== wxEVT_SCROLLWIN_TOP
)
378 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
379 topLineNew
= MaxScrollPos();
380 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
383 ScrollTo(topLineNew
);
387 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
388 int topLineNew
= topLine
;
391 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
393 KeyCommand(SCI_ZOOMIN
);
396 KeyCommand(SCI_ZOOMOUT
);
399 else { // otherwise just scroll the window
400 wheelRotation
+= rotation
;
401 lines
= wheelRotation
/ delta
;
402 wheelRotation
-= lines
* delta
;
404 lines
*= linesPerAction
;
406 ScrollTo(topLineNew
);
412 void ScintillaWX::DoSize(int width
, int height
) {
413 PRectangle
rcClient(0,0,width
,height
);
414 SetScrollBarsTo(rcClient
);
418 void ScintillaWX::DoLoseFocus(){
419 SetFocusState(false);
422 void ScintillaWX::DoGainFocus(){
426 void ScintillaWX::DoSysColourChange() {
427 InvalidateStyleData();
430 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
431 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
434 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
435 ButtonUp(pt
, curTime
, ctrl
);
438 void ScintillaWX::DoButtonMove(Point pt
) {
443 void ScintillaWX::DoAddChar(char ch
) {
447 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
449 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
450 if (ctrl
&& key
>= 1 && key
<= 26)
455 case WXK_DOWN
: key
= SCK_DOWN
; break;
456 case WXK_UP
: key
= SCK_UP
; break;
457 case WXK_LEFT
: key
= SCK_LEFT
; break;
458 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
459 case WXK_HOME
: key
= SCK_HOME
; break;
460 case WXK_END
: key
= SCK_END
; break;
461 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
462 case WXK_NEXT
: key
= SCK_NEXT
; break;
463 case WXK_DELETE
: key
= SCK_DELETE
; break;
464 case WXK_INSERT
: key
= SCK_INSERT
; break;
465 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
466 case WXK_BACK
: key
= SCK_BACK
; break;
467 case WXK_TAB
: key
= SCK_TAB
; break;
468 case WXK_RETURN
: key
= SCK_RETURN
; break;
471 key
= SCK_ADD
; break;
473 case WXK_NUMPAD_SUBTRACT
:
474 key
= SCK_SUBTRACT
; break;
476 case WXK_NUMPAD_DIVIDE
:
477 key
= SCK_DIVIDE
; break;
478 case WXK_CONTROL
: key
= 0; break;
479 case WXK_ALT
: key
= 0; break;
480 case WXK_SHIFT
: key
= 0; break;
481 case WXK_MENU
: key
= 0; break;
484 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
493 void ScintillaWX::DoCommand(int ID
) {
498 void ScintillaWX::DoContextMenu(Point pt
) {
502 void ScintillaWX::DoOnListBox() {
503 AutoCompleteCompleted();
506 //----------------------------------------------------------------------
508 #if wxUSE_DRAG_AND_DROP
509 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
510 SetDragPosition(invalidPosition
);
512 // Send an event to allow the drag details to be changed
513 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
514 evt
.SetEventObject(stc
);
515 evt
.SetDragResult(dragResult
);
518 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
519 evt
.SetDragText(data
);
520 stc
->GetEventHandler()->ProcessEvent(evt
);
522 dragResult
= evt
.GetDragResult();
523 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
524 DropAt(evt
.GetPosition(),
526 dragResult
== wxDragMove
,
527 FALSE
); // TODO: rectangular?
534 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
540 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
541 SetDragPosition(PositionFromLocation(Point(x
, y
)));
543 // Send an event to allow the drag result to be changed
544 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
545 evt
.SetEventObject(stc
);
546 evt
.SetDragResult(def
);
549 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
550 stc
->GetEventHandler()->ProcessEvent(evt
);
552 dragResult
= evt
.GetDragResult();
557 void ScintillaWX::DoDragLeave() {
558 SetDragPosition(invalidPosition
);
561 //----------------------------------------------------------------------
563 // Redraw all of text area. This paint will not be abandoned.
564 void ScintillaWX::FullPaint() {
565 paintState
= painting
;
566 rcPaint
= GetTextRectangle();
567 paintingAllText
= true;
568 wxClientDC
dc(wMain
.GetID());
569 Surface surfaceWindow
;
570 surfaceWindow
.Init(&dc
);
571 Paint(&surfaceWindow
, rcPaint
);
572 surfaceWindow
.Release();
574 // wMain.GetID()->Refresh(FALSE);
576 paintState
= notPainting
;
580 void ScintillaWX::DoScrollToLine(int line
) {
585 void ScintillaWX::DoScrollToColumn(int column
) {
586 HorizontalScrollTo(column
* vs
.spaceWidth
);
591 //----------------------------------------------------------------------
592 //----------------------------------------------------------------------