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.

587 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 (ownerComponent->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. Component::BailOutChecker checker (this);
  168. listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorHidden (this, *textEditor); });
  169. if (checker.shouldBailOut())
  170. return;
  171. if (onEditorHide != nullptr)
  172. onEditorHide();
  173. }
  174. void Label::showEditor()
  175. {
  176. if (editor == nullptr)
  177. {
  178. editor.reset (createEditorComponent());
  179. editor->setSize (10, 10);
  180. addAndMakeVisible (editor.get());
  181. editor->setText (getText(), false);
  182. editor->setKeyboardType (keyboardType);
  183. editor->addListener (this);
  184. editor->grabKeyboardFocus();
  185. if (editor == nullptr) // may be deleted by a callback
  186. return;
  187. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  188. resized();
  189. repaint();
  190. editorShown (editor.get());
  191. enterModalState (false);
  192. editor->grabKeyboardFocus();
  193. }
  194. }
  195. bool Label::updateFromTextEditorContents (TextEditor& ed)
  196. {
  197. auto newText = ed.getText();
  198. if (textValue.toString() != newText)
  199. {
  200. lastTextValue = newText;
  201. textValue = newText;
  202. repaint();
  203. textWasChanged();
  204. if (ownerComponent != nullptr)
  205. componentMovedOrResized (*ownerComponent, true, true);
  206. return true;
  207. }
  208. return false;
  209. }
  210. void Label::hideEditor (bool discardCurrentEditorContents)
  211. {
  212. if (editor != nullptr)
  213. {
  214. WeakReference<Component> deletionChecker (this);
  215. std::unique_ptr<TextEditor> outgoingEditor;
  216. std::swap (outgoingEditor, editor);
  217. editorAboutToBeHidden (outgoingEditor.get());
  218. const bool changed = (! discardCurrentEditorContents)
  219. && updateFromTextEditorContents (*outgoingEditor);
  220. outgoingEditor.reset();
  221. if (deletionChecker != nullptr)
  222. repaint();
  223. if (changed)
  224. textWasEdited();
  225. if (deletionChecker != nullptr)
  226. exitModalState (0);
  227. if (changed && deletionChecker != nullptr)
  228. callChangeListeners();
  229. }
  230. }
  231. void Label::inputAttemptWhenModal()
  232. {
  233. if (editor != nullptr)
  234. {
  235. if (lossOfFocusDiscardsChanges)
  236. textEditorEscapeKeyPressed (*editor);
  237. else
  238. textEditorReturnKeyPressed (*editor);
  239. }
  240. }
  241. bool Label::isBeingEdited() const noexcept
  242. {
  243. return editor != nullptr;
  244. }
  245. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  246. {
  247. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  248. ed.setColour (targetColourID, l.findColour (colourID));
  249. }
  250. TextEditor* Label::createEditorComponent()
  251. {
  252. auto* ed = new TextEditor (getName());
  253. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  254. copyAllExplicitColoursTo (*ed);
  255. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  256. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  257. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::focusedOutlineColourId);
  258. return ed;
  259. }
  260. TextEditor* Label::getCurrentTextEditor() const noexcept
  261. {
  262. return editor.get();
  263. }
  264. //==============================================================================
  265. void Label::paint (Graphics& g)
  266. {
  267. getLookAndFeel().drawLabel (g, *this);
  268. }
  269. void Label::mouseUp (const MouseEvent& e)
  270. {
  271. if (editSingleClick
  272. && isEnabled()
  273. && contains (e.getPosition())
  274. && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  275. {
  276. showEditor();
  277. }
  278. }
  279. void Label::mouseDoubleClick (const MouseEvent& e)
  280. {
  281. if (editDoubleClick
  282. && isEnabled()
  283. && ! e.mods.isPopupMenu())
  284. {
  285. showEditor();
  286. }
  287. }
  288. void Label::resized()
  289. {
  290. if (editor != nullptr)
  291. editor->setBounds (getLocalBounds());
  292. }
  293. void Label::focusGained (FocusChangeType cause)
  294. {
  295. if (editSingleClick
  296. && isEnabled()
  297. && cause == focusChangedByTabKey)
  298. {
  299. showEditor();
  300. }
  301. }
  302. void Label::enablementChanged()
  303. {
  304. repaint();
  305. }
  306. void Label::colourChanged()
  307. {
  308. repaint();
  309. }
  310. void Label::setMinimumHorizontalScale (const float newScale)
  311. {
  312. if (minimumHorizontalScale != newScale)
  313. {
  314. minimumHorizontalScale = newScale;
  315. repaint();
  316. }
  317. }
  318. //==============================================================================
  319. // We'll use a custom focus traverser here to make sure focus goes from the
  320. // text editor to another component rather than back to the label itself.
  321. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  322. {
  323. public:
  324. explicit LabelKeyboardFocusTraverser (Label& l) : owner (l) {}
  325. Component* getDefaultComponent (Component* parent) override
  326. {
  327. if (auto* container = getKeyboardFocusContainer (parent))
  328. return KeyboardFocusTraverser::getDefaultComponent (container);
  329. return nullptr;
  330. }
  331. Component* getNextComponent (Component* c) override { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  332. Component* getPreviousComponent (Component* c) override { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  333. std::vector<Component*> getAllComponents (Component* parent) override
  334. {
  335. if (auto* container = getKeyboardFocusContainer (parent))
  336. return KeyboardFocusTraverser::getAllComponents (container);
  337. return {};
  338. }
  339. private:
  340. Component* getComp (Component* current) const
  341. {
  342. if (auto* ed = owner.getCurrentTextEditor())
  343. if (current == ed)
  344. return current->getParentComponent();
  345. return current;
  346. }
  347. Component* getKeyboardFocusContainer (Component* parent) const
  348. {
  349. if (owner.getCurrentTextEditor() != nullptr && parent == &owner)
  350. return owner.findKeyboardFocusContainer();
  351. return parent;
  352. }
  353. Label& owner;
  354. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LabelKeyboardFocusTraverser)
  355. };
  356. std::unique_ptr<ComponentTraverser> Label::createKeyboardFocusTraverser()
  357. {
  358. return std::make_unique<LabelKeyboardFocusTraverser> (*this);
  359. }
  360. //==============================================================================
  361. void Label::addListener (Label::Listener* l) { listeners.add (l); }
  362. void Label::removeListener (Label::Listener* l) { listeners.remove (l); }
  363. void Label::callChangeListeners()
  364. {
  365. Component::BailOutChecker checker (this);
  366. listeners.callChecked (checker, [this] (Listener& l) { l.labelTextChanged (this); });
  367. if (checker.shouldBailOut())
  368. return;
  369. if (onTextChange != nullptr)
  370. onTextChange();
  371. }
  372. //==============================================================================
  373. void Label::textEditorTextChanged (TextEditor& ed)
  374. {
  375. if (editor != nullptr)
  376. {
  377. jassert (&ed == editor.get());
  378. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  379. {
  380. if (lossOfFocusDiscardsChanges)
  381. textEditorEscapeKeyPressed (ed);
  382. else
  383. textEditorReturnKeyPressed (ed);
  384. }
  385. }
  386. }
  387. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  388. {
  389. if (editor != nullptr)
  390. {
  391. jassert (&ed == editor.get());
  392. WeakReference<Component> deletionChecker (this);
  393. bool changed = updateFromTextEditorContents (ed);
  394. hideEditor (true);
  395. if (changed && deletionChecker != nullptr)
  396. {
  397. textWasEdited();
  398. if (deletionChecker != nullptr)
  399. callChangeListeners();
  400. }
  401. }
  402. }
  403. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  404. {
  405. if (editor != nullptr)
  406. {
  407. jassertquiet (&ed == editor.get());
  408. editor->setText (textValue.toString(), false);
  409. hideEditor (true);
  410. }
  411. }
  412. void Label::textEditorFocusLost (TextEditor& ed)
  413. {
  414. textEditorTextChanged (ed);
  415. }
  416. //==============================================================================
  417. class LabelAccessibilityHandler : public AccessibilityHandler
  418. {
  419. public:
  420. explicit LabelAccessibilityHandler (Label& labelToWrap)
  421. : AccessibilityHandler (labelToWrap,
  422. labelToWrap.isEditable() ? AccessibilityRole::editableText : AccessibilityRole::label,
  423. getAccessibilityActions (labelToWrap),
  424. { std::make_unique<LabelValueInterface> (labelToWrap) }),
  425. label (labelToWrap)
  426. {
  427. }
  428. String getTitle() const override { return label.getText(); }
  429. String getHelp() const override { return label.getTooltip(); }
  430. AccessibleState getCurrentState() const override
  431. {
  432. if (label.isBeingEdited())
  433. return {}; // allow focus to pass through to the TextEditor
  434. return AccessibilityHandler::getCurrentState();
  435. }
  436. private:
  437. class LabelValueInterface : public AccessibilityTextValueInterface
  438. {
  439. public:
  440. explicit LabelValueInterface (Label& labelToWrap)
  441. : label (labelToWrap)
  442. {
  443. }
  444. bool isReadOnly() const override { return true; }
  445. String getCurrentValueAsString() const override { return label.getText(); }
  446. void setValueAsString (const String&) override {}
  447. private:
  448. Label& label;
  449. //==============================================================================
  450. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LabelValueInterface)
  451. };
  452. static AccessibilityActions getAccessibilityActions (Label& label)
  453. {
  454. if (label.isEditable())
  455. return AccessibilityActions().addAction (AccessibilityActionType::press, [&label] { label.showEditor(); });
  456. return {};
  457. }
  458. Label& label;
  459. //==============================================================================
  460. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LabelAccessibilityHandler)
  461. };
  462. std::unique_ptr<AccessibilityHandler> Label::createAccessibilityHandler()
  463. {
  464. return std::make_unique<LabelAccessibilityHandler> (*this);
  465. }
  466. } // namespace juce