From ec55f11ff7beb41ca1a988d287662d0172234c19 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sat, 5 Aug 2017 20:44:15 +0200 Subject: [PATCH] Add support for wxTE_CHARWRAP to wxTextCtrl Apparently various text wrapping modes are not supported natively by NSTextView (apart from word wrapping which is used by default) and non-default wrapping has to be implemented in the custom code. To wrap lines at any character, NSLineBreakByCharWrapping style should be applied at any text change to the entire text stored in NSTextStorage associated with NSTextView. This is done in DoUpdateTextStyle() method which is called from controlTextDidChange() when text is modified by the user, or SetStringValue() and WriteText() when text is set programmatically. --- docs/changes.txt | 1 + include/wx/osx/cocoa/private/textimpl.h | 5 +++++ interface/wx/textctrl.h | 2 +- src/osx/cocoa/textctrl.mm | 26 +++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/changes.txt b/docs/changes.txt index 094162c394..6deef2d39b 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -226,6 +226,7 @@ wxOSX: - Implement wxTextCtrl::PositionToXY() and XYToPosition(). - Implement wxTextCtrl::ShowPosition(). - Add support for wxTE_NO_VSCROLL style to wxTextCtrl. +- Add support for wxTE_CHARWRAP style to wxTextCtrl. Unix: diff --git a/include/wx/osx/cocoa/private/textimpl.h b/include/wx/osx/cocoa/private/textimpl.h index e0a7cdaa9a..579dbf8ceb 100644 --- a/include/wx/osx/cocoa/private/textimpl.h +++ b/include/wx/osx/cocoa/private/textimpl.h @@ -124,9 +124,14 @@ public: virtual wxSize GetBestSize() const wxOVERRIDE; + virtual void controlTextDidChange() wxOVERRIDE; + protected: + void DoUpdateTextStyle(); + NSScrollView* m_scrollView; NSTextView* m_textView; + bool m_useCharWrapping; }; class wxNSComboBoxControl : public wxNSTextFieldControl, public wxComboWidgetImpl diff --git a/interface/wx/textctrl.h b/interface/wx/textctrl.h index d7c0cc54cc..96da213db2 100644 --- a/interface/wx/textctrl.h +++ b/interface/wx/textctrl.h @@ -990,7 +990,7 @@ public: scrollbar instead. @style{wxTE_CHARWRAP} For multiline controls only: wrap the lines too long to be shown - entirely at any position (wxUniv and wxGTK). + entirely at any position (wxUniv, wxGTK, wxOSX). @style{wxTE_WORDWRAP} For multiline controls only: wrap the lines too long to be shown entirely at word boundaries (wxUniv, wxMSW, wxGTK, wxOSX). diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index aed884cdb8..28316d14f6 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -713,6 +713,7 @@ wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w, long s m_scrollView = sv; const bool hasHScroll = (style & wxHSCROLL) != 0; + m_useCharWrapping = (style & wxTE_CHARWRAP) != 0; [m_scrollView setHasVerticalScroller:YES]; [m_scrollView setHasHorizontalScroller:hasHScroll]; @@ -809,6 +810,8 @@ void wxNSTextViewControl::SetStringValue( const wxString &str) // Make sure that any URLs in the new text are highlighted. [m_textView checkTextInDocument:nil]; } + // Some text styles have to be updated manually. + DoUpdateTextStyle(); } } @@ -952,6 +955,29 @@ void wxNSTextViewControl::WriteText(const wxString& str) m_lastKeyDownEvent = nil; [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; m_lastKeyDownEvent = formerEvent; + // Some text styles have to be updated manually. + DoUpdateTextStyle(); +} + +void wxNSTextViewControl::controlTextDidChange() +{ + wxNSTextBase::controlTextDidChange(); + // Some text styles have to be updated manually. + DoUpdateTextStyle(); +} + +void wxNSTextViewControl::DoUpdateTextStyle() +{ + if ( m_useCharWrapping ) + { + // Set line wrapping at any position + // by applying a style to the entire text. + NSTextStorage* storage = [m_textView textStorage]; + NSMutableParagraphStyle* style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; + [style setLineBreakMode:NSLineBreakByCharWrapping]; + [storage addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [storage length])]; + [style release]; + } } void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )