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.

170 lines
5.4KB

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