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