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.

171 lines
5.4KB

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