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.

221 lines
8.1KB

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