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.

184 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. class TextPropertyComponent::LabelComp : public Label,
  20. public FileDragAndDropTarget
  21. {
  22. public:
  23. LabelComp (TextPropertyComponent& tpc, const int charLimit, const bool multiline)
  24. : Label (String(), String()),
  25. owner (tpc),
  26. maxChars (charLimit),
  27. isMultiline (multiline)
  28. {
  29. setEditable (true, true, false);
  30. updateColours();
  31. }
  32. bool isInterestedInFileDrag (const StringArray&) override
  33. {
  34. return interestedInFileDrag;
  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. void updateColours()
  57. {
  58. setColour (backgroundColourId, owner.findColour (TextPropertyComponent::backgroundColourId));
  59. setColour (outlineColourId, owner.findColour (TextPropertyComponent::outlineColourId));
  60. setColour (textColourId, owner.findColour (TextPropertyComponent::textColourId));
  61. repaint();
  62. }
  63. void setInterestedInFileDrag (bool isInterested)
  64. {
  65. interestedInFileDrag = isInterested;
  66. }
  67. private:
  68. TextPropertyComponent& owner;
  69. int maxChars;
  70. bool isMultiline;
  71. bool interestedInFileDrag = true;
  72. };
  73. //==============================================================================
  74. TextPropertyComponent::TextPropertyComponent (const String& name,
  75. const int maxNumChars,
  76. const bool isMultiLine)
  77. : PropertyComponent (name)
  78. {
  79. createEditor (maxNumChars, isMultiLine);
  80. }
  81. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  82. const String& name,
  83. const int maxNumChars,
  84. const bool isMultiLine)
  85. : PropertyComponent (name)
  86. {
  87. createEditor (maxNumChars, isMultiLine);
  88. textEditor->getTextValue().referTo (valueToControl);
  89. }
  90. TextPropertyComponent::~TextPropertyComponent()
  91. {
  92. }
  93. void TextPropertyComponent::setText (const String& newText)
  94. {
  95. textEditor->setText (newText, sendNotificationSync);
  96. }
  97. String TextPropertyComponent::getText() const
  98. {
  99. return textEditor->getText();
  100. }
  101. Value& TextPropertyComponent::getValue() const
  102. {
  103. return textEditor->getTextValue();
  104. }
  105. void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
  106. {
  107. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine));
  108. if (isMultiLine)
  109. {
  110. textEditor->setJustificationType (Justification::topLeft);
  111. preferredHeight = 100;
  112. }
  113. }
  114. void TextPropertyComponent::refresh()
  115. {
  116. textEditor->setText (getText(), dontSendNotification);
  117. }
  118. void TextPropertyComponent::textWasEdited()
  119. {
  120. const String newText (textEditor->getText());
  121. if (getText() != newText)
  122. setText (newText);
  123. callListeners();
  124. }
  125. void TextPropertyComponent::addListener (TextPropertyComponentListener* const listener)
  126. {
  127. listenerList.add (listener);
  128. }
  129. void TextPropertyComponent::removeListener (TextPropertyComponentListener* const listener)
  130. {
  131. listenerList.remove (listener);
  132. }
  133. void TextPropertyComponent::callListeners()
  134. {
  135. Component::BailOutChecker checker (this);
  136. listenerList.callChecked (checker, &TextPropertyComponentListener::textPropertyComponentChanged, this);
  137. }
  138. void TextPropertyComponent::colourChanged()
  139. {
  140. PropertyComponent::colourChanged();
  141. textEditor->updateColours();
  142. }
  143. void TextPropertyComponent::setInterestedInFileDrag (bool isInterested)
  144. {
  145. if (textEditor != nullptr)
  146. textEditor->setInterestedInFileDrag (isInterested);
  147. }