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
9.4KB

  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. Array<Font> fonts;
  26. ListBox* listBox;
  27. TextEditor* textBox;
  28. ToggleButton* boldButton;
  29. ToggleButton* italicButton;
  30. Slider* sizeSlider;
  31. Slider* kerningSlider;
  32. Slider* horizontalScaleSlider;
  33. StretchableLayoutManager verticalLayout;
  34. StretchableLayoutManager horizontalLayout;
  35. StretchableLayoutResizerBar* verticalDividerBar;
  36. public:
  37. //==============================================================================
  38. FontsAndTextDemo()
  39. {
  40. setName (T("Fonts"));
  41. Font::findFonts (fonts);
  42. addAndMakeVisible (listBox = new ListBox (T("fonts"), this));
  43. listBox->setRowHeight (28);
  44. addAndMakeVisible (textBox = new TextEditor());
  45. textBox->setColour (TextEditor::backgroundColourId, Colours::white);
  46. textBox->setColour (TextEditor::outlineColourId, Colours::black.withAlpha (0.5f));
  47. textBox->setMultiLine (true, true);
  48. textBox->setReturnKeyStartsNewLine (true);
  49. textBox->setText (T("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"));
  50. addAndMakeVisible (boldButton = new ToggleButton (T("bold")));
  51. boldButton->addButtonListener (this);
  52. addAndMakeVisible (italicButton = new ToggleButton (T("italic")));
  53. italicButton->addButtonListener (this);
  54. addAndMakeVisible (sizeSlider = new Slider ("Size"));
  55. sizeSlider->setRange (3.0, 150.0, 0.1);
  56. sizeSlider->setValue (20.0);
  57. sizeSlider->addListener (this);
  58. (new Label (String::empty, sizeSlider->getName()))->attachToComponent (sizeSlider, true);
  59. addAndMakeVisible (kerningSlider = new Slider ("Kerning"));
  60. kerningSlider->setRange (-1.0, 1.0, 0.01);
  61. kerningSlider->setValue (0.0);
  62. kerningSlider->addListener (this);
  63. (new Label (String::empty, kerningSlider->getName()))->attachToComponent (kerningSlider, true);
  64. addAndMakeVisible (horizontalScaleSlider = new Slider ("Stretch"));
  65. horizontalScaleSlider->setRange (0.1, 4.0, 0.01);
  66. horizontalScaleSlider->setValue (1.0);
  67. horizontalScaleSlider->addListener (this);
  68. (new Label (String::empty, horizontalScaleSlider->getName()))->attachToComponent (horizontalScaleSlider, true);
  69. listBox->setColour (ListBox::outlineColourId, Colours::black.withAlpha (0.5f));
  70. listBox->setOutlineThickness (1);
  71. listBox->selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  72. // set up the layout and resizer bars..
  73. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.5); // width of the font list must be
  74. // between 20% and 80%, preferably 50%
  75. verticalLayout.setItemLayout (1, 8, 8, 8); // the vertical divider drag-bar thing is always 8 pixels wide
  76. verticalLayout.setItemLayout (2, 150, -1.0, -0.5); // the components on the right must be
  77. // at least 150 pixels wide, preferably 50% of the total width
  78. verticalDividerBar = new StretchableLayoutResizerBar (&verticalLayout, 1, true);
  79. addAndMakeVisible (verticalDividerBar);
  80. horizontalLayout.setItemLayout (0, -0.2, -1.0, -0.4); // height of the font text box must be
  81. // between 20% and 100%, preferably 40%
  82. horizontalLayout.setItemLayout (1, 8, 8, 8); // the horizontal divider drag-bar thing is always 8 pixels high
  83. horizontalLayout.setItemLayout (2, 2, 5, 5); // a gap between the controls
  84. horizontalLayout.setItemLayout (3, 15, 20, 20); // the italic button would like to be 20 pixels high
  85. horizontalLayout.setItemLayout (4, 2, 5, 5); // a gap between the controls
  86. horizontalLayout.setItemLayout (5, 15, 20, 20); // the bold button would like to be 20 pixels high
  87. horizontalLayout.setItemLayout (6, 2, 5, 5); // a gap between the controls
  88. horizontalLayout.setItemLayout (7, 15, 20, 20); // the italic button would like to be 20 pixels high
  89. horizontalLayout.setItemLayout (8, 2, 5, 5); // a gap between the controls
  90. horizontalLayout.setItemLayout (9, 15, 20, 20); // the copy code button would like to be 20 pixels high
  91. horizontalLayout.setItemLayout (10, 5, -1.0, 5); // add a gap at the bottom that will fill up any
  92. // space left over - this will stop the
  93. // sliders from always sticking to the
  94. // bottom of the window
  95. }
  96. ~FontsAndTextDemo()
  97. {
  98. deleteAllChildren();
  99. }
  100. void resized()
  101. {
  102. // lay out the list box and vertical divider..
  103. Component* vcomps[] = { listBox, verticalDividerBar, 0 };
  104. verticalLayout.layOutComponents (vcomps, 3,
  105. 4, 4, getWidth() - 8, getHeight() - 8,
  106. false, // lay out side-by-side
  107. true); // resize the components' heights as well as widths
  108. // now lay out the text box and the controls below it..
  109. int x = verticalLayout.getItemCurrentPosition (2) + 4;
  110. textBox->setBounds (x, 0, getWidth() - x, getHeight() - 110);
  111. x += 70;
  112. sizeSlider->setBounds (x, getHeight() - 106, getWidth() - x, 22);
  113. kerningSlider->setBounds (x, getHeight() - 82, getWidth() - x, 22);
  114. horizontalScaleSlider->setBounds (x, getHeight() - 58, getWidth() - x, 22);
  115. boldButton->setBounds (x, getHeight() - 34, (getWidth() - x) / 2, 22);
  116. italicButton->setBounds (x + (getWidth() - x) / 2, getHeight() - 34, (getWidth() - x) / 2, 22);
  117. }
  118. // implements the ListBoxModel method
  119. int getNumRows()
  120. {
  121. return fonts.size();
  122. }
  123. // implements the ListBoxModel method
  124. void paintListBoxItem (int rowNumber,
  125. Graphics& g,
  126. int width, int height,
  127. bool rowIsSelected)
  128. {
  129. if (rowIsSelected)
  130. g.fillAll (Colours::lightblue);
  131. Font font (fonts [rowNumber]);
  132. font.setHeight (height * 0.7f);
  133. g.setFont (font);
  134. g.setColour (Colours::black);
  135. g.drawText (font.getTypefaceName(),
  136. 4, 0, width - 4, height,
  137. Justification::centredLeft, true);
  138. int x = jmax (0, font.getStringWidth (font.getTypefaceName())) + 12;
  139. g.setFont (Font (11.0f, Font::italic));
  140. g.setColour (Colours::grey);
  141. g.drawText (font.getTypefaceName(),
  142. x, 0, width - x - 2, height,
  143. Justification::centredLeft, true);
  144. }
  145. void updatePreviewBoxText()
  146. {
  147. Font font (fonts [listBox->getSelectedRow()]);
  148. font.setHeight ((float) sizeSlider->getValue());
  149. font.setBold (boldButton->getToggleState());
  150. font.setItalic (italicButton->getToggleState());
  151. font.setExtraKerningFactor ((float) kerningSlider->getValue());
  152. font.setHorizontalScale ((float) horizontalScaleSlider->getValue());
  153. textBox->applyFontToAllText (font);
  154. }
  155. void selectedRowsChanged (int lastRowselected)
  156. {
  157. updatePreviewBoxText();
  158. }
  159. void buttonClicked (Button* button)
  160. {
  161. updatePreviewBoxText();
  162. }
  163. void sliderValueChanged (Slider*)
  164. {
  165. // (this is called when the size slider is moved)
  166. updatePreviewBoxText();
  167. }
  168. };
  169. //==============================================================================
  170. Component* createFontsAndTextDemo()
  171. {
  172. return new FontsAndTextDemo();
  173. }