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.

125 lines
3.3 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. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <widgets/font_choice.h>
  20. #include <kiplatform/ui.h>
  21. #include <wx/fontenum.h>
  22. #include <font/fontconfig.h>
  23. #include <pgm_base.h>
  24. // The "official" name of the building Kicad stroke font (always existing)
  25. #include <font/kicad_font_name.h>
  26. FONT_CHOICE::FONT_CHOICE( wxWindow* aParent, int aId, wxPoint aPosition, wxSize aSize,
  27. int nChoices, wxString* aChoices, int aStyle ) :
  28. wxChoice( aParent, aId, aPosition, aSize, nChoices, aChoices, aStyle )
  29. {
  30. m_systemFontCount = wxChoice::GetCount();
  31. std::vector<std::string> fontNames;
  32. Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
  33. wxArrayString menuList;
  34. // The initial list of fonts has on top 1 or 2 options
  35. // only "KiCad Font" (KICAD_FONT_NAME)
  36. // "Default Font" and "KiCad Font" (KICAD_FONT_NAME)
  37. // "KiCad Font" is also a keyword, and cannot be translated.
  38. // So rebuilt the starting list
  39. wxChoice::Clear();
  40. if( m_systemFontCount > 1 )
  41. Append( _( "Default Font" ) );
  42. Append( KICAD_FONT_NAME );
  43. m_systemFontCount = wxChoice::GetCount();
  44. for( const std::string& name : fontNames )
  45. menuList.Add( wxString( name ) );
  46. menuList.Sort();
  47. Freeze();
  48. Append( menuList );
  49. KIPLATFORM::UI::LargeChoiceBoxHack( this );
  50. Thaw();
  51. m_notFound = wxS( " " ) + _( "<not found>" );
  52. }
  53. FONT_CHOICE::~FONT_CHOICE()
  54. {
  55. }
  56. void FONT_CHOICE::SetFontSelection( KIFONT::FONT* aFont, bool aSilentMode )
  57. {
  58. if( !aFont )
  59. {
  60. SetSelection( 0 );
  61. }
  62. else
  63. {
  64. bool result = SetStringSelection( aFont->GetName() );
  65. if( !result )
  66. {
  67. Append( aFont->GetName() + m_notFound );
  68. SetSelection( GetCount() - 1 );
  69. }
  70. }
  71. if( !aSilentMode )
  72. SendSelectionChangedEvent( wxEVT_CHOICE );
  73. }
  74. bool FONT_CHOICE::HaveFontSelection() const
  75. {
  76. int sel = GetSelection();
  77. if( sel < 0 )
  78. return false;
  79. if( GetString( sel ).EndsWith( m_notFound ) )
  80. return false;
  81. return true;
  82. }
  83. KIFONT::FONT* FONT_CHOICE::GetFontSelection( bool aBold, bool aItalic, bool aForDrawingSheet ) const
  84. {
  85. if( GetSelection() <= 0 )
  86. {
  87. return nullptr;
  88. }
  89. else if( GetSelection() == 1 && m_systemFontCount == 2 )
  90. {
  91. return KIFONT::FONT::GetFont( KICAD_FONT_NAME, aBold, aItalic );
  92. }
  93. else
  94. {
  95. return KIFONT::FONT::GetFont( GetStringSelection(), aBold, aItalic, nullptr,
  96. aForDrawingSheet );
  97. }
  98. }