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