juce_TextPropertyComponent.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class TextPropertyComponent::LabelComp : public Label,
  18. public FileDragAndDropTarget
  19. {
  20. public:
  21. LabelComp (TextPropertyComponent& tpc, const int charLimit, const bool multiline)
  22. : Label (String::empty, String::empty),
  23. owner (tpc),
  24. maxChars (charLimit),
  25. isMultiline (multiline)
  26. {
  27. setEditable (true, true, false);
  28. setColour (backgroundColourId, owner.findColour (TextPropertyComponent::backgroundColourId));
  29. setColour (outlineColourId, owner.findColour (TextPropertyComponent::outlineColourId));
  30. setColour (textColourId, owner.findColour (TextPropertyComponent::textColourId));
  31. }
  32. bool isInterestedInFileDrag (const StringArray&) override
  33. {
  34. return true;
  35. }
  36. void filesDropped (const StringArray& files, int, int) override
  37. {
  38. setText (getText() + files.joinIntoString (isMultiline ? "\n" : ", "), sendNotificationSync);
  39. showEditor();
  40. }
  41. TextEditor* createEditorComponent() override
  42. {
  43. TextEditor* const ed = Label::createEditorComponent();
  44. ed->setInputRestrictions (maxChars);
  45. if (isMultiline)
  46. {
  47. ed->setMultiLine (true, true);
  48. ed->setReturnKeyStartsNewLine (true);
  49. }
  50. return ed;
  51. }
  52. void textWasEdited() override
  53. {
  54. owner.textWasEdited();
  55. }
  56. private:
  57. TextPropertyComponent& owner;
  58. int maxChars;
  59. bool isMultiline;
  60. };
  61. //==============================================================================
  62. TextPropertyComponent::TextPropertyComponent (const String& name,
  63. const int maxNumChars,
  64. const bool isMultiLine)
  65. : PropertyComponent (name)
  66. {
  67. createEditor (maxNumChars, isMultiLine);
  68. }
  69. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  70. const String& name,
  71. const int maxNumChars,
  72. const bool isMultiLine)
  73. : PropertyComponent (name)
  74. {
  75. createEditor (maxNumChars, isMultiLine);
  76. textEditor->getTextValue().referTo (valueToControl);
  77. }
  78. TextPropertyComponent::~TextPropertyComponent()
  79. {
  80. }
  81. void TextPropertyComponent::setText (const String& newText)
  82. {
  83. textEditor->setText (newText, sendNotificationSync);
  84. }
  85. String TextPropertyComponent::getText() const
  86. {
  87. return textEditor->getText();
  88. }
  89. void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
  90. {
  91. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine));
  92. if (isMultiLine)
  93. {
  94. textEditor->setJustificationType (Justification::topLeft);
  95. preferredHeight = 100;
  96. }
  97. }
  98. void TextPropertyComponent::refresh()
  99. {
  100. textEditor->setText (getText(), dontSendNotification);
  101. }
  102. void TextPropertyComponent::textWasEdited()
  103. {
  104. const String newText (textEditor->getText());
  105. if (getText() != newText)
  106. setText (newText);
  107. }