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.

136 lines
4.0KB

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