KiCad PCB EDA Suite
SCINTILLA_TRICKS Class Reference

SCINTILLA_TRICKS is used to add cut/copy/paste, autocomplete and brace highlighting to a wxStyleTextCtrl instance. More...

#include <scintilla_tricks.h>

Inheritance diagram for SCINTILLA_TRICKS:

Public Member Functions

 SCINTILLA_TRICKS (wxStyledTextCtrl *aScintilla, const wxString &aBraces)
 
void DoAutocomplete (const wxString &aPartial, const wxArrayString &aTokens)
 

Protected Member Functions

void onCharHook (wxKeyEvent &aEvent)
 
void onScintillaUpdateUI (wxStyledTextEvent &aEvent)
 

Protected Attributes

wxStyledTextCtrl * m_te
 
wxString m_braces
 
int m_lastCaretPos
 

Detailed Description

SCINTILLA_TRICKS is used to add cut/copy/paste, autocomplete and brace highlighting to a wxStyleTextCtrl instance.

Definition at line 35 of file scintilla_tricks.h.

Constructor & Destructor Documentation

◆ SCINTILLA_TRICKS()

SCINTILLA_TRICKS::SCINTILLA_TRICKS ( wxStyledTextCtrl *  aScintilla,
const wxString &  aBraces 
)

Definition at line 31 of file scintilla_tricks.cpp.

31  :
32  m_te( aScintilla ),
33  m_braces( aBraces ),
34  m_lastCaretPos( -1 )
35 {
36  // A hack which causes Scintilla to auto-size the text editor canvas
37  // See: https://github.com/jacobslusser/ScintillaNET/issues/216
38  m_te->SetScrollWidth( 1 );
39  m_te->SetScrollWidthTracking( true );
40 
41  // Set up the brace highlighting
42  wxColour highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
43  wxColour highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
44 
45  if( KIGFX::COLOR4D( highlightText ).GetBrightness() > 0.5 )
46  highlight = highlight.ChangeLightness( 80 );
47  else
48  highlight = highlight.ChangeLightness( 120 );
49 
50  m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText );
51  m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, highlight );
52  m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
53 
54  // Set up autocomplete
55  m_te->AutoCompSetIgnoreCase( true );
56  m_te->AutoCompSetFillUps( m_braces[1] );
57  m_te->AutoCompSetMaxHeight( 20 );
58 
59  // Hook up events
60  m_te->Bind( wxEVT_STC_UPDATEUI, &SCINTILLA_TRICKS::onScintillaUpdateUI, this );
61 
62  // Dispatch command-keys in Scintilla control.
63  m_te->Bind( wxEVT_CHAR_HOOK, &SCINTILLA_TRICKS::onCharHook, this );
64 }
void onCharHook(wxKeyEvent &aEvent)
wxStyledTextCtrl * m_te
void onScintillaUpdateUI(wxStyledTextEvent &aEvent)
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99

References m_braces, m_te, onCharHook(), and onScintillaUpdateUI().

Member Function Documentation

◆ DoAutocomplete()

void SCINTILLA_TRICKS::DoAutocomplete ( const wxString &  aPartial,
const wxArrayString &  aTokens 
)

Definition at line 178 of file scintilla_tricks.cpp.

179 {
180  wxArrayString matchedTokens;
181 
182  wxString filter = wxT( "*" ) + aPartial.Lower() + wxT( "*" );
183 
184  for( const wxString& token : aTokens )
185  {
186  if( token.Lower().Matches( filter ) )
187  matchedTokens.push_back( token );
188  }
189 
190  if( matchedTokens.size() > 0 )
191  {
192  // NB: tokens MUST be in alphabetical order because the Scintilla engine is going
193  // to do a binary search on them
194  matchedTokens.Sort( []( const wxString& first, const wxString& second ) -> int
195  {
196  return first.CmpNoCase( second );
197  });
198 
199  m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens, ' ' ) );
200  }
201 }
wxStyledTextCtrl * m_te

References m_te.

Referenced by PANEL_SETUP_RULES::onScintillaCharAdded(), PROPERTIES_FRAME::onScintillaCharAdded(), DIALOG_LABEL_EDITOR::onScintillaCharAdded(), and DIALOG_SCH_EDIT_ONE_FIELD::onScintillaCharAdded().

