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.

274 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: FontsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays different font styles and types.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics
  26. exporters: xcode_mac, vs2019, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: FontsDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. class FontsDemo : public Component,
  37. private ListBoxModel,
  38. private Slider::Listener
  39. {
  40. public:
  41. FontsDemo()
  42. {
  43. setOpaque (true);
  44. addAndMakeVisible (listBox);
  45. addAndMakeVisible (demoTextBox);
  46. addAndMakeVisible (heightSlider);
  47. addAndMakeVisible (heightLabel);
  48. addAndMakeVisible (kerningLabel);
  49. addAndMakeVisible (kerningSlider);
  50. addAndMakeVisible (scaleLabel);
  51. addAndMakeVisible (scaleSlider);
  52. addAndMakeVisible (boldToggle);
  53. addAndMakeVisible (italicToggle);
  54. addAndMakeVisible (styleBox);
  55. kerningLabel.attachToComponent (&kerningSlider, true);
  56. heightLabel .attachToComponent (&heightSlider, true);
  57. scaleLabel .attachToComponent (&scaleSlider, true);
  58. styleLabel .attachToComponent (&styleBox, true);
  59. heightSlider .addListener (this);
  60. kerningSlider.addListener (this);
  61. scaleSlider .addListener (this);
  62. boldToggle .onClick = [this] { refreshPreviewBoxFont(); };
  63. italicToggle.onClick = [this] { refreshPreviewBoxFont(); };
  64. styleBox .onChange = [this] { refreshPreviewBoxFont(); };
  65. Font::findFonts (fonts); // Generate the list of fonts
  66. listBox.setRowHeight (20);
  67. listBox.setModel (this); // Tell the listbox where to get its data model
  68. listBox.setColour (ListBox::textColourId, Colours::black);
  69. listBox.setColour (ListBox::backgroundColourId, Colours::white);
  70. heightSlider .setRange (3.0, 150.0, 0.01);
  71. scaleSlider .setRange (0.2, 3.0, 0.01);
  72. kerningSlider.setRange (-2.0, 2.0, 0.01);
  73. scaleSlider .setValue (1.0); // Set some initial values for the sliders.
  74. heightSlider .setValue (20.0);
  75. kerningSlider.setValue (0);
  76. // set up the layout and resizer bars..
  77. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.35); // width of the font list must be
  78. // between 20% and 80%, preferably 50%
  79. verticalLayout.setItemLayout (1, 8, 8, 8); // the vertical divider drag-bar thing is always 8 pixels wide
  80. verticalLayout.setItemLayout (2, 150, -1.0, -0.65); // the components on the right must be
  81. // at least 150 pixels wide, preferably 50% of the total width
  82. verticalDividerBar.reset (new StretchableLayoutResizerBar (&verticalLayout, 1, true));
  83. addAndMakeVisible (verticalDividerBar.get());
  84. // ..and pick a random font to select initially
  85. listBox.selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  86. demoTextBox.setMultiLine (true);
  87. demoTextBox.setReturnKeyStartsNewLine (true);
  88. demoTextBox.setText ("Aa Bb Cc Dd Ee Ff Gg Hh Ii\n"
  89. "Jj Kk Ll Mm Nn Oo Pp Qq Rr\n"
  90. "Ss Tt Uu Vv Ww Xx Yy Zz\n"
  91. "0123456789\n\n"
  92. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
  93. "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
  94. "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
  95. "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat "
  96. "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
  97. demoTextBox.setCaretPosition (0);
  98. demoTextBox.setColour (TextEditor::textColourId, Colours::black);
  99. demoTextBox.setColour (TextEditor::backgroundColourId, Colours::white);
  100. setSize (750, 750);
  101. }
  102. //==============================================================================
  103. void paint (Graphics& g) override
  104. {
  105. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  106. }
  107. void resized() override
  108. {
  109. auto r = getLocalBounds().reduced (5);
  110. // lay out the list box and vertical divider..
  111. Component* vcomps[] = { &listBox, verticalDividerBar.get(), nullptr };
  112. verticalLayout.layOutComponents (vcomps, 3,
  113. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  114. false, // lay out side-by-side
  115. true); // resize the components' heights as well as widths
  116. r.removeFromLeft (verticalDividerBar->getRight());
  117. int labelWidth = 60;
  118. auto styleArea = r.removeFromBottom (26);
  119. styleArea.removeFromLeft (labelWidth);
  120. styleBox.setBounds (styleArea);
  121. r.removeFromBottom (8);
  122. auto row = r.removeFromBottom (30);
  123. row.removeFromLeft (labelWidth);
  124. boldToggle.setBounds (row.removeFromLeft (row.getWidth() / 2));
  125. italicToggle.setBounds (row);
  126. r.removeFromBottom (8);
  127. scaleSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  128. r.removeFromBottom (8);
  129. kerningSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  130. r.removeFromBottom (8);
  131. heightSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  132. r.removeFromBottom (8);
  133. demoTextBox.setBounds (r);
  134. }
  135. void sliderValueChanged (Slider* sliderThatWasMoved) override
  136. {
  137. if (sliderThatWasMoved == &heightSlider) refreshPreviewBoxFont();
  138. else if (sliderThatWasMoved == &kerningSlider) refreshPreviewBoxFont();
  139. else if (sliderThatWasMoved == &scaleSlider) refreshPreviewBoxFont();
  140. }
  141. // The following methods implement the ListBoxModel virtual methods:
  142. int getNumRows() override
  143. {
  144. return fonts.size();
  145. }
  146. void paintListBoxItem (int rowNumber, Graphics& g,
  147. int width, int height, bool rowIsSelected) override
  148. {
  149. if (rowIsSelected)
  150. g.fillAll (Colours::lightblue);
  151. auto font = fonts[rowNumber];
  152. AttributedString s;
  153. s.setWordWrap (AttributedString::none);
  154. s.setJustification (Justification::centredLeft);
  155. s.append (font.getTypefaceName(), font.withHeight (height * 0.7f), Colours::black);
  156. s.append (" " + font.getTypefaceName(), Font (height * 0.5f, Font::italic), Colours::grey);
  157. s.draw (g, Rectangle<int> (width, height).expanded (-4, 50).toFloat());
  158. }
  159. void selectedRowsChanged (int /*lastRowselected*/) override
  160. {
  161. refreshPreviewBoxFont();
  162. }
  163. private:
  164. Array<Font> fonts;
  165. StringArray currentStyleList;
  166. ListBox listBox;
  167. TextEditor demoTextBox;
  168. Label heightLabel { {}, "Height:" },
  169. kerningLabel { {}, "Kerning:" },
  170. scaleLabel { {}, "Scale:" },
  171. styleLabel { {}, "Style:" };
  172. ToggleButton boldToggle { "Bold" },
  173. italicToggle { "Italic" };
  174. Slider heightSlider, kerningSlider, scaleSlider;
  175. ComboBox styleBox;
  176. StretchableLayoutManager verticalLayout;
  177. std::unique_ptr<StretchableLayoutResizerBar> verticalDividerBar;
  178. //==============================================================================
  179. void refreshPreviewBoxFont()
  180. {
  181. auto bold = boldToggle .getToggleState();
  182. auto italic = italicToggle.getToggleState();
  183. auto useStyle = ! (bold || italic);
  184. auto font = fonts[listBox.getSelectedRow()];
  185. font = font.withPointHeight ((float) heightSlider .getValue())
  186. .withExtraKerningFactor ((float) kerningSlider.getValue())
  187. .withHorizontalScale ((float) scaleSlider .getValue());
  188. if (bold) font = font.boldened();
  189. if (italic) font = font.italicised();
  190. updateStylesList (font);
  191. styleBox.setEnabled (useStyle);
  192. if (useStyle)
  193. font = font.withTypefaceStyle (styleBox.getText());
  194. demoTextBox.applyFontToAllText (font);
  195. }
  196. void updateStylesList (const Font& newFont)
  197. {
  198. auto newStyles = newFont.getAvailableStyles();
  199. if (newStyles != currentStyleList)
  200. {
  201. currentStyleList = newStyles;
  202. styleBox.clear();
  203. styleBox.addItemList (newStyles, 1);
  204. styleBox.setSelectedItemIndex (0);
  205. }
  206. }
  207. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontsDemo)
  208. };