Audio plugin host https://kx.studio/carla
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.

juce_Label.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. font (15.0f),
  26. justification (Justification::centredLeft),
  27. border (1, 5, 1, 5),
  28. minimumHorizontalScale (0.0f),
  29. keyboardType (TextEditor::textKeyboard),
  30. editSingleClick (false),
  31. editDoubleClick (false),
  32. lossOfFocusDiscardsChanges (false)
  33. {
  34. setColour (TextEditor::textColourId, Colours::black);
  35. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  36. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  37. textValue.addListener (this);
  38. }
  39. Label::~Label()
  40. {
  41. textValue.removeListener (this);
  42. if (ownerComponent != nullptr)
  43. ownerComponent->removeComponentListener (this);
  44. editor = nullptr;
  45. }
  46. //==============================================================================
  47. void Label::setText (const String& newText,
  48. const NotificationType notification)
  49. {
  50. hideEditor (true);
  51. if (lastTextValue != newText)
  52. {
  53. lastTextValue = newText;
  54. textValue = newText;
  55. repaint();
  56. textWasChanged();
  57. if (ownerComponent != nullptr)
  58. componentMovedOrResized (*ownerComponent, true, true);
  59. if (notification != dontSendNotification)
  60. callChangeListeners();
  61. }
  62. }
  63. String Label::getText (const bool returnActiveEditorContents) const
  64. {
  65. return (returnActiveEditorContents && isBeingEdited())
  66. ? editor->getText()
  67. : textValue.toString();
  68. }
  69. void Label::valueChanged (Value&)
  70. {
  71. if (lastTextValue != textValue.toString())
  72. setText (textValue.toString(), sendNotification);
  73. }
  74. //==============================================================================
  75. void Label::setFont (const Font& newFont)
  76. {
  77. if (font != newFont)
  78. {
  79. font = newFont;
  80. repaint();
  81. }
  82. }
  83. Font Label::getFont() const noexcept
  84. {
  85. return font;
  86. }
  87. void Label::setEditable (const bool editOnSingleClick,
  88. const bool editOnDoubleClick,
  89. const bool lossOfFocusDiscardsChanges_)
  90. {
  91. editSingleClick = editOnSingleClick;
  92. editDoubleClick = editOnDoubleClick;
  93. lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;
  94. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  95. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  96. }
  97. void Label::setJustificationType (Justification newJustification)
  98. {
  99. if (justification != newJustification)
  100. {
  101. justification = newJustification;
  102. repaint();
  103. }
  104. }
  105. void Label::setBorderSize (BorderSize<int> newBorder)
  106. {
  107. if (border != newBorder)
  108. {
  109. border = newBorder;
  110. repaint();
  111. }
  112. }
  113. //==============================================================================
  114. Component* Label::getAttachedComponent() const
  115. {
  116. return static_cast<Component*> (ownerComponent);
  117. }
  118. void Label::attachToComponent (Component* owner, const bool onLeft)
  119. {
  120. if (ownerComponent != nullptr)
  121. ownerComponent->removeComponentListener (this);
  122. ownerComponent = owner;
  123. leftOfOwnerComp = onLeft;
  124. if (ownerComponent != nullptr)
  125. {
  126. setVisible (owner->isVisible());
  127. ownerComponent->addComponentListener (this);
  128. componentParentHierarchyChanged (*ownerComponent);
  129. componentMovedOrResized (*ownerComponent, true, true);
  130. }
  131. }
  132. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  133. {
  134. const Font f (getLookAndFeel().getLabelFont (*this));
  135. if (leftOfOwnerComp)
  136. {
  137. setSize (jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f) + getBorderSize().getLeftAndRight(),
  138. component.getX()),
  139. component.getHeight());
  140. setTopRightPosition (component.getX(), component.getY());
  141. }
  142. else
  143. {
  144. setSize (component.getWidth(),
  145. getBorderSize().getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f));
  146. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  147. }
  148. }
  149. void Label::componentParentHierarchyChanged (Component& component)
  150. {
  151. if (Component* parent = component.getParentComponent())
  152. parent->addChildComponent (this);
  153. }
  154. void Label::componentVisibilityChanged (Component& component)
  155. {
  156. setVisible (component.isVisible());
  157. }
  158. //==============================================================================
  159. void Label::textWasEdited() {}
  160. void Label::textWasChanged() {}
  161. void Label::editorShown (TextEditor* textEditor)
  162. {
  163. Component::BailOutChecker checker (this);
  164. listeners.callChecked (checker, &LabelListener::editorShown, this, *textEditor);
  165. }
  166. void Label::editorAboutToBeHidden (TextEditor* textEditor)
  167. {
  168. if (ComponentPeer* const peer = getPeer())
  169. peer->dismissPendingTextInput();
  170. Component::BailOutChecker checker (this);
  171. listeners.callChecked (checker, &LabelListener::editorHidden, this, *textEditor);
  172. }
  173. void Label::showEditor()
  174. {
  175. if (editor == nullptr)
  176. {
  177. addAndMakeVisible (editor = createEditorComponent());
  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);
  188. enterModalState (false);
  189. editor->grabKeyboardFocus();
  190. }
  191. }
  192. bool Label::updateFromTextEditorContents (TextEditor& ed)
  193. {
  194. const String 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 (const bool discardCurrentEditorContents)
  208. {
  209. if (editor != nullptr)
  210. {
  211. WeakReference<Component> deletionChecker (this);
  212. ScopedPointer<TextEditor> outgoingEditor (editor);
  213. editorAboutToBeHidden (outgoingEditor);
  214. const bool changed = (! discardCurrentEditorContents)
  215. && updateFromTextEditorContents (*outgoingEditor);
  216. outgoingEditor = nullptr;
  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. TextEditor* const 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;
  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) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  317. Component* getPreviousComponent (Component* c) { 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 (LabelListener* const listener)
  330. {
  331. listeners.add (listener);
  332. }
  333. void Label::removeListener (LabelListener* const listener)
  334. {
  335. listeners.remove (listener);
  336. }
  337. void Label::callChangeListeners()
  338. {
  339. Component::BailOutChecker checker (this);
  340. listeners.callChecked (checker, &Label::Listener::labelTextChanged, this);
  341. }
  342. //==============================================================================
  343. void Label::textEditorTextChanged (TextEditor& ed)
  344. {
  345. if (editor != nullptr)
  346. {
  347. jassert (&ed == editor);
  348. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  349. {
  350. if (lossOfFocusDiscardsChanges)
  351. textEditorEscapeKeyPressed (ed);
  352. else
  353. textEditorReturnKeyPressed (ed);
  354. }
  355. }
  356. }
  357. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  358. {
  359. if (editor != nullptr)
  360. {
  361. jassert (&ed == editor);
  362. const bool changed = updateFromTextEditorContents (ed);
  363. hideEditor (true);
  364. if (changed)
  365. {
  366. WeakReference<Component> deletionChecker (this);
  367. textWasEdited();
  368. if (deletionChecker != nullptr)
  369. callChangeListeners();
  370. }
  371. }
  372. }
  373. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  374. {
  375. if (editor != nullptr)
  376. {
  377. jassert (&ed == editor);
  378. ignoreUnused (ed);
  379. editor->setText (textValue.toString(), false);
  380. hideEditor (true);
  381. }
  382. }
  383. void Label::textEditorFocusLost (TextEditor& ed)
  384. {
  385. textEditorTextChanged (ed);
  386. }
  387. } // namespace juce