27#include <wx/stc/stc.h>
30#include <wx/clipbrd.h>
32#include <wx/settings.h>
36 bool aSingleLine, std::function<
void()> onAcceptHandler,
37 std::function<
void( wxStyledTextEvent& )> onCharAddedHandler ) :
43 m_suppressAutocomplete( false ),
44 m_singleLine( aSingleLine ),
45 m_onAcceptHandler( onAcceptHandler ),
46 m_onCharAddedHandler( onCharAddedHandler )
49 m_te->SetEOLMode( wxSTC_EOL_LF );
53 m_te->SetScrollWidth( 1 );
54 m_te->SetScrollWidthTracking(
true );
59 m_te->AutoCompSetIgnoreCase(
true );
60 m_te->AutoCompSetMaxHeight( 20 );
62 if( aBraces.Length() >= 2 )
75 m_te->Bind( wxEVT_SYS_COLOUR_CHANGED,
90 wxTextCtrl
dummy(
m_te->GetParent(), wxID_ANY );
93 KIGFX::COLOR4D highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
94 KIGFX::COLOR4D highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
96 m_te->StyleSetForeground( wxSTC_STYLE_DEFAULT, foreground.
ToColour() );
97 m_te->StyleSetBackground( wxSTC_STYLE_DEFAULT, background.
ToColour() );
98 m_te->StyleClearAll();
102 highlight = highlight.
Mix( background, highlight.
a ).
WithAlpha( 1.0 );
103 highlightText = highlightText.
Mix( background, highlightText.
a ).
WithAlpha( 1.0 );
105 m_te->SetSelForeground(
true, highlightText.
ToColour() );
115 for(
size_t i = 0; i < wxSTC_STYLE_MAX; ++i )
116 m_te->StyleSetFont( i, fixedFont );
118 m_te->SetTabWidth( 4 );
126 m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText.
ToColour() );
127 m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, braceHighlight.
ToColour() );
128 m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
134 if( !aEvent.ControlDown() || aEvent.MetaDown() )
137 if( aEvent.GetUnicodeKey() ==
'/' )
155 if( aEvent.ShiftDown() && ( aEvent.GetUnicodeKey() ==
'7' || aEvent.GetUnicodeKey() ==
':' ) )
174 wxString c = aEvent.GetUnicodeKey();
176 if( !isalpha( aEvent.GetKeyCode() ) )
179 if( ( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
188 else if( aEvent.GetKeyCode() == WXK_TAB )
190 if( aEvent.ControlDown() )
194 if( !aEvent.ShiftDown() )
195 flags |= wxNavigationKeyEvent::IsForward;
197 wxWindow* parent =
m_te->GetParent();
199 while( parent &&
dynamic_cast<DIALOG_SHIM*
>( parent ) ==
nullptr )
200 parent = parent->GetParent();
203 parent->NavigateIn( flags );
210 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
214 else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
215 || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Y' ) )
219 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'A' )
223 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'X' )
227 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'C' )
231 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'V' )
233 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
238 if( wxTheClipboard->Open() )
240 if( wxTheClipboard->IsSupported( wxDF_TEXT ) ||
241 wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
243 wxTextDataObject data;
246 wxTheClipboard->GetData( data );
247 str = data.GetText();
250 m_te->BeginUndoAction();
251 m_te->AddText( str );
252 m_te->EndUndoAction();
255 wxTheClipboard->Close();
258 else if( aEvent.GetKeyCode() == WXK_BACK )
260 if( aEvent.GetModifiers() == wxMOD_CONTROL )
263 else if( aEvent.GetModifiers() == wxMOD_ALT )
265 m_te->WordLeftExtend();
269 else if( aEvent.GetKeyCode() == WXK_DELETE )
271 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
272 m_te->CharRightExtend();
274 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
277 else if( aEvent.GetKeyCode() == WXK_ESCAPE )
279 if(
m_te->AutoCompActive() )
281 m_te->AutoCompCancel();
291 int startLine =
m_te->LineFromPosition(
m_te->GetSelectionStart() );
292 int endLine =
m_te->LineFromPosition(
m_te->GetSelectionEnd() );
296 m_te->BeginUndoAction();
298 for(
int ii = startLine; ii <= endLine; ++ii )
301 m_te->InsertText(
m_te->PositionFromLine( ii ), wxT(
"#" ) );
303 m_te->DeleteRange(
m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
306 m_te->SetSelection(
m_te->PositionFromLine( startLine ),
307 m_te->PositionFromLine( endLine ) +
m_te->GetLineLength( endLine ) );
309 m_te->EndUndoAction();
312 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'A' )
316 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'E' )
321 else if( aEvent.GetKeyCode() == WXK_SPECIAL20 )
335 int lineStart =
m_te->PositionFromLine( aLine );
337 if( aWhitespaceCharCount )
338 *aWhitespaceCharCount = 0;
340 for(
int ii = 0; ii <
m_te->GetLineLength( aLine ); ++ii )
342 int c =
m_te->GetCharAt( lineStart + ii );
344 if( c ==
' ' || c ==
'\t' )
346 if( aWhitespaceCharCount )
347 *aWhitespaceCharCount += 1;
363 auto isBrace = [
this](
int c ) ->
bool
365 return m_braces.Find( (wxChar) c ) >= 0;
369 int caretPos =
m_te->GetCurrentPos();
370 int selStart =
m_te->GetSelectionStart();
371 int selEnd =
m_te->GetSelectionEnd();
382 if( caretPos > 0 && isBrace(
m_te->GetCharAt( caretPos-1 ) ) )
383 bracePos1 = ( caretPos - 1 );
384 else if( isBrace(
m_te->GetCharAt( caretPos ) ) )
385 bracePos1 = caretPos;
390 bracePos2 =
m_te->BraceMatch( bracePos1 );
392 if( bracePos2 == -1 )
394 m_te->BraceBadLight( bracePos1 );
395 m_te->SetHighlightGuide( 0 );
399 m_te->BraceHighlight( bracePos1, bracePos2 );
400 m_te->SetHighlightGuide(
m_te->GetColumn( bracePos1 ) );
406 m_te->BraceHighlight( -1, -1 );
407 m_te->SetHighlightGuide( 0 );
414 wxArrayString* tokens )> aTokenProvider )
416 wxArrayString autocompleteTokens;
417 int text_pos =
m_te->GetCurrentPos();
418 int start =
m_te->WordStartPosition( text_pos,
true );
424 return pos >= 2 &&
m_te->GetCharAt( pos-2 ) ==
'$' &&
m_te->GetCharAt( pos-1 ) ==
'{';
428 if( start > 1 &&
m_te->GetCharAt( start-1 ) ==
':' )
430 int refStart =
m_te->WordStartPosition( start-1,
true );
432 if( textVarRef( refStart ) )
434 partial =
m_te->GetRange( start, text_pos );
435 aTokenProvider(
m_te->GetRange( refStart, start-1 ), &autocompleteTokens );
438 else if( textVarRef( start ) )
440 partial =
m_te->GetTextRange( start, text_pos );
441 aTokenProvider( wxEmptyString, &autocompleteTokens );
454 wxArrayString matchedTokens;
456 wxString
filter = wxT(
"*" ) + aPartial.Lower() + wxT(
"*" );
458 for(
const wxString& token : aTokens )
460 if( token.Lower().Matches(
filter ) )
461 matchedTokens.push_back( token );
464 if( matchedTokens.size() > 0 )
468 matchedTokens.Sort( [](
const wxString& first,
const wxString& second ) ->
int
470 return first.CmpNoCase( second );
473 m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens,
m_te->AutoCompGetSeparator() ) );
480 m_te->AutoCompCancel();
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
A color representation with 4 components: red, green, blue, alpha.
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
wxColour ToColour() const
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
SCINTILLA_TRICKS(wxStyledTextCtrl *aScintilla, const wxString &aBraces, bool aSingleLine, std::function< void()> onAcceptHandler=[]() { }, std::function< void(wxStyledTextEvent &)> onCharAddedHandler=[](wxStyledTextEvent &) { })
void DoTextVarAutocomplete(std::function< void(const wxString &crossRef, wxArrayString *tokens)> aTokenProvider)
void onChar(wxStyledTextEvent &aEvent)
int firstNonWhitespace(int aLine, int *aWhitespaceCount=nullptr)
void onCharHook(wxKeyEvent &aEvent)
void onThemeChanged(wxSysColourChangedEvent &aEvent)
void DoAutocomplete(const wxString &aPartial, const wxArrayString &aTokens)
bool m_suppressAutocomplete
void CancelAutocomplete()
void onScintillaUpdateUI(wxStyledTextEvent &aEvent)
std::function< void()> m_onAcceptHandler
std::function< void(wxStyledTextEvent &aEvent)> m_onCharAddedHandler
This file is part of the common library.
wxFont GetMonospacedUIFont()
bool isCtrlSlash(wxKeyEvent &aEvent)
std::vector< FAB_LAYER_COLOR > dummy
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.