28#include <wx/stc/stc.h>
31#include <wx/clipbrd.h>
33#include <wx/settings.h>
38 std::function<
void( wxKeyEvent& )> onAcceptFn,
39 std::function<
void( wxStyledTextEvent& )> onCharAddedFn ) :
45 m_suppressAutocomplete( false ),
46 m_singleLine( aSingleLine ),
47 m_onAcceptFn(
std::
move( onAcceptFn ) ),
48 m_onCharAddedFn(
std::
move( onCharAddedFn ) )
51 m_te->SetEOLMode( wxSTC_EOL_LF );
55 m_te->SetScrollWidth( 1 );
56 m_te->SetScrollWidthTracking(
true );
60 m_te->SetUseVerticalScrollBar(
false );
61 m_te->SetUseHorizontalScrollBar(
false );
67 m_te->AutoCompSetIgnoreCase(
true );
68 m_te->AutoCompSetMaxHeight( 20 );
70 if( aBraces.Length() >= 2 )
84 m_te->Bind( wxEVT_SYS_COLOUR_CHANGED,
99 wxTextCtrl
dummy(
m_te->GetParent(), wxID_ANY );
102 KIGFX::COLOR4D highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
103 KIGFX::COLOR4D highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
105 m_te->StyleSetForeground( wxSTC_STYLE_DEFAULT, foreground.
ToColour() );
106 m_te->StyleSetBackground( wxSTC_STYLE_DEFAULT, background.
ToColour() );
107 m_te->StyleClearAll();
111 highlight = highlight.
Mix( background, highlight.
a ).
WithAlpha( 1.0 );
112 highlightText = highlightText.
Mix( background, highlightText.
a ).
WithAlpha( 1.0 );
114 m_te->SetSelForeground(
true, highlightText.
ToColour() );
124 for(
size_t i = 0; i < wxSTC_STYLE_MAX; ++i )
125 m_te->StyleSetFont( i, fixedFont );
127 m_te->SetTabWidth( 4 );
135 m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText.
ToColour() );
136 m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, braceHighlight.
ToColour() );
137 m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
143 if( !aEvent.ControlDown() || aEvent.MetaDown() )
146 if( aEvent.GetUnicodeKey() ==
'/' )
164 if( aEvent.ShiftDown() && ( aEvent.GetUnicodeKey() ==
'7' || aEvent.GetUnicodeKey() ==
':' ) )
185 wxString curr_text =
m_te->GetText();
187 if( curr_text.Contains( wxS(
"\n" ) ) || curr_text.Contains( wxS(
"\r" ) ) )
194 int currpos =
m_te->GetCurrentPos();
196 text.Replace( wxS(
"\n" ), wxS(
"" ) );
197 text.Replace( wxS(
"\r" ), wxS(
"" ) );
199 m_te->GotoPos( currpos-1 );
208 wxString c = aEvent.GetUnicodeKey();
210 if(
m_te->AutoCompActive() )
212 if( aEvent.GetKeyCode() == WXK_ESCAPE )
214 m_te->AutoCompCancel();
217 else if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
219 m_te->AutoCompComplete();
230 if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() == WXK_SPACE )
232 if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == WXK_SPACE )
237 wxStyledTextEvent event;
239 event.SetModifiers( wxMOD_CONTROL );
245 if( !isalpha( aEvent.GetKeyCode() ) )
248 if( ( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
257 else if( aEvent.GetKeyCode() == WXK_TAB )
259 wxWindow* ancestor =
m_te->GetParent();
261 while( ancestor && !
dynamic_cast<WX_GRID*
>( ancestor ) )
262 ancestor = ancestor->GetParent();
264 if( aEvent.ControlDown() )
268 if( !aEvent.ShiftDown() )
269 flags |= wxNavigationKeyEvent::IsForward;
271 wxWindow* parent =
m_te->GetParent();
273 while( parent &&
dynamic_cast<DIALOG_SHIM*
>( parent ) ==
nullptr )
274 parent = parent->GetParent();
277 parent->NavigateIn( flags );
279 else if(
dynamic_cast<WX_GRID*
>( ancestor ) )
282 int row =
grid->GetGridCursorRow();
283 int col =
grid->GetGridCursorCol();
285 if( aEvent.ShiftDown() )
293 col = (int)
grid->GetNumberCols() - 1;
298 row = (int)
grid->GetNumberRows() - 1;
303 if( col < (
int)
grid->GetNumberCols() - 1 )
307 else if( row < grid->GetNumberRows() - 1 )
311 if( row < grid->GetNumberRows() - 1 )
318 grid->SetGridCursor( row, col );
325 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
329 else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
330 || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Y' ) )
334 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'A' )
338 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'X' )
342 if( wxTheClipboard->Open() )
344 wxTheClipboard->Flush();
345 wxTheClipboard->Close();
348 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'C' )
352 if( wxTheClipboard->Open() )
354 wxTheClipboard->Flush();
355 wxTheClipboard->Close();
358 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'V' )
360 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
365 if( wxTheClipboard->Open() )
367 if( wxTheClipboard->IsSupported( wxDF_TEXT ) ||
368 wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
370 wxTextDataObject data;
373 wxTheClipboard->GetData( data );
374 str = data.GetText();
380 str.Replace( wxS(
"\n" ), wxEmptyString );
381 str.Replace( wxS(
"\r" ), wxEmptyString );
384 m_te->BeginUndoAction();
385 m_te->AddText( str );
386 m_te->EndUndoAction();
389 wxTheClipboard->Close();
392 else if( aEvent.GetKeyCode() == WXK_BACK )
394 if( aEvent.GetModifiers() == wxMOD_CONTROL )
397 else if( aEvent.GetModifiers() == wxMOD_ALT )
399 m_te->WordLeftExtend();
403 else if( aEvent.GetKeyCode() == WXK_DELETE )
405 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
406 m_te->CharRightExtend();
408 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
413 int startLine =
m_te->LineFromPosition(
m_te->GetSelectionStart() );
414 int endLine =
m_te->LineFromPosition(
m_te->GetSelectionEnd() );
418 m_te->BeginUndoAction();
420 for(
int ii = startLine; ii <= endLine; ++ii )
423 m_te->InsertText(
m_te->PositionFromLine( ii ), wxT(
"#" ) );
425 m_te->DeleteRange(
m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
428 m_te->SetSelection(
m_te->PositionFromLine( startLine ),
429 m_te->PositionFromLine( endLine ) +
m_te->GetLineLength( endLine ) );
431 m_te->EndUndoAction();
434 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'A' )
438 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'E' )
442 else if( ( aEvent.GetModifiers() & wxMOD_RAW_CONTROL ) && aEvent.GetKeyCode() ==
'B' )
444 if( aEvent.GetModifiers() & wxMOD_ALT )
449 else if( ( aEvent.GetModifiers() & wxMOD_RAW_CONTROL ) && aEvent.GetKeyCode() ==
'F' )
451 if( aEvent.GetModifiers() & wxMOD_ALT )
456 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'D' )
458 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
459 m_te->CharRightExtend();
461 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
465 else if( aEvent.GetKeyCode() == WXK_SPECIAL20 )
479 int lineStart =
m_te->PositionFromLine( aLine );
481 if( aWhitespaceCharCount )
482 *aWhitespaceCharCount = 0;
484 for(
int ii = 0; ii <
m_te->GetLineLength( aLine ); ++ii )
486 int c =
m_te->GetCharAt( lineStart + ii );
488 if( c ==
' ' || c ==
'\t' )
490 if( aWhitespaceCharCount )
491 *aWhitespaceCharCount += 1;
507 auto isBrace = [
this](
int c ) ->
bool
509 return m_braces.Find( (wxChar) c ) >= 0;
513 int caretPos =
m_te->GetCurrentPos();
514 int selStart =
m_te->GetSelectionStart();
515 int selEnd =
m_te->GetSelectionEnd();
526 if( caretPos > 0 && isBrace(
m_te->GetCharAt( caretPos-1 ) ) )
527 bracePos1 = ( caretPos - 1 );
528 else if( isBrace(
m_te->GetCharAt( caretPos ) ) )
529 bracePos1 = caretPos;
534 bracePos2 =
m_te->BraceMatch( bracePos1 );
536 if( bracePos2 == -1 )
538 m_te->BraceBadLight( bracePos1 );
539 m_te->SetHighlightGuide( 0 );
543 m_te->BraceHighlight( bracePos1, bracePos2 );
544 m_te->SetHighlightGuide(
m_te->GetColumn( bracePos1 ) );
550 m_te->BraceHighlight( -1, -1 );
551 m_te->SetHighlightGuide( 0 );
558 const std::function<
void(
const wxString& xRef, wxArrayString* tokens )>& getTokensFn )
560 wxArrayString autocompleteTokens;
561 int text_pos =
m_te->GetCurrentPos();
562 int start =
m_te->WordStartPosition( text_pos,
true );
568 return pos >= 2 &&
m_te->GetCharAt( pos-2 ) ==
'$' &&
m_te->GetCharAt( pos-1 ) ==
'{';
572 if( start > 1 &&
m_te->GetCharAt( start-1 ) ==
':' )
574 int refStart =
m_te->WordStartPosition( start-1,
true );
576 if( textVarRef( refStart ) )
578 partial =
m_te->GetRange( start, text_pos );
579 getTokensFn(
m_te->GetRange( refStart, start-1 ), &autocompleteTokens );
582 else if( textVarRef( start ) )
584 partial =
m_te->GetTextRange( start, text_pos );
585 getTokensFn( wxEmptyString, &autocompleteTokens );
598 wxArrayString matchedTokens;
600 wxString
filter = wxT(
"*" ) + aPartial.Lower() + wxT(
"*" );
602 for(
const wxString& token : aTokens )
604 if( token.Lower().Matches(
filter ) )
605 matchedTokens.push_back( token );
608 if( matchedTokens.size() > 0 )
612 matchedTokens.Sort( [](
const wxString& first,
const wxString& second ) ->
int
614 return first.CmpNoCase( second );
617 m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens,
m_te->AutoCompGetSeparator() ) );
624 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 onChar(wxStyledTextEvent &aEvent)
int firstNonWhitespace(int aLine, int *aWhitespaceCount=nullptr)
virtual void onCharHook(wxKeyEvent &aEvent)
std::function< void(wxKeyEvent &aEvent)> m_onAcceptFn
void onThemeChanged(wxSysColourChangedEvent &aEvent)
void DoAutocomplete(const wxString &aPartial, const wxArrayString &aTokens)
bool m_suppressAutocomplete
void CancelAutocomplete()
void onScintillaUpdateUI(wxStyledTextEvent &aEvent)
void DoTextVarAutocomplete(const std::function< void(const wxString &xRef, wxArrayString *tokens)> &getTokensFn)
std::function< void(wxStyledTextEvent &aEvent)> m_onCharAddedFn
void onModified(wxStyledTextEvent &aEvent)
SCINTILLA_TRICKS(wxStyledTextCtrl *aScintilla, const wxString &aBraces, bool aSingleLine, std::function< void(wxKeyEvent &)> onAcceptHandler=[](wxKeyEvent &aEvent) { }, std::function< void(wxStyledTextEvent &)> onCharAddedHandler=[](wxStyledTextEvent &) { })
This file is part of the common library.
KICOMMON_API 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.