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.

447 lines
12KB

  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. Label::Label (const String& name,
  19. const String& labelText)
  20. : Component (name),
  21. textValue (labelText),
  22. lastTextValue (labelText),
  23. font (15.0f),
  24. justification (Justification::centredLeft),
  25. horizontalBorderSize (5),
  26. verticalBorderSize (1),
  27. minimumHorizontalScale (0.7f),
  28. editSingleClick (false),
  29. editDoubleClick (false),
  30. lossOfFocusDiscardsChanges (false)
  31. {
  32. setColour (TextEditor::textColourId, Colours::black);
  33. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  34. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  35. textValue.addListener (this);
  36. }
  37. Label::~Label()
  38. {
  39. textValue.removeListener (this);
  40. if (ownerComponent != nullptr)
  41. ownerComponent->removeComponentListener (this);
  42. editor = nullptr;
  43. }
  44. //==============================================================================
  45. void Label::setText (const String& newText,
  46. const NotificationType notification)
  47. {
  48. hideEditor (true);
  49. if (lastTextValue != newText)
  50. {
  51. lastTextValue = newText;
  52. textValue = newText;
  53. repaint();
  54. textWasChanged();
  55. if (ownerComponent != nullptr)
  56. componentMovedOrResized (*ownerComponent, true, true);
  57. if (notification != dontSendNotification)
  58. callChangeListeners();
  59. }
  60. }
  61. String Label::getText (const bool returnActiveEditorContents) const
  62. {
  63. return (returnActiveEditorContents && isBeingEdited())
  64. ? editor->getText()
  65. : textValue.toString();
  66. }
  67. void Label::valueChanged (Value&)
  68. {
  69. if (lastTextValue != textValue.toString())
  70. setText (textValue.toString(), sendNotification);
  71. }
  72. //==============================================================================
  73. void Label::setFont (const Font& newFont)
  74. {
  75. if (font != newFont)
  76. {
  77. font = newFont;
  78. repaint();
  79. }
  80. }
  81. Font Label::getFont() const noexcept
  82. {
  83. return font;
  84. }
  85. void Label::setEditable (const bool editOnSingleClick,
  86. const bool editOnDoubleClick,
  87. const bool lossOfFocusDiscardsChanges_)
  88. {
  89. editSingleClick = editOnSingleClick;
  90. editDoubleClick = editOnDoubleClick;
  91. lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;
  92. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  93. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  94. }
  95. void Label::setJustificationType (const Justification& newJustification)
  96. {
  97. if (justification != newJustification)
  98. {
  99. justification = newJustification;
  100. repaint();
  101. }
  102. }
  103. void Label::setBorderSize (int h, int v)
  104. {
  105. if (horizontalBorderSize != h || verticalBorderSize != v)
  106. {
  107. horizontalBorderSize = h;
  108. verticalBorderSize = v;
  109. repaint();
  110. }
  111. }
  112. //==============================================================================
  113. Component* Label::getAttachedComponent() const
  114. {
  115. return static_cast<Component*> (ownerComponent);
  116. }
  117. void Label::attachToComponent (Component* owner, const bool onLeft)
  118. {
  119. if (ownerComponent != nullptr)
  120. ownerComponent->removeComponentListener (this);
  121. ownerComponent = owner;
  122. leftOfOwnerComp = onLeft;
  123. if (ownerComponent != nullptr)
  124. {
  125. setVisible (owner->isVisible());
  126. ownerComponent->addComponentListener (this);
  127. componentParentHierarchyChanged (*ownerComponent);
  128. componentMovedOrResized (*ownerComponent, true, true);
  129. }
  130. }
  131. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  132. {
  133. const Font f (getLookAndFeel().getLabelFont (*this));
  134. if (leftOfOwnerComp)
  135. {
  136. setSize (jmin (f.getStringWidth (textValue.toString()) + 8, component.getX()),
  137. component.getHeight());
  138. setTopRightPosition (component.getX(), component.getY());
  139. }
  140. else
  141. {
  142. setSize (component.getWidth(),
  143. 8 + roundToInt (f.getHeight()));
  144. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  145. }
  146. }
  147. void Label::componentParentHierarchyChanged (Component& component)
  148. {
  149. if (Component* parent = component.getParentComponent())
  150. parent->addChildComponent (this);
  151. }
  152. void Label::componentVisibilityChanged (Component& component)
  153. {
  154. setVisible (component.isVisible());
  155. }
  156. //==============================================================================
  157. void Label::textWasEdited() {}
  158. void Label::textWasChanged() {}
  159. void Label::editorShown (TextEditor*) {}
  160. void Label::editorAboutToBeHidden (TextEditor*)
  161. {
  162. if (ComponentPeer* const peer = getPeer())
  163. peer->dismissPendingTextInput();
  164. }
  165. void Label::showEditor()
  166. {
  167. if (editor == nullptr)
  168. {
  169. addAndMakeVisible (editor = createEditorComponent());
  170. editor->setText (getText(), false);
  171. editor->addListener (this);
  172. editor->grabKeyboardFocus();
  173. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  174. resized();
  175. repaint();
  176. editorShown (editor);
  177. enterModalState (false);
  178. editor->grabKeyboardFocus();
  179. }
  180. }
  181. bool Label::updateFromTextEditorContents (TextEditor& ed)
  182. {
  183. const String newText (ed.getText());
  184. if (textValue.toString() != newText)
  185. {
  186. lastTextValue = newText;
  187. textValue = newText;
  188. repaint();
  189. textWasChanged();
  190. if (ownerComponent != nullptr)
  191. componentMovedOrResized (*ownerComponent, true, true);
  192. return true;
  193. }
  194. return false;
  195. }
  196. void Label::hideEditor (const bool discardCurrentEditorContents)
  197. {
  198. if (editor != nullptr)
  199. {
  200. WeakReference<Component> deletionChecker (this);
  201. ScopedPointer<TextEditor> outgoingEditor (editor);
  202. editorAboutToBeHidden (outgoingEditor);
  203. const bool changed = (! discardCurrentEditorContents)
  204. && updateFromTextEditorContents (*outgoingEditor);
  205. outgoingEditor = nullptr;
  206. repaint();
  207. if (changed)
  208. textWasEdited();
  209. if (deletionChecker != nullptr)
  210. exitModalState (0);
  211. if (changed && deletionChecker != nullptr)
  212. callChangeListeners();
  213. }
  214. }
  215. void Label::inputAttemptWhenModal()
  216. {
  217. if (editor != nullptr)
  218. {
  219. if (lossOfFocusDiscardsChanges)
  220. textEditorEscapeKeyPressed (*editor);
  221. else
  222. textEditorReturnKeyPressed (*editor);
  223. }
  224. }
  225. bool Label::isBeingEdited() const noexcept
  226. {
  227. return editor != nullptr;
  228. }
  229. TextEditor* Label::createEditorComponent()
  230. {
  231. TextEditor* const ed = new TextEditor (getName());
  232. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  233. copyAllExplicitColoursTo (*ed);
  234. return ed;
  235. }
  236. TextEditor* Label::getCurrentTextEditor() const noexcept
  237. {
  238. return editor;
  239. }
  240. //==============================================================================
  241. void Label::paint (Graphics& g)
  242. {
  243. getLookAndFeel().drawLabel (g, *this);
  244. }
  245. void Label::mouseUp (const MouseEvent& e)
  246. {
  247. if (editSingleClick
  248. && e.mouseWasClicked()
  249. && contains (e.getPosition())
  250. && ! e.mods.isPopupMenu())
  251. {
  252. showEditor();
  253. }
  254. }
  255. void Label::mouseDoubleClick (const MouseEvent& e)
  256. {
  257. if (editDoubleClick && ! e.mods.isPopupMenu())
  258. showEditor();
  259. }
  260. void Label::resized()
  261. {
  262. if (editor != nullptr)
  263. editor->setBoundsInset (BorderSize<int> (0));
  264. }
  265. void Label::focusGained (FocusChangeType cause)
  266. {
  267. if (editSingleClick && cause == focusChangedByTabKey)
  268. showEditor();
  269. }
  270. void Label::enablementChanged()
  271. {
  272. repaint();
  273. }
  274. void Label::colourChanged()
  275. {
  276. repaint();
  277. }
  278. void Label::setMinimumHorizontalScale (const float newScale)
  279. {
  280. if (minimumHorizontalScale != newScale)
  281. {
  282. minimumHorizontalScale = newScale;
  283. repaint();
  284. }
  285. }
  286. //==============================================================================
  287. // We'll use a custom focus traverser here to make sure focus goes from the
  288. // text editor to another component rather than back to the label itself.
  289. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  290. {
  291. public:
  292. LabelKeyboardFocusTraverser() {}
  293. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  294. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  295. static Component* getComp (Component* current)
  296. {
  297. return dynamic_cast <TextEditor*> (current) != nullptr
  298. ? current->getParentComponent() : current;
  299. }
  300. };
  301. KeyboardFocusTraverser* Label::createFocusTraverser()
  302. {
  303. return new LabelKeyboardFocusTraverser();
  304. }
  305. //==============================================================================
  306. void Label::addListener (LabelListener* const listener)
  307. {
  308. listeners.add (listener);
  309. }
  310. void Label::removeListener (LabelListener* const listener)
  311. {
  312. listeners.remove (listener);
  313. }
  314. void Label::callChangeListeners()
  315. {
  316. Component::BailOutChecker checker (this);
  317. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  318. }
  319. //==============================================================================
  320. void Label::textEditorTextChanged (TextEditor& ed)
  321. {
  322. if (editor != nullptr)
  323. {
  324. jassert (&ed == editor);
  325. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  326. {
  327. if (lossOfFocusDiscardsChanges)
  328. textEditorEscapeKeyPressed (ed);
  329. else
  330. textEditorReturnKeyPressed (ed);
  331. }
  332. }
  333. }
  334. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  335. {
  336. if (editor != nullptr)
  337. {
  338. jassert (&ed == editor);
  339. const bool changed = updateFromTextEditorContents (ed);
  340. hideEditor (true);
  341. if (changed)
  342. {
  343. WeakReference<Component> deletionChecker (this);
  344. textWasEdited();
  345. if (deletionChecker != nullptr)
  346. callChangeListeners();
  347. }
  348. }
  349. }
  350. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  351. {
  352. if (editor != nullptr)
  353. {
  354. jassert (&ed == editor);
  355. (void) ed;
  356. editor->setText (textValue.toString(), false);
  357. hideEditor (true);
  358. }
  359. }
  360. void Label::textEditorFocusLost (TextEditor& ed)
  361. {
  362. textEditorTextChanged (ed);
  363. }