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.

264 lines
10.0KB

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