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.

172 lines
4.8KB

  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 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. 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. private:
  64. TextPropertyComponent& owner;
  65. int maxChars;
  66. bool isMultiline;
  67. };
  68. //==============================================================================
  69. TextPropertyComponent::TextPropertyComponent (const String& name,
  70. const int maxNumChars,
  71. const bool isMultiLine)
  72. : PropertyComponent (name)
  73. {
  74. createEditor (maxNumChars, isMultiLine);
  75. }
  76. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  77. const String& name,
  78. const int maxNumChars,
  79. const bool isMultiLine)
  80. : PropertyComponent (name)
  81. {
  82. createEditor (maxNumChars, isMultiLine);
  83. textEditor->getTextValue().referTo (valueToControl);
  84. }
  85. TextPropertyComponent::~TextPropertyComponent()
  86. {
  87. }
  88. void TextPropertyComponent::setText (const String& newText)
  89. {
  90. textEditor->setText (newText, sendNotificationSync);
  91. }
  92. String TextPropertyComponent::getText() const
  93. {
  94. return textEditor->getText();
  95. }
  96. Value& TextPropertyComponent::getValue() const
  97. {
  98. return textEditor->getTextValue();
  99. }
  100. void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
  101. {
  102. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine));
  103. if (isMultiLine)
  104. {
  105. textEditor->setJustificationType (Justification::topLeft);
  106. preferredHeight = 100;
  107. }
  108. }
  109. void TextPropertyComponent::refresh()
  110. {
  111. textEditor->setText (getText(), dontSendNotification);
  112. }
  113. void TextPropertyComponent::textWasEdited()
  114. {
  115. const String newText (textEditor->getText());
  116. if (getText() != newText)
  117. setText (newText);
  118. callListeners();
  119. }
  120. void TextPropertyComponent::addListener (TextPropertyComponentListener* const listener)
  121. {
  122. listenerList.add (listener);
  123. }
  124. void TextPropertyComponent::removeListener (TextPropertyComponentListener* const listener)
  125. {
  126. listenerList.remove (listener);
  127. }
  128. void TextPropertyComponent::callListeners()
  129. {
  130. Component::BailOutChecker checker (this);
  131. listenerList.callChecked (checker, &TextPropertyComponentListener::textPropertyComponentChanged, this);
  132. }
  133. void TextPropertyComponent::colourChanged()
  134. {
  135. PropertyComponent::colourChanged();
  136. textEditor->updateColours();
  137. }