From a6eaa3257a2891cbabbf025e637da2326e5b5de4 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sat, 30 May 2020 10:22:19 +0100 Subject: [PATCH] Autocomplete for page layout editor text items. --- common/page_layout/ws_painter.cpp | 26 ++++++++++++ include/ws_draw_item.h | 2 + .../dialogs/properties_frame_base.cpp | 36 ++++++++++++++-- .../dialogs/properties_frame_base.fbp | 24 ++++++----- .../dialogs/properties_frame_base.h | 7 ++-- pagelayout_editor/properties_frame.cpp | 41 +++++++++++++++++-- pagelayout_editor/properties_frame.h | 6 ++- 7 files changed, 118 insertions(+), 24 deletions(-) diff --git a/common/page_layout/ws_painter.cpp b/common/page_layout/ws_painter.cpp index b9e1e679ea..7e0e30ba34 100644 --- a/common/page_layout/ws_painter.cpp +++ b/common/page_layout/ws_painter.cpp @@ -85,6 +85,32 @@ const COLOR4D& WS_RENDER_SETTINGS::GetColor( const VIEW_ITEM* aItem, int aLayer } +void WS_DRAW_ITEM_LIST::GetTextVars( wxArrayString* aVars ) +{ + aVars->push_back( wxT( "KICAD_VERSION" ) ); + aVars->push_back( wxT( "#" ) ); + aVars->push_back( wxT( "##" ) ); + aVars->push_back( wxT( "SHEETNAME" ) ); + aVars->push_back( wxT( "FILENAME" ) ); + aVars->push_back( wxT( "PAPER" ) ); + aVars->push_back( wxT( "LAYER" ) ); + aVars->push_back( wxT( "ISSUE_DATE" ) ); + aVars->push_back( wxT( "REVISION" ) ); + aVars->push_back( wxT( "TITLE" ) ); + aVars->push_back( wxT( "COMPANY" ) ); + aVars->push_back( wxT( "COMMENT0" ) ); + aVars->push_back( wxT( "COMMENT1" ) ); + aVars->push_back( wxT( "COMMENT2" ) ); + aVars->push_back( wxT( "COMMENT3" ) ); + aVars->push_back( wxT( "COMMENT4" ) ); + aVars->push_back( wxT( "COMMENT5" ) ); + aVars->push_back( wxT( "COMMENT6" ) ); + aVars->push_back( wxT( "COMMENT7" ) ); + aVars->push_back( wxT( "COMMENT8" ) ); + aVars->push_back( wxT( "COMMENT9" ) ); +} + + // returns the full text corresponding to the aTextbase, // after replacing format symbols by the corresponding value wxString WS_DRAW_ITEM_LIST::BuildFullText( const wxString& aTextbase ) diff --git a/include/ws_draw_item.h b/include/ws_draw_item.h index 2387684c4a..641fe320cc 100644 --- a/include/ws_draw_item.h +++ b/include/ws_draw_item.h @@ -513,6 +513,8 @@ public: */ void BuildWorkSheetGraphicList( const PAGE_INFO& aPageInfo, const TITLE_BLOCK& aTitleBlock ); + static void GetTextVars( wxArrayString* aVars ); + /** * Function BuildFullText * returns the full text corresponding to the aTextbase, diff --git a/pagelayout_editor/dialogs/properties_frame_base.cpp b/pagelayout_editor/dialogs/properties_frame_base.cpp index ef30345262..10ddbcdd6c 100644 --- a/pagelayout_editor/dialogs/properties_frame_base.cpp +++ b/pagelayout_editor/dialogs/properties_frame_base.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version Dec 1 2018) +// C++ code generated with wxFormBuilder (version Oct 26 2018) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! @@ -24,7 +24,7 @@ PANEL_PROPERTIES_BASE::PANEL_PROPERTIES_BASE( wxWindow* parent, wxWindowID id, c m_staticTextType = new wxStaticText( m_swItemProperties, wxID_ANY, _("Type"), wxDefaultPosition, wxDefaultSize, 0 ); m_staticTextType->Wrap( -1 ); - m_staticTextType->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_BOLD, false, wxEmptyString ) ); + m_staticTextType->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_SLANT, wxFONTWEIGHT_BOLD, false, wxEmptyString ) ); bSizerButt->Add( m_staticTextType, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 ); @@ -46,8 +46,36 @@ PANEL_PROPERTIES_BASE::PANEL_PROPERTIES_BASE( wxWindow* parent, wxWindowID id, c m_staticTextText->Wrap( -1 ); m_SizerTextOptions->Add( m_staticTextText, 0, wxTOP|wxRIGHT|wxLEFT, 5 ); - m_textCtrlText = new wxTextCtrl( m_swItemProperties, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); - m_SizerTextOptions->Add( m_textCtrlText, 1, wxEXPAND|wxRIGHT|wxLEFT, 5 ); + m_stcText = new wxStyledTextCtrl( m_swItemProperties, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, wxEmptyString ); + m_stcText->SetUseTabs( true ); + m_stcText->SetTabWidth( 4 ); + m_stcText->SetIndent( 4 ); + m_stcText->SetTabIndents( true ); + m_stcText->SetBackSpaceUnIndents( true ); + m_stcText->SetViewEOL( false ); + m_stcText->SetViewWhiteSpace( false ); + m_stcText->SetMarginWidth( 2, 0 ); + m_stcText->SetIndentationGuides( false ); + m_stcText->SetMarginWidth( 1, 0 ); + m_stcText->SetMarginWidth( 0, 0 ); + m_stcText->MarkerDefine( wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUS ); + m_stcText->MarkerSetBackground( wxSTC_MARKNUM_FOLDER, wxColour( wxT("BLACK") ) ); + m_stcText->MarkerSetForeground( wxSTC_MARKNUM_FOLDER, wxColour( wxT("WHITE") ) ); + m_stcText->MarkerDefine( wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUS ); + m_stcText->MarkerSetBackground( wxSTC_MARKNUM_FOLDEROPEN, wxColour( wxT("BLACK") ) ); + m_stcText->MarkerSetForeground( wxSTC_MARKNUM_FOLDEROPEN, wxColour( wxT("WHITE") ) ); + m_stcText->MarkerDefine( wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY ); + m_stcText->MarkerDefine( wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_BOXPLUS ); + m_stcText->MarkerSetBackground( wxSTC_MARKNUM_FOLDEREND, wxColour( wxT("BLACK") ) ); + m_stcText->MarkerSetForeground( wxSTC_MARKNUM_FOLDEREND, wxColour( wxT("WHITE") ) ); + m_stcText->MarkerDefine( wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_BOXMINUS ); + m_stcText->MarkerSetBackground( wxSTC_MARKNUM_FOLDEROPENMID, wxColour( wxT("BLACK") ) ); + m_stcText->MarkerSetForeground( wxSTC_MARKNUM_FOLDEROPENMID, wxColour( wxT("WHITE") ) ); + m_stcText->MarkerDefine( wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY ); + m_stcText->MarkerDefine( wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY ); + m_stcText->SetSelBackground( true, wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) ); + m_stcText->SetSelForeground( true, wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) ); + m_SizerTextOptions->Add( m_stcText, 1, wxEXPAND|wxRIGHT|wxLEFT, 5 ); wxBoxSizer* bSizerFontOpt; bSizerFontOpt = new wxBoxSizer( wxVERTICAL ); diff --git a/pagelayout_editor/dialogs/properties_frame_base.fbp b/pagelayout_editor/dialogs/properties_frame_base.fbp index 12b0cefb28..ed1608e8c0 100644 --- a/pagelayout_editor/dialogs/properties_frame_base.fbp +++ b/pagelayout_editor/dialogs/properties_frame_base.fbp @@ -438,11 +438,11 @@ -1 - + 5 wxEXPAND|wxRIGHT|wxLEFT 1 - + 1 1 1 @@ -451,6 +451,7 @@ + 1 @@ -466,19 +467,21 @@ 1 1 + 0 0 0 wxID_ANY + 0 + 0 0 - 0 0 1 - m_textCtrlText + m_stcText 1 @@ -488,15 +491,14 @@ Resizable 1 - wxTE_MULTILINE - + ; ; forward_declare + 1 + 4 0 - - wxFILTER_NONE - wxDefaultValidator - - + 1 + 0 + 0 diff --git a/pagelayout_editor/dialogs/properties_frame_base.h b/pagelayout_editor/dialogs/properties_frame_base.h index f8fb798f3e..a910b9ab5a 100644 --- a/pagelayout_editor/dialogs/properties_frame_base.h +++ b/pagelayout_editor/dialogs/properties_frame_base.h @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version Dec 1 2018) +// C++ code generated with wxFormBuilder (version Oct 26 2018) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! @@ -19,8 +19,9 @@ #include #include #include -#include +#include #include +#include #include #include #include @@ -48,7 +49,7 @@ class PANEL_PROPERTIES_BASE : public wxPanel wxStaticLine* m_staticline5; wxBoxSizer* m_SizerTextOptions; wxStaticText* m_staticTextText; - wxTextCtrl* m_textCtrlText; + wxStyledTextCtrl* m_stcText; wxStaticText* m_staticTextHjust; wxChoice* m_choiceHjustify; wxCheckBox* m_checkBoxBold; diff --git a/pagelayout_editor/properties_frame.cpp b/pagelayout_editor/properties_frame.cpp index 4b003a803b..6648120bf4 100644 --- a/pagelayout_editor/properties_frame.cpp +++ b/pagelayout_editor/properties_frame.cpp @@ -26,16 +26,23 @@ #include #include #include +#include #include #include #include #include +#include -PROPERTIES_FRAME::PROPERTIES_FRAME( PL_EDITOR_FRAME* aParent ): - PANEL_PROPERTIES_BASE( aParent ) +PROPERTIES_FRAME::PROPERTIES_FRAME( PL_EDITOR_FRAME* aParent ) : + PANEL_PROPERTIES_BASE( aParent ), + m_scintillaTricks( nullptr ) { m_parent = aParent; + m_stcText->SetUseVerticalScrollBar( false ); + m_stcText->SetUseHorizontalScrollBar( false ); + m_scintillaTricks = new SCINTILLA_TRICKS( m_stcText, wxT( "{}" ) ); + wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ); infoFont.SetSymbolicSize( wxFONTSIZE_SMALL ); m_staticTextSizeInfo->SetFont( infoFont ); @@ -43,11 +50,14 @@ PROPERTIES_FRAME::PROPERTIES_FRAME( PL_EDITOR_FRAME* aParent ): m_staticTextInfoThickness->SetFont( infoFont ); m_buttonOK->SetDefault(); + + m_stcText->Bind( wxEVT_STC_CHARADDED, &PROPERTIES_FRAME::onScintillaCharAdded, this ); } PROPERTIES_FRAME::~PROPERTIES_FRAME() { + delete m_scintillaTricks; } @@ -195,7 +205,7 @@ void PROPERTIES_FRAME::CopyPrmsFromItemToPanel( WS_DATA_ITEM* aItem ) item->m_FullText = item->m_TextBase; // Replace our '\' 'n' sequence by the EOL char item->ReplaceAntiSlashSequence(); - m_textCtrlText->SetValue( item->m_FullText ); + m_stcText->SetValue( item->m_FullText ); msg.Printf( wxT("%d"), item->m_IncrementLabel ); m_textCtrlTextIncrement->SetValue( msg ); @@ -402,7 +412,7 @@ bool PROPERTIES_FRAME::CopyPrmsFromPanelToItem( WS_DATA_ITEM* aItem ) { WS_DATA_ITEM_TEXT* item = (WS_DATA_ITEM_TEXT*) aItem; - item->m_TextBase = m_textCtrlText->GetValue(); + item->m_TextBase = m_stcText->GetValue(); msg = m_textCtrlTextIncrement->GetValue(); msg.ToLong( &itmp ); @@ -464,3 +474,26 @@ bool PROPERTIES_FRAME::CopyPrmsFromPanelToItem( WS_DATA_ITEM* aItem ) return true; } + +void PROPERTIES_FRAME::onScintillaCharAdded( wxStyledTextEvent &aEvent ) +{ + wxArrayString autocompleteTokens; + int pos = m_stcText->GetCurrentPos(); + int start = m_stcText->WordStartPosition( pos, true ); + wxString partial; + + if( start >= 2 + && m_stcText->GetCharAt( start-2 ) == '$' + && m_stcText->GetCharAt( start-1 ) == '{' ) + { + WS_DRAW_ITEM_LIST::GetTextVars( &autocompleteTokens ); + + partial = m_stcText->GetTextRange( start, pos ); + + for( std::pair entry : m_parent->Prj().GetTextVars() ) + autocompleteTokens.push_back( entry.first ); + } + + m_scintillaTricks->DoAutocomplete( partial, autocompleteTokens ); + m_stcText->SetFocus(); +} diff --git a/pagelayout_editor/properties_frame.h b/pagelayout_editor/properties_frame.h index f555a476f0..9e3dc5f826 100644 --- a/pagelayout_editor/properties_frame.h +++ b/pagelayout_editor/properties_frame.h @@ -31,6 +31,7 @@ #include #include +#include class WS_DATA_ITEM; @@ -40,7 +41,8 @@ class WS_DATA_ITEM; class PROPERTIES_FRAME : public PANEL_PROPERTIES_BASE { - PL_EDITOR_FRAME* m_parent; + PL_EDITOR_FRAME* m_parent; + SCINTILLA_TRICKS* m_scintillaTricks; public: PROPERTIES_FRAME( PL_EDITOR_FRAME* aParent ); @@ -50,7 +52,7 @@ public: void OnPageChanged( wxNotebookEvent& event ) override; void OnAcceptPrms( wxCommandEvent& event ) override; void OnSetDefaultValues( wxCommandEvent& event ) override; - + void onScintillaCharAdded( wxStyledTextEvent &aEvent ); // Data transfert from general properties to widgets void CopyPrmsFromGeneralToPanel();