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.

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