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.

189 lines
7.3KB

  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A PropertyComponent that shows its value as editable text.
  24. @see PropertyComponent
  25. @tags{GUI}
  26. */
  27. class JUCE_API TextPropertyComponent : public PropertyComponent
  28. {
  29. protected:
  30. //==============================================================================
  31. /** Creates a text property component.
  32. @param propertyName The name of the property
  33. @param maxNumChars If not zero, then this specifies the maximum allowable length of
  34. the string. If zero, then the string will have no length limit.
  35. @param isMultiLine Sets whether the text editor allows carriage returns.
  36. @param isEditable Sets whether the text editor is editable. The default is true.
  37. @see TextEditor, setEditable
  38. */
  39. TextPropertyComponent (const String& propertyName,
  40. int maxNumChars,
  41. bool isMultiLine,
  42. bool isEditable = true);
  43. public:
  44. /** Creates a text property component.
  45. @param valueToControl The Value that is controlled by the TextPropertyComponent
  46. @param propertyName The name of the property
  47. @param maxNumChars If not zero, then this specifies the maximum allowable length of
  48. the string. If zero, then the string will have no length limit.
  49. @param isMultiLine Sets whether the text editor allows carriage returns.
  50. @param isEditable Sets whether the text editor is editable. The default is true.
  51. @see TextEditor, setEditable
  52. */
  53. TextPropertyComponent (const Value& valueToControl,
  54. const String& propertyName,
  55. int maxNumChars,
  56. bool isMultiLine,
  57. bool isEditable = true);
  58. /** Creates a text property component with a default value.
  59. @param valueToControl The ValueWithDefault that is controlled by the TextPropertyComponent
  60. @param propertyName The name of the property
  61. @param maxNumChars If not zero, then this specifies the maximum allowable length of
  62. the string. If zero, then the string will have no length limit.
  63. @param isMultiLine Sets whether the text editor allows carriage returns.
  64. @param isEditable Sets whether the text editor is editable. The default is true.
  65. @see TextEditor, setEditable
  66. */
  67. TextPropertyComponent (ValueWithDefault& valueToControl,
  68. const String& propertyName,
  69. int maxNumChars,
  70. bool isMultiLine,
  71. bool isEditable = true);
  72. /** Destructor. */
  73. ~TextPropertyComponent();
  74. //==============================================================================
  75. /** Called when the user edits the text.
  76. Your subclass must use this callback to change the value of whatever item
  77. this property component represents.
  78. */
  79. virtual void setText (const String& newText);
  80. /** Returns the text that should be shown in the text editor. */
  81. virtual String getText() const;
  82. /** Returns the text that should be shown in the text editor as a Value object. */
  83. Value& getValue() const;
  84. //==============================================================================
  85. /** Returns true if the text editor allows carriage returns. */
  86. bool isTextEditorMultiLine() const noexcept { return isMultiLine; }
  87. //==============================================================================
  88. /** A set of colour IDs to use to change the colour of various aspects of the component.
  89. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  90. methods.
  91. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  92. */
  93. enum ColourIds
  94. {
  95. backgroundColourId = 0x100e401, /**< The colour to fill the background of the text area. */
  96. textColourId = 0x100e402, /**< The colour to use for the editable text. */
  97. outlineColourId = 0x100e403, /**< The colour to use to draw an outline around the text area. */
  98. };
  99. void colourChanged() override;
  100. //==============================================================================
  101. /** Used to receive callbacks for text changes */
  102. class JUCE_API Listener
  103. {
  104. public:
  105. /** Destructor. */
  106. virtual ~Listener() {}
  107. /** Called when text has finished being entered (i.e. not per keypress) has changed. */
  108. virtual void textPropertyComponentChanged (TextPropertyComponent*) = 0;
  109. };
  110. /** Registers a listener to receive events when this button's state changes.
  111. If the listener is already registered, this will not register it again.
  112. @see removeListener
  113. */
  114. void addListener (Listener* newListener);
  115. /** Removes a previously-registered button listener
  116. @see addListener
  117. */
  118. void removeListener (Listener* listener);
  119. //==============================================================================
  120. /** Sets whether the text property component can have files dropped onto it by an external application.
  121. The default setting for this is true but you may want to disable this behaviour if you derive
  122. from this class and want your subclass to respond to the file drag.
  123. */
  124. void setInterestedInFileDrag (bool isInterested);
  125. /** Sets whether the text editor is editable. The default setting for this is true. */
  126. void setEditable (bool isEditable);
  127. //==============================================================================
  128. /** @internal */
  129. void refresh() override;
  130. /** @internal */
  131. virtual void textWasEdited();
  132. private:
  133. bool isMultiLine;
  134. class RemapperValueSourceWithDefault;
  135. class LabelComp;
  136. friend class LabelComp;
  137. std::unique_ptr<LabelComp> textEditor;
  138. ListenerList<Listener> listenerList;
  139. void callListeners();
  140. void createEditor (int maxNumChars, bool isEditable);
  141. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPropertyComponent)
  142. };
  143. } // namespace juce