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