]>
Commit | Line | Data |
---|---|---|
1 | //////////////////////////////////////////////////////////////////////////// | |
2 | // Name: ScintillaWX.cxx | |
3 | // Purpose: A wxWidgets 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 | ||
18 | #include "ScintillaWX.h" | |
19 | #include "ExternalLexer.h" | |
20 | #include "wx/stc/stc.h" | |
21 | #include "PlatWX.h" | |
22 | #include <wx/textbuf.h> | |
23 | ||
24 | //---------------------------------------------------------------------- | |
25 | // Helper classes | |
26 | ||
27 | class wxSTCTimer : public wxTimer { | |
28 | public: | |
29 | wxSTCTimer(ScintillaWX* swx) { | |
30 | this->swx = swx; | |
31 | } | |
32 | ||
33 | void Notify() { | |
34 | swx->DoTick(); | |
35 | } | |
36 | ||
37 | private: | |
38 | ScintillaWX* swx; | |
39 | }; | |
40 | ||
41 | ||
42 | #if wxUSE_DRAG_AND_DROP | |
43 | bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) { | |
44 | return swx->DoDropText(x, y, data); | |
45 | } | |
46 | ||
47 | wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) { | |
48 | return swx->DoDragEnter(x, y, def); | |
49 | } | |
50 | ||
51 | wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) { | |
52 | return swx->DoDragOver(x, y, def); | |
53 | } | |
54 | ||
55 | void wxSTCDropTarget::OnLeave() { | |
56 | swx->DoDragLeave(); | |
57 | } | |
58 | #endif | |
59 | ||
60 | ||
61 | #if wxUSE_POPUPWIN && wxSTC_USE_POPUP | |
62 | #include <wx/popupwin.h> | |
63 | #define wxSTCCallTipBase wxPopupWindow | |
64 | #define param2 wxBORDER_NONE // popup's 2nd param is flags | |
65 | #else | |
66 | #define wxSTCCallTipBase wxWindow | |
67 | #define param2 -1 // wxWindow's 2nd param is ID | |
68 | #endif | |
69 | ||
70 | #include <wx/dcbuffer.h> | |
71 | ||
72 | class wxSTCCallTip : public wxSTCCallTipBase { | |
73 | public: | |
74 | wxSTCCallTip(wxWindow* parent, CallTip* ct, ScintillaWX* swx) | |
75 | : wxSTCCallTipBase(parent, param2), | |
76 | m_ct(ct), m_swx(swx), m_cx(wxDefaultCoord), m_cy(wxDefaultCoord) | |
77 | { | |
78 | } | |
79 | ||
80 | ~wxSTCCallTip() { | |
81 | #if wxUSE_POPUPWIN && wxSTC_USE_POPUP && defined(__WXGTK__) | |
82 | wxRect rect = GetRect(); | |
83 | rect.x = m_cx; | |
84 | rect.y = m_cy; | |
85 | GetParent()->Refresh(false, &rect); | |
86 | #endif | |
87 | } | |
88 | ||
89 | bool AcceptsFocus() const { return false; } | |
90 | ||
91 | void OnPaint(wxPaintEvent& WXUNUSED(evt)) { | |
92 | wxBufferedPaintDC dc(this); | |
93 | Surface* surfaceWindow = Surface::Allocate(); | |
94 | surfaceWindow->Init(&dc, m_ct->wDraw.GetID()); | |
95 | m_ct->PaintCT(surfaceWindow); | |
96 | surfaceWindow->Release(); | |
97 | delete surfaceWindow; | |
98 | } | |
99 | ||
100 | void OnFocus(wxFocusEvent& event) { | |
101 | GetParent()->SetFocus(); | |
102 | event.Skip(); | |
103 | } | |
104 | ||
105 | void OnLeftDown(wxMouseEvent& event) { | |
106 | wxPoint pt = event.GetPosition(); | |
107 | Point p(pt.x, pt.y); | |
108 | m_ct->MouseClick(p); | |
109 | m_swx->CallTipClick(); | |
110 | } | |
111 | ||
112 | #if wxUSE_POPUPWIN && wxSTC_USE_POPUP | |
113 | virtual void DoSetSize(int x, int y, | |
114 | int width, int height, | |
115 | int sizeFlags = wxSIZE_AUTO) { | |
116 | if (x != wxDefaultCoord) { | |
117 | m_cx = x; | |
118 | GetParent()->ClientToScreen(&x, NULL); | |
119 | } | |
120 | if (y != wxDefaultCoord) { | |
121 | m_cy = y; | |
122 | GetParent()->ClientToScreen(NULL, &y); | |
123 | } | |
124 | wxSTCCallTipBase::DoSetSize(x, y, width, height, sizeFlags); | |
125 | } | |
126 | #endif | |
127 | ||
128 | wxPoint GetMyPosition() { | |
129 | return wxPoint(m_cx, m_cy); | |
130 | } | |
131 | ||
132 | private: | |
133 | CallTip* m_ct; | |
134 | ScintillaWX* m_swx; | |
135 | int m_cx, m_cy; | |
136 | DECLARE_EVENT_TABLE() | |
137 | }; | |
138 | ||
139 | BEGIN_EVENT_TABLE(wxSTCCallTip, wxSTCCallTipBase) | |
140 | EVT_PAINT(wxSTCCallTip::OnPaint) | |
141 | EVT_SET_FOCUS(wxSTCCallTip::OnFocus) | |
142 | EVT_LEFT_DOWN(wxSTCCallTip::OnLeftDown) | |
143 | END_EVENT_TABLE() | |
144 | ||
145 | ||
146 | //---------------------------------------------------------------------- | |
147 | ||
148 | static wxTextFileType wxConvertEOLMode(int scintillaMode) | |
149 | { | |
150 | wxTextFileType type; | |
151 | ||
152 | switch (scintillaMode) { | |
153 | case wxSTC_EOL_CRLF: | |
154 | type = wxTextFileType_Dos; | |
155 | break; | |
156 | ||
157 | case wxSTC_EOL_CR: | |
158 | type = wxTextFileType_Mac; | |
159 | break; | |
160 | ||
161 | case wxSTC_EOL_LF: | |
162 | type = wxTextFileType_Unix; | |
163 | break; | |
164 | ||
165 | default: | |
166 | type = wxTextBuffer::typeDefault; | |
167 | break; | |
168 | } | |
169 | return type; | |
170 | } | |
171 | ||
172 | ||
173 | //---------------------------------------------------------------------- | |
174 | // Constructor/Destructor | |
175 | ||
176 | ||
177 | ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) { | |
178 | capturedMouse = false; | |
179 | focusEvent = false; | |
180 | wMain = win; | |
181 | stc = win; | |
182 | wheelRotation = 0; | |
183 | Initialise(); | |
184 | } | |
185 | ||
186 | ||
187 | ScintillaWX::~ScintillaWX() { | |
188 | Finalise(); | |
189 | } | |
190 | ||
191 | //---------------------------------------------------------------------- | |
192 | // base class virtuals | |
193 | ||
194 | ||
195 | void ScintillaWX::Initialise() { | |
196 | //ScintillaBase::Initialise(); | |
197 | #if wxUSE_DRAG_AND_DROP | |
198 | dropTarget = new wxSTCDropTarget; | |
199 | dropTarget->SetScintilla(this); | |
200 | stc->SetDropTarget(dropTarget); | |
201 | #endif | |
202 | #ifdef __WXMAC__ | |
203 | vs.extraFontFlag = false; // UseAntiAliasing | |
204 | #else | |
205 | vs.extraFontFlag = true; // UseAntiAliasing | |
206 | #endif | |
207 | } | |
208 | ||
209 | ||
210 | void ScintillaWX::Finalise() { | |
211 | ScintillaBase::Finalise(); | |
212 | SetTicking(false); | |
213 | SetIdle(false); | |
214 | } | |
215 | ||
216 | ||
217 | void ScintillaWX::StartDrag() { | |
218 | #if wxUSE_DRAG_AND_DROP | |
219 | wxString dragText = stc2wx(drag.s, drag.len); | |
220 | ||
221 | // Send an event to allow the drag text to be changed | |
222 | wxStyledTextEvent evt(wxEVT_STC_START_DRAG, stc->GetId()); | |
223 | evt.SetEventObject(stc); | |
224 | evt.SetDragText(dragText); | |
225 | evt.SetDragAllowMove(true); | |
226 | evt.SetPosition(wxMin(stc->GetSelectionStart(), | |
227 | stc->GetSelectionEnd())); | |
228 | stc->GetEventHandler()->ProcessEvent(evt); | |
229 | dragText = evt.GetDragText(); | |
230 | ||
231 | if (dragText.Length()) { | |
232 | wxDropSource source(stc); | |
233 | wxTextDataObject data(dragText); | |
234 | wxDragResult result; | |
235 | ||
236 | source.SetData(data); | |
237 | dropWentOutside = true; | |
238 | result = source.DoDragDrop(evt.GetDragAllowMove()); | |
239 | if (result == wxDragMove && dropWentOutside) | |
240 | ClearSelection(); | |
241 | inDragDrop = false; | |
242 | SetDragPosition(invalidPosition); | |
243 | } | |
244 | #endif | |
245 | } | |
246 | ||
247 | ||
248 | bool ScintillaWX::SetIdle(bool on) { | |
249 | if (idler.state != on) { | |
250 | // connect or disconnect the EVT_IDLE handler | |
251 | if (on) | |
252 | stc->Connect(wxID_ANY, wxEVT_IDLE, | |
253 | (wxObjectEventFunction) (wxEventFunction) (wxIdleEventFunction) &wxStyledTextCtrl::OnIdle); | |
254 | else | |
255 | stc->Disconnect(wxID_ANY, wxEVT_IDLE, | |
256 | (wxObjectEventFunction) (wxEventFunction) (wxIdleEventFunction) &wxStyledTextCtrl::OnIdle); | |
257 | idler.state = on; | |
258 | } | |
259 | return idler.state; | |
260 | } | |
261 | ||
262 | ||
263 | void ScintillaWX::SetTicking(bool on) { | |
264 | wxSTCTimer* steTimer; | |
265 | if (timer.ticking != on) { | |
266 | timer.ticking = on; | |
267 | if (timer.ticking) { | |
268 | steTimer = new wxSTCTimer(this); | |
269 | steTimer->Start(timer.tickSize); | |
270 | timer.tickerID = steTimer; | |
271 | } else { | |
272 | steTimer = (wxSTCTimer*)timer.tickerID; | |
273 | steTimer->Stop(); | |
274 | delete steTimer; | |
275 | timer.tickerID = 0; | |
276 | } | |
277 | } | |
278 | timer.ticksToWait = caret.period; | |
279 | } | |
280 | ||
281 | ||
282 | void ScintillaWX::SetMouseCapture(bool on) { | |
283 | if (mouseDownCaptures) { | |
284 | if (on && !capturedMouse) | |
285 | stc->CaptureMouse(); | |
286 | else if (!on && capturedMouse && stc->HasCapture()) | |
287 | stc->ReleaseMouse(); | |
288 | capturedMouse = on; | |
289 | } | |
290 | } | |
291 | ||
292 | ||
293 | bool ScintillaWX::HaveMouseCapture() { | |
294 | return capturedMouse; | |
295 | } | |
296 | ||
297 | ||
298 | void ScintillaWX::ScrollText(int linesToMove) { | |
299 | int dy = vs.lineHeight * (linesToMove); | |
300 | stc->ScrollWindow(0, dy); | |
301 | stc->Update(); | |
302 | } | |
303 | ||
304 | void ScintillaWX::SetVerticalScrollPos() { | |
305 | if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar | |
306 | stc->SetScrollPos(wxVERTICAL, topLine); | |
307 | } | |
308 | else { // otherwise use the one that's been given to us | |
309 | stc->m_vScrollBar->SetThumbPosition(topLine); | |
310 | } | |
311 | } | |
312 | ||
313 | void ScintillaWX::SetHorizontalScrollPos() { | |
314 | if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar | |
315 | stc->SetScrollPos(wxHORIZONTAL, xOffset); | |
316 | } | |
317 | else { // otherwise use the one that's been given to us | |
318 | stc->m_hScrollBar->SetThumbPosition(xOffset); | |
319 | } | |
320 | } | |
321 | ||
322 | ||
323 | const int H_SCROLL_STEP = 20; | |
324 | ||
325 | bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) { | |
326 | bool modified = false; | |
327 | ||
328 | int vertEnd = nMax; | |
329 | if (!verticalScrollBarVisible) | |
330 | vertEnd = 0; | |
331 | ||
332 | // Check the vertical scrollbar | |
333 | if (stc->m_vScrollBar == NULL) { // Use built-in scrollbar | |
334 | int sbMax = stc->GetScrollRange(wxVERTICAL); | |
335 | int sbThumb = stc->GetScrollThumb(wxVERTICAL); | |
336 | int sbPos = stc->GetScrollPos(wxVERTICAL); | |
337 | if (sbMax != vertEnd || sbThumb != nPage) { | |
338 | stc->SetScrollbar(wxVERTICAL, sbPos, nPage, vertEnd+1); | |
339 | modified = true; | |
340 | } | |
341 | } | |
342 | else { // otherwise use the one that's been given to us | |
343 | int sbMax = stc->m_vScrollBar->GetRange(); | |
344 | int sbPage = stc->m_vScrollBar->GetPageSize(); | |
345 | int sbPos = stc->m_vScrollBar->GetThumbPosition(); | |
346 | if (sbMax != vertEnd || sbPage != nPage) { | |
347 | stc->m_vScrollBar->SetScrollbar(sbPos, nPage, vertEnd+1, nPage); | |
348 | modified = true; | |
349 | } | |
350 | } | |
351 | ||
352 | ||
353 | // Check the horizontal scrollbar | |
354 | PRectangle rcText = GetTextRectangle(); | |
355 | int horizEnd = scrollWidth; | |
356 | if (horizEnd < 0) | |
357 | horizEnd = 0; | |
358 | if (!horizontalScrollBarVisible || (wrapState != eWrapNone)) | |
359 | horizEnd = 0; | |
360 | int pageWidth = rcText.Width(); | |
361 | ||
362 | if (stc->m_hScrollBar == NULL) { // Use built-in scrollbar | |
363 | int sbMax = stc->GetScrollRange(wxHORIZONTAL); | |
364 | int sbThumb = stc->GetScrollThumb(wxHORIZONTAL); | |
365 | int sbPos = stc->GetScrollPos(wxHORIZONTAL); | |
366 | if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) { | |
367 | stc->SetScrollbar(wxHORIZONTAL, sbPos, pageWidth, horizEnd); | |
368 | modified = true; | |
369 | if (scrollWidth < pageWidth) { | |
370 | HorizontalScrollTo(0); | |
371 | } | |
372 | } | |
373 | } | |
374 | else { // otherwise use the one that's been given to us | |
375 | int sbMax = stc->m_hScrollBar->GetRange(); | |
376 | int sbThumb = stc->m_hScrollBar->GetPageSize(); | |
377 | int sbPos = stc->m_hScrollBar->GetThumbPosition(); | |
378 | if ((sbMax != horizEnd) || (sbThumb != pageWidth) || (sbPos != 0)) { | |
379 | stc->m_hScrollBar->SetScrollbar(sbPos, pageWidth, horizEnd, pageWidth); | |
380 | modified = true; | |
381 | if (scrollWidth < pageWidth) { | |
382 | HorizontalScrollTo(0); | |
383 | } | |
384 | } | |
385 | } | |
386 | ||
387 | return modified; | |
388 | } | |
389 | ||
390 | ||
391 | void ScintillaWX::NotifyChange() { | |
392 | stc->NotifyChange(); | |
393 | } | |
394 | ||
395 | ||
396 | void ScintillaWX::NotifyParent(SCNotification scn) { | |
397 | stc->NotifyParent(&scn); | |
398 | } | |
399 | ||
400 | ||
401 | // This method is overloaded from ScintillaBase in order to prevent the | |
402 | // AutoComplete window from being destroyed when it gets the focus. There is | |
403 | // a side effect that the AutoComp will also not be destroyed when switching | |
404 | // to another window, but I think that is okay. | |
405 | void ScintillaWX::CancelModes() { | |
406 | if (! focusEvent) | |
407 | AutoCompleteCancel(); | |
408 | ct.CallTipCancel(); | |
409 | Editor::CancelModes(); | |
410 | } | |
411 | ||
412 | ||
413 | ||
414 | void ScintillaWX::Copy() { | |
415 | if (currentPos != anchor) { | |
416 | SelectionText st; | |
417 | CopySelectionRange(&st); | |
418 | CopyToClipboard(st); | |
419 | } | |
420 | } | |
421 | ||
422 | ||
423 | void ScintillaWX::Paste() { | |
424 | pdoc->BeginUndoAction(); | |
425 | ClearSelection(); | |
426 | ||
427 | wxTextDataObject data; | |
428 | bool gotData = false; | |
429 | ||
430 | if (wxTheClipboard->Open()) { | |
431 | wxTheClipboard->UsePrimarySelection(false); | |
432 | gotData = wxTheClipboard->GetData(data); | |
433 | wxTheClipboard->Close(); | |
434 | } | |
435 | if (gotData) { | |
436 | wxString text = wxTextBuffer::Translate(data.GetText(), | |
437 | wxConvertEOLMode(pdoc->eolMode)); | |
438 | wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text); | |
439 | int len = strlen(buf); | |
440 | pdoc->InsertString(currentPos, buf, len); | |
441 | SetEmptySelection(currentPos + len); | |
442 | } | |
443 | ||
444 | pdoc->EndUndoAction(); | |
445 | NotifyChange(); | |
446 | Redraw(); | |
447 | } | |
448 | ||
449 | ||
450 | void ScintillaWX::CopyToClipboard(const SelectionText& st) { | |
451 | if (wxTheClipboard->Open()) { | |
452 | wxTheClipboard->UsePrimarySelection(false); | |
453 | wxString text = wxTextBuffer::Translate(stc2wx(st.s, st.len)); | |
454 | wxTheClipboard->SetData(new wxTextDataObject(text)); | |
455 | wxTheClipboard->Close(); | |
456 | } | |
457 | } | |
458 | ||
459 | ||
460 | bool ScintillaWX::CanPaste() { | |
461 | bool canPaste = false; | |
462 | bool didOpen; | |
463 | ||
464 | if (Editor::CanPaste()) { | |
465 | didOpen = !wxTheClipboard->IsOpened(); | |
466 | if ( didOpen ) | |
467 | wxTheClipboard->Open(); | |
468 | ||
469 | if (wxTheClipboard->IsOpened()) { | |
470 | wxTheClipboard->UsePrimarySelection(false); | |
471 | canPaste = wxTheClipboard->IsSupported(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT); | |
472 | if (didOpen) | |
473 | wxTheClipboard->Close(); | |
474 | } | |
475 | } | |
476 | return canPaste; | |
477 | } | |
478 | ||
479 | void ScintillaWX::CreateCallTipWindow(PRectangle) { | |
480 | if (! ct.wCallTip.Created() ) { | |
481 | ct.wCallTip = new wxSTCCallTip(stc, &ct, this); | |
482 | ct.wDraw = ct.wCallTip; | |
483 | } | |
484 | } | |
485 | ||
486 | ||
487 | void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) { | |
488 | if (!label[0]) | |
489 | ((wxMenu*)popup.GetID())->AppendSeparator(); | |
490 | else | |
491 | ((wxMenu*)popup.GetID())->Append(cmd, wxGetTranslation(stc2wx(label))); | |
492 | ||
493 | if (!enabled) | |
494 | ((wxMenu*)popup.GetID())->Enable(cmd, enabled); | |
495 | } | |
496 | ||
497 | ||
498 | // This is called by the Editor base class whenever something is selected | |
499 | void ScintillaWX::ClaimSelection() { | |
500 | #if 0 | |
501 | // Until wxGTK is able to support using both the primary selection and the | |
502 | // clipboard at the same time I think it causes more problems than it is | |
503 | // worth to implement this method. Selecting text should not clear the | |
504 | // clipboard. --Robin | |
505 | #ifdef __WXGTK__ | |
506 | // Put the selected text in the PRIMARY selection | |
507 | if (currentPos != anchor) { | |
508 | SelectionText st; | |
509 | CopySelectionRange(&st); | |
510 | if (wxTheClipboard->Open()) { | |
511 | wxTheClipboard->UsePrimarySelection(true); | |
512 | wxString text = stc2wx(st.s, st.len); | |
513 | wxTheClipboard->SetData(new wxTextDataObject(text)); | |
514 | wxTheClipboard->UsePrimarySelection(false); | |
515 | wxTheClipboard->Close(); | |
516 | } | |
517 | } | |
518 | #endif | |
519 | #endif | |
520 | } | |
521 | ||
522 | ||
523 | long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) { | |
524 | return 0; | |
525 | } | |
526 | ||
527 | long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) { | |
528 | switch (iMessage) { | |
529 | case SCI_CALLTIPSHOW: { | |
530 | // NOTE: This is copied here from scintilla/src/ScintillaBase.cxx | |
531 | // because of the little tweak that needs done below for wxGTK. | |
532 | // When updating new versions double check that this is still | |
533 | // needed, and that any new code there is copied here too. | |
534 | Point pt = LocationFromPosition(wParam); | |
535 | char* defn = reinterpret_cast<char *>(lParam); | |
536 | AutoCompleteCancel(); | |
537 | pt.y += vs.lineHeight; | |
538 | PRectangle rc = ct.CallTipStart(currentPos, pt, | |
539 | defn, | |
540 | vs.styles[STYLE_DEFAULT].fontName, | |
541 | vs.styles[STYLE_DEFAULT].sizeZoomed, | |
542 | CodePage(), | |
543 | vs.styles[STYLE_DEFAULT].characterSet, | |
544 | wMain); | |
545 | // If the call-tip window would be out of the client | |
546 | // space, adjust so it displays above the text. | |
547 | PRectangle rcClient = GetClientRectangle(); | |
548 | if (rc.bottom > rcClient.bottom) { | |
549 | #ifdef __WXGTK__ | |
550 | int offset = int(vs.lineHeight * 1.25) + rc.Height(); | |
551 | #else | |
552 | int offset = vs.lineHeight + rc.Height(); | |
553 | #endif | |
554 | rc.top -= offset; | |
555 | rc.bottom -= offset; | |
556 | } | |
557 | // Now display the window. | |
558 | CreateCallTipWindow(rc); | |
559 | ct.wCallTip.SetPositionRelative(rc, wMain); | |
560 | ct.wCallTip.Show(); | |
561 | break; | |
562 | } | |
563 | ||
564 | #ifdef SCI_LEXER | |
565 | case SCI_LOADLEXERLIBRARY: | |
566 | LexerManager::GetInstance()->Load((const char*)lParam); | |
567 | break; | |
568 | #endif | |
569 | default: | |
570 | return ScintillaBase::WndProc(iMessage, wParam, lParam); | |
571 | } | |
572 | return 0; | |
573 | } | |
574 | ||
575 | ||
576 | ||
577 | //---------------------------------------------------------------------- | |
578 | // Event delegates | |
579 | ||
580 | void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) { | |
581 | ||
582 | paintState = painting; | |
583 | Surface* surfaceWindow = Surface::Allocate(); | |
584 | surfaceWindow->Init(dc, wMain.GetID()); | |
585 | rcPaint = PRectangleFromwxRect(rect); | |
586 | PRectangle rcClient = GetClientRectangle(); | |
587 | paintingAllText = rcPaint.Contains(rcClient); | |
588 | ||
589 | dc->BeginDrawing(); | |
590 | ClipChildren(*dc, rcPaint); | |
591 | Paint(surfaceWindow, rcPaint); | |
592 | ||
593 | delete surfaceWindow; | |
594 | if (paintState == paintAbandoned) { | |
595 | // Painting area was insufficient to cover new styling or brace | |
596 | // highlight positions | |
597 | FullPaint(); | |
598 | } | |
599 | paintState = notPainting; | |
600 | dc->EndDrawing(); | |
601 | } | |
602 | ||
603 | ||
604 | void ScintillaWX::DoHScroll(int type, int pos) { | |
605 | int xPos = xOffset; | |
606 | PRectangle rcText = GetTextRectangle(); | |
607 | int pageWidth = rcText.Width() * 2 / 3; | |
608 | if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP) | |
609 | xPos -= H_SCROLL_STEP; | |
610 | else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN) | |
611 | xPos += H_SCROLL_STEP; | |
612 | else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP) | |
613 | xPos -= pageWidth; | |
614 | else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN) { | |
615 | xPos += pageWidth; | |
616 | if (xPos > scrollWidth - rcText.Width()) { | |
617 | xPos = scrollWidth - rcText.Width(); | |
618 | } | |
619 | } | |
620 | else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP) | |
621 | xPos = 0; | |
622 | else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM) | |
623 | xPos = scrollWidth; | |
624 | else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK) | |
625 | xPos = pos; | |
626 | ||
627 | HorizontalScrollTo(xPos); | |
628 | } | |
629 | ||
630 | void ScintillaWX::DoVScroll(int type, int pos) { | |
631 | int topLineNew = topLine; | |
632 | if (type == wxEVT_SCROLLWIN_LINEUP || type == wxEVT_SCROLL_LINEUP) | |
633 | topLineNew -= 1; | |
634 | else if (type == wxEVT_SCROLLWIN_LINEDOWN || type == wxEVT_SCROLL_LINEDOWN) | |
635 | topLineNew += 1; | |
636 | else if (type == wxEVT_SCROLLWIN_PAGEUP || type == wxEVT_SCROLL_PAGEUP) | |
637 | topLineNew -= LinesToScroll(); | |
638 | else if (type == wxEVT_SCROLLWIN_PAGEDOWN || type == wxEVT_SCROLL_PAGEDOWN) | |
639 | topLineNew += LinesToScroll(); | |
640 | else if (type == wxEVT_SCROLLWIN_TOP || type == wxEVT_SCROLL_TOP) | |
641 | topLineNew = 0; | |
642 | else if (type == wxEVT_SCROLLWIN_BOTTOM || type == wxEVT_SCROLL_BOTTOM) | |
643 | topLineNew = MaxScrollPos(); | |
644 | else if (type == wxEVT_SCROLLWIN_THUMBTRACK || type == wxEVT_SCROLL_THUMBTRACK) | |
645 | topLineNew = pos; | |
646 | ||
647 | ScrollTo(topLineNew); | |
648 | } | |
649 | ||
650 | void ScintillaWX::DoMouseWheel(int rotation, int delta, | |
651 | int linesPerAction, int ctrlDown, | |
652 | bool isPageScroll ) { | |
653 | int topLineNew = topLine; | |
654 | int lines; | |
655 | ||
656 | if (ctrlDown) { // Zoom the fonts if Ctrl key down | |
657 | if (rotation < 0) { | |
658 | KeyCommand(SCI_ZOOMIN); | |
659 | } | |
660 | else { | |
661 | KeyCommand(SCI_ZOOMOUT); | |
662 | } | |
663 | } | |
664 | else { // otherwise just scroll the window | |
665 | if ( !delta ) | |
666 | delta = 120; | |
667 | wheelRotation += rotation; | |
668 | lines = wheelRotation / delta; | |
669 | wheelRotation -= lines * delta; | |
670 | if (lines != 0) { | |
671 | if (isPageScroll) | |
672 | lines = lines * LinesOnScreen(); // lines is either +1 or -1 | |
673 | else | |
674 | lines *= linesPerAction; | |
675 | topLineNew -= lines; | |
676 | ScrollTo(topLineNew); | |
677 | } | |
678 | } | |
679 | } | |
680 | ||
681 | ||
682 | void ScintillaWX::DoSize(int WXUNUSED(width), int WXUNUSED(height)) { | |
683 | ChangeSize(); | |
684 | } | |
685 | ||
686 | void ScintillaWX::DoLoseFocus(){ | |
687 | focusEvent = true; | |
688 | SetFocusState(false); | |
689 | focusEvent = false; | |
690 | } | |
691 | ||
692 | void ScintillaWX::DoGainFocus(){ | |
693 | focusEvent = true; | |
694 | SetFocusState(true); | |
695 | focusEvent = false; | |
696 | } | |
697 | ||
698 | void ScintillaWX::DoSysColourChange() { | |
699 | InvalidateStyleData(); | |
700 | } | |
701 | ||
702 | void ScintillaWX::DoLeftButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) { | |
703 | ButtonDown(pt, curTime, shift, ctrl, alt); | |
704 | } | |
705 | ||
706 | void ScintillaWX::DoLeftButtonUp(Point pt, unsigned int curTime, bool ctrl) { | |
707 | ButtonUp(pt, curTime, ctrl); | |
708 | } | |
709 | ||
710 | void ScintillaWX::DoLeftButtonMove(Point pt) { | |
711 | ButtonMove(pt); | |
712 | } | |
713 | ||
714 | #ifdef __WXGTK__ | |
715 | void ScintillaWX::DoMiddleButtonUp(Point pt) { | |
716 | // Set the current position to the mouse click point and | |
717 | // then paste in the PRIMARY selection, if any. wxGTK only. | |
718 | int newPos = PositionFromLocation(pt); | |
719 | MovePositionTo(newPos, noSel, true); | |
720 | ||
721 | pdoc->BeginUndoAction(); | |
722 | wxTextDataObject data; | |
723 | bool gotData = false; | |
724 | if (wxTheClipboard->Open()) { | |
725 | wxTheClipboard->UsePrimarySelection(true); | |
726 | gotData = wxTheClipboard->GetData(data); | |
727 | wxTheClipboard->UsePrimarySelection(false); | |
728 | wxTheClipboard->Close(); | |
729 | } | |
730 | if (gotData) { | |
731 | wxString text = wxTextBuffer::Translate(data.GetText(), | |
732 | wxConvertEOLMode(pdoc->eolMode)); | |
733 | wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text); | |
734 | int len = strlen(buf); | |
735 | pdoc->InsertString(currentPos, buf, len); | |
736 | SetEmptySelection(currentPos + len); | |
737 | } | |
738 | pdoc->EndUndoAction(); | |
739 | NotifyChange(); | |
740 | Redraw(); | |
741 | ||
742 | ShowCaretAtCurrentPosition(); | |
743 | EnsureCaretVisible(); | |
744 | } | |
745 | #else | |
746 | void ScintillaWX::DoMiddleButtonUp(Point WXUNUSED(pt)) { | |
747 | } | |
748 | #endif | |
749 | ||
750 | ||
751 | void ScintillaWX::DoAddChar(int key) { | |
752 | #if wxUSE_UNICODE | |
753 | wxChar wszChars[2]; | |
754 | wszChars[0] = (wxChar)key; | |
755 | wszChars[1] = 0; | |
756 | wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(wszChars); | |
757 | AddCharUTF((char*)buf.data(), strlen(buf)); | |
758 | #else | |
759 | AddChar((char)key); | |
760 | #endif | |
761 | } | |
762 | ||
763 | ||
764 | int ScintillaWX::DoKeyDown(const wxKeyEvent& evt, bool* consumed) | |
765 | { | |
766 | int key = evt.GetKeyCode(); | |
767 | bool shift = evt.ShiftDown(), | |
768 | ctrl = evt.ControlDown(), | |
769 | alt = evt.AltDown(); | |
770 | ||
771 | if (ctrl && key >= 1 && key <= 26) | |
772 | key += 'A' - 1; | |
773 | ||
774 | switch (key) { | |
775 | case WXK_DOWN: key = SCK_DOWN; break; | |
776 | case WXK_UP: key = SCK_UP; break; | |
777 | case WXK_LEFT: key = SCK_LEFT; break; | |
778 | case WXK_RIGHT: key = SCK_RIGHT; break; | |
779 | case WXK_HOME: key = SCK_HOME; break; | |
780 | case WXK_END: key = SCK_END; break; | |
781 | case WXK_PAGEUP: // fall through | |
782 | case WXK_PRIOR: key = SCK_PRIOR; break; | |
783 | case WXK_PAGEDOWN: // fall through | |
784 | case WXK_NEXT: key = SCK_NEXT; break; | |
785 | case WXK_DELETE: key = SCK_DELETE; break; | |
786 | case WXK_INSERT: key = SCK_INSERT; break; | |
787 | case WXK_ESCAPE: key = SCK_ESCAPE; break; | |
788 | case WXK_BACK: key = SCK_BACK; break; | |
789 | case WXK_TAB: key = SCK_TAB; break; | |
790 | case WXK_RETURN: key = SCK_RETURN; break; | |
791 | case WXK_ADD: // fall through | |
792 | case WXK_NUMPAD_ADD: key = SCK_ADD; break; | |
793 | case WXK_SUBTRACT: // fall through | |
794 | case WXK_NUMPAD_SUBTRACT: key = SCK_SUBTRACT; break; | |
795 | case WXK_DIVIDE: // fall through | |
796 | case WXK_NUMPAD_DIVIDE: key = SCK_DIVIDE; break; | |
797 | case WXK_CONTROL: key = 0; break; | |
798 | case WXK_ALT: key = 0; break; | |
799 | case WXK_SHIFT: key = 0; break; | |
800 | case WXK_MENU: key = 0; break; | |
801 | } | |
802 | ||
803 | #ifdef __WXMAC__ | |
804 | if ( evt.MetaDown() ) { | |
805 | // check for a few common Mac Meta-key combos and remap them to Ctrl | |
806 | // for Scintilla | |
807 | switch ( key ) { | |
808 | case 'Z': // Undo | |
809 | case 'X': // Cut | |
810 | case 'C': // Copy | |
811 | case 'V': // Paste | |
812 | case 'A': // Select All | |
813 | ctrl = true; | |
814 | break; | |
815 | } | |
816 | } | |
817 | #endif | |
818 | ||
819 | int rv = KeyDown(key, shift, ctrl, alt, consumed); | |
820 | ||
821 | if (key) | |
822 | return rv; | |
823 | else | |
824 | return 1; | |
825 | } | |
826 | ||
827 | ||
828 | void ScintillaWX::DoCommand(int ID) { | |
829 | Command(ID); | |
830 | } | |
831 | ||
832 | ||
833 | void ScintillaWX::DoContextMenu(Point pt) { | |
834 | if (displayPopupMenu) | |
835 | ContextMenu(pt); | |
836 | } | |
837 | ||
838 | void ScintillaWX::DoOnListBox() { | |
839 | AutoCompleteCompleted(); | |
840 | } | |
841 | ||
842 | ||
843 | void ScintillaWX::DoOnIdle(wxIdleEvent& evt) { | |
844 | ||
845 | if ( Idle() ) | |
846 | evt.RequestMore(); | |
847 | else | |
848 | SetIdle(false); | |
849 | } | |
850 | ||
851 | //---------------------------------------------------------------------- | |
852 | ||
853 | #if wxUSE_DRAG_AND_DROP | |
854 | bool ScintillaWX::DoDropText(long x, long y, const wxString& data) { | |
855 | SetDragPosition(invalidPosition); | |
856 | ||
857 | wxString text = wxTextBuffer::Translate(data, | |
858 | wxConvertEOLMode(pdoc->eolMode)); | |
859 | ||
860 | // Send an event to allow the drag details to be changed | |
861 | wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId()); | |
862 | evt.SetEventObject(stc); | |
863 | evt.SetDragResult(dragResult); | |
864 | evt.SetX(x); | |
865 | evt.SetY(y); | |
866 | evt.SetPosition(PositionFromLocation(Point(x,y))); | |
867 | evt.SetDragText(text); | |
868 | stc->GetEventHandler()->ProcessEvent(evt); | |
869 | ||
870 | dragResult = evt.GetDragResult(); | |
871 | if (dragResult == wxDragMove || dragResult == wxDragCopy) { | |
872 | DropAt(evt.GetPosition(), | |
873 | wx2stc(evt.GetDragText()), | |
874 | dragResult == wxDragMove, | |
875 | false); // TODO: rectangular? | |
876 | return true; | |
877 | } | |
878 | return false; | |
879 | } | |
880 | ||
881 | ||
882 | wxDragResult ScintillaWX::DoDragEnter(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) { | |
883 | dragResult = def; | |
884 | return dragResult; | |
885 | } | |
886 | ||
887 | ||
888 | wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) { | |
889 | SetDragPosition(PositionFromLocation(Point(x, y))); | |
890 | ||
891 | // Send an event to allow the drag result to be changed | |
892 | wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId()); | |
893 | evt.SetEventObject(stc); | |
894 | evt.SetDragResult(def); | |
895 | evt.SetX(x); | |
896 | evt.SetY(y); | |
897 | evt.SetPosition(PositionFromLocation(Point(x,y))); | |
898 | stc->GetEventHandler()->ProcessEvent(evt); | |
899 | ||
900 | dragResult = evt.GetDragResult(); | |
901 | return dragResult; | |
902 | } | |
903 | ||
904 | ||
905 | void ScintillaWX::DoDragLeave() { | |
906 | SetDragPosition(invalidPosition); | |
907 | } | |
908 | #endif | |
909 | //---------------------------------------------------------------------- | |
910 | ||
911 | // Force the whole window to be repainted | |
912 | void ScintillaWX::FullPaint() { | |
913 | stc->Refresh(false); | |
914 | stc->Update(); | |
915 | } | |
916 | ||
917 | ||
918 | void ScintillaWX::DoScrollToLine(int line) { | |
919 | ScrollTo(line); | |
920 | } | |
921 | ||
922 | ||
923 | void ScintillaWX::DoScrollToColumn(int column) { | |
924 | HorizontalScrollTo(column * vs.spaceWidth); | |
925 | } | |
926 | ||
927 | #ifdef __WXGTK__ | |
928 | void ScintillaWX::ClipChildren(wxDC& dc, PRectangle rect) { | |
929 | wxRegion rgn(wxRectFromPRectangle(rect)); | |
930 | if (ac.Active()) { | |
931 | wxRect childRect = ((wxWindow*)ac.lb->GetID())->GetRect(); | |
932 | rgn.Subtract(childRect); | |
933 | } | |
934 | if (ct.inCallTipMode) { | |
935 | wxSTCCallTip* tip = (wxSTCCallTip*)ct.wCallTip.GetID(); | |
936 | wxRect childRect = tip->GetRect(); | |
937 | #if wxUSE_POPUPWIN && wxSTC_USE_POPUP | |
938 | childRect.SetPosition(tip->GetMyPosition()); | |
939 | #endif | |
940 | rgn.Subtract(childRect); | |
941 | } | |
942 | ||
943 | dc.SetClippingRegion(rgn); | |
944 | } | |
945 | #else | |
946 | void ScintillaWX::ClipChildren(wxDC& WXUNUSED(dc), PRectangle WXUNUSED(rect)) { | |
947 | } | |
948 | #endif | |
949 | ||
950 | ||
951 | void ScintillaWX::SetUseAntiAliasing(bool useAA) { | |
952 | vs.extraFontFlag = useAA; | |
953 | InvalidateStyleRedraw(); | |
954 | } | |
955 | ||
956 | bool ScintillaWX::GetUseAntiAliasing() { | |
957 | return vs.extraFontFlag; | |
958 | } | |
959 | ||
960 | //---------------------------------------------------------------------- | |
961 | //---------------------------------------------------------------------- |