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