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.

219 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 "../jucedemo_headers.h"
  18. //==============================================================================
  19. class FontsAndTextDemo : public Component,
  20. public ListBoxModel,
  21. public ComboBoxListener,
  22. public SliderListener
  23. {
  24. public:
  25. //==============================================================================
  26. FontsAndTextDemo()
  27. : sizeLabel (String::empty, "Size:"),
  28. kerningLabel (String::empty, "Kerning:"),
  29. horizontalScaleLabel (String::empty, "Scale:"),
  30. styleLabel (String::empty, "Style:")
  31. {
  32. setName ("Fonts");
  33. Font::findFonts (fonts);
  34. addAndMakeVisible (listBox = new ListBox ("fonts", this));
  35. listBox->setRowHeight (28);
  36. addAndMakeVisible (&textBox);
  37. textBox.setColour (TextEditor::backgroundColourId, Colours::white);
  38. textBox.setColour (TextEditor::outlineColourId, Colours::black.withAlpha (0.5f));
  39. textBox.setMultiLine (true, true);
  40. textBox.setReturnKeyStartsNewLine (true);
  41. textBox.setText ("The Quick Brown Fox Jumps Over The Lazy Dog\n\n"
  42. "Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz 0123456789");
  43. addAndMakeVisible (&fontStylesComboBox);
  44. fontStylesComboBox.addListener (this);
  45. styleLabel.attachToComponent (&fontStylesComboBox, true);
  46. addAndMakeVisible (&sizeSlider);
  47. sizeSlider.setRange (3.0, 150.0, 0.1);
  48. sizeSlider.setValue (20.0);
  49. sizeSlider.addListener (this);
  50. sizeLabel.attachToComponent (&sizeSlider, true);
  51. addAndMakeVisible (&kerningSlider);
  52. kerningSlider.setRange (-1.0, 1.0, 0.01);
  53. kerningSlider.setValue (0.0);
  54. kerningSlider.addListener (this);
  55. kerningLabel.attachToComponent (&kerningSlider, true);
  56. addAndMakeVisible (&horizontalScaleSlider);
  57. horizontalScaleSlider.setRange (0.1, 4.0, 0.01);
  58. horizontalScaleSlider.setValue (1.0);
  59. horizontalScaleSlider.addListener (this);
  60. horizontalScaleLabel.attachToComponent (&horizontalScaleSlider, true);
  61. listBox->setColour (ListBox::outlineColourId, Colours::black.withAlpha (0.5f));
  62. listBox->setOutlineThickness (1);
  63. listBox->selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  64. // set up the layout and resizer bars..
  65. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.5); // width of the font list must be
  66. // between 20% and 80%, preferably 50%
  67. verticalLayout.setItemLayout (1, 8, 8, 8); // the vertical divider drag-bar thing is always 8 pixels wide
  68. verticalLayout.setItemLayout (2, 150, -1.0, -0.5); // the components on the right must be
  69. // at least 150 pixels wide, preferably 50% of the total width
  70. verticalDividerBar = new StretchableLayoutResizerBar (&verticalLayout, 1, true);
  71. addAndMakeVisible (verticalDividerBar);
  72. }
  73. void resized()
  74. {
  75. // lay out the list box and vertical divider..
  76. Component* vcomps[] = { listBox, verticalDividerBar, 0 };
  77. verticalLayout.layOutComponents (vcomps, 3,
  78. 4, 4, getWidth() - 8, getHeight() - 8,
  79. false, // lay out side-by-side
  80. true); // resize the components' heights as well as widths
  81. // now lay out the text box and the controls below it..
  82. int x = verticalLayout.getItemCurrentPosition (2) + 4;
  83. textBox.setBounds (x, 0, getWidth() - x, getHeight() - 110);
  84. x += 70;
  85. sizeSlider.setBounds (x, getHeight() - 106, getWidth() - x, 22);
  86. kerningSlider.setBounds (x, getHeight() - 82, getWidth() - x, 22);
  87. horizontalScaleSlider.setBounds (x, getHeight() - 58, getWidth() - x, 22);
  88. fontStylesComboBox.setBounds (x, getHeight() - 34, (getWidth() - x) / 2, 22);
  89. }
  90. // implements the ListBoxModel method
  91. int getNumRows()
  92. {
  93. return fonts.size();
  94. }
  95. // implements the ListBoxModel method
  96. void paintListBoxItem (int rowNumber,
  97. Graphics& g,
  98. int width, int height,
  99. bool rowIsSelected)
  100. {
  101. if (rowIsSelected)
  102. g.fillAll (Colours::lightblue);
  103. Font font = fonts[rowNumber].withPointHeight (height * 0.6f);
  104. g.setFont (font);
  105. g.setColour (Colours::black);
  106. g.drawText (font.getTypefaceName(),
  107. 4, 0, width - 4, height,
  108. Justification::centredLeft, true);
  109. int x = jmax (0, font.getStringWidth (font.getTypefaceName())) + 12;
  110. g.setFont (Font (11.0f, Font::italic));
  111. g.setColour (Colours::grey);
  112. g.drawText (font.getTypefaceName(),
  113. x, 0, width - x - 2, height,
  114. Justification::centredLeft, true);
  115. }
  116. void updatePreviewBoxText()
  117. {
  118. Font font (fonts [listBox->getSelectedRow()]);
  119. font.setHeight ((float) sizeSlider.getValue());
  120. font.setExtraKerningFactor ((float) kerningSlider.getValue());
  121. font.setHorizontalScale ((float) horizontalScaleSlider.getValue());
  122. updateStylesList (font);
  123. font.setTypefaceStyle (fontStylesComboBox.getText());
  124. textBox.applyFontToAllText (font);
  125. }
  126. void updateStylesList (const Font& newFont)
  127. {
  128. const StringArray newStyles (newFont.getAvailableStyles());
  129. if (newStyles != currentStyleList)
  130. {
  131. currentStyleList = newStyles;
  132. fontStylesComboBox.clear();
  133. for (int i = 0; i < newStyles.size(); ++i)
  134. fontStylesComboBox.addItem (newStyles[i], i + 1);
  135. fontStylesComboBox.setSelectedItemIndex (0);
  136. }
  137. }
  138. void selectedRowsChanged (int /*lastRowselected*/)
  139. {
  140. updatePreviewBoxText();
  141. }
  142. void buttonClicked (Button*)
  143. {
  144. updatePreviewBoxText();
  145. }
  146. void sliderValueChanged (Slider*)
  147. {
  148. updatePreviewBoxText();
  149. }
  150. void comboBoxChanged (ComboBox*)
  151. {
  152. updatePreviewBoxText();
  153. }
  154. private:
  155. Array<Font> fonts;
  156. StringArray currentStyleList;
  157. ScopedPointer<ListBox> listBox;
  158. TextEditor textBox;
  159. ComboBox fontStylesComboBox;
  160. Slider sizeSlider, kerningSlider, horizontalScaleSlider;
  161. Label sizeLabel, kerningLabel, horizontalScaleLabel, styleLabel;
  162. StretchableLayoutManager verticalLayout;
  163. ScopedPointer<StretchableLayoutResizerBar> verticalDividerBar;
  164. };
  165. //==============================================================================
  166. Component* createFontsAndTextDemo()
  167. {
  168. return new FontsAndTextDemo();
  169. }