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