KiCad PCB EDA Suite
SCINTILLA_TRICKS Class Reference

Add cut/copy/paste, dark theme, 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, bool aSingleLine, std::function< void()> m_enterCallback=[](){ })
 
void DoAutocomplete (const wxString &aPartial, const wxArrayString &aTokens)
 
void CancelAutocomplete ()
 

Protected Member Functions

void setupStyles ()
 
int firstNonWhitespace (int aLine, int *aWhitespaceCount=nullptr)
 
void onCharHook (wxKeyEvent &aEvent)
 
void onScintillaUpdateUI (wxStyledTextEvent &aEvent)
 
void onThemeChanged (wxSysColourChangedEvent &aEvent)
 

Protected Attributes

wxStyledTextCtrl * m_te
 
wxString m_braces
 
int m_lastCaretPos
 
bool m_suppressAutocomplete
 
bool m_singleLine
 
std::function< void()> m_returnCallback
 

Detailed Description

Add cut/copy/paste, dark theme, 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,
bool  aSingleLine,
std::function< void()>  m_enterCallback = [](){ } 
)

Definition at line 35 of file scintilla_tricks.cpp.

36  :
37  m_te( aScintilla ),
38  m_braces( aBraces ),
39  m_lastCaretPos( -1 ),
40  m_suppressAutocomplete( false ),
41  m_singleLine( aSingleLine ),
42  m_returnCallback( aReturnCallback )
43 {
44  // Always use LF as eol char, regardless the platform
45  m_te->SetEOLMode( wxSTC_EOL_LF );
46 
47  // A hack which causes Scintilla to auto-size the text editor canvas
48  // See: https://github.com/jacobslusser/ScintillaNET/issues/216
49  m_te->SetScrollWidth( 1 );
50  m_te->SetScrollWidthTracking( true );
51 
52  setupStyles();
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 
65  m_te->Bind( wxEVT_SYS_COLOUR_CHANGED,
66  wxSysColourChangedEventHandler( SCINTILLA_TRICKS::onThemeChanged ), this );
67 }
void onCharHook(wxKeyEvent &aEvent)
std::function< void()> m_returnCallback
void onThemeChanged(wxSysColourChangedEvent &aEvent)
wxStyledTextCtrl * m_te
void onScintillaUpdateUI(wxStyledTextEvent &aEvent)

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

Member Function Documentation

◆ CancelAutocomplete()

void SCINTILLA_TRICKS::CancelAutocomplete ( )

Definition at line 408 of file scintilla_tricks.cpp.

409 {
410  m_te->AutoCompCancel();
411 }
wxStyledTextCtrl * m_te

References m_te.

Referenced by DIALOG_TEXT_AND_LABEL_PROPERTIES::onMultiLineTCLostFocus(), and DIALOG_TEXT_PROPERTIES::onMultiLineTCLostFocus().

◆ DoAutocomplete()

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

Definition at line 379 of file scintilla_tricks.cpp.

380 {
382  return;
383 
384  wxArrayString matchedTokens;
385 
386  wxString filter = wxT( "*" ) + aPartial.Lower() + wxT( "*" );
387 
388  for( const wxString& token : aTokens )
389  {
390  if( token.Lower().Matches( filter ) )
391  matchedTokens.push_back( token );
392  }
393 
394  if( matchedTokens.size() > 0 )
395  {
396  // NB: tokens MUST be in alphabetical order because the Scintilla engine is going
397  // to do a binary search on them
398  matchedTokens.Sort( []( const wxString& first, const wxString& second ) -> int
399  {
400  return first.CmpNoCase( second );
401  });
402 
403  m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens, m_te->AutoCompGetSeparator() ) );
404  }
405 }
wxStyledTextCtrl * m_te

References filter, m_suppressAutocomplete, and m_te.

Referenced by PANEL_SETUP_RULES::onScintillaCharAdded(), DIALOG_TEXT_AND_LABEL_PROPERTIES::onScintillaCharAdded(), PROPERTIES_FRAME::onScintillaCharAdded(), and DIALOG_SCH_FIELD_PROPERTIES::onScintillaCharAdded().

◆ firstNonWhitespace()

int SCINTILLA_TRICKS::firstNonWhitespace ( int  aLine,
int *  aWhitespaceCount = nullptr 
)
protected

Definition at line 303 of file scintilla_tricks.cpp.

304 {
305  int lineStart = m_te->PositionFromLine( aLine );
306 
307  if( aWhitespaceCharCount )
308  *aWhitespaceCharCount = 0;
309 
310  for( int ii = 0; ii < m_te->GetLineLength( aLine ); ++ii )
311  {
312  int c = m_te->GetCharAt( lineStart + ii );
313 
314  if( c == ' ' || c == '\t' )
315  {
316  if( aWhitespaceCharCount )
317  *aWhitespaceCharCount += 1;
318 
319  continue;
320  }
321  else
322  {
323  return c;
324  }
325  }
326 
327  return '\r';
328 }
wxStyledTextCtrl * m_te

References m_te.

Referenced by onCharHook().

◆ onCharHook()

