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