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.

485 lines
13KB

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