void SCINTILLA_TRICKS::onCharHook ( wxKeyEvent &  aEvent)
protected

Definition at line 152 of file scintilla_tricks.cpp.

153 {
154  wxString c = aEvent.GetUnicodeKey();
155 
156  if( !isalpha( aEvent.GetKeyCode() ) )
157  m_suppressAutocomplete = false;
158 
159  if( aEvent.GetKeyCode() == WXK_RETURN && ( m_singleLine || aEvent.ShiftDown() ) )
160  {
162  }
163  else if( ConvertSmartQuotesAndDashes( &c ) )
164  {
165  m_te->AddText( c );
166  }
167  else if( aEvent.GetKeyCode() == WXK_TAB )
168  {
169  if( aEvent.ControlDown() )
170  {
171  int flags = 0;
172 
173  if( !aEvent.ShiftDown() )
174  flags |= wxNavigationKeyEvent::IsForward;
175 
176  wxWindow* parent = m_te->GetParent();
177 
178  while( parent && dynamic_cast<DIALOG_SHIM*>( parent ) == nullptr )
179  parent = parent->GetParent();
180 
181  if( parent )
182  parent->NavigateIn( flags );
183  }
184  else
185  {
186  m_te->Tab();
187  }
188  }
189  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
190  {
191  m_te->Undo();
192  }
193  else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
194  || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Y' ) )
195  {
196  m_te->Redo();
197  }
198  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'A' )
199  {
200  m_te->SelectAll();
201  }
202  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'X' )
203  {
204  m_te->Cut();
205  }
206  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'C' )
207  {
208  m_te->Copy();
209  }
210  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'V' )
211  {
212  if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
213  m_te->DeleteBack();
214 
215  wxLogNull doNotLog; // disable logging of failed clipboard actions
216 
217  if( wxTheClipboard->Open() )
218  {
219  if( wxTheClipboard->IsSupported( wxDF_TEXT ) ||
220  wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
221  {
222  wxTextDataObject data;
223  wxString str;
224 
225  wxTheClipboard->GetData( data );
226  str = data.GetText();
227 
229  m_te->AddText( str );
230  }
231 
232  wxTheClipboard->Close();
233  }
234  }
235  else if( aEvent.GetKeyCode() == WXK_BACK )
236  {
237  m_te->DeleteBack();
238  }
239  else if( aEvent.GetKeyCode() == WXK_DELETE )
240  {
241  if( m_te->GetSelectionEnd() == m_te->GetSelectionStart() )
242  m_te->CharRightExtend();
243 
244  if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
245  m_te->DeleteBack();
246  }
247  else if( aEvent.GetKeyCode() == WXK_ESCAPE )
248  {
249  if( m_te->AutoCompActive() )
250  {
251  m_te->AutoCompCancel();
252  m_suppressAutocomplete = true; // Don't run autocomplete again on the next char...
253  }
254  else
255  {
256  aEvent.Skip();
257  }
258  }
259  else if( isCtrlSlash( aEvent ) )
260  {
261  int startLine = m_te->LineFromPosition( m_te->GetSelectionStart() );
262  int endLine = m_te->LineFromPosition( m_te->GetSelectionEnd() );
263  bool comment = firstNonWhitespace( startLine ) != '#';
264  int whitespaceCount;
265 
266  m_te->BeginUndoAction();
267 
268  for( int ii = startLine; ii <= endLine; ++ii )
269  {
270  if( comment )
271  m_te->InsertText( m_te->PositionFromLine( ii ), "#" );
272  else if( firstNonWhitespace( ii, &whitespaceCount ) == '#' )
273  m_te->DeleteRange( m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
274  }
275 
276  m_te->SetSelection( m_te->PositionFromLine( startLine ),
277  m_te->PositionFromLine( endLine ) + m_te->GetLineLength( endLine ) );
278 
279  m_te->EndUndoAction();
280  }
281 #ifdef __WXMAC__
282  else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() == 'A' )
283  {
284  m_te->LineEndWrap();
285  }
286  else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() == 'E' )
287  {
288  m_te->HomeWrap();
289  }
290 #endif
291  else if( aEvent.GetKeyCode() == WXK_SPECIAL20 )
292  {
293  // Proxy for a wxSysColourChangedEvent
294  setupStyles();
295  }
296  else
297  {
298  aEvent.Skip();
299  }
300 }
bool isCtrlSlash(wxKeyEvent &aEvent)
int firstNonWhitespace(int aLine, int *aWhitespaceCount=nullptr)
std::function< void()> m_returnCallback
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
wxStyledTextCtrl * m_te

References ConvertSmartQuotesAndDashes(), firstNonWhitespace(), isCtrlSlash(), m_returnCallback, m_singleLine, m_suppressAutocomplete, m_te, and setupStyles().

Referenced by SCINTILLA_TRICKS().

◆ onScintillaUpdateUI()

void SCINTILLA_TRICKS::onScintillaUpdateUI ( wxStyledTextEvent &  aEvent)
protected

Definition at line 331 of file scintilla_tricks.cpp.

