Filter line-endings from single-line Scintilla editors.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15793
This commit is contained in:
Jeff Young 2023-10-01 14:04:25 +01:00
parent 3b3e4fd34a
commit 34769cec63
4 changed files with 51 additions and 17 deletions

View File

@ -54,6 +54,12 @@ SCINTILLA_TRICKS::SCINTILLA_TRICKS( wxStyledTextCtrl* aScintilla, const wxString
m_te->SetScrollWidth( 1 ); m_te->SetScrollWidth( 1 );
m_te->SetScrollWidthTracking( true ); m_te->SetScrollWidthTracking( true );
if( m_singleLine )
{
m_te->SetUseVerticalScrollBar( false );
m_te->SetUseHorizontalScrollBar( false );
}
setupStyles(); setupStyles();
// Set up autocomplete // Set up autocomplete
@ -65,6 +71,7 @@ SCINTILLA_TRICKS::SCINTILLA_TRICKS( wxStyledTextCtrl* aScintilla, const wxString
// Hook up events // Hook up events
m_te->Bind( wxEVT_STC_UPDATEUI, &SCINTILLA_TRICKS::onScintillaUpdateUI, this ); m_te->Bind( wxEVT_STC_UPDATEUI, &SCINTILLA_TRICKS::onScintillaUpdateUI, this );
m_te->Bind( wxEVT_STC_MODIFIED, &SCINTILLA_TRICKS::onModified, this );
// Handle autocomplete // Handle autocomplete
m_te->Bind( wxEVT_STC_CHARADDED, &SCINTILLA_TRICKS::onChar, this ); m_te->Bind( wxEVT_STC_CHARADDED, &SCINTILLA_TRICKS::onChar, this );
@ -170,6 +177,31 @@ void SCINTILLA_TRICKS::onChar( wxStyledTextEvent& aEvent )
} }
void SCINTILLA_TRICKS::onModified( wxStyledTextEvent& aEvent )
{
if( m_singleLine )
{
wxString text = m_te->GetText();
if( text.Contains( wxS( "\n" ) ) || text.Contains( wxS( "\r" ) ) )
{
// Scintilla won't allow us to call SetText() from within this event processor,
// so we have to delay the processing.
CallAfter( [this]()
{
wxString text = m_te->GetText();
int currpos = m_te->GetCurrentPos();
text.Replace( wxS( "\n" ), wxS( "" ) );
text.Replace( wxS( "\r" ), wxS( "" ) );
m_te->SetText( text );
m_te->GotoPos( currpos-1 );
} );
}
}
}
void SCINTILLA_TRICKS::onCharHook( wxKeyEvent& aEvent ) void SCINTILLA_TRICKS::onCharHook( wxKeyEvent& aEvent )
{ {
wxString c = aEvent.GetUnicodeKey(); wxString c = aEvent.GetUnicodeKey();
@ -267,6 +299,13 @@ void SCINTILLA_TRICKS::onCharHook( wxKeyEvent& aEvent )
str = data.GetText(); str = data.GetText();
ConvertSmartQuotesAndDashes( &str ); ConvertSmartQuotesAndDashes( &str );
if( m_singleLine )
{
str.Replace( wxS( "\n" ), wxEmptyString );
str.Replace( wxS( "\r" ), wxEmptyString );
}
m_te->BeginUndoAction(); m_te->BeginUndoAction();
m_te->AddText( str ); m_te->AddText( str );
m_te->EndUndoAction(); m_te->EndUndoAction();

View File

@ -160,15 +160,13 @@ void DIALOG_FIELD_PROPERTIES::init()
m_CommonConvert->Show( false ); m_CommonConvert->Show( false );
m_CommonUnit->Show( false ); m_CommonUnit->Show( false );
// Predefined fields cannot contain some chars, or cannot be empty, // Predefined fields cannot contain some chars and cannot be empty, so they need a
// and need a SCH_FIELD_VALIDATOR (m_StyledTextCtrl cannot use a SCH_FIELD_VALIDATOR). // SCH_FIELD_VALIDATOR (m_StyledTextCtrl cannot use a SCH_FIELD_VALIDATOR).
bool use_validator = m_fieldId == REFERENCE_FIELD if( m_fieldId == REFERENCE_FIELD
|| m_fieldId == FOOTPRINT_FIELD || m_fieldId == FOOTPRINT_FIELD
|| m_fieldId == DATASHEET_FIELD || m_fieldId == DATASHEET_FIELD
|| m_fieldId == SHEETNAME_V || m_fieldId == SHEETNAME_V
|| m_fieldId == SHEETFILENAME_V; || m_fieldId == SHEETFILENAME_V )
if( use_validator )
{ {
m_TextCtrl->SetValidator( FIELD_VALIDATOR( m_fieldId, &m_text ) ); m_TextCtrl->SetValidator( FIELD_VALIDATOR( m_fieldId, &m_text ) );
SetInitialFocus( m_TextCtrl ); SetInitialFocus( m_TextCtrl );
@ -190,9 +188,8 @@ void DIALOG_FIELD_PROPERTIES::init()
GetSizer()->SetSizeHints( this ); GetSizer()->SetSizeHints( this );
// Adjust the height of the scintilla text editor after the first layout // Adjust the height of the scintilla editor after the first layout to show a single line
// To show only one line // (multiline text is not supported in fields and will be removed)
// (multiline text are is supported in fields and will be removed)
if( m_StyledTextCtrl->IsShown() ) if( m_StyledTextCtrl->IsShown() )
{ {
wxSize maxSize = m_StyledTextCtrl->GetSize(); wxSize maxSize = m_StyledTextCtrl->GetSize();

View File

@ -677,10 +677,7 @@ void SCH_FIELD::OnScintillaCharAdded( SCINTILLA_TRICKS* aScintillaTricks,
int start = scintilla->WordStartPosition( pos, true ); int start = scintilla->WordStartPosition( pos, true );
wxString partial; wxString partial;
// Currently, '\n' is not allowed in fields. So remove it when entered // Multi-line fields are not allowed. So remove '\n' if entered.
// TODO: see if we must close the dialog. However this is not obvious, as
// if a \n is typed (and removed) when a text is selected, this text is deleted
// (in fact replaced by \n, that is removed by the filter)
if( key == '\n' ) if( key == '\n' )
{ {
wxString text = scintilla->GetText(); wxString text = scintilla->GetText();

View File

@ -1,7 +1,7 @@
/* /*
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2020-2022 KiCad Developers, see change_log.txt for contributors. * Copyright (C) 2020-2023 KiCad Developers, see change_log.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -60,6 +60,7 @@ protected:
void onCharHook( wxKeyEvent& aEvent ); void onCharHook( wxKeyEvent& aEvent );
void onChar( wxStyledTextEvent& aEvent ); void onChar( wxStyledTextEvent& aEvent );
void onModified( wxStyledTextEvent& aEvent );
void onScintillaUpdateUI( wxStyledTextEvent& aEvent ); void onScintillaUpdateUI( wxStyledTextEvent& aEvent );
void onThemeChanged( wxSysColourChangedEvent &aEvent ); void onThemeChanged( wxSysColourChangedEvent &aEvent );