2020-05-27 22:28:36 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 KiCad Developers, see change_log.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-08-10 17:16:22 +00:00
|
|
|
#include <kicad_string.h>
|
2020-05-27 22:28:36 +00:00
|
|
|
#include <scintilla_tricks.h>
|
2020-07-22 20:27:38 +00:00
|
|
|
#include <wx/stc/stc.h>
|
2020-05-27 22:28:36 +00:00
|
|
|
#include <gal/color4d.h>
|
|
|
|
#include <dialog_shim.h>
|
2020-08-10 17:16:22 +00:00
|
|
|
#include <wx/clipbrd.h>
|
2020-10-19 08:17:18 +00:00
|
|
|
#include <wx/settings.h>
|
2021-01-23 22:12:25 +00:00
|
|
|
#include <confirm.h>
|
2020-05-27 22:28:36 +00:00
|
|
|
|
|
|
|
SCINTILLA_TRICKS::SCINTILLA_TRICKS( wxStyledTextCtrl* aScintilla, const wxString& aBraces ) :
|
|
|
|
m_te( aScintilla ),
|
|
|
|
m_braces( aBraces ),
|
2021-01-23 22:12:25 +00:00
|
|
|
m_lastCaretPos( -1 ),
|
|
|
|
m_suppressAutocomplete( false )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
|
|
|
// A hack which causes Scintilla to auto-size the text editor canvas
|
|
|
|
// See: https://github.com/jacobslusser/ScintillaNET/issues/216
|
|
|
|
m_te->SetScrollWidth( 1 );
|
|
|
|
m_te->SetScrollWidthTracking( true );
|
|
|
|
|
|
|
|
// Set up the brace highlighting
|
|
|
|
wxColour highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
|
|
|
|
wxColour highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
unsigned char r = highlight.Red();
|
|
|
|
unsigned char g = highlight.Green();
|
|
|
|
unsigned char b = highlight.Blue();
|
|
|
|
wxColour::MakeGrey( &r, &g, &b );
|
|
|
|
highlight.Set( r, g, b );
|
2020-05-27 22:28:36 +00:00
|
|
|
|
|
|
|
m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText );
|
|
|
|
m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, highlight );
|
|
|
|
m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
|
|
|
|
|
2020-05-30 19:00:09 +00:00
|
|
|
// Set up autocomplete
|
|
|
|
m_te->AutoCompSetIgnoreCase( true );
|
|
|
|
m_te->AutoCompSetFillUps( m_braces[1] );
|
|
|
|
m_te->AutoCompSetMaxHeight( 20 );
|
|
|
|
|
2020-05-27 22:28:36 +00:00
|
|
|
// Hook up events
|
|
|
|
m_te->Bind( wxEVT_STC_UPDATEUI, &SCINTILLA_TRICKS::onScintillaUpdateUI, this );
|
|
|
|
|
|
|
|
// Dispatch command-keys in Scintilla control.
|
|
|
|
m_te->Bind( wxEVT_CHAR_HOOK, &SCINTILLA_TRICKS::onCharHook, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-06 17:44:44 +00:00
|
|
|
bool isCtrlSlash( wxKeyEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( !aEvent.ControlDown() || aEvent.MetaDown() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aEvent.GetUnicodeKey() == '/' )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// OK, now the wxWidgets hacks start.
|
|
|
|
// (We should abandon these if https://trac.wxwidgets.org/ticket/18911 gets resolved.)
|
|
|
|
|
|
|
|
// Many Latin America and European keyboars have have the / over the 7. We know that
|
|
|
|
// wxWidgets messes this up and returns Shift+7 through GetUnicodeKey(). However, other
|
|
|
|
// keyboards (such as France and Belgium) have 7 in the shifted position, so a Shift+7
|
|
|
|
// *could* be legitimate.
|
|
|
|
|
|
|
|
// However, we *are* checking Ctrl, so to assume any Shift+7 is a Ctrl-/ really only
|
|
|
|
// disallows Ctrl+Shift+7 from doing something else, which is probably OK. (This routine
|
|
|
|
// is only used in the Scintilla editor, not in the rest of Kicad.)
|
|
|
|
|
|
|
|
// The other main shifted loation of / is over : (France and Belgium), so we'll sacrifice
|
|
|
|
// Ctrl+Shift+: too.
|
|
|
|
|
|
|
|
if( aEvent.ShiftDown() && ( aEvent.GetUnicodeKey() == '7' || aEvent.GetUnicodeKey() == ':' ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// A few keyboards have / in an Alt position. Since we're expressly not checking Alt for
|
|
|
|
// up or down, those should work. However, if they don't, there's room below for yet
|
|
|
|
// another hack....
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-27 22:28:36 +00:00
|
|
|
void SCINTILLA_TRICKS::onCharHook( wxKeyEvent& aEvent )
|
|
|
|
{
|
2020-08-10 17:16:22 +00:00
|
|
|
wxString c = aEvent.GetUnicodeKey();
|
|
|
|
|
2021-01-23 22:12:25 +00:00
|
|
|
if( !isalpha( aEvent.GetKeyCode() ) )
|
|
|
|
m_suppressAutocomplete = false;
|
|
|
|
|
2020-08-10 17:16:22 +00:00
|
|
|
if( ConvertSmartQuotesAndDashes( &c ) )
|
|
|
|
{
|
|
|
|
m_te->AddText( c );
|
|
|
|
}
|
|
|
|
else if( aEvent.GetKeyCode() == WXK_TAB )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
|
|
|
if( aEvent.ControlDown() )
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if( !aEvent.ShiftDown() )
|
|
|
|
flags |= wxNavigationKeyEvent::IsForward;
|
|
|
|
|
|
|
|
wxWindow* parent = m_te->GetParent();
|
|
|
|
|
|
|
|
while( parent && dynamic_cast<DIALOG_SHIM*>( parent ) == nullptr )
|
|
|
|
parent = parent->GetParent();
|
|
|
|
|
|
|
|
if( parent )
|
|
|
|
parent->NavigateIn( flags );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_te->Tab();
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
|
|
|
m_te->Undo();
|
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
|
|
|
|
|| ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Y' ) )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
|
|
|
m_te->Redo();
|
|
|
|
}
|
2020-09-07 11:35:49 +00:00
|
|
|
else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'A' )
|
|
|
|
{
|
|
|
|
m_te->SelectAll();
|
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'X' )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
|
|
|
m_te->Cut();
|
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'C' )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
|
|
|
m_te->Copy();
|
|
|
|
}
|
2020-07-14 12:28:55 +00:00
|
|
|
else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'V' )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
2020-08-10 17:16:22 +00:00
|
|
|
if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
|
|
|
|
m_te->DeleteBack();
|
|
|
|
|
|
|
|
if( wxTheClipboard->Open() )
|
|
|
|
{
|
|
|
|
if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
|
|
|
|
{
|
|
|
|
wxTextDataObject data;
|
|
|
|
wxString str;
|
|
|
|
|
|
|
|
wxTheClipboard->GetData( data );
|
|
|
|
str = data.GetText();
|
|
|
|
|
|
|
|
ConvertSmartQuotesAndDashes( &str );
|
|
|
|
m_te->AddText( str );
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
}
|
2020-05-27 22:28:36 +00:00
|
|
|
}
|
2020-05-30 11:31:19 +00:00
|
|
|
else if( aEvent.GetKeyCode() == WXK_BACK )
|
|
|
|
{
|
|
|
|
m_te->DeleteBack();
|
|
|
|
}
|
|
|
|
else if( aEvent.GetKeyCode() == WXK_DELETE )
|
|
|
|
{
|
|
|
|
if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
|
|
|
|
m_te->DeleteBack();
|
2020-06-05 12:35:35 +00:00
|
|
|
else
|
2020-06-05 13:24:26 +00:00
|
|
|
m_te->DeleteRange( m_te->GetSelectionStart(), 1 );
|
2020-05-30 11:31:19 +00:00
|
|
|
}
|
2021-01-23 22:12:25 +00:00
|
|
|
else if( aEvent.GetKeyCode() == WXK_ESCAPE )
|
|
|
|
{
|
|
|
|
if( m_te->AutoCompActive() )
|
|
|
|
{
|
|
|
|
m_te->AutoCompCancel();
|
|
|
|
m_suppressAutocomplete = true; // Don't run autocomplete again on the next char...
|
|
|
|
}
|
2021-01-24 20:31:04 +00:00
|
|
|
else
|
2021-01-23 22:12:25 +00:00
|
|
|
{
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 17:44:44 +00:00
|
|
|
else if( isCtrlSlash( aEvent ) )
|
2020-09-03 19:33:33 +00:00
|
|
|
{
|
|
|
|
int startLine = m_te->LineFromPosition( m_te->GetSelectionStart() );
|
|
|
|
int endLine = m_te->LineFromPosition( m_te->GetSelectionEnd() );
|
|
|
|
bool comment = firstNonWhitespace( startLine ) != '#';
|
|
|
|
int whitespaceCount;
|
|
|
|
|
|
|
|
m_te->BeginUndoAction();
|
|
|
|
|
|
|
|
for( int ii = startLine; ii <= endLine; ++ii )
|
|
|
|
{
|
|
|
|
if( comment )
|
|
|
|
m_te->InsertText( m_te->PositionFromLine( ii ), "#" );
|
|
|
|
else if( firstNonWhitespace( ii, &whitespaceCount ) == '#' )
|
|
|
|
m_te->DeleteRange( m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_te->SetSelection( m_te->PositionFromLine( startLine ),
|
|
|
|
m_te->PositionFromLine( endLine ) + m_te->GetLineLength( endLine ) );
|
|
|
|
|
|
|
|
m_te->EndUndoAction();
|
|
|
|
}
|
2020-05-27 22:28:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-03 19:33:33 +00:00
|
|
|
int SCINTILLA_TRICKS::firstNonWhitespace( int aLine, int* aWhitespaceCharCount )
|
|
|
|
{
|
|
|
|
int lineStart = m_te->PositionFromLine( aLine );
|
|
|
|
|
|
|
|
if( aWhitespaceCharCount )
|
|
|
|
*aWhitespaceCharCount = 0;
|
|
|
|
|
|
|
|
for( int ii = 0; ii < m_te->GetLineLength( aLine ); ++ii )
|
|
|
|
{
|
|
|
|
int c = m_te->GetCharAt( lineStart + ii );
|
|
|
|
|
|
|
|
if( c == ' ' || c == '\t' )
|
|
|
|
{
|
|
|
|
if( aWhitespaceCharCount )
|
|
|
|
*aWhitespaceCharCount += 1;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '\r';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-27 22:28:36 +00:00
|
|
|
void SCINTILLA_TRICKS::onScintillaUpdateUI( wxStyledTextEvent& aEvent )
|
|
|
|
{
|
|
|
|
auto isBrace = [this]( int c ) -> bool
|
|
|
|
{
|
2020-05-27 23:18:28 +00:00
|
|
|
return m_braces.Find( (wxChar) c ) >= 0;
|
2020-05-27 22:28:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Has the caret changed position?
|
|
|
|
int caretPos = m_te->GetCurrentPos();
|
|
|
|
|
|
|
|
if( m_lastCaretPos != caretPos )
|
|
|
|
{
|
|
|
|
m_lastCaretPos = caretPos;
|
|
|
|
int bracePos1 = -1;
|
|
|
|
int bracePos2 = -1;
|
|
|
|
|
|
|
|
// Is there a brace to the left or right?
|
|
|
|
if( caretPos > 0 && isBrace( m_te->GetCharAt( caretPos-1 ) ) )
|
|
|
|
bracePos1 = ( caretPos - 1 );
|
|
|
|
else if( isBrace( m_te->GetCharAt( caretPos ) ) )
|
|
|
|
bracePos1 = caretPos;
|
|
|
|
|
|
|
|
if( bracePos1 >= 0 )
|
|
|
|
{
|
|
|
|
// Find the matching brace
|
|
|
|
bracePos2 = m_te->BraceMatch( bracePos1 );
|
|
|
|
|
|
|
|
if( bracePos2 == -1 )
|
|
|
|
{
|
|
|
|
m_te->BraceBadLight( bracePos1 );
|
|
|
|
m_te->SetHighlightGuide( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_te->BraceHighlight( bracePos1, bracePos2 );
|
|
|
|
m_te->SetHighlightGuide( m_te->GetColumn( bracePos1 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Turn off brace matching
|
|
|
|
m_te->BraceHighlight( -1, -1 );
|
|
|
|
m_te->SetHighlightGuide( 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-30 19:00:09 +00:00
|
|
|
void SCINTILLA_TRICKS::DoAutocomplete( const wxString& aPartial, const wxArrayString& aTokens )
|
2020-05-27 22:28:36 +00:00
|
|
|
{
|
2021-01-23 22:12:25 +00:00
|
|
|
if( m_suppressAutocomplete )
|
|
|
|
return;
|
|
|
|
|
2020-05-30 19:00:09 +00:00
|
|
|
wxArrayString matchedTokens;
|
2020-05-27 22:28:36 +00:00
|
|
|
|
2020-05-30 19:00:09 +00:00
|
|
|
wxString filter = wxT( "*" ) + aPartial.Lower() + wxT( "*" );
|
2020-05-27 22:28:36 +00:00
|
|
|
|
2020-05-30 19:00:09 +00:00
|
|
|
for( const wxString& token : aTokens )
|
|
|
|
{
|
|
|
|
if( token.Lower().Matches( filter ) )
|
|
|
|
matchedTokens.push_back( token );
|
|
|
|
}
|
2020-05-27 22:28:36 +00:00
|
|
|
|
2020-05-30 19:00:09 +00:00
|
|
|
if( matchedTokens.size() > 0 )
|
|
|
|
{
|
|
|
|
// NB: tokens MUST be in alphabetical order because the Scintilla engine is going
|
|
|
|
// to do a binary search on them
|
2020-07-22 20:27:38 +00:00
|
|
|
matchedTokens.Sort( []( const wxString& first, const wxString& second ) -> int
|
|
|
|
{
|
|
|
|
return first.CmpNoCase( second );
|
|
|
|
});
|
2020-05-30 19:00:09 +00:00
|
|
|
|
|
|
|
m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens, ' ' ) );
|
2020-05-27 22:28:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|