]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/stc/ScintillaWX.cpp
use standard colour for the selected tree item text as well as background
[wxWidgets.git] / src / stc / ScintillaWX.cpp
... / ...
CommitLineData
1////////////////////////////////////////////////////////////////////////////
2// Name: ScintillaWX.cxx
3// Purpose: A wxWindows 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
18#include "ScintillaWX.h"
19#include "wx/stc/stc.h"
20#include "PlatWX.h"
21
22
23//----------------------------------------------------------------------
24
25const int H_SCROLL_MAX = 4000;
26const int H_SCROLL_STEP = 20;
27const int H_SCROLL_PAGE = 200;
28
29//----------------------------------------------------------------------
30// Helper classes
31
32class wxSTCTimer : public wxTimer {
33public:
34 wxSTCTimer(ScintillaWX* swx) {
35 this->swx = swx;
36 }
37
38 void Notify() {
39 swx->DoTick();
40 }
41
42private:
43 ScintillaWX* swx;
44};
45
46
47#if wxUSE_DRAG_AND_DROP
48bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
49 return swx->DoDropText(x, y, data);
50}
51
52wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) {
53 return swx->DoDragEnter(x, y, def);
54}
55
56wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
57 return swx->DoDragOver(x, y, def);
58}
59
60void wxSTCDropTarget::OnLeave() {
61 swx->DoDragLeave();
62}
63#endif
64
65
66#ifdef __WXGTK__
67#undef wxSTC_USE_POPUP
68#define wxSTC_USE_POPUP 0
69#endif
70
71#if wxUSE_POPUPWIN && wxSTC_USE_POPUP
72#include <wx/popupwin.h>
73#define wxSTCCallTipBase wxPopupWindow
74#define param2 wxBORDER_NONE // popup's 2nd param is flags
75#else
76#define wxSTCCallTipBase wxWindow
77#define param2 -1 // wxWindows 2nd param is ID
78#endif
79
80class wxSTCCallTip : public wxSTCCallTipBase {
81public:
82 wxSTCCallTip(wxWindow* parent, CallTip* ct)
83 : wxSTCCallTipBase(parent, param2)
84 {
85 m_ct = ct;
86 }
87
88 ~wxSTCCallTip() {
89 if (HasCapture()) ReleaseMouse();
90 }
91
92 void OnPaint(wxPaintEvent& evt) {
93 wxPaintDC dc(this);
94 Surface* surfaceWindow = Surface::Allocate();
95 surfaceWindow->Init(&dc);
96 m_ct->PaintCT(surfaceWindow);
97 delete surfaceWindow;
98 }
99
100 void OnFocus(wxFocusEvent& event) {
101 GetParent()->SetFocus();
102 event.Skip();
103 }
104
105#if wxUSE_POPUPWIN && wxSTC_USE_POPUP
106 virtual void DoSetSize(int x, int y,
107 int width, int height,
108 int sizeFlags = wxSIZE_AUTO) {
109 if (x != -1)
110 GetParent()->ClientToScreen(&x, NULL);
111 if (y != -1)
112 GetParent()->ClientToScreen(NULL, &y);
113 wxSTCCallTipBase::DoSetSize(x, y, width, height, sizeFlags);
114 }
115
116 virtual bool Show( bool show = TRUE ) {
117 bool retval = wxSTCCallTipBase::Show(show);
118 if (show)
119 CaptureMouse();
120 else
121 if (HasCapture()) ReleaseMouse();
122 return retval;
123 }
124
125 void OnLeftDown(wxMouseEvent& ) {
126 Show(FALSE);
127 }
128#endif
129
130private:
131 CallTip* m_ct;
132 DECLARE_EVENT_TABLE()
133};
134
135BEGIN_EVENT_TABLE(wxSTCCallTip, wxSTCCallTipBase)
136 EVT_PAINT(wxSTCCallTip::OnPaint)
137 EVT_SET_FOCUS(wxSTCCallTip::OnFocus)
138#if wxUSE_POPUPWIN && wxSTC_USE_POPUP
139 EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown)
140#endif
141END_EVENT_TABLE()
142
143
144//----------------------------------------------------------------------
145// Constructor/Destructor
146
147
148ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
149 capturedMouse = false;
150 wMain = win;
151 stc = win;
152 wheelRotation = 0;
153 Initialise();
154}
155
156
157ScintillaWX::~ScintillaWX() {
158 SetTicking(false);
159}
160
161//----------------------------------------------------------------------
162// base class virtuals
163
164
165void ScintillaWX::Initialise() {
166 //ScintillaBase::Initialise();
167#if wxUSE_DRAG_AND_DROP
168 dropTarget = new wxSTCDropTarget;
169 dropTarget->SetScintilla(this);
170 stc->SetDropTarget(dropTarget);
171#endif
172}
173
174
175void ScintillaWX::Finalise() {
176 ScintillaBase::Finalise();
177}
178
179
180void ScintillaWX::StartDrag() {
181#if wxUSE_DRAG_AND_DROP
182 wxString dragText = stc2wx(drag.s, drag.len);
183
184 // Send an event to allow the drag text to be changed
185 wxStyledTextEvent evt(wxEVT_STC_START_DRAG, stc->GetId());
186 evt.SetEventObject(stc);
187 evt.SetDragText(dragText);
188 evt.SetDragAllowMove(TRUE);
189 evt.SetPosition(wxMin(stc->GetSelectionStart(),
190 stc->GetSelectionEnd()));
191 stc->GetEventHandler()->ProcessEvent(evt);
192 dragText = evt.GetDragText();
193
194 if (dragText.Length()) {
195 wxDropSource source(stc);
196 wxTextDataObject data(dragText);
197 wxDragResult result;
198
199 source.SetData(data);
200 dropWentOutside = TRUE;
201 result = source.DoDragDrop(evt.GetDragAllowMove());
202 if (result == wxDragMove && dropWentOutside)
203 ClearSelection();
204 inDragDrop = FALSE;
205 SetDragPosition(invalidPosition);
206 }
207#endif
208}
209
210
211void ScintillaWX::SetTicking(bool on) {
212 wxSTCTimer* steTimer;
213 if (timer.ticking != on) {
214 timer.ticking = on;
215 if (timer.ticking) {
216 steTimer = new wxSTCTimer(this);
217 steTimer->Start(timer.tickSize);
218 timer.tickerID = steTimer;
219 } else {
220 steTimer = (wxSTCTimer*)timer.tickerID;
221 steTimer->Stop();
222 delete steTimer;
223 timer.tickerID = 0;
224 }
225 }
226 timer.ticksToWait = caret.period;
227}
228
229
230void ScintillaWX::SetMouseCapture(bool on) {
231 if (on && !capturedMouse)
232 stc->CaptureMouse();
233 else if (!on && capturedMouse)
234 stc->ReleaseMouse();
235 capturedMouse = on;
236}
237
238
239bool ScintillaWX::HaveMouseCapture() {
240 return capturedMouse;
241}
242
243
244void ScintillaWX::ScrollText(int linesToMove) {
245 int dy = vs.lineHeight * (linesToMove);
246 stc->ScrollWindow(0, dy);
247 stc->Update();
248}
249
250void ScintillaWX::SetVerticalScrollPos() {
251 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
252 stc->SetScrollPos(wxVERTICAL, topLine);
253 }
254 else { // otherwise use the one that's been given to us
255 stc->m_vScrollBar->SetThumbPosition(topLine);
256 }
257}
258
259void ScintillaWX::SetHorizontalScrollPos() {
260 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
261 stc->SetScrollPos(wxHORIZONTAL, xOffset);
262 }
263 else { // otherwise use the one that's been given to us
264 stc->m_hScrollBar->SetThumbPosition(xOffset);
265 }
266}
267
268
269bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) {
270 bool modified = false;
271
272 if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar
273 int sbMax = stc->GetScrollRange(wxVERTICAL);
274 int sbThumb = stc->GetScrollThumb(wxVERTICAL);
275 int sbPos = stc->GetScrollPos(wxVERTICAL);
276 if (sbMax != nMax || sbThumb != nPage) {
277 stc->SetScrollbar(wxVERTICAL, sbPos, nPage, nMax);
278 modified = true;
279 }
280 }
281 else { // otherwise use the one that's been given to us
282 int sbMax = stc->m_vScrollBar->GetRange();
283 int sbPage = stc->m_vScrollBar->GetPageSize();
284 int sbPos = stc->m_vScrollBar->GetThumbPosition();
285 if (sbMax != nMax || sbPage != nPage) {
286 stc->m_vScrollBar->SetScrollbar(sbPos, nPage, nMax, nPage);
287 modified = true;
288 }
289 }
290
291
292 if (horizontalScrollBarVisible) {
293 if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar
294 int sbMax = stc->GetScrollRange(wxHORIZONTAL);
295 int sbThumb = stc->GetScrollThumb(wxHORIZONTAL);
296 if ((sbMax != H_SCROLL_MAX) || (sbThumb != H_SCROLL_STEP)) {
297 stc->SetScrollbar(wxHORIZONTAL, 0, H_SCROLL_STEP, H_SCROLL_MAX);
298 modified = true;
299 }
300 }
301 else { // otherwise use the one that's been given to us
302 int sbMax = stc->m_hScrollBar->GetRange();
303 int sbPage = stc->m_hScrollBar->GetPageSize();
304 if ((sbMax != H_SCROLL_MAX) || (sbPage != H_SCROLL_STEP)) {
305 stc->m_hScrollBar->SetScrollbar(0, H_SCROLL_STEP, H_SCROLL_MAX, H_SCROLL_STEP);
306 modified = true;
307 }
308 }
309 }
310 return modified;
311}
312
313
314void ScintillaWX::NotifyChange() {
315 stc->NotifyChange();
316}
317
318
319void ScintillaWX::NotifyParent(SCNotification scn) {
320 stc->NotifyParent(&scn);
321}
322
323
324
325void ScintillaWX::Copy() {
326 if (currentPos != anchor) {
327 SelectionText st;
328 CopySelectionRange(&st);
329 if (wxTheClipboard->Open()) {
330 wxTheClipboard->UsePrimarySelection();
331 wxString text = stc2wx(st.s, st.len);
332 wxTheClipboard->SetData(new wxTextDataObject(text));
333 wxTheClipboard->Close();
334 }
335 }
336}
337
338
339void ScintillaWX::Paste() {
340 pdoc->BeginUndoAction();
341 ClearSelection();
342
343 wxTextDataObject data;
344 bool gotData = FALSE;
345
346 if (wxTheClipboard->Open()) {
347 wxTheClipboard->UsePrimarySelection();
348 gotData = wxTheClipboard->GetData(data);
349 wxTheClipboard->Close();
350 }
351 if (gotData) {
352 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(data.GetText());
353 int len = strlen(buf);
354 pdoc->InsertString(currentPos, buf, len);
355 SetEmptySelection(currentPos + len);
356 }
357
358 pdoc->EndUndoAction();
359 NotifyChange();
360 Redraw();
361}
362
363
364bool ScintillaWX::CanPaste() {
365 bool canPaste = FALSE;
366 bool didOpen;
367
368 if ( (didOpen = !wxTheClipboard->IsOpened()) )
369 wxTheClipboard->Open();
370
371 if (wxTheClipboard->IsOpened()) {
372 wxTheClipboard->UsePrimarySelection();
373 canPaste = wxTheClipboard->IsSupported(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT);
374 if (didOpen)
375 wxTheClipboard->Close();
376 }
377 return canPaste;
378}
379
380void ScintillaWX::CreateCallTipWindow(PRectangle) {
381 ct.wCallTip = new wxSTCCallTip(stc, &ct);
382 ct.wDraw = ct.wCallTip;
383}
384
385
386void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) {
387 if (!label[0])
388 ((wxMenu*)popup.GetID())->AppendSeparator();
389 else
390 ((wxMenu*)popup.GetID())->Append(cmd, stc2wx(label));
391
392 if (!enabled)
393 ((wxMenu*)popup.GetID())->Enable(cmd, enabled);
394}
395
396
397void ScintillaWX::ClaimSelection() {
398
399}
400
401
402long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
403 return 0;
404}
405
406long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) {
407// switch (iMessage) {
408// case EM_CANPASTE:
409// return CanPaste();
410// default:
411 return ScintillaBase::WndProc(iMessage, wParam, lParam);
412// }
413// return 0;
414}
415
416
417
418//----------------------------------------------------------------------
419// Event delegates
420
421void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
422
423 paintState = painting;
424 Surface* surfaceWindow = Surface::Allocate();
425 surfaceWindow->Init(dc);
426 PRectangle rcPaint = PRectangleFromwxRect(rect);
427 dc->BeginDrawing();
428 Paint(surfaceWindow, rcPaint);
429 dc->EndDrawing();
430 delete surfaceWindow;
431 if (paintState == paintAbandoned) {
432 // Painting area was insufficient to cover new styling or brace highlight positions
433 FullPaint();
434 }
435 paintState = notPainting;
436#ifdef __WXGTK__
437 // On wxGTK the editor window paints can overwrite the listbox...
438 if (ac.Active())
439 ((wxWindow*)ac.lb.GetID())->Refresh(TRUE);
440#endif
441}
442
443
444void ScintillaWX::DoHScroll(int type, int pos) {
445 int xPos = xOffset;
446 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
447 xPos -= H_SCROLL_STEP;
448 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
449 xPos += H_SCROLL_STEP;
450 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
451 xPos -= H_SCROLL_PAGE;
452 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
453 xPos += H_SCROLL_PAGE;
454 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
455 xPos = 0;
456 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
457 xPos = H_SCROLL_MAX;
458 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
459 xPos = pos;
460
461 HorizontalScrollTo(xPos);
462}
463
464void ScintillaWX::DoVScroll(int type, int pos) {
465 int topLineNew = topLine;
466 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
467 topLineNew -= 1;
468 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
469 topLineNew += 1;
470 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
471 topLineNew -= LinesToScroll();
472 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
473 topLineNew += LinesToScroll();
474 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
475 topLineNew = 0;
476 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
477 topLineNew = MaxScrollPos();
478 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
479 topLineNew = pos;
480
481 ScrollTo(topLineNew);
482}
483
484void ScintillaWX::DoMouseWheel(int rotation, int delta,
485 int linesPerAction, int ctrlDown,
486 bool isPageScroll ) {
487 int topLineNew = topLine;
488 int lines;
489
490 if (ctrlDown) { // Zoom the fonts if Ctrl key down
491 if (rotation < 0) {
492 KeyCommand(SCI_ZOOMIN);
493 }
494 else {
495 KeyCommand(SCI_ZOOMOUT);
496 }
497 }
498 else { // otherwise just scroll the window
499 wheelRotation += rotation;
500 lines = wheelRotation / delta;
501 wheelRotation -= lines * delta;
502 if (lines != 0) {
503 if (isPageScroll)
504 lines = lines * LinesOnScreen(); // lines is either +1 or -1
505 else
506 lines *= linesPerAction;
507 topLineNew -= lines;
508 ScrollTo(topLineNew);
509 }
510 }
511}
512
513
514void ScintillaWX::DoSize(int width, int height) {
515// PRectangle rcClient(0,0,width,height);
516// SetScrollBarsTo(rcClient);
517// DropGraphics();
518 ChangeSize();
519}
520
521void ScintillaWX::DoLoseFocus(){
522 SetFocusState(false);
523}
524
525void ScintillaWX::DoGainFocus(){
526 SetFocusState(true);
527}
528
529void ScintillaWX::DoSysColourChange() {
530 InvalidateStyleData();
531}
532
533void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
534 ButtonDown(pt, curTime, shift, ctrl, alt);
535}
536
537void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) {
538 ButtonUp(pt, curTime, ctrl);
539}
540
541void ScintillaWX::DoButtonMove(Point pt) {
542 ButtonMove(pt);
543}
544
545
546void ScintillaWX::DoAddChar(int key) {
547 AddChar(key);
548}
549
550int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt, bool* consumed) {
551#if defined(__WXGTK__) || defined(__WXMAC__)
552 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
553 if (ctrl && key >= 1 && key <= 26)
554 key += 'A' - 1;
555#endif
556
557 switch (key) {
558 case WXK_DOWN: key = SCK_DOWN; break;
559 case WXK_UP: key = SCK_UP; break;
560 case WXK_LEFT: key = SCK_LEFT; break;
561 case WXK_RIGHT: key = SCK_RIGHT; break;
562 case WXK_HOME: key = SCK_HOME; break;
563 case WXK_END: key = SCK_END; break;
564 case WXK_PRIOR: key = SCK_PRIOR; break;
565 case WXK_NEXT: key = SCK_NEXT; break;
566 case WXK_DELETE: key = SCK_DELETE; break;
567 case WXK_INSERT: key = SCK_INSERT; break;
568 case WXK_ESCAPE: key = SCK_ESCAPE; break;
569 case WXK_BACK: key = SCK_BACK; break;
570 case WXK_TAB: key = SCK_TAB; break;
571 case WXK_RETURN: key = SCK_RETURN; break;
572 case WXK_ADD: // fall through
573 case WXK_NUMPAD_ADD: key = SCK_ADD; break;
574 case WXK_SUBTRACT: // fall through
575 case WXK_NUMPAD_SUBTRACT: key = SCK_SUBTRACT; break;
576 case WXK_DIVIDE: // fall through
577 case WXK_NUMPAD_DIVIDE: key = SCK_DIVIDE; break;
578 case WXK_CONTROL: key = 0; break;
579 case WXK_ALT: key = 0; break;
580 case WXK_SHIFT: key = 0; break;
581 case WXK_MENU: key = 0; break;
582 }
583
584 int rv = KeyDown(key, shift, ctrl, alt, consumed);
585
586 if (key)
587 return rv;
588 else
589 return 1;
590}
591
592
593void ScintillaWX::DoCommand(int ID) {
594 Command(ID);
595}
596
597
598void ScintillaWX::DoContextMenu(Point pt) {
599 if (displayPopupMenu)
600 ContextMenu(pt);
601}
602
603void ScintillaWX::DoOnListBox() {
604 AutoCompleteCompleted();
605}
606
607//----------------------------------------------------------------------
608
609#if wxUSE_DRAG_AND_DROP
610bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
611 SetDragPosition(invalidPosition);
612
613 // Send an event to allow the drag details to be changed
614 wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId());
615 evt.SetEventObject(stc);
616 evt.SetDragResult(dragResult);
617 evt.SetX(x);
618 evt.SetY(y);
619 evt.SetPosition(PositionFromLocation(Point(x,y)));
620 evt.SetDragText(data);
621 stc->GetEventHandler()->ProcessEvent(evt);
622
623 dragResult = evt.GetDragResult();
624 if (dragResult == wxDragMove || dragResult == wxDragCopy) {
625 DropAt(evt.GetPosition(),
626 wx2stc(evt.GetDragText()),
627 dragResult == wxDragMove,
628 FALSE); // TODO: rectangular?
629 return TRUE;
630 }
631 return FALSE;
632}
633
634
635wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) {
636 dragResult = def;
637 return dragResult;
638}
639
640
641wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
642 SetDragPosition(PositionFromLocation(Point(x, y)));
643
644 // Send an event to allow the drag result to be changed
645 wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId());
646 evt.SetEventObject(stc);
647 evt.SetDragResult(def);
648 evt.SetX(x);
649 evt.SetY(y);
650 evt.SetPosition(PositionFromLocation(Point(x,y)));
651 stc->GetEventHandler()->ProcessEvent(evt);
652
653 dragResult = evt.GetDragResult();
654 return dragResult;
655}
656
657
658void ScintillaWX::DoDragLeave() {
659 SetDragPosition(invalidPosition);
660}
661#endif
662//----------------------------------------------------------------------
663
664// Redraw all of text area. This paint will not be abandoned.
665void ScintillaWX::FullPaint() {
666 paintState = painting;
667 rcPaint = GetTextRectangle();
668 paintingAllText = true;
669 wxClientDC dc(stc);
670 Surface* surfaceWindow = Surface::Allocate();
671 surfaceWindow->Init(&dc);
672 Paint(surfaceWindow, rcPaint);
673 delete surfaceWindow;
674
675// stc->Refresh(FALSE);
676
677 paintState = notPainting;
678}
679
680
681void ScintillaWX::DoScrollToLine(int line) {
682 ScrollTo(line);
683}
684
685
686void ScintillaWX::DoScrollToColumn(int column) {
687 HorizontalScrollTo(column * vs.spaceWidth);
688}
689
690
691
692//----------------------------------------------------------------------
693//----------------------------------------------------------------------