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