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 14KB

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