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
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. Use a value of 0 for a default value.
  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. /** Set a keyboard type for use when the text editor is shown. */
  129. void setKeyboardType (TextInputTarget::VirtualKeyboardType type) noexcept { keyboardType = type; }
  130. //==============================================================================
  131. /**
  132. A class for receiving events from a Label.
  133. You can register a Label::Listener with a Label using the Label::addListener()
  134. method, and it will be called when the text of the label changes, either because
  135. of a call to Label::setText() or by the user editing the text (if the label is
  136. editable).
  137. @see Label::addListener, Label::removeListener
  138. */
  139. class JUCE_API Listener
  140. {
  141. public:
  142. /** Destructor. */
  143. virtual ~Listener() {}
  144. /** Called when a Label's text has changed. */
  145. virtual void labelTextChanged (Label* labelThatHasChanged) = 0;
  146. /** Called when a Label goes into editing mode and displays a TextEditor. */
  147. virtual void editorShown (Label*, TextEditor&) {}
  148. /** Called when a Label is about to delete its TextEditor and exit editing mode. */
  149. virtual void editorHidden (Label*, TextEditor&) {}
  150. };
  151. /** Registers a listener that will be called when the label's text changes. */
  152. void addListener (Listener* listener);
  153. /** Deregisters a previously-registered listener. */
  154. void removeListener (Listener* listener);
  155. //==============================================================================
  156. /** Makes the label turn into a TextEditor when clicked.
  157. By default this is turned off.
  158. If turned on, then single- or double-clicking will turn the label into
  159. an editor. If the user then changes the text, then the ChangeBroadcaster
  160. base class will be used to send change messages to any listeners that
  161. have registered.
  162. If the user changes the text, the textWasEdited() method will be called
  163. afterwards, and subclasses can override this if they need to do anything
  164. special.
  165. @param editOnSingleClick if true, just clicking once on the label will start editing the text
  166. @param editOnDoubleClick if true, a double-click is needed to start editing
  167. @param lossOfFocusDiscardsChanges if true, clicking somewhere else while the text is being
  168. edited will discard any changes; if false, then this will
  169. commit the changes.
  170. @see showEditor, setEditorColours, TextEditor
  171. */
  172. void setEditable (bool editOnSingleClick,
  173. bool editOnDoubleClick = false,
  174. bool lossOfFocusDiscardsChanges = false);
  175. /** Returns true if this option was set using setEditable(). */
  176. bool isEditableOnSingleClick() const noexcept { return editSingleClick; }
  177. /** Returns true if this option was set using setEditable(). */
  178. bool isEditableOnDoubleClick() const noexcept { return editDoubleClick; }
  179. /** Returns true if this option has been set in a call to setEditable(). */
  180. bool doesLossOfFocusDiscardChanges() const noexcept { return lossOfFocusDiscardsChanges; }
  181. /** Returns true if the user can edit this label's text. */
  182. bool isEditable() const noexcept { return editSingleClick || editDoubleClick; }
  183. /** Makes the editor appear as if the label had been clicked by the user.
  184. @see textWasEdited, setEditable
  185. */
  186. void showEditor();
  187. /** Hides the editor if it was being shown.
  188. @param discardCurrentEditorContents if true, the label's text will be
  189. reset to whatever it was before the editor
  190. was shown; if false, the current contents of the
  191. editor will be used to set the label's text
  192. before it is hidden.
  193. */
  194. void hideEditor (bool discardCurrentEditorContents);
  195. /** Returns true if the editor is currently focused and active. */
  196. bool isBeingEdited() const noexcept;
  197. /** Returns the currently-visible text editor, or nullptr if none is open. */
  198. TextEditor* getCurrentTextEditor() const noexcept;
  199. //==============================================================================
  200. /** This abstract base class is implemented by LookAndFeel classes to provide
  201. label drawing functionality.
  202. */
  203. struct JUCE_API LookAndFeelMethods
  204. {
  205. virtual ~LookAndFeelMethods() {}
  206. virtual void drawLabel (Graphics&, Label&) = 0;
  207. virtual Font getLabelFont (Label&) = 0;
  208. };
  209. protected:
  210. //==============================================================================
  211. /** Creates the TextEditor component that will be used when the user has clicked on the label.
  212. Subclasses can override this if they need to customise this component in some way.
  213. */
  214. virtual TextEditor* createEditorComponent();
  215. /** Called after the user changes the text. */
  216. virtual void textWasEdited();
  217. /** Called when the text has been altered. */
  218. virtual void textWasChanged();
  219. /** Called when the text editor has just appeared, due to a user click or other focus change. */
  220. virtual void editorShown (TextEditor*);
  221. /** Called when the text editor is going to be deleted, after editing has finished. */
  222. virtual void editorAboutToBeHidden (TextEditor*);
  223. //==============================================================================
  224. /** @internal */
  225. void paint (Graphics&) override;
  226. /** @internal */
  227. void resized() override;
  228. /** @internal */
  229. void mouseUp (const MouseEvent&) override;
  230. /** @internal */
  231. void mouseDoubleClick (const MouseEvent&) override;
  232. /** @internal */
  233. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  234. /** @internal */
  235. void componentParentHierarchyChanged (Component&) override;
  236. /** @internal */
  237. void componentVisibilityChanged (Component&) override;
  238. /** @internal */
  239. void inputAttemptWhenModal() override;
  240. /** @internal */
  241. void focusGained (FocusChangeType) override;
  242. /** @internal */
  243. void enablementChanged() override;
  244. /** @internal */
  245. KeyboardFocusTraverser* createFocusTraverser() override;
  246. /** @internal */
  247. void textEditorTextChanged (TextEditor&) override;
  248. /** @internal */
  249. void textEditorReturnKeyPressed (TextEditor&) override;
  250. /** @internal */
  251. void textEditorEscapeKeyPressed (TextEditor&) override;
  252. /** @internal */
  253. void textEditorFocusLost (TextEditor&) override;
  254. /** @internal */
  255. void colourChanged() override;
  256. /** @internal */
  257. void valueChanged (Value&) override;
  258. /** @internal */
  259. void callChangeListeners();
  260. private:
  261. //==============================================================================
  262. Value textValue;
  263. String lastTextValue;
  264. Font font;
  265. Justification justification;
  266. ScopedPointer<TextEditor> editor;
  267. ListenerList<Listener> listeners;
  268. WeakReference<Component> ownerComponent;
  269. BorderSize<int> border;
  270. float minimumHorizontalScale;
  271. TextInputTarget::VirtualKeyboardType keyboardType;
  272. bool editSingleClick;
  273. bool editDoubleClick;
  274. bool lossOfFocusDiscardsChanges;
  275. bool leftOfOwnerComp;
  276. bool updateFromTextEditorContents (TextEditor&);
  277. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Label)
  278. };
  279. /** This typedef is just for compatibility with old code - newer code should use the Label::Listener class directly. */
  280. typedef Label::Listener LabelListener;
  281. #endif // JUCE_LABEL_H_INCLUDED