Audio plugin host https://kx.studio/carla
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.

juce_Label.h 15KB

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