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 canPaste
= 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() {
268 wxTextDataObject data
;
271 wxTheClipboard
->Open();
272 canPaste
= wxTheClipboard
->GetData(data
);
273 wxTheClipboard
->Close();
278 void ScintillaWX::CreateCallTipWindow(PRectangle
) {
279 ct
.wCallTip
= new wxSTCCallTip(wMain
.GetID(), -1, &ct
);
280 ct
.wDraw
= ct
.wCallTip
;
284 void ScintillaWX::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
286 popup
.GetID()->AppendSeparator();
288 popup
.GetID()->Append(cmd
, label
);
291 popup
.GetID()->Enable(cmd
, enabled
);
295 void ScintillaWX::ClaimSelection() {
300 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
304 long ScintillaWX::WndProc(unsigned int iMessage
, unsigned long wParam
, long lParam
) {
305 // switch (iMessage) {
307 // return CanPaste();
309 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
316 //----------------------------------------------------------------------
319 void ScintillaWX::DoPaint(wxDC
* dc
, wxRect rect
) {
321 paintState
= painting
;
322 Surface surfaceWindow
;
323 surfaceWindow
.Init(dc
);
324 PRectangle rcPaint
= PRectangleFromwxRect(rect
);
326 Paint(&surfaceWindow
, rcPaint
);
328 surfaceWindow
.Release();
329 if (paintState
== paintAbandoned
) {
330 // Painting area was insufficient to cover new styling or brace highlight positions
333 paintState
= notPainting
;
335 // On wxGTK the editor window paints can overwrite the listbox...
337 ((wxWindow
*)ac
.lb
.GetID())->Refresh(TRUE
);
342 void ScintillaWX::DoHScroll(int type
, int pos
) {
344 if (type
== wxEVT_SCROLLWIN_LINEUP
)
345 xPos
-= H_SCROLL_STEP
;
346 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
347 xPos
+= H_SCROLL_STEP
;
348 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
349 xPos
-= H_SCROLL_PAGE
;
350 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
351 xPos
+= H_SCROLL_PAGE
;
352 else if (type
== wxEVT_SCROLLWIN_TOP
)
354 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
356 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
359 HorizontalScrollTo(xPos
);
362 void ScintillaWX::DoVScroll(int type
, int pos
) {
363 int topLineNew
= topLine
;
364 if (type
== wxEVT_SCROLLWIN_LINEUP
)
366 else if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
368 else if (type
== wxEVT_SCROLLWIN_PAGEUP
)
369 topLineNew
-= LinesToScroll();
370 else if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
371 topLineNew
+= LinesToScroll();
372 else if (type
== wxEVT_SCROLLWIN_TOP
)
374 else if (type
== wxEVT_SCROLLWIN_BOTTOM
)
375 topLineNew
= MaxScrollPos();
376 else if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
379 ScrollTo(topLineNew
);
383 void ScintillaWX::DoMouseWheel(int rotation
, int delta
, int linesPerAction
, int ctrlDown
) {
384 int topLineNew
= topLine
;
387 if (ctrlDown
) { // Zoom the fonts if Ctrl key down
389 KeyCommand(SCI_ZOOMIN
);
392 KeyCommand(SCI_ZOOMOUT
);
395 else { // otherwise just scroll the window
396 wheelRotation
+= rotation
;
397 lines
= wheelRotation
/ delta
;
398 wheelRotation
-= lines
* delta
;
400 lines
*= linesPerAction
;
402 ScrollTo(topLineNew
);
408 void ScintillaWX::DoSize(int width
, int height
) {
409 PRectangle
rcClient(0,0,width
,height
);
410 SetScrollBarsTo(rcClient
);
414 void ScintillaWX::DoLoseFocus(){
415 SetFocusState(false);
418 void ScintillaWX::DoGainFocus(){
422 void ScintillaWX::DoSysColourChange() {
423 InvalidateStyleData();
426 void ScintillaWX::DoButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
427 ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
430 void ScintillaWX::DoButtonUp(Point pt
, unsigned int curTime
, bool ctrl
) {
431 ButtonUp(pt
, curTime
, ctrl
);
434 void ScintillaWX::DoButtonMove(Point pt
) {
439 void ScintillaWX::DoAddChar(char ch
) {
443 int ScintillaWX::DoKeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool* consumed
) {
445 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
446 if (ctrl
&& key
>= 1 && key
<= 26)
451 case WXK_DOWN
: key
= SCK_DOWN
; break;
452 case WXK_UP
: key
= SCK_UP
; break;
453 case WXK_LEFT
: key
= SCK_LEFT
; break;
454 case WXK_RIGHT
: key
= SCK_RIGHT
; break;
455 case WXK_HOME
: key
= SCK_HOME
; break;
456 case WXK_END
: key
= SCK_END
; break;
457 case WXK_PRIOR
: key
= SCK_PRIOR
; break;
458 case WXK_NEXT
: key
= SCK_NEXT
; break;
459 case WXK_DELETE
: key
= SCK_DELETE
; break;
460 case WXK_INSERT
: key
= SCK_INSERT
; break;
461 case WXK_ESCAPE
: key
= SCK_ESCAPE
; break;
462 case WXK_BACK
: key
= SCK_BACK
; break;
463 case WXK_TAB
: key
= SCK_TAB
; break;
464 case WXK_RETURN
: key
= SCK_RETURN
; break;
467 key
= SCK_ADD
; break;
469 case WXK_NUMPAD_SUBTRACT
:
470 key
= SCK_SUBTRACT
; break;
472 case WXK_NUMPAD_DIVIDE
:
473 key
= SCK_DIVIDE
; break;
474 case WXK_CONTROL
: key
= 0; break;
475 case WXK_ALT
: key
= 0; break;
476 case WXK_SHIFT
: key
= 0; break;
477 case WXK_MENU
: key
= 0; break;
480 int rv
= KeyDown(key
, shift
, ctrl
, alt
, consumed
);
489 void ScintillaWX::DoCommand(int ID
) {
494 void ScintillaWX::DoContextMenu(Point pt
) {
498 void ScintillaWX::DoOnListBox() {
499 AutoCompleteCompleted();
502 //----------------------------------------------------------------------
504 bool ScintillaWX::DoDropText(long x
, long y
, const wxString
& data
) {
505 SetDragPosition(invalidPosition
);
507 // Send an event to allow the drag details to be changed
508 wxStyledTextEvent
evt(wxEVT_STC_DO_DROP
, stc
->GetId());
509 evt
.SetEventObject(stc
);
510 evt
.SetDragResult(dragResult
);
513 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
514 evt
.SetDragText(data
);
515 stc
->GetEventHandler()->ProcessEvent(evt
);
517 dragResult
= evt
.GetDragResult();
518 if (dragResult
== wxDragMove
|| dragResult
== wxDragCopy
) {
519 DropAt(evt
.GetPosition(),
521 dragResult
== wxDragMove
,
522 FALSE
); // TODO: rectangular?
529 wxDragResult
ScintillaWX::DoDragEnter(wxCoord x
, wxCoord y
, wxDragResult def
) {
535 wxDragResult
ScintillaWX::DoDragOver(wxCoord x
, wxCoord y
, wxDragResult def
) {
536 SetDragPosition(PositionFromLocation(Point(x
, y
)));
538 // Send an event to allow the drag result to be changed
539 wxStyledTextEvent
evt(wxEVT_STC_DRAG_OVER
, stc
->GetId());
540 evt
.SetEventObject(stc
);
541 evt
.SetDragResult(def
);
544 evt
.SetPosition(PositionFromLocation(Point(x
,y
)));
545 stc
->GetEventHandler()->ProcessEvent(evt
);
547 dragResult
= evt
.GetDragResult();
552 void ScintillaWX::DoDragLeave() {
553 SetDragPosition(invalidPosition
);
556 //----------------------------------------------------------------------
558 // Redraw all of text area. This paint will not be abandoned.
559 void ScintillaWX::FullPaint() {
560 paintState
= painting
;
561 rcPaint
= GetTextRectangle();
562 paintingAllText
= true;
563 wxClientDC
dc(wMain
.GetID());
564 Surface surfaceWindow
;
565 surfaceWindow
.Init(&dc
);
566 Paint(&surfaceWindow
, rcPaint
);
567 surfaceWindow
.Release();
569 // wMain.GetID()->Refresh(FALSE);
571 paintState
= notPainting
;
575 void ScintillaWX::DoScrollToLine(int line
) {
580 void ScintillaWX::DoScrollToColumn(int column
) {
581 HorizontalScrollTo(column
* vs
.spaceWidth
);
586 //----------------------------------------------------------------------
587 //----------------------------------------------------------------------