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.
This commit is contained in:
Artur Wieczorek
2017-08-05 20:44:15 +02:00
parent 7626292fa4
commit ec55f11ff7
4 changed files with 33 additions and 1 deletions

View File

@@ -226,6 +226,7 @@ wxOSX:
- Implement wxTextCtrl::PositionToXY() and XYToPosition(). - Implement wxTextCtrl::PositionToXY() and XYToPosition().
- Implement wxTextCtrl::ShowPosition(). - Implement wxTextCtrl::ShowPosition().
- Add support for wxTE_NO_VSCROLL style to wxTextCtrl. - Add support for wxTE_NO_VSCROLL style to wxTextCtrl.
- Add support for wxTE_CHARWRAP style to wxTextCtrl.
Unix: Unix:

View File

@@ -124,9 +124,14 @@ public:
virtual wxSize GetBestSize() const wxOVERRIDE; virtual wxSize GetBestSize() const wxOVERRIDE;
virtual void controlTextDidChange() wxOVERRIDE;
protected: protected:
void DoUpdateTextStyle();
NSScrollView* m_scrollView; NSScrollView* m_scrollView;
NSTextView* m_textView; NSTextView* m_textView;
bool m_useCharWrapping;
}; };
class wxNSComboBoxControl : public wxNSTextFieldControl, public wxComboWidgetImpl class wxNSComboBoxControl : public wxNSTextFieldControl, public wxComboWidgetImpl

View File

@@ -990,7 +990,7 @@ public:
scrollbar instead. scrollbar instead.
@style{wxTE_CHARWRAP} @style{wxTE_CHARWRAP}
For multiline controls only: wrap the lines too long to be shown 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} @style{wxTE_WORDWRAP}
For multiline controls only: wrap the lines too long to be shown For multiline controls only: wrap the lines too long to be shown
entirely at word boundaries (wxUniv, wxMSW, wxGTK, wxOSX). entirely at word boundaries (wxUniv, wxMSW, wxGTK, wxOSX).

View File

@@ -713,6 +713,7 @@ wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w, long s
m_scrollView = sv; m_scrollView = sv;
const bool hasHScroll = (style & wxHSCROLL) != 0; const bool hasHScroll = (style & wxHSCROLL) != 0;
m_useCharWrapping = (style & wxTE_CHARWRAP) != 0;
[m_scrollView setHasVerticalScroller:YES]; [m_scrollView setHasVerticalScroller:YES];
[m_scrollView setHasHorizontalScroller:hasHScroll]; [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. // Make sure that any URLs in the new text are highlighted.
[m_textView checkTextInDocument:nil]; [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_lastKeyDownEvent = nil;
[m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
m_lastKeyDownEvent = formerEvent; 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) ) void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )