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.

524 lines
14KB

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