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.

185 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. A PropertyComponent that shows its value as editable text.
  18. @see PropertyComponent
  19. @tags{GUI}
  20. */
  21. class JUCE_API TextPropertyComponent : public PropertyComponent
  22. {
  23. protected:
  24. //==============================================================================
  25. /** Creates a text property component.
  26. @param propertyName The name of the property
  27. @param maxNumChars If not zero, then this specifies the maximum allowable length of
  28. the string. If zero, then the string will have no length limit.
  29. @param isMultiLine Sets whether the text editor allows carriage returns.
  30. @param isEditable Sets whether the text editor is editable. The default is true.
  31. @see TextEditor, setEditable
  32. */
  33. TextPropertyComponent (const String& propertyName,
  34. int maxNumChars,
  35. bool isMultiLine,
  36. bool isEditable = true);
  37. public:
  38. /** Creates a text property component.
  39. @param valueToControl The Value that is controlled by the TextPropertyComponent
  40. @param propertyName The name of the property
  41. @param maxNumChars If not zero, then this specifies the maximum allowable length of
  42. the string. If zero, then the string will have no length limit.
  43. @param isMultiLine Sets whether the text editor allows carriage returns.
  44. @param isEditable Sets whether the text editor is editable. The default is true.
  45. @see TextEditor, setEditable
  46. */
  47. TextPropertyComponent (const Value& valueToControl,
  48. const String& propertyName,
  49. int maxNumChars,
  50. bool isMultiLine,
  51. bool isEditable = true);
  52. /** Creates a text property component with a default value.
  53. @param valueToControl The ValueWithDefault that is controlled by the TextPropertyComponent.
  54. @param propertyName The name of the property
  55. @param maxNumChars If not zero, then this specifies the maximum allowable length of
  56. the string. If zero, then the string will have no length limit.
  57. @param isMultiLine Sets whether the text editor allows carriage returns.
  58. @param isEditable Sets whether the text editor is editable. The default is true.
  59. @see TextEditor, setEditable
  60. */
  61. TextPropertyComponent (ValueWithDefault& valueToControl,
  62. const String& propertyName,
  63. int maxNumChars,
  64. bool isMultiLine,
  65. bool isEditable = true);
  66. /** Destructor. */
  67. ~TextPropertyComponent() override;
  68. //==============================================================================
  69. /** Called when the user edits the text.
  70. Your subclass must use this callback to change the value of whatever item
  71. this property component represents.
  72. */
  73. virtual void setText (const String& newText);
  74. /** Returns the text that should be shown in the text editor. */
  75. virtual String getText() const;
  76. /** Returns the text that should be shown in the text editor as a Value object. */
  77. Value& getValue() const;
  78. //==============================================================================
  79. /** Returns true if the text editor allows carriage returns. */
  80. bool isTextEditorMultiLine() const noexcept { return isMultiLine; }
  81. //==============================================================================
  82. /** A set of colour IDs to use to change the colour of various aspects of the component.
  83. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  84. methods.
  85. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  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. /** Used to receive callbacks for text changes */
  96. class JUCE_API Listener
  97. {
  98. public:
  99. /** Destructor. */
  100. virtual ~Listener() = default;
  101. /** Called when text has finished being entered (i.e. not per keypress) has changed. */
  102. virtual void textPropertyComponentChanged (TextPropertyComponent*) = 0;
  103. };
  104. /** Registers a listener to receive events when this button's state changes.
  105. If the listener is already registered, this will not register it again.
  106. @see removeListener
  107. */
  108. void addListener (Listener* newListener);
  109. /** Removes a previously-registered button listener
  110. @see addListener
  111. */
  112. void removeListener (Listener* listener);
  113. //==============================================================================
  114. /** Sets whether the text property component can have files dropped onto it by an external application.
  115. The default setting for this is true but you may want to disable this behaviour if you derive
  116. from this class and want your subclass to respond to the file drag.
  117. */
  118. void setInterestedInFileDrag (bool isInterested);
  119. /** Sets whether the text editor is editable. The default setting for this is true. */
  120. void setEditable (bool isEditable);
  121. //==============================================================================
  122. /** @internal */
  123. void refresh() override;
  124. /** @internal */
  125. virtual void textWasEdited();
  126. private:
  127. class RemapperValueSourceWithDefault;
  128. class LabelComp;
  129. friend class LabelComp;
  130. //==============================================================================
  131. void callListeners();
  132. void createEditor (int maxNumChars, bool isEditable);
  133. //==============================================================================
  134. bool isMultiLine;
  135. std::unique_ptr<LabelComp> textEditor;
  136. ListenerList<Listener> listenerList;
  137. WeakReference<ValueWithDefault> valueWithDefault;
  138. //==============================================================================
  139. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPropertyComponent)
  140. };
  141. } // namespace juce