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.

483 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 f = getLookAndFeel().getLabelFont (*this);
  127. if (leftOfOwnerComp)
  128. {
  129. auto width = jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f)
  130. + getBorderSize().getLeftAndRight(),
  131. component.getX());
  132. setBounds (component.getX() - width, component.getY(), width, component.getHeight());
  133. }
  134. else
  135. {
  136. auto height = getBorderSize().getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f);
  137. setBounds (component.getX(), component.getY() - height, component.getWidth(), height);
  138. }
  139. }
  140. void Label::componentParentHierarchyChanged (Component& component)
  141. {
  142. if (auto* parent = component.getParentComponent())
  143. parent->addChildComponent (this);
  144. }
  145. void Label::componentVisibilityChanged (Component& component)
  146. {
  147. setVisible (component.isVisible());
  148. }
  149. //==============================================================================
  150. void Label::textWasEdited() {}
  151. void Label::textWasChanged() {}
  152. void Label::editorShown (TextEditor* textEditor)
  153. {
  154. Component::BailOutChecker checker (this);
  155. listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorShown (this, *textEditor); });
  156. if (checker.shouldBailOut())
  157. return;
  158. if (onEditorShow != nullptr)
  159. onEditorShow();
  160. }
  161. void Label::editorAboutToBeHidden (TextEditor* textEditor)
  162. {
  163. if (auto* peer = getPeer())
  164. peer->dismissPendingTextInput();
  165. Component::BailOutChecker checker (this);
  166. listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorHidden (this, *textEditor); });
  167. if (checker.shouldBailOut())
  168. return;
  169. if (onEditorHide != nullptr)
  170. onEditorHide();
  171. }
  172. void Label::showEditor()
  173. {
  174. if (editor == nullptr)
  175. {
  176. editor.reset (createEditorComponent());
  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. repaint();
  219. if (changed)
  220. textWasEdited();
  221. if (deletionChecker != nullptr)
  222. exitModalState (0);
  223. if (changed && deletionChecker != nullptr)
  224. callChangeListeners();
  225. }
  226. }
  227. void Label::inputAttemptWhenModal()
  228. {
  229. if (editor != nullptr)
  230. {
  231. if (lossOfFocusDiscardsChanges)
  232. textEditorEscapeKeyPressed (*editor);
  233. else
  234. textEditorReturnKeyPressed (*editor);
  235. }
  236. }
  237. bool Label::isBeingEdited() const noexcept
  238. {
  239. return editor != nullptr;
  240. }
  241. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  242. {
  243. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  244. ed.setColour (targetColourID, l.findColour (colourID));
  245. }
  246. TextEditor* Label::createEditorComponent()
  247. {
  248. auto* ed = new TextEditor (getName());
  249. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  250. copyAllExplicitColoursTo (*ed);
  251. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  252. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  253. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::focusedOutlineColourId);
  254. return ed;
  255. }
  256. TextEditor* Label::getCurrentTextEditor() const noexcept
  257. {
  258. return editor.get();
  259. }
  260. //==============================================================================
  261. void Label::paint (Graphics& g)
  262. {
  263. getLookAndFeel().drawLabel (g, *this);
  264. }
  265. void Label::mouseUp (const MouseEvent& e)
  266. {
  267. if (editSingleClick
  268. && isEnabled()
  269. && contains (e.getPosition())
  270. && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  271. {
  272. showEditor();
  273. }
  274. }
  275. void Label::mouseDoubleClick (const MouseEvent& e)
  276. {
  277. if (editDoubleClick
  278. && isEnabled()
  279. && ! e.mods.isPopupMenu())
  280. showEditor();
  281. }
  282. void Label::resized()
  283. {
  284. if (editor != nullptr)
  285. editor->setBounds (getLocalBounds());
  286. }
  287. void Label::focusGained (FocusChangeType cause)
  288. {
  289. if (editSingleClick
  290. && isEnabled()
  291. && cause == focusChangedByTabKey)
  292. showEditor();
  293. }
  294. void Label::enablementChanged()
  295. {
  296. repaint();
  297. }
  298. void Label::colourChanged()
  299. {
  300. repaint();
  301. }
  302. void Label::setMinimumHorizontalScale (const float newScale)
  303. {
  304. if (minimumHorizontalScale != newScale)
  305. {
  306. minimumHorizontalScale = newScale;
  307. repaint();
  308. }
  309. }
  310. //==============================================================================
  311. // We'll use a custom focus traverser here to make sure focus goes from the
  312. // text editor to another component rather than back to the label itself.
  313. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  314. {
  315. public:
  316. LabelKeyboardFocusTraverser() {}
  317. Component* getNextComponent (Component* c) override { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  318. Component* getPreviousComponent (Component* c) override { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  319. static Component* getComp (Component* current)
  320. {
  321. return dynamic_cast<TextEditor*> (current) != nullptr
  322. ? current->getParentComponent() : current;
  323. }
  324. };
  325. KeyboardFocusTraverser* Label::createFocusTraverser()
  326. {
  327. return new LabelKeyboardFocusTraverser();
  328. }
  329. //==============================================================================
  330. void Label::addListener (Label::Listener* l) { listeners.add (l); }
  331. void Label::removeListener (Label::Listener* l) { listeners.remove (l); }
  332. void Label::callChangeListeners()
  333. {
  334. Component::BailOutChecker checker (this);
  335. listeners.callChecked (checker, [this] (Listener& l) { l.labelTextChanged (this); });
  336. if (checker.shouldBailOut())
  337. return;
  338. if (onTextChange != nullptr)
  339. onTextChange();
  340. }
  341. //==============================================================================
  342. void Label::textEditorTextChanged (TextEditor& ed)
  343. {
  344. if (editor != nullptr)
  345. {
  346. jassert (&ed == editor.get());
  347. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  348. {
  349. if (lossOfFocusDiscardsChanges)
  350. textEditorEscapeKeyPressed (ed);
  351. else
  352. textEditorReturnKeyPressed (ed);
  353. }
  354. }
  355. }
  356. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  357. {
  358. if (editor != nullptr)
  359. {
  360. jassert (&ed == editor.get());
  361. WeakReference<Component> deletionChecker (this);
  362. bool changed = updateFromTextEditorContents (ed);
  363. hideEditor (true);
  364. if (changed && deletionChecker != nullptr)
  365. {
  366. textWasEdited();
  367. if (deletionChecker != nullptr)
  368. callChangeListeners();
  369. }
  370. }
  371. }
  372. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  373. {
  374. if (editor != nullptr)
  375. {
  376. jassert (&ed == editor.get());
  377. ignoreUnused (ed);
  378. editor->setText (textValue.toString(), false);
  379. hideEditor (true);
  380. }
  381. }
  382. void Label::textEditorFocusLost (TextEditor& ed)
  383. {
  384. textEditorTextChanged (ed);
  385. }
  386. } // namespace juce