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