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