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.

266 lines
10.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceDemoHeader.h"
  20. //==============================================================================
  21. class FontsDemo : public Component,
  22. private ListBoxModel,
  23. private Slider::Listener,
  24. private Button::Listener,
  25. private ComboBox::Listener
  26. {
  27. public:
  28. FontsDemo()
  29. {
  30. setOpaque (true);
  31. addAndMakeVisible (listBox);
  32. addAndMakeVisible (demoTextBox);
  33. addAndMakeVisible (heightSlider);
  34. addAndMakeVisible (heightLabel);
  35. addAndMakeVisible (kerningLabel);
  36. addAndMakeVisible (kerningSlider);
  37. addAndMakeVisible (scaleLabel);
  38. addAndMakeVisible (scaleSlider);
  39. addAndMakeVisible (boldToggle);
  40. addAndMakeVisible (italicToggle);
  41. addAndMakeVisible (styleBox);
  42. kerningLabel.attachToComponent (&kerningSlider, true);
  43. heightLabel.attachToComponent (&heightSlider, true);
  44. scaleLabel.attachToComponent (&scaleSlider, true);
  45. styleLabel.attachToComponent (&styleBox, true);
  46. heightSlider.addListener (this);
  47. kerningSlider.addListener (this);
  48. scaleSlider.addListener (this);
  49. boldToggle.addListener (this);
  50. italicToggle.addListener (this);
  51. styleBox.addListener (this);
  52. Font::findFonts (fonts); // Generate the list of fonts
  53. listBox.setRowHeight (20);
  54. listBox.setModel (this); // Tell the listbox where to get its data model
  55. listBox.setColour (ListBox::textColourId, Colours::black);
  56. listBox.setColour (ListBox::backgroundColourId, Colours::white);
  57. heightSlider.setRange (3.0, 150.0, 0.01);
  58. scaleSlider.setRange (0.2, 3.0, 0.01);
  59. kerningSlider.setRange (-2.0, 2.0, 0.01);
  60. scaleSlider.setValue (1.0); // Set some initial values for the sliders.
  61. heightSlider.setValue (20.0);
  62. kerningSlider.setValue (0);
  63. // set up the layout and resizer bars..
  64. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.35); // width of the font list must be
  65. // between 20% and 80%, preferably 50%
  66. verticalLayout.setItemLayout (1, 8, 8, 8); // the vertical divider drag-bar thing is always 8 pixels wide
  67. verticalLayout.setItemLayout (2, 150, -1.0, -0.65); // the components on the right must be
  68. // at least 150 pixels wide, preferably 50% of the total width
  69. verticalDividerBar = new StretchableLayoutResizerBar (&verticalLayout, 1, true);
  70. addAndMakeVisible (verticalDividerBar);
  71. // ..and pick a random font to select intially
  72. listBox.selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  73. demoTextBox.setMultiLine (true);
  74. demoTextBox.setReturnKeyStartsNewLine (true);
  75. demoTextBox.setText ("Aa Bb Cc Dd Ee Ff Gg Hh Ii\n"
  76. "Jj Kk Ll Mm Nn Oo Pp Qq Rr\n"
  77. "Ss Tt Uu Vv Ww Xx Yy Zz\n"
  78. "0123456789\n\n"
  79. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
  80. "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
  81. "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
  82. "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat "
  83. "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
  84. demoTextBox.setCaretPosition (0);
  85. demoTextBox.setColour (TextEditor::textColourId, Colours::black);
  86. demoTextBox.setColour (TextEditor::backgroundColourId, Colours::white);
  87. }
  88. //==============================================================================
  89. void paint (Graphics& g) override
  90. {
  91. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  92. }
  93. void resized() override
  94. {
  95. auto 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. auto 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.withHeight (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 {{}, "Height:" },
  158. kerningLabel {{}, "Kerning:" },
  159. scaleLabel { "Scale:" },
  160. styleLabel { "Style" };
  161. ToggleButton boldToggle { "Bold" },
  162. italicToggle { "Italic" };
  163. Slider heightSlider, kerningSlider, scaleSlider;
  164. ComboBox styleBox;
  165. StretchableLayoutManager verticalLayout;
  166. ScopedPointer<StretchableLayoutResizerBar> verticalDividerBar;
  167. void refreshPreviewBoxFont()
  168. {
  169. const bool bold = boldToggle.getToggleState();
  170. const bool italic = italicToggle.getToggleState();
  171. const bool useStyle = ! (bold || italic);
  172. Font font (fonts [listBox.getSelectedRow()]);
  173. font = font.withPointHeight ((float) heightSlider.getValue())
  174. .withExtraKerningFactor ((float) kerningSlider.getValue())
  175. .withHorizontalScale ((float) scaleSlider.getValue());
  176. if (bold) font = font.boldened();
  177. if (italic) font = font.italicised();
  178. updateStylesList (font);
  179. styleBox.setEnabled (useStyle);
  180. if (useStyle)
  181. font = font.withTypefaceStyle (styleBox.getText());
  182. demoTextBox.applyFontToAllText (font);
  183. }
  184. void updateStylesList (const Font& newFont)
  185. {
  186. const StringArray newStyles (newFont.getAvailableStyles());
  187. if (newStyles != currentStyleList)
  188. {
  189. currentStyleList = newStyles;
  190. styleBox.clear();
  191. styleBox.addItemList (newStyles, 1);
  192. styleBox.setSelectedItemIndex (0);
  193. }
  194. }
  195. void comboBoxChanged (ComboBox* box) override
  196. {
  197. if (box == &styleBox)
  198. refreshPreviewBoxFont();
  199. }
  200. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontsDemo)
  201. };
  202. // This static object will register this demo type in a global list of demos..
  203. static JuceDemoType<FontsDemo> demo ("20 Graphics: Fonts");