]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/stc/ScintillaWX.cpp
don't eat all events if m_maxLength is 0: this means no limit according to the docs...
[wxWidgets.git] / contrib / src / stc / ScintillaWX.cpp
CommitLineData
9ce192d4
RD
1////////////////////////////////////////////////////////////////////////////
2// Name: ScintillaWX.cxx
be5a51fb 3// Purpose: A wxWidgets implementation of Scintilla. A class derived
9ce192d4
RD
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.
8//
9// Author: Robin Dunn
10//
11// Created: 13-Jan-2000
12// RCS-ID: $Id$
13// Copyright: (c) 2000 by Total Control Software
14// Licence: wxWindows license
15/////////////////////////////////////////////////////////////////////////////
16
d6655166
WS
17#include "wx/wx.h"
18#include "wx/textbuf.h"
19#include "wx/dataobj.h"
20#include "wx/clipbrd.h"
21#include "wx/dnd.h"
f9ee2e27 22
9ce192d4 23#include "ScintillaWX.h"
e14d10b0 24#include "ExternalLexer.h"
9ce192d4 25#include "wx/stc/stc.h"
1a2fb4cd 26#include "PlatWX.h"
9ce192d4 27
249b31b5
WS
28#ifdef __WXMSW__
29 // GetHwndOf()
d6655166 30 #include "wx/msw/private.h"
249b31b5
WS
31#endif
32
9ce192d4
RD
33//----------------------------------------------------------------------
34// Helper classes
35
36class wxSTCTimer : public wxTimer {
37public:
38 wxSTCTimer(ScintillaWX* swx) {
39 this->swx = swx;
40 }
41
42 void Notify() {
43 swx->DoTick();
44 }
45
46private:
47 ScintillaWX* swx;
48};
49
50
1bc32508 51#if wxUSE_DRAG_AND_DROP
9ce192d4
RD
52bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
53 return swx->DoDropText(x, y, data);
54}
55
56wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) {
57 return swx->DoDragEnter(x, y, def);
58}
59
60wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
61 return swx->DoDragOver(x, y, def);
62}
63
64void wxSTCDropTarget::OnLeave() {
65 swx->DoDragLeave();
66}
1bc32508 67#endif
9ce192d4
RD
68
69
9c46ea66 70#if wxUSE_POPUPWIN && wxSTC_USE_POPUP
769a9cb2
RD
71#include <wx/popupwin.h>
72#define wxSTCCallTipBase wxPopupWindow
73#define param2 wxBORDER_NONE // popup's 2nd param is flags
74#else
75#define wxSTCCallTipBase wxWindow
9e730a78 76#define param2 -1 // wxWindow's 2nd param is ID
769a9cb2
RD
77#endif
78
8a793194
RD
79#include <wx/dcbuffer.h>
80
769a9cb2 81class wxSTCCallTip : public wxSTCCallTipBase {
f6bcfd97 82public:
9e730a78
RD
83 wxSTCCallTip(wxWindow* parent, CallTip* ct, ScintillaWX* swx)
84 : wxSTCCallTipBase(parent, param2),
7e126a07 85 m_ct(ct), m_swx(swx), m_cx(wxDefaultCoord), m_cy(wxDefaultCoord)
f6bcfd97 86 {
f6bcfd97
BP
87 }
88
ef08ab52 89 ~wxSTCCallTip() {
8a793194
RD
90#if wxUSE_POPUPWIN && wxSTC_USE_POPUP && defined(__WXGTK__)
91 wxRect rect = GetRect();
ee8bee6c
RD
92 rect.x = m_cx;
93 rect.y = m_cy;
8a793194
RD
94 GetParent()->Refresh(false, &rect);
95#endif
ef08ab52
RD
96 }
97
7e126a07 98 bool AcceptsFocus() const { return false; }
9e730a78 99
5f9eb69c 100 void OnPaint(wxPaintEvent& WXUNUSED(evt)) {
8a793194 101 wxBufferedPaintDC dc(this);
1a2fb4cd 102 Surface* surfaceWindow = Surface::Allocate();
9e730a78 103 surfaceWindow->Init(&dc, m_ct->wDraw.GetID());
1a2fb4cd 104 m_ct->PaintCT(surfaceWindow);
9e730a78 105 surfaceWindow->Release();
1a2fb4cd 106 delete surfaceWindow;
f6bcfd97
BP
107 }
108
267484bc
RD
109 void OnFocus(wxFocusEvent& event) {
110 GetParent()->SetFocus();
111 event.Skip();
112 }
113
9e730a78
RD
114 void OnLeftDown(wxMouseEvent& event) {
115 wxPoint pt = event.GetPosition();
116 Point p(pt.x, pt.y);
117 m_ct->MouseClick(p);
118 m_swx->CallTipClick();
119 }
120
9c46ea66 121#if wxUSE_POPUPWIN && wxSTC_USE_POPUP
769a9cb2
RD
122 virtual void DoSetSize(int x, int y,
123 int width, int height,
124 int sizeFlags = wxSIZE_AUTO) {
7e126a07 125 if (x != wxDefaultCoord) {
ee8bee6c 126 m_cx = x;
769a9cb2 127 GetParent()->ClientToScreen(&x, NULL);
ee8bee6c 128 }
7e126a07 129 if (y != wxDefaultCoord) {
ee8bee6c 130 m_cy = y;
769a9cb2 131 GetParent()->ClientToScreen(NULL, &y);
ee8bee6c 132 }
769a9cb2
RD
133 wxSTCCallTipBase::DoSetSize(x, y, width, height, sizeFlags);
134 }
135#endif
136
ee8bee6c
RD
137 wxPoint GetMyPosition() {
138 return wxPoint(m_cx, m_cy);
139 }
7e126a07 140
769a9cb2 141private:
9e730a78
RD
142 CallTip* m_ct;
143 ScintillaWX* m_swx;
ee8bee6c 144 int m_cx, m_cy;
f6bcfd97
BP
145 DECLARE_EVENT_TABLE()
146};
147
769a9cb2 148BEGIN_EVENT_TABLE(wxSTCCallTip, wxSTCCallTipBase)
f6bcfd97 149 EVT_PAINT(wxSTCCallTip::OnPaint)
267484bc 150 EVT_SET_FOCUS(wxSTCCallTip::OnFocus)
9c46ea66 151 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown)
f6bcfd97 152END_EVENT_TABLE()
9ce192d4 153
769a9cb2 154
610c50a2
RD
155//----------------------------------------------------------------------
156
c54e5eb0 157#if wxUSE_DATAOBJ
610c50a2
RD
158static wxTextFileType wxConvertEOLMode(int scintillaMode)
159{
160 wxTextFileType type;
7e126a07 161
610c50a2
RD
162 switch (scintillaMode) {
163 case wxSTC_EOL_CRLF:
164 type = wxTextFileType_Dos;
165 break;
7e126a07 166
610c50a2
RD
167 case wxSTC_EOL_CR:
168 type = wxTextFileType_Mac;
169 break;
7e126a07 170
610c50a2
RD
171 case wxSTC_EOL_LF:
172 type = wxTextFileType_Unix;
173 break;
7e126a07 174
610c50a2
RD
175 default:
176 type = wxTextBuffer::typeDefault;
177 break;
178 }
179 return type;
180}
c54e5eb0 181#endif // wxUSE_DATAOBJ
7e126a07 182
610c50a2 183
9ce192d4
RD
184//----------------------------------------------------------------------
185// Constructor/Destructor
186
187
188ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
9e730a78 189 capturedMouse = false;
b0d0494f 190 focusEvent = false;
9ce192d4 191 wMain = win;
9ce192d4 192 stc = win;
37d62433 193 wheelRotation = 0;
9ce192d4 194 Initialise();
68985f0e
RD
195#ifdef __WXMSW__
196 sysCaretBitmap = 0;
197 sysCaretWidth = 0;
198 sysCaretHeight = 0;
199#endif
9ce192d4
RD
200}
201
202
203ScintillaWX::~ScintillaWX() {
92dda0f7 204 Finalise();
9ce192d4
RD
205}
206
207//----------------------------------------------------------------------
208// base class virtuals
209
210
211void ScintillaWX::Initialise() {
212 //ScintillaBase::Initialise();
1bc32508 213#if wxUSE_DRAG_AND_DROP
9eb662e9
RD
214 dropTarget = new wxSTCDropTarget;
215 dropTarget->SetScintilla(this);
216 stc->SetDropTarget(dropTarget);
1bc32508 217#endif
0eaf23ed
RD
218#ifdef __WXMAC__
219 vs.extraFontFlag = false; // UseAntiAliasing
220#else
221 vs.extraFontFlag = true; // UseAntiAliasing
222#endif
9ce192d4
RD
223}
224
225
226void ScintillaWX::Finalise() {
227 ScintillaBase::Finalise();
8e54aaed
RD
228 SetTicking(false);
229 SetIdle(false);
d429e187 230 DestroySystemCaret();
9ce192d4
RD
231}
232
233
234void ScintillaWX::StartDrag() {
1bc32508 235#if wxUSE_DRAG_AND_DROP
0c5b83b0 236 wxString dragText = stc2wx(drag.s, drag.len);
a29a241f
RD
237
238 // Send an event to allow the drag text to be changed
239 wxStyledTextEvent evt(wxEVT_STC_START_DRAG, stc->GetId());
240 evt.SetEventObject(stc);
241 evt.SetDragText(dragText);
7e126a07 242 evt.SetDragAllowMove(true);
a29a241f
RD
243 evt.SetPosition(wxMin(stc->GetSelectionStart(),
244 stc->GetSelectionEnd()));
245 stc->GetEventHandler()->ProcessEvent(evt);
246 dragText = evt.GetDragText();
247
248 if (dragText.Length()) {
5fa4613c 249 wxDropSource source(stc);
a29a241f
RD
250 wxTextDataObject data(dragText);
251 wxDragResult result;
252
253 source.SetData(data);
7e126a07 254 dropWentOutside = true;
a29a241f
RD
255 result = source.DoDragDrop(evt.GetDragAllowMove());
256 if (result == wxDragMove && dropWentOutside)
257 ClearSelection();
7e126a07 258 inDragDrop = false;
a29a241f
RD
259 SetDragPosition(invalidPosition);
260 }
1bc32508 261#endif
9ce192d4
RD
262}
263
264
8e54aaed
RD
265bool ScintillaWX::SetIdle(bool on) {
266 if (idler.state != on) {
267 // connect or disconnect the EVT_IDLE handler
7e126a07
WS
268 if (on)
269 stc->Connect(wxID_ANY, wxEVT_IDLE,
5f9eb69c 270 (wxObjectEventFunction) (wxEventFunction) (wxIdleEventFunction) &wxStyledTextCtrl::OnIdle);
8e54aaed 271 else
7e126a07 272 stc->Disconnect(wxID_ANY, wxEVT_IDLE,
5f9eb69c 273 (wxObjectEventFunction) (wxEventFunction) (wxIdleEventFunction) &wxStyledTextCtrl::OnIdle);
8e54aaed
RD
274 idler.state = on;
275 }
276 return idler.state;
277}
278
7e126a07 279
9ce192d4
RD
280void ScintillaWX::SetTicking(bool on) {
281 wxSTCTimer* steTimer;
282 if (timer.ticking != on) {
283 timer.ticking = on;
284 if (timer.ticking) {
285 steTimer = new wxSTCTimer(this);
286 steTimer->Start(timer.tickSize);
1a2fb4cd 287 timer.tickerID = steTimer;
9ce192d4
RD
288 } else {
289 steTimer = (wxSTCTimer*)timer.tickerID;
290 steTimer->Stop();
291 delete steTimer;
292 timer.tickerID = 0;
293 }
294 }
295 timer.ticksToWait = caret.period;
296}
297
298
299void ScintillaWX::SetMouseCapture(bool on) {
8e54aaed
RD
300 if (mouseDownCaptures) {
301 if (on && !capturedMouse)
302 stc->CaptureMouse();
303 else if (!on && capturedMouse && stc->HasCapture())
304 stc->ReleaseMouse();
305 capturedMouse = on;
306 }
9ce192d4
RD
307}
308
309
310bool ScintillaWX::HaveMouseCapture() {
9e730a78 311 return capturedMouse;
9ce192d4
RD
312}
313
314
315void ScintillaWX::ScrollText(int linesToMove) {
316 int dy = vs.lineHeight * (linesToMove);
5fa4613c
RD
317 stc->ScrollWindow(0, dy);
318 stc->Update();
9ce192d4
RD
319}
320
321void ScintillaWX::SetVerticalScrollPos() {
5fa4613c
RD
322 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
323 stc->SetScrollPos(wxVERTICAL, topLine);
324 }
325 else { // otherwise use the one that's been given to us
326 stc->m_vScrollBar->SetThumbPosition(topLine);
327 }
9ce192d4
RD
328}
329
330void ScintillaWX::SetHorizontalScrollPos() {
5fa4613c
RD
331 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
332 stc->SetScrollPos(wxHORIZONTAL, xOffset);
333 }
334 else { // otherwise use the one that's been given to us
335 stc->m_hScrollBar->SetThumbPosition(xOffset);
336 }
9ce192d4
RD
337}
338
1bc54e32 339
a834585d 340const int H_SCROLL_STEP = 20;
9ce192d4
RD
341
342bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) {
343 bool modified = false;
9ce192d4 344
1bc54e32
RD
345 int vertEnd = nMax;
346 if (!verticalScrollBarVisible)
347 vertEnd = 0;
348
a834585d 349 // Check the vertical scrollbar
5fa4613c
RD
350 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
351 int sbMax = stc->GetScrollRange(wxVERTICAL);
352 int sbThumb = stc->GetScrollThumb(wxVERTICAL);
353 int sbPos = stc->GetScrollPos(wxVERTICAL);
1bc54e32
RD
354 if (sbMax != vertEnd || sbThumb != nPage) {
355 stc->SetScrollbar(wxVERTICAL, sbPos, nPage, vertEnd+1);
5fa4613c
RD
356 modified = true;
357 }
358 }
359 else { // otherwise use the one that's been given to us
360 int sbMax = stc->m_vScrollBar->GetRange();
361 int sbPage = stc->m_vScrollBar->GetPageSize();
362 int sbPos = stc->m_vScrollBar->GetThumbPosition();
1bc54e32
RD
363 if (sbMax != vertEnd || sbPage != nPage) {
364 stc->m_vScrollBar->SetScrollbar(sbPos, nPage, vertEnd+1, nPage);
5fa4613c
RD
365 modified = true;
366 }
9ce192d4
RD
367 }
368
5fa4613c 369
a834585d
RD
370 // Check the horizontal scrollbar
371 PRectangle rcText = GetTextRectangle();
372 int horizEnd = scrollWidth;
373 if (horizEnd < 0)
374 horizEnd = 0;
375 if (!horizontalScrollBarVisible || (wrapState != eWrapNone))
376 horizEnd = 0;
377 int pageWidth = rcText.Width();
378
379 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
380 int sbMax = stc->GetScrollRange(wxHORIZONTAL);
381 int sbThumb = stc->GetScrollThumb(wxHORIZONTAL);
382 int sbPos = stc->GetScrollPos(wxHORIZONTAL);
383 if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) {
1bc54e32 384 stc->SetScrollbar(wxHORIZONTAL, sbPos, pageWidth, horizEnd);
a834585d
RD
385 modified = true;
386 if (scrollWidth < pageWidth) {
387 HorizontalScrollTo(0);
5fa4613c
RD
388 }
389 }
a834585d
RD
390 }
391 else { // otherwise use the one that's been given to us
392 int sbMax = stc->m_hScrollBar->GetRange();
393 int sbThumb = stc->m_hScrollBar->GetPageSize();
394 int sbPos = stc->m_hScrollBar->GetThumbPosition();
395 if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) {
1bc54e32 396 stc->m_hScrollBar->SetScrollbar(sbPos, pageWidth, horizEnd, pageWidth);
a834585d
RD
397 modified = true;
398 if (scrollWidth < pageWidth) {
399 HorizontalScrollTo(0);
5fa4613c 400 }
3928c4fd 401 }
9ce192d4 402 }
a834585d 403
9ce192d4
RD
404 return modified;
405}
406
407
408void ScintillaWX::NotifyChange() {
409 stc->NotifyChange();
410}
411
412
413void ScintillaWX::NotifyParent(SCNotification scn) {
414 stc->NotifyParent(&scn);
415}
416
417
b0d0494f
RD
418// This method is overloaded from ScintillaBase in order to prevent the
419// AutoComplete window from being destroyed when it gets the focus. There is
420// a side effect that the AutoComp will also not be destroyed when switching
421// to another window, but I think that is okay.
422void ScintillaWX::CancelModes() {
423 if (! focusEvent)
424 AutoCompleteCancel();
425 ct.CallTipCancel();
426 Editor::CancelModes();
427}
428
429
9ce192d4
RD
430
431void ScintillaWX::Copy() {
432 if (currentPos != anchor) {
b8b0e402
RD
433 SelectionText st;
434 CopySelectionRange(&st);
e14d10b0 435 CopyToClipboard(st);
9ce192d4
RD
436 }
437}
438
439
440void ScintillaWX::Paste() {
441 pdoc->BeginUndoAction();
442 ClearSelection();
443
c54e5eb0 444#if wxUSE_DATAOBJ
9ce192d4 445 wxTextDataObject data;
7e126a07 446 bool gotData = false;
9ce192d4 447
45c6a927 448 if (wxTheClipboard->Open()) {
7e126a07 449 wxTheClipboard->UsePrimarySelection(false);
45c6a927
RD
450 gotData = wxTheClipboard->GetData(data);
451 wxTheClipboard->Close();
452 }
e26c0634 453 if (gotData) {
610c50a2
RD
454 wxString text = wxTextBuffer::Translate(data.GetText(),
455 wxConvertEOLMode(pdoc->eolMode));
456 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
10ef30eb
RD
457 int len = strlen(buf);
458 pdoc->InsertString(currentPos, buf, len);
9ce192d4
RD
459 SetEmptySelection(currentPos + len);
460 }
c54e5eb0 461#endif // wxUSE_DATAOBJ
9ce192d4
RD
462
463 pdoc->EndUndoAction();
464 NotifyChange();
465 Redraw();
466}
467
468
e14d10b0 469void ScintillaWX::CopyToClipboard(const SelectionText& st) {
c54e5eb0 470#if wxUSE_CLIPBOARD
e14d10b0 471 if (wxTheClipboard->Open()) {
7e126a07 472 wxTheClipboard->UsePrimarySelection(false);
98719eab 473 wxString text = wxTextBuffer::Translate(stc2wx(st.s, st.len-1));
e14d10b0
RD
474 wxTheClipboard->SetData(new wxTextDataObject(text));
475 wxTheClipboard->Close();
476 }
c54e5eb0
WS
477#else
478 wxUnusedVar(st);
479#endif // wxUSE_CLIPBOARD
e14d10b0
RD
480}
481
482
9ce192d4 483bool ScintillaWX::CanPaste() {
c54e5eb0 484#if wxUSE_CLIPBOARD
7e126a07 485 bool canPaste = false;
45c6a927 486 bool didOpen;
9ce192d4 487
0b887a21 488 if (Editor::CanPaste()) {
88a8b04e
RD
489 didOpen = !wxTheClipboard->IsOpened();
490 if ( didOpen )
0b887a21 491 wxTheClipboard->Open();
45c6a927 492
0b887a21 493 if (wxTheClipboard->IsOpened()) {
7e126a07 494 wxTheClipboard->UsePrimarySelection(false);
0b887a21
RD
495 canPaste = wxTheClipboard->IsSupported(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT);
496 if (didOpen)
497 wxTheClipboard->Close();
498 }
6ba338ec 499 }
9ce192d4 500 return canPaste;
c54e5eb0
WS
501#else
502 return false;
503#endif // wxUSE_CLIPBOARD
9ce192d4
RD
504}
505
506void ScintillaWX::CreateCallTipWindow(PRectangle) {
9e730a78
RD
507 if (! ct.wCallTip.Created() ) {
508 ct.wCallTip = new wxSTCCallTip(stc, &ct, this);
509 ct.wDraw = ct.wCallTip;
510 }
9ce192d4
RD
511}
512
513
514void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) {
515 if (!label[0])
1a2fb4cd 516 ((wxMenu*)popup.GetID())->AppendSeparator();
9ce192d4 517 else
1e545382 518 ((wxMenu*)popup.GetID())->Append(cmd, wxGetTranslation(stc2wx(label)));
9ce192d4
RD
519
520 if (!enabled)
1a2fb4cd 521 ((wxMenu*)popup.GetID())->Enable(cmd, enabled);
9ce192d4
RD
522}
523
524
2b5f62a0 525// This is called by the Editor base class whenever something is selected
9ce192d4 526void ScintillaWX::ClaimSelection() {
f3030ba7
RD
527#if 0
528 // Until wxGTK is able to support using both the primary selection and the
529 // clipboard at the same time I think it causes more problems than it is
530 // worth to implement this method. Selecting text should not clear the
531 // clipboard. --Robin
2b5f62a0
VZ
532#ifdef __WXGTK__
533 // Put the selected text in the PRIMARY selection
534 if (currentPos != anchor) {
535 SelectionText st;
536 CopySelectionRange(&st);
537 if (wxTheClipboard->Open()) {
7e126a07 538 wxTheClipboard->UsePrimarySelection(true);
2b5f62a0
VZ
539 wxString text = stc2wx(st.s, st.len);
540 wxTheClipboard->SetData(new wxTextDataObject(text));
7e126a07 541 wxTheClipboard->UsePrimarySelection(false);
2b5f62a0
VZ
542 wxTheClipboard->Close();
543 }
544 }
545#endif
f3030ba7 546#endif
9ce192d4
RD
547}
548
549
d429e187
RD
550void ScintillaWX::UpdateSystemCaret() {
551#ifdef __WXMSW__
552 if (hasFocus) {
553 if (HasCaretSizeChanged()) {
554 DestroySystemCaret();
555 CreateSystemCaret();
556 }
557 Point pos = LocationFromPosition(currentPos);
558 ::SetCaretPos(pos.x, pos.y);
559 }
560#endif
561}
562
563
564bool ScintillaWX::HasCaretSizeChanged() {
565#ifdef __WXMSW__
566 if (( (0 != vs.caretWidth) && (sysCaretWidth != vs.caretWidth) )
567 || (0 != vs.lineHeight) && (sysCaretHeight != vs.lineHeight)) {
568 return true;
569 }
570#endif
571 return false;
572}
573
574bool ScintillaWX::CreateSystemCaret() {
575#ifdef __WXMSW__
576 sysCaretWidth = vs.caretWidth;
577 if (0 == sysCaretWidth) {
578 sysCaretWidth = 1;
579 }
580 sysCaretHeight = vs.lineHeight;
581 int bitmapSize = (((sysCaretWidth + 15) & ~15) >> 3) * sysCaretHeight;
582 char *bits = new char[bitmapSize];
583 memset(bits, 0, bitmapSize);
584 sysCaretBitmap = ::CreateBitmap(sysCaretWidth, sysCaretHeight, 1,
585 1, reinterpret_cast<BYTE *>(bits));
586 delete [] bits;
587 BOOL retval = ::CreateCaret(GetHwndOf(stc), sysCaretBitmap,
588 sysCaretWidth, sysCaretHeight);
589 ::ShowCaret(GetHwndOf(stc));
590 return retval != 0;
591#else
592 return false;
593#endif
594}
595
596bool ScintillaWX::DestroySystemCaret() {
597#ifdef __WXMSW__
598 ::HideCaret(GetHwndOf(stc));
599 BOOL retval = ::DestroyCaret();
600 if (sysCaretBitmap) {
601 ::DeleteObject(sysCaretBitmap);
602 sysCaretBitmap = 0;
603 }
604 return retval != 0;
605#else
606 return false;
607#endif
608}
609
610
611//----------------------------------------------------------------------
612
613
d134f170 614long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
9ce192d4
RD
615 return 0;
616}
617
d134f170 618long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) {
fdec65df
RD
619 switch (iMessage) {
620 case SCI_CALLTIPSHOW: {
621 // NOTE: This is copied here from scintilla/src/ScintillaBase.cxx
9e730a78
RD
622 // because of the little tweak that needs done below for wxGTK.
623 // When updating new versions double check that this is still
624 // needed, and that any new code there is copied here too.
625 Point pt = LocationFromPosition(wParam);
626 char* defn = reinterpret_cast<char *>(lParam);
fdec65df 627 AutoCompleteCancel();
9e730a78
RD
628 pt.y += vs.lineHeight;
629 PRectangle rc = ct.CallTipStart(currentPos, pt,
630 defn,
631 vs.styles[STYLE_DEFAULT].fontName,
632 vs.styles[STYLE_DEFAULT].sizeZoomed,
a33203cb
RD
633 CodePage(),
634 vs.styles[STYLE_DEFAULT].characterSet,
9e730a78
RD
635 wMain);
636 // If the call-tip window would be out of the client
637 // space, adjust so it displays above the text.
638 PRectangle rcClient = GetClientRectangle();
639 if (rc.bottom > rcClient.bottom) {
fdec65df 640#ifdef __WXGTK__
9e730a78 641 int offset = int(vs.lineHeight * 1.25) + rc.Height();
fdec65df 642#else
9e730a78 643 int offset = vs.lineHeight + rc.Height();
fdec65df 644#endif
9e730a78
RD
645 rc.top -= offset;
646 rc.bottom -= offset;
fdec65df 647 }
9e730a78
RD
648 // Now display the window.
649 CreateCallTipWindow(rc);
650 ct.wCallTip.SetPositionRelative(rc, wMain);
651 ct.wCallTip.Show();
fdec65df 652 break;
9e730a78 653 }
fdec65df 654
e14d10b0 655#ifdef SCI_LEXER
7e126a07 656 case SCI_LOADLEXERLIBRARY:
e14d10b0
RD
657 LexerManager::GetInstance()->Load((const char*)lParam);
658 break;
659#endif
719493e1 660
fdec65df
RD
661 default:
662 return ScintillaBase::WndProc(iMessage, wParam, lParam);
663 }
664 return 0;
9ce192d4
RD
665}
666
667
668
669//----------------------------------------------------------------------
670// Event delegates
671
672void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
673
674 paintState = painting;
1a2fb4cd 675 Surface* surfaceWindow = Surface::Allocate();
9e730a78
RD
676 surfaceWindow->Init(dc, wMain.GetID());
677 rcPaint = PRectangleFromwxRect(rect);
678 PRectangle rcClient = GetClientRectangle();
679 paintingAllText = rcPaint.Contains(rcClient);
680
9ce192d4 681 dc->BeginDrawing();
9e730a78 682 ClipChildren(*dc, rcPaint);
1a2fb4cd 683 Paint(surfaceWindow, rcPaint);
9e730a78 684
1a2fb4cd 685 delete surfaceWindow;
9ce192d4 686 if (paintState == paintAbandoned) {
cadea444
RD
687 // Painting area was insufficient to cover new styling or brace
688 // highlight positions
ab0f5fa6 689 FullPaint();
9ce192d4
RD
690 }
691 paintState = notPainting;
cadea444 692 dc->EndDrawing();
9ce192d4
RD
693}
694
695
696void ScintillaWX::DoHScroll(int type, int pos) {
697 int xPos = xOffset;
a834585d
RD
698 PRectangle rcText = GetTextRectangle();
699 int pageWidth = rcText.Width() * 2 / 3;
5fa4613c 700 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
9ce192d4 701 xPos -= H_SCROLL_STEP;
5fa4613c 702 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
9ce192d4 703 xPos += H_SCROLL_STEP;
5fa4613c 704 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
a834585d
RD
705 xPos -= pageWidth;
706 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN) {
707 xPos += pageWidth;
708 if (xPos > scrollWidth - rcText.Width()) {
709 xPos = scrollWidth - rcText.Width();
710 }
711 }
5fa4613c 712 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
9ce192d4 713 xPos = 0;
5fa4613c 714 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
a834585d 715 xPos = scrollWidth;
5fa4613c 716 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
9ce192d4 717 xPos = pos;
ce1ecc6d 718
9ce192d4
RD
719 HorizontalScrollTo(xPos);
720}
721
722void ScintillaWX::DoVScroll(int type, int pos) {
723 int topLineNew = topLine;
5fa4613c 724 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
9ce192d4 725 topLineNew -= 1;
5fa4613c 726 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
9ce192d4 727 topLineNew += 1;
5fa4613c 728 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
9ce192d4 729 topLineNew -= LinesToScroll();
5fa4613c 730 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
9ce192d4 731 topLineNew += LinesToScroll();
5fa4613c 732 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
9ce192d4 733 topLineNew = 0;
5fa4613c 734 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
9ce192d4 735 topLineNew = MaxScrollPos();
5fa4613c 736 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
9ce192d4 737 topLineNew = pos;
ce1ecc6d 738
9ce192d4
RD
739 ScrollTo(topLineNew);
740}
741
9b9337da
RD
742void ScintillaWX::DoMouseWheel(int rotation, int delta,
743 int linesPerAction, int ctrlDown,
744 bool isPageScroll ) {
37d62433
RD
745 int topLineNew = topLine;
746 int lines;
747
65ec6247
RD
748 if (ctrlDown) { // Zoom the fonts if Ctrl key down
749 if (rotation < 0) {
750 KeyCommand(SCI_ZOOMIN);
751 }
752 else {
753 KeyCommand(SCI_ZOOMOUT);
754 }
755 }
756 else { // otherwise just scroll the window
91f580b2
RD
757 if ( !delta )
758 delta = 120;
65ec6247
RD
759 wheelRotation += rotation;
760 lines = wheelRotation / delta;
761 wheelRotation -= lines * delta;
762 if (lines != 0) {
9b9337da
RD
763 if (isPageScroll)
764 lines = lines * LinesOnScreen(); // lines is either +1 or -1
765 else
766 lines *= linesPerAction;
65ec6247
RD
767 topLineNew -= lines;
768 ScrollTo(topLineNew);
769 }
37d62433
RD
770 }
771}
772
773
88a8b04e 774void ScintillaWX::DoSize(int WXUNUSED(width), int WXUNUSED(height)) {
1a2fb4cd 775 ChangeSize();
9ce192d4
RD
776}
777
778void ScintillaWX::DoLoseFocus(){
b0d0494f 779 focusEvent = true;
65ec6247 780 SetFocusState(false);
b0d0494f 781 focusEvent = false;
d429e187 782 DestroySystemCaret();
9ce192d4
RD
783}
784
785void ScintillaWX::DoGainFocus(){
b0d0494f 786 focusEvent = true;
65ec6247 787 SetFocusState(true);
b0d0494f 788 focusEvent = false;
d429e187
RD
789 DestroySystemCaret();
790 CreateSystemCaret();
9ce192d4
RD
791}
792
793void ScintillaWX::DoSysColourChange() {
794 InvalidateStyleData();
795}
796
2b5f62a0 797void ScintillaWX::DoLeftButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
9ce192d4
RD
798 ButtonDown(pt, curTime, shift, ctrl, alt);
799}
800
2b5f62a0 801void ScintillaWX::DoLeftButtonUp(Point pt, unsigned int curTime, bool ctrl) {
9ce192d4
RD
802 ButtonUp(pt, curTime, ctrl);
803}
804
2b5f62a0 805void ScintillaWX::DoLeftButtonMove(Point pt) {
9ce192d4
RD
806 ButtonMove(pt);
807}
808
2b5f62a0 809#ifdef __WXGTK__
88a8b04e 810void ScintillaWX::DoMiddleButtonUp(Point pt) {
2b5f62a0
VZ
811 // Set the current position to the mouse click point and
812 // then paste in the PRIMARY selection, if any. wxGTK only.
813 int newPos = PositionFromLocation(pt);
8e54aaed 814 MovePositionTo(newPos, noSel, true);
2b5f62a0
VZ
815
816 pdoc->BeginUndoAction();
817 wxTextDataObject data;
7e126a07 818 bool gotData = false;
2b5f62a0 819 if (wxTheClipboard->Open()) {
7e126a07 820 wxTheClipboard->UsePrimarySelection(true);
2b5f62a0 821 gotData = wxTheClipboard->GetData(data);
7e126a07 822 wxTheClipboard->UsePrimarySelection(false);
2b5f62a0
VZ
823 wxTheClipboard->Close();
824 }
825 if (gotData) {
610c50a2
RD
826 wxString text = wxTextBuffer::Translate(data.GetText(),
827 wxConvertEOLMode(pdoc->eolMode));
828 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
2b5f62a0
VZ
829 int len = strlen(buf);
830 pdoc->InsertString(currentPos, buf, len);
831 SetEmptySelection(currentPos + len);
832 }
833 pdoc->EndUndoAction();
834 NotifyChange();
835 Redraw();
836
837 ShowCaretAtCurrentPosition();
838 EnsureCaretVisible();
2b5f62a0 839}
88a8b04e
RD
840#else
841void ScintillaWX::DoMiddleButtonUp(Point WXUNUSED(pt)) {
842}
843#endif
2b5f62a0 844
9ce192d4 845
10ef30eb 846void ScintillaWX::DoAddChar(int key) {
2b5f62a0 847#if wxUSE_UNICODE
fdec65df 848 wxChar wszChars[2];
c8b75e94 849 wszChars[0] = (wxChar)key;
fdec65df
RD
850 wszChars[1] = 0;
851 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(wszChars);
2b5f62a0
VZ
852 AddCharUTF((char*)buf.data(), strlen(buf));
853#else
c8b75e94 854 AddChar((char)key);
2b5f62a0 855#endif
9ce192d4
RD
856}
857
2b5f62a0 858
5fd656d5
RD
859int ScintillaWX::DoKeyDown(const wxKeyEvent& evt, bool* consumed)
860{
861 int key = evt.GetKeyCode();
862 bool shift = evt.ShiftDown(),
863 ctrl = evt.ControlDown(),
864 alt = evt.AltDown();
249b31b5 865
443eb93a 866 if (ctrl && key >= 1 && key <= 26 && key != WXK_BACK)
39178e3b 867 key += 'A' - 1;
39178e3b 868
d134f170 869 switch (key) {
0b9dfbc0
RD
870 case WXK_DOWN: key = SCK_DOWN; break;
871 case WXK_UP: key = SCK_UP; break;
872 case WXK_LEFT: key = SCK_LEFT; break;
873 case WXK_RIGHT: key = SCK_RIGHT; break;
874 case WXK_HOME: key = SCK_HOME; break;
875 case WXK_END: key = SCK_END; break;
c9c50e23 876 case WXK_PAGEUP: // fall through
0b9dfbc0 877 case WXK_PRIOR: key = SCK_PRIOR; break;
c9c50e23 878 case WXK_PAGEDOWN: // fall through
0b9dfbc0
RD
879 case WXK_NEXT: key = SCK_NEXT; break;
880 case WXK_DELETE: key = SCK_DELETE; break;
881 case WXK_INSERT: key = SCK_INSERT; break;
882 case WXK_ESCAPE: key = SCK_ESCAPE; break;
883 case WXK_BACK: key = SCK_BACK; break;
884 case WXK_TAB: key = SCK_TAB; break;
21334a46 885 case WXK_NUMPAD_ENTER: // fall through
0b9dfbc0
RD
886 case WXK_RETURN: key = SCK_RETURN; break;
887 case WXK_ADD: // fall through
888 case WXK_NUMPAD_ADD: key = SCK_ADD; break;
889 case WXK_SUBTRACT: // fall through
890 case WXK_NUMPAD_SUBTRACT: key = SCK_SUBTRACT; break;
891 case WXK_DIVIDE: // fall through
892 case WXK_NUMPAD_DIVIDE: key = SCK_DIVIDE; break;
893 case WXK_CONTROL: key = 0; break;
894 case WXK_ALT: key = 0; break;
895 case WXK_SHIFT: key = 0; break;
896 case WXK_MENU: key = 0; break;
d134f170
RD
897 }
898
88a8b04e 899#ifdef __WXMAC__
5fd656d5 900 if ( evt.MetaDown() ) {
88a8b04e
RD
901 // check for a few common Mac Meta-key combos and remap them to Ctrl
902 // for Scintilla
903 switch ( key ) {
904 case 'Z': // Undo
905 case 'X': // Cut
906 case 'C': // Copy
907 case 'V': // Paste
908 case 'A': // Select All
909 ctrl = true;
910 break;
dd9b0a54
RD
911 }
912 }
88a8b04e 913#endif
7e126a07 914
d6582821 915 int rv = KeyDown(key, shift, ctrl, alt, consumed);
0122b7e3 916
d6582821
RD
917 if (key)
918 return rv;
919 else
920 return 1;
9ce192d4
RD
921}
922
923
924void ScintillaWX::DoCommand(int ID) {
925 Command(ID);
926}
927
928
929void ScintillaWX::DoContextMenu(Point pt) {
752cd08c
RD
930 if (displayPopupMenu)
931 ContextMenu(pt);
9ce192d4
RD
932}
933
f6bcfd97
BP
934void ScintillaWX::DoOnListBox() {
935 AutoCompleteCompleted();
936}
9ce192d4 937
7e126a07 938
8e54aaed
RD
939void ScintillaWX::DoOnIdle(wxIdleEvent& evt) {
940
941 if ( Idle() )
942 evt.RequestMore();
943 else
944 SetIdle(false);
945}
7e126a07 946
9ce192d4
RD
947//----------------------------------------------------------------------
948
1bc32508 949#if wxUSE_DRAG_AND_DROP
9ce192d4
RD
950bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
951 SetDragPosition(invalidPosition);
a29a241f 952
610c50a2
RD
953 wxString text = wxTextBuffer::Translate(data,
954 wxConvertEOLMode(pdoc->eolMode));
7e126a07 955
a29a241f
RD
956 // Send an event to allow the drag details to be changed
957 wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId());
958 evt.SetEventObject(stc);
959 evt.SetDragResult(dragResult);
960 evt.SetX(x);
961 evt.SetY(y);
962 evt.SetPosition(PositionFromLocation(Point(x,y)));
610c50a2 963 evt.SetDragText(text);
a29a241f
RD
964 stc->GetEventHandler()->ProcessEvent(evt);
965
966 dragResult = evt.GetDragResult();
967 if (dragResult == wxDragMove || dragResult == wxDragCopy) {
968 DropAt(evt.GetPosition(),
0c5b83b0 969 wx2stc(evt.GetDragText()),
a29a241f 970 dragResult == wxDragMove,
7e126a07
WS
971 false); // TODO: rectangular?
972 return true;
a29a241f 973 }
7e126a07 974 return false;
9ce192d4
RD
975}
976
977
88a8b04e 978wxDragResult ScintillaWX::DoDragEnter(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) {
b8b0e402
RD
979 dragResult = def;
980 return dragResult;
9ce192d4
RD
981}
982
983
984wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
985 SetDragPosition(PositionFromLocation(Point(x, y)));
a29a241f
RD
986
987 // Send an event to allow the drag result to be changed
988 wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId());
989 evt.SetEventObject(stc);
990 evt.SetDragResult(def);
991 evt.SetX(x);
992 evt.SetY(y);
993 evt.SetPosition(PositionFromLocation(Point(x,y)));
994 stc->GetEventHandler()->ProcessEvent(evt);
995
996 dragResult = evt.GetDragResult();
b8b0e402 997 return dragResult;
9ce192d4
RD
998}
999
1000
1001void ScintillaWX::DoDragLeave() {
1002 SetDragPosition(invalidPosition);
1003}
1bc32508 1004#endif
9ce192d4
RD
1005//----------------------------------------------------------------------
1006
cadea444 1007// Force the whole window to be repainted
ab0f5fa6 1008void ScintillaWX::FullPaint() {
719493e1 1009#ifndef __WXMAC__
cadea444 1010 stc->Refresh(false);
719493e1 1011#endif
cadea444 1012 stc->Update();
9ce192d4
RD
1013}
1014
1015
1016void ScintillaWX::DoScrollToLine(int line) {
1017 ScrollTo(line);
1018}
1019
1020
1021void ScintillaWX::DoScrollToColumn(int column) {
1022 HorizontalScrollTo(column * vs.spaceWidth);
1023}
1024
382fe640
RD
1025// wxGTK doesn't appear to need this explicit clipping code any longer, but I
1026// will leave it here commented out for a while just in case...
1027void ScintillaWX::ClipChildren(wxDC& WXUNUSED(dc), PRectangle WXUNUSED(rect))
1028{
1029// wxRegion rgn(wxRectFromPRectangle(rect));
1030// if (ac.Active()) {
1031// wxRect childRect = ((wxWindow*)ac.lb->GetID())->GetRect();
1032// rgn.Subtract(childRect);
1033// }
1034// if (ct.inCallTipMode) {
1035// wxSTCCallTip* tip = (wxSTCCallTip*)ct.wCallTip.GetID();
1036// wxRect childRect = tip->GetRect();
1037// #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
1038// childRect.SetPosition(tip->GetMyPosition());
1039// #endif
1040// rgn.Subtract(childRect);
1041// }
1042// dc.SetClippingRegion(rgn);
88a8b04e 1043}
9ce192d4 1044
d1558f3d
RD
1045
1046void ScintillaWX::SetUseAntiAliasing(bool useAA) {
1047 vs.extraFontFlag = useAA;
1048 InvalidateStyleRedraw();
1049}
1050
1051bool ScintillaWX::GetUseAntiAliasing() {
1052 return vs.extraFontFlag;
1053}
7e126a07 1054
9ce192d4
RD
1055//----------------------------------------------------------------------
1056//----------------------------------------------------------------------