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

10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
8 years ago
9 years ago
10 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. Label::Label (const String& name, const String& labelText)
  20. : Component (name),
  21. textValue (labelText),
  22. lastTextValue (labelText),
  23. font (15.0f),
  24. justification (Justification::centredLeft),
  25. border (1, 5, 1, 5),
  26. minimumHorizontalScale (0.0f),
  27. keyboardType (TextEditor::textKeyboard),
  28. editSingleClick (false),
  29. editDoubleClick (false),
  30. lossOfFocusDiscardsChanges (false)
  31. {
  32. setColour (TextEditor::textColourId, Colours::black);
  33. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  34. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  35. textValue.addListener (this);
  36. }
  37. Label::~Label()
  38. {
  39. textValue.removeListener (this);
  40. if (ownerComponent != nullptr)
  41. ownerComponent->removeComponentListener (this);
  42. editor = nullptr;
  43. }
  44. //==============================================================================
  45. void Label::setText (const String& newText,
  46. const NotificationType notification)
  47. {
  48. hideEditor (true);
  49. if (lastTextValue != newText)
  50. {
  51. lastTextValue = newText;
  52. textValue = newText;
  53. repaint();
  54. textWasChanged();
  55. if (ownerComponent != nullptr)
  56. componentMovedOrResized (*ownerComponent, true, true);
  57. if (notification != dontSendNotification)
  58. callChangeListeners();
  59. }
  60. }
  61. String Label::getText (const bool returnActiveEditorContents) const
  62. {
  63. return (returnActiveEditorContents && isBeingEdited())
  64. ? editor->getText()
  65. : textValue.toString();
  66. }
  67. void Label::valueChanged (Value&)
  68. {
  69. if (lastTextValue != textValue.toString())
  70. setText (textValue.toString(), sendNotification);
  71. }
  72. //==============================================================================
  73. void Label::setFont (const Font& newFont)
  74. {
  75. if (font != newFont)
  76. {
  77. font = newFont;
  78. repaint();
  79. }
  80. }
  81. Font Label::getFont() const noexcept
  82. {
  83. return font;
  84. }
  85. void Label::setEditable (const bool editOnSingleClick,
  86. const bool editOnDoubleClick,
  87. const bool lossOfFocusDiscardsChanges_)
  88. {
  89. editSingleClick = editOnSingleClick;
  90. editDoubleClick = editOnDoubleClick;
  91. lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;
  92. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  93. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  94. }
  95. void Label::setJustificationType (Justification newJustification)
  96. {
  97. if (justification != newJustification)
  98. {
  99. justification = newJustification;
  100. repaint();
  101. }
  102. }
  103. void Label::setBorderSize (BorderSize<int> newBorder)
  104. {
  105. if (border != newBorder)
  106. {
  107. border = newBorder;
  108. repaint();
  109. }
  110. }
  111. //==============================================================================
  112. Component* Label::getAttachedComponent() const
  113. {
  114. return static_cast<Component*> (ownerComponent);
  115. }
  116. void Label::attachToComponent (Component* owner, const bool onLeft)
  117. {
  118. if (ownerComponent != nullptr)
  119. ownerComponent->removeComponentListener (this);
  120. ownerComponent = owner;
  121. leftOfOwnerComp = onLeft;
  122. if (ownerComponent != nullptr)
  123. {
  124. setVisible (owner->isVisible());
  125. ownerComponent->addComponentListener (this);
  126. componentParentHierarchyChanged (*ownerComponent);
  127. componentMovedOrResized (*ownerComponent, true, true);
  128. }
  129. }
  130. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  131. {
  132. const Font f (getLookAndFeel().getLabelFont (*this));
  133. if (leftOfOwnerComp)
  134. {
  135. setSize (jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f) + getBorderSize().getLeftAndRight(),
  136. component.getX()),
  137. component.getHeight());
  138. setTopRightPosition (component.getX(), component.getY());
  139. }
  140. else
  141. {
  142. setSize (component.getWidth(),
  143. getBorderSize().getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f));
  144. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  145. }
  146. }
  147. void Label::componentParentHierarchyChanged (Component& component)
  148. {
  149. if (Component* parent = component.getParentComponent())
  150. parent->addChildComponent (this);
  151. }
  152. void Label::componentVisibilityChanged (Component& component)
  153. {
  154. setVisible (component.isVisible());
  155. }
  156. //==============================================================================
  157. void Label::textWasEdited() {}
  158. void Label::textWasChanged() {}
  159. void Label::editorShown (TextEditor* textEditor)
  160. {
  161. Component::BailOutChecker checker (this);
  162. listeners.callChecked (checker, &LabelListener::editorShown, this, *textEditor);
  163. }
  164. void Label::editorAboutToBeHidden (TextEditor* textEditor)
  165. {
  166. if (ComponentPeer* const peer = getPeer())
  167. peer->dismissPendingTextInput();
  168. Component::BailOutChecker checker (this);
  169. listeners.callChecked (checker, &LabelListener::editorHidden, this, *textEditor);
  170. }
  171. void Label::showEditor()
  172. {
  173. if (editor == nullptr)
  174. {
  175. addAndMakeVisible (editor = createEditorComponent());
  176. editor->setText (getText(), false);
  177. editor->setKeyboardType (keyboardType);
  178. editor->addListener (this);
  179. editor->grabKeyboardFocus();
  180. if (editor == nullptr) // may be deleted by a callback
  181. return;
  182. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  183. resized();
  184. repaint();
  185. editorShown (editor);
  186. enterModalState (false);
  187. editor->grabKeyboardFocus();
  188. }
  189. }
  190. bool Label::updateFromTextEditorContents (TextEditor& ed)
  191. {
  192. const String newText (ed.getText());
  193. if (textValue.toString() != newText)
  194. {
  195. lastTextValue = newText;
  196. textValue = newText;
  197. repaint();
  198. textWasChanged();
  199. if (ownerComponent != nullptr)
  200. componentMovedOrResized (*ownerComponent, true, true);
  201. return true;
  202. }
  203. return false;
  204. }
  205. void Label::hideEditor (const bool discardCurrentEditorContents)
  206. {
  207. if (editor != nullptr)
  208. {
  209. WeakReference<Component> deletionChecker (this);
  210. ScopedPointer<TextEditor> outgoingEditor (editor);
  211. editorAboutToBeHidden (outgoingEditor);
  212. const bool changed = (! discardCurrentEditorContents)
  213. && updateFromTextEditorContents (*outgoingEditor);
  214. outgoingEditor = nullptr;
  215. repaint();
  216. if (changed)
  217. textWasEdited();
  218. if (deletionChecker != nullptr)
  219. exitModalState (0);
  220. if (changed && deletionChecker != nullptr)
  221. callChangeListeners();
  222. }
  223. }
  224. void Label::inputAttemptWhenModal()
  225. {
  226. if (editor != nullptr)
  227. {
  228. if (lossOfFocusDiscardsChanges)
  229. textEditorEscapeKeyPressed (*editor);
  230. else
  231. textEditorReturnKeyPressed (*editor);
  232. }
  233. }
  234. bool Label::isBeingEdited() const noexcept
  235. {
  236. return editor != nullptr;
  237. }
  238. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  239. {
  240. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  241. ed.setColour (targetColourID, l.findColour (colourID));
  242. }
  243. TextEditor* Label::createEditorComponent()
  244. {
  245. TextEditor* const ed = new TextEditor (getName());
  246. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  247. copyAllExplicitColoursTo (*ed);
  248. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  249. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  250. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::focusedOutlineColourId);
  251. return ed;
  252. }
  253. TextEditor* Label::getCurrentTextEditor() const noexcept
  254. {
  255. return editor;
  256. }
  257. //==============================================================================
  258. void Label::paint (Graphics& g)
  259. {
  260. getLookAndFeel().drawLabel (g, *this);
  261. }
  262. void Label::mouseUp (const MouseEvent& e)
  263. {
  264. if (editSingleClick
  265. && isEnabled()
  266. && contains (e.getPosition())
  267. && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  268. {
  269. showEditor();
  270. }
  271. }
  272. void Label::mouseDoubleClick (const MouseEvent& e)
  273. {
  274. if (editDoubleClick
  275. && isEnabled()
  276. && ! e.mods.isPopupMenu())
  277. showEditor();
  278. }
  279. void Label::resized()
  280. {
  281. if (editor != nullptr)
  282. editor->setBounds (getLocalBounds());
  283. }
  284. void Label::focusGained (FocusChangeType cause)
  285. {
  286. if (editSingleClick
  287. && isEnabled()
  288. && cause == focusChangedByTabKey)
  289. showEditor();
  290. }
  291. void Label::enablementChanged()
  292. {
  293. repaint();
  294. }
  295. void Label::colourChanged()
  296. {
  297. repaint();
  298. }
  299. void Label::setMinimumHorizontalScale (const float newScale)
  300. {
  301. if (minimumHorizontalScale != newScale)
  302. {
  303. minimumHorizontalScale = newScale;
  304. repaint();
  305. }
  306. }
  307. //==============================================================================
  308. // We'll use a custom focus traverser here to make sure focus goes from the
  309. // text editor to another component rather than back to the label itself.
  310. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  311. {
  312. public:
  313. LabelKeyboardFocusTraverser() {}
  314. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  315. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  316. static Component* getComp (Component* current)
  317. {
  318. return dynamic_cast<TextEditor*> (current) != nullptr
  319. ? current->getParentComponent() : current;
  320. }
  321. };
  322. KeyboardFocusTraverser* Label::createFocusTraverser()
  323. {
  324. return new LabelKeyboardFocusTraverser();
  325. }
  326. //==============================================================================
  327. void Label::addListener (LabelListener* const listener)
  328. {
  329. listeners.add (listener);
  330. }
  331. void Label::removeListener (LabelListener* const listener)
  332. {
  333. listeners.remove (listener);
  334. }
  335. void Label::callChangeListeners()
  336. {
  337. Component::BailOutChecker checker (this);
  338. listeners.callChecked (checker, &Label::Listener::labelTextChanged, this);
  339. }
  340. //==============================================================================
  341. void Label::textEditorTextChanged (TextEditor& ed)
  342. {
  343. if (editor != nullptr)
  344. {
  345. jassert (&ed == editor);
  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);
  360. const bool changed = updateFromTextEditorContents (ed);
  361. hideEditor (true);
  362. if (changed)
  363. {
  364. WeakReference<Component> deletionChecker (this);
  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);
  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. }