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.

205 lines
7.7KB

  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 ButtonListener,
  23. public SliderListener
  24. {
  25. public:
  26. //==============================================================================
  27. FontsAndTextDemo()
  28. : boldButton ("Bold"),
  29. italicButton ("Italic"),
  30. sizeLabel (String::empty, "Size"),
  31. kerningLabel (String::empty, "Kerning"),
  32. horizontalScaleLabel (String::empty, "Scale")
  33. {
  34. setName ("Fonts");
  35. Font::findFonts (fonts);
  36. addAndMakeVisible (listBox = new ListBox ("fonts", this));
  37. listBox->setRowHeight (28);
  38. addAndMakeVisible (&textBox);
  39. textBox.setColour (TextEditor::backgroundColourId, Colours::white);
  40. textBox.setColour (TextEditor::outlineColourId, Colours::black.withAlpha (0.5f));
  41. textBox.setMultiLine (true, true);
  42. textBox.setReturnKeyStartsNewLine (true);
  43. textBox.setText ("The Quick Brown Fox Jumps Over The Lazy Dog\n\nAa 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 (&boldButton);
  45. boldButton.addListener (this);
  46. addAndMakeVisible (&italicButton);
  47. italicButton.addListener (this);
  48. addAndMakeVisible (&sizeSlider);
  49. sizeSlider.setRange (3.0, 150.0, 0.1);
  50. sizeSlider.setValue (20.0);
  51. sizeSlider.addListener (this);
  52. sizeLabel.attachToComponent (&sizeSlider, true);
  53. addAndMakeVisible (&kerningSlider);
  54. kerningSlider.setRange (-1.0, 1.0, 0.01);
  55. kerningSlider.setValue (0.0);
  56. kerningSlider.addListener (this);
  57. kerningLabel.attachToComponent (&kerningSlider, true);
  58. addAndMakeVisible (&horizontalScaleSlider);
  59. horizontalScaleSlider.setRange (0.1, 4.0, 0.01);
  60. horizontalScaleSlider.setValue (1.0);
  61. horizontalScaleSlider.addListener (this);
  62. horizontalScaleLabel.attachToComponent (&horizontalScaleSlider, true);
  63. listBox->setColour (ListBox::outlineColourId, Colours::black.withAlpha (0.5f));
  64. listBox->setOutlineThickness (1);
  65. listBox->selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  66. // set up the layout and resizer bars..
  67. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.5); // 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.5); // 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. }
  75. ~FontsAndTextDemo()
  76. {
  77. }
  78. void resized()
  79. {
  80. // lay out the list box and vertical divider..
  81. Component* vcomps[] = { listBox, verticalDividerBar, 0 };
  82. verticalLayout.layOutComponents (vcomps, 3,
  83. 4, 4, getWidth() - 8, getHeight() - 8,
  84. false, // lay out side-by-side
  85. true); // resize the components' heights as well as widths
  86. // now lay out the text box and the controls below it..
  87. int x = verticalLayout.getItemCurrentPosition (2) + 4;
  88. textBox.setBounds (x, 0, getWidth() - x, getHeight() - 110);
  89. x += 70;
  90. sizeSlider.setBounds (x, getHeight() - 106, getWidth() - x, 22);
  91. kerningSlider.setBounds (x, getHeight() - 82, getWidth() - x, 22);
  92. horizontalScaleSlider.setBounds (x, getHeight() - 58, getWidth() - x, 22);
  93. boldButton.setBounds (x, getHeight() - 34, (getWidth() - x) / 2, 22);
  94. italicButton.setBounds (x + (getWidth() - x) / 2, getHeight() - 34, (getWidth() - x) / 2, 22);
  95. }
  96. // implements the ListBoxModel method
  97. int getNumRows()
  98. {
  99. return fonts.size();
  100. }
  101. // implements the ListBoxModel method
  102. void paintListBoxItem (int rowNumber,
  103. Graphics& g,
  104. int width, int height,
  105. bool rowIsSelected)
  106. {
  107. if (rowIsSelected)
  108. g.fillAll (Colours::lightblue);
  109. Font font (fonts [rowNumber]);
  110. font.setHeight (height * 0.7f);
  111. g.setFont (font);
  112. g.setColour (Colours::black);
  113. g.drawText (font.getTypefaceName(),
  114. 4, 0, width - 4, height,
  115. Justification::centredLeft, true);
  116. int x = jmax (0, font.getStringWidth (font.getTypefaceName())) + 12;
  117. g.setFont (Font (11.0f, Font::italic));
  118. g.setColour (Colours::grey);
  119. g.drawText (font.getTypefaceName(),
  120. x, 0, width - x - 2, height,
  121. Justification::centredLeft, true);
  122. }
  123. void updatePreviewBoxText()
  124. {
  125. Font font (fonts [listBox->getSelectedRow()]);
  126. font.setHeight ((float) sizeSlider.getValue());
  127. font.setBold (boldButton.getToggleState());
  128. font.setItalic (italicButton.getToggleState());
  129. font.setExtraKerningFactor ((float) kerningSlider.getValue());
  130. font.setHorizontalScale ((float) horizontalScaleSlider.getValue());
  131. textBox.applyFontToAllText (font);
  132. }
  133. void selectedRowsChanged (int /*lastRowselected*/)
  134. {
  135. updatePreviewBoxText();
  136. }
  137. void buttonClicked (Button*)
  138. {
  139. updatePreviewBoxText();
  140. }
  141. void sliderValueChanged (Slider*)
  142. {
  143. // (this is called when the size slider is moved)
  144. updatePreviewBoxText();
  145. }
  146. private:
  147. Array<Font> fonts;
  148. ScopedPointer<ListBox> listBox;
  149. TextEditor textBox;
  150. ToggleButton boldButton, italicButton;
  151. Slider sizeSlider, kerningSlider, horizontalScaleSlider;
  152. Label sizeLabel, kerningLabel, horizontalScaleLabel;
  153. StretchableLayoutManager verticalLayout;
  154. ScopedPointer<StretchableLayoutResizerBar> verticalDividerBar;
  155. };
  156. //==============================================================================
  157. Component* createFontsAndTextDemo()
  158. {
  159. return new FontsAndTextDemo();
  160. }