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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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() noexcept { 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. backgroundWhenEditingColourId = 0x1000283, /**< The background colour when the label is being edited. */
  86. textWhenEditingColourId = 0x1000284, /**< The colour for the text when the label is being edited. */
  87. outlineWhenEditingColourId = 0x1000285 /**< An optional border colour when the label is being edited. */
  88. };
  89. //==============================================================================
  90. /** Sets the style of justification to be used for positioning the text.
  91. (The default is Justification::centredLeft)
  92. */
  93. void setJustificationType (Justification justification);
  94. /** Returns the type of justification, as set in setJustificationType(). */
  95. Justification getJustificationType() const noexcept { return justification; }
  96. /** Changes the border that is left between the edge of the component and the text.
  97. By default there's a small gap left at the sides of the component to allow for
  98. the drawing of the border, but you can change this if necessary.
  99. */
  100. void setBorderSize (BorderSize<int> newBorderSize);
  101. /** Returns the size of the border to be left around the text. */
  102. BorderSize<int> getBorderSize() const noexcept { return border; }
  103. /** Makes this label "stick to" another component.
  104. This will cause the label to follow another component around, staying
  105. either to its left or above it.
  106. @param owner the component to follow
  107. @param onLeft if true, the label will stay on the left of its component; if
  108. false, it will stay above it.
  109. */
  110. void attachToComponent (Component* owner, bool onLeft);
  111. /** If this label has been attached to another component using attachToComponent, this
  112. returns the other component.
  113. Returns nullptr if the label is not attached.
  114. */
  115. Component* getAttachedComponent() const;
  116. /** If the label is attached to the left of another component, this returns true.
  117. Returns false if the label is above the other component. This is only relevent if
  118. attachToComponent() has been called.
  119. */
  120. bool isAttachedOnLeft() const noexcept { return leftOfOwnerComp; }
  121. /** Specifies the minimum amount that the font can be squashed horizontally before it starts
  122. using ellipsis.
  123. @see Graphics::drawFittedText
  124. */
  125. void setMinimumHorizontalScale (float newScale);
  126. /** Specifies the amount that the font can be squashed horizontally. */
  127. float getMinimumHorizontalScale() const noexcept { return minimumHorizontalScale; }
  128. //==============================================================================
  129. /**
  130. A class for receiving events from a Label.
  131. You can register a Label::Listener with a Label using the Label::addListener()
  132. method, and it will be called when the text of the label changes, either because
  133. of a call to Label::setText() or by the user editing the text (if the label is
  134. editable).
  135. @see Label::addListener, Label::removeListener
  136. */
  137. class JUCE_API Listener
  138. {
  139. public:
  140. /** Destructor. */
  141. virtual ~Listener() {}
  142. /** Called when a Label's text has changed. */
  143. virtual void labelTextChanged (Label* labelThatHasChanged) = 0;
  144. /** Called when a Label goes into editing mode and displays a TextEditor. */
  145. virtual void editorShown (Label*, TextEditor& textEditorShown);
  146. };
  147. /** Registers a listener that will be called when the label's text changes. */
  148. void addListener (Listener* listener);
  149. /** Deregisters a previously-registered listener. */
  150. void removeListener (Listener* listener);
  151. //==============================================================================
  152. /** Makes the label turn into a TextEditor when clicked.
  153. By default this is turned off.
  154. If turned on, then single- or double-clicking will turn the label into
  155. an editor. If the user then changes the text, then the ChangeBroadcaster
  156. base class will be used to send change messages to any listeners that
  157. have registered.
  158. If the user changes the text, the textWasEdited() method will be called
  159. afterwards, and subclasses can override this if they need to do anything
  160. special.
  161. @param editOnSingleClick if true, just clicking once on the label will start editing the text
  162. @param editOnDoubleClick if true, a double-click is needed to start editing
  163. @param lossOfFocusDiscardsChanges if true, clicking somewhere else while the text is being
  164. edited will discard any changes; if false, then this will
  165. commit the changes.
  166. @see showEditor, setEditorColours, TextEditor
  167. */
  168. void setEditable (bool editOnSingleClick,
  169. bool editOnDoubleClick = false,
  170. bool lossOfFocusDiscardsChanges = false);
  171. /** Returns true if this option was set using setEditable(). */
  172. bool isEditableOnSingleClick() const noexcept { return editSingleClick; }
  173. /** Returns true if this option was set using setEditable(). */
  174. bool isEditableOnDoubleClick() const noexcept { return editDoubleClick; }
  175. /** Returns true if this option has been set in a call to setEditable(). */
  176. bool doesLossOfFocusDiscardChanges() const noexcept { return lossOfFocusDiscardsChanges; }
  177. /** Returns true if the user can edit this label's text. */
  178. bool isEditable() const noexcept { return editSingleClick || editDoubleClick; }
  179. /** Makes the editor appear as if the label had been clicked by the user.
  180. @see textWasEdited, setEditable
  181. */
  182. void showEditor();
  183. /** Hides the editor if it was being shown.
  184. @param discardCurrentEditorContents if true, the label's text will be
  185. reset to whatever it was before the editor
  186. was shown; if false, the current contents of the
  187. editor will be used to set the label's text
  188. before it is hidden.
  189. */
  190. void hideEditor (bool discardCurrentEditorContents);
  191. /** Returns true if the editor is currently focused and active. */
  192. bool isBeingEdited() const noexcept;
  193. /** Returns the currently-visible text editor, or nullptr if none is open. */
  194. TextEditor* getCurrentTextEditor() const noexcept;
  195. //==============================================================================
  196. /** This abstract base class is implemented by LookAndFeel classes to provide
  197. label drawing functionality.
  198. */
  199. struct JUCE_API LookAndFeelMethods
  200. {
  201. virtual ~LookAndFeelMethods() {}
  202. virtual void drawLabel (Graphics&, Label&) = 0;
  203. virtual Font getLabelFont (Label&) = 0;
  204. };
  205. protected:
  206. //==============================================================================
  207. /** Creates the TextEditor component that will be used when the user has clicked on the label.
  208. Subclasses can override this if they need to customise this component in some way.
  209. */
  210. virtual TextEditor* createEditorComponent();
  211. /** Called after the user changes the text. */
  212. virtual void textWasEdited();
  213. /** Called when the text has been altered. */
  214. virtual void textWasChanged();
  215. /** Called when the text editor has just appeared, due to a user click or other focus change. */
  216. virtual void editorShown (TextEditor*);
  217. /** Called when the text editor is going to be deleted, after editing has finished. */
  218. virtual void editorAboutToBeHidden (TextEditor*);
  219. //==============================================================================
  220. /** @internal */
  221. void paint (Graphics&) override;
  222. /** @internal */
  223. void resized() override;
  224. /** @internal */
  225. void mouseUp (const MouseEvent&) override;
  226. /** @internal */
  227. void mouseDoubleClick (const MouseEvent&) override;
  228. /** @internal */
  229. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  230. /** @internal */
  231. void componentParentHierarchyChanged (Component&) override;
  232. /** @internal */
  233. void componentVisibilityChanged (Component&) override;
  234. /** @internal */
  235. void inputAttemptWhenModal() override;
  236. /** @internal */
  237. void focusGained (FocusChangeType) override;
  238. /** @internal */
  239. void enablementChanged() override;
  240. /** @internal */
  241. KeyboardFocusTraverser* createFocusTraverser() override;
  242. /** @internal */
  243. void textEditorTextChanged (TextEditor&) override;
  244. /** @internal */
  245. void textEditorReturnKeyPressed (TextEditor&) override;
  246. /** @internal */
  247. void textEditorEscapeKeyPressed (TextEditor&) override;
  248. /** @internal */
  249. void textEditorFocusLost (TextEditor&) override;
  250. /** @internal */
  251. void colourChanged() override;
  252. /** @internal */
  253. void valueChanged (Value&) override;
  254. /** @internal */
  255. void callChangeListeners();
  256. private:
  257. //==============================================================================
  258. Value textValue;
  259. String lastTextValue;
  260. Font font;
  261. Justification justification;
  262. ScopedPointer<TextEditor> editor;
  263. ListenerList<Listener> listeners;
  264. WeakReference<Component> ownerComponent;
  265. BorderSize<int> border;
  266. float minimumHorizontalScale;
  267. bool editSingleClick;
  268. bool editDoubleClick;
  269. bool lossOfFocusDiscardsChanges;
  270. bool leftOfOwnerComp;
  271. bool updateFromTextEditorContents (TextEditor&);
  272. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Label)
  273. };
  274. /** This typedef is just for compatibility with old code - newer code should use the Label::Listener class directly. */
  275. typedef Label::Listener LabelListener;
  276. #endif // JUCE_LABEL_H_INCLUDED