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