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 <fctsys.h>
26 #include <scintilla_tricks.h>
27 #include <wx/stc/stc.h>
28 #include <gal/color4d.h>
29 #include <dialog_shim.h>
30 
31 SCINTILLA_TRICKS::SCINTILLA_TRICKS( wxStyledTextCtrl* aScintilla, const wxString& aBraces ) :
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 }
65 
66 
67 void SCINTILLA_TRICKS::onCharHook( wxKeyEvent& aEvent )
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 }
128 
129 
130 void SCINTILLA_TRICKS::onScintillaUpdateUI( wxStyledTextEvent& aEvent )
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 }
176 
177 
178 void SCINTILLA_TRICKS::DoAutocomplete( const wxString& aPartial, const wxArrayString& aTokens )
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 }
202 
203 
void onCharHook(wxKeyEvent &aEvent)
void DoAutocomplete(const wxString &aPartial, const wxArrayString &aTokens)
SCINTILLA_TRICKS(wxStyledTextCtrl *aScintilla, const wxString &aBraces)
wxStyledTextCtrl * m_te
void onScintillaUpdateUI(wxStyledTextEvent &aEvent)
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99