You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <richio.h>
  25. #include <common.h>
  26. #include <title_block.h>
  27. #include <core/kicad_algo.h>
  28. void TITLE_BLOCK::Format( OUTPUTFORMATTER* aFormatter ) const
  29. {
  30. // Don't write the title block information if there is nothing to write.
  31. bool isempty = true;
  32. for( unsigned idx = 0; idx < m_tbTexts.GetCount(); idx++ )
  33. {
  34. if( ! m_tbTexts[idx].IsEmpty() )
  35. {
  36. isempty = false;
  37. break;
  38. }
  39. }
  40. if( !isempty )
  41. {
  42. aFormatter->Print( "(title_block" );
  43. if( !GetTitle().IsEmpty() )
  44. aFormatter->Print( "(title %s)", aFormatter->Quotew( GetTitle() ).c_str() );
  45. if( !GetDate().IsEmpty() )
  46. aFormatter->Print( "(date %s)", aFormatter->Quotew( GetDate() ).c_str() );
  47. if( !GetRevision().IsEmpty() )
  48. aFormatter->Print( "(rev %s)", aFormatter->Quotew( GetRevision() ).c_str() );
  49. if( !GetCompany().IsEmpty() )
  50. aFormatter->Print( "(company %s)", aFormatter->Quotew( GetCompany() ).c_str() );
  51. for( int ii = 0; ii < 9; ii++ )
  52. {
  53. if( !GetComment(ii).IsEmpty() )
  54. {
  55. aFormatter->Print( "(comment %d %s)",
  56. ii+1,
  57. aFormatter->Quotew( GetComment(ii) ).c_str() );
  58. }
  59. }
  60. aFormatter->Print( ")" );
  61. }
  62. }
  63. void TITLE_BLOCK::GetContextualTextVars( wxArrayString* aVars )
  64. {
  65. if( !alg::contains( *aVars, wxT( "ISSUE_DATE" ) ) )
  66. {
  67. aVars->push_back( wxT( "ISSUE_DATE" ) );
  68. aVars->push_back( wxT( "CURRENT_DATE" ) );
  69. aVars->push_back( wxT( "REVISION" ) );
  70. aVars->push_back( wxT( "TITLE" ) );
  71. aVars->push_back( wxT( "COMPANY" ) );
  72. aVars->push_back( wxT( "COMMENT1" ) );
  73. aVars->push_back( wxT( "COMMENT2" ) );
  74. aVars->push_back( wxT( "COMMENT3" ) );
  75. aVars->push_back( wxT( "COMMENT4" ) );
  76. aVars->push_back( wxT( "COMMENT5" ) );
  77. aVars->push_back( wxT( "COMMENT6" ) );
  78. aVars->push_back( wxT( "COMMENT7" ) );
  79. aVars->push_back( wxT( "COMMENT8" ) );
  80. aVars->push_back( wxT( "COMMENT9" ) );
  81. }
  82. }
  83. wxString TITLE_BLOCK::GetCurrentDate()
  84. {
  85. // We can choose different formats. Should probably be kept in sync with ISSUE_DATE
  86. // formatting in DIALOG_PAGES_SETTINGS.
  87. //
  88. // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_SHORT_DATE_FMT ) );
  89. // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_LONG_DATE_FMT ) );
  90. // return wxDateTime::Now().Format( wxT("%Y-%b-%d") );
  91. return wxDateTime::Now().FormatISODate();
  92. };
  93. bool TITLE_BLOCK::TextVarResolver( wxString* aToken, const PROJECT* aProject, int aFlags ) const
  94. {
  95. bool tokenUpdated = false;
  96. wxString originalToken = *aToken;
  97. if( aToken->IsSameAs( wxT( "ISSUE_DATE" ) ) )
  98. {
  99. *aToken = GetDate();
  100. tokenUpdated = true;
  101. }
  102. else if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
  103. {
  104. *aToken = GetCurrentDate();
  105. tokenUpdated = true;
  106. }
  107. else if( aToken->IsSameAs( wxT( "REVISION" ) ) )
  108. {
  109. *aToken = GetRevision();
  110. tokenUpdated = true;
  111. }
  112. else if( aToken->IsSameAs( wxT( "TITLE" ) ) )
  113. {
  114. *aToken = GetTitle();
  115. tokenUpdated = true;
  116. }
  117. else if( aToken->IsSameAs( wxT( "COMPANY" ) ) )
  118. {
  119. *aToken = GetCompany();
  120. tokenUpdated = true;
  121. }
  122. else if( aToken->Left( aToken->Len() - 1 ).IsSameAs( wxT( "COMMENT" ) ) )
  123. {
  124. wxChar c = aToken->Last();
  125. switch( c )
  126. {
  127. case '1':
  128. case '2':
  129. case '3':
  130. case '4':
  131. case '5':
  132. case '6':
  133. case '7':
  134. case '8':
  135. case '9':
  136. *aToken = GetComment( c - '1' );
  137. tokenUpdated = true;
  138. }
  139. }
  140. if( tokenUpdated )
  141. {
  142. if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
  143. *aToken = GetCurrentDate();
  144. else if( aProject )
  145. *aToken = ExpandTextVars( *aToken, aProject, aFlags );
  146. // This is the default fallback, so don't claim we resolved it
  147. if( *aToken == wxT( "${" ) + originalToken + wxT( "}" ) )
  148. return false;
  149. return true;
  150. }
  151. return false;
  152. }