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.

258 lines
9.8KB

  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. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. class FontsDemo : public Component,
  20. private ListBoxModel,
  21. private Slider::Listener,
  22. private Button::Listener,
  23. private ComboBox::Listener
  24. {
  25. public:
  26. FontsDemo()
  27. : heightLabel (String(), "Height:"),
  28. kerningLabel (String(), "Kerning:"),
  29. scaleLabel (String(), "Scale:"),
  30. styleLabel ("Style"),
  31. boldToggle ("Bold"),
  32. italicToggle ("Italic")
  33. {
  34. setOpaque (true);
  35. addAndMakeVisible (listBox);
  36. addAndMakeVisible (demoTextBox);
  37. addAndMakeVisible (heightSlider);
  38. addAndMakeVisible (heightLabel);
  39. addAndMakeVisible (kerningLabel);
  40. addAndMakeVisible (kerningSlider);
  41. addAndMakeVisible (scaleLabel);
  42. addAndMakeVisible (scaleSlider);
  43. addAndMakeVisible (boldToggle);
  44. addAndMakeVisible (italicToggle);
  45. addAndMakeVisible (styleBox);
  46. kerningLabel.attachToComponent (&kerningSlider, true);
  47. heightLabel.attachToComponent (&heightSlider, true);
  48. scaleLabel.attachToComponent (&scaleSlider, true);
  49. styleLabel.attachToComponent (&styleBox, true);
  50. heightSlider.addListener (this);
  51. kerningSlider.addListener (this);
  52. scaleSlider.addListener (this);
  53. boldToggle.addListener (this);
  54. italicToggle.addListener (this);
  55. styleBox.addListener (this);
  56. Font::findFonts (fonts); // Generate the list of fonts
  57. listBox.setRowHeight (20);
  58. listBox.setModel (this); // Tell the listbox where to get its data model
  59. heightSlider.setRange (3.0, 150.0, 0.01);
  60. scaleSlider.setRange (0.2, 3.0, 0.01);
  61. kerningSlider.setRange (-2.0, 2.0, 0.01);
  62. scaleSlider.setValue (1.0); // Set some initial values for the sliders.
  63. heightSlider.setValue (20.0);
  64. kerningSlider.setValue (0);
  65. // set up the layout and resizer bars..
  66. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.35); // width of the font list must be
  67. // between 20% and 80%, preferably 50%
  68. verticalLayout.setItemLayout (1, 8, 8, 8); // the vertical divider drag-bar thing is always 8 pixels wide
  69. verticalLayout.setItemLayout (2, 150, -1.0, -0.65); // the components on the right must be
  70. // at least 150 pixels wide, preferably 50% of the total width
  71. verticalDividerBar = new StretchableLayoutResizerBar (&verticalLayout, 1, true);
  72. addAndMakeVisible (verticalDividerBar);
  73. // ..and pick a random font to select intially
  74. listBox.selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  75. demoTextBox.setMultiLine (true);
  76. demoTextBox.setReturnKeyStartsNewLine (true);
  77. demoTextBox.setText ("Aa Bb Cc Dd Ee Ff Gg Hh Ii\n"
  78. "Jj Kk Ll Mm Nn Oo Pp Qq Rr\n"
  79. "Ss Tt Uu Vv Ww Xx Yy Zz\n"
  80. "0123456789\n\n"
  81. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
  82. "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
  83. "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
  84. "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat "
  85. "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
  86. demoTextBox.setCaretPosition (0);
  87. }
  88. //==============================================================================
  89. void paint (Graphics& g) override
  90. {
  91. fillStandardDemoBackground (g);
  92. }
  93. void resized() override
  94. {
  95. Rectangle<int> r (getLocalBounds().reduced (5));
  96. // lay out the list box and vertical divider..
  97. Component* vcomps[] = { &listBox, verticalDividerBar, nullptr };
  98. verticalLayout.layOutComponents (vcomps, 3,
  99. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  100. false, // lay out side-by-side
  101. true); // resize the components' heights as well as widths
  102. r.removeFromLeft (verticalDividerBar->getRight());
  103. styleBox.setBounds (r.removeFromBottom (26));
  104. r.removeFromBottom (8);
  105. const int labelWidth = 60;
  106. Rectangle<int> row (r.removeFromBottom (30));
  107. row.removeFromLeft (labelWidth);
  108. boldToggle.setBounds (row.removeFromLeft (row.getWidth() / 2));
  109. italicToggle.setBounds (row);
  110. r.removeFromBottom (8);
  111. scaleSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  112. r.removeFromBottom (8);
  113. kerningSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  114. r.removeFromBottom (8);
  115. heightSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  116. r.removeFromBottom (8);
  117. demoTextBox.setBounds (r);
  118. }
  119. void sliderValueChanged (Slider* sliderThatWasMoved) override
  120. {
  121. if (sliderThatWasMoved == &heightSlider) refreshPreviewBoxFont();
  122. else if (sliderThatWasMoved == &kerningSlider) refreshPreviewBoxFont();
  123. else if (sliderThatWasMoved == &scaleSlider) refreshPreviewBoxFont();
  124. }
  125. void buttonClicked (Button* buttonThatWasClicked) override
  126. {
  127. if (buttonThatWasClicked == &boldToggle) refreshPreviewBoxFont();
  128. else if (buttonThatWasClicked == &italicToggle) refreshPreviewBoxFont();
  129. }
  130. // The following methods implement the ListBoxModel virtual methods:
  131. int getNumRows() override
  132. {
  133. return fonts.size();
  134. }
  135. void paintListBoxItem (int rowNumber, Graphics& g,
  136. int width, int height, bool rowIsSelected) override
  137. {
  138. if (rowIsSelected)
  139. g.fillAll (Colours::lightblue);
  140. Font font (fonts [rowNumber]);
  141. AttributedString s;
  142. s.setWordWrap (AttributedString::none);
  143. s.setJustification (Justification::centredLeft);
  144. s.append (font.getTypefaceName(), font.withPointHeight (height * 0.7f), Colours::black);
  145. s.append (" " + font.getTypefaceName(), Font (height * 0.5f, Font::italic), Colours::grey);
  146. s.draw (g, Rectangle<int> (width, height).expanded (-4, 50).toFloat());
  147. }
  148. void selectedRowsChanged (int /*lastRowselected*/) override
  149. {
  150. refreshPreviewBoxFont();
  151. }
  152. private:
  153. Array<Font> fonts;
  154. StringArray currentStyleList;
  155. ListBox listBox;
  156. TextEditor demoTextBox;
  157. Label heightLabel, kerningLabel, scaleLabel, styleLabel;
  158. Slider heightSlider, kerningSlider, scaleSlider;
  159. ToggleButton boldToggle, italicToggle;
  160. ComboBox styleBox;
  161. StretchableLayoutManager verticalLayout;
  162. ScopedPointer<StretchableLayoutResizerBar> verticalDividerBar;
  163. void refreshPreviewBoxFont()
  164. {
  165. const bool bold = boldToggle.getToggleState();
  166. const bool italic = italicToggle.getToggleState();
  167. const bool useStyle = ! (bold || italic);
  168. Font font (fonts [listBox.getSelectedRow()]);
  169. font = font.withPointHeight ((float) heightSlider.getValue())
  170. .withExtraKerningFactor ((float) kerningSlider.getValue())
  171. .withHorizontalScale ((float) scaleSlider.getValue());
  172. if (bold) font = font.boldened();
  173. if (italic) font = font.italicised();
  174. updateStylesList (font);
  175. styleBox.setEnabled (useStyle);
  176. if (useStyle)
  177. font = font.withTypefaceStyle (styleBox.getText());
  178. demoTextBox.applyFontToAllText (font);
  179. }
  180. void updateStylesList (const Font& newFont)
  181. {
  182. const StringArray newStyles (newFont.getAvailableStyles());
  183. if (newStyles != currentStyleList)
  184. {
  185. currentStyleList = newStyles;
  186. styleBox.clear();
  187. styleBox.addItemList (newStyles, 1);
  188. styleBox.setSelectedItemIndex (0);
  189. }
  190. }
  191. void comboBoxChanged (ComboBox* box) override
  192. {
  193. if (box == &styleBox)
  194. refreshPreviewBoxFont();
  195. }
  196. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontsDemo)
  197. };
  198. // This static object will register this demo type in a global list of demos..
  199. static JuceDemoType<FontsDemo> demo ("20 Graphics: Fonts");