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.

583 lines
16KB

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