332 {
333  auto isBrace = [this]( int c ) -> bool
334  {
335  return m_braces.Find( (wxChar) c ) >= 0;
336  };
337 
338  // Has the caret changed position?
339  int caretPos = m_te->GetCurrentPos();
340 
341  if( m_lastCaretPos != caretPos )
342  {
343  m_lastCaretPos = caretPos;
344  int bracePos1 = -1;
345  int bracePos2 = -1;
346 
347  // Is there a brace to the left or right?
348  if( caretPos > 0 && isBrace( m_te->GetCharAt( caretPos-1 ) ) )
349  bracePos1 = ( caretPos - 1 );
350  else if( isBrace( m_te->GetCharAt( caretPos ) ) )
351  bracePos1 = caretPos;
352 
353  if( bracePos1 >= 0 )
354  {
355  // Find the matching brace
356  bracePos2 = m_te->BraceMatch( bracePos1 );
357 
358  if( bracePos2 == -1 )
359  {
360  m_te->BraceBadLight( bracePos1 );
361  m_te->SetHighlightGuide( 0 );
362  }
363  else
364  {
365  m_te->BraceHighlight( bracePos1, bracePos2 );
366  m_te->SetHighlightGuide( m_te->GetColumn( bracePos1 ) );
367  }
368  }
369  else
370  {
371  // Turn off brace matching
372  m_te->BraceHighlight( -1, -1 );
373  m_te->SetHighlightGuide( 0 );
374  }
375  }
376 }
wxStyledTextCtrl * m_te

References m_braces, m_lastCaretPos, and m_te.

Referenced by SCINTILLA_TRICKS().

◆ onThemeChanged()

void SCINTILLA_TRICKS::onThemeChanged ( wxSysColourChangedEvent &  aEvent)
protected

Definition at line 70 of file scintilla_tricks.cpp.

71 {
72  setupStyles();
73 
74  aEvent.Skip();
75 }

References setupStyles().

Referenced by SCINTILLA_TRICKS().

◆ setupStyles()

void SCINTILLA_TRICKS::setupStyles ( )
protected

Definition at line 78 of file scintilla_tricks.cpp.

79 {
80  wxTextCtrl dummy( m_te->GetParent(), wxID_ANY );
81  wxColour foreground = dummy.GetForegroundColour();
82  wxColour background = dummy.GetBackgroundColour();
83  wxColour highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
84  wxColour highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
85 
86  m_te->StyleSetForeground( wxSTC_STYLE_DEFAULT, foreground );
87  m_te->StyleSetBackground( wxSTC_STYLE_DEFAULT, background );
88  m_te->StyleClearAll();
89 
90  m_te->SetSelForeground( true, highlightText );
91  m_te->SetSelBackground( true, highlight );
92  m_te->SetCaretForeground( foreground );
93 
94  if( !m_singleLine )
95  {
96  // Set a monospace font with a tab width of 4. This is the closest we can get to having
97  // Scintilla mimic the stroke font's tab positioning.
98  wxFont fixedFont = KIUI::GetMonospacedUIFont();
99 
100  for( size_t i = 0; i < wxSTC_STYLE_MAX; ++i )
101  m_te->StyleSetFont( i, fixedFont );
102 
103  m_te->SetTabWidth( 4 );
104  }
105 
106  // Set up the brace highlighting
107  unsigned char r = highlight.Red();
108  unsigned char g = highlight.Green();
109  unsigned char b = highlight.Blue();
110  wxColour::MakeGrey( &r, &g, &b );
111  highlight.Set( r, g, b );
112  m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText );
113  m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, highlight );
114  m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
115 }
wxFont GetMonospacedUIFont()
Definition: ui_common.cpp:85
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
E_SERIE r
Definition: eserie.cpp:41
wxStyledTextCtrl * m_te

References dummy(), KIUI::GetMonospacedUIFont(), m_singleLine, m_te, and r.

Referenced by onCharHook(), onThemeChanged(), and SCINTILLA_TRICKS().

Member Data Documentation

◆ m_braces

wxString SCINTILLA_TRICKS::m_braces
protected

Definition at line 57 of file scintilla_tricks.h.

Referenced by onScintillaUpdateUI(), and SCINTILLA_TRICKS().

◆ m_lastCaretPos

int SCINTILLA_TRICKS::m_lastCaretPos
protected

Definition at line 58 of file scintilla_tricks.h.

Referenced by onScintillaUpdateUI().

◆ m_returnCallback

std::function<void()> SCINTILLA_TRICKS::m_returnCallback
protected

Definition at line 62 of file scintilla_tricks.h.

Referenced by onCharHook().

◆ m_singleLine

bool SCINTILLA_TRICKS::m_singleLine
protected

Definition at line 60 of file scintilla_tricks.h.

Referenced by onCharHook(), and setupStyles().

◆ m_suppressAutocomplete

bool SCINTILLA_TRICKS::m_suppressAutocomplete
protected

Definition at line 59 of file scintilla_tricks.h.

Referenced by DoAutocomplete(), and onCharHook().

◆ m_te

wxStyledTextCtrl* SCINTILLA_TRICKS::m_te
protected

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