KiCad PCB EDA Suite
scintilla_tricks.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 KiCad Developers, see change_log.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
25 #include <kicad_string.h>
26 #include <scintilla_tricks.h>
27 #include <wx/stc/stc.h>
28 #include <gal/color4d.h>
29 #include <dialog_shim.h>
30 #include <wx/clipbrd.h>
31 #include <wx/settings.h>
32 #include <confirm.h>
33 
34 SCINTILLA_TRICKS::SCINTILLA_TRICKS( wxStyledTextCtrl* aScintilla, const wxString& aBraces ) :
35  m_te( aScintilla ),
36  m_braces( aBraces ),
37  m_lastCaretPos( -1 ),
38  m_suppressAutocomplete( false )
39 {
40  // A hack which causes Scintilla to auto-size the text editor canvas
41  // See: https://github.com/jacobslusser/ScintillaNET/issues/216
42  m_te->SetScrollWidth( 1 );
43  m_te->SetScrollWidthTracking( true );
44 
45  // Set up the brace highlighting
46  wxColour highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
47  wxColour highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
48 
49  unsigned char r = highlight.Red();
50  unsigned char g = highlight.Green();
51  unsigned char b = highlight.Blue();
52  wxColour::MakeGrey( &r, &g, &b );
53  highlight.Set( r, g, b );
54 
55  m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText );
56  m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, highlight );
57  m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
58 
59  // Set up autocomplete
60  m_te->AutoCompSetIgnoreCase( true );
61  m_te->AutoCompSetFillUps( m_braces[1] );
62  m_te->AutoCompSetMaxHeight( 20 );
63 
64  // Hook up events
65  m_te->Bind( wxEVT_STC_UPDATEUI, &SCINTILLA_TRICKS::onScintillaUpdateUI, this );
66 
67  // Dispatch command-keys in Scintilla control.
68  m_te->Bind( wxEVT_CHAR_HOOK, &SCINTILLA_TRICKS::onCharHook, this );
69 }
70 
71 
72 bool isCtrlSlash( wxKeyEvent& aEvent )
73 {
74  if( !aEvent.ControlDown() || aEvent.MetaDown() )
75  return false;
76 
77  if( aEvent.GetUnicodeKey() == '/' )
78  return true;
79 
80  // OK, now the wxWidgets hacks start.
81  // (We should abandon these if https://trac.wxwidgets.org/ticket/18911 gets resolved.)
82 
83  // Many Latin America and European keyboars have have the / over the 7. We know that
84  // wxWidgets messes this up and returns Shift+7 through GetUnicodeKey(). However, other
85  // keyboards (such as France and Belgium) have 7 in the shifted position, so a Shift+7
86  // *could* be legitimate.
87 
88  // However, we *are* checking Ctrl, so to assume any Shift+7 is a Ctrl-/ really only
89  // disallows Ctrl+Shift+7 from doing something else, which is probably OK. (This routine
90  // is only used in the Scintilla editor, not in the rest of Kicad.)
91 
92  // The other main shifted loation of / is over : (France and Belgium), so we'll sacrifice
93  // Ctrl+Shift+: too.
94 
95  if( aEvent.ShiftDown() && ( aEvent.GetUnicodeKey() == '7' || aEvent.GetUnicodeKey() == ':' ) )
96  return true;
97 
98  // A few keyboards have / in an Alt position. Since we're expressly not checking Alt for
99  // up or down, those should work. However, if they don't, there's room below for yet
100  // another hack....
101 
102  return false;
103 }
104 
105 
106 void SCINTILLA_TRICKS::onCharHook( wxKeyEvent& aEvent )
107 {
108  wxString c = aEvent.GetUnicodeKey();
109 
110  if( !isalpha( aEvent.GetKeyCode() ) )
111  m_suppressAutocomplete = false;
112 
113  if( ConvertSmartQuotesAndDashes( &c ) )
114  {
115  m_te->AddText( c );
116  }
117  else if( aEvent.GetKeyCode() == WXK_TAB )
118  {
119  if( aEvent.ControlDown() )
120  {
121  int flags = 0;
122 
123  if( !aEvent.ShiftDown() )
124  flags |= wxNavigationKeyEvent::IsForward;
125 
126  wxWindow* parent = m_te->GetParent();
127 
128  while( parent && dynamic_cast<DIALOG_SHIM*>( parent ) == nullptr )
129  parent = parent->GetParent();
130 
131  if( parent )
132  parent->NavigateIn( flags );
133  }
134  else
135  {
136  m_te->Tab();
137  }
138  }
139  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
140  {
141  m_te->Undo();
142  }
143  else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() == 'Z' )
144  || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'Y' ) )
145  {
146  m_te->Redo();
147  }
148  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'A' )
149  {
150  m_te->SelectAll();
151  }
152  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'X' )
153  {
154  m_te->Cut();
155  }
156  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'C' )
157  {
158  m_te->Copy();
159  }
160  else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'V' )
161  {
162  if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
163  m_te->DeleteBack();
164 
165  wxLogNull doNotLog; // disable logging of failed clipboard actions
166 
167  if( wxTheClipboard->Open() )
168  {
169  if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
170  {
171  wxTextDataObject data;
172  wxString str;
173 
174  wxTheClipboard->GetData( data );
175  str = data.GetText();
176 
178  m_te->AddText( str );
179  }
180 
181  wxTheClipboard->Close();
182  }
183  }
184  else if( aEvent.GetKeyCode() == WXK_BACK )
185  {
186  m_te->DeleteBack();
187  }
188  else if( aEvent.GetKeyCode() == WXK_DELETE )
189  {
190  if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
191  m_te->DeleteBack();
192  else
193  m_te->DeleteRange( m_te->GetSelectionStart(), 1 );
194  }
195  else if( aEvent.GetKeyCode() == WXK_ESCAPE )
196  {
197  if( m_te->AutoCompActive() )
198  {
199  m_te->AutoCompCancel();
200  m_suppressAutocomplete = true; // Don't run autocomplete again on the next char...
201  }
202  else
203  {
204  aEvent.Skip();
205  }
206  }
207  else if( isCtrlSlash( aEvent ) )
208  {
209  int startLine = m_te->LineFromPosition( m_te->GetSelectionStart() );
210  int endLine = m_te->LineFromPosition( m_te->GetSelectionEnd() );
211  bool comment = firstNonWhitespace( startLine ) != '#';
212  int whitespaceCount;
213 
214  m_te->BeginUndoAction();
215 
216  for( int ii = startLine; ii <= endLine; ++ii )
217  {
218  if( comment )
219  m_te->InsertText( m_te->PositionFromLine( ii ), "#" );
220  else if( firstNonWhitespace( ii, &whitespaceCount ) == '#' )
221  m_te->DeleteRange( m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
222  }
223 
224  m_te->SetSelection( m_te->PositionFromLine( startLine ),
225  m_te->PositionFromLine( endLine ) + m_te->GetLineLength( endLine ) );
226 
227  m_te->EndUndoAction();
228  }
229  else
230  {
231  aEvent.Skip();
232  }
233 }
234 
235 
236 int SCINTILLA_TRICKS::firstNonWhitespace( int aLine, int* aWhitespaceCharCount )
237 {
238  int lineStart = m_te->PositionFromLine( aLine );
239 
240  if( aWhitespaceCharCount )
241  *aWhitespaceCharCount = 0;
242 
243  for( int ii = 0; ii < m_te->GetLineLength( aLine ); ++ii )
244  {
245  int c = m_te->GetCharAt( lineStart + ii );
246 
247  if( c == ' ' || c == '\t' )
248  {
249  if( aWhitespaceCharCount )
250  *aWhitespaceCharCount += 1;
251 
252  continue;
253  }
254  else
255  {
256  return c;
257  }
258  }
259 
260  return '\r';
261 }
262 
263 
264 void SCINTILLA_TRICKS::onScintillaUpdateUI( wxStyledTextEvent& aEvent )
265 {
266  auto isBrace = [this]( int c ) -> bool
267  {
268  return m_braces.Find( (wxChar) c ) >= 0;
269  };
270 
271  // Has the caret changed position?
272  int caretPos = m_te->GetCurrentPos();
273 
274  if( m_lastCaretPos != caretPos )
275  {
276  m_lastCaretPos = caretPos;
277  int bracePos1 = -1;
278  int bracePos2 = -1;
279 
280  // Is there a brace to the left or right?
281  if( caretPos > 0 && isBrace( m_te->GetCharAt( caretPos-1 ) ) )
282  bracePos1 = ( caretPos - 1 );
283  else if( isBrace( m_te->GetCharAt( caretPos ) ) )
284  bracePos1 = caretPos;
285 
286  if( bracePos1 >= 0 )
287  {
288  // Find the matching brace
289  bracePos2 = m_te->BraceMatch( bracePos1 );
290 
291  if( bracePos2 == -1 )
292  {
293  m_te->BraceBadLight( bracePos1 );
294  m_te->SetHighlightGuide( 0 );
295  }
296  else
297  {
298  m_te->BraceHighlight( bracePos1, bracePos2 );
299  m_te->SetHighlightGuide( m_te->GetColumn( bracePos1 ) );
300  }
301  }
302  else
303  {
304  // Turn off brace matching
305  m_te->BraceHighlight( -1, -1 );
306  m_te->SetHighlightGuide( 0 );
307  }
308  }
309 }
310 
311 
312 void SCINTILLA_TRICKS::DoAutocomplete( const wxString& aPartial, const wxArrayString& aTokens )
313 {
315  return;
316 
317  wxArrayString matchedTokens;
318 
319  wxString filter = wxT( "*" ) + aPartial.Lower() + wxT( "*" );
320 
321  for( const wxString& token : aTokens )
322  {
323  if( token.Lower().Matches( filter ) )
324  matchedTokens.push_back( token );
325  }
326 
327  if( matchedTokens.size() > 0 )
328  {
329  // NB: tokens MUST be in alphabetical order because the Scintilla engine is going
330  // to do a binary search on them
331  matchedTokens.Sort( []( const wxString& first, const wxString& second ) -> int
332  {
333  return first.CmpNoCase( second );
334  });
335 
336  m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens, ' ' ) );
337  }
338 }
339 
340 
void onCharHook(wxKeyEvent &aEvent)
bool isCtrlSlash(wxKeyEvent &aEvent)
This file is part of the common library.
int firstNonWhitespace(int aLine, int *aWhitespaceCount=nullptr)
void DoAutocomplete(const wxString &aPartial, const wxArrayString &aTokens)
SCINTILLA_TRICKS(wxStyledTextCtrl *aScintilla, const wxString &aBraces)
wxStyledTextCtrl * m_te
void onScintillaUpdateUI(wxStyledTextEvent &aEvent)
bool ConvertSmartQuotesAndDashes(wxString *aString)
Converts curly quotes and em/en dashes to straight quotes and dashes.
Definition: string.cpp:43