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.

338 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 __JUCE_LABEL_JUCEHEADER__
  18. #define __JUCE_LABEL_JUCEHEADER__
  19. #include "juce_TextEditor.h"
  20. //==============================================================================
  21. /**
  22. A component that displays a text string, and can optionally become a text
  23. editor when clicked.
  24. */
  25. class JUCE_API Label : public Component,
  26. public SettableTooltipClient,
  27. protected TextEditorListener,
  28. private ComponentListener,
  29. private ValueListener
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a Label.
  34. @param componentName the name to give the component
  35. @param labelText the text to show in the label
  36. */
  37. Label (const String& componentName = String::empty,
  38. const String& labelText = String::empty);
  39. /** Destructor. */
  40. ~Label();
  41. //==============================================================================
  42. /** Changes the label text.
  43. The NotificationType parameter indicates whether to send a change message to
  44. any Label::Listener objects if the new text is different.
  45. */
  46. void setText (const String& newText,
  47. NotificationType notification);
  48. /** Returns the label's current text.
  49. @param returnActiveEditorContents if this is true and the label is currently
  50. being edited, then this method will return the
  51. text as it's being shown in the editor. If false,
  52. then the value returned here won't be updated until
  53. the user has finished typing and pressed the return
  54. key.
  55. */
  56. String getText (bool returnActiveEditorContents = false) const;
  57. /** Returns the text content as a Value object.
  58. You can call Value::referTo() on this object to make the label read and control
  59. a Value object that you supply.
  60. */
  61. Value& getTextValue() { return textValue; }
  62. //==============================================================================
  63. /** Changes the font to use to draw the text.
  64. @see getFont
  65. */
  66. void setFont (const Font& newFont);
  67. /** Returns the font currently being used.
  68. This may be the one set by setFont(), unless it has been overridden by the current LookAndFeel
  69. @see setFont
  70. */
  71. Font getFont() const noexcept;
  72. //==============================================================================
  73. /** A set of colour IDs to use to change the colour of various aspects of the label.
  74. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  75. methods.
  76. Note that you can also use the constants from TextEditor::ColourIds to change the
  77. colour of the text editor that is opened when a label is editable.
  78. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  79. */
  80. enum ColourIds
  81. {
  82. backgroundColourId = 0x1000280, /**< The background colour to fill the label with. */
  83. textColourId = 0x1000281, /**< The colour for the text. */
  84. outlineColourId = 0x1000282 /**< An optional colour to use to draw a border around the label.
  85. Leave this transparent to not have an outline. */
  86. };
  87. //==============================================================================
  88. /** Sets the style of justification to be used for positioning the text.
  89. (The default is Justification::centredLeft)
  90. */
  91. void setJustificationType (const Justification& justification);
  92. /** Returns the type of justification, as set in setJustificationType(). */
  93. Justification getJustificationType() const noexcept { return justification; }
  94. /** Changes the gap that is left between the edge of the component and the text.
  95. By default there's a small gap left at the sides of the component to allow for
  96. the drawing of the border, but you can change this if necessary.
  97. */
  98. void setBorderSize (int horizontalBorder, int verticalBorder);
  99. /** Returns the size of the horizontal gap being left around the text.
  100. */
  101. int getHorizontalBorderSize() const noexcept { return horizontalBorderSize; }
  102. /** Returns the size of the vertical gap being left around the text.
  103. */
  104. int getVerticalBorderSize() const noexcept { return verticalBorderSize; }
  105. /** Makes this label "stick to" another component.
  106. This will cause the label to follow another component around, staying
  107. either to its left or above it.
  108. @param owner the component to follow
  109. @param onLeft if true, the label will stay on the left of its component; if
  110. false, it will stay above it.
  111. */
  112. void attachToComponent (Component* owner, bool onLeft);
  113. /** If this label has been attached to another component using attachToComponent, this
  114. returns the other component.
  115. Returns nullptr if the label is not attached.
  116. */
  117. Component* getAttachedComponent() const;
  118. /** If the label is attached to the left of another component, this returns true.
  119. Returns false if the label is above the other component. This is only relevent if
  120. attachToComponent() has been called.
  121. */
  122. bool isAttachedOnLeft() const noexcept { return leftOfOwnerComp; }
  123. /** Specifies the minimum amount that the font can be squashed horizantally before it starts
  124. using ellipsis.
  125. @see Graphics::drawFittedText
  126. */
  127. void setMinimumHorizontalScale (float newScale);
  128. float getMinimumHorizontalScale() const noexcept { return minimumHorizontalScale; }
  129. //==============================================================================
  130. /**
  131. A class for receiving events from a Label.
  132. You can register a Label::Listener with a Label using the Label::addListener()
  133. method, and it will be called when the text of the label changes, either because
  134. of a call to Label::setText() or by the user editing the text (if the label is
  135. editable).
  136. @see Label::addListener, Label::removeListener
  137. */
  138. class JUCE_API Listener
  139. {
  140. public:
  141. /** Destructor. */
  142. virtual ~Listener() {}
  143. /** Called when a Label's text has changed. */
  144. virtual void labelTextChanged (Label* labelThatHasChanged) = 0;
  145. };
  146. /** Registers a listener that will be called when the label's text changes. */
  147. void addListener (Listener* listener);
  148. /** Deregisters a previously-registered listener. */
  149. void removeListener (Listener* listener);
  150. //==============================================================================
  151. /** Makes the label turn into a TextEditor when clicked.
  152. By default this is turned off.
  153. If turned on, then single- or double-clicking will turn the label into
  154. an editor. If the user then changes the text, then the ChangeBroadcaster
  155. base class will be used to send change messages to any listeners that
  156. have registered.
  157. If the user changes the text, the textWasEdited() method will be called
  158. afterwards, and subclasses can override this if they need to do anything
  159. special.
  160. @param editOnSingleClick if true, just clicking once on the label will start editing the text
  161. @param editOnDoubleClick if true, a double-click is needed to start editing
  162. @param lossOfFocusDiscardsChanges if true, clicking somewhere else while the text is being
  163. edited will discard any changes; if false, then this will
  164. commit the changes.
  165. @see showEditor, setEditorColours, TextEditor
  166. */
  167. void setEditable (bool editOnSingleClick,
  168. bool editOnDoubleClick = false,
  169. bool lossOfFocusDiscardsChanges = false);
  170. /** Returns true if this option was set using setEditable(). */
  171. bool isEditableOnSingleClick() const noexcept { return editSingleClick; }
  172. /** Returns true if this option was set using setEditable(). */
  173. bool isEditableOnDoubleClick() const noexcept { return editDoubleClick; }
  174. /** Returns true if this option has been set in a call to setEditable(). */
  175. bool doesLossOfFocusDiscardChanges() const noexcept { return lossOfFocusDiscardsChanges; }
  176. /** Returns true if the user can edit this label's text. */
  177. bool isEditable() const noexcept { return editSingleClick || editDoubleClick; }
  178. /** Makes the editor appear as if the label had been clicked by the user.
  179. @see textWasEdited, setEditable
  180. */
  181. void showEditor();
  182. /** Hides the editor if it was being shown.
  183. @param discardCurrentEditorContents if true, the label's text will be
  184. reset to whatever it was before the editor
  185. was shown; if false, the current contents of the
  186. editor will be used to set the label's text
  187. before it is hidden.
  188. */
  189. void hideEditor (bool discardCurrentEditorContents);
  190. /** Returns true if the editor is currently focused and active. */
  191. bool isBeingEdited() const noexcept;
  192. /** Returns the currently-visible text editor, or nullptr if none is open. */
  193. TextEditor* getCurrentTextEditor() const noexcept;
  194. protected:
  195. //==============================================================================
  196. /** Creates the TextEditor component that will be used when the user has clicked on the label.
  197. Subclasses can override this if they need to customise this component in some way.
  198. */
  199. virtual TextEditor* createEditorComponent();
  200. /** Called after the user changes the text. */
  201. virtual void textWasEdited();
  202. /** Called when the text has been altered. */
  203. virtual void textWasChanged();
  204. /** Called when the text editor has just appeared, due to a user click or other focus change. */
  205. virtual void editorShown (TextEditor*);
  206. /** Called when the text editor is going to be deleted, after editing has finished. */
  207. virtual void editorAboutToBeHidden (TextEditor*);
  208. //==============================================================================
  209. /** @internal */
  210. void paint (Graphics&) override;
  211. /** @internal */
  212. void resized() override;
  213. /** @internal */
  214. void mouseUp (const MouseEvent&) override;
  215. /** @internal */
  216. void mouseDoubleClick (const MouseEvent&) override;
  217. /** @internal */
  218. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  219. /** @internal */
  220. void componentParentHierarchyChanged (Component&) override;
  221. /** @internal */
  222. void componentVisibilityChanged (Component&) override;
  223. /** @internal */
  224. void inputAttemptWhenModal() override;
  225. /** @internal */
  226. void focusGained (FocusChangeType) override;
  227. /** @internal */
  228. void enablementChanged() override;
  229. /** @internal */
  230. KeyboardFocusTraverser* createFocusTraverser() override;
  231. /** @internal */
  232. void textEditorTextChanged (TextEditor&) override;
  233. /** @internal */
  234. void textEditorReturnKeyPressed (TextEditor&) override;
  235. /** @internal */
  236. void textEditorEscapeKeyPressed (TextEditor&) override;
  237. /** @internal */
  238. void textEditorFocusLost (TextEditor&) override;
  239. /** @internal */
  240. void colourChanged() override;
  241. /** @internal */
  242. void valueChanged (Value&) override;
  243. /** @internal */
  244. void callChangeListeners();
  245. private:
  246. //==============================================================================
  247. Value textValue;
  248. String lastTextValue;
  249. Font font;
  250. Justification justification;
  251. ScopedPointer<TextEditor> editor;
  252. ListenerList<Listener> listeners;
  253. WeakReference<Component> ownerComponent;
  254. int horizontalBorderSize, verticalBorderSize;
  255. float minimumHorizontalScale;
  256. bool editSingleClick : 1;
  257. bool editDoubleClick : 1;
  258. bool lossOfFocusDiscardsChanges : 1;
  259. bool leftOfOwnerComp : 1;
  260. bool updateFromTextEditorContents (TextEditor&);
  261. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Label)
  262. };
  263. /** This typedef is just for compatibility with old code - newer code should use the Label::Listener class directly. */
  264. typedef Label::Listener LabelListener;
  265. #endif // __JUCE_LABEL_JUCEHEADER__