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.

259 lines
9.9KB

  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 ("Aa Bb Cc Dd Ee Ff Gg Hh Ii\n"
  79. "Jj Kk Ll Mm Nn Oo Pp Qq Rr\n"
  80. "Ss Tt Uu Vv Ww Xx Yy Zz\n"
  81. "0123456789\n\n"
  82. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
  83. "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
  84. "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
  85. "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat "
  86. "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
  87. demoTextBox.setCaretPosition (0);
  88. }
  89. //==============================================================================
  90. void paint (Graphics& g)
  91. {
  92. fillBrushedAluminiumBackground (g);
  93. }
  94. void resized()
  95. {
  96. Rectangle<int> r (getLocalBounds().reduced (5));
  97. // lay out the list box and vertical divider..
  98. Component* vcomps[] = { &listBox, verticalDividerBar, nullptr };
  99. verticalLayout.layOutComponents (vcomps, 3,
  100. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  101. false, // lay out side-by-side
  102. true); // resize the components' heights as well as widths
  103. r.removeFromLeft (verticalDividerBar->getRight());
  104. styleBox.setBounds (r.removeFromBottom (26));
  105. r.removeFromBottom (8);
  106. const int labelWidth = 60;
  107. Rectangle<int> row (r.removeFromBottom (30));
  108. row.removeFromLeft (labelWidth);
  109. boldToggle.setBounds (row.removeFromLeft (row.getWidth() / 2));
  110. italicToggle.setBounds (row);
  111. r.removeFromBottom (8);
  112. scaleSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  113. r.removeFromBottom (8);
  114. kerningSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  115. r.removeFromBottom (8);
  116. heightSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  117. r.removeFromBottom (8);
  118. demoTextBox.setBounds (r);
  119. }
  120. void sliderValueChanged (Slider* sliderThatWasMoved)
  121. {
  122. if (sliderThatWasMoved == &heightSlider) refreshPreviewBoxFont();
  123. else if (sliderThatWasMoved == &kerningSlider) refreshPreviewBoxFont();
  124. else if (sliderThatWasMoved == &scaleSlider) refreshPreviewBoxFont();
  125. }
  126. void buttonClicked (Button* buttonThatWasClicked)
  127. {
  128. if (buttonThatWasClicked == &boldToggle) refreshPreviewBoxFont();
  129. else if (buttonThatWasClicked == &italicToggle) refreshPreviewBoxFont();
  130. }
  131. // The following methods implement the ListBoxModel virtual methods:
  132. int getNumRows()
  133. {
  134. return fonts.size();
  135. }
  136. void paintListBoxItem (int rowNumber, Graphics& g,
  137. int width, int height, bool rowIsSelected)
  138. {
  139. if (rowIsSelected)
  140. g.fillAll (Colours::lightblue);
  141. Font font (fonts [rowNumber]);
  142. AttributedString s;
  143. s.setWordWrap (AttributedString::none);
  144. s.setJustification (Justification::centredLeft);
  145. s.append (font.getTypefaceName(), font.withPointHeight (height * 0.7f), Colours::black);
  146. s.append (" " + font.getTypefaceName(), Font (height * 0.5f, Font::italic), Colours::grey);
  147. s.draw (g, Rectangle<int> (width, height).expanded (-4, 50).toFloat());
  148. }
  149. void selectedRowsChanged (int /*lastRowselected*/)
  150. {
  151. refreshPreviewBoxFont();
  152. }
  153. private:
  154. Array<Font> fonts;
  155. StringArray currentStyleList;
  156. ListBox listBox;
  157. TextEditor demoTextBox;
  158. Label heightLabel, kerningLabel, scaleLabel, styleLabel;
  159. Slider heightSlider, kerningSlider, scaleSlider;
  160. ToggleButton boldToggle, italicToggle;
  161. ComboBox styleBox;
  162. StretchableLayoutManager verticalLayout;
  163. ScopedPointer<StretchableLayoutResizerBar> verticalDividerBar;
  164. void refreshPreviewBoxFont()
  165. {
  166. const bool bold = boldToggle.getToggleState();
  167. const bool italic = italicToggle.getToggleState();
  168. const bool useStyle = ! (bold || italic);
  169. Font font (fonts [listBox.getSelectedRow()]);
  170. font = font.withPointHeight ((float) heightSlider.getValue())
  171. .withExtraKerningFactor ((float) kerningSlider.getValue())
  172. .withHorizontalScale ((float) scaleSlider.getValue());
  173. if (bold) font = font.boldened();
  174. if (italic) font = font.italicised();
  175. updateStylesList (font);
  176. styleBox.setEnabled (useStyle);
  177. if (useStyle)
  178. font = font.withTypefaceStyle (styleBox.getText());
  179. demoTextBox.applyFontToAllText (font);
  180. }
  181. void updateStylesList (const Font& newFont)
  182. {
  183. const StringArray newStyles (newFont.getAvailableStyles());
  184. if (newStyles != currentStyleList)
  185. {
  186. currentStyleList = newStyles;
  187. styleBox.clear();
  188. styleBox.addItemList (newStyles, 1);
  189. styleBox.setSelectedItemIndex (0);
  190. }
  191. }
  192. void comboBoxChanged (ComboBox* box) override
  193. {
  194. if (box == &styleBox)
  195. refreshPreviewBoxFont();
  196. }
  197. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontsDemo)
  198. };
  199. // This static object will register this demo type in a global list of demos..
  200. static JuceDemoType<FontsDemo> demo ("20 Graphics: Fonts");