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