]>
Commit | Line | Data |
---|---|---|
9ce192d4 RD |
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 | ||
f6bcfd97 BP |
17 | #include <ctype.h> |
18 | ||
9ce192d4 RD |
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 | ||
f6bcfd97 BP |
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() | |
9ce192d4 RD |
88 | |
89 | //---------------------------------------------------------------------- | |
90 | // Constructor/Destructor | |
91 | ||
92 | ||
93 | ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) { | |
94 | capturedMouse = false; | |
95 | wMain = win; | |
9ce192d4 | 96 | stc = win; |
37d62433 | 97 | wheelRotation = 0; |
9ce192d4 RD |
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(); | |
9eb662e9 RD |
112 | dropTarget = new wxSTCDropTarget; |
113 | dropTarget->SetScintilla(this); | |
114 | stc->SetDropTarget(dropTarget); | |
9ce192d4 RD |
115 | } |
116 | ||
117 | ||
118 | void ScintillaWX::Finalise() { | |
119 | ScintillaBase::Finalise(); | |
120 | } | |
121 | ||
122 | ||
123 | void ScintillaWX::StartDrag() { | |
8759d4d5 | 124 | wxDropSource source(wMain.GetID()); |
b8b0e402 | 125 | wxTextDataObject data(wxString(drag.s, drag.len)); |
9ce192d4 RD |
126 | wxDragResult result; |
127 | ||
b8b0e402 | 128 | dropWentOutside = true; |
9ce192d4 RD |
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) { | |
8759d4d5 | 158 | if (on && !capturedMouse) |
9ce192d4 | 159 | wMain.GetID()->CaptureMouse(); |
8759d4d5 | 160 | else if (!on && capturedMouse) |
9ce192d4 RD |
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); | |
9ce192d4 | 173 | wMain.GetID()->ScrollWindow(0, dy); |
65ec6247 | 174 | wMain.GetID()->Update(); |
9ce192d4 RD |
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) { | |
b8b0e402 RD |
221 | SelectionText st; |
222 | CopySelectionRange(&st); | |
9ce192d4 | 223 | wxTheClipboard->Open(); |
b8b0e402 | 224 | wxTheClipboard->SetData(new wxTextDataObject(wxString(st.s, st.len))); |
9ce192d4 RD |
225 | wxTheClipboard->Close(); |
226 | } | |
227 | } | |
228 | ||
229 | ||
230 | void ScintillaWX::Paste() { | |
231 | pdoc->BeginUndoAction(); | |
232 | ClearSelection(); | |
233 | ||
234 | wxTextDataObject data; | |
235 | bool canPaste; | |
236 | ||
237 | wxTheClipboard->Open(); | |
238 | canPaste = wxTheClipboard->GetData(data); | |
239 | wxTheClipboard->Close(); | |
240 | if (canPaste) { | |
241 | wxString str = data.GetText(); | |
242 | int len = str.Length(); | |
243 | pdoc->InsertString(currentPos, str.c_str(), len); | |
244 | SetEmptySelection(currentPos + len); | |
245 | } | |
246 | ||
247 | pdoc->EndUndoAction(); | |
248 | NotifyChange(); | |
249 | Redraw(); | |
250 | } | |
251 | ||
252 | ||
253 | bool ScintillaWX::CanPaste() { | |
254 | wxTextDataObject data; | |
255 | bool canPaste; | |
256 | ||
257 | wxTheClipboard->Open(); | |
258 | canPaste = wxTheClipboard->GetData(data); | |
259 | wxTheClipboard->Close(); | |
260 | ||
261 | return canPaste; | |
262 | } | |
263 | ||
264 | void ScintillaWX::CreateCallTipWindow(PRectangle) { | |
65ec6247 | 265 | ct.wCallTip = new wxSTCCallTip(wMain.GetID(), -1, &ct); |
9ce192d4 RD |
266 | ct.wDraw = ct.wCallTip; |
267 | } | |
268 | ||
269 | ||
270 | void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) { | |
271 | if (!label[0]) | |
272 | popup.GetID()->AppendSeparator(); | |
273 | else | |
274 | popup.GetID()->Append(cmd, label); | |
275 | ||
276 | if (!enabled) | |
277 | popup.GetID()->Enable(cmd, enabled); | |
9ce192d4 RD |
278 | } |
279 | ||
280 | ||
281 | void ScintillaWX::ClaimSelection() { | |
282 | ||
283 | } | |
284 | ||
285 | ||
d134f170 | 286 | long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) { |
9ce192d4 RD |
287 | return 0; |
288 | } | |
289 | ||
d134f170 RD |
290 | long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) { |
291 | // switch (iMessage) { | |
292 | // case EM_CANPASTE: | |
293 | // return CanPaste(); | |
294 | // default: | |
9ce192d4 | 295 | return ScintillaBase::WndProc(iMessage, wParam, lParam); |
d134f170 RD |
296 | // } |
297 | // return 0; | |
9ce192d4 RD |
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; | |
f97d84a6 RD |
320 | #ifdef __WXGTK__ |
321 | // On wxGTK the editor window paints can overwrite the listbox... | |
322 | if (ac.Active()) | |
323 | ((wxWindow*)ac.lb.GetID())->Refresh(TRUE); | |
324 | #endif | |
9ce192d4 RD |
325 | } |
326 | ||
327 | ||
328 | void ScintillaWX::DoHScroll(int type, int pos) { | |
329 | int xPos = xOffset; | |
ce1ecc6d | 330 | if (type == wxEVT_SCROLLWIN_LINEUP) |
9ce192d4 | 331 | xPos -= H_SCROLL_STEP; |
ce1ecc6d | 332 | else if (type == wxEVT_SCROLLWIN_LINEDOWN) |
9ce192d4 | 333 | xPos += H_SCROLL_STEP; |
ce1ecc6d | 334 | else if (type == wxEVT_SCROLLWIN_PAGEUP) |
9ce192d4 | 335 | xPos -= H_SCROLL_PAGE; |
ce1ecc6d | 336 | else if (type == wxEVT_SCROLLWIN_PAGEDOWN) |
9ce192d4 | 337 | xPos += H_SCROLL_PAGE; |
ce1ecc6d | 338 | else if (type == wxEVT_SCROLLWIN_TOP) |
9ce192d4 | 339 | xPos = 0; |
ce1ecc6d | 340 | else if (type == wxEVT_SCROLLWIN_BOTTOM) |
9ce192d4 | 341 | xPos = H_SCROLL_MAX; |
ce1ecc6d | 342 | else if (type == wxEVT_SCROLLWIN_THUMBTRACK) |
9ce192d4 | 343 | xPos = pos; |
ce1ecc6d | 344 | |
9ce192d4 RD |
345 | HorizontalScrollTo(xPos); |
346 | } | |
347 | ||
348 | void ScintillaWX::DoVScroll(int type, int pos) { | |
349 | int topLineNew = topLine; | |
ce1ecc6d | 350 | if (type == wxEVT_SCROLLWIN_LINEUP) |
9ce192d4 | 351 | topLineNew -= 1; |
ce1ecc6d | 352 | else if (type == wxEVT_SCROLLWIN_LINEDOWN) |
9ce192d4 | 353 | topLineNew += 1; |
ce1ecc6d | 354 | else if (type == wxEVT_SCROLLWIN_PAGEUP) |
9ce192d4 | 355 | topLineNew -= LinesToScroll(); |
ce1ecc6d | 356 | else if (type == wxEVT_SCROLLWIN_PAGEDOWN) |
9ce192d4 | 357 | topLineNew += LinesToScroll(); |
ce1ecc6d | 358 | else if (type == wxEVT_SCROLLWIN_TOP) |
9ce192d4 | 359 | topLineNew = 0; |
ce1ecc6d | 360 | else if (type == wxEVT_SCROLLWIN_BOTTOM) |
9ce192d4 | 361 | topLineNew = MaxScrollPos(); |
ce1ecc6d | 362 | else if (type == wxEVT_SCROLLWIN_THUMBTRACK) |
9ce192d4 | 363 | topLineNew = pos; |
ce1ecc6d | 364 | |
9ce192d4 RD |
365 | ScrollTo(topLineNew); |
366 | } | |
367 | ||
37d62433 | 368 | |
65ec6247 | 369 | void ScintillaWX::DoMouseWheel(int rotation, int delta, int linesPerAction, int ctrlDown) { |
37d62433 RD |
370 | int topLineNew = topLine; |
371 | int lines; | |
372 | ||
65ec6247 RD |
373 | if (ctrlDown) { // Zoom the fonts if Ctrl key down |
374 | if (rotation < 0) { | |
375 | KeyCommand(SCI_ZOOMIN); | |
376 | } | |
377 | else { | |
378 | KeyCommand(SCI_ZOOMOUT); | |
379 | } | |
380 | } | |
381 | else { // otherwise just scroll the window | |
382 | wheelRotation += rotation; | |
383 | lines = wheelRotation / delta; | |
384 | wheelRotation -= lines * delta; | |
385 | if (lines != 0) { | |
386 | lines *= linesPerAction; | |
387 | topLineNew -= lines; | |
388 | ScrollTo(topLineNew); | |
389 | } | |
37d62433 RD |
390 | } |
391 | } | |
392 | ||
393 | ||
9ce192d4 RD |
394 | void ScintillaWX::DoSize(int width, int height) { |
395 | PRectangle rcClient(0,0,width,height); | |
396 | SetScrollBarsTo(rcClient); | |
397 | DropGraphics(); | |
398 | } | |
399 | ||
400 | void ScintillaWX::DoLoseFocus(){ | |
65ec6247 | 401 | SetFocusState(false); |
9ce192d4 RD |
402 | } |
403 | ||
404 | void ScintillaWX::DoGainFocus(){ | |
65ec6247 | 405 | SetFocusState(true); |
9ce192d4 RD |
406 | } |
407 | ||
408 | void ScintillaWX::DoSysColourChange() { | |
409 | InvalidateStyleData(); | |
410 | } | |
411 | ||
412 | void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) { | |
413 | ButtonDown(pt, curTime, shift, ctrl, alt); | |
414 | } | |
415 | ||
416 | void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) { | |
417 | ButtonUp(pt, curTime, ctrl); | |
418 | } | |
419 | ||
420 | void ScintillaWX::DoButtonMove(Point pt) { | |
421 | ButtonMove(pt); | |
422 | } | |
423 | ||
424 | ||
425 | void ScintillaWX::DoAddChar(char ch) { | |
426 | AddChar(ch); | |
427 | } | |
428 | ||
65ec6247 | 429 | int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt, bool* consumed) { |
39178e3b RD |
430 | #ifdef __WXGTK__ |
431 | // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK... | |
432 | if (ctrl && key >= 1 && key <= 26) | |
433 | key += 'A' - 1; | |
434 | #endif | |
435 | ||
d134f170 | 436 | switch (key) { |
d6582821 RD |
437 | case WXK_DOWN: key = SCK_DOWN; break; |
438 | case WXK_UP: key = SCK_UP; break; | |
439 | case WXK_LEFT: key = SCK_LEFT; break; | |
440 | case WXK_RIGHT: key = SCK_RIGHT; break; | |
441 | case WXK_HOME: key = SCK_HOME; break; | |
442 | case WXK_END: key = SCK_END; break; | |
443 | case WXK_PRIOR: key = SCK_PRIOR; break; | |
444 | case WXK_NEXT: key = SCK_NEXT; break; | |
445 | case WXK_DELETE: key = SCK_DELETE; break; | |
446 | case WXK_INSERT: key = SCK_INSERT; break; | |
447 | case WXK_ESCAPE: key = SCK_ESCAPE; break; | |
448 | case WXK_BACK: key = SCK_BACK; break; | |
449 | case WXK_TAB: key = SCK_TAB; break; | |
450 | case WXK_RETURN: key = SCK_RETURN; break; | |
451 | case WXK_ADD: | |
452 | case WXK_NUMPAD_ADD: | |
453 | key = SCK_ADD; break; | |
454 | case WXK_SUBTRACT: | |
455 | case WXK_NUMPAD_SUBTRACT: | |
456 | key = SCK_SUBTRACT; break; | |
457 | case WXK_DIVIDE: | |
458 | case WXK_NUMPAD_DIVIDE: | |
459 | key = SCK_DIVIDE; break; | |
460 | case WXK_CONTROL: key = 0; break; | |
461 | case WXK_ALT: key = 0; break; | |
462 | case WXK_SHIFT: key = 0; break; | |
463 | case WXK_MENU: key = 0; break; | |
d134f170 RD |
464 | } |
465 | ||
d6582821 | 466 | int rv = KeyDown(key, shift, ctrl, alt, consumed); |
0122b7e3 | 467 | |
d6582821 RD |
468 | if (key) |
469 | return rv; | |
470 | else | |
471 | return 1; | |
9ce192d4 RD |
472 | } |
473 | ||
474 | ||
475 | void ScintillaWX::DoCommand(int ID) { | |
476 | Command(ID); | |
477 | } | |
478 | ||
479 | ||
480 | void ScintillaWX::DoContextMenu(Point pt) { | |
481 | ContextMenu(pt); | |
482 | } | |
483 | ||
f6bcfd97 BP |
484 | void ScintillaWX::DoOnListBox() { |
485 | AutoCompleteCompleted(); | |
486 | } | |
9ce192d4 RD |
487 | |
488 | //---------------------------------------------------------------------- | |
489 | ||
490 | bool ScintillaWX::DoDropText(long x, long y, const wxString& data) { | |
491 | SetDragPosition(invalidPosition); | |
492 | int movePos = PositionFromLocation(Point(x,y)); | |
493 | DropAt(movePos, data, dragResult == wxDragMove, FALSE); // TODO: rectangular? | |
494 | return TRUE; | |
495 | } | |
496 | ||
497 | ||
498 | wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) { | |
b8b0e402 RD |
499 | dragResult = def; |
500 | return dragResult; | |
9ce192d4 RD |
501 | } |
502 | ||
503 | ||
504 | wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) { | |
505 | SetDragPosition(PositionFromLocation(Point(x, y))); | |
506 | dragResult = def; | |
b8b0e402 | 507 | return dragResult; |
9ce192d4 RD |
508 | } |
509 | ||
510 | ||
511 | void ScintillaWX::DoDragLeave() { | |
512 | SetDragPosition(invalidPosition); | |
513 | } | |
514 | ||
515 | //---------------------------------------------------------------------- | |
516 | ||
517 | // Redraw all of text area. This paint will not be abandoned. | |
518 | void ScintillaWX::FullPaint() { | |
519 | paintState = painting; | |
a7642be1 RD |
520 | rcPaint = GetTextRectangle(); |
521 | paintingAllText = true; | |
522 | wxClientDC dc(wMain.GetID()); | |
523 | Surface surfaceWindow; | |
524 | surfaceWindow.Init(&dc); | |
525 | Paint(&surfaceWindow, rcPaint); | |
526 | surfaceWindow.Release(); | |
527 | ||
528 | // wMain.GetID()->Refresh(FALSE); | |
529 | ||
9ce192d4 RD |
530 | paintState = notPainting; |
531 | } | |
532 | ||
533 | ||
534 | void ScintillaWX::DoScrollToLine(int line) { | |
535 | ScrollTo(line); | |
536 | } | |
537 | ||
538 | ||
539 | void ScintillaWX::DoScrollToColumn(int column) { | |
540 | HorizontalScrollTo(column * vs.spaceWidth); | |
541 | } | |
542 | ||
543 | ||
544 | ||
545 | //---------------------------------------------------------------------- | |
546 | //---------------------------------------------------------------------- |