◆ onCharHook()

void SCINTILLA_TRICKS::onCharHook ( wxKeyEvent &  aEvent)
protected

Definition at line 67 of file scintilla_tricks.cpp.

68 {
69  if( aEvent.GetKeyCode() == WXK_TAB )
70  {
71  if( aEvent.ControlDown() )
72  {
73  int flags = 0;
74 
75  if( !aEvent.ShiftDown() )
76  flags |= wxNavigationKeyEvent::IsForward;
77 
78  wxWindow* parent = m_te->GetParent();
79 
80  while( parent && dynamic_cast<DIALOG_SHIM*>( parent ) == nullptr )
81  parent = parent->GetParent();
82 
83  if( parent )
84  parent->NavigateIn( flags );
85  }
86  else
87  {
88  m_te->Tab();
89  }
90  }
91  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
92  {
93  m_te->Undo();
94  }
95  else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
96  || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Y' ) )
97  {
98  m_te->Redo();
99  }
100  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'X' )
101  {
102  m_te->Cut();
103  }
104  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'C' )
105  {
106  m_te->Copy();
107  }
108  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'V' )
109  {
110  m_te->Paste();
111  }
112  else if( aEvent.GetKeyCode() == WXK_BACK )
113  {
114  m_te->DeleteBack();
115  }
116  else if( aEvent.GetKeyCode() == WXK_DELETE )
117  {
118  if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
119  m_te->DeleteBack();
120  else
121  m_te->DeleteRange( m_te->GetSelectionStart(), 1 );
122  }
123  else
124  {
125  aEvent.Skip();
126  }
127 }
wxStyledTextCtrl * m_te

References m_te.

Referenced by SCINTILLA_TRICKS().

◆ onScintillaUpdateUI()

void SCINTILLA_TRICKS::onScintillaUpdateUI ( wxStyledTextEvent &  aEvent)
protected

Definition at line 130 of file scintilla_tricks.cpp.

131 {
132  auto isBrace = [this]( int c ) -> bool
133  {
134  return m_braces.Find( (wxChar) c ) >= 0;
135  };
136 
137  // Has the caret changed position?
138  int caretPos = m_te->GetCurrentPos();
139 
140  if( m_lastCaretPos != caretPos )
141  {
142  m_lastCaretPos = caretPos;
143  int bracePos1 = -1;
144  int bracePos2 = -1;
145 
146  // Is there a brace to the left or right?
147  if( caretPos > 0 && isBrace( m_te->GetCharAt( caretPos-1 ) ) )
148  bracePos1 = ( caretPos - 1 );
149  else if( isBrace( m_te->GetCharAt( caretPos ) ) )
150  bracePos1 = caretPos;
151 
152  if( bracePos1 >= 0 )
153  {
154  // Find the matching brace
155  bracePos2 = m_te->BraceMatch( bracePos1 );
156 
157  if( bracePos2 == -1 )
158  {
159  m_te->BraceBadLight( bracePos1 );
160  m_te->SetHighlightGuide( 0 );
161  }
162  else
163  {
164  m_te->BraceHighlight( bracePos1, bracePos2 );
165  m_te->SetHighlightGuide( m_te->GetColumn( bracePos1 ) );
166  }
167  }
168  else
169  {
170  // Turn off brace matching
171  m_te->BraceHighlight( -1, -1 );
172  m_te->SetHighlightGuide( 0 );
173  }
174  }
175 }
wxStyledTextCtrl * m_te

References m_braces, m_lastCaretPos, and m_te.

Referenced by SCINTILLA_TRICKS().

Member Data Documentation

◆ m_braces

wxString SCINTILLA_TRICKS::m_braces
protected

Definition at line 49 of file scintilla_tricks.h.

Referenced by onScintillaUpdateUI(), and SCINTILLA_TRICKS().

◆ m_lastCaretPos

int SCINTILLA_TRICKS::m_lastCaretPos
protected

Definition at line 51 of file scintilla_tricks.h.

Referenced by onScintillaUpdateUI().

◆ m_te

wxStyledTextCtrl* SCINTILLA_TRICKS::m_te
protected

The documentation for this class was generated from the following files: