X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4d551ad5e5d4f9643d69e70638788fb9a78f209a..a4f4d548ce0d3533fe9e84d812643cc7c21dd580:/src/richtext/richtextctrl.cpp diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp index 8e8ec62272..297a3dea4f 100644 --- a/src/richtext/richtextctrl.cpp +++ b/src/richtext/richtextctrl.cpp @@ -57,6 +57,7 @@ BEGIN_EVENT_TABLE( wxRichTextCtrl, wxScrolledWindow ) EVT_PAINT(wxRichTextCtrl::OnPaint) EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground) EVT_IDLE(wxRichTextCtrl::OnIdle) + EVT_SCROLLWIN(wxRichTextCtrl::OnScroll) EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick) EVT_MOTION(wxRichTextCtrl::OnMoveMouse) EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp) @@ -151,11 +152,6 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos // Create a buffer RecreateBuffer(size); - wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16); - SetCaret(caret); - caret->Show(); - PositionCaret(); - SetCursor(wxCursor(wxCURSOR_IBEAM)); return true; @@ -222,31 +218,41 @@ void wxRichTextCtrl::Clear() /// Painting void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) { - wxBufferedPaintDC dc(this, m_bufferBitmap); - - PrepareDC(dc); - - if (m_freezeCount > 0) - return; - - dc.SetFont(GetFont()); - - // Paint the background - PaintBackground(dc); + if (GetCaret()) + GetCaret()->Hide(); - wxRegion dirtyRegion = GetUpdateRegion(); - - wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); - wxRect availableSpace(GetClientSize()); - if (GetBuffer().GetDirty()) { - GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); - GetBuffer().SetDirty(false); - SetupScrollbars(); - PositionCaret(); + wxBufferedPaintDC dc(this, m_bufferBitmap); + //wxLogDebug(wxT("OnPaint")); + + PrepareDC(dc); + + if (m_freezeCount > 0) + return; + + dc.SetFont(GetFont()); + + // Paint the background + PaintBackground(dc); + + wxRegion dirtyRegion = GetUpdateRegion(); + + wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); + wxRect availableSpace(GetClientSize()); + if (GetBuffer().GetDirty()) + { + GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); + GetBuffer().SetDirty(false); + SetupScrollbars(); + } + + GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); } - GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); + if (GetCaret()) + GetCaret()->Show(); + + PositionCaret(); } // Empty implementation, to prevent flicker @@ -256,12 +262,19 @@ void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) { + wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16); + SetCaret(caret); + caret->Show(); + PositionCaret(); + if (!IsFrozen()) Refresh(); } void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) { + SetCaret(NULL); + if (!IsFrozen()) Refresh(); } @@ -633,8 +646,7 @@ bool wxRichTextCtrl::Navigate(int keyCode, int flags) SetDefaultStyleToCursorStyle(); } - // Only refresh if something changed - Thaw(success); + Thaw(false); return success; } @@ -712,16 +724,27 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) // Make it scroll so this item is at the bottom // of the window int y = rect.y - (clientSize.y - rect.height); - SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); + y = (int) (0.5 + y/ppuY); + + if (startY != y) + { + SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + scrolled = true; + } } else if (rect.y < startY) { // Make it scroll so this item is at the top // of the window int y = rect.y ; - SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); + y = (int) (0.5 + y/ppuY); + + if (startY != y) + { + SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + scrolled = true; + } } - scrolled = true; } // Going up else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PRIOR || keyCode == WXK_PAGEUP) @@ -731,16 +754,27 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) // Make it scroll so this item is at the top // of the window int y = rect.y ; - SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); + y = (int) (0.5 + y/ppuY); + + if (startY != y) + { + SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + scrolled = true; + } } else if ((rect.y + rect.height) > (clientSize.y + startY)) { // Make it scroll so this item is at the bottom // of the window int y = rect.y - (clientSize.y - rect.height); - SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); + y = (int) (0.5 + y/ppuY); + + if (startY != y) + { + SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + scrolled = true; + } } - scrolled = true; } PositionCaret(); @@ -904,8 +938,8 @@ bool wxRichTextCtrl::MoveRight(int noPositions, int flags) PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) - Refresh(); // TODO: optimize so that if we didn't change the selection, we don't refresh + if (extendSel) + Refresh(); return true; } else @@ -933,7 +967,7 @@ bool wxRichTextCtrl::MoveLeft(int noPositions, int flags) PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -950,6 +984,9 @@ bool wxRichTextCtrl::MoveUp(int noLines, int flags) /// Move up bool wxRichTextCtrl::MoveDown(int noLines, int flags) { + if (!GetCaret()) + return false; + long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart); wxPoint pt = GetCaret()->GetPosition(); long newLine = lineNumber + noLines; @@ -1013,14 +1050,15 @@ bool wxRichTextCtrl::MoveDown(int noLines, int flags) long newSelEnd = newPos; - if (!ExtendSelection(m_caretPosition, newSelEnd, flags)) + bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags); + if (!extendSel) SelectNone(); SetCaretPosition(newPos, caretLineStart); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1035,14 +1073,15 @@ bool wxRichTextCtrl::MoveToParagraphEnd(int flags) if (para) { long newPos = para->GetRange().GetEnd() - 1; - if (!ExtendSelection(m_caretPosition, newPos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); + if (!extendSel) SelectNone(); SetCaretPosition(newPos); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1057,14 +1096,15 @@ bool wxRichTextCtrl::MoveToParagraphStart(int flags) if (para) { long newPos = para->GetRange().GetStart() - 1; - if (!ExtendSelection(m_caretPosition, newPos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); + if (!extendSel) SelectNone(); SetCaretPosition(newPos); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1081,14 +1121,15 @@ bool wxRichTextCtrl::MoveToLineEnd(int flags) { wxRichTextRange lineRange = line->GetAbsoluteRange(); long newPos = lineRange.GetEnd(); - if (!ExtendSelection(m_caretPosition, newPos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); + if (!extendSel) SelectNone(); SetCaretPosition(newPos); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1105,7 +1146,8 @@ bool wxRichTextCtrl::MoveToLineStart(int flags) wxRichTextRange lineRange = line->GetAbsoluteRange(); long newPos = lineRange.GetStart()-1; - if (!ExtendSelection(m_caretPosition, newPos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); + if (!extendSel) SelectNone(); wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line); @@ -1114,7 +1156,7 @@ bool wxRichTextCtrl::MoveToLineStart(int flags) PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1127,14 +1169,15 @@ bool wxRichTextCtrl::MoveHome(int flags) { if (m_caretPosition != -1) { - if (!ExtendSelection(m_caretPosition, -1, flags)) + bool extendSel = ExtendSelection(m_caretPosition, -1, flags); + if (!extendSel) SelectNone(); SetCaretPosition(-1); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1149,14 +1192,15 @@ bool wxRichTextCtrl::MoveEnd(int flags) if (m_caretPosition != endPos) { - if (!ExtendSelection(m_caretPosition, endPos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, endPos, flags); + if (!extendSel) SelectNone(); SetCaretPosition(endPos); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1189,14 +1233,15 @@ bool wxRichTextCtrl::PageDown(int noPages, int flags) { wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine); - if (!ExtendSelection(m_caretPosition, pos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, pos, flags); + if (!extendSel) SelectNone(); SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart()); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1287,14 +1332,15 @@ bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags) { wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); - if (!ExtendSelection(m_caretPosition, pos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, pos, flags); + if (!extendSel) SelectNone(); SetCaretPosition(pos, para->GetRange().GetStart() != pos); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1310,14 +1356,15 @@ bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags) { wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); - if (!ExtendSelection(m_caretPosition, pos, flags)) + bool extendSel = ExtendSelection(m_caretPosition, pos, flags); + if (!extendSel) SelectNone(); SetCaretPosition(pos, para->GetRange().GetStart() != pos); PositionCaret(); SetDefaultStyleToCursorStyle(); - if (!IsFrozen()) + if (extendSel) Refresh(); return true; } @@ -1361,6 +1408,13 @@ void wxRichTextCtrl::OnIdle(wxIdleEvent& event) event.Skip(); } +/// Scrolling +void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event) +{ + // Not used + event.Skip(); +} + /// Set up scrollbars, e.g. after a resize void wxRichTextCtrl::SetupScrollbars(bool atTop) { @@ -1514,7 +1568,8 @@ void wxRichTextCtrl::SelectAll() /// Select none void wxRichTextCtrl::SelectNone() { - SetSelection(-2, -2); + if (!(GetSelectionRange() == wxRichTextRange(-2, -2))) + SetSelection(-2, -2); m_selectionAnchor = -2; } @@ -1832,8 +1887,7 @@ void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCare { m_selectionAnchor = from; m_selectionRange.SetRange(from, to); - if (!IsFrozen()) - Refresh(); + Refresh(); PositionCaret(); } @@ -2171,14 +2225,21 @@ wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const /// Position the caret void wxRichTextCtrl::PositionCaret() { + if (!GetCaret()) + return; + + //wxLogDebug(wxT("PositionCaret")); + wxRect caretRect; if (GetCaretPositionForIndex(GetCaretPosition(), caretRect)) { wxPoint originalPt = caretRect.GetPosition(); wxPoint pt = GetPhysicalPoint(originalPt); - - GetCaret()->Move(pt); - GetCaret()->SetSize(caretRect.GetSize()); + if (GetCaret()->GetPosition() != pt) + { + GetCaret()->Move(pt); + GetCaret()->SetSize(caretRect.GetSize()); + } } }