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.

482 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 static_cast<Component*> (ownerComponent);
  108. }
  109. void Label::attachToComponent (Component* owner, bool onLeft)
  110. {
  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 f = getLookAndFeel().getLabelFont (*this);
  126. if (leftOfOwnerComp)
  127. {
  128. auto width = jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f)
  129. + getBorderSize().getLeftAndRight(),
  130. component.getX());
  131. setBounds (component.getX() - width, component.getY(), width, component.getHeight());
  132. }
  133. else
  134. {
  135. auto height = getBorderSize().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. addAndMakeVisible (editor.get());
  177. editor->setText (getText(), false);
  178. editor->setKeyboardType (keyboardType);
  179. editor->addListener (this);
  180. editor->grabKeyboardFocus();
  181. if (editor == nullptr) // may be deleted by a callback
  182. return;
  183. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  184. resized();
  185. repaint();
  186. editorShown (editor.get());
  187. enterModalState (false);
  188. editor->grabKeyboardFocus();
  189. }
  190. }
  191. bool Label::updateFromTextEditorContents (TextEditor& ed)
  192. {
  193. auto newText = ed.getText();
  194. if (textValue.toString() != newText)
  195. {
  196. lastTextValue = newText;
  197. textValue = newText;
  198. repaint();
  199. textWasChanged();
  200. if (ownerComponent != nullptr)
  201. componentMovedOrResized (*ownerComponent, true, true);
  202. return true;
  203. }
  204. return false;
  205. }
  206. void Label::hideEditor (bool discardCurrentEditorContents)
  207. {
  208. if (editor != nullptr)
  209. {
  210. WeakReference<Component> deletionChecker (this);
  211. std::unique_ptr<TextEditor> outgoingEditor;
  212. std::swap (outgoingEditor, editor);
  213. editorAboutToBeHidden (outgoingEditor.get());
  214. const bool changed = (! discardCurrentEditorContents)
  215. && updateFromTextEditorContents (*outgoingEditor);
  216. outgoingEditor.reset();
  217. repaint();
  218. if (changed)
  219. textWasEdited();
  220. if (deletionChecker != nullptr)
  221. exitModalState (0);
  222. if (changed && deletionChecker != nullptr)
  223. callChangeListeners();
  224. }
  225. }
  226. void Label::inputAttemptWhenModal()
  227. {
  228. if (editor != nullptr)
  229. {
  230. if (lossOfFocusDiscardsChanges)
  231. textEditorEscapeKeyPressed (*editor);
  232. else
  233. textEditorReturnKeyPressed (*editor);
  234. }
  235. }
  236. bool Label::isBeingEdited() const noexcept
  237. {
  238. return editor != nullptr;
  239. }
  240. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  241. {
  242. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  243. ed.setColour (targetColourID, l.findColour (colourID));
  244. }
  245. TextEditor* Label::createEditorComponent()
  246. {
  247. auto* ed = new TextEditor (getName());
  248. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  249. copyAllExplicitColoursTo (*ed);
  250. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  251. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  252. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::focusedOutlineColourId);
  253. return ed;
  254. }
  255. TextEditor* Label::getCurrentTextEditor() const noexcept
  256. {
  257. return editor.get();
  258. }
  259. //==============================================================================
  260. void Label::paint (Graphics& g)
  261. {
  262. getLookAndFeel().drawLabel (g, *this);
  263. }
  264. void Label::mouseUp (const MouseEvent& e)
  265. {
  266. if (editSingleClick
  267. && isEnabled()
  268. && contains (e.getPosition())
  269. && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  270. {
  271. showEditor();
  272. }
  273. }
  274. void Label::mouseDoubleClick (const MouseEvent& e)
  275. {
  276. if (editDoubleClick
  277. && isEnabled()
  278. && ! e.mods.isPopupMenu())
  279. showEditor();
  280. }
  281. void Label::resized()
  282. {
  283. if (editor != nullptr)
  284. editor->setBounds (getLocalBounds());
  285. }
  286. void Label::focusGained (FocusChangeType cause)
  287. {
  288. if (editSingleClick
  289. && isEnabled()
  290. && cause == focusChangedByTabKey)
  291. showEditor();
  292. }
  293. void Label::enablementChanged()
  294. {
  295. repaint();
  296. }
  297. void Label::colourChanged()
  298. {
  299. repaint();
  300. }
  301. void Label::setMinimumHorizontalScale (const float newScale)
  302. {
  303. if (minimumHorizontalScale != newScale)
  304. {
  305. minimumHorizontalScale = newScale;
  306. repaint();
  307. }
  308. }
  309. //==============================================================================
  310. // We'll use a custom focus traverser here to make sure focus goes from the
  311. // text editor to another component rather than back to the label itself.
  312. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  313. {
  314. public:
  315. LabelKeyboardFocusTraverser() {}
  316. Component* getNextComponent (Component* c) override { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  317. Component* getPreviousComponent (Component* c) override { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  318. static Component* getComp (Component* current)
  319. {
  320. return dynamic_cast<TextEditor*> (current) != nullptr
  321. ? current->getParentComponent() : current;
  322. }
  323. };
  324. KeyboardFocusTraverser* Label::createFocusTraverser()
  325. {
  326. return new LabelKeyboardFocusTraverser();
  327. }
  328. //==============================================================================
  329. void Label::addListener (Label::Listener* l) { listeners.add (l); }
  330. void Label::removeListener (Label::Listener* l) { listeners.remove (l); }
  331. void Label::callChangeListeners()
  332. {
  333. Component::BailOutChecker checker (this);
  334. listeners.callChecked (checker, [this] (Listener& l) { l.labelTextChanged (this); });
  335. if (checker.shouldBailOut())
  336. return;
  337. if (onTextChange != nullptr)
  338. onTextChange();
  339. }
  340. //==============================================================================
  341. void Label::textEditorTextChanged (TextEditor& ed)
  342. {
  343. if (editor != nullptr)
  344. {
  345. jassert (&ed == editor.get());
  346. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  347. {
  348. if (lossOfFocusDiscardsChanges)
  349. textEditorEscapeKeyPressed (ed);
  350. else
  351. textEditorReturnKeyPressed (ed);
  352. }
  353. }
  354. }
  355. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  356. {
  357. if (editor != nullptr)
  358. {
  359. jassert (&ed == editor.get());
  360. WeakReference<Component> deletionChecker (this);
  361. bool changed = updateFromTextEditorContents (ed);
  362. hideEditor (true);
  363. if (changed && deletionChecker != nullptr)
  364. {
  365. textWasEdited();
  366. if (deletionChecker != nullptr)
  367. callChangeListeners();
  368. }
  369. }
  370. }
  371. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  372. {
  373. if (editor != nullptr)
  374. {
  375. jassert (&ed == editor.get());
  376. ignoreUnused (ed);
  377. editor->setText (textValue.toString(), false);
  378. hideEditor (true);
  379. }
  380. }
  381. void Label::textEditorFocusLost (TextEditor& ed)
  382. {
  383. textEditorTextChanged (ed);
  384. }
  385. } // namespace juce