29#include <wx/stc/stc.h>
32#include <wx/clipbrd.h>
34#include <wx/settings.h>
39 std::function<
void( wxKeyEvent& )> onAcceptFn,
40 std::function<
void( wxStyledTextEvent& )> onCharAddedFn ) :
46 m_suppressAutocomplete( false ),
47 m_singleLine( aSingleLine ),
48 m_onAcceptFn(
std::
move( onAcceptFn ) ),
49 m_onCharAddedFn(
std::
move( onCharAddedFn ) )
52 m_te->SetEOLMode( wxSTC_EOL_LF );
56 m_te->SetScrollWidth( 1 );
57 m_te->SetScrollWidthTracking(
true );
61 m_te->SetUseVerticalScrollBar(
false );
62 m_te->SetUseHorizontalScrollBar(
false );
68 m_te->AutoCompSetIgnoreCase(
true );
69 m_te->AutoCompSetMaxHeight( 20 );
71 if( aBraces.Length() >= 2 )
85 m_te->Bind( wxEVT_SYS_COLOUR_CHANGED,
100 wxTextCtrl
dummy(
m_te->GetParent(), wxID_ANY );
103 KIGFX::COLOR4D highlight = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
104 KIGFX::COLOR4D highlightText = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
106 m_te->StyleSetForeground( wxSTC_STYLE_DEFAULT, foreground.
ToColour() );
107 m_te->StyleSetBackground( wxSTC_STYLE_DEFAULT, background.
ToColour() );
108 m_te->StyleClearAll();
112 highlight = highlight.
Mix( background, highlight.
a ).
WithAlpha( 1.0 );
113 highlightText = highlightText.
Mix( background, highlightText.
a ).
WithAlpha( 1.0 );
115 m_te->SetSelForeground(
true, highlightText.
ToColour() );
125 for(
size_t i = 0; i < wxSTC_STYLE_MAX; ++i )
126 m_te->StyleSetFont( i, fixedFont );
128 m_te->SetTabWidth( 4 );
136 m_te->StyleSetForeground( wxSTC_STYLE_BRACELIGHT, highlightText.
ToColour() );
137 m_te->StyleSetBackground( wxSTC_STYLE_BRACELIGHT, braceHighlight.
ToColour() );
138 m_te->StyleSetForeground( wxSTC_STYLE_BRACEBAD, *wxRED );
144 if( !aEvent.ControlDown() || aEvent.MetaDown() )
147 if( aEvent.GetUnicodeKey() ==
'/' )
165 if( aEvent.ShiftDown() && ( aEvent.GetUnicodeKey() ==
'7' || aEvent.GetUnicodeKey() ==
':' ) )
186 wxString curr_text =
m_te->GetText();
188 if( curr_text.Contains( wxS(
"\n" ) ) || curr_text.Contains( wxS(
"\r" ) ) )
195 int currpos =
m_te->GetCurrentPos();
197 text.Replace( wxS(
"\n" ), wxS(
"" ) );
198 text.Replace( wxS(
"\r" ), wxS(
"" ) );
200 m_te->GotoPos( currpos-1 );
209 wxString c = aEvent.GetUnicodeKey();
211 if(
m_te->AutoCompActive() )
213 if( aEvent.GetKeyCode() == WXK_ESCAPE )
215 m_te->AutoCompCancel();
218 else if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
220 int start =
m_te->AutoCompPosStart();
222 m_te->AutoCompComplete();
224 int finish =
m_te->GetCurrentPos();
230 int selStart =
m_te->FindText( finish, start,
"<" );
231 int selEnd =
m_te->FindText( finish, start,
">" );
233 if( selStart > start && selEnd <= finish && selEnd > selStart )
234 m_te->SetSelection( selStart, selEnd + 1 );
246 if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() == WXK_SPACE )
248 if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == WXK_SPACE )
253 wxStyledTextEvent event;
255 event.SetModifiers( wxMOD_CONTROL );
261 if( !isalpha( aEvent.GetKeyCode() ) )
264 if( ( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
273 else if( aEvent.GetKeyCode() == WXK_TAB )
275 wxWindow* ancestor =
m_te->GetParent();
277 while( ancestor && !
dynamic_cast<WX_GRID*
>( ancestor ) )
278 ancestor = ancestor->GetParent();
280 if( aEvent.ControlDown() )
284 if( !aEvent.ShiftDown() )
285 flags |= wxNavigationKeyEvent::IsForward;
288 dlg->NavigateIn( flags );
290 else if(
dynamic_cast<WX_GRID*
>( ancestor ) )
293 int row =
grid->GetGridCursorRow();
294 int col =
grid->GetGridCursorCol();
296 if( aEvent.ShiftDown() )
304 col = (int)
grid->GetNumberCols() - 1;
309 row = (int)
grid->GetNumberRows() - 1;
314 if( col < (
int)
grid->GetNumberCols() - 1 )
318 else if( row < grid->GetNumberRows() - 1 )
322 if( row < grid->GetNumberRows() - 1 )
329 grid->SetGridCursor( row, col );
336 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
340 else if( ( aEvent.GetModifiers() == wxMOD_SHIFT+wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Z' )
341 || ( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'Y' ) )
345 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'A' )
349 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'X' )
353 if( wxTheClipboard->Open() )
355 wxTheClipboard->Flush();
356 wxTheClipboard->Close();
359 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'C' )
363 if( wxTheClipboard->Open() )
365 wxTheClipboard->Flush();
366 wxTheClipboard->Close();
369 else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() ==
'V' )
371 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
376 if( wxTheClipboard->Open() )
378 if( wxTheClipboard->IsSupported( wxDF_TEXT ) ||
379 wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
381 wxTextDataObject data;
384 wxTheClipboard->GetData( data );
385 str = data.GetText();
391 str.Replace( wxS(
"\n" ), wxEmptyString );
392 str.Replace( wxS(
"\r" ), wxEmptyString );
395 m_te->BeginUndoAction();
396 m_te->AddText( str );
397 m_te->EndUndoAction();
400 wxTheClipboard->Close();
403 else if( aEvent.GetKeyCode() == WXK_BACK )
405 if( aEvent.GetModifiers() == wxMOD_CONTROL )
408 else if( aEvent.GetModifiers() == wxMOD_ALT )
410 m_te->WordLeftExtend();
414 else if( aEvent.GetKeyCode() == WXK_DELETE )
416 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
417 m_te->CharRightExtend();
419 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
424 int startLine =
m_te->LineFromPosition(
m_te->GetSelectionStart() );
425 int endLine =
m_te->LineFromPosition(
m_te->GetSelectionEnd() );
429 m_te->BeginUndoAction();
431 for(
int ii = startLine; ii <= endLine; ++ii )
434 m_te->InsertText(
m_te->PositionFromLine( ii ), wxT(
"#" ) );
436 m_te->DeleteRange(
m_te->PositionFromLine( ii ) + whitespaceCount, 1 );
439 m_te->SetSelection(
m_te->PositionFromLine( startLine ),
440 m_te->PositionFromLine( endLine ) +
m_te->GetLineLength( endLine ) );
442 m_te->EndUndoAction();
445 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'A' )
449 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'E' )
453 else if( ( aEvent.GetModifiers() & wxMOD_RAW_CONTROL ) && aEvent.GetKeyCode() ==
'B' )
455 if( aEvent.GetModifiers() & wxMOD_ALT )
460 else if( ( aEvent.GetModifiers() & wxMOD_RAW_CONTROL ) && aEvent.GetKeyCode() ==
'F' )
462 if( aEvent.GetModifiers() & wxMOD_ALT )
467 else if( aEvent.GetModifiers() == wxMOD_RAW_CONTROL && aEvent.GetKeyCode() ==
'D' )
469 if(
m_te->GetSelectionEnd() ==
m_te->GetSelectionStart() )
470 m_te->CharRightExtend();
472 if(
m_te->GetSelectionEnd() >
m_te->GetSelectionStart() )
476 else if( aEvent.GetKeyCode() == WXK_SPECIAL20 )
490 int lineStart =
m_te->PositionFromLine( aLine );
492 if( aWhitespaceCharCount )
493 *aWhitespaceCharCount = 0;
495 for(
int ii = 0; ii <
m_te->GetLineLength( aLine ); ++ii )
497 int c =
m_te->GetCharAt( lineStart + ii );
499 if( c ==
' ' || c ==
'\t' )
501 if( aWhitespaceCharCount )
502 *aWhitespaceCharCount += 1;
518 auto isBrace = [
this](
int c ) ->
bool
520 return m_braces.Find( (wxChar) c ) >= 0;
524 int caretPos =
m_te->GetCurrentPos();
525 int selStart =
m_te->GetSelectionStart();
526 int selEnd =
m_te->GetSelectionEnd();
537 if( caretPos > 0 && isBrace(
m_te->GetCharAt( caretPos-1 ) ) )
538 bracePos1 = ( caretPos - 1 );
539 else if( isBrace(
m_te->GetCharAt( caretPos ) ) )
540 bracePos1 = caretPos;
545 bracePos2 =
m_te->BraceMatch( bracePos1 );
547 if( bracePos2 == -1 )
549 m_te->BraceBadLight( bracePos1 );
550 m_te->SetHighlightGuide( 0 );
554 m_te->BraceHighlight( bracePos1, bracePos2 );
555 m_te->SetHighlightGuide(
m_te->GetColumn( bracePos1 ) );
561 m_te->BraceHighlight( -1, -1 );
562 m_te->SetHighlightGuide( 0 );
569 const std::function<
void(
const wxString& xRef, wxArrayString* tokens )>& getTokensFn )
571 wxArrayString autocompleteTokens;
572 int text_pos =
m_te->GetCurrentPos();
573 int start =
m_te->WordStartPosition( text_pos,
true );
579 return pos >= 2 &&
m_te->GetCharAt( pos-2 ) ==
'$' &&
m_te->GetCharAt( pos-1 ) ==
'{';
583 if( start > 1 &&
m_te->GetCharAt( start-1 ) ==
':' )
585 int refStart =
m_te->WordStartPosition( start-1,
true );
587 if( textVarRef( refStart ) )
589 partial =
m_te->GetRange( start, text_pos );
590 getTokensFn(
m_te->GetRange( refStart, start-1 ), &autocompleteTokens );
593 else if( textVarRef( start ) )
595 partial =
m_te->GetTextRange( start, text_pos );
596 getTokensFn( wxEmptyString, &autocompleteTokens );
609 wxArrayString matchedTokens;
611 wxString
filter = wxT(
"*" ) + aPartial.Lower() + wxT(
"*" );
613 for(
const wxString& token : aTokens )
615 if( token.Lower().Matches(
filter ) )
616 matchedTokens.push_back( token );
619 if( matchedTokens.size() > 0 )
623 matchedTokens.Sort( [](
const wxString& first,
const wxString& second ) ->
int
625 return first.CmpNoCase( second );
628 m_te->AutoCompSetSeparator(
'\t' );
629 m_te->AutoCompShow( aPartial.size(), wxJoin( matchedTokens,
'\t' ) );
636 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.
Functions to provide common constants and other functions to assist in making a consistent UI.