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.

477 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - 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 this technical preview, this file is not subject to commercial licensing.
  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. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  80. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  81. }
  82. void Label::setJustificationType (Justification newJustification)
  83. {
  84. if (justification != newJustification)
  85. {
  86. justification = newJustification;
  87. repaint();
  88. }
  89. }
  90. void Label::setBorderSize (BorderSize<int> newBorder)
  91. {
  92. if (border != newBorder)
  93. {
  94. border = newBorder;
  95. repaint();
  96. }
  97. }
  98. //==============================================================================
  99. Component* Label::getAttachedComponent() const
  100. {
  101. return ownerComponent.get();
  102. }
  103. void Label::attachToComponent (Component* owner, bool onLeft)
  104. {
  105. jassert (owner != this); // Not a great idea to try to attach it to itself!
  106. if (ownerComponent != nullptr)
  107. ownerComponent->removeComponentListener (this);
  108. ownerComponent = owner;
  109. leftOfOwnerComp = onLeft;
  110. if (ownerComponent != nullptr)
  111. {
  112. setVisible (owner->isVisible());
  113. ownerComponent->addComponentListener (this);
  114. componentParentHierarchyChanged (*ownerComponent);
  115. componentMovedOrResized (*ownerComponent, true, true);
  116. }
  117. }
  118. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  119. {
  120. auto& lf = getLookAndFeel();
  121. auto f = lf.getLabelFont (*this);
  122. auto borderSize = lf.getLabelBorderSize (*this);
  123. if (leftOfOwnerComp)
  124. {
  125. auto width = jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f)
  126. + borderSize.getLeftAndRight(),
  127. component.getX());
  128. setBounds (component.getX() - width, component.getY(), width, component.getHeight());
  129. }
  130. else
  131. {
  132. auto height = borderSize.getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f);
  133. setBounds (component.getX(), component.getY() - height, component.getWidth(), height);
  134. }
  135. }
  136. void Label::componentParentHierarchyChanged (Component& component)
  137. {
  138. if (auto* parent = component.getParentComponent())
  139. parent->addChildComponent (this);
  140. }
  141. void Label::componentVisibilityChanged (Component& component)
  142. {
  143. setVisible (component.isVisible());
  144. }
  145. //==============================================================================
  146. void Label::textWasEdited() {}
  147. void Label::textWasChanged() {}
  148. void Label::editorShown (TextEditor* textEditor)
  149. {
  150. Component::BailOutChecker checker (this);
  151. listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorShown (this, *textEditor); });
  152. if (checker.shouldBailOut())
  153. return;
  154. if (onEditorShow != nullptr)
  155. onEditorShow();
  156. }
  157. void Label::editorAboutToBeHidden (TextEditor* textEditor)
  158. {
  159. if (auto* peer = getPeer())
  160. peer->dismissPendingTextInput();
  161. Component::BailOutChecker checker (this);
  162. listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorHidden (this, *textEditor); });
  163. if (checker.shouldBailOut())
  164. return;
  165. if (onEditorHide != nullptr)
  166. onEditorHide();
  167. }
  168. void Label::showEditor()
  169. {
  170. if (editor == nullptr)
  171. {
  172. editor.reset (createEditorComponent());
  173. addAndMakeVisible (editor.get());
  174. editor->setText (getText(), false);
  175. editor->setKeyboardType (keyboardType);
  176. editor->addListener (this);
  177. editor->grabKeyboardFocus();
  178. if (editor == nullptr) // may be deleted by a callback
  179. return;
  180. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  181. resized();
  182. repaint();
  183. editorShown (editor.get());
  184. enterModalState (false);
  185. editor->grabKeyboardFocus();
  186. }
  187. }
  188. bool Label::updateFromTextEditorContents (TextEditor& ed)
  189. {
  190. auto newText = ed.getText();
  191. if (textValue.toString() != newText)
  192. {
  193. lastTextValue = newText;
  194. textValue = newText;
  195. repaint();
  196. textWasChanged();
  197. if (ownerComponent != nullptr)
  198. componentMovedOrResized (*ownerComponent, true, true);
  199. return true;
  200. }
  201. return false;
  202. }
  203. void Label::hideEditor (bool discardCurrentEditorContents)
  204. {
  205. if (editor != nullptr)
  206. {
  207. WeakReference<Component> deletionChecker (this);
  208. std::unique_ptr<TextEditor> outgoingEditor;
  209. std::swap (outgoingEditor, editor);
  210. editorAboutToBeHidden (outgoingEditor.get());
  211. const bool changed = (! discardCurrentEditorContents)
  212. && updateFromTextEditorContents (*outgoingEditor);
  213. outgoingEditor.reset();
  214. repaint();
  215. if (changed)
  216. textWasEdited();
  217. if (deletionChecker != nullptr)
  218. exitModalState (0);
  219. if (changed && deletionChecker != nullptr)
  220. callChangeListeners();
  221. }
  222. }
  223. void Label::inputAttemptWhenModal()
  224. {
  225. if (editor != nullptr)
  226. {
  227. if (lossOfFocusDiscardsChanges)
  228. textEditorEscapeKeyPressed (*editor);
  229. else
  230. textEditorReturnKeyPressed (*editor);
  231. }
  232. }
  233. bool Label::isBeingEdited() const noexcept
  234. {
  235. return editor != nullptr;
  236. }
  237. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  238. {
  239. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  240. ed.setColour (targetColourID, l.findColour (colourID));
  241. }
  242. TextEditor* Label::createEditorComponent()
  243. {
  244. auto* ed = new TextEditor (getName());
  245. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  246. copyAllExplicitColoursTo (*ed);
  247. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  248. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  249. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::focusedOutlineColourId);
  250. return ed;
  251. }
  252. TextEditor* Label::getCurrentTextEditor() const noexcept
  253. {
  254. return editor.get();
  255. }
  256. //==============================================================================
  257. void Label::paint (Graphics& g)
  258. {
  259. getLookAndFeel().drawLabel (g, *this);
  260. }
  261. void Label::mouseUp (const MouseEvent& e)
  262. {
  263. if (editSingleClick
  264. && isEnabled()
  265. && contains (e.getPosition())
  266. && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  267. {
  268. showEditor();
  269. }
  270. }
  271. void Label::mouseDoubleClick (const MouseEvent& e)
  272. {
  273. if (editDoubleClick
  274. && isEnabled()
  275. && ! e.mods.isPopupMenu())
  276. showEditor();
  277. }
  278. void Label::resized()
  279. {
  280. if (editor != nullptr)
  281. editor->setBounds (getLocalBounds());
  282. }
  283. void Label::focusGained (FocusChangeType cause)
  284. {
  285. if (editSingleClick
  286. && isEnabled()
  287. && cause == focusChangedByTabKey)
  288. showEditor();
  289. }
  290. void Label::enablementChanged()
  291. {
  292. repaint();
  293. }
  294. void Label::colourChanged()
  295. {
  296. repaint();
  297. }
  298. void Label::setMinimumHorizontalScale (const float newScale)
  299. {
  300. if (minimumHorizontalScale != newScale)
  301. {
  302. minimumHorizontalScale = newScale;
  303. repaint();
  304. }
  305. }
  306. //==============================================================================
  307. // We'll use a custom focus traverser here to make sure focus goes from the
  308. // text editor to another component rather than back to the label itself.
  309. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  310. {
  311. public:
  312. LabelKeyboardFocusTraverser() {}
  313. Component* getNextComponent (Component* c) override { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  314. Component* getPreviousComponent (Component* c) override { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  315. static Component* getComp (Component* current)
  316. {
  317. return dynamic_cast<TextEditor*> (current) != nullptr
  318. ? current->getParentComponent() : current;
  319. }
  320. };
  321. KeyboardFocusTraverser* Label::createFocusTraverser()
  322. {
  323. return new LabelKeyboardFocusTraverser();
  324. }
  325. //==============================================================================
  326. void Label::addListener (Label::Listener* l) { listeners.add (l); }
  327. void Label::removeListener (Label::Listener* l) { listeners.remove (l); }
  328. void Label::callChangeListeners()
  329. {
  330. Component::BailOutChecker checker (this);
  331. listeners.callChecked (checker, [this] (Listener& l) { l.labelTextChanged (this); });
  332. if (checker.shouldBailOut())
  333. return;
  334. if (onTextChange != nullptr)
  335. onTextChange();
  336. }
  337. //==============================================================================
  338. void Label::textEditorTextChanged (TextEditor& ed)
  339. {
  340. if (editor != nullptr)
  341. {
  342. jassert (&ed == editor.get());
  343. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  344. {
  345. if (lossOfFocusDiscardsChanges)
  346. textEditorEscapeKeyPressed (ed);
  347. else
  348. textEditorReturnKeyPressed (ed);
  349. }
  350. }
  351. }
  352. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  353. {
  354. if (editor != nullptr)
  355. {
  356. jassert (&ed == editor.get());
  357. WeakReference<Component> deletionChecker (this);
  358. bool changed = updateFromTextEditorContents (ed);
  359. hideEditor (true);
  360. if (changed && deletionChecker != nullptr)
  361. {
  362. textWasEdited();
  363. if (deletionChecker != nullptr)
  364. callChangeListeners();
  365. }
  366. }
  367. }
  368. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  369. {
  370. if (editor != nullptr)
  371. {
  372. jassert (&ed == editor.get());
  373. ignoreUnused (ed);
  374. editor->setText (textValue.toString(), false);
  375. hideEditor (true);
  376. }
  377. }
  378. void Label::textEditorFocusLost (TextEditor& ed)
  379. {
  380. textEditorTextChanged (ed);
  381. }
  382. } // namespace juce