]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/ScintillaWX.cpp
Added MouseWheel support to wxSTC
[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
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
64
65 class wxSTCCallTip : public wxWindow {
66 public:
67 wxSTCCallTip(wxWindow* parent, int ID, CallTip* ct)
68 : wxWindow(parent, ID)
69 {
70 m_ct = ct;
71 }
72
73 void OnPaint(wxPaintEvent& evt) {
74 wxPaintDC dc(this);
75 Surface surfaceWindow;
76 surfaceWindow.Init(&dc);
77 m_ct->PaintCT(&surfaceWindow);
78 surfaceWindow.Release();
79 }
80
81 CallTip* m_ct;
82 DECLARE_EVENT_TABLE()
83 };
84
85 BEGIN_EVENT_TABLE(wxSTCCallTip, wxWindow)
86 EVT_PAINT(wxSTCCallTip::OnPaint)
87 END_EVENT_TABLE()
88
89 //----------------------------------------------------------------------
90 // Constructor/Destructor
91
92
93 ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
94 capturedMouse = false;
95 wMain = win;
96 wDraw = 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 dropTarget = new wxSTCDropTarget;
114 dropTarget->SetScintilla(this);
115 stc->SetDropTarget(dropTarget);
116 }
117
118
119 void ScintillaWX::Finalise() {
120 ScintillaBase::Finalise();
121 }
122
123
124 void ScintillaWX::StartDrag() {
125 wxDropSource source(wMain.GetID());
126 wxTextDataObject data(dragChars);
127 wxDragResult result;
128
129 source.SetData(data);
130 result = source.DoDragDrop(TRUE);
131 if (result == wxDragMove && dropWentOutside)
132 ClearSelection();
133 inDragDrop = FALSE;
134 SetDragPosition(invalidPosition);
135 }
136
137
138 void ScintillaWX::SetTicking(bool on) {
139 wxSTCTimer* steTimer;
140 if (timer.ticking != on) {
141 timer.ticking = on;
142 if (timer.ticking) {
143 steTimer = new wxSTCTimer(this);
144 steTimer->Start(timer.tickSize);
145 timer.tickerID = (int)steTimer;
146 } else {
147 steTimer = (wxSTCTimer*)timer.tickerID;
148 steTimer->Stop();
149 delete steTimer;
150 timer.tickerID = 0;
151 }
152 }
153 timer.ticksToWait = caret.period;
154 }
155
156
157 void ScintillaWX::SetMouseCapture(bool on) {
158 if (on && !capturedMouse)
159 wMain.GetID()->CaptureMouse();
160 else if (!on && capturedMouse)
161 wMain.GetID()->ReleaseMouse();
162 capturedMouse = on;
163 }
164
165
166 bool ScintillaWX::HaveMouseCapture() {
167 return capturedMouse;
168 }
169
170
171 void ScintillaWX::ScrollText(int linesToMove) {
172 int dy = vs.lineHeight * (linesToMove);
173 // TODO: calculate the rectangle to refreshed...
174 wMain.GetID()->ScrollWindow(0, dy);
175 }
176
177 void ScintillaWX::SetVerticalScrollPos() {
178 wMain.GetID()->SetScrollPos(wxVERTICAL, topLine);
179 }
180
181 void ScintillaWX::SetHorizontalScrollPos() {
182 wMain.GetID()->SetScrollPos(wxHORIZONTAL, xOffset);
183 }
184
185
186 bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) {
187 bool modified = false;
188 int sbMax = wMain.GetID()->GetScrollRange(wxVERTICAL);
189 int sbThumb = wMain.GetID()->GetScrollThumb(wxVERTICAL);
190 int sbPos = wMain.GetID()->GetScrollPos(wxVERTICAL);
191
192
193 if (sbMax != nMax || sbThumb != nPage) {
194 wMain.GetID()->SetScrollbar(wxVERTICAL, sbPos, nPage, nMax);
195 modified = true;
196 }
197
198 sbMax = wMain.GetID()->GetScrollRange(wxHORIZONTAL);
199 sbThumb = wMain.GetID()->GetScrollThumb(wxHORIZONTAL);
200 if ((sbMax != H_SCROLL_MAX) || (sbThumb != H_SCROLL_STEP)) {
201 wMain.GetID()->SetScrollbar(wxHORIZONTAL, 0, H_SCROLL_STEP, H_SCROLL_MAX);
202 modified = true;
203 }
204 return modified;
205 }
206
207
208 void ScintillaWX::NotifyChange() {
209 stc->NotifyChange();
210 }
211
212
213 void ScintillaWX::NotifyParent(SCNotification scn) {
214 stc->NotifyParent(&scn);
215 }
216
217
218
219 void ScintillaWX::Copy() {
220 if (currentPos != anchor) {
221 char* text = CopySelectionRange();
222 wxTheClipboard->Open();
223 wxTheClipboard->SetData(new wxTextDataObject(text));
224 wxTheClipboard->Close();
225 }
226 }
227
228
229 void ScintillaWX::Paste() {
230 pdoc->BeginUndoAction();
231 ClearSelection();
232
233 wxTextDataObject data;
234 bool canPaste;
235
236 wxTheClipboard->Open();
237 canPaste = wxTheClipboard->GetData(data);
238 wxTheClipboard->Close();
239 if (canPaste) {
240 wxString str = data.GetText();
241 int len = str.Length();
242 pdoc->InsertString(currentPos, str.c_str(), len);
243 SetEmptySelection(currentPos + len);
244 }
245
246 pdoc->EndUndoAction();
247 NotifyChange();
248 Redraw();
249 }
250
251
252 bool ScintillaWX::CanPaste() {
253 wxTextDataObject data;
254 bool canPaste;
255
256 wxTheClipboard->Open();
257 canPaste = wxTheClipboard->GetData(data);
258 wxTheClipboard->Close();
259
260 return canPaste;
261 }
262
263 void ScintillaWX::CreateCallTipWindow(PRectangle) {
264 ct.wCallTip = new wxSTCCallTip(wDraw.GetID(), -1, &ct);
265 ct.wDraw = ct.wCallTip;
266 }
267
268
269 void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) {
270 if (!label[0])
271 popup.GetID()->AppendSeparator();
272 else
273 popup.GetID()->Append(cmd, label);
274
275 if (!enabled)
276 popup.GetID()->Enable(cmd, enabled);
277 }
278
279
280 void ScintillaWX::ClaimSelection() {
281
282 }
283
284
285 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
286 return 0;
287 }
288
289 long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) {
290 // switch (iMessage) {
291 // case EM_CANPASTE:
292 // return CanPaste();
293 // default:
294 return ScintillaBase::WndProc(iMessage, wParam, lParam);
295 // }
296 // return 0;
297 }
298
299
300
301 //----------------------------------------------------------------------
302 // Event delegates
303
304 void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
305
306 paintState = painting;
307 Surface surfaceWindow;
308 surfaceWindow.Init(dc);
309 PRectangle rcPaint = PRectangleFromwxRect(rect);
310 dc->BeginDrawing();
311 Paint(&surfaceWindow, rcPaint);
312 dc->EndDrawing();
313 surfaceWindow.Release();
314 if (paintState == paintAbandoned) {
315 // Painting area was insufficient to cover new styling or brace highlight positions
316 FullPaint();
317 }
318 paintState = notPainting;
319 #ifdef __WXGTK__
320 // On wxGTK the editor window paints can overwrite the listbox...
321 if (ac.Active())
322 ((wxWindow*)ac.lb.GetID())->Refresh(TRUE);
323 #endif
324 }
325
326
327 void ScintillaWX::DoHScroll(int type, int pos) {
328 int xPos = xOffset;
329 if (type == wxEVT_SCROLLWIN_LINEUP)
330 xPos -= H_SCROLL_STEP;
331 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
332 xPos += H_SCROLL_STEP;
333 else if (type == wxEVT_SCROLLWIN_PAGEUP)
334 xPos -= H_SCROLL_PAGE;
335 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
336 xPos += H_SCROLL_PAGE;
337 else if (type == wxEVT_SCROLLWIN_TOP)
338 xPos = 0;
339 else if (type == wxEVT_SCROLLWIN_BOTTOM)
340 xPos = H_SCROLL_MAX;
341 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
342 xPos = pos;
343
344 HorizontalScrollTo(xPos);
345 }
346
347 void ScintillaWX::DoVScroll(int type, int pos) {
348 int topLineNew = topLine;
349 if (type == wxEVT_SCROLLWIN_LINEUP)
350 topLineNew -= 1;
351 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
352 topLineNew += 1;
353 else if (type == wxEVT_SCROLLWIN_PAGEUP)
354 topLineNew -= LinesToScroll();
355 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
356 topLineNew += LinesToScroll();
357 else if (type == wxEVT_SCROLLWIN_TOP)
358 topLineNew = 0;
359 else if (type == wxEVT_SCROLLWIN_BOTTOM)
360 topLineNew = MaxScrollPos();
361 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
362 topLineNew = pos;
363
364 ScrollTo(topLineNew);
365 }
366
367
368 void ScintillaWX::DoMouseWheel(int rotation, int delta, int linesPerAction) {
369 int topLineNew = topLine;
370 int lines;
371
372 wheelRotation += rotation;
373 lines = wheelRotation / delta;
374 wheelRotation -= lines * delta;
375 if (lines != 0) {
376 lines *= linesPerAction;
377 topLineNew -= lines;
378 ScrollTo(topLineNew);
379 }
380 }
381
382
383 void ScintillaWX::DoSize(int width, int height) {
384 PRectangle rcClient(0,0,width,height);
385 SetScrollBarsTo(rcClient);
386 DropGraphics();
387 }
388
389 void ScintillaWX::DoLoseFocus(){
390 DropCaret();
391 }
392
393 void ScintillaWX::DoGainFocus(){
394 ShowCaretAtCurrentPosition();
395 }
396
397 void ScintillaWX::DoSysColourChange() {
398 InvalidateStyleData();
399 }
400
401 void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
402 ButtonDown(pt, curTime, shift, ctrl, alt);
403 }
404
405 void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) {
406 ButtonUp(pt, curTime, ctrl);
407 }
408
409 void ScintillaWX::DoButtonMove(Point pt) {
410 ButtonMove(pt);
411 }
412
413
414 void ScintillaWX::DoAddChar(char ch) {
415 //bool acActiveBeforeCharAdded = ac.Active();
416 AddChar(ch);
417 //if (acActiveBeforeCharAdded)
418 // AutoCompleteChanged(ch);
419 }
420
421 int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt) {
422 switch (key) {
423 case WXK_DOWN: key = SCK_DOWN; break;
424 case WXK_UP: key = SCK_UP; break;
425 case WXK_LEFT: key = SCK_LEFT; break;
426 case WXK_RIGHT: key = SCK_RIGHT; break;
427 case WXK_HOME: key = SCK_HOME; break;
428 case WXK_END: key = SCK_END; break;
429 case WXK_PRIOR: key = SCK_PRIOR; break;
430 case WXK_NEXT: key = SCK_NEXT; break;
431 case WXK_DELETE: key = SCK_DELETE; break;
432 case WXK_INSERT: key = SCK_INSERT; break;
433 case WXK_ESCAPE: key = SCK_ESCAPE; break;
434 case WXK_BACK: key = SCK_BACK; break;
435 case WXK_TAB: key = SCK_TAB; break;
436 case WXK_RETURN: key = SCK_RETURN; break;
437 case WXK_ADD: key = SCK_ADD; break;
438 case WXK_SUBTRACT: key = SCK_SUBTRACT; break;
439 case WXK_DIVIDE: key = SCK_DIVIDE; break;
440 case WXK_CONTROL: key = 0; break;
441 case WXK_ALT: key = 0; break;
442 case WXK_SHIFT: key = 0; break;
443 }
444
445 return KeyDown(key, shift, ctrl, alt);
446 }
447
448
449 void ScintillaWX::DoCommand(int ID) {
450 Command(ID);
451 }
452
453
454 void ScintillaWX::DoContextMenu(Point pt) {
455 ContextMenu(pt);
456 }
457
458 void ScintillaWX::DoOnListBox() {
459 AutoCompleteCompleted();
460 }
461
462 //----------------------------------------------------------------------
463
464 bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
465 SetDragPosition(invalidPosition);
466 int movePos = PositionFromLocation(Point(x,y));
467 DropAt(movePos, data, dragResult == wxDragMove, FALSE); // TODO: rectangular?
468 return TRUE;
469 }
470
471
472 wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) {
473 return def;
474 }
475
476
477 wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
478 SetDragPosition(PositionFromLocation(Point(x, y)));
479 dragResult = def;
480 return def;
481 }
482
483
484 void ScintillaWX::DoDragLeave() {
485 SetDragPosition(invalidPosition);
486 }
487
488 //----------------------------------------------------------------------
489
490 // Redraw all of text area. This paint will not be abandoned.
491 void ScintillaWX::FullPaint() {
492 paintState = painting;
493 rcPaint = GetTextRectangle();
494 paintingAllText = true;
495 wxClientDC dc(wMain.GetID());
496 Surface surfaceWindow;
497 surfaceWindow.Init(&dc);
498 Paint(&surfaceWindow, rcPaint);
499 surfaceWindow.Release();
500
501 // wMain.GetID()->Refresh(FALSE);
502
503 paintState = notPainting;
504 }
505
506
507 void ScintillaWX::DoScrollToLine(int line) {
508 ScrollTo(line);
509 }
510
511
512 void ScintillaWX::DoScrollToColumn(int column) {
513 HorizontalScrollTo(column * vs.spaceWidth);
514 }
515
516
517
518 //----------------------------------------------------------------------
519 //----------------------------------------------------------------------