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