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.

juce_TextPropertyComponent.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. namespace juce
  20. {
  21. class TextPropertyComponent::LabelComp : public Label,
  22. public FileDragAndDropTarget
  23. {
  24. public:
  25. LabelComp (TextPropertyComponent& tpc, const int charLimit, const bool multiline)
  26. : Label (String(), String()),
  27. owner (tpc),
  28. maxChars (charLimit),
  29. isMultiline (multiline)
  30. {
  31. setEditable (true, true, false);
  32. updateColours();
  33. }
  34. bool isInterestedInFileDrag (const StringArray&) override
  35. {
  36. return interestedInFileDrag;
  37. }
  38. void filesDropped (const StringArray& files, int, int) override
  39. {
  40. setText (getText() + files.joinIntoString (isMultiline ? "\n" : ", "), sendNotificationSync);
  41. showEditor();
  42. }
  43. TextEditor* createEditorComponent() override
  44. {
  45. TextEditor* const ed = Label::createEditorComponent();
  46. ed->setInputRestrictions (maxChars);
  47. if (isMultiline)
  48. {
  49. ed->setMultiLine (true, true);
  50. ed->setReturnKeyStartsNewLine (true);
  51. }
  52. return ed;
  53. }
  54. void textWasEdited() override
  55. {
  56. owner.textWasEdited();
  57. }
  58. void updateColours()
  59. {
  60. setColour (backgroundColourId, owner.findColour (TextPropertyComponent::backgroundColourId));
  61. setColour (outlineColourId, owner.findColour (TextPropertyComponent::outlineColourId));
  62. setColour (textColourId, owner.findColour (TextPropertyComponent::textColourId));
  63. repaint();
  64. }
  65. void setInterestedInFileDrag (bool isInterested)
  66. {
  67. interestedInFileDrag = isInterested;
  68. }
  69. private:
  70. TextPropertyComponent& owner;
  71. int maxChars;
  72. bool isMultiline;
  73. bool interestedInFileDrag = true;
  74. };
  75. //==============================================================================
  76. TextPropertyComponent::TextPropertyComponent (const String& name,
  77. const int maxNumChars,
  78. const bool isMultiLine)
  79. : PropertyComponent (name)
  80. {
  81. createEditor (maxNumChars, isMultiLine);
  82. }
  83. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  84. const String& name,
  85. const int maxNumChars,
  86. const bool isMultiLine)
  87. : PropertyComponent (name)
  88. {
  89. createEditor (maxNumChars, isMultiLine);
  90. textEditor->getTextValue().referTo (valueToControl);
  91. }
  92. TextPropertyComponent::~TextPropertyComponent()
  93. {
  94. }
  95. void TextPropertyComponent::setText (const String& newText)
  96. {
  97. textEditor->setText (newText, sendNotificationSync);
  98. }
  99. String TextPropertyComponent::getText() const
  100. {
  101. return textEditor->getText();
  102. }
  103. Value& TextPropertyComponent::getValue() const
  104. {
  105. return textEditor->getTextValue();
  106. }
  107. void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
  108. {
  109. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine));
  110. if (isMultiLine)
  111. {
  112. textEditor->setJustificationType (Justification::topLeft);
  113. preferredHeight = 100;
  114. }
  115. }
  116. void TextPropertyComponent::refresh()
  117. {
  118. textEditor->setText (getText(), dontSendNotification);
  119. }
  120. void TextPropertyComponent::textWasEdited()
  121. {
  122. const String newText (textEditor->getText());
  123. if (getText() != newText)
  124. setText (newText);
  125. callListeners();
  126. }
  127. void TextPropertyComponent::addListener (TextPropertyComponentListener* const listener)
  128. {
  129. listenerList.add (listener);
  130. }
  131. void TextPropertyComponent::removeListener (TextPropertyComponentListener* const listener)
  132. {
  133. listenerList.remove (listener);
  134. }
  135. void TextPropertyComponent::callListeners()
  136. {
  137. Component::BailOutChecker checker (this);
  138. listenerList.callChecked (checker, &TextPropertyComponentListener::textPropertyComponentChanged, this);
  139. }
  140. void TextPropertyComponent::colourChanged()
  141. {
  142. PropertyComponent::colourChanged();
  143. textEditor->updateColours();
  144. }
  145. void TextPropertyComponent::setInterestedInFileDrag (bool isInterested)
  146. {
  147. if (textEditor != nullptr)
  148. textEditor->setInterestedInFileDrag (isInterested);
  149. }
  150. } // namespace juce