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.

270 lines
10KB

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