27#include <wx/stc/stc.h>
30#include <wx/clipbrd.h>
32#include <wx/settings.h>
37 std::function<
void( wxKeyEvent& )> onAcceptHandler,
38 std::function<
void( wxStyledTextEvent& )> onCharAddedHandler ) :
44 m_suppressAutocomplete( false ),
45 m_singleLine( aSingleLine ),
46 m_onAcceptHandler( onAcceptHandler ),
47 m_onCharAddedHandler( onCharAddedHandler )
50 m_te->SetEOLMode( wxSTC_EOL_LF );
54 m_te->SetScrollWidth( 1 );
55 m_te->SetScrollWidthTracking(
true );
59 m_te->SetUseVerticalScrollBar(
false );
60 m_te->SetUseHorizontalScrollBar(
false );
66 m_te->AutoCompSetIgnoreCase(
true );
67 m_te->AutoCompSetMaxHeight( 20 );
69 if( aBraces.Length() >= 2 )
83 m_te->Bind( wxEVT_SYS_COLOUR_CHANGED,
98 wxTextCtrl
dummy(
m_te->GetParent(), wxID_ANY );
101 KIGFX::COLOR4D highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
102 KIGFX::COLOR4D highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
104 m_te->StyleSetForeground( wxSTC_STYLE_DEFAULT, foreground.
ToColour() );
105 m_te->StyleSetBackground( wxSTC_STYLE_DEFAULT, background.
ToColour() );
106 m_te->StyleClearAll();
110 highlight = highlight.
Mix( background, highlight.
a ).
WithAlpha( 1.0 );
111 highlightText = highlightText.
Mix( background, highlightText.
a ).
WithAlpha( 1.0 );
113 m_te->SetSelForeground(
true, highlightText.
ToColour() );
123 for(
size_t i = 0; i < wxSTC_STYLE_MAX; ++i )
124 m_te->StyleSetFont( i, fixedFont );
126 m_te->SetTabWidth( 4 );
134 m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText.
ToColour() );
135 m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, braceHighlight.
ToColour() );
136 m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
142 if( !aEvent.ControlDown() || aEvent.MetaDown() )
145 if( aEvent.GetUnicodeKey() ==
'/' )
163 if( aEvent.ShiftDown() && ( aEvent.GetUnicodeKey() ==
'7' || aEvent.GetUnicodeKey() ==
':' ) )
184 wxString curr_text =
m_te->GetText();
186 if( curr_text.Contains( wxS(
"\n" ) ) || curr_text.Contains( wxS(
"\r" ) ) )
193 int currpos =
m_te->GetCurrentPos();
195 text.Replace( wxS(
"\n" ), wxS(
"" ) );
196 text.Replace( wxS(
"\r" ), wxS(
"" ) );
198 m_te->GotoPos( currpos-1 );
207 wxString c = aEvent.GetUnicodeKey();
209 if(
m_te->AutoCompActive() )
211 if( aEvent.GetKeyCode() == WXK_ESCAPE )
213 m_te->AutoCompCancel();
216 else if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
218 m_te->AutoCompComplete();
228 if( !isalpha( aEvent.GetKeyCode() ) )
231 if( ( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
240 else if( aEvent.GetKeyCode() == WXK_TAB )
242 if( aEvent.ControlDown() )
246 if( !aEvent.ShiftDown() )
247 flags |= wxNavigationKeyEvent::IsForward;
249 wxWindow* parent =
m_te->GetParent();
251 while( parent &&
dynamic_cast<DIALOG_SHIM*
>( parent ) ==
nullptr )
252 parent = parent->GetParent();
255 parent->NavigateIn( flags );
262 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
266 else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
267 || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Y' ) )
271 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'A' )
275 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'X' )
279 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'C' )
283 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'V' )
285 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
290 if( wxTheClipboard->Open() )
292 if( wxTheClipboard->IsSupported( wxDF_TEXT ) ||
293 wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
295 wxTextDataObject data;
298 wxTheClipboard->GetData( data );
299 str = data.GetText();
305 str.Replace( wxS(
"\n" ), wxEmptyString );
306 str.Replace( wxS(
"\r" ), wxEmptyString );
309 m_te->BeginUndoAction();
310 m_te->AddText( str );
311 m_te->EndUndoAction();
314 wxTheClipboard->Close();
317 else if( aEvent.GetKeyCode() == WXK_BACK )
319 if( aEvent.GetModifiers() == wxMOD_CONTROL )
322 else if( aEvent.GetModifiers() == wxMOD_ALT )
324 m_te->WordLeftExtend();
328 else if( aEvent.GetKeyCode() == WXK_DELETE )
330 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
331 m_te->CharRightExtend();
333 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
338 int startLine =
m_te->LineFromPosition(
m_te->GetSelectionStart() );
339 int endLine =
m_te->LineFromPosition(
m_te->GetSelectionEnd() );
343 m_te->BeginUndoAction();
345 for(
int ii = startLine; ii <= endLine; ++ii )
348 m_te->InsertText(
m_te->PositionFromLine( ii ), wxT(
"#" ) );
350 m_te->DeleteRange(
m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
353 m_te->SetSelection(
m_te->PositionFromLine( startLine ),
354 m_te->PositionFromLine( endLine ) +
m_te->GetLineLength( endLine ) );
356 m_te->EndUndoAction();
359 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'A' )
363 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'E' )
367 else if( ( aEvent.GetModifiers() & wxMOD_RAW_CONTROL ) && aEvent.GetKeyCode() ==
'B' )
369 if( aEvent.GetModifiers() & wxMOD_ALT )
374 else if( ( aEvent.GetModifiers() & wxMOD_RAW_CONTROL ) && aEvent.GetKeyCode() ==
'F' )
376 if( aEvent.GetModifiers() & wxMOD_ALT )
381 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'D' )
383 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
384 m_te->CharRightExtend();
386 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
390 else if( aEvent.GetKeyCode() == WXK_SPECIAL20 )
404 int lineStart =
m_te->PositionFromLine( aLine );
406 if( aWhitespaceCharCount )
407 *aWhitespaceCharCount = 0;
409 for(
int ii = 0; ii <
m_te->GetLineLength( aLine ); ++ii )
411 int c =
m_te->GetCharAt( lineStart + ii );
413 if( c ==
' ' || c ==
'\t' )
415 if( aWhitespaceCharCount )
416 *aWhitespaceCharCount += 1;
432 auto isBrace = [
this](
int c ) ->
bool
434 return m_braces.Find( (wxChar) c ) >= 0;
438 int caretPos =
m_te->GetCurrentPos();
439 int selStart =
m_te->GetSelectionStart();
440 int selEnd =
m_te->GetSelectionEnd();
451 if( caretPos > 0 && isBrace(
m_te->GetCharAt( caretPos-1 ) ) )
452 bracePos1 = ( caretPos - 1 );
453 else if( isBrace(
m_te->GetCharAt( caretPos ) ) )
454 bracePos1 = caretPos;
459 bracePos2 =
m_te->BraceMatch( bracePos1 );
461 if( bracePos2 == -1 )
463 m_te->BraceBadLight( bracePos1 );
464 m_te->SetHighlightGuide( 0 );
468 m_te->BraceHighlight( bracePos1, bracePos2 );
469 m_te->SetHighlightGuide(
m_te->GetColumn( bracePos1 ) );
475 m_te->BraceHighlight( -1, -1 );
476 m_te->SetHighlightGuide( 0 );
483 wxArrayString* tokens )> aTokenProvider )
485 wxArrayString autocompleteTokens;
486 int text_pos =
m_te->GetCurrentPos();
487 int start =
m_te->WordStartPosition( text_pos,
true );
493 return pos >= 2 &&
m_te->GetCharAt( pos-2 ) ==
'$' &&
m_te->GetCharAt( pos-1 ) ==
'{';
497 if( start > 1 &&
m_te->GetCharAt( start-1 ) ==
':' )
499 int refStart =
m_te->WordStartPosition( start-1,
true );
501 if( textVarRef( refStart ) )
503 partial =
m_te->GetRange( start, text_pos );
504 aTokenProvider(
m_te->GetRange( refStart, start-1 ), &autocompleteTokens );
507 else if( textVarRef( start ) )
509 partial =
m_te->GetTextRange( start, text_pos );
510 aTokenProvider( wxEmptyString, &autocompleteTokens );
523 wxArrayString matchedTokens;
525 wxString
filter = wxT(
"*" ) + aPartial.Lower() + wxT(
"*" );
527 for(
const wxString& token : aTokens )
529 if( token.Lower().Matches(
filter ) )
530 matchedTokens.push_back( token );
533 if( matchedTokens.size() > 0 )
537 matchedTokens.Sort( [](
const wxString& first,
const wxString& second ) ->
int
539 return first.CmpNoCase( second );
542 m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens,
m_te->AutoCompGetSeparator() ) );
549 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.
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)
void onModified(wxStyledTextEvent &aEvent)
std::function< void(wxKeyEvent &aEvent)> m_onAcceptHandler
SCINTILLA_TRICKS(wxStyledTextCtrl *aScintilla, const wxString &aBraces, bool aSingleLine, std::function< void(wxKeyEvent &)> onAcceptHandler=[](wxKeyEvent &aEvent) { }, std::function< void(wxStyledTextEvent &)> onCharAddedHandler=[](wxStyledTextEvent &) { })
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.