From e9f1000463520959c7a17f4ff0e409a2799d0c5c Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Wed, 21 Jan 2009 12:06:21 +0000 Subject: [PATCH] wxRTC now properly honours margin size Added wxRE_CENTRE_CARET style to centre the caret line vertically git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58257 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/richtext/richtextctrl.h | 2 ++ interface/wx/richtext/richtextctrl.h | 10 ++++++ src/richtext/richtextctrl.cpp | 54 ++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/include/wx/richtext/richtextctrl.h b/include/wx/richtext/richtextctrl.h index b098cc8c8b..3785428c7d 100644 --- a/include/wx/richtext/richtextctrl.h +++ b/include/wx/richtext/richtextctrl.h @@ -38,6 +38,8 @@ class WXDLLIMPEXP_FWD_RICHTEXT wxRichTextStyleDefinition; #define wxRE_READONLY 0x0010 #define wxRE_MULTILINE 0x0020 +#define wxRE_CENTRE_CARET 0x8000 +#define wxRE_CENTER_CARET wxRE_CENTRE_CARET /* Flags */ diff --git a/interface/wx/richtext/richtextctrl.h b/interface/wx/richtext/richtextctrl.h index 44fd95b134..d7f4147ef3 100644 --- a/interface/wx/richtext/richtextctrl.h +++ b/interface/wx/richtext/richtextctrl.h @@ -159,6 +159,16 @@ public: For more information, see the @ref overview_richtextctrl. + @beginStyleTable + @style{wxRE_CENTRE_CARET} + The control will try to keep the caret line centred vertically while editing. + wxRE_CENTER_CARET is a synonym for this style. + @style{wxRE_MULTILINE} + The control will be multiline (mandatory). + @style{wxRE_READONLY} + The control will not be editable. + @endStyleTable + @library{wxrichtext} @category{richtext} @appearance{richtextctrl.png} diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp index 652150c8a7..c75c4bfd40 100644 --- a/src/richtext/richtextctrl.cpp +++ b/src/richtext/richtextctrl.cpp @@ -206,6 +206,9 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& va SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); } + // No physical scrolling, so we can preserve margins + EnableScrolling(false, false); + if (style & wxTE_READONLY) SetEditable(false); @@ -365,7 +368,18 @@ void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) SetupScrollbars(); } + wxRect clipRect(availableSpace); + clipRect.x += GetBuffer().GetLeftMargin(); + clipRect.y += GetBuffer().GetTopMargin(); + clipRect.width -= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin()); + clipRect.height -= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin()); + clipRect.SetPosition(GetLogicalPoint(clipRect.GetPosition())); + dc.SetClippingRegion(clipRect); + GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); + + dc.DestroyClippingRegion(); + #if wxRICHTEXT_USE_OWN_CARET if (GetCaret()->IsVisible()) { @@ -1231,6 +1245,27 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) bool scrolled = false; wxSize clientSize = GetClientSize(); + clientSize.y -= GetBuffer().GetBottomMargin(); + + if (GetWindowStyle() & wxRE_CENTRE_CARET) + { + int y = rect.y - GetClientSize().y/2; + int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); + if (y >= 0 && (y + clientSize.y) < GetBuffer().GetCachedSize().y) + { + if (startYUnits != yUnits) + { + SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); + scrolled = true; + } +#if !wxRICHTEXT_USE_OWN_CARET + if (scrolled) +#endif + PositionCaret(); + + return scrolled; + } + } // Going down if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN || @@ -1255,11 +1290,11 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) scrolled = true; } } - else if (rect.y < startY) + else if (rect.y < (startY + GetBuffer().GetTopMargin())) { // Make it scroll so this item is at the top // of the window - int y = rect.y ; + int y = rect.y - GetBuffer().GetTopMargin(); int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); if (startYUnits != yUnits) @@ -1275,11 +1310,11 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME || keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP ) { - if (rect.y < startY) + if (rect.y < (startY + GetBuffer().GetBottomMargin())) { // Make it scroll so this item is at the top // of the window - int y = rect.y ; + int y = rect.y - GetBuffer().GetTopMargin(); int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); if (startYUnits != yUnits) @@ -1333,8 +1368,9 @@ bool wxRichTextCtrl::IsPositionVisible(long pos) const wxRect rect = line->GetRect(); wxSize clientSize = GetClientSize(); + clientSize.y -= GetBuffer().GetBottomMargin(); - return (rect.GetBottom() > startY) && (rect.GetTop() < (startY + clientSize.y)); + return (rect.GetBottom() > (startY + GetBuffer().GetTopMargin())) && (rect.GetTop() < (startY + clientSize.y)); } void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart) @@ -1982,7 +2018,7 @@ void wxRichTextCtrl::SetupScrollbars(bool atTop) int pixelsPerUnit = 5; wxSize clientSize = GetClientSize(); - int maxHeight = GetBuffer().GetCachedSize().y; + int maxHeight = GetBuffer().GetCachedSize().y + GetBuffer().GetTopMargin(); // Round up so we have at least maxHeight pixels int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5); @@ -2883,6 +2919,12 @@ void wxRichTextCtrl::PositionCaret() GetCaret()->Hide(); if (GetCaret()->GetSize() != newSz) GetCaret()->SetSize(newSz); + + int halfSize = newSz.y/2; + // If the caret is beyond the margin, hide it by moving it out of the way + if (((pt.y + halfSize) < GetBuffer().GetTopMargin()) || ((pt.y + halfSize) > (GetClientSize().y - GetBuffer().GetBottomMargin()))) + pt.y = -200; + GetCaret()->Move(pt); GetCaret()->Show(); } -- 2.45.2