27 #include <wx/stc/stc.h> 30 #include <wx/clipbrd.h> 32 #include <wx/settings.h> 36 bool aSingleLine, std::function<
void()> aReturnCallback ) :
42 m_suppressAutocomplete( false ),
43 m_singleLine( aSingleLine ),
44 m_returnCallback( aReturnCallback )
47 m_te->SetEOLMode( wxSTC_EOL_LF );
51 m_te->SetScrollWidth( 1 );
52 m_te->SetScrollWidthTracking(
true );
57 m_te->AutoCompSetIgnoreCase(
true );
59 m_te->AutoCompSetMaxHeight( 20 );
67 m_te->Bind( wxEVT_SYS_COLOUR_CHANGED,
82 wxTextCtrl
dummy(
m_te->GetParent(), wxID_ANY );
83 wxColour foreground =
dummy.GetForegroundColour();
84 wxColour background =
dummy.GetBackgroundColour();
85 wxColour highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
86 wxColour highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
88 m_te->StyleSetForeground( wxSTC_STYLE_DEFAULT, foreground );
89 m_te->StyleSetBackground( wxSTC_STYLE_DEFAULT, background );
90 m_te->StyleClearAll();
92 m_te->SetSelForeground(
true, highlightText );
93 m_te->SetSelBackground(
true, highlight );
94 m_te->SetCaretForeground( foreground );
102 for(
size_t i = 0; i < wxSTC_STYLE_MAX; ++i )
103 m_te->StyleSetFont( i, fixedFont );
105 m_te->SetTabWidth( 4 );
109 unsigned char r = highlight.Red();
110 unsigned char g = highlight.Green();
111 unsigned char b = highlight.Blue();
112 wxColour::MakeGrey( &
r, &g, &b );
113 highlight.Set(
r, g, b );
114 m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText );
115 m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, highlight );
116 m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
122 if( !aEvent.ControlDown() || aEvent.MetaDown() )
125 if( aEvent.GetUnicodeKey() ==
'/' )
143 if( aEvent.ShiftDown() && ( aEvent.GetUnicodeKey() ==
'7' || aEvent.GetUnicodeKey() ==
':' ) )
156 wxString c = aEvent.GetUnicodeKey();
158 if( !isalpha( aEvent.GetKeyCode() ) )
161 if( aEvent.GetKeyCode() == WXK_RETURN && (
m_singleLine || aEvent.ShiftDown() ) )
169 else if( aEvent.GetKeyCode() == WXK_TAB )
171 if( aEvent.ControlDown() )
175 if( !aEvent.ShiftDown() )
176 flags |= wxNavigationKeyEvent::IsForward;
178 wxWindow* parent =
m_te->GetParent();
180 while( parent && dynamic_cast<DIALOG_SHIM*>( parent ) ==
nullptr )
181 parent = parent->GetParent();
184 parent->NavigateIn( flags );
191 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
195 else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
196 || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Y' ) )
200 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'A' )
204 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'X' )
208 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'C' )
212 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'V' )
214 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
219 if( wxTheClipboard->Open() )
221 if( wxTheClipboard->IsSupported( wxDF_TEXT ) ||
222 wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
224 wxTextDataObject data;
227 wxTheClipboard->GetData( data );
228 str = data.GetText();
231 m_te->AddText( str );
234 wxTheClipboard->Close();
237 else if( aEvent.GetKeyCode() == WXK_BACK )
241 else if( aEvent.GetKeyCode() == WXK_DELETE )
243 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
244 m_te->CharRightExtend();
246 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
249 else if( aEvent.GetKeyCode() == WXK_ESCAPE )
251 if(
m_te->AutoCompActive() )
253 m_te->AutoCompCancel();
263 int startLine =
m_te->LineFromPosition(
m_te->GetSelectionStart() );
264 int endLine =
m_te->LineFromPosition(
m_te->GetSelectionEnd() );
268 m_te->BeginUndoAction();
270 for(
int ii = startLine; ii <= endLine; ++ii )
273 m_te->InsertText(
m_te->PositionFromLine( ii ), wxT(
"#" ) );
275 m_te->DeleteRange(
m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
278 m_te->SetSelection(
m_te->PositionFromLine( startLine ),
279 m_te->PositionFromLine( endLine ) +
m_te->GetLineLength( endLine ) );
281 m_te->EndUndoAction();
284 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'A' )
288 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'E' )
293 else if( aEvent.GetKeyCode() == WXK_SPECIAL20 )
307 int lineStart =
m_te->PositionFromLine( aLine );
309 if( aWhitespaceCharCount )
310 *aWhitespaceCharCount = 0;
312 for(
int ii = 0; ii <
m_te->GetLineLength( aLine ); ++ii )
314 int c =
m_te->GetCharAt( lineStart + ii );
316 if( c ==
' ' || c ==
'\t' )
318 if( aWhitespaceCharCount )
319 *aWhitespaceCharCount += 1;
335 auto isBrace = [
this](
int c ) ->
bool 337 return m_braces.Find( (wxChar) c ) >= 0;
341 int caretPos =
m_te->GetCurrentPos();
342 int selStart =
m_te->GetSelectionStart();
343 int selEnd =
m_te->GetSelectionEnd();
354 if( caretPos > 0 && isBrace(
m_te->GetCharAt( caretPos-1 ) ) )
355 bracePos1 = ( caretPos - 1 );
356 else if( isBrace(
m_te->GetCharAt( caretPos ) ) )
357 bracePos1 = caretPos;
362 bracePos2 =
m_te->BraceMatch( bracePos1 );
364 if( bracePos2 == -1 )
366 m_te->BraceBadLight( bracePos1 );
367 m_te->SetHighlightGuide( 0 );
371 m_te->BraceHighlight( bracePos1, bracePos2 );
372 m_te->SetHighlightGuide(
m_te->GetColumn( bracePos1 ) );
378 m_te->BraceHighlight( -1, -1 );
379 m_te->SetHighlightGuide( 0 );
390 wxArrayString matchedTokens;
392 wxString
filter = wxT(
"*" ) + aPartial.Lower() + wxT(
"*" );
394 for(
const wxString& token : aTokens )
396 if( token.Lower().Matches(
filter ) )
397 matchedTokens.push_back( token );
400 if( matchedTokens.size() > 0 )
404 matchedTokens.Sort( [](
const wxString& first,
const wxString& second ) ->
int 406 return first.CmpNoCase( second );
409 m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens,
m_te->AutoCompGetSeparator() ) );
416 m_te->AutoCompCancel();
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)
wxFont GetMonospacedUIFont()
bool m_suppressAutocomplete
void CancelAutocomplete()
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
std::function< void()> m_returnCallback
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
SCINTILLA_TRICKS(wxStyledTextCtrl *aScintilla, const wxString &aBraces, bool aSingleLine, std::function< void()> m_enterCallback=[](){ })
void onThemeChanged(wxSysColourChangedEvent &aEvent)
void onScintillaUpdateUI(wxStyledTextEvent &aEvent)