The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

123 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCER_FONTPROPERTYCOMPONENT_H_INCLUDED
  18. #define JUCER_FONTPROPERTYCOMPONENT_H_INCLUDED
  19. class FontPropertyComponent : public ChoicePropertyComponent
  20. {
  21. public:
  22. FontPropertyComponent (const String& name)
  23. : ChoicePropertyComponent (name)
  24. {
  25. choices.add (getDefaultFont());
  26. choices.add (getDefaultSans());
  27. choices.add (getDefaultSerif());
  28. choices.add (getDefaultMono());
  29. choices.add (String());
  30. static StringArray fontNames;
  31. if (fontNames.size() == 0)
  32. {
  33. Array<Font> fonts;
  34. Font::findFonts (fonts);
  35. for (int i = 0; i < fonts.size(); ++i)
  36. fontNames.add (fonts[i].getTypefaceName());
  37. }
  38. choices.addArray (fontNames);
  39. }
  40. static String getDefaultFont() { return "Default font"; }
  41. static String getDefaultSans() { return "Default sans-serif font"; }
  42. static String getDefaultSerif() { return "Default serif font"; }
  43. static String getDefaultMono() { return "Default monospaced font"; }
  44. //==============================================================================
  45. virtual void setTypefaceName (const String& newFontName) = 0;
  46. virtual String getTypefaceName() const = 0;
  47. //==============================================================================
  48. void setIndex (int newIndex)
  49. {
  50. String type (choices [newIndex]);
  51. if (type.isEmpty())
  52. type = getDefaultFont();
  53. if (getTypefaceName() != type)
  54. setTypefaceName (type);
  55. }
  56. int getIndex() const
  57. {
  58. return choices.indexOf (getTypefaceName());
  59. }
  60. static Font applyNameToFont (const String& typefaceName, const Font& font)
  61. {
  62. if (typefaceName == getDefaultFont()) return Font (font.getHeight(), font.getStyleFlags());
  63. if (typefaceName == getDefaultSans()) return Font (Font::getDefaultSansSerifFontName(), font.getHeight(), font.getStyleFlags());
  64. if (typefaceName == getDefaultSerif()) return Font (Font::getDefaultSerifFontName(), font.getHeight(), font.getStyleFlags());
  65. if (typefaceName == getDefaultMono()) return Font (Font::getDefaultMonospacedFontName(), font.getHeight(), font.getStyleFlags());
  66. return Font (typefaceName, font.getHeight(), font.getStyleFlags());
  67. }
  68. static String getTypefaceNameCode (const String& typefaceName)
  69. {
  70. if (typefaceName == getDefaultFont()) return String();
  71. if (typefaceName == getDefaultSans()) return "Font::getDefaultSansSerifFontName(), ";
  72. if (typefaceName == getDefaultSerif()) return "Font::getDefaultSerifFontName(), ";
  73. if (typefaceName == getDefaultMono()) return "Font::getDefaultMonospacedFontName(), ";
  74. return "\"" + typefaceName + "\", ";
  75. }
  76. static String getFontStyleCode (const Font& font)
  77. {
  78. if (font.isBold() && font.isItalic()) return "Font::bold | Font::italic";
  79. if (font.isBold()) return "Font::bold";
  80. if (font.isItalic()) return "Font::italic";
  81. return "Font::plain";
  82. }
  83. static String getCompleteFontCode (const Font& font, const String& typefaceName)
  84. {
  85. return "Font ("
  86. + getTypefaceNameCode (typefaceName)
  87. + CodeHelpers::floatLiteral (font.getHeight(), 2)
  88. + ", "
  89. + getFontStyleCode (font)
  90. + ")";
  91. }
  92. };
  93. #endif // JUCER_FONTPROPERTYCOMPONENT_H_INCLUDED