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.

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