From e5ce18f0cbfe60ba8ff6419ca092da418a8647ca Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 9 Jun 2020 11:13:55 +0200 Subject: [PATCH] Make multiline controls 5 lines high by default under Mac This is consistent with the current behaviour of GetSizeFromTextSize() and the behaviour of GetBestSize() before the changes in this branch. This is still not consistent with the behaviour of the other ports, but this will be addressed later, by replacing the currently hardcoded 5. Note that calling usedRectForTextContainer: here was apparently wrong in any case because we were not sure to have already performed a layout and we should have had a call to ensureLayoutForTextContainer: before it to make it actually work. However, this made it work "too well" because it then correctly returned potentially very big sizes for the text controls containing a lot of text, which is not what we need here, as explained in the comment added by this commit. --- src/osx/cocoa/textctrl.mm | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index da1a9e0cda..138b49e40f 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -1222,13 +1222,25 @@ void wxNSTextViewControl::EnableAutomaticDashSubstitution(bool enable) wxSize wxNSTextViewControl::GetBestSize() const { + wxSize size; if ( NSLayoutManager* const layoutManager = [m_textView layoutManager] ) { - NSRect rect = [layoutManager usedRectForTextContainer: [m_textView textContainer]]; - return wxSize((int)(rect.size.width + [m_textView textContainerInset].width), - (int)(rect.size.height + [m_textView textContainerInset].height)); + // We could have used usedRectForTextContainer: here to compute the + // total rectangle needed for the current text, but this is actually + // not too helpful for determining the best size of the control, as we + // don't always want to fit it to its text: e.g. if it contains 1000 + // lines, we definitely don't want to make it higher than display. + // + // So instead we just get the height of a single line (which works for + // any non-empty control) and then multiply it by the number of lines + // we want to have by default, which is currently just hardcoded as 5 + // for compatibility with the behaviour of the previous versions. + NSRect rect = [layoutManager lineFragmentRectForGlyphAtIndex: 0 + effectiveRange: nil]; + size.y = (int)(5*rect.size.height + [m_textView textContainerInset].height); } - return wxSize(0,0); + + return size; } void wxNSTextViewControl::SetJustification()