The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

345 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_LABEL_JUCEHEADER__
  19. #define __JUCE_LABEL_JUCEHEADER__
  20. #include "juce_TextEditor.h"
  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 TextEditorListener,
  29. private ComponentListener,
  30. private ValueListener
  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::empty,
  39. const String& labelText = String::empty);
  40. /** Destructor. */
  41. ~Label();
  42. //==============================================================================
  43. /** Changes the label text.
  44. If broadcastChangeMessage is true and the new text is different to the current
  45. text, then the class will broadcast a change message to any Label::Listener objects
  46. that are registered.
  47. */
  48. void setText (const String& newText, bool broadcastChangeMessage);
  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() { 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. @see setFont
  70. */
  71. const Font& getFont() const noexcept;
  72. //==============================================================================
  73. /** A set of colour IDs to use to change the colour of various aspects of the label.
  74. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  75. methods.
  76. Note that you can also use the constants from TextEditor::ColourIds to change the
  77. colour of the text editor that is opened when a label is editable.
  78. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  79. */
  80. enum ColourIds
  81. {
  82. backgroundColourId = 0x1000280, /**< The background colour to fill the label with. */
  83. textColourId = 0x1000281, /**< The colour for the text. */
  84. outlineColourId = 0x1000282 /**< An optional colour to use to draw a border around the label.
  85. Leave this transparent to not have an outline. */
  86. };
  87. //==============================================================================
  88. /** Sets the style of justification to be used for positioning the text.
  89. (The default is Justification::centredLeft)
  90. */
  91. void setJustificationType (const Justification& justification);
  92. /** Returns the type of justification, as set in setJustificationType(). */
  93. Justification getJustificationType() const noexcept { return justification; }
  94. /** Changes the gap that is left between the edge of the component and the text.
  95. By default there's a small gap left at the sides of the component to allow for
  96. the drawing of the border, but you can change this if necessary.
  97. */
  98. void setBorderSize (int horizontalBorder, int verticalBorder);
  99. /** Returns the size of the horizontal gap being left around the text.
  100. */
  101. int getHorizontalBorderSize() const noexcept { return horizontalBorderSize; }
  102. /** Returns the size of the vertical gap being left around the text.
  103. */
  104. int getVerticalBorderSize() const noexcept { return verticalBorderSize; }
  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 0 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 relevent 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 horizantally before it starts
  124. using ellipsis.
  125. @see Graphics::drawFittedText
  126. */
  127. void setMinimumHorizontalScale (float newScale);
  128. float getMinimumHorizontalScale() const noexcept { return minimumHorizontalScale; }
  129. //==============================================================================
  130. /**
  131. A class for receiving events from a Label.
  132. You can register a Label::Listener with a Label using the Label::addListener()
  133. method, and it will be called when the text of the label changes, either because
  134. of a call to Label::setText() or by the user editing the text (if the label is
  135. editable).
  136. @see Label::addListener, Label::removeListener
  137. */
  138. class JUCE_API Listener
  139. {
  140. public:
  141. /** Destructor. */
  142. virtual ~Listener() {}
  143. /** Called when a Label's text has changed.
  144. */
  145. virtual void labelTextChanged (Label* labelThatHasChanged) = 0;
  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. //==============================================================================
  194. struct Ids
  195. {
  196. static const Identifier tagType, text, font, editMode, justification, focusLossDiscardsChanges;
  197. };
  198. void refreshFromValueTree (const ValueTree&, ComponentBuilder&);
  199. protected:
  200. //==============================================================================
  201. /** Creates the TextEditor component that will be used when the user has clicked on the label.
  202. Subclasses can override this if they need to customise this component in some way.
  203. */
  204. virtual TextEditor* createEditorComponent();
  205. /** Called after the user changes the text. */
  206. virtual void textWasEdited();
  207. /** Called when the text has been altered. */
  208. virtual void textWasChanged();
  209. /** Called when the text editor has just appeared, due to a user click or other focus change. */
  210. virtual void editorShown (TextEditor* editorComponent);
  211. /** Called when the text editor is going to be deleted, after editing has finished. */
  212. virtual void editorAboutToBeHidden (TextEditor* editorComponent);
  213. //==============================================================================
  214. /** @internal */
  215. void paint (Graphics& g);
  216. /** @internal */
  217. void resized();
  218. /** @internal */
  219. void mouseUp (const MouseEvent& e);
  220. /** @internal */
  221. void mouseDoubleClick (const MouseEvent& e);
  222. /** @internal */
  223. void componentMovedOrResized (Component& component, bool wasMoved, bool wasResized);
  224. /** @internal */
  225. void componentParentHierarchyChanged (Component& component);
  226. /** @internal */
  227. void componentVisibilityChanged (Component& component);
  228. /** @internal */
  229. void inputAttemptWhenModal();
  230. /** @internal */
  231. void focusGained (FocusChangeType);
  232. /** @internal */
  233. void enablementChanged();
  234. /** @internal */
  235. KeyboardFocusTraverser* createFocusTraverser();
  236. /** @internal */
  237. void textEditorTextChanged (TextEditor& editor);
  238. /** @internal */
  239. void textEditorReturnKeyPressed (TextEditor& editor);
  240. /** @internal */
  241. void textEditorEscapeKeyPressed (TextEditor& editor);
  242. /** @internal */
  243. void textEditorFocusLost (TextEditor& editor);
  244. /** @internal */
  245. void colourChanged();
  246. /** @internal */
  247. void valueChanged (Value&);
  248. private:
  249. //==============================================================================
  250. Value textValue;
  251. String lastTextValue;
  252. Font font;
  253. Justification justification;
  254. ScopedPointer<TextEditor> editor;
  255. ListenerList<Listener> listeners;
  256. WeakReference<Component> ownerComponent;
  257. int horizontalBorderSize, verticalBorderSize;
  258. float minimumHorizontalScale;
  259. bool editSingleClick : 1;
  260. bool editDoubleClick : 1;
  261. bool lossOfFocusDiscardsChanges : 1;
  262. bool leftOfOwnerComp : 1;
  263. bool updateFromTextEditorContents (TextEditor&);
  264. void callChangeListeners();
  265. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Label);
  266. };
  267. /** This typedef is just for compatibility with old code - newer code should use the Label::Listener class directly. */
  268. typedef Label::Listener LabelListener;
  269. #endif // __JUCE_LABEL_JUCEHEADER__