]>
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 | ||
17 | #include "ScintillaWX.h" | |
18 | #include "wx/stc/stc.h" | |
19 | ||
20 | ||
21 | //---------------------------------------------------------------------- | |
22 | ||
23 | const int H_SCROLL_MAX = 2000; | |
24 | const int H_SCROLL_STEP = 20; | |
25 | const int H_SCROLL_PAGE = 200; | |
26 | ||
27 | //---------------------------------------------------------------------- | |
28 | // Helper classes | |
29 | ||
30 | class wxSTCTimer : public wxTimer { | |
31 | public: | |
32 | wxSTCTimer(ScintillaWX* swx) { | |
33 | this->swx = swx; | |
34 | } | |
35 | ||
36 | void Notify() { | |
37 | swx->DoTick(); | |
38 | } | |
39 | ||
40 | private: | |
41 | ScintillaWX* swx; | |
42 | }; | |
43 | ||
44 | ||
45 | ||
46 | bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) { | |
47 | return swx->DoDropText(x, y, data); | |
48 | } | |
49 | ||
50 | wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) { | |
51 | return swx->DoDragEnter(x, y, def); | |
52 | } | |
53 | ||
54 | wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) { | |
55 | return swx->DoDragOver(x, y, def); | |
56 | } | |
57 | ||
58 | void wxSTCDropTarget::OnLeave() { | |
59 | swx->DoDragLeave(); | |
60 | } | |
61 | ||
62 | ||
63 | ||
64 | //---------------------------------------------------------------------- | |
65 | // Constructor/Destructor | |
66 | ||
67 | ||
68 | ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) { | |
69 | capturedMouse = false; | |
70 | wMain = win; | |
71 | wDraw = win; | |
72 | stc = win; | |
73 | Initialise(); | |
74 | } | |
75 | ||
76 | ||
77 | ScintillaWX::~ScintillaWX() { | |
78 | SetTicking(false); | |
79 | } | |
80 | ||
81 | //---------------------------------------------------------------------- | |
82 | // base class virtuals | |
83 | ||
84 | ||
85 | void ScintillaWX::Initialise() { | |
86 | //ScintillaBase::Initialise(); | |
9eb662e9 RD |
87 | dropTarget = new wxSTCDropTarget; |
88 | dropTarget->SetScintilla(this); | |
89 | stc->SetDropTarget(dropTarget); | |
9ce192d4 RD |
90 | } |
91 | ||
92 | ||
93 | void ScintillaWX::Finalise() { | |
94 | ScintillaBase::Finalise(); | |
95 | } | |
96 | ||
97 | ||
98 | void ScintillaWX::StartDrag() { | |
8759d4d5 | 99 | wxDropSource source(wMain.GetID()); |
9ce192d4 RD |
100 | wxTextDataObject data(dragChars); |
101 | wxDragResult result; | |
102 | ||
103 | source.SetData(data); | |
104 | result = source.DoDragDrop(TRUE); | |
105 | if (result == wxDragMove && dropWentOutside) | |
106 | ClearSelection(); | |
107 | inDragDrop = FALSE; | |
108 | SetDragPosition(invalidPosition); | |
109 | } | |
110 | ||
111 | ||
112 | void ScintillaWX::SetTicking(bool on) { | |
113 | wxSTCTimer* steTimer; | |
114 | if (timer.ticking != on) { | |
115 | timer.ticking = on; | |
116 | if (timer.ticking) { | |
117 | steTimer = new wxSTCTimer(this); | |
118 | steTimer->Start(timer.tickSize); | |
119 | timer.tickerID = (int)steTimer; | |
120 | } else { | |
121 | steTimer = (wxSTCTimer*)timer.tickerID; | |
122 | steTimer->Stop(); | |
123 | delete steTimer; | |
124 | timer.tickerID = 0; | |
125 | } | |
126 | } | |
127 | timer.ticksToWait = caret.period; | |
128 | } | |
129 | ||
130 | ||
131 | void ScintillaWX::SetMouseCapture(bool on) { | |
8759d4d5 | 132 | if (on && !capturedMouse) |
9ce192d4 | 133 | wMain.GetID()->CaptureMouse(); |
8759d4d5 | 134 | else if (!on && capturedMouse) |
9ce192d4 RD |
135 | wMain.GetID()->ReleaseMouse(); |
136 | capturedMouse = on; | |
137 | } | |
138 | ||
139 | ||
140 | bool ScintillaWX::HaveMouseCapture() { | |
141 | return capturedMouse; | |
142 | } | |
143 | ||
144 | ||
145 | void ScintillaWX::ScrollText(int linesToMove) { | |
146 | int dy = vs.lineHeight * (linesToMove); | |
147 | // TODO: calculate the rectangle to refreshed... | |
148 | wMain.GetID()->ScrollWindow(0, dy); | |
149 | } | |
150 | ||
151 | void ScintillaWX::SetVerticalScrollPos() { | |
152 | wMain.GetID()->SetScrollPos(wxVERTICAL, topLine); | |
153 | } | |
154 | ||
155 | void ScintillaWX::SetHorizontalScrollPos() { | |
156 | wMain.GetID()->SetScrollPos(wxHORIZONTAL, xOffset); | |
157 | } | |
158 | ||
159 | ||
160 | bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) { | |
161 | bool modified = false; | |
162 | int sbMax = wMain.GetID()->GetScrollRange(wxVERTICAL); | |
163 | int sbThumb = wMain.GetID()->GetScrollThumb(wxVERTICAL); | |
164 | int sbPos = wMain.GetID()->GetScrollPos(wxVERTICAL); | |
165 | ||
166 | ||
167 | if (sbMax != nMax || sbThumb != nPage) { | |
168 | wMain.GetID()->SetScrollbar(wxVERTICAL, sbPos, nPage, nMax); | |
169 | modified = true; | |
170 | } | |
171 | ||
172 | sbMax = wMain.GetID()->GetScrollRange(wxHORIZONTAL); | |
173 | sbThumb = wMain.GetID()->GetScrollThumb(wxHORIZONTAL); | |
174 | if ((sbMax != H_SCROLL_MAX) || (sbThumb != H_SCROLL_STEP)) { | |
175 | wMain.GetID()->SetScrollbar(wxHORIZONTAL, 0, H_SCROLL_STEP, H_SCROLL_MAX); | |
176 | modified = true; | |
177 | } | |
178 | return modified; | |
179 | } | |
180 | ||
181 | ||
182 | void ScintillaWX::NotifyChange() { | |
183 | stc->NotifyChange(); | |
184 | } | |
185 | ||
186 | ||
187 | void ScintillaWX::NotifyParent(SCNotification scn) { | |
188 | stc->NotifyParent(&scn); | |
189 | } | |
190 | ||
191 | ||
192 | ||
193 | void ScintillaWX::Copy() { | |
194 | if (currentPos != anchor) { | |
195 | char* text = CopySelectionRange(); | |
196 | textDO.SetText(text); | |
197 | wxTheClipboard->Open(); | |
198 | wxTheClipboard->SetData(&textDO); | |
199 | wxTheClipboard->Close(); | |
200 | } | |
201 | } | |
202 | ||
203 | ||
204 | void ScintillaWX::Paste() { | |
205 | pdoc->BeginUndoAction(); | |
206 | ClearSelection(); | |
207 | ||
208 | wxTextDataObject data; | |
209 | bool canPaste; | |
210 | ||
211 | wxTheClipboard->Open(); | |
212 | canPaste = wxTheClipboard->GetData(data); | |
213 | wxTheClipboard->Close(); | |
214 | if (canPaste) { | |
215 | wxString str = data.GetText(); | |
216 | int len = str.Length(); | |
217 | pdoc->InsertString(currentPos, str.c_str(), len); | |
218 | SetEmptySelection(currentPos + len); | |
219 | } | |
220 | ||
221 | pdoc->EndUndoAction(); | |
222 | NotifyChange(); | |
223 | Redraw(); | |
224 | } | |
225 | ||
226 | ||
227 | bool ScintillaWX::CanPaste() { | |
228 | wxTextDataObject data; | |
229 | bool canPaste; | |
230 | ||
231 | wxTheClipboard->Open(); | |
232 | canPaste = wxTheClipboard->GetData(data); | |
233 | wxTheClipboard->Close(); | |
234 | ||
235 | return canPaste; | |
236 | } | |
237 | ||
238 | void ScintillaWX::CreateCallTipWindow(PRectangle) { | |
239 | ct.wCallTip = new wxWindow(wDraw.GetID(), -1); | |
240 | ct.wDraw = ct.wCallTip; | |
241 | } | |
242 | ||
243 | ||
244 | void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) { | |
245 | if (!label[0]) | |
246 | popup.GetID()->AppendSeparator(); | |
247 | else | |
248 | popup.GetID()->Append(cmd, label); | |
249 | ||
250 | if (!enabled) | |
251 | popup.GetID()->Enable(cmd, enabled); | |
252 | ||
253 | // TODO: need to create event handler mappings for the cmd ID | |
254 | } | |
255 | ||
256 | ||
257 | void ScintillaWX::ClaimSelection() { | |
258 | ||
259 | } | |
260 | ||
261 | ||
262 | LRESULT ScintillaWX::DefWndProc(UINT /*iMessage*/, WPARAM /*wParam*/, LPARAM /*lParam*/) { | |
263 | return 0; | |
264 | } | |
265 | ||
266 | LRESULT ScintillaWX::WndProc(UINT iMessage, WPARAM wParam, LPARAM lParam) { | |
267 | switch (iMessage) { | |
268 | case EM_CANPASTE: | |
269 | return CanPaste(); | |
270 | default: | |
271 | return ScintillaBase::WndProc(iMessage, wParam, lParam); | |
272 | } | |
273 | return 0; | |
274 | } | |
275 | ||
276 | ||
277 | ||
278 | //---------------------------------------------------------------------- | |
279 | // Event delegates | |
280 | ||
281 | void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) { | |
282 | ||
283 | paintState = painting; | |
284 | Surface surfaceWindow; | |
285 | surfaceWindow.Init(dc); | |
286 | PRectangle rcPaint = PRectangleFromwxRect(rect); | |
287 | dc->BeginDrawing(); | |
288 | Paint(&surfaceWindow, rcPaint); | |
289 | dc->EndDrawing(); | |
290 | surfaceWindow.Release(); | |
291 | if (paintState == paintAbandoned) { | |
292 | // Painting area was insufficient to cover new styling or brace highlight positions | |
293 | FullPaint(); | |
294 | } | |
295 | paintState = notPainting; | |
296 | } | |
297 | ||
298 | ||
299 | void ScintillaWX::DoHScroll(int type, int pos) { | |
300 | int xPos = xOffset; | |
301 | switch (type) { | |
302 | case wxEVT_SCROLLWIN_LINEUP: | |
303 | xPos -= H_SCROLL_STEP; | |
304 | break; | |
305 | case wxEVT_SCROLLWIN_LINEDOWN: | |
306 | xPos += H_SCROLL_STEP; | |
307 | break; | |
308 | case wxEVT_SCROLLWIN_PAGEUP: | |
309 | xPos -= H_SCROLL_PAGE; | |
310 | break; | |
311 | case wxEVT_SCROLLWIN_PAGEDOWN: | |
312 | xPos += H_SCROLL_PAGE; | |
313 | break; | |
314 | case wxEVT_SCROLLWIN_TOP: | |
315 | xPos = 0; | |
316 | break; | |
317 | case wxEVT_SCROLLWIN_BOTTOM: | |
318 | xPos = H_SCROLL_MAX; | |
319 | break; | |
320 | case wxEVT_SCROLLWIN_THUMBTRACK: | |
321 | xPos = pos; | |
322 | break; | |
323 | } | |
324 | HorizontalScrollTo(xPos); | |
325 | } | |
326 | ||
327 | void ScintillaWX::DoVScroll(int type, int pos) { | |
328 | int topLineNew = topLine; | |
329 | switch (type) { | |
330 | case wxEVT_SCROLLWIN_LINEUP: | |
331 | topLineNew -= 1; | |
332 | break; | |
333 | case wxEVT_SCROLLWIN_LINEDOWN: | |
334 | topLineNew += 1; | |
335 | break; | |
336 | case wxEVT_SCROLLWIN_PAGEUP: | |
337 | topLineNew -= LinesToScroll(); | |
338 | break; | |
339 | case wxEVT_SCROLLWIN_PAGEDOWN: | |
340 | topLineNew += LinesToScroll(); | |
341 | break; | |
342 | case wxEVT_SCROLLWIN_TOP: | |
343 | topLineNew = 0; | |
344 | break; | |
345 | case wxEVT_SCROLLWIN_BOTTOM: | |
346 | topLineNew = MaxScrollPos(); | |
347 | break; | |
348 | case wxEVT_SCROLLWIN_THUMBTRACK: | |
349 | topLineNew = pos; | |
350 | break; | |
351 | } | |
352 | ScrollTo(topLineNew); | |
353 | } | |
354 | ||
355 | void ScintillaWX::DoSize(int width, int height) { | |
356 | PRectangle rcClient(0,0,width,height); | |
357 | SetScrollBarsTo(rcClient); | |
358 | DropGraphics(); | |
359 | } | |
360 | ||
361 | void ScintillaWX::DoLoseFocus(){ | |
362 | DropCaret(); | |
363 | } | |
364 | ||
365 | void ScintillaWX::DoGainFocus(){ | |
366 | ShowCaretAtCurrentPosition(); | |
367 | } | |
368 | ||
369 | void ScintillaWX::DoSysColourChange() { | |
370 | InvalidateStyleData(); | |
371 | } | |
372 | ||
373 | void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) { | |
374 | ButtonDown(pt, curTime, shift, ctrl, alt); | |
375 | } | |
376 | ||
377 | void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) { | |
378 | ButtonUp(pt, curTime, ctrl); | |
379 | } | |
380 | ||
381 | void ScintillaWX::DoButtonMove(Point pt) { | |
382 | ButtonMove(pt); | |
383 | } | |
384 | ||
385 | ||
386 | void ScintillaWX::DoAddChar(char ch) { | |
387 | AddChar(ch); | |
388 | } | |
389 | ||
390 | int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt) { | |
391 | return KeyDown(key, shift, ctrl, alt); | |
392 | } | |
393 | ||
394 | ||
395 | void ScintillaWX::DoCommand(int ID) { | |
396 | Command(ID); | |
397 | } | |
398 | ||
399 | ||
400 | void ScintillaWX::DoContextMenu(Point pt) { | |
401 | ContextMenu(pt); | |
402 | } | |
403 | ||
404 | ||
405 | //---------------------------------------------------------------------- | |
406 | ||
407 | bool ScintillaWX::DoDropText(long x, long y, const wxString& data) { | |
408 | SetDragPosition(invalidPosition); | |
409 | int movePos = PositionFromLocation(Point(x,y)); | |
410 | DropAt(movePos, data, dragResult == wxDragMove, FALSE); // TODO: rectangular? | |
411 | return TRUE; | |
412 | } | |
413 | ||
414 | ||
415 | wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) { | |
416 | return def; | |
417 | } | |
418 | ||
419 | ||
420 | wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) { | |
421 | SetDragPosition(PositionFromLocation(Point(x, y))); | |
422 | dragResult = def; | |
423 | return def; | |
424 | } | |
425 | ||
426 | ||
427 | void ScintillaWX::DoDragLeave() { | |
428 | SetDragPosition(invalidPosition); | |
429 | } | |
430 | ||
431 | //---------------------------------------------------------------------- | |
432 | ||
433 | // Redraw all of text area. This paint will not be abandoned. | |
434 | void ScintillaWX::FullPaint() { | |
435 | paintState = painting; | |
436 | rcPaint = GetTextRectangle(); | |
437 | wxClientDC dc(wMain.GetID()); | |
438 | Surface surfaceWindow; | |
439 | surfaceWindow.Init(&dc); | |
440 | Paint(&surfaceWindow, rcPaint); | |
441 | surfaceWindow.Release(); | |
442 | paintState = notPainting; | |
443 | } | |
444 | ||
445 | ||
446 | void ScintillaWX::DoScrollToLine(int line) { | |
447 | ScrollTo(line); | |
448 | } | |
449 | ||
450 | ||
451 | void ScintillaWX::DoScrollToColumn(int column) { | |
452 | HorizontalScrollTo(column * vs.spaceWidth); | |
453 | } | |
454 | ||
455 | ||
456 | ||
457 | //---------------------------------------------------------------------- | |
458 | //---------------------------------------------------------------------- |