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.

470 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. Label::Label (const String& name, const String& labelText)
  22. : Component (name),
  23. textValue (labelText),
  24. lastTextValue (labelText)
  25. {
  26. setColour (TextEditor::textColourId, Colours::black);
  27. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  28. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  29. textValue.addListener (this);
  30. }
  31. Label::~Label()
  32. {
  33. textValue.removeListener (this);
  34. if (ownerComponent != nullptr)
  35. ownerComponent->removeComponentListener (this);
  36. editor.reset();
  37. }
  38. //==============================================================================
  39. void Label::setText (const String& newText, NotificationType notification)
  40. {
  41. hideEditor (true);
  42. if (lastTextValue != newText)
  43. {
  44. lastTextValue = newText;
  45. textValue = newText;
  46. repaint();
  47. textWasChanged();
  48. if (ownerComponent != nullptr)
  49. componentMovedOrResized (*ownerComponent, true, true);
  50. if (notification != dontSendNotification)
  51. callChangeListeners();
  52. }
  53. }
  54. String Label::getText (const bool returnActiveEditorContents) const
  55. {
  56. return (returnActiveEditorContents && isBeingEdited())
  57. ? editor->getText()
  58. : textValue.toString();
  59. }
  60. void Label::valueChanged (Value&)
  61. {
  62. if (lastTextValue != textValue.toString())
  63. setText (textValue.toString(), sendNotification);
  64. }
  65. //==============================================================================
  66. void Label::setFont (const Font& newFont)
  67. {
  68. if (font != newFont)
  69. {
  70. font = newFont;
  71. repaint();
  72. }
  73. }
  74. Font Label::getFont() const noexcept
  75. {
  76. return font;
  77. }
  78. void Label::setEditable (bool editOnSingleClick,
  79. bool editOnDoubleClick,
  80. bool lossOfFocusDiscards)
  81. {
  82. editSingleClick = editOnSingleClick;
  83. editDoubleClick = editOnDoubleClick;
  84. lossOfFocusDiscardsChanges = lossOfFocusDiscards;
  85. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  86. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  87. }
  88. void Label::setJustificationType (Justification newJustification)
  89. {
  90. if (justification != newJustification)
  91. {
  92. justification = newJustification;
  93. repaint();
  94. }
  95. }
  96. void Label::setBorderSize (BorderSize<int> newBorder)
  97. {
  98. if (border != newBorder)
  99. {
  100. border = newBorder;
  101. repaint();
  102. }
  103. }
  104. //==============================================================================
  105. Component* Label::getAttachedComponent() const
  106. {
  107. return static_cast<Component*> (ownerComponent);
  108. }
  109. void Label::attachToComponent (Component* owner, const bool onLeft)
  110. {
  111. if (ownerComponent != nullptr)
  112. ownerComponent->removeComponentListener (this);
  113. ownerComponent = owner;
  114. leftOfOwnerComp = onLeft;
  115. if (ownerComponent != nullptr)
  116. {
  117. setVisible (owner->isVisible());
  118. ownerComponent->addComponentListener (this);
  119. componentParentHierarchyChanged (*ownerComponent);
  120. componentMovedOrResized (*ownerComponent, true, true);
  121. }
  122. }
  123. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  124. {
  125. const Font f (getLookAndFeel().getLabelFont (*this));
  126. if (leftOfOwnerComp)
  127. {
  128. setSize (jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f) + getBorderSize().getLeftAndRight(),
  129. component.getX()),
  130. component.getHeight());
  131. setTopRightPosition (component.getX(), component.getY());
  132. }
  133. else
  134. {
  135. setSize (component.getWidth(),
  136. getBorderSize().getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f));
  137. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  138. }
  139. }
  140. void Label::componentParentHierarchyChanged (Component& component)
  141. {
  142. if (auto* parent = component.getParentComponent())
  143. parent->addChildComponent (this);
  144. }
  145. void Label::componentVisibilityChanged (Component& component)
  146. {
  147. setVisible (component.isVisible());
  148. }
  149. //==============================================================================
  150. void Label::textWasEdited() {}
  151. void Label::textWasChanged() {}
  152. void Label::editorShown (TextEditor* textEditor)
  153. {
  154. Component::BailOutChecker checker (this);
  155. listeners.callChecked (checker, &LabelListener::editorShown, this, *textEditor);
  156. }
  157. void Label::editorAboutToBeHidden (TextEditor* textEditor)
  158. {
  159. if (ComponentPeer* const peer = getPeer())
  160. peer->dismissPendingTextInput();
  161. Component::BailOutChecker checker (this);
  162. listeners.callChecked (checker, &LabelListener::editorHidden, this, *textEditor);
  163. }
  164. void Label::showEditor()
  165. {
  166. if (editor == nullptr)
  167. {
  168. addAndMakeVisible (editor = createEditorComponent());
  169. editor->setText (getText(), false);
  170. editor->setKeyboardType (keyboardType);
  171. editor->addListener (this);
  172. editor->grabKeyboardFocus();
  173. if (editor == nullptr) // may be deleted by a callback
  174. return;
  175. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  176. resized();
  177. repaint();
  178. editorShown (editor);
  179. enterModalState (false);
  180. editor->grabKeyboardFocus();
  181. }
  182. }
  183. bool Label::updateFromTextEditorContents (TextEditor& ed)
  184. {
  185. const String newText (ed.getText());
  186. if (textValue.toString() != newText)
  187. {
  188. lastTextValue = newText;
  189. textValue = newText;
  190. repaint();
  191. textWasChanged();
  192. if (ownerComponent != nullptr)
  193. componentMovedOrResized (*ownerComponent, true, true);
  194. return true;
  195. }
  196. return false;
  197. }
  198. void Label::hideEditor (const bool discardCurrentEditorContents)
  199. {
  200. if (editor != nullptr)
  201. {
  202. WeakReference<Component> deletionChecker (this);
  203. ScopedPointer<TextEditor> outgoingEditor (editor);
  204. editorAboutToBeHidden (outgoingEditor);
  205. const bool changed = (! discardCurrentEditorContents)
  206. && updateFromTextEditorContents (*outgoingEditor);
  207. outgoingEditor.reset();
  208. repaint();
  209. if (changed)
  210. textWasEdited();
  211. if (deletionChecker != nullptr)
  212. exitModalState (0);
  213. if (changed && deletionChecker != nullptr)
  214. callChangeListeners();
  215. }
  216. }
  217. void Label::inputAttemptWhenModal()
  218. {
  219. if (editor != nullptr)
  220. {
  221. if (lossOfFocusDiscardsChanges)
  222. textEditorEscapeKeyPressed (*editor);
  223. else
  224. textEditorReturnKeyPressed (*editor);
  225. }
  226. }
  227. bool Label::isBeingEdited() const noexcept
  228. {
  229. return editor != nullptr;
  230. }
  231. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  232. {
  233. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  234. ed.setColour (targetColourID, l.findColour (colourID));
  235. }
  236. TextEditor* Label::createEditorComponent()
  237. {
  238. auto* ed = new TextEditor (getName());
  239. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  240. copyAllExplicitColoursTo (*ed);
  241. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  242. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  243. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::focusedOutlineColourId);
  244. return ed;
  245. }
  246. TextEditor* Label::getCurrentTextEditor() const noexcept
  247. {
  248. return editor;
  249. }
  250. //==============================================================================
  251. void Label::paint (Graphics& g)
  252. {
  253. getLookAndFeel().drawLabel (g, *this);
  254. }
  255. void Label::mouseUp (const MouseEvent& e)
  256. {
  257. if (editSingleClick
  258. && isEnabled()
  259. && contains (e.getPosition())
  260. && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  261. {
  262. showEditor();
  263. }
  264. }
  265. void Label::mouseDoubleClick (const MouseEvent& e)
  266. {
  267. if (editDoubleClick
  268. && isEnabled()
  269. && ! e.mods.isPopupMenu())
  270. showEditor();
  271. }
  272. void Label::resized()
  273. {
  274. if (editor != nullptr)
  275. editor->setBounds (getLocalBounds());
  276. }
  277. void Label::focusGained (FocusChangeType cause)
  278. {
  279. if (editSingleClick
  280. && isEnabled()
  281. && cause == focusChangedByTabKey)
  282. showEditor();
  283. }
  284. void Label::enablementChanged()
  285. {
  286. repaint();
  287. }
  288. void Label::colourChanged()
  289. {
  290. repaint();
  291. }
  292. void Label::setMinimumHorizontalScale (const float newScale)
  293. {
  294. if (minimumHorizontalScale != newScale)
  295. {
  296. minimumHorizontalScale = newScale;
  297. repaint();
  298. }
  299. }
  300. //==============================================================================
  301. // We'll use a custom focus traverser here to make sure focus goes from the
  302. // text editor to another component rather than back to the label itself.
  303. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  304. {
  305. public:
  306. LabelKeyboardFocusTraverser() {}
  307. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  308. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  309. static Component* getComp (Component* current)
  310. {
  311. return dynamic_cast<TextEditor*> (current) != nullptr
  312. ? current->getParentComponent() : current;
  313. }
  314. };
  315. KeyboardFocusTraverser* Label::createFocusTraverser()
  316. {
  317. return new LabelKeyboardFocusTraverser();
  318. }
  319. //==============================================================================
  320. void Label::addListener (LabelListener* const listener)
  321. {
  322. listeners.add (listener);
  323. }
  324. void Label::removeListener (LabelListener* const listener)
  325. {
  326. listeners.remove (listener);
  327. }
  328. void Label::callChangeListeners()
  329. {
  330. Component::BailOutChecker checker (this);
  331. listeners.callChecked (checker, &Label::Listener::labelTextChanged, this);
  332. }
  333. //==============================================================================
  334. void Label::textEditorTextChanged (TextEditor& ed)
  335. {
  336. if (editor != nullptr)
  337. {
  338. jassert (&ed == editor);
  339. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  340. {
  341. if (lossOfFocusDiscardsChanges)
  342. textEditorEscapeKeyPressed (ed);
  343. else
  344. textEditorReturnKeyPressed (ed);
  345. }
  346. }
  347. }
  348. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  349. {
  350. if (editor != nullptr)
  351. {
  352. jassert (&ed == editor);
  353. const bool changed = updateFromTextEditorContents (ed);
  354. hideEditor (true);
  355. if (changed)
  356. {
  357. WeakReference<Component> deletionChecker (this);
  358. textWasEdited();
  359. if (deletionChecker != nullptr)
  360. callChangeListeners();
  361. }
  362. }
  363. }
  364. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  365. {
  366. if (editor != nullptr)
  367. {
  368. jassert (&ed == editor);
  369. ignoreUnused (ed);
  370. editor->setText (textValue.toString(), false);
  371. hideEditor (true);
  372. }
  373. }
  374. void Label::textEditorFocusLost (TextEditor& ed)
  375. {
  376. textEditorTextChanged (ed);
  377. }
  378. } // namespace juce