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.

462 lines
12KB

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