]> git.saurik.com Git - wxWidgets.git/blob - src/stc/ScintillaWX.cpp
905bcec38c921dbee6e2c0a9227360d8f6d52210
[wxWidgets.git] / src / stc / ScintillaWX.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: src/stc/ScintillaWX.cpp
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 licence
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 #if !wxUSE_STD_CONTAINERS && !wxUSE_STD_IOSTREAM && !wxUSE_STD_STRING
38 #include "wx/beforestd.h"
39 #include <string>
40 #include "wx/afterstd.h"
41 #endif
42
43 #include "ScintillaWX.h"
44 #include "ExternalLexer.h"
45 #include "wx/stc/stc.h"
46 #include "wx/stc/private.h"
47 #include "PlatWX.h"
48
49 #ifdef __WXMSW__
50 // GetHwndOf()
51 #include "wx/msw/private.h"
52 #endif
53
54 //----------------------------------------------------------------------
55 // Helper classes
56
57 class wxSTCTimer : public wxTimer {
58 public:
59 wxSTCTimer(ScintillaWX* swx) {
60 m_swx = swx;
61 }
62
63 void Notify() {
64 m_swx->DoTick();
65 }
66
67 private:
68 ScintillaWX* m_swx;
69 };
70
71
72 #if wxUSE_DRAG_AND_DROP
73 bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
74 return m_swx->DoDropText(x, y, data);
75 }
76
77 wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) {
78 return m_swx->DoDragEnter(x, y, def);
79 }
80
81 wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
82 return m_swx->DoDragOver(x, y, def);
83 }
84
85 void wxSTCDropTarget::OnLeave() {
86 m_swx->DoDragLeave();
87 }
88 #endif // wxUSE_DRAG_AND_DROP
89
90
91 #if wxUSE_POPUPWIN
92 #include "wx/popupwin.h"
93 #define wxSTCCallTipBase wxPopupWindow
94 #else
95 #include "wx/frame.h"
96 #define wxSTCCallTipBase wxFrame
97 #endif
98
99 #include "wx/dcbuffer.h"
100
101 class wxSTCCallTip : public wxSTCCallTipBase {
102 public:
103 wxSTCCallTip(wxWindow* parent, CallTip* ct, ScintillaWX* swx) :
104 #if wxUSE_POPUPWIN
105 wxSTCCallTipBase(parent, wxBORDER_NONE),
106 #else
107 wxSTCCallTipBase(parent, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize,
108 wxFRAME_NO_TASKBAR
109 | wxFRAME_FLOAT_ON_PARENT
110 | wxBORDER_NONE
111 #ifdef __WXMAC__
112 | wxPOPUP_WINDOW
113 #endif
114 ),
115 #endif
116 m_ct(ct), m_swx(swx), m_cx(wxDefaultCoord), m_cy(wxDefaultCoord)
117 {
118 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
119 }
120
121 ~wxSTCCallTip() {
122 #if wxUSE_POPUPWIN && defined(__WXGTK__)
123 wxRect rect = GetRect();
124 rect.x = m_cx;
125 rect.y = m_cy;
126 GetParent()->Refresh(false, &rect);
127 #endif
128 }
129
130 bool AcceptsFocus() const { return false; }
131
132 void OnPaint(wxPaintEvent& WXUNUSED(evt))
133 {
134 wxAutoBufferedPaintDC dc(this);
135 Surface* surfaceWindow = Surface::Allocate();
136 surfaceWindow->Init(&dc, m_ct->wDraw.GetID());
137 m_ct->PaintCT(surfaceWindow);
138 surfaceWindow->Release();
139 delete surfaceWindow;
140 }
141
142 void OnFocus(wxFocusEvent& event)
143 {
144 GetParent()->SetFocus();
145 event.Skip();
146 }
147
148 void OnLeftDown(wxMouseEvent& event)
149 {
150 wxPoint pt = event.GetPosition();
151 Point p(pt.x, pt.y);
152 m_ct->MouseClick(p);
153 m_swx->CallTipClick();
154 }
155
156 virtual void DoSetSize(int x, int y,
157 int width, int height,
158 int sizeFlags = wxSIZE_AUTO)
159 {
160 // convert coords to screen coords since we're a top-level window
161 if (x != wxDefaultCoord) {
162 m_cx = x;
163 GetParent()->ClientToScreen(&x, NULL);
164 }
165 if (y != wxDefaultCoord) {
166 m_cy = y;
167 GetParent()->ClientToScreen(NULL, &y);
168 }
169 wxSTCCallTipBase::DoSetSize(x, y, width, height, sizeFlags);
170 }
171
172 #if wxUSE_POPUPWIN
173 #else
174 virtual bool Show( bool show = true )
175 {
176 // Although we're a frame, we always want the parent to be active, so
177 // raise it whenever we get shown.
178 bool rv = wxSTCCallTipBase::Show(show);
179 if (rv && show)
180 {
181 wxTopLevelWindow *frame = wxDynamicCast(
182 wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
183 if (frame)
184 frame->Raise();
185 }
186 return rv;
187 }
188 #endif
189
190 wxPoint GetMyPosition()
191 {
192 return wxPoint(m_cx, m_cy);
193 }
194
195 private:
196 CallTip* m_ct;
197 ScintillaWX* m_swx;
198 int m_cx, m_cy;
199 DECLARE_EVENT_TABLE()
200 };
201
202 BEGIN_EVENT_TABLE(wxSTCCallTip, wxSTCCallTipBase)
203 EVT_PAINT(wxSTCCallTip::OnPaint)
204 EVT_SET_FOCUS(wxSTCCallTip::OnFocus)
205 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown)
206 END_EVENT_TABLE()
207
208
209 //----------------------------------------------------------------------
210
211 #if wxUSE_DATAOBJ
212 static wxTextFileType wxConvertEOLMode(int scintillaMode)
213 {
214 wxTextFileType type;
215
216 switch (scintillaMode) {
217 case wxSTC_EOL_CRLF:
218 type = wxTextFileType_Dos;
219 break;
220
221 case wxSTC_EOL_CR:
222 type = wxTextFileType_Mac;
223 break;
224
225 case wxSTC_EOL_LF:
226 type = wxTextFileType_Unix;
227 break;
228
229 default:
230 type = wxTextBuffer::typeDefault;
231 break;
232 }
233 return type;
234 }
235 #endif // wxUSE_DATAOBJ
236
237
238 //----------------------------------------------------------------------
239 // Constructor/Destructor
240
241
242 ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
243 capturedMouse = false;
244 focusEvent = false;
245 wMain = win;
246 stc = win;
247 wheelRotation = 0;
248 Initialise();
249 #ifdef __WXMSW__
250 sysCaretBitmap = 0;
251 sysCaretWidth = 0;
252 sysCaretHeight = 0;
253 #endif
254 }
255
256
257 ScintillaWX::~ScintillaWX() {
258 Finalise();
259 }
260
261 //----------------------------------------------------------------------
262 // base class virtuals
263
264
265 void ScintillaWX::Initialise() {
266 //ScintillaBase::Initialise();
267 #if wxUSE_DRAG_AND_DROP
268 dropTarget = new wxSTCDropTarget;
269 dropTarget->SetScintilla(this);
270 stc->SetDropTarget(dropTarget);
271 #endif // wxUSE_DRAG_AND_DROP
272 vs.extraFontFlag = true; // UseAntiAliasing
273 }
274
275
276 void ScintillaWX::Finalise() {
277 ScintillaBase::Finalise();
278 SetTicking(false);
279 SetIdle(false);
280 DestroySystemCaret();
281 }
282
283
284 void ScintillaWX::StartDrag() {
285 #if wxUSE_DRAG_AND_DROP
286 wxString dragText = stc2wx(drag.s, drag.len);
287
288 // Send an event to allow the drag text to be changed
289 wxStyledTextEvent evt(wxEVT_STC_START_DRAG, stc->GetId());
290 evt.SetEventObject(stc);
291 evt.SetDragText(dragText);
292 evt.SetDragFlags(wxDrag_DefaultMove);
293 evt.SetPosition(wxMin(stc->GetSelectionStart(),
294 stc->GetSelectionEnd()));
295 stc->GetEventHandler()->ProcessEvent(evt);
296 dragText = evt.GetDragText();
297
298 if ( !dragText.empty() ) {
299 wxDropSource source(stc);
300 wxTextDataObject data(dragText);
301 wxDragResult result;
302
303 source.SetData(data);
304 dropWentOutside = true;
305 inDragDrop = ddDragging;
306 result = source.DoDragDrop(evt.GetDragFlags());
307 if (result == wxDragMove && dropWentOutside)
308 ClearSelection();
309 inDragDrop = ddNone;
310 SetDragPosition(SelectionPosition(invalidPosition));
311 }
312 #endif // wxUSE_DRAG_AND_DROP
313 }
314
315
316 bool ScintillaWX::SetIdle(bool on) {
317 if (idler.state != on) {
318 // connect or disconnect the EVT_IDLE handler
319 if (on)
320 stc->Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(wxStyledTextCtrl::OnIdle));
321 else
322 stc->Disconnect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(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 }
368
369 void ScintillaWX::SetVerticalScrollPos() {
370 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
371 stc->SetScrollPos(wxVERTICAL, topLine);
372 }
373 else { // otherwise use the one that's been given to us
374 stc->m_vScrollBar->SetThumbPosition(topLine);
375 }
376 }
377
378 void ScintillaWX::SetHorizontalScrollPos() {
379 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
380 stc->SetScrollPos(wxHORIZONTAL, xOffset);
381 }
382 else { // otherwise use the one that's been given to us
383 stc->m_hScrollBar->SetThumbPosition(xOffset);
384 }
385 }
386
387
388 const int H_SCROLL_STEP = 20;
389
390 bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) {
391 bool modified = false;
392
393 int vertEnd = nMax;
394 if (!verticalScrollBarVisible)
395 vertEnd = 0;
396
397 // Check the vertical scrollbar
398 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
399 int sbMax = stc->GetScrollRange(wxVERTICAL);
400 int sbThumb = stc->GetScrollThumb(wxVERTICAL);
401 int sbPos = stc->GetScrollPos(wxVERTICAL);
402 if (sbMax != vertEnd || sbThumb != nPage) {
403 stc->SetScrollbar(wxVERTICAL, sbPos, nPage, vertEnd+1);
404 modified = true;
405 }
406 }
407 else { // otherwise use the one that's been given to us
408 int sbMax = stc->m_vScrollBar->GetRange();
409 int sbPage = stc->m_vScrollBar->GetPageSize();
410 int sbPos = stc->m_vScrollBar->GetThumbPosition();
411 if (sbMax != vertEnd || sbPage != nPage) {
412 stc->m_vScrollBar->SetScrollbar(sbPos, nPage, vertEnd+1, nPage);
413 modified = true;
414 }
415 }
416
417
418 // Check the horizontal scrollbar
419 PRectangle rcText = GetTextRectangle();
420 int horizEnd = scrollWidth;
421 if (horizEnd < 0)
422 horizEnd = 0;
423 if (!horizontalScrollBarVisible || (wrapState != eWrapNone))
424 horizEnd = 0;
425 int pageWidth = rcText.Width();
426
427 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
428 int sbMax = stc->GetScrollRange(wxHORIZONTAL);
429 int sbThumb = stc->GetScrollThumb(wxHORIZONTAL);
430 int sbPos = stc->GetScrollPos(wxHORIZONTAL);
431 if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) {
432 stc->SetScrollbar(wxHORIZONTAL, sbPos, pageWidth, horizEnd);
433 modified = true;
434 if (scrollWidth < pageWidth) {
435 HorizontalScrollTo(0);
436 }
437 }
438 }
439 else { // otherwise use the one that's been given to us
440 int sbMax = stc->m_hScrollBar->GetRange();
441 int sbThumb = stc->m_hScrollBar->GetPageSize();
442 int sbPos = stc->m_hScrollBar->GetThumbPosition();
443 if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) {
444 stc->m_hScrollBar->SetScrollbar(sbPos, pageWidth, horizEnd, pageWidth);
445 modified = true;
446 if (scrollWidth < pageWidth) {
447 HorizontalScrollTo(0);
448 }
449 }
450 }
451
452 return modified;
453 }
454
455
456 void ScintillaWX::NotifyChange() {
457 stc->NotifyChange();
458 }
459
460
461 void ScintillaWX::NotifyParent(SCNotification scn) {
462 stc->NotifyParent(&scn);
463 }
464
465
466 // This method is overloaded from ScintillaBase in order to prevent the
467 // AutoComplete window from being destroyed when it gets the focus. There is
468 // a side effect that the AutoComp will also not be destroyed when switching
469 // to another window, but I think that is okay.
470 void ScintillaWX::CancelModes() {
471 if (! focusEvent)
472 AutoCompleteCancel();
473 ct.CallTipCancel();
474 Editor::CancelModes();
475 }
476
477
478
479 void ScintillaWX::Copy() {
480 if (!sel.Empty()) {
481 SelectionText st;
482 CopySelectionRange(&st);
483 CopyToClipboard(st);
484 }
485 }
486
487
488 void ScintillaWX::Paste() {
489 pdoc->BeginUndoAction();
490 ClearSelection();
491
492 #if wxUSE_DATAOBJ
493 wxTextDataObject data;
494 bool gotData = false;
495
496 wxTheClipboard->UsePrimarySelection(false);
497 if (wxTheClipboard->Open()) {
498 gotData = wxTheClipboard->GetData(data);
499 wxTheClipboard->Close();
500 }
501 if (gotData) {
502 wxString text = wxTextBuffer::Translate(data.GetText(),
503 wxConvertEOLMode(pdoc->eolMode));
504 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
505
506 #if wxUSE_UNICODE
507 // free up the old character buffer in case the text is real big
508 data.SetText(wxEmptyString);
509 text = wxEmptyString;
510 #endif
511 int len = strlen(buf);
512 int caretMain = sel.MainCaret();
513 pdoc->InsertString(caretMain, buf, len);
514 SetEmptySelection(caretMain + 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 (!sel.Empty()) {
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 = PointMainCaret();
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 int ctStyle = ct.UseStyleCallTip() ? STYLE_CALLTIP : STYLE_DEFAULT;
684 if (ct.UseStyleCallTip())
685 {
686 ct.SetForeBack(vs.styles[STYLE_CALLTIP].fore, vs.styles[STYLE_CALLTIP].back);
687 }
688 int caretMain = sel.MainCaret();
689 PRectangle rc = ct.CallTipStart(caretMain, pt,
690 defn,
691 vs.styles[ctStyle].fontName,
692 vs.styles[ctStyle].sizeZoomed,
693 CodePage(),
694 vs.styles[ctStyle].characterSet,
695 wMain);
696 // If the call-tip window would be out of the client
697 // space, adjust so it displays above the text.
698 PRectangle rcClient = GetClientRectangle();
699 if (rc.bottom > rcClient.bottom) {
700 #ifdef __WXGTK__
701 int offset = int(vs.lineHeight * 1.25) + rc.Height();
702 #else
703 int offset = vs.lineHeight + rc.Height();
704 #endif
705 rc.top -= offset;
706 rc.bottom -= offset;
707 }
708 // Now display the window.
709 CreateCallTipWindow(rc);
710 ct.wCallTip.SetPositionRelative(rc, wMain);
711 ct.wCallTip.Show();
712 break;
713 }
714
715 #ifdef SCI_LEXER
716 case SCI_LOADLEXERLIBRARY:
717 LexerManager::GetInstance()->Load((const char*)lParam);
718 break;
719 #endif
720
721 default:
722 return ScintillaBase::WndProc(iMessage, wParam, lParam);
723 }
724 return 0;
725 }
726
727
728
729 //----------------------------------------------------------------------
730 // Event delegates
731
732 void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
733
734 paintState = painting;
735 AutoSurface surfaceWindow(dc, this);
736 if (surfaceWindow) {
737 rcPaint = PRectangleFromwxRect(rect);
738 PRectangle rcClient = GetClientRectangle();
739 paintingAllText = rcPaint.Contains(rcClient);
740
741 ClipChildren(*dc, rcPaint);
742 Paint(surfaceWindow, rcPaint);
743 surfaceWindow->Release();
744 }
745
746 if (paintState == paintAbandoned) {
747 // Painting area was insufficient to cover new styling or brace
748 // highlight positions
749 FullPaintDC(dc);
750 }
751 paintState = notPainting;
752 }
753
754
755 // Force the whole window to be repainted
756 void ScintillaWX::FullPaint() {
757 wxClientDC dc(stc);
758 FullPaintDC(&dc);
759 }
760
761
762 void ScintillaWX::FullPaintDC(wxDC* dc) {
763 paintState = painting;
764 rcPaint = GetClientRectangle();
765 paintingAllText = true;
766 AutoSurface surfaceWindow(dc, this);
767 if (surfaceWindow) {
768 Paint(surfaceWindow, rcPaint);
769 surfaceWindow->Release();
770 }
771 paintState = notPainting;
772 }
773
774
775
776 void ScintillaWX::DoHScroll(int type, int pos) {
777 int xPos = xOffset;
778 PRectangle rcText = GetTextRectangle();
779 int pageWidth = rcText.Width() * 2 / 3;
780 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
781 xPos -= H_SCROLL_STEP;
782 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
783 xPos += H_SCROLL_STEP;
784 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
785 xPos -= pageWidth;
786 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN) {
787 xPos += pageWidth;
788 if (xPos > scrollWidth - rcText.Width()) {
789 xPos = scrollWidth - rcText.Width();
790 }
791 }
792 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
793 xPos = 0;
794 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
795 xPos = scrollWidth;
796 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
797 xPos = pos;
798
799 HorizontalScrollTo(xPos);
800 }
801
802 void ScintillaWX::DoVScroll(int type, int pos) {
803 int topLineNew = topLine;
804 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
805 topLineNew -= 1;
806 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
807 topLineNew += 1;
808 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
809 topLineNew -= LinesToScroll();
810 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
811 topLineNew += LinesToScroll();
812 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
813 topLineNew = 0;
814 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
815 topLineNew = MaxScrollPos();
816 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
817 topLineNew = pos;
818
819 ScrollTo(topLineNew);
820 }
821
822 void ScintillaWX::DoMouseWheel(int rotation, int delta,
823 int linesPerAction, int ctrlDown,
824 bool isPageScroll ) {
825 int topLineNew = topLine;
826 int lines;
827
828 if (ctrlDown) { // Zoom the fonts if Ctrl key down
829 if (rotation > 0) {
830 KeyCommand(SCI_ZOOMIN);
831 }
832 else {
833 KeyCommand(SCI_ZOOMOUT);
834 }
835 }
836 else { // otherwise just scroll the window
837 if ( !delta )
838 delta = 120;
839 wheelRotation += rotation;
840 lines = wheelRotation / delta;
841 wheelRotation -= lines * delta;
842 if (lines != 0) {
843 if (isPageScroll)
844 lines = lines * LinesOnScreen(); // lines is either +1 or -1
845 else
846 lines *= linesPerAction;
847 topLineNew -= lines;
848 ScrollTo(topLineNew);
849 }
850 }
851 }
852
853
854 void ScintillaWX::DoSize(int WXUNUSED(width), int WXUNUSED(height)) {
855 ChangeSize();
856 }
857
858 void ScintillaWX::DoLoseFocus(){
859 focusEvent = true;
860 SetFocusState(false);
861 focusEvent = false;
862 DestroySystemCaret();
863 }
864
865 void ScintillaWX::DoGainFocus(){
866 focusEvent = true;
867 SetFocusState(true);
868 focusEvent = false;
869 DestroySystemCaret();
870 CreateSystemCaret();
871 }
872
873 void ScintillaWX::DoSysColourChange() {
874 InvalidateStyleData();
875 }
876
877 void ScintillaWX::DoLeftButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
878 ButtonDown(pt, curTime, shift, ctrl, alt);
879 }
880
881 void ScintillaWX::DoLeftButtonUp(Point pt, unsigned int curTime, bool ctrl) {
882 ButtonUp(pt, curTime, ctrl);
883 }
884
885 void ScintillaWX::DoLeftButtonMove(Point pt) {
886 ButtonMove(pt);
887 }
888
889 #ifdef __WXGTK__
890 void ScintillaWX::DoMiddleButtonUp(Point pt) {
891 // Set the current position to the mouse click point and
892 // then paste in the PRIMARY selection, if any. wxGTK only.
893 int newPos = PositionFromLocation(pt);
894 MovePositionTo(newPos, Selection::noSel, true);
895
896 pdoc->BeginUndoAction();
897 wxTextDataObject data;
898 bool gotData = false;
899 wxTheClipboard->UsePrimarySelection(true);
900 if (wxTheClipboard->Open()) {
901 gotData = wxTheClipboard->GetData(data);
902 wxTheClipboard->Close();
903 }
904 wxTheClipboard->UsePrimarySelection(false);
905 if (gotData) {
906 wxString text = wxTextBuffer::Translate(data.GetText(),
907 wxConvertEOLMode(pdoc->eolMode));
908 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
909 int len = strlen(buf);
910 int caretMain = sel.MainCaret();
911 pdoc->InsertString(caretMain, buf, len);
912 SetEmptySelection(caretMain + len);
913 }
914 pdoc->EndUndoAction();
915 NotifyChange();
916 Redraw();
917
918 ShowCaretAtCurrentPosition();
919 EnsureCaretVisible();
920 }
921 #else
922 void ScintillaWX::DoMiddleButtonUp(Point WXUNUSED(pt)) {
923 }
924 #endif
925
926
927 void ScintillaWX::DoAddChar(int key) {
928 #if wxUSE_UNICODE
929 wxChar wszChars[2];
930 wszChars[0] = (wxChar)key;
931 wszChars[1] = 0;
932 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(wszChars);
933 AddCharUTF((char*)buf.data(), strlen(buf));
934 #else
935 AddChar((char)key);
936 #endif
937 }
938
939
940 int ScintillaWX::DoKeyDown(const wxKeyEvent& evt, bool* consumed)
941 {
942 int key = evt.GetKeyCode();
943 if (key == WXK_NONE) {
944 // This is a Unicode character not representable in Latin-1 or some key
945 // without key code at all (e.g. dead key or VK_PROCESSKEY under MSW).
946 if ( consumed )
947 *consumed = false;
948 return 0;
949 }
950
951 bool shift = evt.ShiftDown(),
952 ctrl = evt.ControlDown(),
953 alt = evt.AltDown();
954
955 if (ctrl && key >= 1 && key <= 26 && key != WXK_BACK)
956 key += 'A' - 1;
957
958 switch (key) {
959 case WXK_DOWN: key = SCK_DOWN; break;
960 case WXK_UP: key = SCK_UP; break;
961 case WXK_LEFT: key = SCK_LEFT; break;
962 case WXK_RIGHT: key = SCK_RIGHT; break;
963 case WXK_HOME: key = SCK_HOME; break;
964 case WXK_END: key = SCK_END; break;
965 case WXK_PAGEUP: key = SCK_PRIOR; break;
966 case WXK_PAGEDOWN: key = SCK_NEXT; break;
967 case WXK_NUMPAD_DOWN: key = SCK_DOWN; break;
968 case WXK_NUMPAD_UP: key = SCK_UP; break;
969 case WXK_NUMPAD_LEFT: key = SCK_LEFT; break;
970 case WXK_NUMPAD_RIGHT: key = SCK_RIGHT; break;
971 case WXK_NUMPAD_HOME: key = SCK_HOME; break;
972 case WXK_NUMPAD_END: key = SCK_END; break;
973 case WXK_NUMPAD_PAGEUP: key = SCK_PRIOR; break;
974 case WXK_NUMPAD_PAGEDOWN: key = SCK_NEXT; break;
975 case WXK_NUMPAD_DELETE: key = SCK_DELETE; break;
976 case WXK_NUMPAD_INSERT: key = SCK_INSERT; break;
977 case WXK_DELETE: key = SCK_DELETE; break;
978 case WXK_INSERT: key = SCK_INSERT; break;
979 case WXK_ESCAPE: key = SCK_ESCAPE; break;
980 case WXK_BACK: key = SCK_BACK; break;
981 case WXK_TAB: key = SCK_TAB; break;
982 case WXK_NUMPAD_ENTER: // fall through
983 case WXK_RETURN: key = SCK_RETURN; break;
984 case WXK_ADD: // fall through
985 case WXK_NUMPAD_ADD: key = SCK_ADD; break;
986 case WXK_SUBTRACT: // fall through
987 case WXK_NUMPAD_SUBTRACT: key = SCK_SUBTRACT; break;
988 case WXK_DIVIDE: // fall through
989 case WXK_NUMPAD_DIVIDE: key = SCK_DIVIDE; break;
990 case WXK_CONTROL: key = 0; break;
991 case WXK_ALT: key = 0; break;
992 case WXK_SHIFT: key = 0; break;
993 case WXK_MENU: key = SCK_MENU; break;
994 }
995
996 #ifdef __WXMAC__
997 if ( evt.MetaDown() ) {
998 // check for a few common Mac Meta-key combos and remap them to Ctrl
999 // for Scintilla
1000 switch ( key ) {
1001 case 'Z': // Undo
1002 case 'X': // Cut
1003 case 'C': // Copy
1004 case 'V': // Paste
1005 case 'A': // Select All
1006 ctrl = true;
1007 break;
1008 }
1009 }
1010 #endif
1011
1012 int rv = KeyDown(key, shift, ctrl, alt, consumed);
1013
1014 if (key)
1015 return rv;
1016 else
1017 return 1;
1018 }
1019
1020
1021 void ScintillaWX::DoCommand(int ID) {
1022 Command(ID);
1023 }
1024
1025
1026 void ScintillaWX::DoContextMenu(Point pt) {
1027 if (displayPopupMenu)
1028 ContextMenu(pt);
1029 }
1030
1031 void ScintillaWX::DoOnListBox() {
1032 AutoCompleteCompleted();
1033 }
1034
1035
1036 void ScintillaWX::DoOnIdle(wxIdleEvent& evt) {
1037
1038 if ( Idle() )
1039 evt.RequestMore();
1040 else
1041 SetIdle(false);
1042 }
1043
1044 //----------------------------------------------------------------------
1045
1046 #if wxUSE_DRAG_AND_DROP
1047 bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
1048 SetDragPosition(SelectionPosition(invalidPosition));
1049
1050 wxString text = wxTextBuffer::Translate(data,
1051 wxConvertEOLMode(pdoc->eolMode));
1052
1053 // Send an event to allow the drag details to be changed
1054 wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId());
1055 evt.SetEventObject(stc);
1056 evt.SetDragResult(dragResult);
1057 evt.SetX(x);
1058 evt.SetY(y);
1059 evt.SetPosition(PositionFromLocation(Point(x,y)));
1060 evt.SetDragText(text);
1061 stc->GetEventHandler()->ProcessEvent(evt);
1062
1063 dragResult = evt.GetDragResult();
1064 if (dragResult == wxDragMove || dragResult == wxDragCopy) {
1065 DropAt(SelectionPosition(evt.GetPosition()),
1066 wx2stc(evt.GetDragText()),
1067 dragResult == wxDragMove,
1068 false); // TODO: rectangular?
1069 return true;
1070 }
1071 return false;
1072 }
1073
1074
1075 wxDragResult ScintillaWX::DoDragEnter(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) {
1076 dragResult = def;
1077 return dragResult;
1078 }
1079
1080
1081 wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
1082 SetDragPosition(SelectionPosition(PositionFromLocation(Point(x, y))));
1083
1084 // Send an event to allow the drag result to be changed
1085 wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId());
1086 evt.SetEventObject(stc);
1087 evt.SetDragResult(def);
1088 evt.SetX(x);
1089 evt.SetY(y);
1090 evt.SetPosition(PositionFromLocation(Point(x,y)));
1091 stc->GetEventHandler()->ProcessEvent(evt);
1092
1093 dragResult = evt.GetDragResult();
1094 return dragResult;
1095 }
1096
1097
1098 void ScintillaWX::DoDragLeave() {
1099 SetDragPosition(SelectionPosition(invalidPosition));
1100 }
1101 #endif // wxUSE_DRAG_AND_DROP
1102 //----------------------------------------------------------------------
1103
1104 void ScintillaWX::DoScrollToLine(int line) {
1105 ScrollTo(line);
1106 }
1107
1108
1109 void ScintillaWX::DoScrollToColumn(int column) {
1110 HorizontalScrollTo(column * vs.spaceWidth);
1111 }
1112
1113 // wxGTK doesn't appear to need this explicit clipping code any longer, but I
1114 // will leave it here commented out for a while just in case...
1115 void ScintillaWX::ClipChildren(wxDC& WXUNUSED(dc), PRectangle WXUNUSED(rect))
1116 {
1117 // wxRegion rgn(wxRectFromPRectangle(rect));
1118 // if (ac.Active()) {
1119 // wxRect childRect = ((wxWindow*)ac.lb->GetID())->GetRect();
1120 // rgn.Subtract(childRect);
1121 // }
1122 // if (ct.inCallTipMode) {
1123 // wxSTCCallTip* tip = (wxSTCCallTip*)ct.wCallTip.GetID();
1124 // wxRect childRect = tip->GetRect();
1125 // #if wxUSE_POPUPWIN
1126 // childRect.SetPosition(tip->GetMyPosition());
1127 // #endif
1128 // rgn.Subtract(childRect);
1129 // }
1130 // dc.SetClippingRegion(rgn);
1131 }
1132
1133
1134 void ScintillaWX::SetUseAntiAliasing(bool useAA) {
1135 vs.extraFontFlag = useAA;
1136 InvalidateStyleRedraw();
1137 }
1138
1139 bool ScintillaWX::GetUseAntiAliasing() {
1140 return vs.extraFontFlag != 0;
1141 }
1142
1143 //----------------------------------------------------------------------
1144 //----------------------------------------------------------------------
1145
1146 #endif // wxUSE_STC