juce_Label.h 16 KB

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