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