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.

167 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - 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. #pragma once
  18. template<typename Type>
  19. class TextWithDefaultPropertyComponent : public PropertyComponent,
  20. private Label::Listener
  21. {
  22. //==========================================================================
  23. class LabelComp : public Label,
  24. public FileDragAndDropTarget
  25. {
  26. public:
  27. LabelComp (TextWithDefaultPropertyComponent& tpc, const int charLimit)
  28. : Label (String(), String()),
  29. owner (tpc),
  30. maxChars (charLimit)
  31. {
  32. setEditable (true, true, false);
  33. addListener (&tpc);
  34. updateColours();
  35. }
  36. bool isInterestedInFileDrag (const StringArray&) override
  37. {
  38. return true;
  39. }
  40. void filesDropped (const StringArray& files, int, int) override
  41. {
  42. setText (getText() + files.joinIntoString (", "), sendNotificationSync);
  43. showEditor();
  44. }
  45. TextEditor* createEditorComponent() override
  46. {
  47. TextEditor* const ed = Label::createEditorComponent();
  48. ed->setInputRestrictions (maxChars);
  49. return ed;
  50. }
  51. void textWasEdited() override
  52. {
  53. owner.textWasEdited();
  54. }
  55. void updateColours()
  56. {
  57. setColour (backgroundColourId, owner.findColour (TextWithDefaultPropertyComponent::backgroundColourId));
  58. setColour (outlineColourId, owner.findColour (TextWithDefaultPropertyComponent::outlineColourId));
  59. setColour (textColourId, owner.findColour (TextWithDefaultPropertyComponent::textColourId));
  60. repaint();
  61. }
  62. private:
  63. TextWithDefaultPropertyComponent& owner;
  64. int maxChars;
  65. };
  66. protected:
  67. //==========================================================================
  68. TextWithDefaultPropertyComponent (const String& propertyName, int maxNumChars)
  69. : PropertyComponent (propertyName)
  70. {
  71. createEditor (maxNumChars);
  72. }
  73. public:
  74. //==========================================================================
  75. TextWithDefaultPropertyComponent (CachedValue<Type>& valueToControl, const String& propertyName, int maxNumChars)
  76. : PropertyComponent (propertyName),
  77. cachedValue (valueToControl)
  78. {
  79. createEditor (maxNumChars);
  80. refresh();
  81. }
  82. virtual String getText() const
  83. {
  84. return cachedValue.get();
  85. }
  86. enum ColourIds
  87. {
  88. backgroundColourId = 0x100e401, /**< The colour to fill the background of the text area. */
  89. textColourId = 0x100e402, /**< The colour to use for the editable text. */
  90. outlineColourId = 0x100e403, /**< The colour to use to draw an outline around the text area. */
  91. };
  92. void colourChanged() override
  93. {
  94. PropertyComponent::colourChanged();
  95. textEditor->updateColours();
  96. }
  97. //==============================================================================
  98. void refresh() override
  99. {
  100. if (cachedValue.isUsingDefault())
  101. setColour (textColourId, Colours::grey);
  102. else
  103. setColour (textColourId, Colours::black);
  104. textEditor->setText (getText(), dontSendNotification);
  105. }
  106. virtual void textWasEdited()
  107. {
  108. String textDisplayed = textEditor->getText();
  109. if (textDisplayed.isEmpty())
  110. cachedValue.resetToDefault();
  111. else
  112. cachedValue = textDisplayed;
  113. refresh();
  114. }
  115. private:
  116. //==============================================================================
  117. friend class LabelComp;
  118. CachedValue<Type>& cachedValue;
  119. ScopedPointer<LabelComp> textEditor;
  120. void createEditor (int maxNumChars)
  121. {
  122. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars));
  123. }
  124. void labelTextChanged (Label*) override {}
  125. void editorShown (Label*, TextEditor& editor) override
  126. {
  127. if (cachedValue.isUsingDefault())
  128. editor.setText (String(), dontSendNotification);
  129. }
  130. void editorHidden (Label*, TextEditor&) override {}
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextWithDefaultPropertyComponent)
  132. };