diff --git a/pcbnew/class_module.h b/pcbnew/class_module.h index 9345b7ce4c..27b87ab889 100644 --- a/pcbnew/class_module.h +++ b/pcbnew/class_module.h @@ -382,7 +382,7 @@ public: * Function GetValue * @return const wxString& - the value text. */ - const wxString& GetValue() + const wxString& GetValue() const { return m_Value->GetText(); } diff --git a/pcbnew/class_text_mod.cpp b/pcbnew/class_text_mod.cpp index 957b25f321..8eb13c4843 100644 --- a/pcbnew/class_text_mod.cpp +++ b/pcbnew/class_text_mod.cpp @@ -492,3 +492,64 @@ void TEXTE_MODULE::ViewGetLayers( int aLayers[], int& aCount ) const aCount = 1; } +/** + * Macro-expansion for text in library modules + */ +wxString TEXTE_MODULE::GetShownText() const +{ + /* First order optimization: no % means that no processing is + * needed; just hope that RVO and copy constructor implementation + * avoid to copy the whole block; anyway it should be better than + * rebuild the string one character at a time... + * Also it seems wise to only expand macros in user text (but there + * is no technical reason, probably) */ + + if( (m_Type != TEXT_is_DIVERS) || (wxString::npos == m_Text.find('%')) ) + return m_Text; + wxString newbuf; + + + const MODULE *module = static_cast( GetParent() ); + + for( wxString::const_iterator it = m_Text.begin(); + it != m_Text.end(); ++it ) + { + // Process '%' and copy everything else + if( *it != '%' ) + newbuf.append(*it); + else + { + /* Look at the next character (if is it there) and append + * its expansion */ + ++it; + if( it != m_Text.end() ) + { + switch( char(*it) ) + { + case '%': + newbuf.append( '%' ); + break; + + case 'R': + if( module ) + newbuf.append( module->GetReference() ); + break; + + case 'V': + if( module ) + newbuf.append( module->GetValue() ); + break; + + default: + newbuf.append( '?' ); + break; + } + } + else + break; // The string is over and we can't ++ anymore + } + } + return newbuf; +} + + diff --git a/pcbnew/class_text_mod.h b/pcbnew/class_text_mod.h index 6fb6e10af0..4ade64b213 100644 --- a/pcbnew/class_text_mod.h +++ b/pcbnew/class_text_mod.h @@ -173,6 +173,8 @@ public: EDA_ITEM* Clone() const; + virtual wxString GetShownText() const; + /// @copydoc VIEW_ITEM::ViewBBox() virtual const BOX2I ViewBBox() const;