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.

254 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. //==============================================================================
  20. class FontsDemo : public Component,
  21. private ListBoxModel,
  22. private Slider::Listener,
  23. private Button::Listener,
  24. private ComboBox::Listener
  25. {
  26. public:
  27. FontsDemo()
  28. : heightLabel (String::empty, "Height:"),
  29. kerningLabel (String::empty, "Kerning:"),
  30. scaleLabel (String::empty, "Scale:"),
  31. styleLabel ("Style"),
  32. boldToggle ("Bold"),
  33. italicToggle ("Italic")
  34. {
  35. setOpaque (true);
  36. addAndMakeVisible (&listBox);
  37. addAndMakeVisible (&demoTextBox);
  38. addAndMakeVisible (&heightSlider);
  39. addAndMakeVisible (&heightLabel);
  40. addAndMakeVisible (&kerningLabel);
  41. addAndMakeVisible (&kerningSlider);
  42. addAndMakeVisible (&scaleLabel);
  43. addAndMakeVisible (&scaleSlider);
  44. addAndMakeVisible (&boldToggle);
  45. addAndMakeVisible (&italicToggle);
  46. addAndMakeVisible (&styleBox);
  47. kerningLabel.attachToComponent (&kerningSlider, true);
  48. heightLabel.attachToComponent (&heightSlider, true);
  49. scaleLabel.attachToComponent (&scaleSlider, true);
  50. styleLabel.attachToComponent (&styleBox, true);
  51. heightSlider.addListener (this);
  52. kerningSlider.addListener (this);
  53. scaleSlider.addListener (this);
  54. boldToggle.addListener (this);
  55. italicToggle.addListener (this);
  56. styleBox.addListener (this);
  57. Font::findFonts (fonts); // Generate the list of fonts
  58. listBox.setRowHeight (20);
  59. listBox.setModel (this); // Tell the listbox where to get its data model
  60. heightSlider.setRange (3.0, 150.0, 0.01);
  61. scaleSlider.setRange (0.2, 3.0, 0.01);
  62. kerningSlider.setRange (-2.0, 2.0, 0.01);
  63. scaleSlider.setValue (1.0); // Set some initial values for the sliders.
  64. heightSlider.setValue (20.0);
  65. kerningSlider.setValue (0);
  66. // set up the layout and resizer bars..
  67. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.35); // width of the font list must be
  68. // between 20% and 80%, preferably 50%
  69. verticalLayout.setItemLayout (1, 8, 8, 8); // the vertical divider drag-bar thing is always 8 pixels wide
  70. verticalLayout.setItemLayout (2, 150, -1.0, -0.65); // the components on the right must be
  71. // at least 150 pixels wide, preferably 50% of the total width
  72. verticalDividerBar = new StretchableLayoutResizerBar (&verticalLayout, 1, true);
  73. addAndMakeVisible (verticalDividerBar);
  74. // ..and pick a random font to select intially
  75. listBox.selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  76. demoTextBox.setMultiLine (true);
  77. demoTextBox.setReturnKeyStartsNewLine (true);
  78. demoTextBox.setText ("The Quick Brown Fox Jumped Over The Lazy Dog\n\n"
  79. "Aa Bb Cc Dd Ee Ff Gg Hh Ii\n"
  80. "Jj Kk Ll Mm Nn Oo Pp Qq Rr\n"
  81. "Ss Tt Uu Vv Ww Xx Yy Zz\n"
  82. "0123456789");
  83. demoTextBox.setCaretPosition (0);
  84. }
  85. //==============================================================================
  86. void paint (Graphics& g)
  87. {
  88. fillBrushedAluminiumBackground (g);
  89. }
  90. void resized()
  91. {
  92. Rectangle<int> r (getLocalBounds().reduced (5));
  93. // lay out the list box and vertical divider..
  94. Component* vcomps[] = { &listBox, verticalDividerBar, nullptr };
  95. verticalLayout.layOutComponents (vcomps, 3,
  96. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  97. false, // lay out side-by-side
  98. true); // resize the components' heights as well as widths
  99. r.removeFromLeft (verticalDividerBar->getRight());
  100. styleBox.setBounds (r.removeFromBottom (26));
  101. r.removeFromBottom (8);
  102. const int labelWidth = 60;
  103. Rectangle<int> row (r.removeFromBottom (30));
  104. row.removeFromLeft (labelWidth);
  105. boldToggle.setBounds (row.removeFromLeft (row.getWidth() / 2));
  106. italicToggle.setBounds (row);
  107. r.removeFromBottom (8);
  108. scaleSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  109. r.removeFromBottom (8);
  110. kerningSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  111. r.removeFromBottom (8);
  112. heightSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  113. r.removeFromBottom (8);
  114. demoTextBox.setBounds (r);
  115. }
  116. void sliderValueChanged (Slider* sliderThatWasMoved)
  117. {
  118. if (sliderThatWasMoved == &heightSlider) refreshPreviewBoxFont();
  119. else if (sliderThatWasMoved == &kerningSlider) refreshPreviewBoxFont();
  120. else if (sliderThatWasMoved == &scaleSlider) refreshPreviewBoxFont();
  121. }
  122. void buttonClicked (Button* buttonThatWasClicked)
  123. {
  124. if (buttonThatWasClicked == &boldToggle) refreshPreviewBoxFont();
  125. else if (buttonThatWasClicked == &italicToggle) refreshPreviewBoxFont();
  126. }
  127. // The following methods implement the ListBoxModel virtual methods:
  128. int getNumRows()
  129. {
  130. return fonts.size();
  131. }
  132. void paintListBoxItem (int rowNumber, Graphics& g,
  133. int width, int height, bool rowIsSelected)
  134. {
  135. if (rowIsSelected)
  136. g.fillAll (Colours::lightblue);
  137. Font font (fonts [rowNumber]);
  138. AttributedString s;
  139. s.setWordWrap (AttributedString::none);
  140. s.setJustification (Justification::centredLeft);
  141. s.append (font.getTypefaceName(), font.withHeight (height * 0.8f), Colours::black);
  142. s.append (" " + font.getTypefaceName(), Font (height * 0.5f, Font::italic), Colours::grey);
  143. s.draw (g, Rectangle<float> (4.0f, 0.0f, width - 5.0f, (float) height));
  144. }
  145. void selectedRowsChanged (int /*lastRowselected*/)
  146. {
  147. refreshPreviewBoxFont();
  148. }
  149. private:
  150. Array<Font> fonts;
  151. StringArray currentStyleList;
  152. ListBox listBox;
  153. TextEditor demoTextBox;
  154. Label heightLabel, kerningLabel, scaleLabel, styleLabel;
  155. Slider heightSlider, kerningSlider, scaleSlider;
  156. ToggleButton boldToggle, italicToggle;
  157. ComboBox styleBox;
  158. StretchableLayoutManager verticalLayout;
  159. ScopedPointer<StretchableLayoutResizerBar> verticalDividerBar;
  160. void refreshPreviewBoxFont()
  161. {
  162. const bool bold = boldToggle.getToggleState();
  163. const bool italic = italicToggle.getToggleState();
  164. const bool useStyle = ! (bold || italic);
  165. Font font (fonts [listBox.getSelectedRow()]);
  166. font.setHeight ((float) heightSlider.getValue());
  167. font.setBold (bold);
  168. font.setItalic (italic);
  169. font.setExtraKerningFactor ((float) kerningSlider.getValue());
  170. font.setHorizontalScale ((float) scaleSlider.getValue());
  171. updateStylesList (font);
  172. styleBox.setEnabled (useStyle);
  173. if (useStyle)
  174. font.setTypefaceStyle (styleBox.getText());
  175. demoTextBox.applyFontToAllText (font);
  176. }
  177. void updateStylesList (const Font& newFont)
  178. {
  179. const StringArray newStyles (newFont.getAvailableStyles());
  180. if (newStyles != currentStyleList)
  181. {
  182. currentStyleList = newStyles;
  183. styleBox.clear();
  184. styleBox.addItemList (newStyles, 1);
  185. styleBox.setSelectedItemIndex (0);
  186. }
  187. }
  188. void comboBoxChanged (ComboBox* box) override
  189. {
  190. if (box == &styleBox)
  191. refreshPreviewBoxFont();
  192. }
  193. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontsDemo)
  194. };
  195. // This static object will register this demo type in a global list of demos..
  196. static JuceDemoType<FontsDemo> demo ("20 Graphics: Fonts");