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.

124 lines
4.7KB

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