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