Audio plugin host https://kx.studio/carla
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.

170 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. updateColours();
  29. }
  30. bool isInterestedInFileDrag (const StringArray&) override
  31. {
  32. return true;
  33. }
  34. void filesDropped (const StringArray& files, int, int) override
  35. {
  36. setText (getText() + files.joinIntoString (isMultiline ? "\n" : ", "), sendNotificationSync);
  37. showEditor();
  38. }
  39. TextEditor* createEditorComponent() override
  40. {
  41. TextEditor* const ed = Label::createEditorComponent();
  42. ed->setInputRestrictions (maxChars);
  43. if (isMultiline)
  44. {
  45. ed->setMultiLine (true, true);
  46. ed->setReturnKeyStartsNewLine (true);
  47. }
  48. return ed;
  49. }
  50. void textWasEdited() override
  51. {
  52. owner.textWasEdited();
  53. }
  54. void updateColours()
  55. {
  56. setColour (backgroundColourId, owner.findColour (TextPropertyComponent::backgroundColourId));
  57. setColour (outlineColourId, owner.findColour (TextPropertyComponent::outlineColourId));
  58. setColour (textColourId, owner.findColour (TextPropertyComponent::textColourId));
  59. repaint();
  60. }
  61. private:
  62. TextPropertyComponent& owner;
  63. int maxChars;
  64. bool isMultiline;
  65. };
  66. //==============================================================================
  67. TextPropertyComponent::TextPropertyComponent (const String& name,
  68. const int maxNumChars,
  69. const bool isMultiLine)
  70. : PropertyComponent (name)
  71. {
  72. createEditor (maxNumChars, isMultiLine);
  73. }
  74. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  75. const String& name,
  76. const int maxNumChars,
  77. const bool isMultiLine)
  78. : PropertyComponent (name)
  79. {
  80. createEditor (maxNumChars, isMultiLine);
  81. textEditor->getTextValue().referTo (valueToControl);
  82. }
  83. TextPropertyComponent::~TextPropertyComponent()
  84. {
  85. }
  86. void TextPropertyComponent::setText (const String& newText)
  87. {
  88. textEditor->setText (newText, sendNotificationSync);
  89. }
  90. String TextPropertyComponent::getText() const
  91. {
  92. return textEditor->getText();
  93. }
  94. Value& TextPropertyComponent::getValue() const
  95. {
  96. return textEditor->getTextValue();
  97. }
  98. void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
  99. {
  100. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine));
  101. if (isMultiLine)
  102. {
  103. textEditor->setJustificationType (Justification::topLeft);
  104. preferredHeight = 100;
  105. }
  106. }
  107. void TextPropertyComponent::refresh()
  108. {
  109. textEditor->setText (getText(), dontSendNotification);
  110. }
  111. void TextPropertyComponent::textWasEdited()
  112. {
  113. const String newText (textEditor->getText());
  114. if (getText() != newText)
  115. setText (newText);
  116. callListeners();
  117. }
  118. void TextPropertyComponent::addListener (TextPropertyComponentListener* const listener)
  119. {
  120. listenerList.add (listener);
  121. }
  122. void TextPropertyComponent::removeListener (TextPropertyComponentListener* const listener)
  123. {
  124. listenerList.remove (listener);
  125. }
  126. void TextPropertyComponent::callListeners()
  127. {
  128. Component::BailOutChecker checker (this);
  129. listenerList.callChecked (checker, &TextPropertyComponentListener::textPropertyComponentChanged, this);
  130. }
  131. void TextPropertyComponent::colourChanged()
  132. {
  133. PropertyComponent::colourChanged();
  134. textEditor->updateColours();
  135. }