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.

484 lines
13KB

  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. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  85. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  86. }
  87. void Label::setJustificationType (Justification newJustification)
  88. {
  89. if (justification != newJustification)
  90. {
  91. justification = newJustification;
  92. repaint();
  93. }
  94. }
  95. void Label::setBorderSize (BorderSize<int> newBorder)
  96. {
  97. if (border != newBorder)
  98. {
  99. border = newBorder;
  100. repaint();
  101. }
  102. }
  103. //==============================================================================
  104. Component* Label::getAttachedComponent() const
  105. {
  106. return ownerComponent.get();
  107. }
  108. void Label::attachToComponent (Component* owner, bool onLeft)
  109. {
  110. jassert (owner != this); // Not a great idea to try to attach it to itself!
  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. auto& lf = getLookAndFeel();
  126. auto f = lf.getLabelFont (*this);
  127. auto borderSize = lf.getLabelBorderSize (*this);
  128. if (leftOfOwnerComp)
  129. {
  130. auto width = jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f)
  131. + borderSize.getLeftAndRight(),
  132. component.getX());
  133. setBounds (component.getX() - width, component.getY(), width, component.getHeight());
  134. }
  135. else
  136. {
  137. auto height = borderSize.getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f);
  138. setBounds (component.getX(), component.getY() - height, component.getWidth(), height);
  139. }
  140. }
  141. void Label::componentParentHierarchyChanged (Component& component)
  142. {
  143. if (auto* parent = component.getParentComponent())
  144. parent->addChildComponent (this);
  145. }
  146. void Label::componentVisibilityChanged (Component& component)
  147. {
  148. setVisible (component.isVisible());
  149. }
  150. //==============================================================================
  151. void Label::textWasEdited() {}
  152. void Label::textWasChanged() {}
  153. void Label::editorShown (TextEditor* textEditor)
  154. {
  155. Component::BailOutChecker checker (this);
  156. listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorShown (this, *textEditor); });
  157. if (checker.shouldBailOut())
  158. return;
  159. if (onEditorShow != nullptr)
  160. onEditorShow();
  161. }
  162. void Label::editorAboutToBeHidden (TextEditor* textEditor)
  163. {
  164. if (auto* peer = getPeer())
  165. peer->dismissPendingTextInput();
  166. Component::BailOutChecker checker (this);
  167. listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorHidden (this, *textEditor); });
  168. if (checker.shouldBailOut())
  169. return;
  170. if (onEditorHide != nullptr)
  171. onEditorHide();
  172. }
  173. void Label::showEditor()
  174. {
  175. if (editor == nullptr)
  176. {
  177. editor.reset (createEditorComponent());
  178. addAndMakeVisible (editor.get());
  179. editor->setText (getText(), false);
  180. editor->setKeyboardType (keyboardType);
  181. editor->addListener (this);
  182. editor->grabKeyboardFocus();
  183. if (editor == nullptr) // may be deleted by a callback
  184. return;
  185. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  186. resized();
  187. repaint();
  188. editorShown (editor.get());
  189. enterModalState (false);
  190. editor->grabKeyboardFocus();
  191. }
  192. }
  193. bool Label::updateFromTextEditorContents (TextEditor& ed)
  194. {
  195. auto newText = ed.getText();
  196. if (textValue.toString() != newText)
  197. {
  198. lastTextValue = newText;
  199. textValue = newText;
  200. repaint();
  201. textWasChanged();
  202. if (ownerComponent != nullptr)
  203. componentMovedOrResized (*ownerComponent, true, true);
  204. return true;
  205. }
  206. return false;
  207. }
  208. void Label::hideEditor (bool discardCurrentEditorContents)
  209. {
  210. if (editor != nullptr)
  211. {
  212. WeakReference<Component> deletionChecker (this);
  213. std::unique_ptr<TextEditor> outgoingEditor;
  214. std::swap (outgoingEditor, editor);
  215. editorAboutToBeHidden (outgoingEditor.get());
  216. const bool changed = (! discardCurrentEditorContents)
  217. && updateFromTextEditorContents (*outgoingEditor);
  218. outgoingEditor.reset();
  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. showEditor();
  282. }
  283. void Label::resized()
  284. {
  285. if (editor != nullptr)
  286. editor->setBounds (getLocalBounds());
  287. }
  288. void Label::focusGained (FocusChangeType cause)
  289. {
  290. if (editSingleClick
  291. && isEnabled()
  292. && cause == focusChangedByTabKey)
  293. showEditor();
  294. }
  295. void Label::enablementChanged()
  296. {
  297. repaint();
  298. }
  299. void Label::colourChanged()
  300. {
  301. repaint();
  302. }
  303. void Label::setMinimumHorizontalScale (const float newScale)
  304. {
  305. if (minimumHorizontalScale != newScale)
  306. {
  307. minimumHorizontalScale = newScale;
  308. repaint();
  309. }
  310. }
  311. //==============================================================================
  312. // We'll use a custom focus traverser here to make sure focus goes from the
  313. // text editor to another component rather than back to the label itself.
  314. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  315. {
  316. public:
  317. LabelKeyboardFocusTraverser() {}
  318. Component* getNextComponent (Component* c) override { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  319. Component* getPreviousComponent (Component* c) override { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  320. static Component* getComp (Component* current)
  321. {
  322. return dynamic_cast<TextEditor*> (current) != nullptr
  323. ? current->getParentComponent() : current;
  324. }
  325. };
  326. KeyboardFocusTraverser* Label::createFocusTraverser()
  327. {
  328. return new LabelKeyboardFocusTraverser();
  329. }
  330. //==============================================================================
  331. void Label::addListener (Label::Listener* l) { listeners.add (l); }
  332. void Label::removeListener (Label::Listener* l) { listeners.remove (l); }
  333. void Label::callChangeListeners()
  334. {
  335. Component::BailOutChecker checker (this);
  336. listeners.callChecked (checker, [this] (Listener& l) { l.labelTextChanged (this); });
  337. if (checker.shouldBailOut())
  338. return;
  339. if (onTextChange != nullptr)
  340. onTextChange();
  341. }
  342. //==============================================================================
  343. void Label::textEditorTextChanged (TextEditor& ed)
  344. {
  345. if (editor != nullptr)
  346. {
  347. jassert (&ed == editor.get());
  348. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  349. {
  350. if (lossOfFocusDiscardsChanges)
  351. textEditorEscapeKeyPressed (ed);
  352. else
  353. textEditorReturnKeyPressed (ed);
  354. }
  355. }
  356. }
  357. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  358. {
  359. if (editor != nullptr)
  360. {
  361. jassert (&ed == editor.get());
  362. WeakReference<Component> deletionChecker (this);
  363. bool changed = updateFromTextEditorContents (ed);
  364. hideEditor (true);
  365. if (changed && deletionChecker != nullptr)
  366. {
  367. textWasEdited();
  368. if (deletionChecker != nullptr)
  369. callChangeListeners();
  370. }
  371. }
  372. }
  373. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  374. {
  375. if (editor != nullptr)
  376. {
  377. jassert (&ed == editor.get());
  378. ignoreUnused (ed);
  379. editor->setText (textValue.toString(), false);
  380. hideEditor (true);
  381. }
  382. }
  383. void Label::textEditorFocusLost (TextEditor& ed)
  384. {
  385. textEditorTextChanged (ed);
  386. }
  387. } // namespace juce