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.

130 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. class TextPropLabel : public Label
  21. {
  22. public:
  23. TextPropLabel (TextPropertyComponent& owner_,
  24. const int maxChars_, const bool isMultiline_)
  25. : Label (String::empty, String::empty),
  26. owner (owner_),
  27. maxChars (maxChars_),
  28. isMultiline (isMultiline_)
  29. {
  30. setEditable (true, true, false);
  31. setColour (backgroundColourId, Colours::white);
  32. setColour (outlineColourId, findColour (ComboBox::outlineColourId));
  33. }
  34. TextEditor* createEditorComponent()
  35. {
  36. TextEditor* const textEditor = Label::createEditorComponent();
  37. textEditor->setInputRestrictions (maxChars);
  38. if (isMultiline)
  39. {
  40. textEditor->setMultiLine (true, true);
  41. textEditor->setReturnKeyStartsNewLine (true);
  42. }
  43. return textEditor;
  44. }
  45. void textWasEdited()
  46. {
  47. owner.textWasEdited();
  48. }
  49. private:
  50. TextPropertyComponent& owner;
  51. int maxChars;
  52. bool isMultiline;
  53. };
  54. //==============================================================================
  55. TextPropertyComponent::TextPropertyComponent (const String& name,
  56. const int maxNumChars,
  57. const bool isMultiLine)
  58. : PropertyComponent (name)
  59. {
  60. createEditor (maxNumChars, isMultiLine);
  61. }
  62. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  63. const String& name,
  64. const int maxNumChars,
  65. const bool isMultiLine)
  66. : PropertyComponent (name)
  67. {
  68. createEditor (maxNumChars, isMultiLine);
  69. textEditor->getTextValue().referTo (valueToControl);
  70. }
  71. TextPropertyComponent::~TextPropertyComponent()
  72. {
  73. }
  74. void TextPropertyComponent::setText (const String& newText)
  75. {
  76. textEditor->setText (newText, true);
  77. }
  78. String TextPropertyComponent::getText() const
  79. {
  80. return textEditor->getText();
  81. }
  82. void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
  83. {
  84. addAndMakeVisible (textEditor = new TextPropLabel (*this, maxNumChars, isMultiLine));
  85. if (isMultiLine)
  86. {
  87. textEditor->setJustificationType (Justification::topLeft);
  88. preferredHeight = 120;
  89. }
  90. }
  91. void TextPropertyComponent::refresh()
  92. {
  93. textEditor->setText (getText(), false);
  94. }
  95. void TextPropertyComponent::textWasEdited()
  96. {
  97. const String newText (textEditor->getText());
  98. if (getText() != newText)
  99. setText (newText);
  100. }
  101. END_JUCE_NAMESPACE