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