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