]> git.saurik.com Git - wxWidgets.git/blob - src/stc/ScintillaWX.cpp
hasSubDirs already defined in dircmn
[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 = 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 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(stc);
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 stc->CaptureMouse();
179 else if (!on && capturedMouse)
180 stc->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 stc->ScrollWindow(0, dy);
193 stc->Update();
194 }
195
196 void ScintillaWX::SetVerticalScrollPos() {
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 }
203 }
204
205 void ScintillaWX::SetHorizontalScrollPos() {
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 }
212 }
213
214
215 bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) {
216 bool modified = false;
217
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 }
235 }
236
237
238 if (horizontalScrollBarVisible) {
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 }
254 }
255 }
256 return modified;
257 }
258
259
260 void ScintillaWX::NotifyChange() {
261 stc->NotifyChange();
262 }
263
264
265 void ScintillaWX::NotifyParent(SCNotification scn) {
266 stc->NotifyParent(&scn);
267 }
268
269
270
271 void ScintillaWX::Copy() {
272 if (currentPos != anchor) {
273 SelectionText st;
274 CopySelectionRange(&st);
275 wxTheClipboard->Open();
276 wxTheClipboard->SetData(new wxTextDataObject(wxString(st.s, st.len)));
277 wxTheClipboard->Close();
278 }
279 }
280
281
282 void ScintillaWX::Paste() {
283 pdoc->BeginUndoAction();
284 ClearSelection();
285
286 wxTextDataObject data;
287 bool gotData;
288
289 wxTheClipboard->Open();
290 gotData = wxTheClipboard->GetData(data);
291 wxTheClipboard->Close();
292 if (gotData) {
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
305 bool ScintillaWX::CanPaste() {
306 bool canPaste;
307
308 wxTheClipboard->Open();
309 canPaste = wxTheClipboard->IsSupported( wxDF_TEXT );
310 wxTheClipboard->Close();
311
312 return canPaste;
313 }
314
315 void ScintillaWX::CreateCallTipWindow(PRectangle) {
316 ct.wCallTip = new wxSTCCallTip(stc, -1, &ct);
317 ct.wDraw = ct.wCallTip;
318 }
319
320
321 void 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);
329 }
330
331
332 void ScintillaWX::ClaimSelection() {
333
334 }
335
336
337 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
338 return 0;
339 }
340
341 long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) {
342 // switch (iMessage) {
343 // case EM_CANPASTE:
344 // return CanPaste();
345 // default:
346 return ScintillaBase::WndProc(iMessage, wParam, lParam);
347 // }
348 // return 0;
349 }
350
351
352
353 //----------------------------------------------------------------------
354 // Event delegates
355
356 void 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;
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
376 }
377
378
379 void ScintillaWX::DoHScroll(int type, int pos) {
380 int xPos = xOffset;
381 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
382 xPos -= H_SCROLL_STEP;
383 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
384 xPos += H_SCROLL_STEP;
385 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
386 xPos -= H_SCROLL_PAGE;
387 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
388 xPos += H_SCROLL_PAGE;
389 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
390 xPos = 0;
391 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
392 xPos = H_SCROLL_MAX;
393 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
394 xPos = pos;
395
396 HorizontalScrollTo(xPos);
397 }
398
399 void ScintillaWX::DoVScroll(int type, int pos) {
400 int topLineNew = topLine;
401 if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP)
402 topLineNew -= 1;
403 else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN)
404 topLineNew += 1;
405 else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP)
406 topLineNew -= LinesToScroll();
407 else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN)
408 topLineNew += LinesToScroll();
409 else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP)
410 topLineNew = 0;
411 else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM)
412 topLineNew = MaxScrollPos();
413 else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK)
414 topLineNew = pos;
415
416 ScrollTo(topLineNew);
417 }
418
419
420 void ScintillaWX::DoMouseWheel(int rotation, int delta, int linesPerAction, int ctrlDown) {
421 int topLineNew = topLine;
422 int lines;
423
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 }
441 }
442 }
443
444
445 void ScintillaWX::DoSize(int width, int height) {
446 PRectangle rcClient(0,0,width,height);
447 SetScrollBarsTo(rcClient);
448 DropGraphics();
449 }
450
451 void ScintillaWX::DoLoseFocus(){
452 SetFocusState(false);
453 }
454
455 void ScintillaWX::DoGainFocus(){
456 SetFocusState(true);
457 }
458
459 void ScintillaWX::DoSysColourChange() {
460 InvalidateStyleData();
461 }
462
463 void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
464 ButtonDown(pt, curTime, shift, ctrl, alt);
465 }
466
467 void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) {
468 ButtonUp(pt, curTime, ctrl);
469 }
470
471 void ScintillaWX::DoButtonMove(Point pt) {
472 ButtonMove(pt);
473 }
474
475
476 void ScintillaWX::DoAddChar(char ch) {
477 AddChar(ch);
478 }
479
480 int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt, bool* consumed) {
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
487 switch (key) {
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;
515 }
516
517 int rv = KeyDown(key, shift, ctrl, alt, consumed);
518
519 if (key)
520 return rv;
521 else
522 return 1;
523 }
524
525
526 void ScintillaWX::DoCommand(int ID) {
527 Command(ID);
528 }
529
530
531 void ScintillaWX::DoContextMenu(Point pt) {
532 ContextMenu(pt);
533 }
534
535 void ScintillaWX::DoOnListBox() {
536 AutoCompleteCompleted();
537 }
538
539 //----------------------------------------------------------------------
540
541 #if wxUSE_DRAG_AND_DROP
542 bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
543 SetDragPosition(invalidPosition);
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;
564 }
565
566
567 wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) {
568 dragResult = def;
569 return dragResult;
570 }
571
572
573 wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
574 SetDragPosition(PositionFromLocation(Point(x, y)));
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();
586 return dragResult;
587 }
588
589
590 void ScintillaWX::DoDragLeave() {
591 SetDragPosition(invalidPosition);
592 }
593 #endif
594 //----------------------------------------------------------------------
595
596 // Redraw all of text area. This paint will not be abandoned.
597 void ScintillaWX::FullPaint() {
598 paintState = painting;
599 rcPaint = GetTextRectangle();
600 paintingAllText = true;
601 wxClientDC dc(stc);
602 Surface surfaceWindow;
603 surfaceWindow.Init(&dc);
604 Paint(&surfaceWindow, rcPaint);
605 surfaceWindow.Release();
606
607 // stc->Refresh(FALSE);
608
609 paintState = notPainting;
610 }
611
612
613 void ScintillaWX::DoScrollToLine(int line) {
614 ScrollTo(line);
615 }
616
617
618 void ScintillaWX::DoScrollToColumn(int column) {
619 HorizontalScrollTo(column * vs.spaceWidth);
620 }
621
622
623
624 //----------------------------------------------------------------------
625 //----------------------------------------------------------------------