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