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