Allow using multiple lines in single line wxTextCtrl under Mac

Add new OSX specific OSXEnableNewLineReplacement(bool enable) function
to control if new lines should be replaced with spaces (which is still
done by default) in a single-line wxTextCtrl.

Closes #22245.
This commit is contained in:
Tobias Fleischer
2022-03-26 13:56:31 +01:00
committed by Vadim Zeitlin
parent c63de862c4
commit e8872b335c
6 changed files with 74 additions and 2 deletions

View File

@@ -81,6 +81,8 @@ public :
virtual bool becomeFirstResponder(WXWidget slf, void *_cmd) wxOVERRIDE;
virtual bool resignFirstResponder(WXWidget slf, void *_cmd) wxOVERRIDE;
virtual void EnableNewLineReplacement(bool enable) wxOVERRIDE;
virtual bool GetNewLineReplacement() wxOVERRIDE;
virtual void SetInternalSelection( long from , long to );
virtual void UpdateInternalSelectionFromEditor( wxNSTextFieldEditor* editor);
protected :
@@ -133,6 +135,8 @@ public:
#endif // wxUSE_SPELLCHECK
virtual void EnableAutomaticQuoteSubstitution(bool enable) wxOVERRIDE;
virtual void EnableAutomaticDashSubstitution(bool enable) wxOVERRIDE;
virtual void EnableNewLineReplacement(bool enable) wxOVERRIDE;
virtual bool GetNewLineReplacement() wxOVERRIDE;
virtual wxSize GetBestSize() const wxOVERRIDE;
virtual void SetJustification() wxOVERRIDE;

View File

@@ -750,6 +750,8 @@ public :
virtual void EnableAutomaticQuoteSubstitution(bool WXUNUSED(enable)) {}
virtual void EnableAutomaticDashSubstitution(bool WXUNUSED(enable)) {}
virtual void EnableNewLineReplacement(bool WXUNUSED(enable)) {}
virtual bool GetNewLineReplacement() { return true; }
virtual wxSize GetBestSize() const { return wxDefaultSize; }
virtual bool SetHint(const wxString& WXUNUSED(hint)) { return false; }

View File

@@ -143,6 +143,7 @@ public:
wxDEPRECATED( virtual void MacCheckSpelling(bool check) );
#endif // WXWIN_COMPATIBILITY_3_0 && wxUSE_SPELLCHECK
void OSXEnableNewLineReplacement(bool enable);
void OSXEnableAutomaticQuoteSubstitution(bool enable);
void OSXEnableAutomaticDashSubstitution(bool enable);
void OSXDisableAllSmartSubstitutions();

View File

@@ -1761,6 +1761,35 @@ public:
*/
virtual long XYToPosition(long x, long y) const;
/**
@name Mac-specific functions
*/
//@{
/**
Enable the automatic replacement of new lines characters in a
single-line text field with spaces under macOS.
This feature is enabled by default and will replace any new line (`\n`)
character entered into a single-line text field with the space
character. Usually single-line text fields are not expected to hold
multiple lines of text (that is what wxTE_MULTILINE is for, after all)
and it is impossible to have multiple lines of text in them under
non-Mac platforms. However, under macOS/Cocoa, a single-line text
control can still show multiple lines and this function allows to lift
the restriction preventing multiple lines from being entered unless
wxTE_MULTILINE is specified.
@note This function is only available for macOS/Cocoa. It also has no
effect if the wxTE_MULTILINE flag is set on a text control.
@onlyfor{wxosx}
@since 3.1.6
*/
virtual void OSXEnableNewLineReplacement(bool enable);
//@}
//@{
/**
Operator definitions for appending to a text control.

View File

@@ -118,6 +118,7 @@ NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
int maxLength;
wxTextEntry* field;
bool forceUpper;
bool replaceNewLine;
}
@end
@@ -130,6 +131,7 @@ NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
{
maxLength = 0;
forceUpper = false;
replaceNewLine = true;
}
return self;
}
@@ -139,6 +141,16 @@ NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
maxLength = maxlen;
}
- (void) replaceNewLine:(bool) enable
{
replaceNewLine = enable;
}
- (bool) getReplaceNewLine
{
return replaceNewLine;
}
- (void) forceUpper
{
forceUpper = true;
@@ -177,7 +189,7 @@ NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
// wxTextEntryFormatter is always associated with single-line text entry (through wxNSTextFieldControl)
// so all new line characters should be replaced with spaces (like it is done in single-line NSCell).
NSString* lineStr = [*partialStringPtr stringByReplacingOccurrencesOfString: @"\n" withString: @" "];
NSString* lineStr = replaceNewLine ? [*partialStringPtr stringByReplacingOccurrencesOfString: @"\n" withString: @" "] : *partialStringPtr;
NSString* newStr = forceUpper ? [lineStr uppercaseString] : lineStr;
@@ -1327,6 +1339,15 @@ void wxNSTextViewControl::EnableAutomaticDashSubstitution(bool enable)
[m_textView setAutomaticDashSubstitutionEnabled:enable];
}
void wxNSTextViewControl::EnableNewLineReplacement(bool enable)
{
}
bool wxNSTextViewControl::GetNewLineReplacement()
{
return false;
}
wxSize wxNSTextViewControl::GetBestSize() const
{
wxSize size;
@@ -1443,6 +1464,16 @@ void wxNSTextFieldControl::SetMaxLength(unsigned long len)
[GetFormatter() setMaxLength:len];
}
void wxNSTextFieldControl::EnableNewLineReplacement(bool enable)
{
[GetFormatter() replaceNewLine:enable];
}
bool wxNSTextFieldControl::GetNewLineReplacement()
{
return [GetFormatter() getReplaceNewLine];
}
void wxNSTextFieldControl::ForceUpper()
{
[GetFormatter() forceUpper];

View File

@@ -132,6 +132,11 @@ void wxTextCtrl::MacCheckSpelling(bool check)
}
#endif // WXWIN_COMPATIBILITY_3_0 && wxUSE_SPELLCHECK
void wxTextCtrl::OSXEnableNewLineReplacement(bool enable)
{
GetTextPeer()->EnableNewLineReplacement(enable);
}
void wxTextCtrl::OSXEnableAutomaticQuoteSubstitution(bool enable)
{
GetTextPeer()->EnableAutomaticQuoteSubstitution(enable);
@@ -458,7 +463,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
return;
}
if ( !(m_windowStyle & wxTE_MULTILINE) )
if ( GetTextPeer()->GetNewLineReplacement() )
{
wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
if ( tlw && tlw->GetDefaultItem() )