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.

197 lines
5.5KB

  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, int charLimit, bool multiline, bool editable)
  26. : Label (String(), String()),
  27. owner (tpc),
  28. maxChars (charLimit),
  29. isMultiline (multiline)
  30. {
  31. setEditable (editable, editable);
  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. auto* 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. int maxNumChars,
  78. bool isMultiLine,
  79. bool isEditable)
  80. : PropertyComponent (name)
  81. {
  82. createEditor (maxNumChars, isMultiLine, isEditable);
  83. }
  84. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  85. const String& name,
  86. int maxNumChars,
  87. bool isMultiLine,
  88. bool isEditable)
  89. : PropertyComponent (name)
  90. {
  91. createEditor (maxNumChars, isMultiLine, isEditable);
  92. textEditor->getTextValue().referTo (valueToControl);
  93. }
  94. TextPropertyComponent::~TextPropertyComponent()
  95. {
  96. }
  97. void TextPropertyComponent::setText (const String& newText)
  98. {
  99. textEditor->setText (newText, sendNotificationSync);
  100. }
  101. String TextPropertyComponent::getText() const
  102. {
  103. return textEditor->getText();
  104. }
  105. Value& TextPropertyComponent::getValue() const
  106. {
  107. return textEditor->getTextValue();
  108. }
  109. void TextPropertyComponent::createEditor (int maxNumChars, bool isMultiLine, bool isEditable)
  110. {
  111. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine, isEditable));
  112. if (isMultiLine)
  113. {
  114. textEditor->setJustificationType (Justification::topLeft);
  115. preferredHeight = 100;
  116. }
  117. }
  118. void TextPropertyComponent::refresh()
  119. {
  120. textEditor->setText (getText(), dontSendNotification);
  121. }
  122. void TextPropertyComponent::textWasEdited()
  123. {
  124. auto newText = textEditor->getText();
  125. if (getText() != newText)
  126. setText (newText);
  127. callListeners();
  128. }
  129. void TextPropertyComponent::addListener (TextPropertyComponentListener* const listener)
  130. {
  131. listenerList.add (listener);
  132. }
  133. void TextPropertyComponent::removeListener (TextPropertyComponentListener* const listener)
  134. {
  135. listenerList.remove (listener);
  136. }
  137. void TextPropertyComponent::callListeners()
  138. {
  139. Component::BailOutChecker checker (this);
  140. listenerList.callChecked (checker, [this] (Listener& l) { l.textPropertyComponentChanged (this); });
  141. }
  142. void TextPropertyComponent::colourChanged()
  143. {
  144. PropertyComponent::colourChanged();
  145. textEditor->updateColours();
  146. }
  147. void TextPropertyComponent::setInterestedInFileDrag (bool isInterested)
  148. {
  149. if (textEditor != nullptr)
  150. textEditor->setInterestedInFileDrag (isInterested);
  151. }
  152. void TextPropertyComponent::setEditable (bool isEditable)
  153. {
  154. if (textEditor != nullptr)
  155. textEditor->setEditable (isEditable, isEditable);
  156. }
  157. } // namespace juce